Skip to content

Commit a6616f2

Browse files
[3.15] gh-145921: Clarify return value of tp_traverse (GH-160455) (GH-160826)
(cherry picked from commit cfe7e0a) Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 493031d commit a6616f2

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎Doc/c-api/gcsupport.rst‎

Lines changed: 10 additions & 1 deletion
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,14 @@ 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, garbage reference
323+
cycles involving the skipped references will not be reliably broken,
324+
but correctness won't be affected.
325+
For example, if traversal requires a lock that cannot be acquired promptly,
326+
skip the traversal.
327+
The garbage collector will visit your object again in a future pass.
328+
320329
.. note::
321330
322331
The :c:func:`Py_VISIT` call may be skipped for those members that provably
@@ -362,7 +371,7 @@ implementation must name its arguments exactly *visit* and *arg*:
362371
This corresponds roughly to::
363372
364373
#define Py_VISIT(o) \
365-
if (op) { \
374+
if (o) { \
366375
int visit_result = visit(o, arg); \
367376
if (visit_result != 0) { \
368377
return visit_result; \

0 commit comments

Comments
 (0)