Commit graph

9004 commits

Author SHA1 Message Date
Neil Schemenauer
bb25f7280a
gh-132657: Add maybe_enable_deferred_ref_count() (gh-142843)
If we are specializing to `LOAD_GLOBAL_MODULE` or `LOAD_ATTR_MODULE`, try
to enable deferred reference counting for the value, if the object is owned by
a different thread.  This applies to the free-threaded build only and should
improve scaling of multi-threaded programs.
2026-01-17 01:54:43 +00:00
Dino Viehland
4d5a676aa0
gh-142913: Add test case for interpreter generator w/ overridden opcodes (#142911)
Add test case for interpreter generator w/ overridden opcodes
2026-01-16 10:33:29 -08:00
Peter Bierma
19e64afddf
gh-141070: Rename PyUnstable_Object_Dump to PyObject_Dump (GH-142848) 2026-01-16 09:19:43 -05:00
sobolevn
bdba5f0db2
gh-143635: Fix crash in ga_repr_items_list (#143670) 2026-01-15 06:50:47 +03:00
Donghee Na
794f758cd8
gh-141504: Refactor policy object into a single opt_config (gh-143644) 2026-01-15 09:53:00 +09:00
Serhiy Storchaka
b8e925b4f8
gh-141805: Fix crash after concurrent addition objects with the same hash to set (GH-143815)
This happens when the set contained several elements with the same hash,
and then some of them were removed.
2026-01-14 23:29:17 +02:00
Cody Maloney
510ab7d6e1
gh-132108: Add Buffer Protocol support to int.from_bytes to improve performance (#132109)
Speed up conversion from `bytes-like` objects like `bytearray` while
keeping conversion from `bytes` stable.

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
2026-01-13 10:33:26 +01:00
Inada Naoki
43c76587c1
gh-143189: fix insertdict() for non-Unicode key (#143285) 2026-01-12 13:21:23 +02:00
James Hilton-Balfe
265381b7e8
gh-128335: Make slice generic at runtime (#128336)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2026-01-11 11:27:24 -08:00
Manuel Jacob
03e6457096
Remove outdated comment about excess_args warning in object.__init__ (#143669)
The code emitting a warning was removed in 96384b93aa.
2026-01-11 10:27:11 +03:00
Serhiy Storchaka
66bca383bd
gh-143006: Fix and optimize mixed comparison of float and int (GH-143084)
When comparing negative non-integer float and int with the same number
of bits in the integer part, __neg__() in the int subclass returning
not an int caused an assertion error.

Now the integer is no longer negated. Also, reduced the number of
temporary created Python objects.
2026-01-09 19:06:45 +02:00
Sam Gross
e2f15aec16
gh-120321: Make gen.gi_frame.clear() thread-safe (gh-143112) 2026-01-08 14:45:54 -05:00
VanshAgarwal24036
67d3d0344f
gh-143089: Fix ParamSpec default examples to use list instead of tuple (#143179) 2026-01-07 19:32:44 -08:00
Sam Gross
98e55d70bc
gh-132070: Fix PyObject_Realloc thread-safety in free threaded Python (gh-143441)
The PyObject header reference count fields must be initialized using
atomic operations because they may be concurrently read by another
thread (e.g., from `_Py_TryIncref`).
2026-01-06 15:55:37 -05:00
Sam Gross
71119a164c
gh-129824: Fix data race in _PyBuiltins_AddExceptions with subinterpreters (gh-143446) 2026-01-06 09:15:55 -05:00
Serhiy Storchaka
4d21297d28
gh-41779: Allow defining any __slots__ for a class derived from tuple (GH-141763) 2026-01-06 11:36:00 +02:00
Serhiy Storchaka
522563549a
gh-143003: Fix possible shared buffer overflow in bytearray.extend() (GH-143086)
When __length_hint__() returns 0 for non-empty iterator, the data can be
written past the shared 0-terminated buffer, corrupting it.
2025-12-28 12:30:36 +00:00
Bénédikt Tran
61ee04834b
gh-142557: fix UAF in bytearray.__mod__ when object is mutated while formatting %-style arguments (#143213) 2025-12-27 14:57:13 +00:00
Bénédikt Tran
84fcdbd86e
gh-142664: fix PyObject_Hash invokation post GH-143217 (#143223) 2025-12-27 14:30:09 +00:00
Bénédikt Tran
00e24b80e0
gh-142664: fix UAF in memoryview.__hash__ via re-entrant data's __hash__ (#143217) 2025-12-27 13:12:03 +00:00
Bénédikt Tran
9976c2b634
gh-143195: fix UAF in {bytearray,memoryview}.hex(sep) via re-entrant sep.__len__ (#143209) 2025-12-27 13:32:52 +01:00
Yongtao Huang
de22e718bb
Remove redundant pycore_optimizer.h includes (#143184)
`pycore_optimizer.h` was included redundantly in
Objects/frameobject.c and Python/instrumentation.c.
Both includes are unnecessary and can be safely removed.
No functional change.

Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
2025-12-26 11:11:11 +00:00
Sam Gross
594a4631c3
gh-120321: Fix TSan reported races on gi_frame_state (gh-143128) 2025-12-24 16:10:43 -05:00
Sam Gross
e8e044eda3
gh-143100: Fix memcpy data race in setobject.c (gh-143127) 2025-12-24 08:02:19 -05:00
Chris Eibl
be3c131640
GH-139922: Tail calling for MSVC (VS 2026) (GH-143068)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Co-authored-by: Brandt Bucher <brandt@python.org>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
2025-12-22 23:01:34 +00:00
Kumar Aditya
487e91c120
gh-129069: fix more thread safety issues in list (#143019) 2025-12-22 21:45:28 +05:30
Sam Gross
7607712b61
gh-120321: Avoid -Wunreachable-code warning on Clang (gh-143022) 2025-12-20 14:42:12 -05:00
Sam Gross
e46f28c6af
gh-129069: Fix listobject.c data races due to memmove (gh-142957)
The use of memmove and _Py_memory_repeat were not thread-safe in the
free threading build in some cases. In theory, memmove and
_Py_memory_repeat can copy byte-by-byte instead of pointer-by-pointer,
so concurrent readers could see uninitialized data or tearing.

Additionally, we should be using "release" (or stronger) ordering to be
compliant with the C11 memory model when copying objects within a list.
2025-12-19 18:06:47 -05:00
Sam Gross
4ea3c1a047
gh-120321: Fix TSan reported race in gen_clear_frame (gh-142995)
TSan treats compare-exchanges that fail as if they are writes
so there is a false positive with the read of gi_frame_state in
gen_close.
2025-12-19 17:33:49 -05:00
Sam Gross
08bc03ff2a
gh-120321: Make gi_frame_state transitions atomic in FT build (gh-142599)
This makes generator frame state transitions atomic in the free
threading build, which avoids segfaults when trying to execute
a generator from multiple threads concurrently.

There are still a few operations that aren't thread-safe and may crash
if performed concurrently on the same generator/coroutine:

 * Accessing gi_yieldfrom/cr_await/ag_await
 * Accessing gi_frame/cr_frame/ag_frame
 * Async generator operations
2025-12-19 19:10:37 +00:00
wangxiaolei
220f0b1077
gh-142560: prevent use-after-free in search-like methods by exporting buffer in bytearray (#142938) 2025-12-19 08:02:23 +00:00
Sam Gross
f54d44d333
gh-129068: Make range iterators thread-safe (gh-142886)
Now that we specialize range iteration in the interpreter for the common
case where the iterator has only one reference, there's not a
significant performance cost to making the iteration thread-safe.
2025-12-18 13:11:51 -05:00
Kumar Aditya
e22c49522b
gh-142890: remove unnecessary interp parameter from dict functions and _PyDict_NotifyEvent (#142923) 2025-12-18 22:48:56 +05:30
Donghee Na
14f0b5191a
gh-142419: Add mmap.set_name method for user custom annotation (gh-142480) 2025-12-18 23:33:49 +09:00
Sam Gross
25397f9541
gh-142766: Clear frame when generator.close() is called (gh-142838) 2025-12-17 13:06:32 -05:00
Bartosz Sławecki
f277781bba
gh-142737: Handle lost io.open in _Py_FindSourceFile (GH-142747) 2025-12-15 22:58:50 +00:00
Ken Jin
0ac4e6c6cd
gh-134584: Remove custom float decref ops (GH-142576) 2025-12-15 19:38:58 +00:00
Sam Gross
a882ae198a
gh-142472: Clean-up _PyStackRef functions (gh-142479)
This combines most _PyStackRef functions and macros between the free
threaded and default builds.

- Remove Py_TAG_DEFERRED (same as Py_TAG_REFCNT)
- Remove PyStackRef_IsDeferred (same as !PyStackRef_RefcountOnObject)
2025-12-15 12:03:49 -05:00
Cody Maloney
14e6052b43
gh-139871: Optimize bytearray construction with encoding (#142243)
When a `str` is encoded in `bytearray.__init__` the encoder tends to
create a new unique bytes object. Rather than allocate new memory and
copy the bytes use the already created bytes object as bytearray
backing. The bigger the `str` the bigger the saving.

Mean +- std dev: [main_encoding] 497 us +- 9 us -> [encoding] 14.2 us +- 0.3 us: 34.97x faster

```python
import pyperf

runner = pyperf.Runner()

runner.timeit(
    name="encode",
    setup="a = 'a' * 1_000_000",
    stmt="bytearray(a, encoding='utf8')")
```
2025-12-15 13:10:31 +01:00
Neil Schemenauer
19c72d23fd
gh-132657: Use stronger memory ordering for so->mask. (gh-142735)
We need to use release/acquire ordering for the 'mask' member of the set
structure. Without this, `set_lookkey_threadsafe()` could be looking at
the old value of `table` but the new value of `mask`.
2025-12-14 20:27:37 -08:00
Bénédikt Tran
4e4163676a
gh-142554: avoid divmod crashes due to bad _pylong.int_divmod (#142673) 2025-12-14 09:38:23 +01:00
Neil Schemenauer
c98182be8d
gh-132657: Add lock-free set contains implementation (#132290)
This roughly follows what was done for dictobject to make a lock-free
lookup operation. With this change, the set contains operation scales much
better when used from multiple-threads. The frozenset contains performance
seems unchanged (as already lock-free).

Summary of changes:

* refactor set_lookkey() into set_do_lookup() which now takes a function
  pointer that does the entry comparison. This is similar to dictobject and
  do_lookup(). In an optimized build, the comparison function is inlined and
  there should be no performance cost to this.

* change set_do_lookup() to return a status separately from the entry value

* add set_compare_frozenset() and use if the object is a frozenset. For the
  free-threaded build, this avoids some overhead (locking, atomic operations,
  incref/decref on key)

* use FT_ATOMIC_* macros as needed for atomic loads and stores

* use a deferred free on the set table array, if shared (only on free-threaded
  build, normal build always does an immediate free)

* for free-threaded build, use explicit for loop to zero the table, rather than memcpy()

* when mutating the set, assign so->table to NULL while the change is a
  happening. Assign the real table array after the change is done.
2025-12-13 09:50:23 +00:00
Victor Stinner
7aa353c414
gh-142217: Deprecate the private _Py_Identifier C API (#142221)
Deprecate functions:

* _PyObject_CallMethodId()
* _PyObject_GetAttrId()
* _PyUnicode_FromId()
2025-12-12 14:10:25 +01:00
Sam Gross
0a62f8277e
gh-142534: Avoid TSan warnings in dictobject.c (gh-142544)
There are places we use "relaxed" loads where C11 requires "consume" or
stronger. Unfortunately, compilers don't really implement "consume" so
fake it for our use in a way that avoids upsetting TSan.
2025-12-11 16:23:19 -05:00
Sam Gross
a26c831bc4
gh-142589: Fix PyUnstable_Object_IsUniqueReferencedTemporary (gh-142593)
PyUnstable_Object_IsUniqueReferencedTemporary wasn't handling tagged
ints on the evaluation stack properly.
2025-12-11 14:41:03 -05:00
Dino Viehland
da8199f884
gh-123241: Don't modify ref count during visitation (GH-142232) 2025-12-11 09:54:29 +01:00
AZero13
785268fdce
gh-142433: Move deref to below the error when checking for laststring (#142402)
Move deref of laststring to below the error checking so the deref
is applied after the object in strings is replaced.
2025-12-10 16:41:52 +01:00
Donghee Na
c4ccaf4b10
gh-141770: Annotate anonymous mmap usage if "-X dev" is used (gh-142079) 2025-12-08 14:47:19 +00:00
dr-carlos
ff2577f56e
gh-141732: Fix ExceptionGroup repr changing when original exception sequence is mutated (#141736) 2025-12-07 21:04:04 +00:00
Sam Gross
547d8daf78
gh-142218: Fix split table dictionary crash (gh-142229)
This fixes a regression introduced in gh-140558. The interpreter would
crash if we inserted a non `str` key into a split table that matches an
existing key.
2025-12-03 18:37:35 -05:00