Sophie

Sophie

distrib > Fedora > 14 > i386 > by-pkgid > aad95ed02015570e8e657e9b095a0226 > files > 535

python-docs-2.7-1.fc14.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Supporting Cyclic Garbage Collection &mdash; Python v2.7 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.7 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.7 documentation" href="../index.html" />
    <link rel="up" title="Object Implementation Support" href="objimpl.html" />
    <link rel="next" title="Distributing Python Modules" href="../distutils/index.html" />
    <link rel="prev" title="Type Objects" href="typeobj.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../distutils/index.html" title="Distributing Python Modules"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="typeobj.html" title="Type Objects"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.7 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="objimpl.html" accesskey="U">Object Implementation Support</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="supporting-cyclic-garbage-collection">
<span id="supporting-cycle-detection"></span><h1>Supporting Cyclic Garbage Collection<a class="headerlink" href="#supporting-cyclic-garbage-collection" title="Permalink to this headline">ΒΆ</a></h1>
<p>Python&#8217;s support for detecting and collecting garbage which involves circular
references requires support from object types which are &#8220;containers&#8221; 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>
<p>To create a container type, the <tt class="xref py py-attr docutils literal"><span class="pre">tp_flags</span></tt> field of the type object must
include the <tt class="xref py py-const docutils literal"><span class="pre">Py_TPFLAGS_HAVE_GC</span></tt> and provide an implementation of the
<tt class="xref py py-attr docutils literal"><span class="pre">tp_traverse</span></tt> handler.  If instances of the type are mutable, a
<tt class="xref py py-attr docutils literal"><span class="pre">tp_clear</span></tt> implementation must also be provided.</p>
<dl class="data">
<dt>
<tt class="descname">Py_TPFLAGS_HAVE_GC</tt></dt>
<dd><p>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.</p>
</dd></dl>

<p>Constructors for container types must conform to two rules:</p>
<ol class="arabic simple">
<li>The memory for the object must be allocated using <a href="#id1"><span class="problematic" id="id2">:cfunc:`PyObject_GC_New`</span></a>
or <a href="#id3"><span class="problematic" id="id4">:cfunc:`PyObject_GC_NewVar`</span></a>.</li>
<li>Once all the fields which may contain references to other containers are
initialized, it must call <a href="#id5"><span class="problematic" id="id6">:cfunc:`PyObject_GC_Track`</span></a>.</li>
</ol>
<p>Similarly, the deallocator for the object must conform to a similar pair of
rules:</p>
<ol class="arabic simple">
<li>Before fields which refer to other containers are invalidated,
<a href="#id7"><span class="problematic" id="id8">:cfunc:`PyObject_GC_UnTrack`</span></a> must be called.</li>
<li>The object&#8217;s memory must be deallocated using <a href="#id9"><span class="problematic" id="id10">:cfunc:`PyObject_GC_Del`</span></a>.</li>
</ol>
<p>The <tt class="xref py py-attr docutils literal"><span class="pre">tp_traverse</span></tt> handler accepts a function parameter of this type:</p>
<p>The <tt class="xref py py-attr docutils literal"><span class="pre">tp_traverse</span></tt> handler must have the following type:</p>
<p>To simplify writing <tt class="xref py py-attr docutils literal"><span class="pre">tp_traverse</span></tt> handlers, a <a href="#id11"><span class="problematic" id="id12">:cfunc:`Py_VISIT`</span></a> macro is
provided.  In order to use this macro, the <tt class="xref py py-attr docutils literal"><span class="pre">tp_traverse</span></tt> implementation
must name its arguments exactly <em>visit</em> and <em>arg</em>:</p>
<p>The <tt class="xref py py-attr docutils literal"><span class="pre">tp_clear</span></tt> handler must be of the <a href="#id13"><span class="problematic" id="id14">:ctype:`inquiry`</span></a> type, or <em>NULL</em>
if the object is immutable.</p>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="typeobj.html"
                        title="previous chapter">Type Objects</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="../distutils/index.html"
                        title="next chapter">Distributing Python Modules</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/c-api/gcsupport.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../distutils/index.html" title="Distributing Python Modules"
             >next</a> |</li>
        <li class="right" >
          <a href="typeobj.html" title="Type Objects"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.7 documentation</a> &raquo;</li>

          <li><a href="index.html" >Python/C API Reference Manual</a> &raquo;</li>
          <li><a href="objimpl.html" >Object Implementation Support</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Aug 09, 2010.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0b2.
    </div>

  </body>
</html>