blob: 338365b6657efe8ae3d64019fa9554bfccaa0c76 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
3.. _supporting-cycle-detection:
4
5Supporting Cyclic Garbage Collection
6====================================
7
8Python's support for detecting and collecting garbage which involves circular
9references requires support from object types which are "containers" for other
10objects which may also be containers. Types which do not store references to
11other objects, or which only store references to atomic types (such as numbers
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000012or strings), do not need to provide any explicit support for garbage
13collection.
Georg Brandl54a3faa2008-01-20 09:30:57 +000014
Antoine Pitrou39668f52013-08-01 21:12:45 +020015To create a container type, the :c:member:`~PyTypeObject.tp_flags` field of the type object must
Georg Brandl54a3faa2008-01-20 09:30:57 +000016include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the
Antoine Pitrou39668f52013-08-01 21:12:45 +020017: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 Brandl54a3faa2008-01-20 09:30:57 +000019
20
21.. data:: Py_TPFLAGS_HAVE_GC
Georg Brandlc31b0f72008-01-20 11:00:14 +000022 :noindex:
Georg Brandl54a3faa2008-01-20 09:30:57 +000023
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000024 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 Brandl54a3faa2008-01-20 09:30:57 +000027
28Constructors for container types must conform to two rules:
29
Georg Brandl60203b42010-10-06 10:11:56 +000030#. The memory for the object must be allocated using :c:func:`PyObject_GC_New`
31 or :c:func:`PyObject_GC_NewVar`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
33#. Once all the fields which may contain references to other containers are
Georg Brandl60203b42010-10-06 10:11:56 +000034 initialized, it must call :c:func:`PyObject_GC_Track`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000035
Miss Islington (bot)11049be2021-05-28 20:21:26 -070036 .. 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
41 Some APIs like :c:func:`PyType_FromSpecWithBases` or
42 :c:func:`PyType_FromSpec` will automatically populate the
43 :c:member:`~PyTypeObject.tp_flags`, :c:member:`~PyTypeObject.tp_traverse`
44 and :c:member:`~PyTypeObject.tp_clear` fields if the type inherits from a
45 class that implements the garbage collector protocol and the child class
46 does *not* include the :const:`Py_TPFLAGS_HAVE_GC` flag.
Georg Brandl54a3faa2008-01-20 09:30:57 +000047
Georg Brandl60203b42010-10-06 10:11:56 +000048.. c:function:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type)
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
Georg Brandl60203b42010-10-06 10:11:56 +000050 Analogous to :c:func:`PyObject_New` but for container objects with the
Georg Brandl54a3faa2008-01-20 09:30:57 +000051 :const:`Py_TPFLAGS_HAVE_GC` flag set.
52
53
Georg Brandl60203b42010-10-06 10:11:56 +000054.. c:function:: TYPE* PyObject_GC_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +000055
Georg Brandl60203b42010-10-06 10:11:56 +000056 Analogous to :c:func:`PyObject_NewVar` but for container objects with the
Georg Brandl54a3faa2008-01-20 09:30:57 +000057 :const:`Py_TPFLAGS_HAVE_GC` flag set.
58
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000059
Georg Brandl60203b42010-10-06 10:11:56 +000060.. c:function:: TYPE* PyObject_GC_Resize(TYPE, PyVarObject *op, Py_ssize_t newsize)
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
Georg Brandl60203b42010-10-06 10:11:56 +000062 Resize an object allocated by :c:func:`PyObject_NewVar`. Returns the
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020063 resized object or ``NULL`` on failure. *op* must not be tracked by the collector yet.
Georg Brandl54a3faa2008-01-20 09:30:57 +000064
65
Georg Brandl60203b42010-10-06 10:11:56 +000066.. c:function:: void PyObject_GC_Track(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000067
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000068 Adds the object *op* to the set of container objects tracked by the
69 collector. The collector can run at unexpected times so objects must be
70 valid while being tracked. This should be called once all the fields
Antoine Pitrou39668f52013-08-01 21:12:45 +020071 followed by the :c:member:`~PyTypeObject.tp_traverse` handler become valid, usually near the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000072 end of the constructor.
Georg Brandl54a3faa2008-01-20 09:30:57 +000073
Hai Shi675d9a32020-04-15 02:11:20 +080074
75.. c:function:: int PyObject_IS_GC(PyObject *obj)
76
77 Returns non-zero if the object implements the garbage collector protocol,
78 otherwise returns 0.
79
80 The object cannot be tracked by the garbage collector if this function returns 0.
81
82
Pablo Galindof13072b2020-04-11 01:21:54 +010083.. c:function:: int PyObject_GC_IsTracked(PyObject *op)
84
85 Returns 1 if the object type of *op* implements the GC protocol and *op* is being
86 currently tracked by the garbage collector and 0 otherwise.
87
88 This is analogous to the Python function :func:`gc.is_tracked`.
89
90 .. versionadded:: 3.9
91
92
93.. c:function:: int PyObject_GC_IsFinalized(PyObject *op)
94
95 Returns 1 if the object type of *op* implements the GC protocol and *op* has been
96 already finalized by the garbage collector and 0 otherwise.
97
98 This is analogous to the Python function :func:`gc.is_finalized`.
99
100 .. versionadded:: 3.9
Georg Brandl54a3faa2008-01-20 09:30:57 +0000101
Georg Brandl54a3faa2008-01-20 09:30:57 +0000102Similarly, the deallocator for the object must conform to a similar pair of
103rules:
104
105#. Before fields which refer to other containers are invalidated,
Georg Brandl60203b42010-10-06 10:11:56 +0000106 :c:func:`PyObject_GC_UnTrack` must be called.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000107
Georg Brandl60203b42010-10-06 10:11:56 +0000108#. The object's memory must be deallocated using :c:func:`PyObject_GC_Del`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000109
110
Georg Brandl60203b42010-10-06 10:11:56 +0000111.. c:function:: void PyObject_GC_Del(void *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000112
Georg Brandl60203b42010-10-06 10:11:56 +0000113 Releases memory allocated to an object using :c:func:`PyObject_GC_New` or
114 :c:func:`PyObject_GC_NewVar`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000115
116
Georg Brandl60203b42010-10-06 10:11:56 +0000117.. c:function:: void PyObject_GC_UnTrack(void *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000118
119 Remove the object *op* from the set of container objects tracked by the
Georg Brandl60203b42010-10-06 10:11:56 +0000120 collector. Note that :c:func:`PyObject_GC_Track` can be called again on
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000121 this object to add it back to the set of tracked objects. The deallocator
Antoine Pitrou39668f52013-08-01 21:12:45 +0200122 (:c:member:`~PyTypeObject.tp_dealloc` handler) should call this for the object before any of
123 the fields used by the :c:member:`~PyTypeObject.tp_traverse` handler become invalid.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000124
125
Victor Stinner1a6be912018-11-13 12:52:18 +0100126.. versionchanged:: 3.8
Georg Brandl54a3faa2008-01-20 09:30:57 +0000127
Victor Stinner1a6be912018-11-13 12:52:18 +0100128 The :c:func:`_PyObject_GC_TRACK` and :c:func:`_PyObject_GC_UNTRACK` macros
129 have been removed from the public C API.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000130
Antoine Pitrou39668f52013-08-01 21:12:45 +0200131The :c:member:`~PyTypeObject.tp_traverse` handler accepts a function parameter of this type:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000132
133
Georg Brandl60203b42010-10-06 10:11:56 +0000134.. c:type:: int (*visitproc)(PyObject *object, void *arg)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000135
Antoine Pitrou39668f52013-08-01 21:12:45 +0200136 Type of the visitor function passed to the :c:member:`~PyTypeObject.tp_traverse` handler.
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000137 The function should be called with an object to traverse as *object* and
Antoine Pitrou39668f52013-08-01 21:12:45 +0200138 the third parameter to the :c:member:`~PyTypeObject.tp_traverse` handler as *arg*. The
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000139 Python core uses several visitor functions to implement cyclic garbage
140 detection; it's not expected that users will need to write their own
141 visitor functions.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000142
Antoine Pitrou39668f52013-08-01 21:12:45 +0200143The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000144
145
Georg Brandl60203b42010-10-06 10:11:56 +0000146.. c:type:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000147
148 Traversal function for a container object. Implementations must call the
149 *visit* function for each object directly contained by *self*, with the
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000150 parameters to *visit* being the contained object and the *arg* value passed
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200151 to the handler. The *visit* function must not be called with a ``NULL``
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000152 object argument. If *visit* returns a non-zero value that value should be
153 returned immediately.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000154
Antoine Pitrou39668f52013-08-01 21:12:45 +0200155To simplify writing :c:member:`~PyTypeObject.tp_traverse` handlers, a :c:func:`Py_VISIT` macro is
156provided. In order to use this macro, the :c:member:`~PyTypeObject.tp_traverse` implementation
Georg Brandl54a3faa2008-01-20 09:30:57 +0000157must name its arguments exactly *visit* and *arg*:
158
159
Georg Brandl60203b42010-10-06 10:11:56 +0000160.. c:function:: void Py_VISIT(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000161
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200162 If *o* is not ``NULL``, call the *visit* callback, with arguments *o*
Benjamin Petersona49ccf22016-06-02 11:35:59 -0700163 and *arg*. If *visit* returns a non-zero value, then return it.
164 Using this macro, :c:member:`~PyTypeObject.tp_traverse` handlers
165 look like::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000166
167 static int
168 my_traverse(Noddy *self, visitproc visit, void *arg)
169 {
170 Py_VISIT(self->foo);
171 Py_VISIT(self->bar);
172 return 0;
173 }
174
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200175The :c:member:`~PyTypeObject.tp_clear` handler must be of the :c:type:`inquiry` type, or ``NULL``
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000176if the object is immutable.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000177
178
Georg Brandl60203b42010-10-06 10:11:56 +0000179.. c:type:: int (*inquiry)(PyObject *self)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000180
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000181 Drop references that may have created reference cycles. Immutable objects
182 do not have to define this method since they can never directly create
183 reference cycles. Note that the object must still be valid after calling
Georg Brandl60203b42010-10-06 10:11:56 +0000184 this method (don't just call :c:func:`Py_DECREF` on a reference). The
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +0000185 collector will call this method if it detects that this object is involved
186 in a reference cycle.
scoder3cc481b2021-04-28 18:12:16 +0200187
188
189Controlling the Garbage Collector State
190---------------------------------------
191
192The C-API provides the following functions for controlling
193garbage collection runs.
194
195.. c:function:: Py_ssize_t PyGC_Collect(void)
196
197 Perform a full garbage collection, if the garbage collector is enabled.
198 (Note that :func:`gc.collect` runs it unconditionally.)
199
200 Returns the number of collected + unreachable objects which cannot
201 be collected.
202 If the garbage collector is disabled or already collecting,
203 returns ``0`` immediately.
204 Errors during garbage collection are passed to :data:`sys.unraisablehook`.
205 This function does not raise exceptions.
206
207
208.. c:function:: int PyGC_Enable(void)
209
210 Enable the garbage collector: similar to :func:`gc.enable`.
211 Returns the previous state, 0 for disabled and 1 for enabled.
212
213 .. versionadded:: 3.10
214
215
216.. c:function:: int PyGC_Disable(void)
217
218 Disable the garbage collector: similar to :func:`gc.disable`.
219 Returns the previous state, 0 for disabled and 1 for enabled.
220
221 .. versionadded:: 3.10
222
223
224.. c:function:: int PyGC_IsEnabled(void)
225
226 Query the state of the garbage collector: similar to :func:`gc.isenabled`.
227 Returns the current state, 0 for disabled and 1 for enabled.
228
229 .. versionadded:: 3.10