gh-118789: Add PyUnstable_Object_ClearWeakRefsNoCallbacks (#118807)

This exposes `PyUnstable_Object_ClearWeakRefsNoCallbacks` as an unstable
C-API function to provide a thread-safe mechanism for clearing weakrefs
without executing callbacks.

Some C-API extensions need to clear weakrefs without calling callbacks,
such as after running finalizers like we do in subtype_dealloc.
Previously they could use `_PyWeakref_ClearRef` on each weakref, but
that's not thread-safe in the free-threaded build.

Co-authored-by: Petr Viktorin <encukou@gmail.com>
This commit is contained in:
Sam Gross 2024-06-18 09:57:23 -04:00 committed by GitHub
parent 360f14a493
commit e8752d7b80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 68 additions and 4 deletions

View file

@ -96,3 +96,19 @@ as much as it can.
This iterates through the weak references for *object* and calls callbacks
for those references which have one. It returns when all callbacks have
been attempted.
.. c:function:: void PyUnstable_Object_ClearWeakRefsNoCallbacks(PyObject *object)
Clears the weakrefs for *object* without calling the callbacks.
This function is called by the :c:member:`~PyTypeObject.tp_dealloc` handler
for types with finalizers (i.e., :meth:`~object.__del__`). The handler for
those objects first calls :c:func:`PyObject_ClearWeakRefs` to clear weakrefs
and call their callbacks, then the finalizer, and finally this function to
clear any weakrefs that may have been created by the finalizer.
In most circumstances, it's more appropriate to use
:c:func:`PyObject_ClearWeakRefs` to clear weakrefs instead of this function.
.. versionadded:: 3.13