https://github.com/python/cpython/commit/238a1ca713f6259ee00e1d5e3cf8cb6abe126007
commit: 238a1ca713f6259ee00e1d5e3cf8cb6abe126007
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-08-26T23:43:42+05:30
summary:

[3.13] gh-137740: Clarify `__del__` invocation mechanism in reference counting 
(GH-137741) (#138112)

gh-137740: Clarify `__del__` invocation mechanism in reference counting 
(GH-137741)
(cherry picked from commit 90b932e65080008dfd974b2e03c3068dbb72b95d)

Co-authored-by: Robin Narsingh Ranabhat <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>

files:
M Doc/extending/extending.rst

diff --git a/Doc/extending/extending.rst b/Doc/extending/extending.rst
index 17c6fb224265ca..f9b65643dfe888 100644
--- a/Doc/extending/extending.rst
+++ b/Doc/extending/extending.rst
@@ -1084,7 +1084,14 @@ references to all its items, so when item 1 is replaced, 
it has to dispose of
 the original item 1.  Now let's suppose the original item 1 was an instance of 
a
 user-defined class, and let's further suppose that the class defined a
 :meth:`!__del__` method.  If this class instance has a reference count of 1,
-disposing of it will call its :meth:`!__del__` method.
+disposing of it will call its :meth:`!__del__` method. Internally,
+:c:func:`PyList_SetItem` calls :c:func:`Py_DECREF` on the replaced item,
+which invokes replaced item's corresponding
+:c:member:`~PyTypeObject.tp_dealloc` function. During
+deallocation, :c:member:`~PyTypeObject.tp_dealloc` calls
+:c:member:`~PyTypeObject.tp_finalize`, which is mapped to the
+:meth:`!__del__` method for class instances (see :pep:`442`). This entire
+sequence happens synchronously within the :c:func:`PyList_SetItem` call.
 
 Since it is written in Python, the :meth:`!__del__` method can execute 
arbitrary
 Python code.  Could it perhaps do something to invalidate the reference to

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to