Skip to content

Commit 5caa6cd

Browse files
miss-islingtonrobinnarsinghranabhatkumaraditya303
authored
[3.14] pythongh-137740: Clarify __del__ invocation mechanism in reference counting (pythonGH-137741) (python#138113)
Co-authored-by: Robin Narsingh Ranabhat <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
1 parent 97c6d72 commit 5caa6cd

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
@@ -1059,7 +1059,14 @@ references to all its items, so when item 1 is replaced, it has to dispose of
10591059
the original item 1. Now let's suppose the original item 1 was an instance of a
10601060
user-defined class, and let's further suppose that the class defined a
10611061
:meth:`!__del__` method. If this class instance has a reference count of 1,
1062-
disposing of it will call its :meth:`!__del__` method.
1062+
disposing of it will call its :meth:`!__del__` method. Internally,
1063+
:c:func:`PyList_SetItem` calls :c:func:`Py_DECREF` on the replaced item,
1064+
which invokes replaced item's corresponding
1065+
:c:member:`~PyTypeObject.tp_dealloc` function. During
1066+
deallocation, :c:member:`~PyTypeObject.tp_dealloc` calls
1067+
:c:member:`~PyTypeObject.tp_finalize`, which is mapped to the
1068+
:meth:`!__del__` method for class instances (see :pep:`442`). This entire
1069+
sequence happens synchronously within the :c:func:`PyList_SetItem` call.
10631070

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

0 commit comments

Comments
 (0)