]> granicus.if.org Git - python/commitdiff
Implemented a new Py_CLEAR macro. This macro should be used when
authorJim Fulton <jim@zope.com>
Wed, 14 Jul 2004 19:07:35 +0000 (19:07 +0000)
committerJim Fulton <jim@zope.com>
Wed, 14 Jul 2004 19:07:35 +0000 (19:07 +0000)
decrementing the refcount of variables that might be accessed as a
result of calling Python

Doc/api/refcounting.tex
Include/object.h

index 42b9e6a156a30f16b16c3b619937229825f3bc8e..add5d656b3dede0ffa7725acd322f7725c5c02a1 100644 (file)
@@ -42,6 +42,22 @@ of Python objects.
   applies.
 \end{cfuncdesc}
 
+\begin{cfuncdesc}{void}{Py_CLEAR}{PyObject *o}
+  Decrement the reference count for object \var{o}.  The object may be
+  \NULL, in which case the macro has no effect; otherwise the effect
+  is the same as for \cfunction{Py_DECREF()}, except that the argument
+  is also set to \NULL.  The warning for \cfunction{Py_DECREF()}, does
+  not apply with respect to the object passed because the macro
+  carefully uses a temporary variable and sets the argument to \NULL
+  before decrementing it's reference count.
+
+  It is a good idea to use this macro whenever decrementing the value
+  of a variable that might be traversed during garbage collection.
+
+\versionadded{2.4}
+\end{cfuncdesc}
+
+
 The following functions are for runtime dynamic embedding of Python:
 \cfunction{Py_IncRef(PyObject *o)}, \cfunction{Py_DecRef(PyObject *o)}.
 They are simply exported function versions of \cfunction{Py_XINCREF()} and 
index 555d810cbf872d969bee45796b8717edec75db5d..60cf1464fa04823c9f656be9fd79f0cedd90f013 100644 (file)
@@ -620,6 +620,15 @@ PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force);
        else                                            \
                _Py_Dealloc((PyObject *)(op))
 
+#define Py_CLEAR(op)                           \
+        do {                                   \
+                if (op) {                      \
+                        PyObject *tmp = (op);  \
+                        (op) = NULL;           \
+                        Py_DECREF(tmp);                \
+                }                              \
+        } while (0)
+
 /* Macros to use in case the object pointer may be NULL: */
 #define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op)
 #define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op)