Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa6389e

Browse files
author
Jim Fulton
committed
Documented the new Py_VISIT macro to simplify implementation of
tp_traverse handlers. (Tim made me do it. ;)
1 parent 8c5aeaa commit aa6389e

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

Doc/api/newtypes.tex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,29 @@ \section{Supporting Cyclic Garbage Collection
16631663
that value should be returned immediately.
16641664
\end{ctypedesc}
16651665

1666+
To simplify writing \member{tp_traverse} handlers, a
1667+
\cfunction{Py_VISIT()} is provided:
1668+
1669+
\begin{cfuncdesc}{void}{Py_VISIT}{PyObject *o}
1670+
Call the \var{visit} for \var{o} with \var{arg}. If \var{visit}
1671+
returns a non-zero value, then return it. Using this macro,
1672+
\member{tp_traverse} handlers look like:
1673+
1674+
1675+
\begin{verbatim}
1676+
static int
1677+
my_traverse(Noddy *self, visitproc visit, void *arg)
1678+
{
1679+
Py_VISIT(self->foo);
1680+
Py_VISIT(self->bar);
1681+
return 0;
1682+
}
1683+
\end{verbatim}
1684+
1685+
\versionadded{2.4}
1686+
\end{cfuncdesc}
1687+
1688+
16661689
The \member{tp_clear} handler must be of the \ctype{inquiry} type, or
16671690
\NULL{} if the object is immutable.
16681691

Include/objimpl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ PyAPI_FUNC(void) PyObject_GC_Del(void *);
302302
( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
303303

304304

305+
/* Utility macro to help write tp_traverse functions */
306+
#define Py_VISIT(op) \
307+
do { \
308+
if (op) { \
309+
int vret = visit((op), arg); \
310+
if (vret) \
311+
return vret; \
312+
} \
313+
} while (0)
314+
305315
/* This is here for the sake of backwards compatibility. Extensions that
306316
* use the old GC API will still compile but the objects will not be
307317
* tracked by the GC. */

0 commit comments

Comments
 (0)