Skip to content

Commit 10faada

Browse files
authored
Note that tp_clear and m_clear are not always called (GH-27581)
1 parent 3d2b4c6 commit 10faada

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Doc/c-api/module.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ or request "multi-phase initialization" by returning the definition struct itsel
221221
than 0 and the module state (as returned by :c:func:`PyModule_GetState`)
222222
is ``NULL``.
223223
224+
Like :c:member:`PyTypeObject.tp_clear`, this function is not *always*
225+
called before a module is deallocated. For example, when reference
226+
counting is enough to determine that an object is no longer used,
227+
the cyclic garbage collector is not involved and
228+
:c:member:`~PyModuleDef.m_free` is called directly.
229+
224230
.. versionchanged:: 3.9
225231
No longer called before the module state is allocated.
226232

Doc/c-api/typeobj.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,12 @@ and :c:type:`PyType_Type` effectively act as defaults.)
13811381
so that *self* knows the contained object can no longer be used. The
13821382
:c:func:`Py_CLEAR` macro performs the operations in a safe order.
13831383

1384+
Note that :c:member:`~PyTypeObject.tp_clear` is not *always* called
1385+
before an instance is deallocated. For example, when reference counting
1386+
is enough to determine that an object is no longer used, the cyclic garbage
1387+
collector is not involved and :c:member:`~PyTypeObject.tp_dealloc` is
1388+
called directly.
1389+
13841390
Because the goal of :c:member:`~PyTypeObject.tp_clear` functions is to break reference cycles,
13851391
it's not necessary to clear contained objects like Python strings or Python
13861392
integers, which can't participate in reference cycles. On the other hand, it may

0 commit comments

Comments
 (0)