blob: 077543b852172039359f132e2aac26dd7d405b4c [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Reference Counting \label{countingRefs}}
2
3
4The macros in this section are used for managing reference counts
5of Python objects.
6
7
8\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
9 Increment the reference count for object \var{o}. The object must
10 not be \NULL; if you aren't sure that it isn't \NULL, use
11 \cfunction{Py_XINCREF()}.
12\end{cfuncdesc}
13
14\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
15 Increment the reference count for object \var{o}. The object may be
16 \NULL, in which case the macro has no effect.
17\end{cfuncdesc}
18
19\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
20 Decrement the reference count for object \var{o}. The object must
21 not be \NULL; if you aren't sure that it isn't \NULL, use
22 \cfunction{Py_XDECREF()}. If the reference count reaches zero, the
23 object's type's deallocation function (which must not be \NULL) is
24 invoked.
25
26 \warning{The deallocation function can cause arbitrary Python code
27 to be invoked (e.g. when a class instance with a \method{__del__()}
28 method is deallocated). While exceptions in such code are not
29 propagated, the executed code has free access to all Python global
30 variables. This means that any object that is reachable from a
31 global variable should be in a consistent state before
32 \cfunction{Py_DECREF()} is invoked. For example, code to delete an
33 object from a list should copy a reference to the deleted object in
34 a temporary variable, update the list data structure, and then call
35 \cfunction{Py_DECREF()} for the temporary variable.}
36\end{cfuncdesc}
37
38\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
39 Decrement the reference count for object \var{o}. The object may be
40 \NULL, in which case the macro has no effect; otherwise the effect
41 is the same as for \cfunction{Py_DECREF()}, and the same warning
42 applies.
43\end{cfuncdesc}
44
Jim Fulton8c5aeaa2004-07-14 19:07:35 +000045\begin{cfuncdesc}{void}{Py_CLEAR}{PyObject *o}
46 Decrement the reference count for object \var{o}. The object may be
47 \NULL, in which case the macro has no effect; otherwise the effect
48 is the same as for \cfunction{Py_DECREF()}, except that the argument
Andrew M. Kuchlinge03664f2004-07-26 19:25:54 +000049 is also set to \NULL. The warning for \cfunction{Py_DECREF()} does
Jim Fulton8c5aeaa2004-07-14 19:07:35 +000050 not apply with respect to the object passed because the macro
51 carefully uses a temporary variable and sets the argument to \NULL
Andrew M. Kuchlinge03664f2004-07-26 19:25:54 +000052 before decrementing its reference count.
Jim Fulton8c5aeaa2004-07-14 19:07:35 +000053
54 It is a good idea to use this macro whenever decrementing the value
55 of a variable that might be traversed during garbage collection.
56
57\versionadded{2.4}
58\end{cfuncdesc}
59
60
Thomas Heller1328b522004-04-22 17:23:49 +000061The following functions are for runtime dynamic embedding of Python:
62\cfunction{Py_IncRef(PyObject *o)}, \cfunction{Py_DecRef(PyObject *o)}.
63They are simply exported function versions of \cfunction{Py_XINCREF()} and
64\cfunction{Py_XDECREF()}, respectively.
65
Fred Drake3adf79e2001-10-12 19:01:43 +000066The following functions or macros are only for use within the
67interpreter core: \cfunction{_Py_Dealloc()},
68\cfunction{_Py_ForgetReference()}, \cfunction{_Py_NewReference()}, as
69well as the global variable \cdata{_Py_RefTotal}.