Skip to content

Commit 2cff2b5

Browse files
committed
Add documentation about the change
1 parent a7ccd4d commit 2cff2b5

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Doc/whatsnew/3.8.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,25 @@ Changes in the C API
20792079
#endif
20802080
}
20812081
2082+
* Ensure that all custom ``tp_traverse`` functions of heap-allocated types
2083+
visit the object parent **but not for subclasses**.
2084+
2085+
Example:
2086+
2087+
.. code-block:: c
2088+
2089+
int
2090+
foo_traverse(foo_struct *self, visitproc visit, void *arg) {
2091+
// Rest of the traverse function
2092+
#if PY_VERSION_HEX >= 0x03080000
2093+
// This was not needed before Python 3.8 (Python issue 35810)
2094+
PyObject *type = Py_TYPE(instance);
2095+
if (type->tp_traverse == (traverseproc)foo_traverse) {
2096+
Py_VISIT(type);
2097+
}
2098+
#endif
2099+
}
2100+
20822101
(Contributed by Eddie Elizondo in :issue:`35810`.)
20832102

20842103
* The :c:macro:`Py_DEPRECATED()` macro has been implemented for MSVC.

Doc/whatsnew/3.9.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,35 @@ Changes in the Python API
933933
(Contributed by Inada Naoki in :issue:`34538`.)
934934

935935

936+
Changes in the C API
937+
--------------------
938+
939+
* Instances of heap-allocated types (such as those created with
940+
:c:func:`PyType_FromSpec` and similar APIs) hold a reference to their type
941+
object since Python 3.8. As indicated in the "Changes in the C API" of Python
942+
3.8, for the vast majority of cases, there should be no side effect but for
943+
types that have a custom :c:member:`~PyTypeObject.tp_traverse` function,
944+
ensure that all custom ``tp_traverse`` functions of heap-allocated types
945+
visit the object parent **but not for subclasses**.
946+
947+
Example:
948+
949+
.. code-block:: c
950+
951+
int
952+
foo_traverse(foo_struct *self, visitproc visit, void *arg) {
953+
// Rest of the traverse function
954+
#if PY_VERSION_HEX >= 0x03080000
955+
// This was not needed before Python 3.8 (Python issue 35810 and 40217)
956+
PyObject *type = Py_TYPE(instance);
957+
if (type->tp_traverse == (traverseproc)foo_traverse) {
958+
Py_VISIT(type);
959+
}
960+
#endif
961+
}
962+
963+
(See :issue:`35810` and :issue:`40217` for more information.)
964+
936965
CPython bytecode changes
937966
------------------------
938967

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Instances of types created with :c:func:`PyType_FromSpecWithBases` will no
2+
longer visit automaticallyt its class object when traversing references in
3+
the garbage collector. The user is expected to manually visit the parent
4+
class (but not for subclasses). Patch by Pablo Galindo.

0 commit comments

Comments
 (0)