From f1fc2bc0cb2db4d00e53eead73ffc5c72e2576af Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Wed, 8 May 2024 22:00:13 +0000 Subject: [PATCH 1/5] 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. --- Doc/c-api/weakref.rst | 7 +++++++ Include/cpython/weakrefobject.h | 3 +++ .../C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst | 2 ++ Objects/weakrefobject.c | 6 ++++++ 4 files changed, 18 insertions(+) create mode 100644 Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst diff --git a/Doc/c-api/weakref.rst b/Doc/c-api/weakref.rst index 038f54a9751fd1..fdfa149a1ed602 100644 --- a/Doc/c-api/weakref.rst +++ b/Doc/c-api/weakref.rst @@ -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. + + .. versionadded:: 3.13 diff --git a/Include/cpython/weakrefobject.h b/Include/cpython/weakrefobject.h index 9a796098c6b48f..5b616926c3d5cd 100644 --- a/Include/cpython/weakrefobject.h +++ b/Include/cpython/weakrefobject.h @@ -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); diff --git a/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst b/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst new file mode 100644 index 00000000000000..b4088d7a00ff61 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst @@ -0,0 +1,2 @@ +Add :c:func:`PyUnstable_Weakref_ClearWeakRefsExceptCallbacks`, which clears +weakrefs without calling their callbacks. diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 88afaec86827ed..b1e33173a2dad3 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -1090,6 +1090,12 @@ _PyWeakref_ClearWeakRefsExceptCallbacks(PyObject *obj) UNLOCK_WEAKREFS(obj); } +void +PyUnstable_Weakref_ClearWeakRefsExceptCallbacks(PyObject *obj) +{ + _PyWeakref_ClearWeakRefsExceptCallbacks(obj); +} + int _PyWeakref_IsDead(PyObject *weakref) { From 9330f5130f33f12b6c29df47ab75be50ca878e79 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 9 May 2024 15:39:21 +0000 Subject: [PATCH 2/5] Adjust name and check _PyType_SUPPORTS_WEAKREFS --- Doc/c-api/weakref.rst | 2 +- Include/cpython/object.h | 2 ++ Include/cpython/weakrefobject.h | 3 --- .../2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst | 2 +- Objects/weakrefobject.c | 14 ++++++++------ 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Doc/c-api/weakref.rst b/Doc/c-api/weakref.rst index fdfa149a1ed602..c07f6bfa717e7e 100644 --- a/Doc/c-api/weakref.rst +++ b/Doc/c-api/weakref.rst @@ -98,7 +98,7 @@ as much as it can. been attempted. -.. c:function:: void PyUnstable_Weakref_ClearWeakRefsExceptCallbacks(PyObject *object) +.. c:function:: void PyUnstable_Object_ClearWeakRefsExceptCallbacks(PyObject *object) Clears the weakrefs for *object* without calling the callbacks. diff --git a/Include/cpython/object.h b/Include/cpython/object.h index e624326693d8e7..563c80ba05bb55 100644 --- a/Include/cpython/object.h +++ b/Include/cpython/object.h @@ -288,6 +288,8 @@ PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *); PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *); PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *); +PyAPI_FUNC(void) PyUnstable_Object_ClearWeakRefsExceptCallbacks(PyObject *); + /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes dict as the last parameter. */ PyAPI_FUNC(PyObject *) diff --git a/Include/cpython/weakrefobject.h b/Include/cpython/weakrefobject.h index 5b616926c3d5cd..9a796098c6b48f 100644 --- a/Include/cpython/weakrefobject.h +++ b/Include/cpython/weakrefobject.h @@ -59,6 +59,3 @@ 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); diff --git a/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst b/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst index b4088d7a00ff61..332e0110483549 100644 --- a/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst +++ b/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst @@ -1,2 +1,2 @@ -Add :c:func:`PyUnstable_Weakref_ClearWeakRefsExceptCallbacks`, which clears +Add :c:func:`PyUnstable_Object_ClearWeakRefsExceptCallbacks`, which clears weakrefs without calling their callbacks. diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index b1e33173a2dad3..05cef6896ed78c 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -1057,6 +1057,14 @@ PyObject_ClearWeakRefs(PyObject *object) PyErr_SetRaisedException(exc); } +void +PyUnstable_Object_ClearWeakRefsExceptCallbacks(PyObject *obj) +{ + if (_PyType_SUPPORTS_WEAKREFS(Py_TYPE(obj))) { + _PyWeakref_ClearWeakRefsExceptCallbacks(obj); + } +} + /* This function is called by _PyStaticType_Dealloc() to clear weak references. * * This is called at the end of runtime finalization, so we can just @@ -1090,12 +1098,6 @@ _PyWeakref_ClearWeakRefsExceptCallbacks(PyObject *obj) UNLOCK_WEAKREFS(obj); } -void -PyUnstable_Weakref_ClearWeakRefsExceptCallbacks(PyObject *obj) -{ - _PyWeakref_ClearWeakRefsExceptCallbacks(obj); -} - int _PyWeakref_IsDead(PyObject *weakref) { From dc28627fb6a4b05217511d735cc2c5b031b85f03 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Thu, 6 Jun 2024 16:57:30 +0000 Subject: [PATCH 3/5] Rename to PyUnstable_Object_ClearWeakRefsNoCallbacks --- Doc/c-api/weakref.rst | 2 +- Include/cpython/object.h | 2 +- Include/internal/pycore_weakref.h | 2 +- .../C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst | 2 +- Objects/typeobject.c | 2 +- Objects/weakrefobject.c | 8 ++++---- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Doc/c-api/weakref.rst b/Doc/c-api/weakref.rst index c07f6bfa717e7e..12aeb20d0cdb55 100644 --- a/Doc/c-api/weakref.rst +++ b/Doc/c-api/weakref.rst @@ -98,7 +98,7 @@ as much as it can. been attempted. -.. c:function:: void PyUnstable_Object_ClearWeakRefsExceptCallbacks(PyObject *object) +.. c:function:: void PyUnstable_Object_ClearWeakRefsNoCallbacks(PyObject *object) Clears the weakrefs for *object* without calling the callbacks. diff --git a/Include/cpython/object.h b/Include/cpython/object.h index 563c80ba05bb55..6cb2c40fe2eb71 100644 --- a/Include/cpython/object.h +++ b/Include/cpython/object.h @@ -288,7 +288,7 @@ PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *); PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *); PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *); -PyAPI_FUNC(void) PyUnstable_Object_ClearWeakRefsExceptCallbacks(PyObject *); +PyAPI_FUNC(void) PyUnstable_Object_ClearWeakRefsNoCallbacks(PyObject *); /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes dict as the last parameter. */ diff --git a/Include/internal/pycore_weakref.h b/Include/internal/pycore_weakref.h index e057a27340f718..94f637bcc31258 100644 --- a/Include/internal/pycore_weakref.h +++ b/Include/internal/pycore_weakref.h @@ -109,7 +109,7 @@ extern Py_ssize_t _PyWeakref_GetWeakrefCount(PyObject *obj); // Clear all the weak references to obj but leave their callbacks uncalled and // intact. -extern void _PyWeakref_ClearWeakRefsExceptCallbacks(PyObject *obj); +extern void _PyWeakref_ClearWeakRefsNoCallbacks(PyObject *obj); extern void _PyWeakref_ClearRef(PyWeakReference *self); diff --git a/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst b/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst index 332e0110483549..32a9ec6d0710f6 100644 --- a/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst +++ b/Misc/NEWS.d/next/C API/2024-05-08-21-57-50.gh-issue-118789.Ni4UQx.rst @@ -1,2 +1,2 @@ -Add :c:func:`PyUnstable_Object_ClearWeakRefsExceptCallbacks`, which clears +Add :c:func:`PyUnstable_Object_ClearWeakRefsNoCallbacks`, which clears weakrefs without calling their callbacks. diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 4b144fab5de8f1..bf3072c4a746e0 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -2253,7 +2253,7 @@ subtype_dealloc(PyObject *self) finalizers since they might rely on part of the object being finalized that has already been destroyed. */ if (type->tp_weaklistoffset && !base->tp_weaklistoffset) { - _PyWeakref_ClearWeakRefsExceptCallbacks(self); + _PyWeakref_ClearWeakRefsNoCallbacks(self); } } diff --git a/Objects/weakrefobject.c b/Objects/weakrefobject.c index 05cef6896ed78c..4a69abe72d674e 100644 --- a/Objects/weakrefobject.c +++ b/Objects/weakrefobject.c @@ -1016,7 +1016,7 @@ PyObject_ClearWeakRefs(PyObject *object) PyObject *exc = PyErr_GetRaisedException(); PyObject *tuple = PyTuple_New(num_weakrefs * 2); if (tuple == NULL) { - _PyWeakref_ClearWeakRefsExceptCallbacks(object); + _PyWeakref_ClearWeakRefsNoCallbacks(object); PyErr_WriteUnraisable(NULL); PyErr_SetRaisedException(exc); return; @@ -1058,10 +1058,10 @@ PyObject_ClearWeakRefs(PyObject *object) } void -PyUnstable_Object_ClearWeakRefsExceptCallbacks(PyObject *obj) +PyUnstable_Object_ClearWeakRefsNoCallbacks(PyObject *obj) { if (_PyType_SUPPORTS_WEAKREFS(Py_TYPE(obj))) { - _PyWeakref_ClearWeakRefsExceptCallbacks(obj); + _PyWeakref_ClearWeakRefsNoCallbacks(obj); } } @@ -1084,7 +1084,7 @@ _PyStaticType_ClearWeakRefs(PyInterpreterState *interp, PyTypeObject *type) } void -_PyWeakref_ClearWeakRefsExceptCallbacks(PyObject *obj) +_PyWeakref_ClearWeakRefsNoCallbacks(PyObject *obj) { /* Modeled after GET_WEAKREFS_LISTPTR(). From 652c1c1814ae96146ff3f5411b166e505f033a1b Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Fri, 7 Jun 2024 11:27:04 +0200 Subject: [PATCH 4/5] Add test for PyUnstable_Object_ClearWeakRefsNoCallbacks --- Lib/test/test_capi/test_object.py | 28 ++++++++++++++++++++++++++++ Modules/_testcapi/object.c | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/Lib/test/test_capi/test_object.py b/Lib/test/test_capi/test_object.py index fa23bff4e98918..cc9c9b688f00e2 100644 --- a/Lib/test/test_capi/test_object.py +++ b/Lib/test/test_capi/test_object.py @@ -103,5 +103,33 @@ def testPyObjectPrintOSError(self): with self.assertRaises(OSError): _testcapi.pyobject_print_os_error(output_filename) + +class ClearWeakRefsNoCallbacksTest(unittest.TestCase): + """Test PyUnstable_Object_ClearWeakRefsNoCallbacks""" + def test_ClearWeakRefsNoCallbacks(self): + """Ensure PyUnstable_Object_ClearWeakRefsNoCallbacks works""" + import weakref + import gc + class C: + pass + obj = C() + messages = [] + ref = weakref.ref(obj, lambda: messages.append("don't add this")) + self.assertIs(ref(), obj) + self.assertFalse(messages) + _testcapi.pyobject_clear_weakrefs_no_callbacks(obj) + self.assertIsNone(ref()) + gc.collect() + self.assertFalse(messages) + + def test_ClearWeakRefsNoCallbacks_no_weakref_support(self): + """Don't fail on objects that don't support weakrefs""" + import weakref + obj = object() + with self.assertRaises(TypeError): + ref = weakref.ref(obj) + _testcapi.pyobject_clear_weakrefs_no_callbacks(obj) + + if __name__ == "__main__": unittest.main() diff --git a/Modules/_testcapi/object.c b/Modules/_testcapi/object.c index 8dd34cf4fc47d4..1c76e766a790f0 100644 --- a/Modules/_testcapi/object.c +++ b/Modules/_testcapi/object.c @@ -117,11 +117,19 @@ pyobject_print_os_error(PyObject *self, PyObject *args) Py_RETURN_NONE; } +static PyObject * +pyobject_clear_weakrefs_no_callbacks(PyObject *self, PyObject *obj) +{ + PyUnstable_Object_ClearWeakRefsNoCallbacks(obj); + Py_RETURN_NONE; +} + static PyMethodDef test_methods[] = { {"call_pyobject_print", call_pyobject_print, METH_VARARGS}, {"pyobject_print_null", pyobject_print_null, METH_VARARGS}, {"pyobject_print_noref_object", pyobject_print_noref_object, METH_VARARGS}, {"pyobject_print_os_error", pyobject_print_os_error, METH_VARARGS}, + {"pyobject_clear_weakrefs_no_callbacks", pyobject_clear_weakrefs_no_callbacks, METH_O}, {NULL}, }; From 519a347603b81b7eb23620b97ecb6b59720e1bf2 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Mon, 17 Jun 2024 17:07:32 +0000 Subject: [PATCH 5/5] Add more context for when to use PyUnstable_Object_ClearWeakRefsNoCallbacks --- Doc/c-api/weakref.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Doc/c-api/weakref.rst b/Doc/c-api/weakref.rst index 12aeb20d0cdb55..5b13dfce22f91e 100644 --- a/Doc/c-api/weakref.rst +++ b/Doc/c-api/weakref.rst @@ -102,4 +102,13 @@ as much as it can. 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