Skip to content

Commit 6665568

Browse files
Update the weak reference support section in the extending tutorial
Weak references should be cleared after calling the finalizer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cfd57f9 commit 6665568

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

‎Doc/extending/newtypes.rst‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,16 @@ references (by calling :c:func:`PyObject_ClearWeakRefs`)::
578578
static void
579579
Trivial_dealloc(PyObject *op)
580580
{
581-
/* Clear weakrefs first before calling any destructors */
581+
/* Clear weakrefs before clearing the object's state */
582582
PyObject_ClearWeakRefs(op);
583583
/* ... remainder of destruction code omitted for brevity ... */
584584
Py_TYPE(op)->tp_free(op);
585585
}
586586

587+
If the type has a finalizer (:c:member:`~PyTypeObject.tp_finalize`),
588+
clear weak references after calling :c:func:`PyObject_CallFinalizerFromDealloc`,
589+
because the finalizer can resurrect the object.
590+
587591

588592
More Suggestions
589593
----------------

0 commit comments

Comments
 (0)