Commit graph

8962 commits

Author SHA1 Message Date
Peter aed31beca9
gh-120811: Fix reference leak upon _PyContext_Exit failure (#120812)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2024-06-21 17:28:19 +00:00
Petr Viktorin 6f1d448bc1
gh-113993: Allow interned strings to be mortal, and fix related issues (GH-120520)
* Add an InternalDocs file describing how interning should work and how to use it.

* Add internal functions to *explicitly* request what kind of interning is done:
  - `_PyUnicode_InternMortal`
  - `_PyUnicode_InternImmortal`
  - `_PyUnicode_InternStatic`

* Switch uses of `PyUnicode_InternInPlace` to those.

* Disallow using `_Py_SetImmortal` on strings directly.
  You should use `_PyUnicode_InternImmortal` instead:
  - Strings should be interned before immortalization, otherwise you're possibly
    interning a immortalizing copy.
  - `_Py_SetImmortal` doesn't handle the `SSTATE_INTERNED_MORTAL` to
    `SSTATE_INTERNED_IMMORTAL` update, and those flags can't be changed in
    backports, as they are now part of public API and version-specific ABI.

* Add private `_only_immortal` argument for `sys.getunicodeinternedsize`, used in refleak test machinery.

* Make sure the statically allocated string singletons are unique. This means these sets are now disjoint:
  - `_Py_ID`
  - `_Py_STR` (including the empty string)
  - one-character latin-1 singletons

  Now, when you intern a singleton, that exact singleton will be interned.

* Add a `_Py_LATIN1_CHR` macro, use it instead of `_Py_ID`/`_Py_STR` for one-character latin-1 singletons everywhere (including Clinic).

* Intern `_Py_STR` singletons at startup.

* For free-threaded builds, intern `_Py_LATIN1_CHR` singletons at startup.

* Beef up the tests. Cover internal details (marked with `@cpython_only`).

* Add lots of assertions

Co-Authored-By: Eric Snow <ericsnowcurrently@gmail.com>
2024-06-21 17:19:31 +02:00
Victor Stinner 56657f6be2
gh-119182: Use public PyUnicodeWriter in contextvar_tp_repr() (#120809)
The public PyUnicodeWriter API enables overallocation by default and
so is more efficient. It also makes the code simpler and shorter.
2024-06-20 19:56:47 +00:00
Sam Gross 3af7263037
gh-117511: Make PyMutex public in the non-limited API (#117731) 2024-06-20 11:29:08 -04:00
Irit Katriel 55596ae044
gh-98442: fix locations of with statement's cleanup instructions (#120763)
gh-98442: fix location of with statement's cleanup instructions
2024-06-20 09:32:06 +01:00
Xie Yanbo 656a1c8108
Fix typos in comments (#120481) 2024-06-19 23:16:14 -04:00
Nadeshiko Manju f385d99f57
gh-120437: Fix _CHECK_STACK_SPACE optimization problems introduced in gh-118322 (GH-120712)
Co-authored-by: Ken Jin <kenjin4096@gmail.com>
2024-06-19 23:34:39 +08:00
Irit Katriel eaaf6995a8
gh-120733: rename internal compiler functions according to naming convention (#120734) 2024-06-19 14:19:59 +01:00
Jelle Zijlstra d8f27cb114
gh-120722: Set position on RETURN_VALUE in lambda (#120724) 2024-06-19 05:56:36 -07:00
Kirill Podoprigora a816cd67f4
gh-120726: Fix compiler warnings on is_core_module() (#120727)
Fix compiler warnings on is_core_module() and
check_interpreter_whence(): only define them when
assertions are built.
2024-06-19 07:46:04 +00:00
Irit Katriel b7f478948f
gh-120367: fix bug where compiler detects redundant jump after pseudo op replacement (#120714) 2024-06-18 22:09:23 +00:00
devdanzin c81a5e6b5b
gh-119574: Add some missing environment variables to '--help-env'. (GH-120006) 2024-06-18 17:12:58 +00:00
Mark Shannon 9cefcc0ee7
GH-120507: Lower the BEFORE_WITH and BEFORE_ASYNC_WITH instructions. (#120640)
* Remove BEFORE_WITH and BEFORE_ASYNC_WITH instructions.

* Add LOAD_SPECIAL instruction

* Reimplement `with` and `async with` statements using LOAD_SPECIAL
2024-06-18 12:17:46 +01:00
Daniele Parmeggiani 362cd2680b
gh-117657: Fix __slots__ thread safety in free-threaded build (#119368)
Fix a race in `PyMember_GetOne` and `PyMember_SetOne` for `Py_T_OBJECT_EX`.
These functions implement `__slots__` accesses for Python objects.
2024-06-17 18:44:54 +00:00
Sam Gross 460cc9e14e
gh-117657: Fix TSan reported data race on ioctl_works (#120175) 2024-06-17 13:23:40 -04:00
AN Long 28140d1f2d
gh-115649: Copy the filename into main interpreter before intern in import.c (#120315)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2024-06-17 21:27:22 +05:30
Bénédikt Tran 4bf17c381f
gh-119933: Improve `SyntaxError` message for invalid type parameters expressions (#119976)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2024-06-17 06:51:03 -07:00
Mark Shannon 274f844830
GH-120619: Clean up RETURN_VALUE instruction (GH-120624)
* Rename _POP_FRAME to _RETURN_VALUE as it returns a value as well as popping a frame.

* Remove remaining _POP_FRAMEs
2024-06-17 14:40:11 +01:00
Irit Katriel 21866c8ed2
gh-120367: fix removal of redundant NOPs and jumps after reordering hot-cold blocks (#120425) 2024-06-17 10:10:06 +00:00
Victor Stinner c2d5df5787
gh-83754: Use the Py_TYPE() macro (#120599)
Don't access directly PyObject.ob_type, but use the Py_TYPE() macro
instead.
2024-06-17 10:34:29 +02:00
Carson Radtke 92cebaa491
gh-120568: fix file leak in PyUnstable_CopyPerfMapFile (#120569) 2024-06-16 17:51:52 +00:00
Adam Williamson d4039d3f6f
gh-120526: Correct signature of map() builtin (GH-120528)
map() requires at least one iterable arg.

Signed-off-by: Adam Williamson <awilliam@redhat.com>
2024-06-15 05:33:09 +00:00
Eric Snow b2e71ff4f8
gh-120161: Fix a Crash in the _datetime Module (gh-120182)
In gh-120009 I used an atexit hook to finalize the _datetime module's static types at interpreter shutdown.  However, atexit hooks are executed very early in finalization, which is a problem in the few cases where a subclass of one of those static types is still alive until the final GC collection.  The static builtin types don't have this probably because they are finalized toward the end, after the final GC collection.  To avoid the problem for _datetime, I have applied a similar approach here.

Also, credit goes to @mgorny and @neonene for the new tests.

FYI, I would have liked to take a slightly cleaner approach with managed static types, but wanted to get a smaller fix in first for the sake of backporting.  I'll circle back to the cleaner approach with a future change on the main branch.
2024-06-14 13:29:09 -06:00
Ken Jin b1b61dc4ce
gh-117657: Fix some simple races in instrumentation.c (GH-120118)
* stop the world when setting local events
2024-06-13 17:31:21 +08:00
Lysandros Nikolaou 4b5d3e0e72
gh-120343: Fix column offsets of multiline tokens in tokenize (#120391) 2024-06-12 20:52:55 +02:00
ixgbe00 4b1e85bafc
gh-120400 :Support Linux perf profile to see Python calls on RISC-V architecture (#120089)
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
2024-06-12 14:24:46 +01:00
Bénédikt Tran 32d3e05fe6
gh-120029: remove unused macros in `symtable.c` (#120222)
Co-authored-by: Carl Meyer <carl@oddbird.net>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2024-06-12 09:23:45 -04:00
Irit Katriel 97b69db167
gh-93691: fix too broad source locations of for statement iterators (#120330) 2024-06-12 12:53:19 +01:00
Kirill Podoprigora 19435d299a
gh-120385: Fix reference leak in symtable (#120386)
Decref 'new_ste' if symtable_visit_annotations() fails.
2024-06-12 09:37:14 +02:00
Lysandros Nikolaou 1b62bcee94
gh-120343: Do not reset byte_col_offset_diff after multiline tokens (#120352)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
2024-06-11 17:00:53 +00:00
Jelle Zijlstra 9b8611eeea
gh-119180: PEP 649 compiler changes (#119361) 2024-06-11 13:06:49 +00:00
Carl Meyer 0ae8579b85
gh-119666: fix multiple class-scope comprehensions referencing __class__ (#120295) 2024-06-09 22:23:30 -04:00
Saul Shanabrook 55402d3232
gh-119258: Eliminate Type Guards in Tier 2 Optimizer with Watcher (GH-119365)
Co-authored-by: parmeggiani <parmeggiani@spaziodati.eu>
Co-authored-by: dpdani <git@danieleparmeggiani.me>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
Co-authored-by: Ken Jin <kenjin@python.org>
2024-06-08 17:41:45 +08:00
Irit Katriel 4fc82b6d3b
gh-120225: fix crash in compiler on empty block at end of exception handler (#120235) 2024-06-07 22:37:35 +01:00
Irit Katriel eca3f7762c
gh-93691: fix too broad source locations of with-statement instructions (#120125) 2024-06-07 14:06:24 +01:00
Sam Gross e21057b999
gh-117657: Fix TSAN race involving import lock (#118523)
This adds a `_PyRecursiveMutex` type based on `PyMutex` and uses that
for the import lock. This fixes some data races in the free-threaded
build and generally simplifies the import lock code.
2024-06-06 13:40:58 -04:00
Irit Katriel eeb8f67f83
gh-119786: move adaptive interpreter doc from Python to InternalsDoc (#120137) 2024-06-06 10:56:58 +00:00
Sam Gross e69d068ad0
gh-117657: Fix race involving GC and heap initialization (#119923)
The `_PyThreadState_Bind()` function is called before the first
`PyEval_AcquireThread()` so it's not synchronized with the stop the
world GC. We had a race where `gc_visit_heaps()` might visit a thread's
heap while it's being initialized.

Use a simple atomic int to avoid visiting heaps for threads that are not
yet fully initialized (i.e., before `tstate_mimalloc_bind()` is called).

The race was reproducible by running:
`python Lib/test/test_importlib/partial/pool_in_threads.py`.
2024-06-04 09:42:13 -04:00
Xie Yanbo 9e052619a6
Fix typos in documentation and comments (#119763) 2024-06-04 10:22:22 +00:00
Victor Stinner 5a1205b641
gh-111499: Fix PYTHONMALLOCSTATS at Python exit (#120021)
Call _PyObject_DebugMallocStats() earlier in Py_FinalizeEx(), before
the interpreter is deleted.
2024-06-04 11:39:07 +02:00
Eric Snow 105f22ea46
gh-117398: Use Per-Interpreter State for the _datetime Static Types (gh-119929)
We make use of the same mechanism that we use for the static builtin types.  This required a few tweaks.

The relevant code could use some cleanup but I opted to avoid the significant churn in this change.  I'll tackle that separately.

This change is the final piece needed to make _datetime support multiple interpreters.  I've updated the module slot accordingly.
2024-06-03 17:09:18 -06:00
Sam Gross 47fb4327b5
gh-117657: Fix race involving immortalizing objects (#119927)
The free-threaded build currently immortalizes objects that use deferred
reference counting (see gh-117783). This typically happens once the
first non-main thread is created, but the behavior can be suppressed for
tests, in subinterpreters, or during a compile() call.

This fixes a race condition involving the tracking of whether the
behavior is suppressed.
2024-06-03 20:58:41 +00:00
Sam Gross 41c1cefbae
gh-117657: Avoid sem_clockwait in TSAN (#119915)
The `sem_clockwait` function is not currently instrumented, which leads
to false positives.
2024-06-03 13:42:27 -04:00
Bénédikt Tran 153b118b78
gh-119981: Use do while(0) in some symtable.c multi-line macros (#119982) 2024-06-03 15:48:02 +00:00
Sam Gross 90ec19fd33
gh-117657: Fix TSAN race in QSBR assertion (#119887)
Due to a limitation in TSAN, all reads from `PyThreadState.state` must be
atomic to avoid reported races.
2024-06-01 10:04:38 -04:00
Sam Gross 60593b2052
gh-117657: Fix TSAN race in free-threaded GC (#119883)
Only call `gc_restore_tid()` from stop-the-world contexts.
`worklist_pop()` can be called while other threads are running, so use a
relaxed atomic to modify `ob_tid`.
2024-06-01 10:04:05 -04:00
Alyssa Coghlan 3859e09e3d
gh-74929: PEP 667 C API documentation (gh-119379)
* Add docs for new APIs
* Add soft-deprecation notices
* Add What's New porting entries
* Update comments referencing `PyFrame_LocalsToFast()` to mention the proxy instead
* Other related cleanups found when looking for refs to the deprecated APIs
2024-06-01 13:59:35 +10:00
Jelle Zijlstra 80a4e38994
gh-119821: Support non-dict globals in LOAD_FROM_DICT_OR_GLOBALS (#119822)
Support non-dict globals in LOAD_FROM_DICT_OR_GLOBALS

The implementation basically copies LOAD_GLOBAL. Possibly it could be deduplicated,
but that seems like it may get hairy since the two operations have different operands.

This is important to fix in 3.14 for PEP 649, but it's a bug in earlier versions too,
and we should backport to 3.13 and 3.12 if possible.
2024-05-31 14:05:24 -07:00
Sam Gross 078b8c8cf2
gh-119369: Fix deadlock during thread exit in free-threaded build (#119528)
Release the GIL before calling `_Py_qsbr_unregister`.

The deadlock could occur when the GIL was enabled at runtime. The
`_Py_qsbr_unregister` call might block while holding the GIL because the
thread state was not active, but the GIL was still held.
2024-05-31 13:04:59 -04:00
Sam Gross bcc1be39cb
gh-119585: Fix crash involving PyGILState_Release() and PyThreadState_Clear() (#119753)
Make sure that `gilstate_counter` is not zero in when calling
`PyThreadState_Clear()`. A destructor called from `PyThreadState_Clear()` may
call back into `PyGILState_Ensure()` and `PyGILState_Release()`. If
`gilstate_counter` is zero, it will try to create a new thread state before
the current active thread state is destroyed, leading to an assertion failure
or crash.
2024-05-31 10:50:52 -04:00