[Python-checkins] Note that tp_clear and m_clear are not always called (GH-27581)

ambv webhook-mailer at python.org
Wed Aug 4 14:01:40 EDT 2021


https://github.com/python/cpython/commit/10faada709561663d6b1f623d308ff45e3808cca
commit: 10faada709561663d6b1f623d308ff45e3808cca
branch: main
author: Petr Viktorin <encukou at gmail.com>
committer: ambv <lukasz at langa.pl>
date: 2021-08-04T20:01:31+02:00
summary:

Note that tp_clear and m_clear are not always called (GH-27581)

files:
M Doc/c-api/module.rst
M Doc/c-api/typeobj.rst

diff --git a/Doc/c-api/module.rst b/Doc/c-api/module.rst
index a2541afb685c3..94c8d9f981713 100644
--- a/Doc/c-api/module.rst
+++ b/Doc/c-api/module.rst
@@ -221,6 +221,12 @@ or request "multi-phase initialization" by returning the definition struct itsel
       than 0 and the module state (as returned by :c:func:`PyModule_GetState`)
       is ``NULL``.
 
+      Like :c:member:`PyTypeObject.tp_clear`, this function is not *always*
+      called before a module is deallocated. For example, when reference
+      counting is enough to determine that an object is no longer used,
+      the cyclic garbage collector is not involved and
+      :c:member:`~PyModuleDef.m_free` is called directly.
+
       .. versionchanged:: 3.9
          No longer called before the module state is allocated.
 
diff --git a/Doc/c-api/typeobj.rst b/Doc/c-api/typeobj.rst
index b18e3852be3ad..b17fb22b69495 100644
--- a/Doc/c-api/typeobj.rst
+++ b/Doc/c-api/typeobj.rst
@@ -1381,6 +1381,12 @@ and :c:type:`PyType_Type` effectively act as defaults.)
    so that *self* knows the contained object can no longer be used.  The
    :c:func:`Py_CLEAR` macro performs the operations in a safe order.
 
+   Note that :c:member:`~PyTypeObject.tp_clear` is not *always* called
+   before an instance is deallocated. For example, when reference counting
+   is enough to determine that an object is no longer used, the cyclic garbage
+   collector is not involved and :c:member:`~PyTypeObject.tp_dealloc` is
+   called directly.
+
    Because the goal of :c:member:`~PyTypeObject.tp_clear` functions is to break reference cycles,
    it's not necessary to clear contained objects like Python strings or Python
    integers, which can't participate in reference cycles. On the other hand, it may



More information about the Python-checkins mailing list