blob: 49675a80325d11b1b5caf35a68513477a6167685 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. highlightlang:: c
2
3
4.. _countingrefs:
5
6******************
7Reference Counting
8******************
9
10The macros in this section are used for managing reference counts of Python
11objects.
12
13
Sandro Tosi98ed08f2012-01-14 16:42:02 +010014.. c:function:: void Py_INCREF(PyObject *o)
Georg Brandl8ec7f652007-08-15 14:28:01 +000015
16 Increment the reference count for object *o*. The object must not be *NULL*; if
Sandro Tosi98ed08f2012-01-14 16:42:02 +010017 you aren't sure that it isn't *NULL*, use :c:func:`Py_XINCREF`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000018
19
Sandro Tosi98ed08f2012-01-14 16:42:02 +010020.. c:function:: void Py_XINCREF(PyObject *o)
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
22 Increment the reference count for object *o*. The object may be *NULL*, in
23 which case the macro has no effect.
24
25
Sandro Tosi98ed08f2012-01-14 16:42:02 +010026.. c:function:: void Py_DECREF(PyObject *o)
Georg Brandl8ec7f652007-08-15 14:28:01 +000027
28 Decrement the reference count for object *o*. The object must not be *NULL*; if
Sandro Tosi98ed08f2012-01-14 16:42:02 +010029 you aren't sure that it isn't *NULL*, use :c:func:`Py_XDECREF`. If the reference
Georg Brandl8ec7f652007-08-15 14:28:01 +000030 count reaches zero, the object's type's deallocation function (which must not be
31 *NULL*) is invoked.
32
33 .. warning::
34
35 The deallocation function can cause arbitrary Python code to be invoked (e.g.
36 when a class instance with a :meth:`__del__` method is deallocated). While
37 exceptions in such code are not propagated, the executed code has free access to
38 all Python global variables. This means that any object that is reachable from
Sandro Tosi98ed08f2012-01-14 16:42:02 +010039 a global variable should be in a consistent state before :c:func:`Py_DECREF` is
Georg Brandl8ec7f652007-08-15 14:28:01 +000040 invoked. For example, code to delete an object from a list should copy a
41 reference to the deleted object in a temporary variable, update the list data
Sandro Tosi98ed08f2012-01-14 16:42:02 +010042 structure, and then call :c:func:`Py_DECREF` for the temporary variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +000043
44
Sandro Tosi98ed08f2012-01-14 16:42:02 +010045.. c:function:: void Py_XDECREF(PyObject *o)
Georg Brandl8ec7f652007-08-15 14:28:01 +000046
47 Decrement the reference count for object *o*. The object may be *NULL*, in
48 which case the macro has no effect; otherwise the effect is the same as for
Sandro Tosi98ed08f2012-01-14 16:42:02 +010049 :c:func:`Py_DECREF`, and the same warning applies.
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
51
Sandro Tosi98ed08f2012-01-14 16:42:02 +010052.. c:function:: void Py_CLEAR(PyObject *o)
Georg Brandl8ec7f652007-08-15 14:28:01 +000053
54 Decrement the reference count for object *o*. The object may be *NULL*, in
55 which case the macro has no effect; otherwise the effect is the same as for
Sandro Tosi98ed08f2012-01-14 16:42:02 +010056 :c:func:`Py_DECREF`, except that the argument is also set to *NULL*. The warning
57 for :c:func:`Py_DECREF` does not apply with respect to the object passed because
Georg Brandl8ec7f652007-08-15 14:28:01 +000058 the macro carefully uses a temporary variable and sets the argument to *NULL*
59 before decrementing its reference count.
60
61 It is a good idea to use this macro whenever decrementing the value of a
62 variable that might be traversed during garbage collection.
63
64 .. versionadded:: 2.4
65
66The following functions are for runtime dynamic embedding of Python:
Georg Brandl45c088c2007-12-05 19:49:21 +000067``Py_IncRef(PyObject *o)``, ``Py_DecRef(PyObject *o)``. They are
Sandro Tosi98ed08f2012-01-14 16:42:02 +010068simply exported function versions of :c:func:`Py_XINCREF` and
69:c:func:`Py_XDECREF`, respectively.
Georg Brandl8ec7f652007-08-15 14:28:01 +000070
71The following functions or macros are only for use within the interpreter core:
Sandro Tosi98ed08f2012-01-14 16:42:02 +010072:c:func:`_Py_Dealloc`, :c:func:`_Py_ForgetReference`, :c:func:`_Py_NewReference`,
73as well as the global variable :c:data:`_Py_RefTotal`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000074