From 4bbb9164f21f103a17eabf117369c0d035bb9949 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Salgado Date: Tue, 24 Nov 2026 12:11:20 +0000 Subject: [PATCH] gh-162407: Clarify the consistency requirement for tp_traverse --- Doc/c-api/gcsupport.rst | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Doc/c-api/gcsupport.rst b/Doc/c-api/gcsupport.rst index 2afade177674a2e..0ebe719af4ff4d4 100644 --- a/Doc/c-api/gcsupport.rst +++ b/Doc/c-api/gcsupport.rst @@ -253,6 +253,13 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type: returned immediately. Otherwise, return zero. + The garbage collector can call :c:member:`~PyTypeObject.tp_traverse` more + than once for the same object during a collection. If the object's + references do not change, each traversal must visit the same references. + In particular, do not skip a reference because a lock is busy. + If one traversal visits a reference and a later traversal skips it, the + collector can clear an object that is still reachable. + A typical :c:member:`!tp_traverse` function calls the :c:func:`Py_VISIT` convenience macro on each of the instance's members that are Python objects that the instance owns. @@ -318,14 +325,6 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type: For a list of safe functions, see a :ref:`separate section ` below. - If an error occurs during traversal, ignore it and return 0. - If ``tp_traverse`` fails to visit some strong references, garbage reference - cycles involving the skipped references will not be reliably broken, - but correctness won't be affected. - For example, if traversal requires a lock that cannot be acquired promptly, - skip the traversal. - The garbage collector will visit your object again in a future pass. - .. note:: The :c:func:`Py_VISIT` call may be skipped for those members that provably