MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //usr/share/doc/python-docs-2.4.3/html/api/supporting-cycle-detection.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="api.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="api.html" title='Python/C API Reference Manual' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="prev" href="supporting-iteration.html" />
<link rel="parent" href="newTypes.html" />
<link rel="next" href="reporting-bugs.html" />
<meta name='aesop' content='information' />
<title>10.9 Supporting Cyclic Garbage Collection </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="10.8 Supporting the Iterator"
  href="supporting-iteration.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="10. Object Implementation Support"
  href="newTypes.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="A. Reporting Bugs"
  href="reporting-bugs.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python/C API Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="supporting-iteration.html">10.8 Supporting the Iterator</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="newTypes.html">10. Object Implementation Support</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="reporting-bugs.html">A. Reporting Bugs</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION0012900000000000000000"></A><A NAME="supporting-cycle-detection"></A>
<BR>
10.9 Supporting Cyclic Garbage Collection
         
</H1>

<P>
Python's support for detecting and collecting garbage which involves
circular references requires support from object types which are
``containers'' for other objects which may also be containers.  Types
which do not store references to other objects, or which only store
references to atomic types (such as numbers or strings), do not need
to provide any explicit support for garbage collection.

<P>
An example showing the use of these interfaces can be found in
``<a class="ulink" href="../ext/example-cycle-support.html"
  >Supporting the Cycle
Collector</a>'' in
<em class="citetitle"><a
 href="../ext/ext.html"
 title="Extending and Embedding the Python
Interpreter"
 >Extending and Embedding the Python
Interpreter</a></em>.

<P>
To create a container type, the <tt class="member">tp_flags</tt> field of the type
object must include the <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> and provide an
implementation of the <tt class="member">tp_traverse</tt> handler.  If instances of the
type are mutable, a <tt class="member">tp_clear</tt> implementation must also be
provided.

<P>
<dl><dt><b><tt id='l2h-1014' xml:id='l2h-1014'>Py_TPFLAGS_HAVE_GC</tt></b></dt>
<dd>
  Objects with a type with this flag set must conform with the rules
  documented here.  For convenience these objects will be referred to
  as container objects.
</dd></dl>

<P>
Constructors for container types must conform to two rules:

<P>

<OL>
<LI>The memory for the object must be allocated using
       <tt class="cfunction">PyObject_GC_New()</tt> or <tt class="cfunction">PyObject_GC_VarNew()</tt>.

<P>
</LI>
<LI>Once all the fields which may contain references to other
       containers are initialized, it must call
       <tt class="cfunction">PyObject_GC_Track()</tt>.
</LI>
</OL>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr><var>TYPE</var>*&nbsp;<b><tt id='l2h-1015' xml:id='l2h-1015' class="cfunction">PyObject_GC_New</tt></b>(</nobr></td><td>TYPE, PyTypeObject *<var>type</var>)</td></tr></table></dt>
<dd>
  Analogous to <tt class="cfunction">PyObject_New()</tt> but for container objects with
  the <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> flag set.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr><var>TYPE</var>*&nbsp;<b><tt id='l2h-1016' xml:id='l2h-1016' class="cfunction">PyObject_GC_NewVar</tt></b>(</nobr></td><td>TYPE, PyTypeObject *<var>type</var>,
                                                   int <var>size</var>)</td></tr></table></dt>
<dd>
  Analogous to <tt class="cfunction">PyObject_NewVar()</tt> but for container objects
  with the <tt class="constant">Py_TPFLAGS_HAVE_GC</tt> flag set.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyVarObject *&nbsp;<b><tt id='l2h-1017' xml:id='l2h-1017' class="cfunction">PyObject_GC_Resize</tt></b>(</nobr></td><td>PyVarObject *<var>op</var>, <var>int</var>)</td></tr></table></dt>
<dd>
  Resize an object allocated by <tt class="cfunction">PyObject_NewVar()</tt>.  Returns
  the resized object or <tt class="constant">NULL</tt> on failure.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void&nbsp;<b><tt id='l2h-1018' xml:id='l2h-1018' class="cfunction">PyObject_GC_Track</tt></b>(</nobr></td><td>PyObject *<var>op</var>)</td></tr></table></dt>
<dd>
  Adds the object <var>op</var> to the set of container objects tracked by
  the collector.  The collector can run at unexpected times so objects
  must be valid while being tracked.  This should be called once all
  the fields followed by the <tt class="member">tp_traverse</tt> handler become valid,
  usually near the end of the constructor.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void&nbsp;<b><tt id='l2h-1019' xml:id='l2h-1019' class="cfunction">_PyObject_GC_TRACK</tt></b>(</nobr></td><td>PyObject *<var>op</var>)</td></tr></table></dt>
<dd>
  A macro version of <tt class="cfunction">PyObject_GC_Track()</tt>.  It should not be
  used for extension modules.
</dd></dl>

<P>
Similarly, the deallocator for the object must conform to a similar
pair of rules:

<P>

<OL>
<LI>Before fields which refer to other containers are invalidated,
       <tt class="cfunction">PyObject_GC_UnTrack()</tt> must be called.

<P>
</LI>
<LI>The object's memory must be deallocated using
       <tt class="cfunction">PyObject_GC_Del()</tt>.
</LI>
</OL>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void&nbsp;<b><tt id='l2h-1020' xml:id='l2h-1020' class="cfunction">PyObject_GC_Del</tt></b>(</nobr></td><td>PyObject *<var>op</var>)</td></tr></table></dt>
<dd>
  Releases memory allocated to an object using
  <tt class="cfunction">PyObject_GC_New()</tt> or <tt class="cfunction">PyObject_GC_NewVar()</tt>.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void&nbsp;<b><tt id='l2h-1021' xml:id='l2h-1021' class="cfunction">PyObject_GC_UnTrack</tt></b>(</nobr></td><td>PyObject *<var>op</var>)</td></tr></table></dt>
<dd>
  Remove the object <var>op</var> from the set of container objects tracked
  by the collector.  Note that <tt class="cfunction">PyObject_GC_Track()</tt> can be
  called again on this object to add it back to the set of tracked
  objects.  The deallocator (<tt class="member">tp_dealloc</tt> handler) should call
  this for the object before any of the fields used by the
  <tt class="member">tp_traverse</tt> handler become invalid.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void&nbsp;<b><tt id='l2h-1022' xml:id='l2h-1022' class="cfunction">_PyObject_GC_UNTRACK</tt></b>(</nobr></td><td>PyObject *<var>op</var>)</td></tr></table></dt>
<dd>
  A macro version of <tt class="cfunction">PyObject_GC_UnTrack()</tt>.  It should not be
  used for extension modules.
</dd></dl>

<P>
The <tt class="member">tp_traverse</tt> handler accepts a function parameter of this
type:

<P>
<dl><dt><b><tt class="ctype"><a id='l2h-1023' xml:id='l2h-1023'>int (*visitproc)(PyObject *object, void *arg)</a></tt></b></dt>
<dd>
  Type of the visitor function passed to the <tt class="member">tp_traverse</tt>
  handler.  The function should be called with an object to traverse
  as <var>object</var> and the third parameter to the <tt class="member">tp_traverse</tt>
  handler as <var>arg</var>.  The Python core uses several visitor functions
  to implement cyclic garbage detection; it's not expected that users will
  need to write their own visitor functions.
</dl>

<P>
The <tt class="member">tp_traverse</tt> handler must have the following type:

<P>
<dl><dt><b><tt class="ctype"><a id='l2h-1024' xml:id='l2h-1024'>int (*traverseproc)(PyObject *self,
                                visitproc visit, void *arg)</a></tt></b></dt>
<dd>
  Traversal function for a container object.  Implementations must
  call the <var>visit</var> function for each object directly contained by
  <var>self</var>, with the parameters to <var>visit</var> being the contained
  object and the <var>arg</var> value passed to the handler.  The <var>visit</var>
  function must not be called with a <tt class="constant">NULL</tt> object argument.  If
  <var>visit</var> returns a non-zero value
  that value should be returned immediately.
</dl>

<P>
To simplify writing <tt class="member">tp_traverse</tt> handlers, a
<tt class="cfunction">Py_VISIT()</tt> macro is provided.  In order to use this macro,
the <tt class="member">tp_traverse</tt> implementation must name its arguments
exactly <var>visit</var> and <var>arg</var>:

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void&nbsp;<b><tt id='l2h-1025' xml:id='l2h-1025' class="cfunction">Py_VISIT</tt></b>(</nobr></td><td>PyObject *<var>o</var>)</td></tr></table></dt>
<dd>
  Call the <var>visit</var> callback, with arguments <var>o</var> and <var>arg</var>.
  If <var>visit</var> returns a non-zero value, then return it.  Using this
  macro, <tt class="member">tp_traverse</tt> handlers look like:

<P>
<div class="verbatim"><pre>
static int
my_traverse(Noddy *self, visitproc visit, void *arg)
{
    Py_VISIT(self-&gt;foo);
    Py_VISIT(self-&gt;bar);
    return 0;
}
</pre></div>

<P>

<span class="versionnote">New in version 2.4.</span>

</dd></dl>

<P>
The <tt class="member">tp_clear</tt> handler must be of the <tt class="ctype">inquiry</tt> type, or
<tt class="constant">NULL</tt> if the object is immutable.

<P>
<dl><dt><b><tt class="ctype"><a id='l2h-1026' xml:id='l2h-1026'>int (*inquiry)(PyObject *self)</a></tt></b></dt>
<dd>
  Drop references that may have created reference cycles.  Immutable
  objects do not have to define this method since they can never
  directly create reference cycles.  Note that the object must still
  be valid after calling this method (don't just call
  <tt class="cfunction">Py_DECREF()</tt> on a reference).  The collector will call
  this method if it detects that this object is involved in a
  reference cycle.
</dl>

<P>

<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="10.8 Supporting the Iterator"
  href="supporting-iteration.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="10. Object Implementation Support"
  href="newTypes.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="A. Reporting Bugs"
  href="reporting-bugs.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python/C API Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="supporting-iteration.html">10.8 Supporting the Iterator</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="newTypes.html">10. Object Implementation Support</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="reporting-bugs.html">A. Reporting Bugs</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.3, documentation updated on 29 March 2006.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>