@@ -1131,6 +1131,9 @@ and :c:data:`PyType_Type` effectively act as defaults.)
11311131
11321132 If this flag is set, :c:macro: `Py_TPFLAGS_HAVE_GC ` should also be set.
11331133
1134+ The type traverse function must call :c:func: `PyObject_VisitManagedDict `
1135+ and its clear function must call :c:func: `PyObject_ClearManagedDict `.
1136+
11341137 .. versionadded :: 3.12
11351138
11361139 **Inheritance: **
@@ -1368,6 +1371,23 @@ and :c:data:`PyType_Type` effectively act as defaults.)
13681371 debugging aid you may want to visit it anyway just so the :mod: `gc ` module's
13691372 :func: `~gc.get_referents ` function will include it.
13701373
1374+ Heap types (:c:macro: `Py_TPFLAGS_HEAPTYPE `) must visit their type with::
1375+
1376+ Py_VISIT(Py_TYPE(self));
1377+
1378+ It is only needed since Python 3.9. To support Python 3.8 and older, this
1379+ line must be conditionnal::
1380+
1381+ #if PY_VERSION_HEX >= 0x03090000
1382+ Py_VISIT(Py_TYPE(self));
1383+ #endif
1384+
1385+ If the :c:macro: `Py_TPFLAGS_MANAGED_DICT ` bit is set in the
1386+ :c:member: `~PyTypeObject.tp_flags ` field, the traverse function must call
1387+ :c:func: `PyObject_VisitManagedDict ` like this::
1388+
1389+ PyObject_VisitManagedDict((PyObject*)self, visit, arg);
1390+
13711391 .. warning ::
13721392 When implementing :c:member: `~PyTypeObject.tp_traverse `, only the
13731393 members that the instance *owns * (by having :term: `strong references
@@ -1451,6 +1471,12 @@ and :c:data:`PyType_Type` effectively act as defaults.)
14511471 so that *self * knows the contained object can no longer be used. The
14521472 :c:func: `Py_CLEAR ` macro performs the operations in a safe order.
14531473
1474+ If the :c:macro: `Py_TPFLAGS_MANAGED_DICT ` bit is set in the
1475+ :c:member: `~PyTypeObject.tp_flags ` field, the traverse function must call
1476+ :c:func: `PyObject_ClearManagedDict ` like this::
1477+
1478+ PyObject_ClearManagedDict((PyObject*)self);
1479+
14541480 Note that :c:member: `~PyTypeObject.tp_clear ` is not *always * called
14551481 before an instance is deallocated. For example, when reference counting
14561482 is enough to determine that an object is no longer used, the cyclic garbage
@@ -1801,7 +1827,7 @@ and :c:data:`PyType_Type` effectively act as defaults.)
18011827 field is ``NULL `` then no :attr: `~object.__dict__ ` gets created for instances.
18021828
18031829 If the :c:macro: `Py_TPFLAGS_MANAGED_DICT ` bit is set in the
1804- :c:member: `~PyTypeObject.tp_dict ` field, then
1830+ :c:member: `~PyTypeObject.tp_flags ` field, then
18051831 :c:member: `~PyTypeObject.tp_dictoffset ` will be set to ``-1 ``, to indicate
18061832 that it is unsafe to use this field.
18071833
0 commit comments