gcsupport.html 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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="Supporting Cyclic Garbage Collection" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/gcsupport.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="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. Ty..." />
  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="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. Ty..." />
  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>Supporting Cyclic Garbage Collection &#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="API and ABI Versioning" href="apiabiversion.html" />
  34. <link rel="prev" title="Type Objects" href="typeobj.html" />
  35. <link rel="canonical" href="https://docs.python.org/3/c-api/gcsupport.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. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#">Supporting Cyclic Garbage Collection</a><ul>
  86. <li><a class="reference internal" href="#controlling-the-garbage-collector-state">Controlling the Garbage Collector State</a></li>
  87. <li><a class="reference internal" href="#querying-garbage-collector-state">Querying Garbage Collector State</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="typeobj.html"
  95. title="previous chapter">Type Objects</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="apiabiversion.html"
  100. title="next chapter">API and ABI Versioning</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/gcsupport.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="apiabiversion.html" title="API and ABI Versioning"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="typeobj.html" title="Type Objects"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.0 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="objimpl.html" accesskey="U">Object Implementation Support</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href="">Supporting Cyclic Garbage Collection</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="supporting-cyclic-garbage-collection">
  170. <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>
  171. <p>Python’s support for detecting and collecting garbage which involves circular
  172. references requires support from object types which are “containers” for other
  173. objects which may also be containers. Types which do not store references to
  174. other objects, or which only store references to atomic types (such as numbers
  175. or strings), do not need to provide any explicit support for garbage
  176. collection.</p>
  177. <p>To create a container type, the <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_flags" title="PyTypeObject.tp_flags"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_flags</span></code></a> field of the type object must
  178. include the <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> and provide an implementation of the
  179. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> handler. If instances of the type are mutable, a
  180. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_clear</span></code></a> implementation must also be provided.</p>
  181. <dl class="simple">
  182. <dt><a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a></dt><dd><p>Objects with a type with this flag set must conform with the rules
  183. documented here. For convenience these objects will be referred to as
  184. container objects.</p>
  185. </dd>
  186. </dl>
  187. <p>Constructors for container types must conform to two rules:</p>
  188. <ol class="arabic simple">
  189. <li><p>The memory for the object must be allocated using <a class="reference internal" href="#c.PyObject_GC_New" title="PyObject_GC_New"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_GC_New</span></code></a>
  190. or <a class="reference internal" href="#c.PyObject_GC_NewVar" title="PyObject_GC_NewVar"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_GC_NewVar</span></code></a>.</p></li>
  191. <li><p>Once all the fields which may contain references to other containers are
  192. initialized, it must call <a class="reference internal" href="#c.PyObject_GC_Track" title="PyObject_GC_Track"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_Track()</span></code></a>.</p></li>
  193. </ol>
  194. <p>Similarly, the deallocator for the object must conform to a similar pair of
  195. rules:</p>
  196. <ol class="arabic">
  197. <li><p>Before fields which refer to other containers are invalidated,
  198. <a class="reference internal" href="#c.PyObject_GC_UnTrack" title="PyObject_GC_UnTrack"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_UnTrack()</span></code></a> must be called.</p></li>
  199. <li><p>The object’s memory must be deallocated using <a class="reference internal" href="#c.PyObject_GC_Del" title="PyObject_GC_Del"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_Del()</span></code></a>.</p>
  200. <div class="admonition warning">
  201. <p class="admonition-title">Warning</p>
  202. <p>If a type adds the Py_TPFLAGS_HAVE_GC, then it <em>must</em> implement at least
  203. a <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> handler or explicitly use one
  204. from its subclass or subclasses.</p>
  205. <p>When calling <a class="reference internal" href="type.html#c.PyType_Ready" title="PyType_Ready"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_Ready()</span></code></a> or some of the APIs that indirectly
  206. call it like <a class="reference internal" href="type.html#c.PyType_FromSpecWithBases" title="PyType_FromSpecWithBases"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_FromSpecWithBases()</span></code></a> or
  207. <a class="reference internal" href="type.html#c.PyType_FromSpec" title="PyType_FromSpec"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyType_FromSpec()</span></code></a> the interpreter will automatically populate the
  208. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_flags" title="PyTypeObject.tp_flags"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_flags</span></code></a>, <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a>
  209. and <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_clear</span></code></a> fields if the type inherits from a
  210. class that implements the garbage collector protocol and the child class
  211. does <em>not</em> include the <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag.</p>
  212. </div>
  213. </li>
  214. </ol>
  215. <dl class="c macro">
  216. <dt class="sig sig-object c" id="c.PyObject_GC_New">
  217. <span class="sig-name descname"><span class="n"><span class="pre">PyObject_GC_New</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">TYPE</span></span>, <span class="n"><span class="pre">typeobj</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GC_New" title="Permalink to this definition">¶</a><br /></dt>
  218. <dd><p>Analogous to <a class="reference internal" href="allocation.html#c.PyObject_New" title="PyObject_New"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_New</span></code></a> but for container objects with the
  219. <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag set.</p>
  220. </dd></dl>
  221. <dl class="c macro">
  222. <dt class="sig sig-object c" id="c.PyObject_GC_NewVar">
  223. <span class="sig-name descname"><span class="n"><span class="pre">PyObject_GC_NewVar</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">TYPE</span></span>, <span class="n"><span class="pre">typeobj</span></span>, <span class="n"><span class="pre">size</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GC_NewVar" title="Permalink to this definition">¶</a><br /></dt>
  224. <dd><p>Analogous to <a class="reference internal" href="allocation.html#c.PyObject_NewVar" title="PyObject_NewVar"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_NewVar</span></code></a> but for container objects with the
  225. <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_HAVE_GC" title="Py_TPFLAGS_HAVE_GC"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_HAVE_GC</span></code></a> flag set.</p>
  226. </dd></dl>
  227. <dl class="c function">
  228. <dt class="sig sig-object c" id="c.PyUnstable_Object_GC_NewWithExtraData">
  229. <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">PyUnstable_Object_GC_NewWithExtraData</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><span class="n"><span class="pre">PyTypeObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">type</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">extra_size</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnstable_Object_GC_NewWithExtraData" title="Permalink to this definition">¶</a><br /></dt>
  230. <dd><div class="unstable-c-api warning admonition">
  231. <em>This is <a class="reference internal" href="stable.html#unstable-c-api"><span class="std std-ref">Unstable API</span></a>. It may change without warning in minor releases.</em></div>
  232. <p>Analogous to <a class="reference internal" href="#c.PyObject_GC_New" title="PyObject_GC_New"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_GC_New</span></code></a> but allocates <em>extra_size</em>
  233. bytes at the end of the object (at offset
  234. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_basicsize" title="PyTypeObject.tp_basicsize"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_basicsize</span></code></a>).
  235. The allocated memory is initialized to zeros,
  236. except for the <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">Python</span> <span class="pre">object</span> <span class="pre">header</span></code></a>.</p>
  237. <p>The extra data will be deallocated with the object, but otherwise it is
  238. not managed by Python.</p>
  239. <div class="admonition warning">
  240. <p class="admonition-title">Warning</p>
  241. <p>The function is marked as unstable because the final mechanism
  242. for reserving extra data after an instance is not yet decided.
  243. For allocating a variable number of fields, prefer using
  244. <a class="reference internal" href="structures.html#c.PyVarObject" title="PyVarObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyVarObject</span></code></a> and <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_itemsize" title="PyTypeObject.tp_itemsize"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_itemsize</span></code></a>
  245. instead.</p>
  246. </div>
  247. <div class="versionadded">
  248. <p><span class="versionmodified added">New in version 3.12.</span></p>
  249. </div>
  250. </dd></dl>
  251. <dl class="c function">
  252. <dt class="sig sig-object c" id="c.PyObject_GC_Resize">
  253. <span class="n"><span class="pre">TYPE</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_GC_Resize</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">TYPE</span></span>, <a class="reference internal" href="structures.html#c.PyVarObject" title="PyVarObject"><span class="n"><span class="pre">PyVarObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">op</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">newsize</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GC_Resize" title="Permalink to this definition">¶</a><br /></dt>
  254. <dd><p>Resize an object allocated by <a class="reference internal" href="allocation.html#c.PyObject_NewVar" title="PyObject_NewVar"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_NewVar</span></code></a>. Returns the
  255. resized object or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> on failure. <em>op</em> must not be tracked by the collector yet.</p>
  256. </dd></dl>
  257. <dl class="c function">
  258. <dt class="sig sig-object c" id="c.PyObject_GC_Track">
  259. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_GC_Track</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">op</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GC_Track" title="Permalink to this definition">¶</a><br /></dt>
  260. <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>Adds the object <em>op</em> to the set of container objects tracked by the
  261. collector. The collector can run at unexpected times so objects must be
  262. valid while being tracked. This should be called once all the fields
  263. followed by the <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> handler become valid, usually near the
  264. end of the constructor.</p>
  265. </dd></dl>
  266. <dl class="c function">
  267. <dt class="sig sig-object c" id="c.PyObject_IS_GC">
  268. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_IS_GC</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">obj</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_IS_GC" title="Permalink to this definition">¶</a><br /></dt>
  269. <dd><p>Returns non-zero if the object implements the garbage collector protocol,
  270. otherwise returns 0.</p>
  271. <p>The object cannot be tracked by the garbage collector if this function returns 0.</p>
  272. </dd></dl>
  273. <dl class="c function">
  274. <dt class="sig sig-object c" id="c.PyObject_GC_IsTracked">
  275. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_GC_IsTracked</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">op</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GC_IsTracked" title="Permalink to this definition">¶</a><br /></dt>
  276. <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.9.</em><p>Returns 1 if the object type of <em>op</em> implements the GC protocol and <em>op</em> is being
  277. currently tracked by the garbage collector and 0 otherwise.</p>
  278. <p>This is analogous to the Python function <a class="reference internal" href="../library/gc.html#gc.is_tracked" title="gc.is_tracked"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.is_tracked()</span></code></a>.</p>
  279. <div class="versionadded">
  280. <p><span class="versionmodified added">New in version 3.9.</span></p>
  281. </div>
  282. </dd></dl>
  283. <dl class="c function">
  284. <dt class="sig sig-object c" id="c.PyObject_GC_IsFinalized">
  285. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_GC_IsFinalized</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">op</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GC_IsFinalized" title="Permalink to this definition">¶</a><br /></dt>
  286. <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.9.</em><p>Returns 1 if the object type of <em>op</em> implements the GC protocol and <em>op</em> has been
  287. already finalized by the garbage collector and 0 otherwise.</p>
  288. <p>This is analogous to the Python function <a class="reference internal" href="../library/gc.html#gc.is_finalized" title="gc.is_finalized"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.is_finalized()</span></code></a>.</p>
  289. <div class="versionadded">
  290. <p><span class="versionmodified added">New in version 3.9.</span></p>
  291. </div>
  292. </dd></dl>
  293. <dl class="c function">
  294. <dt class="sig sig-object c" id="c.PyObject_GC_Del">
  295. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_GC_Del</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">op</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GC_Del" title="Permalink to this definition">¶</a><br /></dt>
  296. <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>Releases memory allocated to an object using <a class="reference internal" href="#c.PyObject_GC_New" title="PyObject_GC_New"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_GC_New</span></code></a> or
  297. <a class="reference internal" href="#c.PyObject_GC_NewVar" title="PyObject_GC_NewVar"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyObject_GC_NewVar</span></code></a>.</p>
  298. </dd></dl>
  299. <dl class="c function">
  300. <dt class="sig sig-object c" id="c.PyObject_GC_UnTrack">
  301. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_GC_UnTrack</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">op</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_GC_UnTrack" title="Permalink to this definition">¶</a><br /></dt>
  302. <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>Remove the object <em>op</em> from the set of container objects tracked by the
  303. collector. Note that <a class="reference internal" href="#c.PyObject_GC_Track" title="PyObject_GC_Track"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GC_Track()</span></code></a> can be called again on
  304. this object to add it back to the set of tracked objects. The deallocator
  305. (<a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_dealloc" title="PyTypeObject.tp_dealloc"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_dealloc</span></code></a> handler) should call this for the object before any of
  306. the fields used by the <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> handler become invalid.</p>
  307. </dd></dl>
  308. <div class="versionchanged">
  309. <p><span class="versionmodified changed">Changed in version 3.8: </span>The <code class="xref c c-func docutils literal notranslate"><span class="pre">_PyObject_GC_TRACK()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">_PyObject_GC_UNTRACK()</span></code> macros
  310. have been removed from the public C API.</p>
  311. </div>
  312. <p>The <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> handler accepts a function parameter of this type:</p>
  313. <dl class="c type">
  314. <dt class="sig sig-object c" id="c.visitproc">
  315. <span class="k"><span class="pre">typedef</span></span><span class="w"> </span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">visitproc</span></span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></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">object</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">arg</span></span><span class="p"><span class="pre">)</span></span><a class="headerlink" href="#c.visitproc" title="Permalink to this definition">¶</a><br /></dt>
  316. <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>Type of the visitor function passed to the <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> handler.
  317. The function should be called with an object to traverse as <em>object</em> and
  318. the third parameter to the <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> handler as <em>arg</em>. The
  319. Python core uses several visitor functions to implement cyclic garbage
  320. detection; it’s not expected that users will need to write their own
  321. visitor functions.</p>
  322. </dd></dl>
  323. <p>The <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> handler must have the following type:</p>
  324. <dl class="c type">
  325. <dt class="sig sig-object c" id="c.traverseproc">
  326. <span class="k"><span class="pre">typedef</span></span><span class="w"> </span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">traverseproc</span></span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></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">self</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference internal" href="#c.visitproc" title="visitproc"><span class="n"><span class="pre">visitproc</span></span></a><span class="w"> </span><span class="n"><span class="pre">visit</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">arg</span></span><span class="p"><span class="pre">)</span></span><a class="headerlink" href="#c.traverseproc" title="Permalink to this definition">¶</a><br /></dt>
  327. <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>Traversal function for a container object. Implementations must call the
  328. <em>visit</em> function for each object directly contained by <em>self</em>, with the
  329. parameters to <em>visit</em> being the contained object and the <em>arg</em> value passed
  330. to the handler. The <em>visit</em> function must not be called with a <code class="docutils literal notranslate"><span class="pre">NULL</span></code>
  331. object argument. If <em>visit</em> returns a non-zero value that value should be
  332. returned immediately.</p>
  333. </dd></dl>
  334. <p>To simplify writing <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> handlers, a <a class="reference internal" href="#c.Py_VISIT" title="Py_VISIT"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_VISIT()</span></code></a> macro is
  335. provided. In order to use this macro, the <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> implementation
  336. must name its arguments exactly <em>visit</em> and <em>arg</em>:</p>
  337. <dl class="c function">
  338. <dt class="sig sig-object c" id="c.Py_VISIT">
  339. <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_VISIT</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_VISIT" title="Permalink to this definition">¶</a><br /></dt>
  340. <dd><p>If <em>o</em> is not <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, call the <em>visit</em> callback, with arguments <em>o</em>
  341. and <em>arg</em>. If <em>visit</em> returns a non-zero value, then return it.
  342. Using this macro, <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_traverse" title="PyTypeObject.tp_traverse"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_traverse</span></code></a> handlers
  343. look like:</p>
  344. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="kt">int</span>
  345. <span class="nf">my_traverse</span><span class="p">(</span><span class="n">Noddy</span><span class="w"> </span><span class="o">*</span><span class="n">self</span><span class="p">,</span><span class="w"> </span><span class="n">visitproc</span><span class="w"> </span><span class="n">visit</span><span class="p">,</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">arg</span><span class="p">)</span>
  346. <span class="p">{</span>
  347. <span class="w"> </span><span class="n">Py_VISIT</span><span class="p">(</span><span class="n">self</span><span class="o">-&gt;</span><span class="n">foo</span><span class="p">);</span>
  348. <span class="w"> </span><span class="n">Py_VISIT</span><span class="p">(</span><span class="n">self</span><span class="o">-&gt;</span><span class="n">bar</span><span class="p">);</span>
  349. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
  350. <span class="p">}</span>
  351. </pre></div>
  352. </div>
  353. </dd></dl>
  354. <p>The <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_clear" title="PyTypeObject.tp_clear"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_clear</span></code></a> handler must be of the <a class="reference internal" href="#c.inquiry" title="inquiry"><code class="xref c c-type docutils literal notranslate"><span class="pre">inquiry</span></code></a> type, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code>
  355. if the object is immutable.</p>
  356. <dl class="c type">
  357. <dt class="sig sig-object c" id="c.inquiry">
  358. <span class="k"><span class="pre">typedef</span></span><span class="w"> </span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">inquiry</span></span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></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">self</span></span><span class="p"><span class="pre">)</span></span><a class="headerlink" href="#c.inquiry" title="Permalink to this definition">¶</a><br /></dt>
  359. <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>Drop references that may have created reference cycles. Immutable objects
  360. do not have to define this method since they can never directly create
  361. reference cycles. Note that the object must still be valid after calling
  362. this method (don’t just call <a class="reference internal" href="refcounting.html#c.Py_DECREF" title="Py_DECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DECREF()</span></code></a> on a reference). The
  363. collector will call this method if it detects that this object is involved
  364. in a reference cycle.</p>
  365. </dd></dl>
  366. <section id="controlling-the-garbage-collector-state">
  367. <h2>Controlling the Garbage Collector State<a class="headerlink" href="#controlling-the-garbage-collector-state" title="Permalink to this headline">¶</a></h2>
  368. <p>The C-API provides the following functions for controlling
  369. garbage collection runs.</p>
  370. <dl class="c function">
  371. <dt class="sig sig-object c" id="c.PyGC_Collect">
  372. <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">PyGC_Collect</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyGC_Collect" title="Permalink to this definition">¶</a><br /></dt>
  373. <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>Perform a full garbage collection, if the garbage collector is enabled.
  374. (Note that <a class="reference internal" href="../library/gc.html#gc.collect" title="gc.collect"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.collect()</span></code></a> runs it unconditionally.)</p>
  375. <p>Returns the number of collected + unreachable objects which cannot
  376. be collected.
  377. If the garbage collector is disabled or already collecting,
  378. returns <code class="docutils literal notranslate"><span class="pre">0</span></code> immediately.
  379. Errors during garbage collection are passed to <a class="reference internal" href="../library/sys.html#sys.unraisablehook" title="sys.unraisablehook"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.unraisablehook</span></code></a>.
  380. This function does not raise exceptions.</p>
  381. </dd></dl>
  382. <dl class="c function">
  383. <dt class="sig sig-object c" id="c.PyGC_Enable">
  384. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyGC_Enable</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyGC_Enable" title="Permalink to this definition">¶</a><br /></dt>
  385. <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>Enable the garbage collector: similar to <a class="reference internal" href="../library/gc.html#gc.enable" title="gc.enable"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.enable()</span></code></a>.
  386. Returns the previous state, 0 for disabled and 1 for enabled.</p>
  387. <div class="versionadded">
  388. <p><span class="versionmodified added">New in version 3.10.</span></p>
  389. </div>
  390. </dd></dl>
  391. <dl class="c function">
  392. <dt class="sig sig-object c" id="c.PyGC_Disable">
  393. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyGC_Disable</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyGC_Disable" title="Permalink to this definition">¶</a><br /></dt>
  394. <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>Disable the garbage collector: similar to <a class="reference internal" href="../library/gc.html#gc.disable" title="gc.disable"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.disable()</span></code></a>.
  395. Returns the previous state, 0 for disabled and 1 for enabled.</p>
  396. <div class="versionadded">
  397. <p><span class="versionmodified added">New in version 3.10.</span></p>
  398. </div>
  399. </dd></dl>
  400. <dl class="c function">
  401. <dt class="sig sig-object c" id="c.PyGC_IsEnabled">
  402. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyGC_IsEnabled</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyGC_IsEnabled" title="Permalink to this definition">¶</a><br /></dt>
  403. <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>Query the state of the garbage collector: similar to <a class="reference internal" href="../library/gc.html#gc.isenabled" title="gc.isenabled"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.isenabled()</span></code></a>.
  404. Returns the current state, 0 for disabled and 1 for enabled.</p>
  405. <div class="versionadded">
  406. <p><span class="versionmodified added">New in version 3.10.</span></p>
  407. </div>
  408. </dd></dl>
  409. </section>
  410. <section id="querying-garbage-collector-state">
  411. <h2>Querying Garbage Collector State<a class="headerlink" href="#querying-garbage-collector-state" title="Permalink to this headline">¶</a></h2>
  412. <p>The C-API provides the following interface for querying information about
  413. the garbage collector.</p>
  414. <dl class="c function">
  415. <dt class="sig sig-object c" id="c.PyUnstable_GC_VisitObjects">
  416. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyUnstable_GC_VisitObjects</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.gcvisitobjects_t" title="gcvisitobjects_t"><span class="n"><span class="pre">gcvisitobjects_t</span></span></a><span class="w"> </span><span class="n"><span class="pre">callback</span></span>, <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">arg</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyUnstable_GC_VisitObjects" title="Permalink to this definition">¶</a><br /></dt>
  417. <dd><div class="unstable-c-api warning admonition">
  418. <em>This is <a class="reference internal" href="stable.html#unstable-c-api"><span class="std std-ref">Unstable API</span></a>. It may change without warning in minor releases.</em></div>
  419. <p>Run supplied <em>callback</em> on all live GC-capable objects. <em>arg</em> is passed through to
  420. all invocations of <em>callback</em>.</p>
  421. <div class="admonition warning">
  422. <p class="admonition-title">Warning</p>
  423. <p>If new objects are (de)allocated by the callback it is undefined if they
  424. will be visited.</p>
  425. <p>Garbage collection is disabled during operation. Explicitly running a collection
  426. in the callback may lead to undefined behaviour e.g. visiting the same objects
  427. multiple times or not at all.</p>
  428. </div>
  429. <div class="versionadded">
  430. <p><span class="versionmodified added">New in version 3.12.</span></p>
  431. </div>
  432. </dd></dl>
  433. <dl class="c type">
  434. <dt class="sig sig-object c" id="c.gcvisitobjects_t">
  435. <span class="k"><span class="pre">typedef</span></span><span class="w"> </span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">gcvisitobjects_t</span></span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></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">object</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">arg</span></span><span class="p"><span class="pre">)</span></span><a class="headerlink" href="#c.gcvisitobjects_t" title="Permalink to this definition">¶</a><br /></dt>
  436. <dd><p>Type of the visitor function to be passed to <a class="reference internal" href="#c.PyUnstable_GC_VisitObjects" title="PyUnstable_GC_VisitObjects"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyUnstable_GC_VisitObjects()</span></code></a>.
  437. <em>arg</em> is the same as the <em>arg</em> passed to <code class="docutils literal notranslate"><span class="pre">PyUnstable_GC_VisitObjects</span></code>.
  438. Return <code class="docutils literal notranslate"><span class="pre">0</span></code> to continue iteration, return <code class="docutils literal notranslate"><span class="pre">1</span></code> to stop iteration. Other return
  439. values are reserved for now so behavior on returning anything else is undefined.</p>
  440. <div class="versionadded">
  441. <p><span class="versionmodified added">New in version 3.12.</span></p>
  442. </div>
  443. </dd></dl>
  444. </section>
  445. </section>
  446. <div class="clearer"></div>
  447. </div>
  448. </div>
  449. </div>
  450. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  451. <div class="sphinxsidebarwrapper">
  452. <div>
  453. <h3><a href="../contents.html">Table of Contents</a></h3>
  454. <ul>
  455. <li><a class="reference internal" href="#">Supporting Cyclic Garbage Collection</a><ul>
  456. <li><a class="reference internal" href="#controlling-the-garbage-collector-state">Controlling the Garbage Collector State</a></li>
  457. <li><a class="reference internal" href="#querying-garbage-collector-state">Querying Garbage Collector State</a></li>
  458. </ul>
  459. </li>
  460. </ul>
  461. </div>
  462. <div>
  463. <h4>Previous topic</h4>
  464. <p class="topless"><a href="typeobj.html"
  465. title="previous chapter">Type Objects</a></p>
  466. </div>
  467. <div>
  468. <h4>Next topic</h4>
  469. <p class="topless"><a href="apiabiversion.html"
  470. title="next chapter">API and ABI Versioning</a></p>
  471. </div>
  472. <div role="note" aria-label="source link">
  473. <h3>This Page</h3>
  474. <ul class="this-page-menu">
  475. <li><a href="../bugs.html">Report a Bug</a></li>
  476. <li>
  477. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/gcsupport.rst"
  478. rel="nofollow">Show Source
  479. </a>
  480. </li>
  481. </ul>
  482. </div>
  483. </div>
  484. </div>
  485. <div class="clearer"></div>
  486. </div>
  487. <div class="related" role="navigation" aria-label="related navigation">
  488. <h3>Navigation</h3>
  489. <ul>
  490. <li class="right" style="margin-right: 10px">
  491. <a href="../genindex.html" title="General Index"
  492. >index</a></li>
  493. <li class="right" >
  494. <a href="../py-modindex.html" title="Python Module Index"
  495. >modules</a> |</li>
  496. <li class="right" >
  497. <a href="apiabiversion.html" title="API and ABI Versioning"
  498. >next</a> |</li>
  499. <li class="right" >
  500. <a href="typeobj.html" title="Type Objects"
  501. >previous</a> |</li>
  502. <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  503. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  504. <li class="switchers">
  505. <div class="language_switcher_placeholder"></div>
  506. <div class="version_switcher_placeholder"></div>
  507. </li>
  508. <li>
  509. </li>
  510. <li id="cpython-language-and-version">
  511. <a href="../index.html">3.12.0 Documentation</a> &#187;
  512. </li>
  513. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  514. <li class="nav-item nav-item-2"><a href="objimpl.html" >Object Implementation Support</a> &#187;</li>
  515. <li class="nav-item nav-item-this"><a href="">Supporting Cyclic Garbage Collection</a></li>
  516. <li class="right">
  517. <div class="inline-search" role="search">
  518. <form class="inline-search" action="../search.html" method="get">
  519. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  520. <input type="submit" value="Go" />
  521. </form>
  522. </div>
  523. |
  524. </li>
  525. <li class="right">
  526. <label class="theme-selector-label">
  527. Theme
  528. <select class="theme-selector" oninput="activateTheme(this.value)">
  529. <option value="auto" selected>Auto</option>
  530. <option value="light">Light</option>
  531. <option value="dark">Dark</option>
  532. </select>
  533. </label> |</li>
  534. </ul>
  535. </div>
  536. <div class="footer">
  537. &copy; <a href="../copyright.html">Copyright</a> 2001-2023, Python Software Foundation.
  538. <br />
  539. This page is licensed under the Python Software Foundation License Version 2.
  540. <br />
  541. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  542. <br />
  543. See <a href="/license.html">History and License</a> for more information.<br />
  544. <br />
  545. The Python Software Foundation is a non-profit corporation.
  546. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  547. <br />
  548. <br />
  549. Last updated on Oct 02, 2023.
  550. <a href="/bugs.html">Found a bug</a>?
  551. <br />
  552. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.5.0.
  553. </div>
  554. </body>
  555. </html>