Skip to content

Commit 2ad9430

Browse files
colesburymiss-islington
authored andcommitted
pythongh-123241: Document restrictions for tp_traverse implementations (pythongh-142272)
(cherry picked from commit 37988c5) Co-authored-by: Sam Gross <[email protected]>
1 parent 9bb27cd commit 2ad9430

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
@@ -232,6 +232,10 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
232232
object argument. If *visit* returns a non-zero value that value should be
233233
returned immediately.
234234
235+
The traversal function must not have any side effects. Implementations
236+
may not modify the reference counts of any Python objects nor create or
237+
destroy any Python objects.
238+
235239
To simplify writing :c:member:`~PyTypeObject.tp_traverse` handlers, a :c:func:`Py_VISIT` macro is
236240
provided. In order to use this macro, the :c:member:`~PyTypeObject.tp_traverse` implementation
237241
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
@@ -1554,6 +1554,11 @@ and :c:data:`PyType_Type` effectively act as defaults.)
15541554
but the instance has no strong reference to the elements inside it, as they
15551555
are allowed to be removed even if the instance is still alive).
15561556

1557+
.. warning::
1558+
The traversal function must not have any side effects. It must not
1559+
modify the reference counts of any Python objects nor create or destroy
1560+
any Python objects.
1561+
15571562
Note that :c:func:`Py_VISIT` requires the *visit* and *arg* parameters to
15581563
:c:func:`!local_traverse` to have these specific names; don't name them just
15591564
anything.

0 commit comments

Comments
 (0)