Stéphane Wirtel | cbb6484 | 2019-05-17 11:55:34 +0200 | [diff] [blame] | 1 | .. highlight:: c |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 2 | |
| 3 | .. _supporting-cycle-detection: |
| 4 | |
| 5 | Supporting Cyclic Garbage Collection |
| 6 | ==================================== |
| 7 | |
| 8 | Python's support for detecting and collecting garbage which involves circular |
| 9 | references requires support from object types which are "containers" for other |
| 10 | objects which may also be containers. Types which do not store references to |
| 11 | other objects, or which only store references to atomic types (such as numbers |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 12 | or strings), do not need to provide any explicit support for garbage |
| 13 | collection. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 14 | |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 15 | To create a container type, the :c:member:`~PyTypeObject.tp_flags` field of the type object must |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 16 | include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 17 | :c:member:`~PyTypeObject.tp_traverse` handler. If instances of the type are mutable, a |
| 18 | :c:member:`~PyTypeObject.tp_clear` implementation must also be provided. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | .. data:: Py_TPFLAGS_HAVE_GC |
Georg Brandl | c31b0f7 | 2008-01-20 11:00:14 +0000 | [diff] [blame] | 22 | :noindex: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 23 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 24 | Objects with a type with this flag set must conform with the rules |
| 25 | documented here. For convenience these objects will be referred to as |
| 26 | container objects. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 | |
| 28 | Constructors for container types must conform to two rules: |
| 29 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 30 | #. The memory for the object must be allocated using :c:func:`PyObject_GC_New` |
| 31 | or :c:func:`PyObject_GC_NewVar`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 32 | |
| 33 | #. Once all the fields which may contain references to other containers are |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 34 | initialized, it must call :c:func:`PyObject_GC_Track`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 35 | |
Miss Islington (bot) | 11049be | 2021-05-28 20:21:26 -0700 | [diff] [blame] | 36 | .. warning:: |
| 37 | If a type adds the Py_TPFLAGS_HAVE_GC, then it *must* implement at least |
| 38 | a :c:member:`~PyTypeObject.tp_traverse` handler or explicitly use one |
| 39 | from its subclass or subclasses. |
| 40 | |
Miss Islington (bot) | d8ce746 | 2021-05-29 10:49:16 -0700 | [diff] [blame] | 41 | When calling :c:func:`PyType_Ready` or some of the APIs that indirectly |
| 42 | call it like :c:func:`PyType_FromSpecWithBases` or |
| 43 | :c:func:`PyType_FromSpec` the interpreter will automatically populate the |
Miss Islington (bot) | 11049be | 2021-05-28 20:21:26 -0700 | [diff] [blame] | 44 | :c:member:`~PyTypeObject.tp_flags`, :c:member:`~PyTypeObject.tp_traverse` |
| 45 | and :c:member:`~PyTypeObject.tp_clear` fields if the type inherits from a |
| 46 | class that implements the garbage collector protocol and the child class |
| 47 | does *not* include the :const:`Py_TPFLAGS_HAVE_GC` flag. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 48 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 49 | .. c:function:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 50 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 51 | Analogous to :c:func:`PyObject_New` but for container objects with the |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 52 | :const:`Py_TPFLAGS_HAVE_GC` flag set. |
| 53 | |
| 54 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 55 | .. c:function:: TYPE* PyObject_GC_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 56 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 57 | Analogous to :c:func:`PyObject_NewVar` but for container objects with the |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 58 | :const:`Py_TPFLAGS_HAVE_GC` flag set. |
| 59 | |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 60 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 61 | .. c:function:: TYPE* PyObject_GC_Resize(TYPE, PyVarObject *op, Py_ssize_t newsize) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 62 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 63 | Resize an object allocated by :c:func:`PyObject_NewVar`. Returns the |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 64 | resized object or ``NULL`` on failure. *op* must not be tracked by the collector yet. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 65 | |
| 66 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 67 | .. c:function:: void PyObject_GC_Track(PyObject *op) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 68 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 69 | Adds the object *op* to the set of container objects tracked by the |
| 70 | collector. The collector can run at unexpected times so objects must be |
| 71 | valid while being tracked. This should be called once all the fields |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 72 | followed by the :c:member:`~PyTypeObject.tp_traverse` handler become valid, usually near the |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 73 | end of the constructor. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 74 | |
Hai Shi | 675d9a3 | 2020-04-15 02:11:20 +0800 | [diff] [blame] | 75 | |
| 76 | .. c:function:: int PyObject_IS_GC(PyObject *obj) |
| 77 | |
| 78 | Returns non-zero if the object implements the garbage collector protocol, |
| 79 | otherwise returns 0. |
| 80 | |
| 81 | The object cannot be tracked by the garbage collector if this function returns 0. |
| 82 | |
| 83 | |
Pablo Galindo | f13072b | 2020-04-11 01:21:54 +0100 | [diff] [blame] | 84 | .. c:function:: int PyObject_GC_IsTracked(PyObject *op) |
| 85 | |
| 86 | Returns 1 if the object type of *op* implements the GC protocol and *op* is being |
| 87 | currently tracked by the garbage collector and 0 otherwise. |
| 88 | |
| 89 | This is analogous to the Python function :func:`gc.is_tracked`. |
| 90 | |
| 91 | .. versionadded:: 3.9 |
| 92 | |
| 93 | |
| 94 | .. c:function:: int PyObject_GC_IsFinalized(PyObject *op) |
| 95 | |
| 96 | Returns 1 if the object type of *op* implements the GC protocol and *op* has been |
| 97 | already finalized by the garbage collector and 0 otherwise. |
| 98 | |
| 99 | This is analogous to the Python function :func:`gc.is_finalized`. |
| 100 | |
| 101 | .. versionadded:: 3.9 |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 102 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 103 | Similarly, the deallocator for the object must conform to a similar pair of |
| 104 | rules: |
| 105 | |
| 106 | #. Before fields which refer to other containers are invalidated, |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 107 | :c:func:`PyObject_GC_UnTrack` must be called. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 108 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 109 | #. The object's memory must be deallocated using :c:func:`PyObject_GC_Del`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 110 | |
| 111 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 112 | .. c:function:: void PyObject_GC_Del(void *op) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 113 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 114 | Releases memory allocated to an object using :c:func:`PyObject_GC_New` or |
| 115 | :c:func:`PyObject_GC_NewVar`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 116 | |
| 117 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 118 | .. c:function:: void PyObject_GC_UnTrack(void *op) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 119 | |
| 120 | Remove the object *op* from the set of container objects tracked by the |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 121 | collector. Note that :c:func:`PyObject_GC_Track` can be called again on |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 122 | this object to add it back to the set of tracked objects. The deallocator |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 123 | (:c:member:`~PyTypeObject.tp_dealloc` handler) should call this for the object before any of |
| 124 | the fields used by the :c:member:`~PyTypeObject.tp_traverse` handler become invalid. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 125 | |
| 126 | |
Victor Stinner | 1a6be91 | 2018-11-13 12:52:18 +0100 | [diff] [blame] | 127 | .. versionchanged:: 3.8 |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 128 | |
Victor Stinner | 1a6be91 | 2018-11-13 12:52:18 +0100 | [diff] [blame] | 129 | The :c:func:`_PyObject_GC_TRACK` and :c:func:`_PyObject_GC_UNTRACK` macros |
| 130 | have been removed from the public C API. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 132 | The :c:member:`~PyTypeObject.tp_traverse` handler accepts a function parameter of this type: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 133 | |
| 134 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 135 | .. c:type:: int (*visitproc)(PyObject *object, void *arg) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 137 | Type of the visitor function passed to the :c:member:`~PyTypeObject.tp_traverse` handler. |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 138 | The function should be called with an object to traverse as *object* and |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 139 | the third parameter to the :c:member:`~PyTypeObject.tp_traverse` handler as *arg*. The |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 140 | Python core uses several visitor functions to implement cyclic garbage |
| 141 | detection; it's not expected that users will need to write their own |
| 142 | visitor functions. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 143 | |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 144 | The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 145 | |
| 146 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 147 | .. c:type:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 148 | |
| 149 | Traversal function for a container object. Implementations must call the |
| 150 | *visit* function for each object directly contained by *self*, with the |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 151 | parameters to *visit* being the contained object and the *arg* value passed |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 152 | to the handler. The *visit* function must not be called with a ``NULL`` |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 153 | object argument. If *visit* returns a non-zero value that value should be |
| 154 | returned immediately. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 155 | |
Antoine Pitrou | 39668f5 | 2013-08-01 21:12:45 +0200 | [diff] [blame] | 156 | To simplify writing :c:member:`~PyTypeObject.tp_traverse` handlers, a :c:func:`Py_VISIT` macro is |
| 157 | provided. In order to use this macro, the :c:member:`~PyTypeObject.tp_traverse` implementation |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 158 | must name its arguments exactly *visit* and *arg*: |
| 159 | |
| 160 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 161 | .. c:function:: void Py_VISIT(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 162 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 163 | If *o* is not ``NULL``, call the *visit* callback, with arguments *o* |
Benjamin Peterson | a49ccf2 | 2016-06-02 11:35:59 -0700 | [diff] [blame] | 164 | and *arg*. If *visit* returns a non-zero value, then return it. |
| 165 | Using this macro, :c:member:`~PyTypeObject.tp_traverse` handlers |
| 166 | look like:: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 167 | |
| 168 | static int |
| 169 | my_traverse(Noddy *self, visitproc visit, void *arg) |
| 170 | { |
| 171 | Py_VISIT(self->foo); |
| 172 | Py_VISIT(self->bar); |
| 173 | return 0; |
| 174 | } |
| 175 | |
Serhiy Storchaka | 25fc088 | 2019-10-30 12:03:20 +0200 | [diff] [blame] | 176 | The :c:member:`~PyTypeObject.tp_clear` handler must be of the :c:type:`inquiry` type, or ``NULL`` |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 177 | if the object is immutable. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 178 | |
| 179 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 180 | .. c:type:: int (*inquiry)(PyObject *self) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 181 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 182 | Drop references that may have created reference cycles. Immutable objects |
| 183 | do not have to define this method since they can never directly create |
| 184 | reference cycles. Note that the object must still be valid after calling |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 185 | this method (don't just call :c:func:`Py_DECREF` on a reference). The |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 186 | collector will call this method if it detects that this object is involved |
| 187 | in a reference cycle. |
scoder | 3cc481b | 2021-04-28 18:12:16 +0200 | [diff] [blame] | 188 | |
| 189 | |
| 190 | Controlling the Garbage Collector State |
| 191 | --------------------------------------- |
| 192 | |
| 193 | The C-API provides the following functions for controlling |
| 194 | garbage collection runs. |
| 195 | |
| 196 | .. c:function:: Py_ssize_t PyGC_Collect(void) |
| 197 | |
| 198 | Perform a full garbage collection, if the garbage collector is enabled. |
| 199 | (Note that :func:`gc.collect` runs it unconditionally.) |
| 200 | |
| 201 | Returns the number of collected + unreachable objects which cannot |
| 202 | be collected. |
| 203 | If the garbage collector is disabled or already collecting, |
| 204 | returns ``0`` immediately. |
| 205 | Errors during garbage collection are passed to :data:`sys.unraisablehook`. |
| 206 | This function does not raise exceptions. |
| 207 | |
| 208 | |
| 209 | .. c:function:: int PyGC_Enable(void) |
| 210 | |
| 211 | Enable the garbage collector: similar to :func:`gc.enable`. |
| 212 | Returns the previous state, 0 for disabled and 1 for enabled. |
| 213 | |
| 214 | .. versionadded:: 3.10 |
| 215 | |
| 216 | |
| 217 | .. c:function:: int PyGC_Disable(void) |
| 218 | |
| 219 | Disable the garbage collector: similar to :func:`gc.disable`. |
| 220 | Returns the previous state, 0 for disabled and 1 for enabled. |
| 221 | |
| 222 | .. versionadded:: 3.10 |
| 223 | |
| 224 | |
| 225 | .. c:function:: int PyGC_IsEnabled(void) |
| 226 | |
| 227 | Query the state of the garbage collector: similar to :func:`gc.isenabled`. |
| 228 | Returns the current state, 0 for disabled and 1 for enabled. |
| 229 | |
| 230 | .. versionadded:: 3.10 |