gh-118789: Add `PyUnstable_Object_ClearWeakRefsNoCallbacks` by colesbury · Pull Request #118807 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 16 additions & 0 deletions Doc/c-api/weakref.rst
2 changes: 2 additions & 0 deletions Include/cpython/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ClearWeakRefsNoCallbacks(PyObject *);

/* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
dict as the last parameter. */
PyAPI_FUNC(PyObject *)
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_weakref.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

PyAPI_FUNC(int) _PyWeakref_IsDead(PyObject *weakref);

Expand Down
28 changes: 28 additions & 0 deletions Lib/test/test_capi/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :c:func:`PyUnstable_Object_ClearWeakRefsNoCallbacks`, which clears
weakrefs without calling their callbacks.
8 changes: 8 additions & 0 deletions Modules/_testcapi/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down
2 changes: 1 addition & 1 deletion Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2531,7 +2531,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);
}
}

Expand Down
12 changes: 10 additions & 2 deletions Objects/weakrefobject.c