]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-133164: Add `PyUnstable_Object_IsUniqueReferencedTemporary` C API (gh-133170)
authorSam Gross <colesbury@gmail.com>
Fri, 2 May 2025 13:24:57 +0000 (09:24 -0400)
committerGitHub <noreply@github.com>
Fri, 2 May 2025 13:24:57 +0000 (13:24 +0000)
commitf2379535fe2d2219b71653782d5e31defd9b5556
tree97fd405ea675fb919cc95f45e8467d954cedabc9
parent4701ff92d747002d04b67688c7a581b1952773ac
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>
Doc/c-api/object.rst
Doc/c-api/refcounting.rst
Doc/whatsnew/3.14.rst
Include/cpython/object.h
Lib/test/test_capi/test_object.py
Misc/NEWS.d/next/C_API/2025-04-29-19-39-16.gh-issue-133164.W-XTU7.rst [new file with mode: 0644]
Modules/_testcapi/object.c
Objects/object.c