gh-126862: Use `Py_ssize_t` instead of `int` when processing the number of super-classes (GH-127523)
(cherry picked from commit 2fcdc8488c)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
gh-127903: Fix a crash on debug builds when calling `Objects/unicodeobject::_copy_characters`` (GH-127876)
(cherry picked from commit 46cb6340d7)
Co-authored-by: Alexander Shadchin <shadchin@yandex-team.com>
Clean up redundant ifdef in list getitem (GH-128257)
It's already inside a `Py_GIL_DISABLED` block so the `#else` clause is always unused.
(cherry picked from commit 42f7a00ae8)
Co-authored-by: da-woods <dw-git@d-woods.co.uk>
This fixes a UBSan failure (unaligned zero-size memcpy) in `dictobject.c`.
(cherry picked from commit 9af96f4406)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Objects may be temporarily "resurrected" in destructors when calling
finalizers or watcher callbacks. We previously undid the resurrection
by decrementing the reference count using `Py_SET_REFCNT`. This was not
thread-safe because other threads might be accessing the object
(modifying its reference count) if it was exposed by the finalizer,
watcher callback, or temporarily accessed by a racy dictionary or list
access.
This adds internal-only thread-safe functions for temporary object
resurrection during destructors.
(cherry picked from commit f4f530804b)
In the free threading build, if a non-owning thread resizes a list,
it must use QSBR to free the old list array because there may be a
concurrent access (without a lock) from the owning thread.
To match the pattern in dictobject.c, we just mark the list as "shared"
before resizing if it's from a non-owning thread and not already marked
as shared.
(cherry picked from commit c7dec02de2)
Co-authored-by: Sam Gross <colesbury@gmail.com>
Note, that transformed expression is not an equivalent for original one (1/exp(-x) != exp(x) in general for floating-point numbers). Though, the difference seems to be ~1ULP for good libm implementations.
It's more interesting why division was used from beginning. Closest algorithm I've found (no error checks, of course;)) - it's Algorithm 190 from ACM: https://dl.acm.org/doi/10.1145/366663.366679. It uses subtraction in the exponent.
(cherry picked from commit f7bb658124)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Some fields in PyCodeObject are lazily initialized. Use atomics and
critical sections to make their initializations and accesses thread-safe.
(cherry picked from commit 3926842117)
Co-authored-by: Sam Gross <colesbury@gmail.com>
* gh-116510: Fix a Crash Due to Shared Immortal Interned Strings (gh-124865)
Fix a crash caused by immortal interned strings being shared between
sub-interpreters that use basic single-phase init. In that case, the string
can be used by an interpreter that outlives the interpreter that created and
interned it. For interpreters that share obmalloc state, also share the
interned dict with the main interpreter.
This is an un-revert of gh-124646 that then addresses the Py_TRACE_REFS
failures identified by gh-124785.
(cherry picked from commit f2cb399470)
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
* [3.13] gh-125286: Share the Main Refchain With Legacy Interpreters (gh-125709)
They used to be shared, before 3.12. Returning to sharing them resolves a failure on Py_TRACE_REFS builds.
---------
Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
gh-116938: Fix `dict.update` docstring and remove erraneous full stop from `dict` documentation (GH-125421)
(cherry picked from commit 5527c4051c)
Co-authored-by: Prometheus3375 <35541026+Prometheus3375@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Dictionary watchers on an object's attributes dictionary
(`object.__dict__`) were not triggered when the managed dictionary used
the object's inline values.
(cherry picked from commit 5989eb7446)
Co-authored-by: Sam Gross <colesbury@gmail.com>
gh-125590: Allow FrameLocalsProxy to delete and pop keys from extra locals (GH-125616)
(cherry picked from commit 5b7a872b26)
Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
gh-125221: Fix free-threading data race in `object.__reduce_ex__` (GH-125267)
(cherry picked from commit b12e99261e)
Co-authored-by: Sam Gross <colesbury@gmail.com>
* Setting the __module__ attribute for a class now removes the
__firstlineno__ item from the type's dict.
* The _collections_abc and _pydecimal modules now completely replace the
collections.abc and decimal modules after importing them. This
allows to get the source of classes and functions defined in these
modules.
* inspect.findsource() now checks whether the first line number for a
class is out of bound.
(cherry picked from commit 69a4063ca5)
gh-123826: Fix unused function warnings in mimalloc on NetBSD (GH-123827)
(cherry picked from commit 4a6b1f1796)
Co-authored-by: Furkan Onder <furkanonder@protonmail.com>
gh-77894: Fix a crash when the GC breaks a loop containing a memoryview (GH-123898)
Now a memoryview object can only be cleared if there are no buffers
that refer it.
(cherry picked from commit a1dbf2ea69)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
gh-124513: Check args in framelocalsproxy_new() (GH-124515)
Fix a crash in FrameLocalsProxy constructor: check the number of
arguments.
(cherry picked from commit d6954b6421)
Co-authored-by: Victor Stinner <vstinner@python.org>
gh-124498: Fix `TypeAliasType` not to be generic, when `type_params=()` (GH-124499)
(cherry picked from commit abe5f799e6)
Co-authored-by: sobolevn <mail@sobolevn.me>
gh-119004: fix a crash in equality testing between `OrderedDict` (GH-121329)
(cherry picked from commit 38a887dc3e)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
gh-116510: Fix crash due to shared immortal interned strings. (gh-124646)
(cherry picked from commit 98b2ed7e23)
Co-authored-by: Neil Schemenauer <nas-github@arctrix.com>
gh-124538: Fix crash when using `gc.get_referents` on an untracked capsule object (GH-124559)
(cherry picked from commit f923605658)
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Switch more _Py_IsImmortal(...) assertions to _Py_IsImmortalLoose(...)
The remaining calls to _Py_IsImmortal are in free-threaded-only code,
initialization of core objects, tests, and guards that fall back to
code that works with mortal objects.
(cherry picked from commit 57c471a688)
gh-123091: Use _Py_IsImmortalLoose() (#123511)
Use _Py_IsImmortalLoose() in bytesobject.c, typeobject.c
and ceval.c.
(cherry picked from commit f1a0d96f41)
* Parameters after the var-positional parameter are now keyword-only
instead of positional-or-keyword.
* Correctly calculate min_kw_only.
* Raise errors for invalid combinations of the var-positional parameter
with "*", "/" and deprecation markers.
(cherry picked from commit 8393608dd9)
gh-122527: Fix a crash on deallocation of `PyStructSequence` (GH-122577)
The `PyStructSequence` destructor would crash if it was deallocated after
its type's dictionary was cleared by the GC, because it couldn't compute
the "real size" of the instance. This could occur with relatively
straightforward code in the free-threaded build or with a reference
cycle involving the type in the default build, due to differing orders
in which `tp_clear()` was called.
Account for the non-sequence fields in `tp_basicsize` and use that,
along with `Py_SIZE()`, to compute the "real" size of a
`PyStructSequence` in the dealloc function. This avoids the accesses to
the type's dictionary during dealloc, which were unsafe.
(cherry picked from commit 4b63cd170e)
Co-authored-by: Sam Gross <colesbury@gmail.com>
gh-122982: Extend the deprecation period for bool inversion by two years (GH-123306)
(cherry picked from commit 249b083ed8)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Check that the current default heap is initialized in
`_mi_os_get_aligned_hint` and `mi_os_claim_huge_pages`.
The mimalloc function `_mi_os_get_aligned_hint` assumes that there is an
initialized default heap. This is true for our main thread, but not for
background threads. The problematic code path is usually called during
initialization (i.e., `Py_Initialize`), but it may also be called if the
program allocates large amounts of memory in total.
The crash only affected the free-threaded build.
(cherry picked from commit d061ffea7b)
Co-authored-by: Sam Gross <colesbury@gmail.com>
gh-120974: Make asyncio `swap_current_task` safe in free-threaded build (GH-122317)
* gh-120974: Make asyncio `swap_current_task` safe in free-threaded build
(cherry picked from commit b5e6fb39a2)
Co-authored-by: Sam Gross <colesbury@gmail.com>