Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | .. _countingrefs: |
| 5 | |
| 6 | ****************** |
| 7 | Reference Counting |
| 8 | ****************** |
| 9 | |
| 10 | The macros in this section are used for managing reference counts of Python |
| 11 | objects. |
| 12 | |
| 13 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 14 | .. c:function:: void Py_INCREF(PyObject *o) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 16 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
Victor Stinner | 53a03aa | 2020-11-05 15:02:12 +0100 | [diff] [blame] | 19 | See also :c:func:`Py_NewRef`. |
| 20 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 22 | .. c:function:: void Py_XINCREF(PyObject *o) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 24 | Increment the reference count for object *o*. The object may be ``NULL``, in |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | which case the macro has no effect. |
| 26 | |
Victor Stinner | 53a03aa | 2020-11-05 15:02:12 +0100 | [diff] [blame] | 27 | See also :c:func:`Py_XNewRef`. |
| 28 | |
| 29 | |
| 30 | .. c:function:: PyObject* Py_NewRef(PyObject *o) |
| 31 | |
| 32 | Increment the reference count of the object *o* and return the object *o*. |
| 33 | |
| 34 | The object *o* must not be ``NULL``. |
| 35 | |
| 36 | For example:: |
| 37 | |
| 38 | Py_INCREF(obj); |
| 39 | self->attr = obj; |
| 40 | |
| 41 | can be written as:: |
| 42 | |
| 43 | self->attr = Py_NewRef(obj); |
| 44 | |
| 45 | .. versionadded:: 3.10 |
| 46 | |
| 47 | |
| 48 | .. c:function:: PyObject* Py_XNewRef(PyObject *o) |
| 49 | |
| 50 | Similar to :c:func:`Py_NewRef`, but the object *o* can be NULL. |
| 51 | |
| 52 | If the object *o* is ``NULL``, the function just returns ``NULL``. |
| 53 | |
| 54 | .. versionadded:: 3.10 |
| 55 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 57 | .. c:function:: void Py_DECREF(PyObject *o) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 59 | Decrement the reference count for object *o*. The object must not be ``NULL``; if |
| 60 | you aren't sure that it isn't ``NULL``, use :c:func:`Py_XDECREF`. If the reference |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | count reaches zero, the object's type's deallocation function (which must not be |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 62 | ``NULL``) is invoked. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
| 64 | .. warning:: |
| 65 | |
| 66 | The deallocation function can cause arbitrary Python code to be invoked (e.g. |
| 67 | when a class instance with a :meth:`__del__` method is deallocated). While |
| 68 | exceptions in such code are not propagated, the executed code has free access to |
| 69 | all Python global variables. This means that any object that is reachable from |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 70 | a global variable should be in a consistent state before :c:func:`Py_DECREF` is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | invoked. For example, code to delete an object from a list should copy a |
| 72 | reference to the deleted object in a temporary variable, update the list data |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 73 | structure, and then call :c:func:`Py_DECREF` for the temporary variable. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 76 | .. c:function:: void Py_XDECREF(PyObject *o) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 77 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 78 | Decrement the reference count for object *o*. The object may be ``NULL``, in |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | which case the macro has no effect; otherwise the effect is the same as for |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 80 | :c:func:`Py_DECREF`, and the same warning applies. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 83 | .. c:function:: void Py_CLEAR(PyObject *o) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 85 | Decrement the reference count for object *o*. The object may be ``NULL``, in |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | which case the macro has no effect; otherwise the effect is the same as for |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 87 | :c:func:`Py_DECREF`, except that the argument is also set to ``NULL``. The warning |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 88 | for :c:func:`Py_DECREF` does not apply with respect to the object passed because |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 89 | the macro carefully uses a temporary variable and sets the argument to ``NULL`` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | before decrementing its reference count. |
| 91 | |
Beomsoo Kim | 05c1b38 | 2018-12-17 21:57:03 +0900 | [diff] [blame] | 92 | It is a good idea to use this macro whenever decrementing the reference |
| 93 | count of an object that might be traversed during garbage collection. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | |
| 96 | The following functions are for runtime dynamic embedding of Python: |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 97 | ``Py_IncRef(PyObject *o)``, ``Py_DecRef(PyObject *o)``. They are |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 98 | simply exported function versions of :c:func:`Py_XINCREF` and |
| 99 | :c:func:`Py_XDECREF`, respectively. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 100 | |
| 101 | The following functions or macros are only for use within the interpreter core: |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 102 | :c:func:`_Py_Dealloc`, :c:func:`_Py_ForgetReference`, :c:func:`_Py_NewReference`, |
| 103 | as well as the global variable :c:data:`_Py_RefTotal`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |