Skip to content

Commit 03d41f6

Browse files
committed
gh-145921: Clarify return value of tp_traverse
1 parent 0615b5c commit 03d41f6

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

‎Doc/c-api/gcsupport.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
251251
to the handler. The *visit* function must not be called with a ``NULL``
252252
object argument. If *visit* returns a non-zero value, that value should be
253253
returned immediately.
254+
Otherwise, return zero.
254255
255256
A typical :c:member:`!tp_traverse` function calls the :c:func:`Py_VISIT`
256257
convenience macro on each of the instance's members that are Python
@@ -317,6 +318,13 @@ The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
317318
For a list of safe functions, see a
318319
:ref:`separate section <duringgc-functions>` below.
319320
321+
If an error occurs during traversal, ignore it and return 0.
322+
If ``tp_traverse`` fails to visit some strong references, objects
323+
may become unreachable and waste memory, but correctness won't be affected.
324+
For example, if traversal requires a lock that cannot be acquired promptly,
325+
skip the traversal.
326+
The garbage collector will visit your object again in a future pass.
327+
320328
.. note::
321329
322330
The :c:func:`Py_VISIT` call may be skipped for those members that provably

0 commit comments

Comments
 (0)