Commit graph

9072 commits

Author SHA1 Message Date
bkap123
9e0802330c
gh-145036: Fix data race for list capacity in free-threading (#145365)
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2026-03-10 22:00:11 +05:30
Sergey Miryanov
478a315b7a
GH-145247: Implement _PyTuple_FromPair() (#145325)
Implement _PyTuple_FromPair() and _PyTuple_FromPairSteal().

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Bartosz Sławecki <bartosz@ilikepython.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
2026-03-10 11:44:20 +01:00
Stan Ulbrych
bdf0105291
gh-103997: Remove incorrect statements about -c dedenting (gh-138624) 2026-03-10 09:56:00 +01:00
Sam Gross
0b65c88c2a
gh-145685: Stop the world when updating MRO of existing types (gh-145707)
We already have a stop-the-world pause elsewhere in this code path
(type_set_bases) and this makes will make it easier to avoid contention
on the TYPE_LOCK when looking up names in the MRO hierarchy.

Also use deferred reference counting for non-immortal MROs.
2026-03-09 18:41:07 -04:00
Sam Gross
d76df75f51
gh-145615: Fix mimalloc page leak in the free-threaded build (gh-145626)
Fix three issues that caused mimalloc pages to be leaked until the
owning thread exited:

1. In _PyMem_mi_page_maybe_free(), move pages out of the full queue
   when relying on QSBR to defer freeing the page. Pages in the full
   queue are never searched by mi_page_queue_find_free_ex(), so a page
   left there is unusable for allocations.

2. Move _PyMem_mi_page_clear_qsbr() from _mi_page_free_collect() to
   _mi_page_thread_free_collect() where it only fires when all blocks
   on the page are free (used == 0). The previous placement was too
   broad: it cleared QSBR state whenever local_free was non-NULL, but
   _mi_page_free_collect() is called from non-allocation paths (e.g.,
   page visiting in mi_heap_visit_blocks) where the page is not being
   reused.

3. In _PyMem_mi_page_maybe_free(), use the page's heap tld to find the
   correct thread state for QSBR list insertion instead of
   PyThreadState_GET(). During stop-the-world pauses, the function may
   process pages belonging to other threads, so the current thread
   state is not necessarily the owner of the page.
2026-03-09 13:24:34 -04:00
Victor Stinner
0dfe649400
gh-141510: Optimize frozendict(frozendict) (#145592)
Return the same object unmodified if it's exactly the frozendict
type.

Optimize also PyFrozenDict_New(frozendict).
2026-03-09 15:47:02 +01:00
Pieter Eendebak
886bc6e14b
gh-145376: Fix various reference leaks in Objects/ and Modules/ (#145385)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
2026-03-09 14:19:36 +01:00
Pieter Eendebak
8060aa5d7d
gh-145376: Fix various refleaks in Objects/ (#145609) 2026-03-09 14:17:27 +01:00
Sam Gross
1d091a336e
gh-145566: Skip stop-the-world when reassigning __class__ on newly created objects (gh-145567) 2026-03-06 12:01:06 -05:00
Victor Stinner
0c29f83caa
gh-141510: No longer accept frozendict in PyDict_Copy() (#145542)
Rename _PyDict_Copy() to anydict_copy().

Replace PyObject_IsInstance(op, &PyFrozenDict_Type) with
PyFrozenDict_Check().
2026-03-05 15:26:54 +01:00
Victor Stinner
2cd0ddfe04
gh-141510: Fix frozendict.items() ^ frozendict.items() (#145535)
Add non-regression tests.
2026-03-05 14:14:04 +01:00
Victor Stinner
c0ecf211b2
gh-145055: Accept frozendict for globals in exec() and eval() (#145072) 2026-03-05 12:35:43 +01:00
Victor Stinner
0fe20fc170
gh-141510: Don't accept frozendict in PyDict_Watch() (#145529)
Don't accept frozendict in PyDict_Watch() and PyDict_Unwatch().
A frozendict cannot be modified, so it's not useful to watch for
modifications.
2026-03-05 12:31:29 +01:00
Victor Stinner
0a51113a49
gh-141510: Optimize PyDict_Copy() for frozendict (#145509)
Implement fast-path for frozendict in PyDict_Copy().
2026-03-04 20:49:20 +01:00
Victor Stinner
95f56b1206
gh-141510: Return frozendict unmodified in PyDict_Copy() (#145505)
Add also the internal _PyDict_CopyAsDict() function.
2026-03-04 19:11:00 +00:00
Sam Gross
8a7eb8b2ab
gh-145500: Delete _PyType_GetMRO (gh-145501) 2026-03-04 13:32:43 -05:00
Pieter Eendebak
18aec59fe5
gh-145376: Fix refleak in unusual error path in BaseExceptionGroup_new (GH-145474) 2026-03-04 14:34:24 +01:00
Bartosz Sławecki
e6c3c04fab
gh-145452: Initialize PyLazyImport_Type during interpreter startup (#145453)
Co-authored-by: Victor Stinner <vstinner@python.org>
2026-03-03 17:14:12 +01:00
Victor Stinner
c9d123482a
gh-144995: Optimize memoryview == memoryview (#144996)
Optimize memoryview comparison: a memoryview is equal to itself, there is no
need to compare values, except if it uses float format.

Benchmark comparing 1 MiB:

    from timeit import timeit
    with open("/dev/random", 'br') as fp:
        data = fp.read(2**20)
    view = memoryview(data)
    LOOPS = 1_000
    b = timeit('x == x', number=LOOPS, globals={'x': data})
    m = timeit('x == x', number=LOOPS, globals={'x': view})
    print("bytes %f seconds" % b)
    print("mview %f seconds" % m)
    print("=> %f time slower" % (m / b))

Result before the change:

    bytes 0.000026 seconds
    mview 1.445791 seconds
    => 55660.873940 time slower

Result after the change:

    bytes 0.000026 seconds
    mview 0.000028 seconds
    => 1.104382 time slower

This missed optimization was discovered by Pierre-Yves David
while working on Mercurial.

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
2026-03-03 12:15:32 +01:00
Stan Ulbrych
f8d2f9fe67
gh-145118: Add frozendict support to type() (#145124) 2026-03-02 20:45:03 +01:00
Peter Bierma
592e8f0865
gh-130327: Always traverse managed dictionaries, even when inline values are available (#130469) 2026-03-02 14:32:06 -05:00
Sam Gross
02288bf022
gh-130555: Fix use-after-free in dict.clear() with embedded values (gh-145268) 2026-03-02 12:25:13 -05:00
Hai Zhu
107863ee17
gh-144569: Avoid creating temporary objects in BINARY_SLICE for list, tuple, and unicode (GH-144590)
* Scalar replacement of BINARY_SLICE for list, tuple, and unicode
2026-03-02 17:02:38 +00:00
VanshAgarwal24036
a249795538
gh-145142: Make str.maketrans safe under free-threading (gh-145157) 2026-02-27 16:08:15 +00:00
Stan Ulbrych
1dfbde9299
gh-145118: Add frozendict support to str.maketrans() (gh-145129)
Add support to `str.maketrans`
2026-02-23 16:04:16 -06:00
Victor Stinner
3dc8fdb321
gh-145089: Fix frozendict constructor (#145128) 2026-02-23 08:22:29 +01:00
Rudi Heitbaum
faea32b729
gh-145092: Fix compiler warning for memchr() and wcschr() returning const pointer (GH-145093) 2026-02-22 10:01:27 +02:00
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