Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 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 | |
Thomas Heller | 1328b52 | 2004-04-22 17:23:49 +0000 | [diff] [blame] | 45 | The following functions are for runtime dynamic embedding of Python: |
| 46 | \cfunction{Py_IncRef(PyObject *o)}, \cfunction{Py_DecRef(PyObject *o)}. |
| 47 | They are simply exported function versions of \cfunction{Py_XINCREF()} and |
| 48 | \cfunction{Py_XDECREF()}, respectively. |
| 49 | |
Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 50 | The following functions or macros are only for use within the |
| 51 | interpreter core: \cfunction{_Py_Dealloc()}, |
| 52 | \cfunction{_Py_ForgetReference()}, \cfunction{_Py_NewReference()}, as |
| 53 | well as the global variable \cdata{_Py_RefTotal}. |