• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. highlight:: c
2
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
12or strings), do not need to provide any explicit support for garbage
13collection.
14
15To create a container type, the :c:member:`~PyTypeObject.tp_flags` field of the type object must
16include the :const:`Py_TPFLAGS_HAVE_GC` and provide an implementation of the
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.
19
20
21.. data:: Py_TPFLAGS_HAVE_GC
22   :noindex:
23
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.
27
28Constructors for container types must conform to two rules:
29
30#. The memory for the object must be allocated using :c:func:`PyObject_GC_New`
31   or :c:func:`PyObject_GC_NewVar`.
32
33#. Once all the fields which may contain references to other containers are
34   initialized, it must call :c:func:`PyObject_GC_Track`.
35
36
37.. c:function:: TYPE* PyObject_GC_New(TYPE, PyTypeObject *type)
38
39   Analogous to :c:func:`PyObject_New` but for container objects with the
40   :const:`Py_TPFLAGS_HAVE_GC` flag set.
41
42
43.. c:function:: TYPE* PyObject_GC_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
44
45   Analogous to :c:func:`PyObject_NewVar` but for container objects with the
46   :const:`Py_TPFLAGS_HAVE_GC` flag set.
47
48
49.. c:function:: TYPE* PyObject_GC_Resize(TYPE, PyVarObject *op, Py_ssize_t newsize)
50
51   Resize an object allocated by :c:func:`PyObject_NewVar`.  Returns the
52   resized object or ``NULL`` on failure.  *op* must not be tracked by the collector yet.
53
54
55.. c:function:: void PyObject_GC_Track(PyObject *op)
56
57   Adds the object *op* to the set of container objects tracked by the
58   collector.  The collector can run at unexpected times so objects must be
59   valid while being tracked.  This should be called once all the fields
60   followed by the :c:member:`~PyTypeObject.tp_traverse` handler become valid, usually near the
61   end of the constructor.
62
63
64Similarly, the deallocator for the object must conform to a similar pair of
65rules:
66
67#. Before fields which refer to other containers are invalidated,
68   :c:func:`PyObject_GC_UnTrack` must be called.
69
70#. The object's memory must be deallocated using :c:func:`PyObject_GC_Del`.
71
72
73.. c:function:: void PyObject_GC_Del(void *op)
74
75   Releases memory allocated to an object using :c:func:`PyObject_GC_New` or
76   :c:func:`PyObject_GC_NewVar`.
77
78
79.. c:function:: void PyObject_GC_UnTrack(void *op)
80
81   Remove the object *op* from the set of container objects tracked by the
82   collector.  Note that :c:func:`PyObject_GC_Track` can be called again on
83   this object to add it back to the set of tracked objects.  The deallocator
84   (:c:member:`~PyTypeObject.tp_dealloc` handler) should call this for the object before any of
85   the fields used by the :c:member:`~PyTypeObject.tp_traverse` handler become invalid.
86
87
88.. versionchanged:: 3.8
89
90   The :c:func:`_PyObject_GC_TRACK` and :c:func:`_PyObject_GC_UNTRACK` macros
91   have been removed from the public C API.
92
93The :c:member:`~PyTypeObject.tp_traverse` handler accepts a function parameter of this type:
94
95
96.. c:type:: int (*visitproc)(PyObject *object, void *arg)
97
98   Type of the visitor function passed to the :c:member:`~PyTypeObject.tp_traverse` handler.
99   The function should be called with an object to traverse as *object* and
100   the third parameter to the :c:member:`~PyTypeObject.tp_traverse` handler as *arg*.  The
101   Python core uses several visitor functions to implement cyclic garbage
102   detection; it's not expected that users will need to write their own
103   visitor functions.
104
105The :c:member:`~PyTypeObject.tp_traverse` handler must have the following type:
106
107
108.. c:type:: int (*traverseproc)(PyObject *self, visitproc visit, void *arg)
109
110   Traversal function for a container object.  Implementations must call the
111   *visit* function for each object directly contained by *self*, with the
112   parameters to *visit* being the contained object and the *arg* value passed
113   to the handler.  The *visit* function must not be called with a ``NULL``
114   object argument.  If *visit* returns a non-zero value that value should be
115   returned immediately.
116
117To simplify writing :c:member:`~PyTypeObject.tp_traverse` handlers, a :c:func:`Py_VISIT` macro is
118provided.  In order to use this macro, the :c:member:`~PyTypeObject.tp_traverse` implementation
119must name its arguments exactly *visit* and *arg*:
120
121
122.. c:function:: void Py_VISIT(PyObject *o)
123
124   If *o* is not ``NULL``, call the *visit* callback, with arguments *o*
125   and *arg*.  If *visit* returns a non-zero value, then return it.
126   Using this macro, :c:member:`~PyTypeObject.tp_traverse` handlers
127   look like::
128
129      static int
130      my_traverse(Noddy *self, visitproc visit, void *arg)
131      {
132          Py_VISIT(self->foo);
133          Py_VISIT(self->bar);
134          return 0;
135      }
136
137The :c:member:`~PyTypeObject.tp_clear` handler must be of the :c:type:`inquiry` type, or ``NULL``
138if the object is immutable.
139
140
141.. c:type:: int (*inquiry)(PyObject *self)
142
143   Drop references that may have created reference cycles.  Immutable objects
144   do not have to define this method since they can never directly create
145   reference cycles.  Note that the object must still be valid after calling
146   this method (don't just call :c:func:`Py_DECREF` on a reference).  The
147   collector will call this method if it detects that this object is involved
148   in a reference cycle.
149