| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="api.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="api.html" title='Python/C API Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="exceptionHandling.html" /> |
| 13 | <link rel="prev" href="veryhigh.html" /> |
| 14 | <link rel="parent" href="api.html" /> |
| 15 | <link rel="next" href="exceptionHandling.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3. Reference Counting </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="2. The Very High" |
| 25 | href="veryhigh.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="Python/C API Reference Manual" |
| 28 | href="api.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="4. Exception Handling" |
| 31 | href="exceptionHandling.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="veryhigh.html">2. The Very High</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="api.html">Python/C API Reference Manual</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="exceptionHandling.html">4. Exception Handling</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION005000000000000000000"></A><A NAME="countingRefs"></A> |
| 56 | <BR> |
| 57 | 3. Reference Counting |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | The macros in this section are used for managing reference counts |
| 62 | of Python objects. |
| 63 | |
| 64 | <P> |
| 65 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-84' xml:id='l2h-84' class="cfunction">Py_INCREF</tt></b>(</nobr></td><td>PyObject *<var>o</var>)</td></tr></table></dt> |
| 66 | <dd> |
| 67 | Increment the reference count for object <var>o</var>. The object must |
| 68 | not be <tt class="constant">NULL</tt>; if you aren't sure that it isn't <tt class="constant">NULL</tt>, use |
| 69 | <tt class="cfunction">Py_XINCREF()</tt>. |
| 70 | </dd></dl> |
| 71 | |
| 72 | <P> |
| 73 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-85' xml:id='l2h-85' class="cfunction">Py_XINCREF</tt></b>(</nobr></td><td>PyObject *<var>o</var>)</td></tr></table></dt> |
| 74 | <dd> |
| 75 | Increment the reference count for object <var>o</var>. The object may be |
| 76 | <tt class="constant">NULL</tt>, in which case the macro has no effect. |
| 77 | </dd></dl> |
| 78 | |
| 79 | <P> |
| 80 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-86' xml:id='l2h-86' class="cfunction">Py_DECREF</tt></b>(</nobr></td><td>PyObject *<var>o</var>)</td></tr></table></dt> |
| 81 | <dd> |
| 82 | Decrement the reference count for object <var>o</var>. The object must |
| 83 | not be <tt class="constant">NULL</tt>; if you aren't sure that it isn't <tt class="constant">NULL</tt>, use |
| 84 | <tt class="cfunction">Py_XDECREF()</tt>. If the reference count reaches zero, the |
| 85 | object's type's deallocation function (which must not be <tt class="constant">NULL</tt>) is |
| 86 | invoked. |
| 87 | |
| 88 | <P> |
| 89 | <span class="warning"><b class="label">Warning:</b> |
| 90 | The deallocation function can cause arbitrary Python code |
| 91 | to be invoked (e.g. when a class instance with a <tt class="method">__del__()</tt> |
| 92 | method is deallocated). While exceptions in such code are not |
| 93 | propagated, the executed code has free access to all Python global |
| 94 | variables. This means that any object that is reachable from a |
| 95 | global variable should be in a consistent state before |
| 96 | <tt class="cfunction">Py_DECREF()</tt> is invoked. For example, code to delete an |
| 97 | object from a list should copy a reference to the deleted object in |
| 98 | a temporary variable, update the list data structure, and then call |
| 99 | <tt class="cfunction">Py_DECREF()</tt> for the temporary variable.</span> |
| 100 | </dd></dl> |
| 101 | |
| 102 | <P> |
| 103 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-87' xml:id='l2h-87' class="cfunction">Py_XDECREF</tt></b>(</nobr></td><td>PyObject *<var>o</var>)</td></tr></table></dt> |
| 104 | <dd> |
| 105 | Decrement the reference count for object <var>o</var>. The object may be |
| 106 | <tt class="constant">NULL</tt>, in which case the macro has no effect; otherwise the effect |
| 107 | is the same as for <tt class="cfunction">Py_DECREF()</tt>, and the same warning |
| 108 | applies. |
| 109 | </dd></dl> |
| 110 | |
| 111 | <P> |
| 112 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void <b><tt id='l2h-88' xml:id='l2h-88' class="cfunction">Py_CLEAR</tt></b>(</nobr></td><td>PyObject *<var>o</var>)</td></tr></table></dt> |
| 113 | <dd> |
| 114 | Decrement the reference count for object <var>o</var>. The object may be |
| 115 | <tt class="constant">NULL</tt>, in which case the macro has no effect; otherwise the effect |
| 116 | is the same as for <tt class="cfunction">Py_DECREF()</tt>, except that the argument |
| 117 | is also set to <tt class="constant">NULL</tt>. The warning for <tt class="cfunction">Py_DECREF()</tt> does |
| 118 | not apply with respect to the object passed because the macro |
| 119 | carefully uses a temporary variable and sets the argument to <tt class="constant">NULL</tt> |
| 120 | before decrementing its reference count. |
| 121 | |
| 122 | <P> |
| 123 | It is a good idea to use this macro whenever decrementing the value |
| 124 | of a variable that might be traversed during garbage collection. |
| 125 | |
| 126 | <P> |
| 127 | |
| 128 | <span class="versionnote">New in version 2.4.</span> |
| 129 | |
| 130 | </dd></dl> |
| 131 | |
| 132 | <P> |
| 133 | The following functions are for runtime dynamic embedding of Python: |
| 134 | <tt class="cfunction">Py_IncRef(PyObject *o)</tt>, <tt class="cfunction">Py_DecRef(PyObject *o)</tt>. |
| 135 | They are simply exported function versions of <tt class="cfunction">Py_XINCREF()</tt> and |
| 136 | <tt class="cfunction">Py_XDECREF()</tt>, respectively. |
| 137 | |
| 138 | <P> |
| 139 | The following functions or macros are only for use within the |
| 140 | interpreter core: <tt class="cfunction">_Py_Dealloc()</tt>, |
| 141 | <tt class="cfunction">_Py_ForgetReference()</tt>, <tt class="cfunction">_Py_NewReference()</tt>, as |
| 142 | well as the global variable <tt class="cdata">_Py_RefTotal</tt>. |
| 143 | |
| 144 | <DIV CLASS="navigation"> |
| 145 | <div class='online-navigation'> |
| 146 | <p></p><hr /> |
| 147 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 148 | <tr> |
| 149 | <td class='online-navigation'><a rel="prev" title="2. The Very High" |
| 150 | href="veryhigh.html"><img src='../icons/previous.png' |
| 151 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 152 | <td class='online-navigation'><a rel="parent" title="Python/C API Reference Manual" |
| 153 | href="api.html"><img src='../icons/up.png' |
| 154 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 155 | <td class='online-navigation'><a rel="next" title="4. Exception Handling" |
| 156 | href="exceptionHandling.html"><img src='../icons/next.png' |
| 157 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 158 | <td align="center" width="100%">Python/C API Reference Manual</td> |
| 159 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 160 | href="contents.html"><img src='../icons/contents.png' |
| 161 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 162 | <td class='online-navigation'><img src='../icons/blank.png' |
| 163 | border='0' height='32' alt='' width='32' /></td> |
| 164 | <td class='online-navigation'><a rel="index" title="Index" |
| 165 | href="genindex.html"><img src='../icons/index.png' |
| 166 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 167 | </tr></table> |
| 168 | <div class='online-navigation'> |
| 169 | <b class="navlabel">Previous:</b> |
| 170 | <a class="sectref" rel="prev" href="veryhigh.html">2. The Very High</A> |
| 171 | <b class="navlabel">Up:</b> |
| 172 | <a class="sectref" rel="parent" href="api.html">Python/C API Reference Manual</A> |
| 173 | <b class="navlabel">Next:</b> |
| 174 | <a class="sectref" rel="next" href="exceptionHandling.html">4. Exception Handling</A> |
| 175 | </div> |
| 176 | </div> |
| 177 | <hr /> |
| 178 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 179 | </DIV> |
| 180 | <!--End of Navigation Panel--> |
| 181 | <ADDRESS> |
| 182 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 183 | </ADDRESS> |
| 184 | </BODY> |
| 185 | </HTML> |