@@ -4869,8 +4869,8 @@ \section{Buffer Object Structures \label{buffer-structs}}
48694869a \ctype {PyBufferProcs} structure.
48704870
48714871\strong {Note:} It is very important that your
4872- \ctype {PyTypeObject} structure uses \code {Py_TPFLAGS_DEFAULT} for the
4873- value of the \member {tp_flags} member rather than \code {0}. This
4872+ \ctype {PyTypeObject} structure uses \constant {Py_TPFLAGS_DEFAULT} for
4873+ the value of the \member {tp_flags} member rather than \code {0}. This
48744874tells the Python runtime that your \ctype {PyBufferProcs} structure
48754875contains the \member {bf_getcharbuffer} slot. Older versions of Python
48764876did not have this member, so a new Python interpreter using an old
@@ -4898,7 +4898,7 @@ \section{Buffer Object Structures \label{buffer-structs}}
48984898
48994899The last slot is \member {bf_getcharbuffer}, of type
49004900\ctype {getcharbufferproc}. This slot will only be present if the
4901- \code {Py_TPFLAGS_HAVE_GETCHARBUFFER} flag is present in the
4901+ \constant {Py_TPFLAGS_HAVE_GETCHARBUFFER} flag is present in the
49024902\member {tp_flags} field of the object's \ctype {PyTypeObject}. Before using
49034903this slot, the caller should test whether it is present by using the
49044904\cfunction {PyType_HasFeature()}\ttindex {PyType_HasFeature()} function.
@@ -4963,6 +4963,88 @@ \section{Buffer Object Structures \label{buffer-structs}}
49634963\end {ctypedesc }
49644964
49654965
4966+ \section {Supporting Cyclic Garbarge Collection
4967+ \label {supporting-cycle-detection } }
4968+
4969+ Python's support for detecting and collecting garbage which involves
4970+ circular references requires support from object types which are
4971+ `` containers'' for other objects which may also be containers. Types
4972+ which do not store references to other objects, or which only store
4973+ references to atomic types (such as numbers or strings), do not need
4974+ to provide any explicit support for garbage collection.
4975+
4976+ To create a container type, the \member {tp_flags} field of the type
4977+ object must include the \constant {Py_TPFLAGS_GC} and provide an
4978+ implementation of the \member {tp_traverse} handler. The value of the
4979+ \member {tp_basicsize} field must include \constant {PyGC_HEAD_SIZE} as
4980+ well. If instances of the type are mutable, a \member {tp_clear}
4981+ implementation must also be provided.
4982+
4983+ \begin {datadesc }{Py_TPFLAGS_GC}
4984+ Objects with a type with this flag set must conform with the rules
4985+ documented here. For convenience these objects will be referred to
4986+ as container objects.
4987+ \end {datadesc }
4988+
4989+ \begin {datadesc }{PyGC_HEAD_SIZE}
4990+ Extra memory needed for the garbage collector. Container objects
4991+ must include this in the calculation of their tp_basicsize. If the
4992+ collector is disabled at compile time then this is \code {0}.
4993+ \end {datadesc }
4994+
4995+ \begin {cfuncdesc }{void}{PyObject_GC_Init}{PyObject *op}
4996+ Adds the object \var {op} to the set of container objects tracked by
4997+ the collector. The collector can run at unexpected times so objects
4998+ must be valid while being tracked. This should be called once all
4999+ the fields followed by the \member {tp_traverse} handler become valid,
5000+ usually near the end of the constructor.
5001+ \end {cfuncdesc }
5002+
5003+ \begin {cfuncdesc }{void}{PyObject_GC_Fini}{PyObject *op}
5004+ Remove the object \var {op} from the set of container objects tracked
5005+ by the collector. Note that \cfunction {PyObject_GC_Init()} can be
5006+ called again on this object to add it back to the set of tracked
5007+ objects. The deallocator (\member {tp_dealloc} handler) should call
5008+ this for the object before any of the fields used by the
5009+ \member {tp_traverse} handler become invalid.
5010+ \end {cfuncdesc }
5011+
5012+ The \member {tp_traverse} handler accepts a function parameter of this
5013+ type:
5014+
5015+ \begin {ctypedesc }[visitproc]{int (*visitproc)(PyObject *object, void *arg)}
5016+ Type of the visitor function passed to the \member {tp_traverse}
5017+ handler. The function should be called with an object to traverse
5018+ as \var {object} and the third parameter to the \member {tp_traverse}
5019+ handler as \var {arg}.
5020+ \end {ctypedesc }
5021+
5022+ The \member {tp_traverse} handler must have the following type:
5023+
5024+ \begin {ctypedesc }[traverseproc]{int (*traverseproc)(PyObject *self,
5025+ visitproc visit, void *arg)}
5026+ Traversal function for a container object. Implementations must
5027+ call the \var {visit} function for each object directly contained by
5028+ \var {self}, with the parameters to \var {visit} being the contained
5029+ object and the \var {arg} value passed to the handler. If
5030+ \var {visit} returns a non-zero value then an error has occurred and
5031+ that value should be returned immediately.
5032+ \end {ctypedesc }
5033+
5034+ The \member {tp_clear} handler must be of the \ctype {inquiry} type, or
5035+ \NULL {} if the object is immutable.
5036+
5037+ \begin {ctypedesc }[inquiry]{int (*inquiry)(PyObject *self)}
5038+ Drop references that may have created reference cycles. Immutable
5039+ objects do not have to define this method since they can never
5040+ directly create reference cycles. Note that the object must still
5041+ be valid after calling this method (i.e., don't just call
5042+ \cfunction {Py_DECREF()} on a reference). The collector will call
5043+ this method if it detects that this object is involved in a
5044+ reference cycle.
5045+ \end {ctypedesc }
5046+
5047+
49665048% \chapter{Debugging \label{debugging}}
49675049%
49685050% XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
0 commit comments