gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1088 行
103KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="Introduction" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/intro.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="The Application Programmer’s Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is gener..." />
  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 Application Programmer’s Interface to Python gives C and C++ programmers access to the Python interpreter at a variety of levels. The API is equally usable from C++, but for brevity it is gener..." />
  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>Introduction &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="C API Stability" href="stable.html" />
  33. <link rel="prev" title="Python/C API Reference Manual" href="index.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/c-api/intro.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.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="Python 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="#">Introduction</a><ul>
  86. <li><a class="reference internal" href="#coding-standards">Coding standards</a></li>
  87. <li><a class="reference internal" href="#include-files">Include Files</a></li>
  88. <li><a class="reference internal" href="#useful-macros">Useful macros</a></li>
  89. <li><a class="reference internal" href="#objects-types-and-reference-counts">Objects, Types and Reference Counts</a><ul>
  90. <li><a class="reference internal" href="#reference-counts">Reference Counts</a><ul>
  91. <li><a class="reference internal" href="#reference-count-details">Reference Count Details</a></li>
  92. </ul>
  93. </li>
  94. <li><a class="reference internal" href="#types">Types</a></li>
  95. </ul>
  96. </li>
  97. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  98. <li><a class="reference internal" href="#embedding-python">Embedding Python</a></li>
  99. <li><a class="reference internal" href="#debugging-builds">Debugging Builds</a></li>
  100. </ul>
  101. </li>
  102. </ul>
  103. </div>
  104. <div>
  105. <h4>Previous topic</h4>
  106. <p class="topless"><a href="index.html"
  107. title="previous chapter">Python/C API Reference Manual</a></p>
  108. </div>
  109. <div>
  110. <h4>Next topic</h4>
  111. <p class="topless"><a href="stable.html"
  112. title="next chapter">C API Stability</a></p>
  113. </div>
  114. <div role="note" aria-label="source link">
  115. <h3>This Page</h3>
  116. <ul class="this-page-menu">
  117. <li><a href="../bugs.html">Report a Bug</a></li>
  118. <li>
  119. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/intro.rst"
  120. rel="nofollow">Show Source
  121. </a>
  122. </li>
  123. </ul>
  124. </div>
  125. </nav>
  126. </div>
  127. </div>
  128. <div class="related" role="navigation" aria-label="related navigation">
  129. <h3>Navigation</h3>
  130. <ul>
  131. <li class="right" style="margin-right: 10px">
  132. <a href="../genindex.html" title="General Index"
  133. accesskey="I">index</a></li>
  134. <li class="right" >
  135. <a href="../py-modindex.html" title="Python Module Index"
  136. >modules</a> |</li>
  137. <li class="right" >
  138. <a href="stable.html" title="C API Stability"
  139. accesskey="N">next</a> |</li>
  140. <li class="right" >
  141. <a href="index.html" title="Python/C API Reference Manual"
  142. accesskey="P">previous</a> |</li>
  143. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  144. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  145. <li class="switchers">
  146. <div class="language_switcher_placeholder"></div>
  147. <div class="version_switcher_placeholder"></div>
  148. </li>
  149. <li>
  150. </li>
  151. <li id="cpython-language-and-version">
  152. <a href="../index.html">3.12.3 Documentation</a> &#187;
  153. </li>
  154. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python/C API Reference Manual</a> &#187;</li>
  155. <li class="nav-item nav-item-this"><a href="">Introduction</a></li>
  156. <li class="right">
  157. <div class="inline-search" role="search">
  158. <form class="inline-search" action="../search.html" method="get">
  159. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  160. <input type="submit" value="Go" />
  161. </form>
  162. </div>
  163. |
  164. </li>
  165. <li class="right">
  166. <label class="theme-selector-label">
  167. Theme
  168. <select class="theme-selector" oninput="activateTheme(this.value)">
  169. <option value="auto" selected>Auto</option>
  170. <option value="light">Light</option>
  171. <option value="dark">Dark</option>
  172. </select>
  173. </label> |</li>
  174. </ul>
  175. </div>
  176. <div class="document">
  177. <div class="documentwrapper">
  178. <div class="bodywrapper">
  179. <div class="body" role="main">
  180. <section id="introduction">
  181. <span id="api-intro"></span><h1>Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h1>
  182. <p>The Application Programmer’s Interface to Python gives C and C++ programmers
  183. access to the Python interpreter at a variety of levels. The API is equally
  184. usable from C++, but for brevity it is generally referred to as the Python/C
  185. API. There are two fundamentally different reasons for using the Python/C API.
  186. The first reason is to write <em>extension modules</em> for specific purposes; these
  187. are C modules that extend the Python interpreter. This is probably the most
  188. common use. The second reason is to use Python as a component in a larger
  189. application; this technique is generally referred to as <em class="dfn">embedding</em> Python
  190. in an application.</p>
  191. <p>Writing an extension module is a relatively well-understood process, where a
  192. “cookbook” approach works well. There are several tools that automate the
  193. process to some extent. While people have embedded Python in other
  194. applications since its early existence, the process of embedding Python is
  195. less straightforward than writing an extension.</p>
  196. <p>Many API functions are useful independent of whether you’re embedding or
  197. extending Python; moreover, most applications that embed Python will need to
  198. provide a custom extension as well, so it’s probably a good idea to become
  199. familiar with writing an extension before attempting to embed Python in a real
  200. application.</p>
  201. <section id="coding-standards">
  202. <h2>Coding standards<a class="headerlink" href="#coding-standards" title="Link to this heading">¶</a></h2>
  203. <p>If you’re writing C code for inclusion in CPython, you <strong>must</strong> follow the
  204. guidelines and standards defined in <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0007/"><strong>PEP 7</strong></a>. These guidelines apply
  205. regardless of the version of Python you are contributing to. Following these
  206. conventions is not necessary for your own third party extension modules,
  207. unless you eventually expect to contribute them to Python.</p>
  208. </section>
  209. <section id="include-files">
  210. <span id="api-includes"></span><h2>Include Files<a class="headerlink" href="#include-files" title="Link to this heading">¶</a></h2>
  211. <p>All function, type and macro definitions needed to use the Python/C API are
  212. included in your code by the following line:</p>
  213. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cp">#define PY_SSIZE_T_CLEAN</span>
  214. <span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;Python.h&gt;</span>
  215. </pre></div>
  216. </div>
  217. <p>This implies inclusion of the following standard headers: <code class="docutils literal notranslate"><span class="pre">&lt;stdio.h&gt;</span></code>,
  218. <code class="docutils literal notranslate"><span class="pre">&lt;string.h&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;errno.h&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;limits.h&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;assert.h&gt;</span></code> and <code class="docutils literal notranslate"><span class="pre">&lt;stdlib.h&gt;</span></code>
  219. (if available).</p>
  220. <div class="admonition note">
  221. <p class="admonition-title">Note</p>
  222. <p>Since Python may define some pre-processor definitions which affect the standard
  223. headers on some systems, you <em>must</em> include <code class="file docutils literal notranslate"><span class="pre">Python.h</span></code> before any standard
  224. headers are included.</p>
  225. <p>It is recommended to always define <code class="docutils literal notranslate"><span class="pre">PY_SSIZE_T_CLEAN</span></code> before including
  226. <code class="docutils literal notranslate"><span class="pre">Python.h</span></code>. See <a class="reference internal" href="arg.html#arg-parsing"><span class="std std-ref">Parsing arguments and building values</span></a> for a description of this macro.</p>
  227. </div>
  228. <p>All user visible names defined by Python.h (except those defined by the included
  229. standard headers) have one of the prefixes <code class="docutils literal notranslate"><span class="pre">Py</span></code> or <code class="docutils literal notranslate"><span class="pre">_Py</span></code>. Names beginning
  230. with <code class="docutils literal notranslate"><span class="pre">_Py</span></code> are for internal use by the Python implementation and should not be
  231. used by extension writers. Structure member names do not have a reserved prefix.</p>
  232. <div class="admonition note">
  233. <p class="admonition-title">Note</p>
  234. <p>User code should never define names that begin with <code class="docutils literal notranslate"><span class="pre">Py</span></code> or <code class="docutils literal notranslate"><span class="pre">_Py</span></code>. This
  235. confuses the reader, and jeopardizes the portability of the user code to
  236. future Python versions, which may define additional names beginning with one
  237. of these prefixes.</p>
  238. </div>
  239. <p>The header files are typically installed with Python. On Unix, these are
  240. located in the directories <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">/include/pythonversion/</span></code> and
  241. <code class="file docutils literal notranslate"><em><span class="pre">exec_prefix</span></em><span class="pre">/include/pythonversion/</span></code>, where <a class="reference internal" href="../using/configure.html#cmdoption-prefix"><code class="xref std std-option docutils literal notranslate"><span class="pre">prefix</span></code></a> and
  242. <a class="reference internal" href="../using/configure.html#cmdoption-exec-prefix"><code class="xref std std-option docutils literal notranslate"><span class="pre">exec_prefix</span></code></a> are defined by the corresponding parameters to Python’s
  243. <strong class="program">configure</strong> script and <em>version</em> is
  244. <code class="docutils literal notranslate"><span class="pre">'%d.%d'</span> <span class="pre">%</span> <span class="pre">sys.version_info[:2]</span></code>. On Windows, the headers are installed
  245. in <code class="file docutils literal notranslate"><em><span class="pre">prefix</span></em><span class="pre">/include</span></code>, where <code class="docutils literal notranslate"><span class="pre">prefix</span></code> is the installation
  246. directory specified to the installer.</p>
  247. <p>To include the headers, place both directories (if different) on your compiler’s
  248. search path for includes. Do <em>not</em> place the parent directories on the search
  249. path and then use <code class="docutils literal notranslate"><span class="pre">#include</span> <span class="pre">&lt;pythonX.Y/Python.h&gt;</span></code>; this will break on
  250. multi-platform builds since the platform independent headers under
  251. <a class="reference internal" href="../using/configure.html#cmdoption-prefix"><code class="xref std std-option docutils literal notranslate"><span class="pre">prefix</span></code></a> include the platform specific headers from
  252. <a class="reference internal" href="../using/configure.html#cmdoption-exec-prefix"><code class="xref std std-option docutils literal notranslate"><span class="pre">exec_prefix</span></code></a>.</p>
  253. <p>C++ users should note that although the API is defined entirely using C, the
  254. header files properly declare the entry points to be <code class="docutils literal notranslate"><span class="pre">extern</span> <span class="pre">&quot;C&quot;</span></code>. As a result,
  255. there is no need to do anything special to use the API from C++.</p>
  256. </section>
  257. <section id="useful-macros">
  258. <h2>Useful macros<a class="headerlink" href="#useful-macros" title="Link to this heading">¶</a></h2>
  259. <p>Several useful macros are defined in the Python header files. Many are
  260. defined closer to where they are useful (e.g. <a class="reference internal" href="none.html#c.Py_RETURN_NONE" title="Py_RETURN_NONE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_RETURN_NONE</span></code></a>).
  261. Others of a more general utility are defined here. This is not necessarily a
  262. complete listing.</p>
  263. <dl class="c macro">
  264. <dt class="sig sig-object c" id="c.PyMODINIT_FUNC">
  265. <span class="sig-name descname"><span class="n"><span class="pre">PyMODINIT_FUNC</span></span></span><a class="headerlink" href="#c.PyMODINIT_FUNC" title="Link to this definition">¶</a><br /></dt>
  266. <dd><p>Declare an extension module <code class="docutils literal notranslate"><span class="pre">PyInit</span></code> initialization function. The function
  267. return type is <span class="c-expr sig sig-inline c"><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>. The macro declares any special linkage
  268. declarations required by the platform, and for C++ declares the function as
  269. <code class="docutils literal notranslate"><span class="pre">extern</span> <span class="pre">&quot;C&quot;</span></code>.</p>
  270. <p>The initialization function must be named <code class="samp docutils literal notranslate"><span class="pre">PyInit_</span><em><span class="pre">name</span></em></code>, where
  271. <em>name</em> is the name of the module, and should be the only non-<code class="docutils literal notranslate"><span class="pre">static</span></code>
  272. item defined in the module file. Example:</p>
  273. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">PyModuleDef</span><span class="w"> </span><span class="n">spam_module</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
  274. <span class="w"> </span><span class="n">PyModuleDef_HEAD_INIT</span><span class="p">,</span>
  275. <span class="w"> </span><span class="p">.</span><span class="n">m_name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;spam&quot;</span><span class="p">,</span>
  276. <span class="w"> </span><span class="p">...</span>
  277. <span class="p">};</span>
  278. <span class="n">PyMODINIT_FUNC</span>
  279. <span class="nf">PyInit_spam</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
  280. <span class="p">{</span>
  281. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">PyModule_Create</span><span class="p">(</span><span class="o">&amp;</span><span class="n">spam_module</span><span class="p">);</span>
  282. <span class="p">}</span>
  283. </pre></div>
  284. </div>
  285. </dd></dl>
  286. <dl class="c macro">
  287. <dt class="sig sig-object c" id="c.Py_ABS">
  288. <span class="sig-name descname"><span class="n"><span class="pre">Py_ABS</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">x</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_ABS" title="Link to this definition">¶</a><br /></dt>
  289. <dd><p>Return the absolute value of <code class="docutils literal notranslate"><span class="pre">x</span></code>.</p>
  290. <div class="versionadded">
  291. <p><span class="versionmodified added">New in version 3.3.</span></p>
  292. </div>
  293. </dd></dl>
  294. <dl class="c macro">
  295. <dt class="sig sig-object c" id="c.Py_ALWAYS_INLINE">
  296. <span class="sig-name descname"><span class="n"><span class="pre">Py_ALWAYS_INLINE</span></span></span><a class="headerlink" href="#c.Py_ALWAYS_INLINE" title="Link to this definition">¶</a><br /></dt>
  297. <dd><p>Ask the compiler to always inline a static inline function. The compiler can
  298. ignore it and decides to not inline the function.</p>
  299. <p>It can be used to inline performance critical static inline functions when
  300. building Python in debug mode with function inlining disabled. For example,
  301. MSC disables function inlining when building in debug mode.</p>
  302. <p>Marking blindly a static inline function with Py_ALWAYS_INLINE can result in
  303. worse performances (due to increased code size for example). The compiler is
  304. usually smarter than the developer for the cost/benefit analysis.</p>
  305. <p>If Python is <a class="reference internal" href="../using/configure.html#debug-build"><span class="std std-ref">built in debug mode</span></a> (if the <a class="reference internal" href="#c.Py_DEBUG" title="Py_DEBUG"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_DEBUG</span></code></a>
  306. macro is defined), the <a class="reference internal" href="#c.Py_ALWAYS_INLINE" title="Py_ALWAYS_INLINE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_ALWAYS_INLINE</span></code></a> macro does nothing.</p>
  307. <p>It must be specified before the function return type. Usage:</p>
  308. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="kr">inline</span><span class="w"> </span><span class="n">Py_ALWAYS_INLINE</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">random</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
  309. </pre></div>
  310. </div>
  311. <div class="versionadded">
  312. <p><span class="versionmodified added">New in version 3.11.</span></p>
  313. </div>
  314. </dd></dl>
  315. <dl class="c macro">
  316. <dt class="sig sig-object c" id="c.Py_CHARMASK">
  317. <span class="sig-name descname"><span class="n"><span class="pre">Py_CHARMASK</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">c</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_CHARMASK" title="Link to this definition">¶</a><br /></dt>
  318. <dd><p>Argument must be a character or an integer in the range [-128, 127] or [0,
  319. 255]. This macro returns <code class="docutils literal notranslate"><span class="pre">c</span></code> cast to an <code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">char</span></code>.</p>
  320. </dd></dl>
  321. <dl class="c macro">
  322. <dt class="sig sig-object c" id="c.Py_DEPRECATED">
  323. <span class="sig-name descname"><span class="n"><span class="pre">Py_DEPRECATED</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">version</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_DEPRECATED" title="Link to this definition">¶</a><br /></dt>
  324. <dd><p>Use this for deprecated declarations. The macro must be placed before the
  325. symbol name.</p>
  326. <p>Example:</p>
  327. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_DEPRECATED</span><span class="p">(</span><span class="mf">3.8</span><span class="p">)</span><span class="w"> </span><span class="n">PyAPI_FUNC</span><span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="n">Py_OldFunction</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>
  328. </pre></div>
  329. </div>
  330. <div class="versionchanged">
  331. <p><span class="versionmodified changed">Changed in version 3.8: </span>MSVC support was added.</p>
  332. </div>
  333. </dd></dl>
  334. <dl class="c macro">
  335. <dt class="sig sig-object c" id="c.Py_GETENV">
  336. <span class="sig-name descname"><span class="n"><span class="pre">Py_GETENV</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">s</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GETENV" title="Link to this definition">¶</a><br /></dt>
  337. <dd><p>Like <code class="docutils literal notranslate"><span class="pre">getenv(s)</span></code>, but returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if <a class="reference internal" href="../using/cmdline.html#cmdoption-E"><code class="xref std std-option docutils literal notranslate"><span class="pre">-E</span></code></a> was passed on the
  338. command line (see <a class="reference internal" href="init_config.html#c.PyConfig.use_environment" title="PyConfig.use_environment"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.use_environment</span></code></a>).</p>
  339. </dd></dl>
  340. <dl class="c macro">
  341. <dt class="sig sig-object c" id="c.Py_MAX">
  342. <span class="sig-name descname"><span class="n"><span class="pre">Py_MAX</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">x</span></span>, <span class="n"><span class="pre">y</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_MAX" title="Link to this definition">¶</a><br /></dt>
  343. <dd><p>Return the maximum value between <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code>.</p>
  344. <div class="versionadded">
  345. <p><span class="versionmodified added">New in version 3.3.</span></p>
  346. </div>
  347. </dd></dl>
  348. <dl class="c macro">
  349. <dt class="sig sig-object c" id="c.Py_MEMBER_SIZE">
  350. <span class="sig-name descname"><span class="n"><span class="pre">Py_MEMBER_SIZE</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">type</span></span>, <span class="n"><span class="pre">member</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_MEMBER_SIZE" title="Link to this definition">¶</a><br /></dt>
  351. <dd><p>Return the size of a structure (<code class="docutils literal notranslate"><span class="pre">type</span></code>) <code class="docutils literal notranslate"><span class="pre">member</span></code> in bytes.</p>
  352. <div class="versionadded">
  353. <p><span class="versionmodified added">New in version 3.6.</span></p>
  354. </div>
  355. </dd></dl>
  356. <dl class="c macro">
  357. <dt class="sig sig-object c" id="c.Py_MIN">
  358. <span class="sig-name descname"><span class="n"><span class="pre">Py_MIN</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">x</span></span>, <span class="n"><span class="pre">y</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_MIN" title="Link to this definition">¶</a><br /></dt>
  359. <dd><p>Return the minimum value between <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code>.</p>
  360. <div class="versionadded">
  361. <p><span class="versionmodified added">New in version 3.3.</span></p>
  362. </div>
  363. </dd></dl>
  364. <dl class="c macro">
  365. <dt class="sig sig-object c" id="c.Py_NO_INLINE">
  366. <span class="sig-name descname"><span class="n"><span class="pre">Py_NO_INLINE</span></span></span><a class="headerlink" href="#c.Py_NO_INLINE" title="Link to this definition">¶</a><br /></dt>
  367. <dd><p>Disable inlining on a function. For example, it reduces the C stack
  368. consumption: useful on LTO+PGO builds which heavily inline code (see
  369. <a class="reference external" href="https://bugs.python.org/issue?&#64;action=redirect&amp;bpo=33720">bpo-33720</a>).</p>
  370. <p>Usage:</p>
  371. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_NO_INLINE</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">random</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="p">}</span>
  372. </pre></div>
  373. </div>
  374. <div class="versionadded">
  375. <p><span class="versionmodified added">New in version 3.11.</span></p>
  376. </div>
  377. </dd></dl>
  378. <dl class="c macro">
  379. <dt class="sig sig-object c" id="c.Py_STRINGIFY">
  380. <span class="sig-name descname"><span class="n"><span class="pre">Py_STRINGIFY</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">x</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_STRINGIFY" title="Link to this definition">¶</a><br /></dt>
  381. <dd><p>Convert <code class="docutils literal notranslate"><span class="pre">x</span></code> to a C string. E.g. <code class="docutils literal notranslate"><span class="pre">Py_STRINGIFY(123)</span></code> returns
  382. <code class="docutils literal notranslate"><span class="pre">&quot;123&quot;</span></code>.</p>
  383. <div class="versionadded">
  384. <p><span class="versionmodified added">New in version 3.4.</span></p>
  385. </div>
  386. </dd></dl>
  387. <dl class="c macro">
  388. <dt class="sig sig-object c" id="c.Py_UNREACHABLE">
  389. <span class="sig-name descname"><span class="n"><span class="pre">Py_UNREACHABLE</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNREACHABLE" title="Link to this definition">¶</a><br /></dt>
  390. <dd><p>Use this when you have a code path that cannot be reached by design.
  391. For example, in the <code class="docutils literal notranslate"><span class="pre">default:</span></code> clause in a <code class="docutils literal notranslate"><span class="pre">switch</span></code> statement for which
  392. all possible values are covered in <code class="docutils literal notranslate"><span class="pre">case</span></code> statements. Use this in places
  393. where you might be tempted to put an <code class="docutils literal notranslate"><span class="pre">assert(0)</span></code> or <code class="docutils literal notranslate"><span class="pre">abort()</span></code> call.</p>
  394. <p>In release mode, the macro helps the compiler to optimize the code, and
  395. avoids a warning about unreachable code. For example, the macro is
  396. implemented with <code class="docutils literal notranslate"><span class="pre">__builtin_unreachable()</span></code> on GCC in release mode.</p>
  397. <p>A use for <code class="docutils literal notranslate"><span class="pre">Py_UNREACHABLE()</span></code> is following a call a function that
  398. never returns but that is not declared <code class="xref c c-macro docutils literal notranslate"><span class="pre">_Py_NO_RETURN</span></code>.</p>
  399. <p>If a code path is very unlikely code but can be reached under exceptional
  400. case, this macro must not be used. For example, under low memory condition
  401. or if a system call returns a value out of the expected range. In this
  402. case, it’s better to report the error to the caller. If the error cannot
  403. be reported to caller, <a class="reference internal" href="sys.html#c.Py_FatalError" title="Py_FatalError"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FatalError()</span></code></a> can be used.</p>
  404. <div class="versionadded">
  405. <p><span class="versionmodified added">New in version 3.7.</span></p>
  406. </div>
  407. </dd></dl>
  408. <dl class="c macro">
  409. <dt class="sig sig-object c" id="c.Py_UNUSED">
  410. <span class="sig-name descname"><span class="n"><span class="pre">Py_UNUSED</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">arg</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_UNUSED" title="Link to this definition">¶</a><br /></dt>
  411. <dd><p>Use this for unused arguments in a function definition to silence compiler
  412. warnings. Example: <code class="docutils literal notranslate"><span class="pre">int</span> <span class="pre">func(int</span> <span class="pre">a,</span> <span class="pre">int</span> <span class="pre">Py_UNUSED(b))</span> <span class="pre">{</span> <span class="pre">return</span> <span class="pre">a;</span> <span class="pre">}</span></code>.</p>
  413. <div class="versionadded">
  414. <p><span class="versionmodified added">New in version 3.4.</span></p>
  415. </div>
  416. </dd></dl>
  417. <dl class="c macro">
  418. <dt class="sig sig-object c" id="c.PyDoc_STRVAR">
  419. <span class="sig-name descname"><span class="n"><span class="pre">PyDoc_STRVAR</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">name</span></span>, <span class="n"><span class="pre">str</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDoc_STRVAR" title="Link to this definition">¶</a><br /></dt>
  420. <dd><p>Creates a variable with name <code class="docutils literal notranslate"><span class="pre">name</span></code> that can be used in docstrings.
  421. If Python is built without docstrings, the value will be empty.</p>
  422. <p>Use <a class="reference internal" href="#c.PyDoc_STRVAR" title="PyDoc_STRVAR"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyDoc_STRVAR</span></code></a> for docstrings to support building
  423. Python without docstrings, as specified in <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0007/"><strong>PEP 7</strong></a>.</p>
  424. <p>Example:</p>
  425. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyDoc_STRVAR</span><span class="p">(</span><span class="n">pop_doc</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Remove and return the rightmost element.&quot;</span><span class="p">);</span>
  426. <span class="k">static</span><span class="w"> </span><span class="n">PyMethodDef</span><span class="w"> </span><span class="n">deque_methods</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
  427. <span class="w"> </span><span class="c1">// ...</span>
  428. <span class="w"> </span><span class="p">{</span><span class="s">&quot;pop&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">deque_pop</span><span class="p">,</span><span class="w"> </span><span class="n">METH_NOARGS</span><span class="p">,</span><span class="w"> </span><span class="n">pop_doc</span><span class="p">},</span>
  429. <span class="w"> </span><span class="c1">// ...</span>
  430. <span class="p">}</span>
  431. </pre></div>
  432. </div>
  433. </dd></dl>
  434. <dl class="c macro">
  435. <dt class="sig sig-object c" id="c.PyDoc_STR">
  436. <span class="sig-name descname"><span class="n"><span class="pre">PyDoc_STR</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">str</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyDoc_STR" title="Link to this definition">¶</a><br /></dt>
  437. <dd><p>Creates a docstring for the given input string or an empty string
  438. if docstrings are disabled.</p>
  439. <p>Use <a class="reference internal" href="#c.PyDoc_STR" title="PyDoc_STR"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyDoc_STR</span></code></a> in specifying docstrings to support
  440. building Python without docstrings, as specified in <span class="target" id="index-2"></span><a class="pep reference external" href="https://peps.python.org/pep-0007/"><strong>PEP 7</strong></a>.</p>
  441. <p>Example:</p>
  442. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">PyMethodDef</span><span class="w"> </span><span class="n">pysqlite_row_methods</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
  443. <span class="w"> </span><span class="p">{</span><span class="s">&quot;keys&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">PyCFunction</span><span class="p">)</span><span class="n">pysqlite_row_keys</span><span class="p">,</span><span class="w"> </span><span class="n">METH_NOARGS</span><span class="p">,</span>
  444. <span class="w"> </span><span class="n">PyDoc_STR</span><span class="p">(</span><span class="s">&quot;Returns the keys of the row.&quot;</span><span class="p">)},</span>
  445. <span class="w"> </span><span class="p">{</span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">}</span>
  446. <span class="p">};</span>
  447. </pre></div>
  448. </div>
  449. </dd></dl>
  450. </section>
  451. <section id="objects-types-and-reference-counts">
  452. <span id="api-objects"></span><h2>Objects, Types and Reference Counts<a class="headerlink" href="#objects-types-and-reference-counts" title="Link to this heading">¶</a></h2>
  453. <p id="index-3">Most Python/C API functions have one or more arguments as well as a return value
  454. of type <span class="c-expr sig sig-inline c"><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span>. This type is a pointer to an opaque data type
  455. representing an arbitrary Python object. Since all Python object types are
  456. treated the same way by the Python language in most situations (e.g.,
  457. assignments, scope rules, and argument passing), it is only fitting that they
  458. should be represented by a single C type. Almost all Python objects live on the
  459. heap: you never declare an automatic or static variable of type
  460. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a>, only pointer variables of type <span class="c-expr sig sig-inline c"><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n">PyObject</span></a><span class="p">*</span></span> can be
  461. declared. The sole exception are the type objects; since these must never be
  462. deallocated, they are typically static <a class="reference internal" href="type.html#c.PyTypeObject" title="PyTypeObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyTypeObject</span></code></a> objects.</p>
  463. <p>All Python objects (even Python integers) have a <em class="dfn">type</em> and a
  464. <em class="dfn">reference count</em>. An object’s type determines what kind of object it is
  465. (e.g., an integer, a list, or a user-defined function; there are many more as
  466. explained in <a class="reference internal" href="../reference/datamodel.html#types"><span class="std std-ref">The standard type hierarchy</span></a>). For each of the well-known types there is a macro
  467. to check whether an object is of that type; for instance, <code class="docutils literal notranslate"><span class="pre">PyList_Check(a)</span></code> is
  468. true if (and only if) the object pointed to by <em>a</em> is a Python list.</p>
  469. <section id="reference-counts">
  470. <span id="api-refcounts"></span><h3>Reference Counts<a class="headerlink" href="#reference-counts" title="Link to this heading">¶</a></h3>
  471. <p>The reference count is important because today’s computers have a finite
  472. (and often severely limited) memory size; it counts how many different
  473. places there are that have a <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> to an object.
  474. Such a place could be another object, or a global (or static) C variable,
  475. or a local variable in some C function.
  476. When the last <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> to an object is released
  477. (i.e. its reference count becomes zero), the object is deallocated.
  478. If it contains references to other objects, those references are released.
  479. Those other objects may be deallocated in turn, if there are no more
  480. references to them, and so on. (There’s an obvious problem with
  481. objects that reference each other here; for now, the solution
  482. is “don’t do that.”)</p>
  483. <p id="index-4">Reference counts are always manipulated explicitly. The normal way is
  484. to use the macro <a class="reference internal" href="refcounting.html#c.Py_INCREF" title="Py_INCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_INCREF()</span></code></a> to take a new reference to an
  485. object (i.e. increment its reference count by one),
  486. and <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> to release that reference (i.e. decrement the
  487. reference count by one). The <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> macro
  488. is considerably more complex than the incref one, since it must check whether
  489. the reference count becomes zero and then cause the object’s deallocator to be
  490. called. The deallocator is a function pointer contained in the object’s type
  491. structure. The type-specific deallocator takes care of releasing references
  492. for other objects contained in the object if this is a compound
  493. object type, such as a list, as well as performing any additional finalization
  494. that’s needed. There’s no chance that the reference count can overflow; at
  495. least as many bits are used to hold the reference count as there are distinct
  496. memory locations in virtual memory (assuming <code class="docutils literal notranslate"><span class="pre">sizeof(Py_ssize_t)</span> <span class="pre">&gt;=</span> <span class="pre">sizeof(void*)</span></code>).
  497. Thus, the reference count increment is a simple operation.</p>
  498. <p>It is not necessary to hold a <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a> (i.e. increment
  499. the reference count) for every local variable that contains a pointer
  500. to an object. In theory, the object’s
  501. reference count goes up by one when the variable is made to point to it and it
  502. goes down by one when the variable goes out of scope. However, these two
  503. cancel each other out, so at the end the reference count hasn’t changed. The
  504. only real reason to use the reference count is to prevent the object from being
  505. deallocated as long as our variable is pointing to it. If we know that there
  506. is at least one other reference to the object that lives at least as long as
  507. our variable, there is no need to take a new <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a>
  508. (i.e. increment the reference count) temporarily.
  509. An important situation where this arises is in objects that are passed as
  510. arguments to C functions in an extension module that are called from Python;
  511. the call mechanism guarantees to hold a reference to every argument for the
  512. duration of the call.</p>
  513. <p>However, a common pitfall is to extract an object from a list and hold on to it
  514. for a while without taking a new reference. Some other operation might
  515. conceivably remove the object from the list, releasing that reference,
  516. and possibly deallocating it. The real danger is that innocent-looking
  517. operations may invoke arbitrary Python code which could do this; there is a code
  518. path which allows control to flow back to the user from a <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>, so
  519. almost any operation is potentially dangerous.</p>
  520. <p>A safe approach is to always use the generic operations (functions whose name
  521. begins with <code class="docutils literal notranslate"><span class="pre">PyObject_</span></code>, <code class="docutils literal notranslate"><span class="pre">PyNumber_</span></code>, <code class="docutils literal notranslate"><span class="pre">PySequence_</span></code> or <code class="docutils literal notranslate"><span class="pre">PyMapping_</span></code>).
  522. These operations always create a new <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a>
  523. (i.e. increment the reference count) of the object they return.
  524. This leaves the caller with the responsibility to 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> when
  525. they are done with the result; this soon becomes second nature.</p>
  526. <section id="reference-count-details">
  527. <span id="api-refcountdetails"></span><h4>Reference Count Details<a class="headerlink" href="#reference-count-details" title="Link to this heading">¶</a></h4>
  528. <p>The reference count behavior of functions in the Python/C API is best explained
  529. in terms of <em>ownership of references</em>. Ownership pertains to references, never
  530. to objects (objects are not owned: they are always shared). “Owning a
  531. reference” means being responsible for calling Py_DECREF on it when the
  532. reference is no longer needed. Ownership can also be transferred, meaning that
  533. the code that receives ownership of the reference then becomes responsible for
  534. eventually releasing it by calling <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> or <a class="reference internal" href="refcounting.html#c.Py_XDECREF" title="Py_XDECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XDECREF()</span></code></a>
  535. when it’s no longer needed—or passing on this responsibility (usually to its
  536. caller). When a function passes ownership of a reference on to its caller, the
  537. caller is said to receive a <em>new</em> reference. When no ownership is transferred,
  538. the caller is said to <em>borrow</em> the reference. Nothing needs to be done for a
  539. <a class="reference internal" href="../glossary.html#term-borrowed-reference"><span class="xref std std-term">borrowed reference</span></a>.</p>
  540. <p>Conversely, when a calling function passes in a reference to an object, there
  541. are two possibilities: the function <em>steals</em> a reference to the object, or it
  542. does not. <em>Stealing a reference</em> means that when you pass a reference to a
  543. function, that function assumes that it now owns that reference, and you are not
  544. responsible for it any longer.</p>
  545. <p id="index-5">Few functions steal references; the two notable exceptions are
  546. <a class="reference internal" href="list.html#c.PyList_SetItem" title="PyList_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_SetItem()</span></code></a> and <a class="reference internal" href="tuple.html#c.PyTuple_SetItem" title="PyTuple_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_SetItem()</span></code></a>, which steal a reference
  547. to the item (but not to the tuple or list into which the item is put!). These
  548. functions were designed to steal a reference because of a common idiom for
  549. populating a tuple or list with newly created objects; for example, the code to
  550. create the tuple <code class="docutils literal notranslate"><span class="pre">(1,</span> <span class="pre">2,</span> <span class="pre">&quot;three&quot;)</span></code> could look like this (forgetting about
  551. error handling for the moment; a better way to code this is shown below):</p>
  552. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">t</span><span class="p">;</span>
  553. <span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyTuple_New</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span>
  554. <span class="n">PyTuple_SetItem</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">PyLong_FromLong</span><span class="p">(</span><span class="mf">1L</span><span class="p">));</span>
  555. <span class="n">PyTuple_SetItem</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">PyLong_FromLong</span><span class="p">(</span><span class="mf">2L</span><span class="p">));</span>
  556. <span class="n">PyTuple_SetItem</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">PyUnicode_FromString</span><span class="p">(</span><span class="s">&quot;three&quot;</span><span class="p">));</span>
  557. </pre></div>
  558. </div>
  559. <p>Here, <a class="reference internal" href="long.html#c.PyLong_FromLong" title="PyLong_FromLong"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyLong_FromLong()</span></code></a> returns a new reference which is immediately
  560. stolen by <a class="reference internal" href="tuple.html#c.PyTuple_SetItem" title="PyTuple_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_SetItem()</span></code></a>. When you want to keep using an object
  561. although the reference to it will be stolen, use <a class="reference internal" href="refcounting.html#c.Py_INCREF" title="Py_INCREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_INCREF()</span></code></a> to grab
  562. another reference before calling the reference-stealing function.</p>
  563. <p>Incidentally, <a class="reference internal" href="tuple.html#c.PyTuple_SetItem" title="PyTuple_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_SetItem()</span></code></a> is the <em>only</em> way to set tuple items;
  564. <a class="reference internal" href="sequence.html#c.PySequence_SetItem" title="PySequence_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_SetItem()</span></code></a> and <a class="reference internal" href="object.html#c.PyObject_SetItem" title="PyObject_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_SetItem()</span></code></a> refuse to do this
  565. since tuples are an immutable data type. You should only use
  566. <a class="reference internal" href="tuple.html#c.PyTuple_SetItem" title="PyTuple_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyTuple_SetItem()</span></code></a> for tuples that you are creating yourself.</p>
  567. <p>Equivalent code for populating a list can be written using <a class="reference internal" href="list.html#c.PyList_New" title="PyList_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_New()</span></code></a>
  568. and <a class="reference internal" href="list.html#c.PyList_SetItem" title="PyList_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_SetItem()</span></code></a>.</p>
  569. <p>However, in practice, you will rarely use these ways of creating and populating
  570. a tuple or list. There’s a generic function, <a class="reference internal" href="arg.html#c.Py_BuildValue" title="Py_BuildValue"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_BuildValue()</span></code></a>, that can
  571. create most common objects from C values, directed by a <em class="dfn">format string</em>.
  572. For example, the above two blocks of code could be replaced by the following
  573. (which also takes care of the error checking):</p>
  574. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">tuple</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">list</span><span class="p">;</span>
  575. <span class="n">tuple</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">&quot;(iis)&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;three&quot;</span><span class="p">);</span>
  576. <span class="n">list</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">&quot;[iis]&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;three&quot;</span><span class="p">);</span>
  577. </pre></div>
  578. </div>
  579. <p>It is much more common to use <a class="reference internal" href="object.html#c.PyObject_SetItem" title="PyObject_SetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_SetItem()</span></code></a> and friends with items
  580. whose references you are only borrowing, like arguments that were passed in to
  581. the function you are writing. In that case, their behaviour regarding references
  582. is much saner, since you don’t have to take a new reference just so you
  583. can give that reference away (“have it be stolen”). For example, this function
  584. sets all items of a list (actually, any mutable sequence) to a given item:</p>
  585. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span>
  586. <span class="nf">set_all</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">target</span><span class="p">,</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">item</span><span class="p">)</span>
  587. <span class="p">{</span>
  588. <span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
  589. <span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyObject_Length</span><span class="p">(</span><span class="n">target</span><span class="p">);</span>
  590. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
  591. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
  592. <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  593. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyLong_FromSsize_t</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
  594. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">index</span><span class="p">)</span>
  595. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
  596. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyObject_SetItem</span><span class="p">(</span><span class="n">target</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="p">,</span><span class="w"> </span><span class="n">item</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  597. <span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">index</span><span class="p">);</span>
  598. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
  599. <span class="w"> </span><span class="p">}</span>
  600. <span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">index</span><span class="p">);</span>
  601. <span class="w"> </span><span class="p">}</span>
  602. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
  603. <span class="p">}</span>
  604. </pre></div>
  605. </div>
  606. <p id="index-6">The situation is slightly different for function return values. While passing
  607. a reference to most functions does not change your ownership responsibilities
  608. for that reference, many functions that return a reference to an object give
  609. you ownership of the reference. The reason is simple: in many cases, the
  610. returned object is created on the fly, and the reference you get is the only
  611. reference to the object. Therefore, the generic functions that return object
  612. references, like <a class="reference internal" href="object.html#c.PyObject_GetItem" title="PyObject_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetItem()</span></code></a> and <a class="reference internal" href="sequence.html#c.PySequence_GetItem" title="PySequence_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_GetItem()</span></code></a>,
  613. always return a new reference (the caller becomes the owner of the reference).</p>
  614. <p>It is important to realize that whether you own a reference returned by a
  615. function depends on which function you call only — <em>the plumage</em> (the type of
  616. the object passed as an argument to the function) <em>doesn’t enter into it!</em>
  617. Thus, if you extract an item from a list using <a class="reference internal" href="list.html#c.PyList_GetItem" title="PyList_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_GetItem()</span></code></a>, you
  618. don’t own the reference — but if you obtain the same item from the same list
  619. using <a class="reference internal" href="sequence.html#c.PySequence_GetItem" title="PySequence_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_GetItem()</span></code></a> (which happens to take exactly the same
  620. arguments), you do own a reference to the returned object.</p>
  621. <p id="index-7">Here is an example of how you could write a function that computes the sum of
  622. the items in a list of integers; once using <a class="reference internal" href="list.html#c.PyList_GetItem" title="PyList_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyList_GetItem()</span></code></a>, and once
  623. using <a class="reference internal" href="sequence.html#c.PySequence_GetItem" title="PySequence_GetItem"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySequence_GetItem()</span></code></a>.</p>
  624. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">long</span>
  625. <span class="nf">sum_list</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">list</span><span class="p">)</span>
  626. <span class="p">{</span>
  627. <span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
  628. <span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
  629. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">item</span><span class="p">;</span>
  630. <span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyList_Size</span><span class="p">(</span><span class="n">list</span><span class="p">);</span>
  631. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
  632. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Not a list */</span>
  633. <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  634. <span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyList_GetItem</span><span class="p">(</span><span class="n">list</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span><span class="w"> </span><span class="cm">/* Can&#39;t fail */</span>
  635. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">PyLong_Check</span><span class="p">(</span><span class="n">item</span><span class="p">))</span><span class="w"> </span><span class="k">continue</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Skip non-integers */</span>
  636. <span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyLong_AsLong</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
  637. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">value</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">-1</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">PyErr_Occurred</span><span class="p">())</span>
  638. <span class="w"> </span><span class="cm">/* Integer too big to fit in a C long, bail out */</span>
  639. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
  640. <span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
  641. <span class="w"> </span><span class="p">}</span>
  642. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
  643. <span class="p">}</span>
  644. </pre></div>
  645. </div>
  646. <div class="highlight-c notranslate" id="index-8"><div class="highlight"><pre><span></span><span class="kt">long</span>
  647. <span class="nf">sum_sequence</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">sequence</span><span class="p">)</span>
  648. <span class="p">{</span>
  649. <span class="w"> </span><span class="n">Py_ssize_t</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">;</span>
  650. <span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
  651. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">item</span><span class="p">;</span>
  652. <span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PySequence_Length</span><span class="p">(</span><span class="n">sequence</span><span class="p">);</span>
  653. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
  654. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Has no length */</span>
  655. <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  656. <span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PySequence_GetItem</span><span class="p">(</span><span class="n">sequence</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
  657. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">item</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span>
  658. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Not a sequence, or other failure */</span>
  659. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyLong_Check</span><span class="p">(</span><span class="n">item</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
  660. <span class="w"> </span><span class="n">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyLong_AsLong</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
  661. <span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
  662. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">value</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">-1</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">PyErr_Occurred</span><span class="p">())</span>
  663. <span class="w"> </span><span class="cm">/* Integer too big to fit in a C long, bail out */</span>
  664. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
  665. <span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">value</span><span class="p">;</span>
  666. <span class="w"> </span><span class="p">}</span>
  667. <span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
  668. <span class="w"> </span><span class="n">Py_DECREF</span><span class="p">(</span><span class="n">item</span><span class="p">);</span><span class="w"> </span><span class="cm">/* Discard reference ownership */</span>
  669. <span class="w"> </span><span class="p">}</span>
  670. <span class="w"> </span><span class="p">}</span>
  671. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">total</span><span class="p">;</span>
  672. <span class="p">}</span>
  673. </pre></div>
  674. </div>
  675. </section>
  676. </section>
  677. <section id="types">
  678. <span id="api-types"></span><span id="index-9"></span><h3>Types<a class="headerlink" href="#types" title="Link to this heading">¶</a></h3>
  679. <p>There are few other data types that play a significant role in the Python/C
  680. API; most are simple C types such as <span class="c-expr sig sig-inline c"><span class="kt">int</span></span>, <span class="c-expr sig sig-inline c"><span class="kt">long</span></span>,
  681. <span class="c-expr sig sig-inline c"><span class="kt">double</span></span> and <span class="c-expr sig sig-inline c"><span class="kt">char</span><span class="p">*</span></span>. A few structure types are used to
  682. describe static tables used to list the functions exported by a module or the
  683. data attributes of a new object type, and another is used to describe the value
  684. of a complex number. These will be discussed together with the functions that
  685. use them.</p>
  686. <dl class="c type">
  687. <dt class="sig sig-object c" id="c.Py_ssize_t">
  688. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_ssize_t</span></span></span><a class="headerlink" href="#c.Py_ssize_t" title="Link to this definition">¶</a><br /></dt>
  689. <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>A signed integral type such that <code class="docutils literal notranslate"><span class="pre">sizeof(Py_ssize_t)</span> <span class="pre">==</span> <span class="pre">sizeof(size_t)</span></code>.
  690. C99 doesn’t define such a thing directly (size_t is an unsigned integral type).
  691. See <span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0353/"><strong>PEP 353</strong></a> for details. <code class="docutils literal notranslate"><span class="pre">PY_SSIZE_T_MAX</span></code> is the largest positive value
  692. of type <a class="reference internal" href="#c.Py_ssize_t" title="Py_ssize_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_ssize_t</span></code></a>.</p>
  693. </dd></dl>
  694. </section>
  695. </section>
  696. <section id="exceptions">
  697. <span id="api-exceptions"></span><h2>Exceptions<a class="headerlink" href="#exceptions" title="Link to this heading">¶</a></h2>
  698. <p>The Python programmer only needs to deal with exceptions if specific error
  699. handling is required; unhandled exceptions are automatically propagated to the
  700. caller, then to the caller’s caller, and so on, until they reach the top-level
  701. interpreter, where they are reported to the user accompanied by a stack
  702. traceback.</p>
  703. <p id="index-11">For C programmers, however, error checking always has to be explicit. All
  704. functions in the Python/C API can raise exceptions, unless an explicit claim is
  705. made otherwise in a function’s documentation. In general, when a function
  706. encounters an error, it sets an exception, discards any object references that
  707. it owns, and returns an error indicator. If not documented otherwise, this
  708. indicator is either <code class="docutils literal notranslate"><span class="pre">NULL</span></code> or <code class="docutils literal notranslate"><span class="pre">-1</span></code>, depending on the function’s return type.
  709. A few functions return a Boolean true/false result, with false indicating an
  710. error. Very few functions return no explicit error indicator or have an
  711. ambiguous return value, and require explicit testing for errors with
  712. <a class="reference internal" href="exceptions.html#c.PyErr_Occurred" title="PyErr_Occurred"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Occurred()</span></code></a>. These exceptions are always explicitly documented.</p>
  713. <p id="index-12">Exception state is maintained in per-thread storage (this is equivalent to
  714. using global storage in an unthreaded application). A thread can be in one of
  715. two states: an exception has occurred, or not. The function
  716. <a class="reference internal" href="exceptions.html#c.PyErr_Occurred" title="PyErr_Occurred"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Occurred()</span></code></a> can be used to check for this: it returns a borrowed
  717. reference to the exception type object when an exception has occurred, and
  718. <code class="docutils literal notranslate"><span class="pre">NULL</span></code> otherwise. There are a number of functions to set the exception state:
  719. <a class="reference internal" href="exceptions.html#c.PyErr_SetString" title="PyErr_SetString"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_SetString()</span></code></a> is the most common (though not the most general)
  720. function to set the exception state, and <a class="reference internal" href="exceptions.html#c.PyErr_Clear" title="PyErr_Clear"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Clear()</span></code></a> clears the
  721. exception state.</p>
  722. <p>The full exception state consists of three objects (all of which can be
  723. <code class="docutils literal notranslate"><span class="pre">NULL</span></code>): the exception type, the corresponding exception value, and the
  724. traceback. These have the same meanings as the Python result of
  725. <code class="docutils literal notranslate"><span class="pre">sys.exc_info()</span></code>; however, they are not the same: the Python objects represent
  726. the last exception being handled by a Python <a class="reference internal" href="../reference/compound_stmts.html#try"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">try</span></code></a> …
  727. <a class="reference internal" href="../reference/compound_stmts.html#except"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">except</span></code></a> statement, while the C level exception state only exists while
  728. an exception is being passed on between C functions until it reaches the Python
  729. bytecode interpreter’s main loop, which takes care of transferring it to
  730. <code class="docutils literal notranslate"><span class="pre">sys.exc_info()</span></code> and friends.</p>
  731. <p id="index-13">Note that starting with Python 1.5, the preferred, thread-safe way to access the
  732. exception state from Python code is to call the function <a class="reference internal" href="../library/sys.html#sys.exc_info" title="sys.exc_info"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.exc_info()</span></code></a>,
  733. which returns the per-thread exception state for Python code. Also, the
  734. semantics of both ways to access the exception state have changed so that a
  735. function which catches an exception will save and restore its thread’s exception
  736. state so as to preserve the exception state of its caller. This prevents common
  737. bugs in exception handling code caused by an innocent-looking function
  738. overwriting the exception being handled; it also reduces the often unwanted
  739. lifetime extension for objects that are referenced by the stack frames in the
  740. traceback.</p>
  741. <p>As a general principle, a function that calls another function to perform some
  742. task should check whether the called function raised an exception, and if so,
  743. pass the exception state on to its caller. It should discard any object
  744. references that it owns, and return an error indicator, but it should <em>not</em> set
  745. another exception — that would overwrite the exception that was just raised,
  746. and lose important information about the exact cause of the error.</p>
  747. <p id="index-14">A simple example of detecting exceptions and passing them on is shown in the
  748. <code class="xref c c-func docutils literal notranslate"><span class="pre">sum_sequence()</span></code> example above. It so happens that this example doesn’t
  749. need to clean up any owned references when it detects an error. The following
  750. example function shows some error cleanup. First, to remind you why you like
  751. Python, we show the equivalent Python code:</p>
  752. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">def</span><span class="w"> </span><span class="n">incr_item</span><span class="p">(</span><span class="n">dict</span><span class="p">,</span><span class="w"> </span><span class="n">key</span><span class="p">)</span><span class="o">:</span>
  753. <span class="w"> </span><span class="nl">try</span><span class="p">:</span>
  754. <span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
  755. <span class="w"> </span><span class="n">except</span><span class="w"> </span><span class="n">KeyError</span><span class="o">:</span>
  756. <span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span>
  757. <span class="w"> </span><span class="n">dict</span><span class="p">[</span><span class="n">key</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span>
  758. </pre></div>
  759. </div>
  760. <p id="index-15">Here is the corresponding C code, in all its glory:</p>
  761. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="kt">int</span>
  762. <span class="nf">incr_item</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">dict</span><span class="p">,</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">key</span><span class="p">)</span>
  763. <span class="p">{</span>
  764. <span class="w"> </span><span class="cm">/* Objects all initialized to NULL for Py_XDECREF */</span>
  765. <span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">const_one</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">incremented_item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
  766. <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">rv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Return value initialized to -1 (failure) */</span>
  767. <span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyObject_GetItem</span><span class="p">(</span><span class="n">dict</span><span class="p">,</span><span class="w"> </span><span class="n">key</span><span class="p">);</span>
  768. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">item</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
  769. <span class="w"> </span><span class="cm">/* Handle KeyError only: */</span>
  770. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">PyErr_ExceptionMatches</span><span class="p">(</span><span class="n">PyExc_KeyError</span><span class="p">))</span>
  771. <span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">error</span><span class="p">;</span>
  772. <span class="w"> </span><span class="cm">/* Clear the error and use zero: */</span>
  773. <span class="w"> </span><span class="n">PyErr_Clear</span><span class="p">();</span>
  774. <span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyLong_FromLong</span><span class="p">(</span><span class="mf">0L</span><span class="p">);</span>
  775. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">item</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span>
  776. <span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">error</span><span class="p">;</span>
  777. <span class="w"> </span><span class="p">}</span>
  778. <span class="w"> </span><span class="n">const_one</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyLong_FromLong</span><span class="p">(</span><span class="mf">1L</span><span class="p">);</span>
  779. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">const_one</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span>
  780. <span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">error</span><span class="p">;</span>
  781. <span class="w"> </span><span class="n">incremented_item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyNumber_Add</span><span class="p">(</span><span class="n">item</span><span class="p">,</span><span class="w"> </span><span class="n">const_one</span><span class="p">);</span>
  782. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">incremented_item</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span>
  783. <span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">error</span><span class="p">;</span>
  784. <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">PyObject_SetItem</span><span class="p">(</span><span class="n">dict</span><span class="p">,</span><span class="w"> </span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">incremented_item</span><span class="p">)</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
  785. <span class="w"> </span><span class="k">goto</span><span class="w"> </span><span class="n">error</span><span class="p">;</span>
  786. <span class="w"> </span><span class="n">rv</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="cm">/* Success */</span>
  787. <span class="w"> </span><span class="cm">/* Continue with cleanup code */</span>
  788. <span class="w"> </span><span class="nl">error</span><span class="p">:</span>
  789. <span class="w"> </span><span class="cm">/* Cleanup code, shared by success and failure path */</span>
  790. <span class="w"> </span><span class="cm">/* Use Py_XDECREF() to ignore NULL references */</span>
  791. <span class="w"> </span><span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">item</span><span class="p">);</span>
  792. <span class="w"> </span><span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">const_one</span><span class="p">);</span>
  793. <span class="w"> </span><span class="n">Py_XDECREF</span><span class="p">(</span><span class="n">incremented_item</span><span class="p">);</span>
  794. <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">rv</span><span class="p">;</span><span class="w"> </span><span class="cm">/* -1 for error, 0 for success */</span>
  795. <span class="p">}</span>
  796. </pre></div>
  797. </div>
  798. <p id="index-17"><span id="index-16"></span>This example represents an endorsed use of the <code class="docutils literal notranslate"><span class="pre">goto</span></code> statement in C!
  799. It illustrates the use of <a class="reference internal" href="exceptions.html#c.PyErr_ExceptionMatches" title="PyErr_ExceptionMatches"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_ExceptionMatches()</span></code></a> and
  800. <a class="reference internal" href="exceptions.html#c.PyErr_Clear" title="PyErr_Clear"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Clear()</span></code></a> to handle specific exceptions, and the use of
  801. <a class="reference internal" href="refcounting.html#c.Py_XDECREF" title="Py_XDECREF"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_XDECREF()</span></code></a> to dispose of owned references that may be <code class="docutils literal notranslate"><span class="pre">NULL</span></code> (note the
  802. <code class="docutils literal notranslate"><span class="pre">'X'</span></code> in the name; <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> would crash when confronted with a
  803. <code class="docutils literal notranslate"><span class="pre">NULL</span></code> reference). It is important that the variables used to hold owned
  804. references are initialized to <code class="docutils literal notranslate"><span class="pre">NULL</span></code> for this to work; likewise, the proposed
  805. return value is initialized to <code class="docutils literal notranslate"><span class="pre">-1</span></code> (failure) and only set to success after
  806. the final call made is successful.</p>
  807. </section>
  808. <section id="embedding-python">
  809. <span id="api-embedding"></span><h2>Embedding Python<a class="headerlink" href="#embedding-python" title="Link to this heading">¶</a></h2>
  810. <p>The one important task that only embedders (as opposed to extension writers) of
  811. the Python interpreter have to worry about is the initialization, and possibly
  812. the finalization, of the Python interpreter. Most functionality of the
  813. interpreter can only be used after the interpreter has been initialized.</p>
  814. <p id="index-18">The basic initialization function is <a class="reference internal" href="init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>. This initializes
  815. the table of loaded modules, and creates the fundamental modules
  816. <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a>, <a class="reference internal" href="../library/__main__.html#module-__main__" title="__main__: The environment where top-level code is run. Covers command-line interfaces, import-time behavior, and ``__name__ == '__main__'``."><code class="xref py py-mod docutils literal notranslate"><span class="pre">__main__</span></code></a>, and <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">sys</span></code></a>. It also
  817. initializes the module search path (<code class="docutils literal notranslate"><span class="pre">sys.path</span></code>).</p>
  818. <p><a class="reference internal" href="init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> does not set the “script argument list” (<code class="docutils literal notranslate"><span class="pre">sys.argv</span></code>).
  819. If this variable is needed by Python code that will be executed later, setting
  820. <a class="reference internal" href="init_config.html#c.PyConfig.argv" title="PyConfig.argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.argv</span></code></a> and <a class="reference internal" href="init_config.html#c.PyConfig.parse_argv" title="PyConfig.parse_argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.parse_argv</span></code></a> must be set: see
  821. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  822. <p>On most systems (in particular, on Unix and Windows, although the details are
  823. slightly different), <a class="reference internal" href="init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> calculates the module search path
  824. based upon its best guess for the location of the standard Python interpreter
  825. executable, assuming that the Python library is found in a fixed location
  826. relative to the Python interpreter executable. In particular, it looks for a
  827. directory named <code class="file docutils literal notranslate"><span class="pre">lib/python</span><em><span class="pre">X.Y</span></em></code> relative to the parent directory
  828. where the executable named <code class="file docutils literal notranslate"><span class="pre">python</span></code> is found on the shell command search
  829. path (the environment variable <span class="target" id="index-19"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>).</p>
  830. <p>For instance, if the Python executable is found in
  831. <code class="file docutils literal notranslate"><span class="pre">/usr/local/bin/python</span></code>, it will assume that the libraries are in
  832. <code class="file docutils literal notranslate"><span class="pre">/usr/local/lib/python</span><em><span class="pre">X.Y</span></em></code>. (In fact, this particular path is also
  833. the “fallback” location, used when no executable file named <code class="file docutils literal notranslate"><span class="pre">python</span></code> is
  834. found along <span class="target" id="index-20"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PATH</span></code>.) The user can override this behavior by setting the
  835. environment variable <span class="target" id="index-21"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a>, or insert additional directories in
  836. front of the standard path by setting <span class="target" id="index-22"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a>.</p>
  837. <p id="index-23">The embedding application can steer the search by calling
  838. <code class="docutils literal notranslate"><span class="pre">Py_SetProgramName(file)</span></code> <em>before</em> calling <a class="reference internal" href="init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>. Note that
  839. <span class="target" id="index-24"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONHOME"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHOME</span></code></a> still overrides this and <span class="target" id="index-25"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONPATH"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONPATH</span></code></a> is still
  840. inserted in front of the standard path. An application that requires total
  841. control has to provide its own implementation of <a class="reference internal" href="init.html#c.Py_GetPath" title="Py_GetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPath()</span></code></a>,
  842. <a class="reference internal" href="init.html#c.Py_GetPrefix" title="Py_GetPrefix"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPrefix()</span></code></a>, <a class="reference internal" href="init.html#c.Py_GetExecPrefix" title="Py_GetExecPrefix"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetExecPrefix()</span></code></a>, and
  843. <a class="reference internal" href="init.html#c.Py_GetProgramFullPath" title="Py_GetProgramFullPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetProgramFullPath()</span></code></a> (all defined in <code class="file docutils literal notranslate"><span class="pre">Modules/getpath.c</span></code>).</p>
  844. <p id="index-26">Sometimes, it is desirable to “uninitialize” Python. For instance, the
  845. application may want to start over (make another call to
  846. <a class="reference internal" href="init.html#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>) or the application is simply done with its use of
  847. Python and wants to free memory allocated by Python. This can be accomplished
  848. by calling <a class="reference internal" href="init.html#c.Py_FinalizeEx" title="Py_FinalizeEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FinalizeEx()</span></code></a>. The function <a class="reference internal" href="init.html#c.Py_IsInitialized" title="Py_IsInitialized"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_IsInitialized()</span></code></a> returns
  849. true if Python is currently in the initialized state. More information about
  850. these functions is given in a later chapter. Notice that <a class="reference internal" href="init.html#c.Py_FinalizeEx" title="Py_FinalizeEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FinalizeEx()</span></code></a>
  851. does <em>not</em> free all memory allocated by the Python interpreter, e.g. memory
  852. allocated by extension modules currently cannot be released.</p>
  853. </section>
  854. <section id="debugging-builds">
  855. <span id="api-debugging"></span><h2>Debugging Builds<a class="headerlink" href="#debugging-builds" title="Link to this heading">¶</a></h2>
  856. <p>Python can be built with several macros to enable extra checks of the
  857. interpreter and extension modules. These checks tend to add a large amount of
  858. overhead to the runtime so they are not enabled by default.</p>
  859. <p>A full list of the various types of debugging builds is in the file
  860. <code class="file docutils literal notranslate"><span class="pre">Misc/SpecialBuilds.txt</span></code> in the Python source distribution. Builds are
  861. available that support tracing of reference counts, debugging the memory
  862. allocator, or low-level profiling of the main interpreter loop. Only the most
  863. frequently used builds will be described in the remainder of this section.</p>
  864. <dl class="c macro">
  865. <dt class="sig sig-object c" id="c.Py_DEBUG">
  866. <span class="sig-name descname"><span class="n"><span class="pre">Py_DEBUG</span></span></span><a class="headerlink" href="#c.Py_DEBUG" title="Link to this definition">¶</a><br /></dt>
  867. <dd></dd></dl>
  868. <p>Compiling the interpreter with the <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_DEBUG</span></code> macro defined produces
  869. what is generally meant by <a class="reference internal" href="../using/configure.html#debug-build"><span class="std std-ref">a debug build of Python</span></a>.
  870. <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_DEBUG</span></code> is enabled in the Unix build by adding
  871. <a class="reference internal" href="../using/configure.html#cmdoption-with-pydebug"><code class="xref std std-option docutils literal notranslate"><span class="pre">--with-pydebug</span></code></a> to the <code class="file docutils literal notranslate"><span class="pre">./configure</span></code> command.
  872. It is also implied by the presence of the
  873. not-Python-specific <code class="xref c c-macro docutils literal notranslate"><span class="pre">_DEBUG</span></code> macro. When <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_DEBUG</span></code> is enabled
  874. in the Unix build, compiler optimization is disabled.</p>
  875. <p>In addition to the reference count debugging described below, extra checks are
  876. performed, see <a class="reference internal" href="../using/configure.html#debug-build"><span class="std std-ref">Python Debug Build</span></a>.</p>
  877. <p>Defining <code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TRACE_REFS</span></code> enables reference tracing
  878. (see the <a class="reference internal" href="../using/configure.html#cmdoption-with-trace-refs"><code class="xref std std-option docutils literal notranslate"><span class="pre">configure</span> <span class="pre">--with-trace-refs</span> <span class="pre">option</span></code></a>).
  879. When defined, a circular doubly linked list of active objects is maintained by adding two extra
  880. fields to every <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a>. Total allocations are tracked as well. Upon
  881. exit, all existing references are printed. (In interactive mode this happens
  882. after every statement run by the interpreter.)</p>
  883. <p>Please refer to <code class="file docutils literal notranslate"><span class="pre">Misc/SpecialBuilds.txt</span></code> in the Python source distribution
  884. for more detailed information.</p>
  885. </section>
  886. </section>
  887. <div class="clearer"></div>
  888. </div>
  889. </div>
  890. </div>
  891. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  892. <div class="sphinxsidebarwrapper">
  893. <div>
  894. <h3><a href="../contents.html">Table of Contents</a></h3>
  895. <ul>
  896. <li><a class="reference internal" href="#">Introduction</a><ul>
  897. <li><a class="reference internal" href="#coding-standards">Coding standards</a></li>
  898. <li><a class="reference internal" href="#include-files">Include Files</a></li>
  899. <li><a class="reference internal" href="#useful-macros">Useful macros</a></li>
  900. <li><a class="reference internal" href="#objects-types-and-reference-counts">Objects, Types and Reference Counts</a><ul>
  901. <li><a class="reference internal" href="#reference-counts">Reference Counts</a><ul>
  902. <li><a class="reference internal" href="#reference-count-details">Reference Count Details</a></li>
  903. </ul>
  904. </li>
  905. <li><a class="reference internal" href="#types">Types</a></li>
  906. </ul>
  907. </li>
  908. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  909. <li><a class="reference internal" href="#embedding-python">Embedding Python</a></li>
  910. <li><a class="reference internal" href="#debugging-builds">Debugging Builds</a></li>
  911. </ul>
  912. </li>
  913. </ul>
  914. </div>
  915. <div>
  916. <h4>Previous topic</h4>
  917. <p class="topless"><a href="index.html"
  918. title="previous chapter">Python/C API Reference Manual</a></p>
  919. </div>
  920. <div>
  921. <h4>Next topic</h4>
  922. <p class="topless"><a href="stable.html"
  923. title="next chapter">C API Stability</a></p>
  924. </div>
  925. <div role="note" aria-label="source link">
  926. <h3>This Page</h3>
  927. <ul class="this-page-menu">
  928. <li><a href="../bugs.html">Report a Bug</a></li>
  929. <li>
  930. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/intro.rst"
  931. rel="nofollow">Show Source
  932. </a>
  933. </li>
  934. </ul>
  935. </div>
  936. </div>
  937. <div id="sidebarbutton" title="Collapse sidebar">
  938. <span>«</span>
  939. </div>
  940. </div>
  941. <div class="clearer"></div>
  942. </div>
  943. <div class="related" role="navigation" aria-label="related navigation">
  944. <h3>Navigation</h3>
  945. <ul>
  946. <li class="right" style="margin-right: 10px">
  947. <a href="../genindex.html" title="General Index"
  948. >index</a></li>
  949. <li class="right" >
  950. <a href="../py-modindex.html" title="Python Module Index"
  951. >modules</a> |</li>
  952. <li class="right" >
  953. <a href="stable.html" title="C API Stability"
  954. >next</a> |</li>
  955. <li class="right" >
  956. <a href="index.html" title="Python/C API Reference Manual"
  957. >previous</a> |</li>
  958. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  959. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  960. <li class="switchers">
  961. <div class="language_switcher_placeholder"></div>
  962. <div class="version_switcher_placeholder"></div>
  963. </li>
  964. <li>
  965. </li>
  966. <li id="cpython-language-and-version">
  967. <a href="../index.html">3.12.3 Documentation</a> &#187;
  968. </li>
  969. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  970. <li class="nav-item nav-item-this"><a href="">Introduction</a></li>
  971. <li class="right">
  972. <div class="inline-search" role="search">
  973. <form class="inline-search" action="../search.html" method="get">
  974. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  975. <input type="submit" value="Go" />
  976. </form>
  977. </div>
  978. |
  979. </li>
  980. <li class="right">
  981. <label class="theme-selector-label">
  982. Theme
  983. <select class="theme-selector" oninput="activateTheme(this.value)">
  984. <option value="auto" selected>Auto</option>
  985. <option value="light">Light</option>
  986. <option value="dark">Dark</option>
  987. </select>
  988. </label> |</li>
  989. </ul>
  990. </div>
  991. <div class="footer">
  992. &copy;
  993. <a href="../copyright.html">
  994. Copyright
  995. </a>
  996. 2001-2024, Python Software Foundation.
  997. <br />
  998. This page is licensed under the Python Software Foundation License Version 2.
  999. <br />
  1000. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1001. <br />
  1002. See <a href="/license.html">History and License</a> for more information.<br />
  1003. <br />
  1004. The Python Software Foundation is a non-profit corporation.
  1005. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1006. <br />
  1007. <br />
  1008. Last updated on Apr 09, 2024 (13:47 UTC).
  1009. <a href="/bugs.html">Found a bug</a>?
  1010. <br />
  1011. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1012. </div>
  1013. </body>
  1014. </html>
上海开阖软件有限公司 沪ICP备12045867号-1