Skip to content

Commit 8604993

Browse files
gh-137740: Clarify __del__ invocation mechanism in reference counting example
1 parent a10152f commit 8604993

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Doc/extending/extending.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,12 @@ references to all its items, so when item 1 is replaced, it has to dispose of
10841084
the original item 1. Now let's suppose the original item 1 was an instance of a
10851085
user-defined class, and let's further suppose that the class defined a
10861086
:meth:`!__del__` method. If this class instance has a reference count of 1,
1087-
disposing of it will call its :meth:`!__del__` method.
1087+
disposing of it will call its :meth:`!__del__` method. Internally,
1088+
:c:func:`PyList_SetItem` calls :c:func:`Py_XDECREF` on the replaced item,
1089+
which invokes the object's ``tp_dealloc`` function (``subtype_dealloc`` for Python
1090+
class instances). During deallocation, ``subtype_dealloc`` calls ``tp_finalize``,
1091+
which is mapped to the ``__del__`` method for Python classes (see :pep:`442`).
1092+
This entire sequence happens synchronously within the :c:func:`PyList_SetItem` call.
10881093

10891094
Since it is written in Python, the :meth:`!__del__` method can execute arbitrary
10901095
Python code. Could it perhaps do something to invalidate the reference to

0 commit comments

Comments
 (0)