Commit graph

9045 commits

Author SHA1 Message Date
Victor Stinner
fbd3b25e00
gh-141510: Change dict_unhashable_type() error message for frozendict (#145085) 2026-02-21 20:08:28 +01:00
Victor Stinner
c9380aebbe
gh-141510: Check argument in PyDict_Contains() (#145083)
PyDict_Contains() and PyDict_ContainsString() now fail with
SystemError if the first argument is not a dict, frozendict, dict
subclass or frozendict subclass.
2026-02-21 18:36:02 +01:00
Victor Stinner
6940c1dc0c
gh-141510: Check argument in PyDict_MergeFromSeq2() (#145082)
PyDict_MergeFromSeq2() now fails with SystemError if the first
argument is not a dict or a dict subclass.

PyDict_Update(), PyDict_Merge() and _PyDict_MergeEx() no longer
accept frozendict.
2026-02-21 17:21:43 +01:00
Victor Stinner
20b1535ca4
gh-141510, PEP 814: Add frozendict support to pickle (#144967)
Add frozendict.__getnewargs__() method.
2026-02-21 11:07:55 +01:00
Alper
a56532771a
gh-144981: Make PyUnstable_Code_SetExtra/GetExtra thread-safe (#144980)
* Make PyUnstable_Code_SetExtra/GetExtra thread-safe
2026-02-20 10:52:18 -08:00
Mark Shannon
3f37b94c73
GH-144651: Optimize the new uops added when recording values during tracing. (GH-144948)
* Handle dependencies in the optimizer, not the tracer
* Strengthen some checks to avoid relying on optimizer for correctness
2026-02-19 11:52:57 +00:00
Ken Jin
e84a2cc83c
gh-144888: Don't invalidate executors during function deallocation (#144974) 2026-02-19 03:44:50 +00:00
Victor Stinner
16ccdbc50a
gh-141510: Fix frozendict.fromkeys() for dict subclasses (#144962)
Copy also the dictionary if a dict subclass returns a frozendict.
2026-02-18 18:03:04 +01:00
Victor Stinner
c582ff3c25
gh-141510: Fix frozendict.fromkeys() for subclasses (#144952)
Copy the frozendict if needed.
2026-02-18 15:56:09 +00:00
Victor Stinner
1ddb412689
gh-141510: Add can_modify_dict() in dictobject.c (#144955)
can_modify_dict() is stricter than ASSERT_DICT_LOCKED() for
frozendict. It uses PyUnstable_Object_IsUniquelyReferenced() which
matters for free-threaded builds.

Replace anydict_setitem_take2() with setitem_take2_lock_held(). It's
no longer useful to have two functions.
2026-02-18 15:47:49 +01:00
Victor Stinner
f705486745
gh-141510: Add frozendict fast-path to the set type (#144912) 2026-02-18 15:25:47 +01:00
Kumar Aditya
836f2c97f9
gh-144914: use mimalloc for raw allocations on free-threading (#144916) 2026-02-18 04:16:27 +00:00
Donghee Na
d1b541b047
gh-141510: Optimize {frozen}dict.fromkeys for frozendict (gh-144915) 2026-02-17 21:46:20 +00:00
Victor Stinner
d1505b543a
gh-141510: Change repr(frozendict) for empty dict (#144921)
repr(frozendict()) returns "frozendict()" instead of
"frozendict({})".
2026-02-17 18:04:17 +00:00
Victor Stinner
8e211b1ca9
gh-141510: Optimize hash(frozendict) (#144919)
hash(frozendict) no longer creates a temporary items view and a
temporary frozenset object.

Copy frozenset_hash() code to frozendict_hash().
2026-02-17 18:39:33 +01:00
Donghee Na
1bd8cf9ed2
gh-141510: Remove unncessary lock holding for frozendict repr (gh-144920) 2026-02-17 14:52:50 +00:00
Donghee Na
fc05e5e3d7
gh-141510: Update mp_length of frozendict to use non atomic operation (gh-144913) 2026-02-17 13:39:56 +00:00
Victor Stinner
696cdfc0a2
gh-141510, PEP 814: Add built-in frozendict type (#144757)
Add TYPE_FROZENDICT to the marshal module.

Add C API functions:

* PyAnyDict_Check()
* PyAnyDict_CheckExact()
* PyFrozenDict_Check()
* PyFrozenDict_CheckExact()
* PyFrozenDict_New()

Add PyFrozenDict_Type C type.

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Adam Johnson <me@adamj.eu>
Co-authored-by: Benedikt Johannes <benedikt.johannes.hofer@gmail.com>
2026-02-17 10:54:41 +01:00
Chris Eibl
14cbd0e6af
remove unused _PyFunction_LookupByVersion (GH-144814) 2026-02-14 14:09:01 +00:00
Pablo Galindo Salgado
46d5106cfa
gh-142349: Implement PEP 810 - Explicit lazy imports (#142351)
Co-authored-by: T. Wouters <twouters@meta.com >
Co-authored-by: Brittany Reynoso <breynoso@meta.com>
Co-authored-by: Dino Viehland <dinoviehland@meta.com>
2026-02-12 00:15:33 +00:00
Kumar Aditya
347d3594d3
gh-143300: implement PyUnstable_SetImmortal for marking objects as immortal (#144543) 2026-02-11 20:59:31 +05:30
Petr Viktorin
b22ff1e543
gh-140550: allow slots that repeat information from PyModuleDef (GH-144340)
When integrating slots-based module creation is with the inittab,
which currently requires PyModuleDef, it would be convenient to
reuse the the same slots array for the MethodDef.

Allow slots that match what's already present in the PyModuleDef.
2026-02-09 11:35:43 +01:00
Sam Gross
5bb3bbb9c6
gh-144446: Fix some frame object thread-safety issues (gh-144479)
Fix thread-safety issues when accessing frame attributes while another
thread is executing the frame:

- Add critical section to frame_repr() to prevent races when accessing
  the frame's code object and line number

- Add _Py_NO_SANITIZE_THREAD to PyUnstable_InterpreterFrame_GetLasti()
  to allow intentional racy reads of instr_ptr.

- Fix take_ownership() to not write to the original frame's f_executable
2026-02-06 09:43:36 -05:00
Sam Gross
d891b2bbd1
gh-139103: Improve namedtuple scaling in free-threaded build (gh-144332)
Add `_Py_type_getattro_stackref`, a variant of type attribute lookup
that returns `_PyStackRef` instead of `PyObject*`. This allows returning
deferred references in the free-threaded build, reducing reference count
contention when accessing type attributes.

This significantly improves scaling of namedtuple instantiation across
multiple threads.

* Add blurb

* Rename PyObject_GetAttrStackRef to _PyObject_GetAttrStackRef

* Apply suggestion from @vstinner

Co-authored-by: Victor Stinner <vstinner@python.org>

* Apply suggestion from @vstinner

Co-authored-by: Victor Stinner <vstinner@python.org>

* format

* Update Include/internal/pycore_function.h

Co-authored-by: Victor Stinner <vstinner@python.org>

---------

Co-authored-by: Victor Stinner <vstinner@python.org>
2026-02-06 09:43:05 -05:00
Zackery Spytz
01a1dd283b
gh-77188: Add support for pickling private methods and nested classes (GH-21480)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2026-02-05 19:50:51 +00:00
Victor Stinner
ffa68529b4
gh-144330: Initialize classmethod and staticmethod in new (#144469)
Move classmethod and staticmethod initialization from __init__() to
__new__().

PyClassMethod_New() and PyStaticMethod_New() now copy attributes of
the wrapped functions: __module__, __name__, __qualname__ and
__doc__.

Change static type initialization: initialize PyStaticMethod_Type and
PyCFunction_Type earlier.

Remove test_refleaks_in_classmethod___init__() and
test_refleaks_in_staticmethod___init__() tests from test_descr since
classmethod and staticmethod have no __init__() method anymore.
2026-02-05 11:32:56 +01:00
Sam Gross
7e2c9bdc98
gh-120321: Add gi_state, cr_state, and ag_state attributes (gh-144409)
Add `gi_state`, `cr_state`, and `ag_state` attributes to generators,
coroutines, and async generators respectively. These attributes return the
current state as a string (e.g., `GEN_RUNNING`, `CORO_SUSPENDED`).

The `inspect.getgeneratorstate()`, `inspect.getcoroutinestate()`, and
`inspect.getasyncgenstate()` functions now return these attributes directly.

This is in preparation for making `gi_frame` thread-safe, which may involve
stop-the-world synchronization. The new state attributes avoid potential
performance cliffs in `inspect.getgeneratorstate()` and similar functions by
not requiring frame access.

Also removes unused `FRAME_COMPLETED` state and renumbers the frame state enum
to start at 0 instead of -1.
2026-02-03 13:06:32 -05:00
Sam Gross
79c43e7c24
gh-139103: Use borrowed references for positional args in _PyStack_UnpackDict (gh-144407)
The positional arguments passed to _PyStack_UnpackDict are already
kept alive by the caller, so we can avoid the extra reference count
operations by using borrowed references instead of creating new ones.

This reduces reference count contention in the free-threaded build
when calling functions with keyword arguments. In particular, this
avoids contention on the type argument to `__new__` when instantiating
namedtuples with keyword arguments.
2026-02-03 12:24:35 -05:00
Sam Gross
93b27e7f6d
gh-139103: use METH_FASTCALL for tp_new_wrapper (#144406) 2026-02-03 16:02:06 +05:30
Sergey B Kirpichev
15c9f2491d
gh-115231: Fill __module__ for built-in staticmethods (#115232)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Victor Stinner <vstinner@python.org>
2026-02-02 12:34:02 +01:00
Pablo Galindo Salgado
96e4cd698a
gh-144319: Fix huge page safety in pymalloc arenas (#144331)
The pymalloc huge page support had two problems. First, on
architectures where the default huge page size exceeds the arena
size (e.g. 32 MiB on PPC, 512 MiB on ARM64 with 64 KB base
pages), mmap with MAP_HUGETLB silently allocates a full huge page
even when the requested size is smaller. The subsequent munmap
with the original arena size then fails with EINVAL, permanently
leaking the entire huge page. Second, huge pages were always
attempted when compiled in, with no way to disable them at
runtime. On Linux, if the huge page pool is exhausted, page
faults including copy-on-write faults after fork deliver SIGBUS
and kill the process.

The arena allocator now queries the system huge page size from
/proc/meminfo and skips MAP_HUGETLB when the arena size is not a
multiple of it. Huge pages also now require explicit opt-in at
runtime via the PYTHON_PYMALLOC_HUGEPAGES environment variable,
which is read through PyConfig and respects -E and -I flags.
The config field pymalloc_hugepages is propagated to the runtime
allocators struct so the low-level arena allocator can check it
without calling getenv directly.
2026-01-30 18:18:56 +00:00
Sam Gross
a01694dacd
gh-120321: Make gi_yieldfrom thread-safe in free-threading build (#144292)
Add a FRAME_SUSPENDED_YIELD_FROM_LOCKED state that acts as a brief
lock, preventing other threads from transitioning the frame state
while gen_getyieldfrom reads the yield-from object off the stack.
2026-01-30 12:20:27 -05:00
Sam Gross
e666a01ef4
gh-144295: Fix data race in dict method lookup and global load (gh-144312)
In `_PyDict_GetMethodStackRef`, only use the fast-path unicode lookup
when the dict is owned by the current thread or already marked as shared.
This prevents a race between the lookup and concurrent dict resizes,
which may free the PyDictKeysObject (i.e., it ensures that the resize
uses QSBR).

Address a similar issue in `_Py_dict_lookup_threadsafe_stackref` by
calling `ensure_shared_on_read()`.
2026-01-30 11:14:10 -05:00
Pieter Eendebak
1b08143466
gh-143192 Avoid incref/decref pair in long_bitwise (gh-143194)
Remove unnecessary reference count operations in long_bitwise in order to
avoid reference count contention in the free-threading build.
2026-01-29 16:32:09 -05:00
Pablo Galindo Salgado
b1bc868fba
gh-144319: Add huge pages support for pymalloc (#144320) 2026-01-29 12:26:11 +00:00
Pieter Eendebak
08d7bd28fe
gh-140232: Do not track frozenset objects with immutables (#140234)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Mikhail Efimov <efimov.mikhail@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
2026-01-28 11:27:37 +01:00
Serhiy Storchaka
2520617361
gh-142037: Fix a refleak introduced in GH-142081 (GH-144256) 2026-01-26 21:15:21 +00:00
Serhiy Storchaka
012c498035
gh-142037: Improve error messages for printf-style formatting (GH-142081)
This affects string formatting as well as bytes and bytearray formatting.

* For errors in the format string, always include the position of the
  start of the format unit.
* For errors related to the formatted arguments, always include the number
  or the name of the formatted argument.
* Suggest more probable causes of errors in the format string (stray %,
  unsupported format, unexpected character).
* Provide more information when the number of arguments does not match
  the number of format units.
* Raise more specific errors when access of arguments by name is mixed with
  sequential access and when * is used with a mapping.
* Add tests for some uncovered cases.
2026-01-24 11:13:50 +00:00
Gregory P. Smith
a966d94e76
gh-144157: Optimize bytes.translate() by deferring change detection (GH-144158)
Optimize bytes.translate() by deferring change detection

Move the equality check out of the hot loop to allow better compiler
optimization. Instead of checking each byte during translation, perform
a single memcmp at the end to determine if the input can be returned
unchanged.

This allows compilers to unroll and pipeline the loops, resulting in ~2x
throughput improvement for medium-to-large inputs (tested on an AMD zen2).
No change observed on small inputs.

It will also be faster for bytes subclasses as those do not need change
detection.
2026-01-22 09:21:07 -08:00
Sam Gross
43bb6300b3
gh-143939: Fix assignment to _PyThreadStateImpl.generator_return_kind (gh-143951)
The assignment to generator_return_kind has to be after any potentially
escaping calls to ensure that it's not overwritten.
2026-01-20 17:51:55 +00:00
Bartosz Sławecki
f84ea11071
gh-102809: No longer mention Misc/gdbinit in the code (#143980)
Fix misleading comment on `PyObject_Dump`.
2026-01-19 18:36:55 +01:00
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