Commit graph

8805 commits

Author SHA1 Message Date
Mark Shannon
9fbd66a93d
GH-133912: Fix PyObject_GenericSetDict to handle inline values (GH-134725) 2025-05-28 19:03:41 +01:00
Serhiy Storchaka
bac3fcba5b
gh-108512: Add and use new replacements for PySys_GetObject() (GH-111035)
Add functions PySys_GetAttr(), PySys_GetAttrString(),
PySys_GetOptionalAttr() and PySys_GetOptionalAttrString().
2025-05-28 20:11:09 +03:00
Neil Schemenauer
fbbbc10055
gh-127266: avoid data races when updating type slots (gh-133177)
In the free-threaded build, avoid data races caused by updating type
slots or type flags after the type was initially created.  For those
(typically rare) cases, use the stop-the-world mechanism.  Remove the
use of atomics when reading or writing type flags.
2025-05-27 18:27:41 -07:00
Sam Gross
a4d37f88b6
gh-134679: Fix assertion failure in QSBR (gh-134811)
This is the same underlying bug as gh-130519. The destructor may call
arbitrary code, changing the `tstate->qsbr pointer` and invalidating the
old `struct _qsbr_thread_state`.
2025-05-27 16:21:16 -04:00
Kumar Aditya
a380d57873
gh-134043: use stackrefs in vectorcalling methods (#134044)
Adds `_PyObject_GetMethodStackRef` which uses stackrefs and takes advantage of deferred reference counting in free-threading while calling method objects in vectorcall.
2025-05-27 22:28:27 +05:30
Mark Shannon
f6f4e8a662
GH-132554: "Virtual" iterators (GH-132555)
* FOR_ITER now pushes either the iterator and NULL or leaves the iterable and pushes tagged zero

* NEXT_ITER uses the tagged int as the index into the sequence or, if TOS is NULL, iterates as before.
2025-05-27 15:59:45 +01:00
Pablo Galindo Salgado
42b25ad4d3
gh-91048: Refactor and optimize remote debugging module (#134652)
Completely refactor Modules/_remote_debugging_module.c with improved
code organization, replacing scattered reference counting and error
handling with centralized goto error paths. This cleanup improves
maintainability and reduces code duplication throughout the module while
preserving the same external API.

Implement memory page caching optimization in Python/remote_debug.h to
avoid repeated reads of the same memory regions during debugging
operations. The cache stores previously read memory pages and reuses
them for subsequent reads, significantly reducing system calls and
improving performance.

Add code object caching mechanism with a new code_object_generation
field in the interpreter state that tracks when code object caches need
invalidation. This allows efficient reuse of parsed code object metadata
and eliminates redundant processing of the same code objects across
debugging sessions.

Optimize memory operations by replacing multiple individual structure
copies with single bulk reads for the same data structures. This reduces
the number of memory operations and system calls required to gather
debugging information from the target process.

Update Makefile.pre.in to include Python/remote_debug.h in the headers
list, ensuring that changes to the remote debugging header force proper
recompilation of dependent modules and maintain build consistency across
the codebase.

Also, make the module compatible with the free threading build as an extra :)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2025-05-25 20:19:29 +00:00
Jelle Zijlstra
4443110c34
gh-133778: Fix setting __annotations__ under PEP 563 (#133794) 2025-05-25 08:38:18 -07:00
Kumar Aditya
ec39fd2c20
gh-133980: use atomic store in PyObject_GenericSetDict (#133988) 2025-05-20 21:11:47 +05:30
Jelle Zijlstra
fc7f4c3666
gh-134119: Fix crash from calling next() on exhausted template iterator (#134120)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2025-05-17 12:23:19 -07:00
sobolevn
c3a1da5b93
gh-133970: Make PEP750 types generic (#133976) 2025-05-15 09:11:46 +03:00
Peter Hawkins
9ad0c7b0f1
gh-132641: fix race in lru_cache under free-threading (#133787)
Fix race in `lru_cache` by acquiring critical section on the cache object itself and call the lock held variant of dict functions to modify the underlying dict.
2025-05-13 17:38:57 +00:00
Victor Stinner
fe9f6e829a
gh-133968: Add fast path to PyUnicodeWriter_WriteStr() (#133969)
Don't call PyObject_Str() if the input type is str.
2025-05-13 15:31:41 +02:00
Serhiy Storchaka
9f69a58623
gh-133767: Fix use-after-free in the unicode-escape decoder with an error handler (GH-129648)
If the error handler is used, a new bytes object is created to set as
the object attribute of UnicodeDecodeError, and that bytes object then
replaces the original data. A pointer to the decoded data will became invalid
after destroying that temporary bytes object. So we need other way to return
the first invalid escape from _PyUnicode_DecodeUnicodeEscapeInternal().

_PyBytes_DecodeEscape() does not have such issue, because it does not
use the error handlers registry, but it should be changed for compatibility
with _PyUnicode_DecodeUnicodeEscapeInternal().
2025-05-12 20:42:23 +03:00
Inada Naoki
92337f666e
gh-133703: dict: fix calculate_log2_keysize() (GH-133809) 2025-05-11 14:44:21 +09:00
Stan Ulbrych
4fd1095280
gh-133610: Remove PyUnicode_AsDecoded/Encoded functions (#133612) 2025-05-09 17:31:24 +02:00
Angela Liss
421ba589d0
gh-132762: Fix underallocation bug in dict.fromkeys()(gh-133627)
The function `dict_set_fromkeys()` adds elements of a set to an existing
dictionary. The size of the expanded dictionary was estimated with
`PySet_GET_SIZE(iterable)`, which did not take into account the size of the
existing dictionary.
2025-05-08 13:13:11 -04:00
Serhiy Storchaka
4c914e7a36
gh-133583: Add support for fixed size unsigned integers in argument parsing (GH-133584)
* Add Argument Clinic converters: uint8, uint16, uint32, uint64.
* Add private C API: _PyLong_UInt8_Converter(),
  _PyLong_UInt16_Converter(), _PyLong_UInt32_Converter(),
  _PyLong_UInt64_Converter().
2025-05-08 12:27:50 +03:00
Eric Snow
27128e4fa8
gh-132775: Unrevert "Add _PyCode_VerifyStateless()" (gh-133528)
This reverts commit 3c73cf5 (gh-133497), which itself reverted
the original commit d270bb5 (gh-133221).

We reverted the original change due to failing android tests.
The checks in _PyCode_CheckNoInternalState() were too strict,
so we've relaxed them.
2025-05-08 00:00:33 +00:00
Irit Katriel
296cd128bf
Revert "gh-133395: add option for extension modules to specialize BINARY_OP/SUBSCR, apply to arrays (#133396)" (#133498) 2025-05-06 13:12:26 +03:00
Petr Viktorin
3c73cf51df
gh-132775: Revert "gh-132775: Add _PyCode_VerifyStateless() (gh-133221)" (#133497) 2025-05-06 13:09:41 +03:00
Eric Snow
d270bb5792
gh-132775: Add _PyCode_VerifyStateless() (gh-133221)
"Stateless" code is a function or code object which does not rely on external state or internal state.
It may rely on arguments and builtins, but not globals or a closure. I've left a comment in
pycore_code.h that provides more detail.

We also add _PyFunction_VerifyStateless(). The new functions will be used in several later changes
that facilitate "sharing" functions and code objects between interpreters.
2025-05-05 21:48:58 +00:00
Mark Shannon
d48616e08c
GH-133261: Make sure trashcan pointers look mortal -- 32 bit (GH-133450)
Make sure trashcan pointer look mortal -- 32 bit
2025-05-05 21:51:32 +02:00
Eric Snow
24ebb9ccfd
gh-132775: Unrevert "Add _PyCode_GetVarCounts()" (gh-133265)
This reverts commit 811edcf (gh-133232), which itself reverted the original commit 811edcf (gh-133128).

We reverted the original change due to failing s390 builds (a big-endian architecture).
It ended up that I had not accommodated op caches.
2025-05-05 13:24:29 -06:00
Peter Bierma
b275b8f342
gh-133140: Add PyUnstable_Object_IsUniquelyReferenced for free-threading (#133144) 2025-05-05 21:01:20 +02:00
Irit Katriel
082dbf7788
gh-133395: add option for extension modules to specialize BINARY_OP/SUBSCR, apply to arrays (#133396) 2025-05-05 17:46:56 +01:00
Victor Stinner
c336f1c312
Revert "gh-124715: Fix method_dealloc(): use PyObject_GC_UnTrack() (#133199)" (#133434)
This reverts commit 662dd29456.

The root issue was fixed by the
commit f554237b8e.
2025-05-05 17:04:43 +02:00
Mark Shannon
f554237b8e
GH-133261: Make sure that the GC doesn't untrack objects in trashcan (GH-133431) 2025-05-05 13:44:50 +01:00
Ivan Kirpichnikov
a36367520e
gh-132457: make staticmethod and classmethod generic (#132460)
Co-authored-by: sobolevn <mail@sobolevn.me>
2025-05-04 19:26:38 +03:00
Sergey B Kirpichev
f16f06f336
gh-133304: workaround for RISC-V in PyFloat_Pack4/Unpack4() (#133328) 2025-05-03 17:07:52 +02:00
Petr Viktorin
987e45e632
gh-128972: Add _Py_ALIGN_AS and revert PyASCIIObject memory layout. (GH-133085)
Add `_Py_ALIGN_AS` as per C API WG vote: https://github.com/capi-workgroup/decisions/issues/61
This patch only adds it to free-threaded builds; the `#ifdef Py_GIL_DISABLED`
can be removed in the future.

Use this to revert `PyASCIIObject` memory layout for non-free-threaded builds.
The long-term plan is to deprecate the entire struct; until that happens
it's better to keep it unchanged, as courtesy to people that rely on it despite
it not being stable ABI.
2025-05-02 18:30:40 +02:00
Sam Gross
f2379535fe
gh-133164: Add PyUnstable_Object_IsUniqueReferencedTemporary C API (gh-133170)
After gh-130704, the interpreter replaces some uses of `LOAD_FAST` with
`LOAD_FAST_BORROW` which avoid incref/decrefs by "borrowing" references
on the interpreter stack when the bytecode compiler can determine that
it's safe.

This change broke some checks in C API extensions that relied on
`Py_REFCNT()` of `1` to determine if it's safe to modify an object
in-place. Objects may have a reference count of one, but still be
referenced further up the interpreter stack due to borrowing of
references.

This provides a replacement function for those checks.
`PyUnstable_Object_IsUniqueReferencedTemporary` is more conservative:
it checks that the object has a reference count of one and that it exists as a
unique strong reference in the interpreter's stack of temporary
variables in the top most frame.

See also:

* https://github.com/numpy/numpy/issues/28681

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: T. Wouters <thomas@python.org>
Co-authored-by: mpage <mpage@cs.stanford.edu>
Co-authored-by: Mark Shannon <mark@hotpy.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
2025-05-02 13:24:57 +00:00
Sergey B Kirpichev
ad2f0884b1
gh-130317: Fix test_pack_unpack_roundtrip() and add docs (#133204)
* Skip sNaN's testing in 32-bit mode.
* Drop float_set_snan() helper.
* Use memcpy() workaround for sNaN's in PyFloat_Unpack4().
* Document, that sNaN's may not be preserved by PyFloat_Pack/Unpack API.
2025-05-01 16:20:36 +02:00
neonene
fa52f289a3
gh-133166: Fix missing error emission of PyType_GetModuleByDef (GH-133240) 2025-05-01 14:32:57 +02:00
Victor Stinner
662dd29456
gh-124715: Fix method_dealloc(): use PyObject_GC_UnTrack() (#133199)
Replace _PyObject_GC_UNTRACK() with PyObject_GC_UnTrack() to not fail
if the method was already untracked.
2025-05-01 13:42:42 +02:00
Irit Katriel
5529213d4e
gh-100239: specialize BINARY_OP/SUBSCR for list-slice (#132626) 2025-05-01 10:28:52 +00:00
Eric Snow
811edcf9cd
Revert "gh-132775: Add _PyCode_GetVarCounts() (gh-133128)" (gh-133232)
The change broke the s390 builds, so I'm reverting it while I investigate.

This reverts commit 94b4fcd806.
2025-05-01 02:35:20 +00:00
Mae Hood
0119791326
dict: Remove redundant incref of immortal object Py_EMPTY_KEYS (GH-133200) 2025-05-01 08:39:26 +09:00
Eric Snow
94b4fcd806
gh-132775: Add _PyCode_GetVarCounts() (gh-133128)
This helper is useful in a variety of ways, including in demonstrating how the different counts relate to one another.

It will be used in a later change to help identify if a function is "stateless", meaning it doesn't have any free vars or globals.

Note that a majority of this change is tests.
2025-04-30 18:19:20 +00:00
Mark Shannon
44e4c479fb
GH-124715: Move trashcan mechanism into Py_Dealloc (GH-132280) 2025-04-30 11:37:53 +01:00
Lysandros Nikolaou
60202609a2
gh-132661: Implement PEP 750 (#132662)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Wingy <git@wingysam.xyz>
Co-authored-by: Koudai Aono <koxudaxi@gmail.com>
Co-authored-by: Dave Peck <davepeck@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-authored-by: Paul Everitt <pauleveritt@me.com>
Co-authored-by: sobolevn <mail@sobolevn.me>
2025-04-30 11:46:41 +02:00
Shantanu
7e8b153fef
gh-116436: Improve error message when TypeError occurs during dict update (#116443) 2025-04-29 22:18:06 -07:00
Mark Shannon
ccf1b0b1c1
GH-132508: Use tagged integers on the evaluation stack for the last instruction offset (GH-132545) 2025-04-29 18:00:35 +01:00
Victor Stinner
02cd6d7097
gh-130317: Fix strict aliasing in PyFloat_Pack8() (#133150)
* Fix strict aliasing in PyFloat_Pack8() and PyFloat_Pack4().
* Fix _testcapi.float_set_snan() on x86 (32-bit).
2025-04-29 14:27:50 +00:00
Neil Schemenauer
eecafc3380
Revert gh-127266: avoid data races when updating type slots (gh-131174) (gh-133129)
This is triggering deadlocks in test_opcache.  See GH-133130 for stack trace.
2025-04-28 23:38:29 -07:00
Eric Snow
96a7fb93a8
gh-132775: Add _PyCode_ReturnsOnlyNone() (gh-132981)
The function indicates whether or not the function has a return statement.

This is used by a later change related treating some functions like scripts.
2025-04-28 20:12:52 -06:00
Donghee Na
75cbb8d89e
gh-132070: Use _PyObject_IsUniquelyReferenced in unicodeobject (gh-133039)
---------

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2025-04-29 09:48:53 +09:00
Neil Schemenauer
e414a2d81c
gh-127266: avoid data races when updating type slots (gh-131174)
In the free-threaded build, avoid data races caused by updating type slots
or type flags after the type was initially created.  For those (typically
rare) cases, use the stop-the-world mechanism.  Remove the use of atomics
when reading or writing type flags.  The use of atomics is not sufficient to
avoid races (since flags are sometimes read without a lock and without
atomics) and are no longer required.
2025-04-28 20:28:44 +00:00
Neil Schemenauer
31d1342de9
gh-132942: Fix races in type lookup cache (gh-133032)
Two races related to the type lookup cache, when used in the
free-threaded build.  This caused test_opcache to sometimes fail (as
well as other hard to re-produce failures).
2025-04-28 11:52:08 -07:00
Eric Snow
c17238251f
gh-132775: Add _PyModule_GetFilenameObject() and _PyModule_GetFilenameUTF8() (gh-132979)
They are derived from the existing `PyModule_GetFilenameObject().

They are used by a later change related to pickle and handling __main__.
2025-04-28 12:41:32 -06:00