refcounting.html 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
  6. <meta property="og:title" content="Reference Counting" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/refcounting.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="The functions and macros in this section are used for managing reference counts of Python objects." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="The functions and macros in this section are used for managing reference counts of Python objects." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Reference Counting &#8212; Python 3.12.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?digest=b37c26da2f7529d09fe70b41c4b2133fe4931a90" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css" />
  21. <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
  22. <script src="../_static/jquery.js"></script>
  23. <script src="../_static/underscore.js"></script>
  24. <script src="../_static/doctools.js"></script>
  25. <script src="../_static/sidebar.js"></script>
  26. <link rel="search" type="application/opensearchdescription+xml"
  27. title="Search within Python 3.12.0 documentation"
  28. href="../_static/opensearch.xml"/>
  29. <link rel="author" title="About these documents" href="../about.html" />
  30. <link rel="index" title="Index" href="../genindex.html" />
  31. <link rel="search" title="Search" href="../search.html" />
  32. <link rel="copyright" title="Copyright" href="../copyright.html" />
  33. <link rel="next" title="Exception Handling" href="exceptions.html" />
  34. <link rel="prev" title="The Very High Level Layer" href="veryhigh.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/c-api/refcounting.html" />
  36. <style>
  37. @media only screen {
  38. table.full-width-table {
  39. width: 100%;
  40. }
  41. }
  42. </style>
  43. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  44. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  45. <script type="text/javascript" src="../_static/copybutton.js"></script>
  46. <script type="text/javascript" src="../_static/menu.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h4>Previous topic</h4>
  84. <p class="topless"><a href="veryhigh.html"
  85. title="previous chapter">The Very High Level Layer</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="exceptions.html"
  90. title="next chapter">Exception Handling</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/refcounting.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="exceptions.html" title="Exception Handling"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="veryhigh.html" title="The Very High Level Layer"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.0 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python/C API Reference Manual</a> &#187;</li>
  133. <li class="nav-item nav-item-this"><a href="">Reference Counting</a></li>
  134. <li class="right">
  135. <div class="inline-search" role="search">
  136. <form class="inline-search" action="../search.html" method="get">
  137. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  138. <input type="submit" value="Go" />
  139. </form>
  140. </div>
  141. |
  142. </li>
  143. <li class="right">
  144. <label class="theme-selector-label">
  145. Theme
  146. <select class="theme-selector" oninput="activateTheme(this.value)">
  147. <option value="auto" selected>Auto</option>
  148. <option value="light">Light</option>
  149. <option value="dark">Dark</option>
  150. </select>
  151. </label> |</li>
  152. </ul>
  153. </div>
  154. <div class="document">
  155. <div class="documentwrapper">
  156. <div class="bodywrapper">
  157. <div class="body" role="main">
  158. <section id="reference-counting">
  159. <span id="countingrefs"></span><h1>Reference Counting<a class="headerlink" href="#reference-counting" title="Permalink to this headline">¶</a></h1>
  160. <p>The functions and macros in this section are used for managing reference counts
  161. of Python objects.</p>
  162. <dl class="c function">
  163. <dt class="sig sig-object c" id="c.Py_REFCNT">
  164. <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_REFCNT</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_REFCNT" title="Permalink to this definition">¶</a><br /></dt>
  165. <dd><p>Get the reference count of the Python object <em>o</em>.</p>
  166. <p>Note that the returned value may not actually reflect how many
  167. references to the object are actually held. For example, some
  168. objects are “immortal” and have a very high refcount that does not
  169. reflect the actual number of references. Consequently, do not rely
  170. on the returned value to be accurate, other than a value of 0 or 1.</p>
  171. <p>Use the <a class="reference internal" href="#c.Py_SET_REFCNT" title="Py_SET_REFCNT"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SET_REFCNT()</span></code></a> function to set an object reference count.</p>
  172. <div class="versionchanged">
  173. <p><span class="versionmodified changed">Changed in version 3.11: </span>The parameter type is no longer <span class="c-expr sig sig-inline c"><span class="k">const</span><span class="w"> </span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>.</p>
  174. </div>
  175. <div class="versionchanged">
  176. <p><span class="versionmodified changed">Changed in version 3.10: </span><a class="reference internal" href="#c.Py_REFCNT" title="Py_REFCNT"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_REFCNT()</span></code></a> is changed to the inline static function.</p>
  177. </div>
  178. </dd></dl>
  179. <dl class="c function">
  180. <dt class="sig sig-object c" id="c.Py_SET_REFCNT">
  181. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_SET_REFCNT</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span>, <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="n"><span class="pre">refcnt</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_SET_REFCNT" title="Permalink to this definition">¶</a><br /></dt>
  182. <dd><p>Set the object <em>o</em> reference counter to <em>refcnt</em>.</p>
  183. <p>Note that this function has no effect on
  184. <a class="reference external" href="https://peps.python.org/pep-0683/">immortal</a>
  185. objects.</p>
  186. <div class="versionadded">
  187. <p><span class="versionmodified added">New in version 3.9.</span></p>
  188. </div>
  189. <div class="versionchanged">
  190. <p><span class="versionmodified changed">Changed in version 3.12: </span>Immortal objects are not modified.</p>
  191. </div>
  192. </dd></dl>
  193. <dl class="c function">
  194. <dt class="sig sig-object c" id="c.Py_INCREF">
  195. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_INCREF</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_INCREF" title="Permalink to this definition">¶</a><br /></dt>
  196. <dd><p>Indicate taking a new <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> to object <em>o</em>,
  197. indicating it is in use and should not be destroyed.</p>
  198. <p>This function is usually used to convert a <a class="reference internal" href="../glossary.html#term-borrowed-reference"><span class="xref std std-term">borrowed reference</span></a> to a
  199. <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> in-place. The <a class="reference internal" href="#c.Py_NewRef" title="Py_NewRef"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_NewRef()</span></code></a> function can be
  200. used to create a new <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a>.</p>
  201. <p>When done using the object, release it by calling <a class="reference internal" href="#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a>.</p>
  202. <p>The object must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>; if you aren’t sure that it isn’t
  203. <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, use <a class="reference internal" href="#c.Py_XINCREF" title="Py_XINCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XINCREF()</span></code></a>.</p>
  204. <p>Do not expect this function to actually modify <em>o</em> in any way.
  205. For at least <a class="reference external" href="https://peps.python.org/pep-0683/">some objects</a>,
  206. this function has no effect.</p>
  207. <div class="versionchanged">
  208. <p><span class="versionmodified changed">Changed in version 3.12: </span>Immortal objects are not modified.</p>
  209. </div>
  210. </dd></dl>
  211. <dl class="c function">
  212. <dt class="sig sig-object c" id="c.Py_XINCREF">
  213. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_XINCREF</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_XINCREF" title="Permalink to this definition">¶</a><br /></dt>
  214. <dd><p>Similar to <a class="reference internal" href="#c.Py_INCREF" title="Py_INCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_INCREF()</span></code></a>, but the object <em>o</em> can be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>,
  215. in which case this has no effect.</p>
  216. <p>See also <a class="reference internal" href="#c.Py_XNewRef" title="Py_XNewRef"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XNewRef()</span></code></a>.</p>
  217. </dd></dl>
  218. <dl class="c function">
  219. <dt class="sig sig-object c" id="c.Py_NewRef">
  220. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_NewRef</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_NewRef" title="Permalink to this definition">¶</a><br /></dt>
  221. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.10.</em><p>Create a new <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> to an object:
  222. call <a class="reference internal" href="#c.Py_INCREF" title="Py_INCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_INCREF()</span></code></a> on <em>o</em> and return the object <em>o</em>.</p>
  223. <p>When the <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> is no longer needed, <a class="reference internal" href="#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a>
  224. should be called on it to release the reference.</p>
  225. <p>The object <em>o</em> must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>; use <a class="reference internal" href="#c.Py_XNewRef" title="Py_XNewRef"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XNewRef()</span></code></a> if <em>o</em> can be
  226. <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  227. <p>For example:</p>
  228. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_INCREF</span><span class="p">(</span><span class="n">obj</span><span class="p">);</span>
  229. <span class="n">self</span><span class="o">-&gt;</span><span class="n">attr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">obj</span><span class="p">;</span>
  230. </pre></div>
  231. </div>
  232. <p>can be written as:</p>
  233. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">self</span><span class="o">-&gt;</span><span class="n">attr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_NewRef</span><span class="p">(</span><span class="n">obj</span><span class="p">);</span>
  234. </pre></div>
  235. </div>
  236. <p>See also <a class="reference internal" href="#c.Py_INCREF" title="Py_INCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_INCREF()</span></code></a>.</p>
  237. <div class="versionadded">
  238. <p><span class="versionmodified added">New in version 3.10.</span></p>
  239. </div>
  240. </dd></dl>
  241. <dl class="c function">
  242. <dt class="sig sig-object c" id="c.Py_XNewRef">
  243. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_XNewRef</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_XNewRef" title="Permalink to this definition">¶</a><br /></dt>
  244. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.10.</em><p>Similar to <a class="reference internal" href="#c.Py_NewRef" title="Py_NewRef"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_NewRef()</span></code></a>, but the object <em>o</em> can be NULL.</p>
  245. <p>If the object <em>o</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the function just returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  246. <div class="versionadded">
  247. <p><span class="versionmodified added">New in version 3.10.</span></p>
  248. </div>
  249. </dd></dl>
  250. <dl class="c function">
  251. <dt class="sig sig-object c" id="c.Py_DECREF">
  252. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_DECREF</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_DECREF" title="Permalink to this definition">¶</a><br /></dt>
  253. <dd><p>Release a <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> to object <em>o</em>, indicating the
  254. reference is no longer used.</p>
  255. <p>Once the last <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> is released
  256. (i.e. the object’s reference count reaches 0),
  257. the object’s type’s deallocation
  258. function (which must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>) is invoked.</p>
  259. <p>This function is usually used to delete a <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> before
  260. exiting its scope.</p>
  261. <p>The object must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>; if you aren’t sure that it isn’t <code class="docutils literal notranslate"><span class="pre">NULL</span></code>,
  262. use <a class="reference internal" href="#c.Py_XDECREF" title="Py_XDECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XDECREF()</span></code></a>.</p>
  263. <p>Do not expect this function to actually modify <em>o</em> in any way.
  264. For at least <a class="reference external" href="https://peps.python.org/pep-0683/">some objects</a>,
  265. this function has no effect.</p>
  266. <div class="admonition warning">
  267. <p class="admonition-title">Warning</p>
  268. <p>The deallocation function can cause arbitrary Python code to be invoked (e.g.
  269. when a class instance with a <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> method is deallocated). While
  270. exceptions in such code are not propagated, the executed code has free access to
  271. all Python global variables. This means that any object that is reachable from
  272. a global variable should be in a consistent state before <a class="reference internal" href="#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a> is
  273. invoked. For example, code to delete an object from a list should copy a
  274. reference to the deleted object in a temporary variable, update the list data
  275. structure, and then call <a class="reference internal" href="#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a> for the temporary variable.</p>
  276. </div>
  277. <div class="versionchanged">
  278. <p><span class="versionmodified changed">Changed in version 3.12: </span>Immortal objects are not modified.</p>
  279. </div>
  280. </dd></dl>
  281. <dl class="c function">
  282. <dt class="sig sig-object c" id="c.Py_XDECREF">
  283. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_XDECREF</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_XDECREF" title="Permalink to this definition">¶</a><br /></dt>
  284. <dd><p>Similar to <a class="reference internal" href="#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a>, but the object <em>o</em> can be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>,
  285. in which case this has no effect.
  286. The same warning from <a class="reference internal" href="#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a> applies here as well.</p>
  287. </dd></dl>
  288. <dl class="c function">
  289. <dt class="sig sig-object c" id="c.Py_CLEAR">
  290. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_CLEAR</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_CLEAR" title="Permalink to this definition">¶</a><br /></dt>
  291. <dd><p>Release a <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> for object <em>o</em>.
  292. The object may be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, in
  293. which case the macro has no effect; otherwise the effect is the same as for
  294. <a class="reference internal" href="#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a>, except that the argument is also set to <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. The warning
  295. for <a class="reference internal" href="#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a> does not apply with respect to the object passed because
  296. the macro carefully uses a temporary variable and sets the argument to <code class="docutils literal notranslate"><span class="pre">NULL</span></code>
  297. before releasing the reference.</p>
  298. <p>It is a good idea to use this macro whenever releasing a reference
  299. to an object that might be traversed during garbage collection.</p>
  300. <div class="versionchanged">
  301. <p><span class="versionmodified changed">Changed in version 3.12: </span>The macro argument is now only evaluated once. If the argument has side
  302. effects, these are no longer duplicated.</p>
  303. </div>
  304. </dd></dl>
  305. <dl class="c function">
  306. <dt class="sig sig-object c" id="c.Py_IncRef">
  307. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_IncRef</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_IncRef" title="Permalink to this definition">¶</a><br /></dt>
  308. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Indicate taking a new <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> to object <em>o</em>.
  309. A function version of <a class="reference internal" href="#c.Py_XINCREF" title="Py_XINCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XINCREF()</span></code></a>.
  310. It can be used for runtime dynamic embedding of Python.</p>
  311. </dd></dl>
  312. <dl class="c function">
  313. <dt class="sig sig-object c" id="c.Py_DecRef">
  314. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_DecRef</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_DecRef" title="Permalink to this definition">¶</a><br /></dt>
  315. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Release a <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> to object <em>o</em>.
  316. A function version of <a class="reference internal" href="#c.Py_XDECREF" title="Py_XDECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XDECREF()</span></code></a>.
  317. It can be used for runtime dynamic embedding of Python.</p>
  318. </dd></dl>
  319. <dl class="c macro">
  320. <dt class="sig sig-object c" id="c.Py_SETREF">
  321. <span class="sig-name descname"><span class="n"><span class="pre">Py_SETREF</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">dst</span></span>, <span class="n"><span class="pre">src</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_SETREF" title="Permalink to this definition">¶</a><br /></dt>
  322. <dd><p>Macro safely releasing a <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> to object <em>dst</em>
  323. and setting <em>dst</em> to <em>src</em>.</p>
  324. <p>As in case of <a class="reference internal" href="#c.Py_CLEAR" title="Py_CLEAR"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_CLEAR()</span></code></a>, “the obvious” code can be deadly:</p>
  325. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">dst</span><span class="p">);</span>
  326. <span class="n">dst</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">src</span><span class="p">;</span>
  327. </pre></div>
  328. </div>
  329. <p>The safe way is:</p>
  330. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_SETREF</span><span class="p">(</span><span class="n">dst</span><span class="p">,</span><span class="w"> </span><span class="n">src</span><span class="p">);</span>
  331. </pre></div>
  332. </div>
  333. <p>That arranges to set <em>dst</em> to <em>src</em> _before_ releasing the reference
  334. to the old value of <em>dst</em>, so that any code triggered as a side-effect
  335. of <em>dst</em> getting torn down no longer believes <em>dst</em> points
  336. to a valid object.</p>
  337. <div class="versionadded">
  338. <p><span class="versionmodified added">New in version 3.6.</span></p>
  339. </div>
  340. <div class="versionchanged">
  341. <p><span class="versionmodified changed">Changed in version 3.12: </span>The macro arguments are now only evaluated once. If an argument has side
  342. effects, these are no longer duplicated.</p>
  343. </div>
  344. </dd></dl>
  345. <dl class="c macro">
  346. <dt class="sig sig-object c" id="c.Py_XSETREF">
  347. <span class="sig-name descname"><span class="n"><span class="pre">Py_XSETREF</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">dst</span></span>, <span class="n"><span class="pre">src</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_XSETREF" title="Permalink to this definition">¶</a><br /></dt>
  348. <dd><p>Variant of <a class="reference internal" href="#c.Py_SETREF" title="Py_SETREF"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_SETREF</span></code></a> macro that uses <a class="reference internal" href="#c.Py_XDECREF" title="Py_XDECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XDECREF()</span></code></a> instead
  349. of <a class="reference internal" href="#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a>.</p>
  350. <div class="versionadded">
  351. <p><span class="versionmodified added">New in version 3.6.</span></p>
  352. </div>
  353. <div class="versionchanged">
  354. <p><span class="versionmodified changed">Changed in version 3.12: </span>The macro arguments are now only evaluated once. If an argument has side
  355. effects, these are no longer duplicated.</p>
  356. </div>
  357. </dd></dl>
  358. </section>
  359. <div class="clearer"></div>
  360. </div>
  361. </div>
  362. </div>
  363. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  364. <div class="sphinxsidebarwrapper">
  365. <div>
  366. <h4>Previous topic</h4>
  367. <p class="topless"><a href="veryhigh.html"
  368. title="previous chapter">The Very High Level Layer</a></p>
  369. </div>
  370. <div>
  371. <h4>Next topic</h4>
  372. <p class="topless"><a href="exceptions.html"
  373. title="next chapter">Exception Handling</a></p>
  374. </div>
  375. <div role="note" aria-label="source link">
  376. <h3>This Page</h3>
  377. <ul class="this-page-menu">
  378. <li><a href="../bugs.html">Report a Bug</a></li>
  379. <li>
  380. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/refcounting.rst"
  381. rel="nofollow">Show Source
  382. </a>
  383. </li>
  384. </ul>
  385. </div>
  386. </div>
  387. </div>
  388. <div class="clearer"></div>
  389. </div>
  390. <div class="related" role="navigation" aria-label="related navigation">
  391. <h3>Navigation</h3>
  392. <ul>
  393. <li class="right" style="margin-right: 10px">
  394. <a href="../genindex.html" title="General Index"
  395. >index</a></li>
  396. <li class="right" >
  397. <a href="../py-modindex.html" title="Python Module Index"
  398. >modules</a> |</li>
  399. <li class="right" >
  400. <a href="exceptions.html" title="Exception Handling"
  401. >next</a> |</li>
  402. <li class="right" >
  403. <a href="veryhigh.html" title="The Very High Level Layer"
  404. >previous</a> |</li>
  405. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  406. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  407. <li class="switchers">
  408. <div class="language_switcher_placeholder"></div>
  409. <div class="version_switcher_placeholder"></div>
  410. </li>
  411. <li>
  412. </li>
  413. <li id="cpython-language-and-version">
  414. <a href="../index.html">3.12.0 Documentation</a> &#187;
  415. </li>
  416. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  417. <li class="nav-item nav-item-this"><a href="">Reference Counting</a></li>
  418. <li class="right">
  419. <div class="inline-search" role="search">
  420. <form class="inline-search" action="../search.html" method="get">
  421. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  422. <input type="submit" value="Go" />
  423. </form>
  424. </div>
  425. |
  426. </li>
  427. <li class="right">
  428. <label class="theme-selector-label">
  429. Theme
  430. <select class="theme-selector" oninput="activateTheme(this.value)">
  431. <option value="auto" selected>Auto</option>
  432. <option value="light">Light</option>
  433. <option value="dark">Dark</option>
  434. </select>
  435. </label> |</li>
  436. </ul>
  437. </div>
  438. <div class="footer">
  439. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  440. <br />
  441. This page is licensed under the Python Software Foundation License Version 2.
  442. <br />
  443. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  444. <br />
  445. See <a href="/license.html">History and License</a> for more information.<br />
  446. <br />
  447. The Python Software Foundation is a non-profit corporation.
  448. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  449. <br />
  450. <br />
  451. Last updated on Oct 02, 2023.
  452. <a href="/bugs.html">Found a bug</a>?
  453. <br />
  454. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  455. </div>
  456. </body>
  457. </html>