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 | |
Victor Stinner | 23c5f93 | 2020-11-09 13:40:47 +0100 | [diff] [blame] | 16 | Increment the reference count for object *o*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
Victor Stinner | 23c5f93 | 2020-11-09 13:40:47 +0100 | [diff] [blame] | 18 | This function is usually used to convert a :term:`borrowed reference` to a |
| 19 | :term:`strong reference` in-place. The :c:func:`Py_NewRef` function can be |
| 20 | used to create a new :term:`strong reference`. |
| 21 | |
| 22 | The object must not be ``NULL``; if you aren't sure that it isn't |
| 23 | ``NULL``, use :c:func:`Py_XINCREF`. |
Victor Stinner | 53a03aa | 2020-11-05 15:02:12 +0100 | [diff] [blame] | 24 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 26 | .. c:function:: void Py_XINCREF(PyObject *o) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 28 | 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] | 29 | which case the macro has no effect. |
| 30 | |
Victor Stinner | 53a03aa | 2020-11-05 15:02:12 +0100 | [diff] [blame] | 31 | See also :c:func:`Py_XNewRef`. |
| 32 | |
| 33 | |
| 34 | .. c:function:: PyObject* Py_NewRef(PyObject *o) |
| 35 | |
Victor Stinner | 23c5f93 | 2020-11-09 13:40:47 +0100 | [diff] [blame] | 36 | Create a new :term:`strong reference` to an object: increment the reference |
| 37 | count of the object *o* and return the object *o*. |
Victor Stinner | 53a03aa | 2020-11-05 15:02:12 +0100 | [diff] [blame] | 38 | |
Victor Stinner | 23c5f93 | 2020-11-09 13:40:47 +0100 | [diff] [blame] | 39 | When the :term:`strong reference` is no longer needed, :c:func:`Py_DECREF` |
| 40 | should be called on it to decrement the object reference count. |
| 41 | |
| 42 | The object *o* must not be ``NULL``; use :c:func:`Py_XNewRef` if *o* can be |
| 43 | ``NULL``. |
Victor Stinner | 53a03aa | 2020-11-05 15:02:12 +0100 | [diff] [blame] | 44 | |
| 45 | For example:: |
| 46 | |
| 47 | Py_INCREF(obj); |
| 48 | self->attr = obj; |
| 49 | |
| 50 | can be written as:: |
| 51 | |
| 52 | self->attr = Py_NewRef(obj); |
| 53 | |
Victor Stinner | 23c5f93 | 2020-11-09 13:40:47 +0100 | [diff] [blame] | 54 | See also :c:func:`Py_INCREF`. |
| 55 | |
Victor Stinner | 53a03aa | 2020-11-05 15:02:12 +0100 | [diff] [blame] | 56 | .. versionadded:: 3.10 |
| 57 | |
| 58 | |
| 59 | .. c:function:: PyObject* Py_XNewRef(PyObject *o) |
| 60 | |
| 61 | Similar to :c:func:`Py_NewRef`, but the object *o* can be NULL. |
| 62 | |
| 63 | If the object *o* is ``NULL``, the function just returns ``NULL``. |
| 64 | |
| 65 | .. versionadded:: 3.10 |
| 66 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 68 | .. c:function:: void Py_DECREF(PyObject *o) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 69 | |
Victor Stinner | 23c5f93 | 2020-11-09 13:40:47 +0100 | [diff] [blame] | 70 | Decrement the reference count for object *o*. |
| 71 | |
| 72 | If the reference count reaches zero, the object's type's deallocation |
| 73 | function (which must not be ``NULL``) is invoked. |
| 74 | |
| 75 | This function is usually used to delete a :term:`strong reference` before |
| 76 | exiting its scope. |
| 77 | |
| 78 | The object must not be ``NULL``; if you aren't sure that it isn't ``NULL``, |
| 79 | use :c:func:`Py_XDECREF`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
| 81 | .. warning:: |
| 82 | |
| 83 | The deallocation function can cause arbitrary Python code to be invoked (e.g. |
| 84 | when a class instance with a :meth:`__del__` method is deallocated). While |
| 85 | exceptions in such code are not propagated, the executed code has free access to |
| 86 | 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] | 87 | 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] | 88 | invoked. For example, code to delete an object from a list should copy a |
| 89 | 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] | 90 | structure, and then call :c:func:`Py_DECREF` for the temporary variable. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
| 92 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 93 | .. c:function:: void Py_XDECREF(PyObject *o) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 95 | 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] | 96 | 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] | 97 | :c:func:`Py_DECREF`, and the same warning applies. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 98 | |
| 99 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 100 | .. c:function:: void Py_CLEAR(PyObject *o) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 101 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 102 | 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] | 103 | 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] | 104 | :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] | 105 | 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] | 106 | 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] | 107 | before decrementing its reference count. |
| 108 | |
Beomsoo Kim | 05c1b38 | 2018-12-17 21:57:03 +0900 | [diff] [blame] | 109 | It is a good idea to use this macro whenever decrementing the reference |
| 110 | count of an object that might be traversed during garbage collection. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 111 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
| 113 | The following functions are for runtime dynamic embedding of Python: |
Christian Heimes | b9eccbf | 2007-12-05 20:18:38 +0000 | [diff] [blame] | 114 | ``Py_IncRef(PyObject *o)``, ``Py_DecRef(PyObject *o)``. They are |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 115 | simply exported function versions of :c:func:`Py_XINCREF` and |
| 116 | :c:func:`Py_XDECREF`, respectively. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | |
| 118 | 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] | 119 | :c:func:`_Py_Dealloc`, :c:func:`_Py_ForgetReference`, :c:func:`_Py_NewReference`, |
| 120 | as well as the global variable :c:data:`_Py_RefTotal`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 121 | |