Skip to content

Commit 90b932e

Browse files
pythongh-137740: Clarify __del__ invocation mechanism in reference counting (python#137741)
Co-authored-by: Kumar Aditya <[email protected]>
1 parent f278afc commit 90b932e

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Doc/extending/extending.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,14 @@ 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_DECREF` on the replaced item,
1089+
which invokes replaced item's corresponding
1090+
:c:member:`~PyTypeObject.tp_dealloc` function. During
1091+
deallocation, :c:member:`~PyTypeObject.tp_dealloc` calls
1092+
:c:member:`~PyTypeObject.tp_finalize`, which is mapped to the
1093+
:meth:`!__del__` method for class instances (see :pep:`442`). This entire
1094+
sequence happens synchronously within the :c:func:`PyList_SetItem` call.
10881095

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

0 commit comments

Comments
 (0)