| 1 | \chapter{Reference Counting \label{countingRefs}}
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 | The macros in this section are used for managing reference counts
|
|---|
| 5 | of 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 |
|
|---|
| 45 | \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
|
|---|
| 49 | is also set to \NULL. The warning for \cfunction{Py_DECREF()} does
|
|---|
| 50 | not apply with respect to the object passed because the macro
|
|---|
| 51 | carefully uses a temporary variable and sets the argument to \NULL
|
|---|
| 52 | before decrementing its reference count.
|
|---|
| 53 |
|
|---|
| 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 |
|
|---|
| 61 | The following functions are for runtime dynamic embedding of Python:
|
|---|
| 62 | \cfunction{Py_IncRef(PyObject *o)}, \cfunction{Py_DecRef(PyObject *o)}.
|
|---|
| 63 | They are simply exported function versions of \cfunction{Py_XINCREF()} and
|
|---|
| 64 | \cfunction{Py_XDECREF()}, respectively.
|
|---|
| 65 |
|
|---|
| 66 | The following functions or macros are only for use within the
|
|---|
| 67 | interpreter core: \cfunction{_Py_Dealloc()},
|
|---|
| 68 | \cfunction{_Py_ForgetReference()}, \cfunction{_Py_NewReference()}, as
|
|---|
| 69 | well as the global variable \cdata{_Py_RefTotal}.
|
|---|