]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-123241: Document restrictions for `tp_traverse` implementations (gh-142272)
authorSam Gross <colesbury@gmail.com>
Mon, 8 Dec 2025 17:22:13 +0000 (12:22 -0500)
committerGitHub <noreply@github.com>
Mon, 8 Dec 2025 17:22:13 +0000 (12:22 -0500)
Doc/c-api/gcsupport.rst
Doc/c-api/typeobj.rst

index f6fa52b36c5ab31a1ab3909fd1bceb6d6b68ba41..fed795b1e8c963715b3ac9c487fb6371b0039be6 100644 (file)
@@ -232,6 +232,10 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
    object argument.  If *visit* returns a non-zero value that value should be
    returned immediately.
 
+   The traversal function must not have any side effects.  Implementations
+   may not modify the reference counts of any Python objects nor create or
+   destroy any Python objects.
+
 To simplify writing :c:member:`~PyTypeObject.tp_traverse` handlers, a :c:func:`Py_VISIT` macro is
 provided.  In order to use this macro, the :c:member:`~PyTypeObject.tp_traverse` implementation
 must name its arguments exactly *visit* and *arg*:
index 49fe02d919df8b50f25963299fcb85240b7d9b43..efac86078f9af559a4131425a47601c0a12b0a44 100644 (file)
@@ -1569,6 +1569,11 @@ and :c:data:`PyType_Type` effectively act as defaults.)
        but the instance has no strong reference to the elements inside it, as they
        are allowed to be removed even if the instance is still alive).
 
+   .. warning::
+      The traversal function must not have any side effects.  It must not
+      modify the reference counts of any Python objects nor create or destroy
+      any Python objects.
+
    Note that :c:func:`Py_VISIT` requires the *visit* and *arg* parameters to
    :c:func:`!local_traverse` to have these specific names; don't name them just
    anything.