gh-133140: Add PyUnstable_Object_IsUniquelyReferenced for free-threading (#133144)

This commit is contained in:
Peter Bierma 2025-05-05 15:01:20 -04:00 committed by GitHub
parent 0eeaa0ef8b
commit b275b8f342
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 61 additions and 1 deletions

View file

@ -737,3 +737,21 @@ Object Protocol
caller must hold a :term:`strong reference` to *obj* when calling this.
.. versionadded:: 3.14
.. c:function:: int PyUnstable_Object_IsUniquelyReferenced(PyObject *op)
Determine if *op* only has one reference.
On GIL-enabled builds, this function is equivalent to
:c:expr:`Py_REFCNT(op) == 1`.
On a :term:`free threaded <free threading>` build, this checks if *op*'s
:term:`reference count` is equal to one and additionally checks if *op*
is only used by this thread. :c:expr:`Py_REFCNT(op) == 1` is **not**
thread-safe on free threaded builds; prefer this function.
The caller must hold an :term:`attached thread state`, despite the fact
that this function doesn't call into the Python interpreter. This function
cannot fail.
.. versionadded:: 3.14

View file

@ -23,7 +23,14 @@ of Python objects.
Use the :c:func:`Py_SET_REFCNT()` function to set an object reference count.
See also the function :c:func:`PyUnstable_Object_IsUniqueReferencedTemporary()`.
.. note::
On :term:`free threaded <free threading>` builds of Python, returning 1
isn't sufficient to determine if it's safe to treat *o* as having no
access by other threads. Use :c:func:`PyUnstable_Object_IsUniquelyReferenced`
for that instead.
See also the function :c:func:`PyUnstable_Object_IsUniqueReferencedTemporary()`.
.. versionchanged:: 3.10
:c:func:`Py_REFCNT()` is changed to the inline static function.