]> granicus.if.org Git - python/commitdiff
Documented the new Py_VISIT macro to simplify implementation of
authorJim Fulton <jim@zope.com>
Wed, 14 Jul 2004 19:08:17 +0000 (19:08 +0000)
committerJim Fulton <jim@zope.com>
Wed, 14 Jul 2004 19:08:17 +0000 (19:08 +0000)
tp_traverse handlers. (Tim made me do it. ;)

Doc/api/newtypes.tex
Include/objimpl.h

index 036664e5ae21af0c32180ddef7737765ccc17f21..2b58cd90cdfaea8dbb88f953de0fa5c4094b1035 100644 (file)
@@ -1663,6 +1663,29 @@ The \member{tp_traverse} handler must have the following type:
   that value should be returned immediately.
 \end{ctypedesc}
 
+To simplify writing \member{tp_traverse} handlers, a
+\cfunction{Py_VISIT()} is provided:
+
+\begin{cfuncdesc}{void}{Py_VISIT}{PyObject *o}
+  Call the \var{visit} for \var{o} with \var{arg}. If \var{visit}
+  returns a non-zero value, then return it.  Using this macro,
+  \member{tp_traverse} handlers look like:
+
+
+\begin{verbatim}
+static int
+my_traverse(Noddy *self, visitproc visit, void *arg)
+{
+    Py_VISIT(self->foo);
+    Py_VISIT(self->bar);
+    return 0;
+}
+\end{verbatim}
+
+\versionadded{2.4}
+\end{cfuncdesc}
+
+
 The \member{tp_clear} handler must be of the \ctype{inquiry} type, or
 \NULL{} if the object is immutable.
 
index 3679cbad1bf11286f65233081895fcc1ddb4bfc6..649d1bc8b34d5135c58d649b169d34aac36783ba 100644 (file)
@@ -302,6 +302,16 @@ PyAPI_FUNC(void) PyObject_GC_Del(void *);
                ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
 
 
+/* Utility macro to help write tp_traverse functions */
+#define Py_VISIT(op)                                   \
+        do {                                           \
+                if (op) {                              \
+                        int vret = visit((op), arg);   \
+                        if (vret)                      \
+                                return vret;           \
+                }                                      \
+        } while (0)
+
 /* This is here for the sake of backwards compatibility.  Extensions that
  * use the old GC API will still compile but the objects will not be
  * tracked by the GC. */