3
.. _supporting-cycle-detection:
5
Supporting Cyclic Garbage Collection
6
====================================
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
12
or strings), do not need to provide any explicit support for garbage collection.
14
.. An example showing the use of these interfaces can be found in "Supporting the
15
.. Cycle Collector (XXX not found: ../ext/example-cycle-support.html)".
17
To create a container type, the :attr:`tp_flags` field of the type object must
18
include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the
19
:attr:`tp_traverse` handler. If instances of the type are mutable, a
20
:attr:`tp_clear` implementation must also be provided.
23
.. data:: Py_TPFLAGS_HAVE_GC
26
Objects with a type with this flag set must conform with the rules documented
27
here. For convenience these objects will be referred to as container objects.
29
Constructors for container types must conform to two rules:
31
#. The memory for the object must be allocated using :cfunc:`PyObject_GC_New` or
32
:cfunc:`PyObject_GC_VarNew`.
34
#. Once all the fields which may contain references to other containers are
35
initialized, it must call :cfunc:`PyObject_GC_Track`.
38
.. cfunction:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type)
40
Analogous to :cfunc:`PyObject_New` but for container objects with the
41
:const:`Py_TPFLAGS_HAVE_GC` flag set.
44
.. cfunction:: TYPE* PyObject_GC_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
46
Analogous to :cfunc:`PyObject_NewVar` but for container objects with the
47
:const:`Py_TPFLAGS_HAVE_GC` flag set.
50
.. cfunction:: PyVarObject * PyObject_GC_Resize(PyVarObject *op, Py_ssize_t)
52
Resize an object allocated by :cfunc:`PyObject_NewVar`. Returns the resized
53
object or *NULL* on failure.
56
.. cfunction:: void PyObject_GC_Track(PyObject *op)
58
Adds the object *op* to the set of container objects tracked by the collector.
59
The collector can run at unexpected times so objects must be valid while being
60
tracked. This should be called once all the fields followed by the
61
:attr:`tp_traverse` handler become valid, usually near the end of the
65
.. cfunction:: void _PyObject_GC_TRACK(PyObject *op)
67
A macro version of :cfunc:`PyObject_GC_Track`. It should not be used for
70
Similarly, the deallocator for the object must conform to a similar pair of
73
#. Before fields which refer to other containers are invalidated,
74
:cfunc:`PyObject_GC_UnTrack` must be called.
76
#. The object's memory must be deallocated using :cfunc:`PyObject_GC_Del`.
79
.. cfunction:: void PyObject_GC_Del(void *op)
81
Releases memory allocated to an object using :cfunc:`PyObject_GC_New` or
82
:cfunc:`PyObject_GC_NewVar`.
85
.. cfunction:: void PyObject_GC_UnTrack(void *op)
87
Remove the object *op* from the set of container objects tracked by the
88
collector. Note that :cfunc:`PyObject_GC_Track` can be called again on this
89
object to add it back to the set of tracked objects. The deallocator
90
(:attr:`tp_dealloc` handler) should call this for the object before any of the
91
fields used by the :attr:`tp_traverse` handler become invalid.
94
.. cfunction:: void _PyObject_GC_UNTRACK(PyObject *op)
96
A macro version of :cfunc:`PyObject_GC_UnTrack`. It should not be used for
99
The :attr:`tp_traverse` handler accepts a function parameter of this type:
102
.. ctype:: int (*visitproc)(PyObject *object, void *arg)
104
Type of the visitor function passed to the :attr:`tp_traverse` handler. The
105
function should be called with an object to traverse as *object* and the third
106
parameter to the :attr:`tp_traverse` handler as *arg*. The Python core uses
107
several visitor functions to implement cyclic garbage detection; it's not
108
expected that users will need to write their own visitor functions.
110
The :attr:`tp_traverse` handler must have the following type:
113
.. ctype:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
115
Traversal function for a container object. Implementations must call the
116
*visit* function for each object directly contained by *self*, with the
117
parameters to *visit* being the contained object and the *arg* value passed to
118
the handler. The *visit* function must not be called with a *NULL* object
119
argument. If *visit* returns a non-zero value that value should be returned
122
To simplify writing :attr:`tp_traverse` handlers, a :cfunc:`Py_VISIT` macro is
123
provided. In order to use this macro, the :attr:`tp_traverse` implementation
124
must name its arguments exactly *visit* and *arg*:
127
.. cfunction:: void Py_VISIT(PyObject *o)
129
Call the *visit* callback, with arguments *o* and *arg*. If *visit* returns a
130
non-zero value, then return it. Using this macro, :attr:`tp_traverse` handlers
134
my_traverse(Noddy *self, visitproc visit, void *arg)
141
.. versionadded:: 2.4
143
The :attr:`tp_clear` handler must be of the :ctype:`inquiry` type, or *NULL* if
144
the object is immutable.
147
.. ctype:: int (*inquiry)(PyObject *self)
149
Drop references that may have created reference cycles. Immutable objects do
150
not have to define this method since they can never directly create reference
151
cycles. Note that the object must still be valid after calling this method
152
(don't just call :cfunc:`Py_DECREF` on a reference). The collector will call
153
this method if it detects that this object is involved in a reference cycle.