Supporting Cyclic Garbage Collection

Python’s support for detecting and collecting garbage which involves circularreferences requires support from object types which are “containers” for otherobjects which may also be containers. Types which do not store references toother objects, or which only store references to atomic types (such as numbersor strings), do not need to provide any explicit support for garbagecollection.

To create a container type, thetp_flags field of the type object mustinclude thePy_TPFLAGS_HAVE_GC and provide an implementation of thetp_traverse handler. If instances of the type are mutable, atp_clear implementation must also be provided.

Py_TPFLAGS_HAVE_GC

Objects with a type with this flag set must conform with the rulesdocumented here. For convenience these objects will be referred to ascontainer objects.

Constructors for container types must conform to two rules:

  1. The memory for the object must be allocated usingPyObject_GC_New()orPyObject_GC_NewVar().
  2. Once all the fields which may contain references to other containers areinitialized, it must callPyObject_GC_Track().
TYPE*PyObject_GC_New(TYPE,PyTypeObject *type)

Analogous toPyObject_New() but for container objects with thePy_TPFLAGS_HAVE_GC flag set.

TYPE*PyObject_GC_NewVar(TYPE,PyTypeObject *type, Py_ssize_t size)

Analogous toPyObject_NewVar() but for container objects with thePy_TPFLAGS_HAVE_GC flag set.

TYPE*PyObject_GC_Resize(TYPE,PyVarObject *op, Py_ssize_t newsize)

Resize an object allocated byPyObject_NewVar(). Returns theresized object orNULL on failure.

voidPyObject_GC_Track(PyObject *op)

Adds the objectop to the set of container objects tracked by thecollector. The collector can run at unexpected times so objects must bevalid while being tracked. This should be called once all the fieldsfollowed by thetp_traverse handler become valid, usually near theend of the constructor.

void_PyObject_GC_TRACK(PyObject *op)

A macro version ofPyObject_GC_Track(). It should not be used forextension modules.

Similarly, the deallocator for the object must conform to a similar pair ofrules:

  1. Before fields which refer to other containers are invalidated,PyObject_GC_UnTrack() must be called.
  2. The object’s memory must be deallocated usingPyObject_GC_Del().
voidPyObject_GC_Del(void *op)

Releases memory allocated to an object usingPyObject_GC_New() orPyObject_GC_NewVar().

voidPyObject_GC_UnTrack(void *op)

Remove the objectop from the set of container objects tracked by thecollector. Note thatPyObject_GC_Track() can be called again onthis object to add it back to the set of tracked objects. The deallocator(tp_dealloc handler) should call this for the object before any ofthe fields used by thetp_traverse handler become invalid.

void_PyObject_GC_UNTRACK(PyObject *op)

A macro version ofPyObject_GC_UnTrack(). It should not be used forextension modules.

Thetp_traverse handler accepts a function parameter of this type:

int(*visitproc)(PyObject *object, void *arg)

Type of the visitor function passed to thetp_traverse handler.The function should be called with an object to traverse asobject andthe third parameter to thetp_traverse handler asarg. ThePython core uses several visitor functions to implement cyclic garbagedetection; it’s not expected that users will need to write their ownvisitor functions.

Thetp_traverse handler must have the following type:

int(*traverseproc)(PyObject *self,visitproc visit, void *arg)

Traversal function for a container object. Implementations must call thevisit function for each object directly contained byself, with theparameters tovisit being the contained object and thearg value passedto the handler. Thevisit function must not be called with aNULLobject argument. Ifvisit returns a non-zero value that value should bereturned immediately.

To simplify writingtp_traverse handlers, aPy_VISIT() macro isprovided. In order to use this macro, thetp_traverse implementationmust name its arguments exactlyvisit andarg:

voidPy_VISIT(PyObject *o)

Ifo is notNULL, call thevisit callback, with argumentsoandarg. Ifvisit returns a non-zero value, then return it.Using this macro,tp_traverse handlerslook like:

staticintmy_traverse(Noddy*self,visitprocvisit,void*arg){Py_VISIT(self->foo);Py_VISIT(self->bar);return0;}

Thetp_clear handler must be of theinquiry type, orNULLif the object is immutable.

int(*inquiry)(PyObject *self)

Drop references that may have created reference cycles. Immutable objectsdo not have to define this method since they can never directly createreference cycles. Note that the object must still be valid after callingthis method (don’t just callPy_DECREF() on a reference). Thecollector will call this method if it detects that this object is involvedin a reference cycle.