Skip to content

Commit 2c3e3ef

Browse files
[3.13] gh-123241: Document restrictions for tp_traverse implementations (gh-142272) (#142422)
(cherry picked from commit 37988c5) Co-authored-by: Sam Gross <[email protected]>
1 parent 2ef7698 commit 2c3e3ef

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

Doc/c-api/gcsupport.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
175175
object argument. If *visit* returns a non-zero value that value should be
176176
returned immediately.
177177
178+
The traversal function must not have any side effects. Implementations
179+
may not modify the reference counts of any Python objects nor create or
180+
destroy any Python objects.
181+
178182
To simplify writing :c:member:`~PyTypeObject.tp_traverse` handlers, a :c:func:`Py_VISIT` macro is
179183
provided. In order to use this macro, the :c:member:`~PyTypeObject.tp_traverse` implementation
180184
must name its arguments exactly *visit* and *arg*:

Doc/c-api/typeobj.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,6 +1483,11 @@ and :c:data:`PyType_Type` effectively act as defaults.)
14831483
but the instance has no strong reference to the elements inside it, as they
14841484
are allowed to be removed even if the instance is still alive).
14851485

1486+
.. warning::
1487+
The traversal function must not have any side effects. It must not
1488+
modify the reference counts of any Python objects nor create or destroy
1489+
any Python objects.
1490+
14861491
Note that :c:func:`Py_VISIT` requires the *visit* and *arg* parameters to
14871492
:c:func:`!local_traverse` to have these specific names; don't name them just
14881493
anything.

0 commit comments

Comments
 (0)