Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-118789: Add PyUnstable_Object_ClearWeakRefsNoCallbacks #118807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-118789: Add PyUnstable_Weakref_ClearWeakRefsExceptCallbacks
This exposes `_PyWeakref_ClearWeakRefsExceptCallbacks` 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.
  • Loading branch information
colesbury committed May 8, 2024
commit f1fc2bc0cb2db4d00e53eead73ffc5c72e2576af
7 changes: 7 additions & 0 deletions 7 Doc/c-api/weakref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,10 @@ 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_Weakref_ClearWeakRefsExceptCallbacks(PyObject *object)

Clears the weakrefs for *object* without calling the callbacks.
vstinner marked this conversation as resolved.
Show resolved Hide resolved

.. versionadded:: 3.13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's too late for 3.13, we're past beta1.

Suggested change
.. versionadded:: 3.13
.. versionadded:: 3.14

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Yhg1s - is it okay to make this function public in 3.13? From our discussion on Discord in May, it sounded like you were okay with this before the RC, but I'd like to make sure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, considering the circumstances, this is reasonable to add in beta 3.

3 changes: 3 additions & 0 deletions 3 Include/cpython/weakrefobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ Py_DEPRECATED(3.13) static inline PyObject* PyWeakref_GET_OBJECT(PyObject *ref_o
return Py_None;
}
#define PyWeakref_GET_OBJECT(ref) PyWeakref_GET_OBJECT(_PyObject_CAST(ref))

// Clear weakrefs but do not call callbacks
PyAPI_FUNC(void) PyUnstable_Weakref_ClearWeakRefsExceptCallbacks(PyObject *obj);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :c:func:`PyUnstable_Weakref_ClearWeakRefsExceptCallbacks`, which clears
weakrefs without calling their callbacks.
6 changes: 6 additions & 0 deletions 6 Objects/weakrefobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,12 @@ _PyWeakref_ClearWeakRefsExceptCallbacks(PyObject *obj)
UNLOCK_WEAKREFS(obj);
}

void
PyUnstable_Weakref_ClearWeakRefsExceptCallbacks(PyObject *obj)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to check if _PyType_SUPPORTS_WEAKREFS(Py_TYPE(op)) and do nothing (return) if it's not the case.

_PyWeakref_ClearWeakRefsExceptCallbacks(obj);
}

int
_PyWeakref_IsDead(PyObject *weakref)
{
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.