blob: 0df12c67f40bc3f8b3749bc6556753b2388af29d [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl116aa622007-08-15 14:28:22 +00002
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
Georg Brandl60203b42010-10-06 10:11:56 +000014.. c:function:: void Py_INCREF(PyObject *o)
Georg Brandl116aa622007-08-15 14:28:22 +000015
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020016 Increment the reference count for object *o*. The object must not be ``NULL``; if
17 you aren't sure that it isn't ``NULL``, use :c:func:`Py_XINCREF`.
Georg Brandl116aa622007-08-15 14:28:22 +000018
19
Georg Brandl60203b42010-10-06 10:11:56 +000020.. c:function:: void Py_XINCREF(PyObject *o)
Georg Brandl116aa622007-08-15 14:28:22 +000021
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020022 Increment the reference count for object *o*. The object may be ``NULL``, in
Georg Brandl116aa622007-08-15 14:28:22 +000023 which case the macro has no effect.
24
25
Georg Brandl60203b42010-10-06 10:11:56 +000026.. c:function:: void Py_DECREF(PyObject *o)
Georg Brandl116aa622007-08-15 14:28:22 +000027
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020028 Decrement the reference count for object *o*. The object must not be ``NULL``; if
29 you aren't sure that it isn't ``NULL``, use :c:func:`Py_XDECREF`. If the reference
Georg Brandl116aa622007-08-15 14:28:22 +000030 count reaches zero, the object's type's deallocation function (which must not be
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020031 ``NULL``) is invoked.
Georg Brandl116aa622007-08-15 14:28:22 +000032
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
Georg Brandl60203b42010-10-06 10:11:56 +000039 a global variable should be in a consistent state before :c:func:`Py_DECREF` is
Georg Brandl116aa622007-08-15 14:28:22 +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
Georg Brandl60203b42010-10-06 10:11:56 +000042 structure, and then call :c:func:`Py_DECREF` for the temporary variable.
Georg Brandl116aa622007-08-15 14:28:22 +000043
44
Georg Brandl60203b42010-10-06 10:11:56 +000045.. c:function:: void Py_XDECREF(PyObject *o)
Georg Brandl116aa622007-08-15 14:28:22 +000046
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020047 Decrement the reference count for object *o*. The object may be ``NULL``, in
Georg Brandl116aa622007-08-15 14:28:22 +000048 which case the macro has no effect; otherwise the effect is the same as for
Georg Brandl60203b42010-10-06 10:11:56 +000049 :c:func:`Py_DECREF`, and the same warning applies.
Georg Brandl116aa622007-08-15 14:28:22 +000050
51
Georg Brandl60203b42010-10-06 10:11:56 +000052.. c:function:: void Py_CLEAR(PyObject *o)
Georg Brandl116aa622007-08-15 14:28:22 +000053
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020054 Decrement the reference count for object *o*. The object may be ``NULL``, in
Georg Brandl116aa622007-08-15 14:28:22 +000055 which case the macro has no effect; otherwise the effect is the same as for
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020056 :c:func:`Py_DECREF`, except that the argument is also set to ``NULL``. The warning
Georg Brandl60203b42010-10-06 10:11:56 +000057 for :c:func:`Py_DECREF` does not apply with respect to the object passed because
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020058 the macro carefully uses a temporary variable and sets the argument to ``NULL``
Georg Brandl116aa622007-08-15 14:28:22 +000059 before decrementing its reference count.
60
Beomsoo Kim05c1b382018-12-17 21:57:03 +090061 It is a good idea to use this macro whenever decrementing the reference
62 count of an object that might be traversed during garbage collection.
Georg Brandl116aa622007-08-15 14:28:22 +000063
Georg Brandl116aa622007-08-15 14:28:22 +000064
65The following functions are for runtime dynamic embedding of Python:
Christian Heimesb9eccbf2007-12-05 20:18:38 +000066``Py_IncRef(PyObject *o)``, ``Py_DecRef(PyObject *o)``. They are
Georg Brandl60203b42010-10-06 10:11:56 +000067simply exported function versions of :c:func:`Py_XINCREF` and
68:c:func:`Py_XDECREF`, respectively.
Georg Brandl116aa622007-08-15 14:28:22 +000069
70The following functions or macros are only for use within the interpreter core:
Georg Brandl60203b42010-10-06 10:11:56 +000071:c:func:`_Py_Dealloc`, :c:func:`_Py_ForgetReference`, :c:func:`_Py_NewReference`,
72as well as the global variable :c:data:`_Py_RefTotal`.
Georg Brandl116aa622007-08-15 14:28:22 +000073