gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2510 line
270KB

  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="Initialization, Finalization, and Threads" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/init.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="See also Python Initialization Configuration. Before Python Initialization: In an application embedding Python, the Py_Initialize() function must be called before using any other Python/C API funct..." />
  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="See also Python Initialization Configuration. Before Python Initialization: In an application embedding Python, the Py_Initialize() function must be called before using any other Python/C API funct..." />
  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>Initialization, Finalization, and Threads &#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="Python Initialization Configuration" href="init_config.html" />
  33. <link rel="prev" title="Objects for Type Hinting" href="typehints.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/c-api/init.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="#">Initialization, Finalization, and Threads</a><ul>
  86. <li><a class="reference internal" href="#before-python-initialization">Before Python Initialization</a></li>
  87. <li><a class="reference internal" href="#global-configuration-variables">Global configuration variables</a></li>
  88. <li><a class="reference internal" href="#initializing-and-finalizing-the-interpreter">Initializing and finalizing the interpreter</a></li>
  89. <li><a class="reference internal" href="#process-wide-parameters">Process-wide parameters</a></li>
  90. <li><a class="reference internal" href="#thread-state-and-the-global-interpreter-lock">Thread State and the Global Interpreter Lock</a><ul>
  91. <li><a class="reference internal" href="#releasing-the-gil-from-extension-code">Releasing the GIL from extension code</a></li>
  92. <li><a class="reference internal" href="#non-python-created-threads">Non-Python created threads</a></li>
  93. <li><a class="reference internal" href="#cautions-about-fork">Cautions about fork()</a></li>
  94. <li><a class="reference internal" href="#high-level-api">High-level API</a></li>
  95. <li><a class="reference internal" href="#low-level-api">Low-level API</a></li>
  96. </ul>
  97. </li>
  98. <li><a class="reference internal" href="#sub-interpreter-support">Sub-interpreter support</a><ul>
  99. <li><a class="reference internal" href="#a-per-interpreter-gil">A Per-Interpreter GIL</a></li>
  100. <li><a class="reference internal" href="#bugs-and-caveats">Bugs and caveats</a></li>
  101. </ul>
  102. </li>
  103. <li><a class="reference internal" href="#asynchronous-notifications">Asynchronous Notifications</a></li>
  104. <li><a class="reference internal" href="#profiling-and-tracing">Profiling and Tracing</a></li>
  105. <li><a class="reference internal" href="#advanced-debugger-support">Advanced Debugger Support</a></li>
  106. <li><a class="reference internal" href="#thread-local-storage-support">Thread Local Storage Support</a><ul>
  107. <li><a class="reference internal" href="#thread-specific-storage-tss-api">Thread Specific Storage (TSS) API</a><ul>
  108. <li><a class="reference internal" href="#dynamic-allocation">Dynamic Allocation</a></li>
  109. <li><a class="reference internal" href="#methods">Methods</a></li>
  110. </ul>
  111. </li>
  112. <li><a class="reference internal" href="#thread-local-storage-tls-api">Thread Local Storage (TLS) API</a></li>
  113. </ul>
  114. </li>
  115. </ul>
  116. </li>
  117. </ul>
  118. </div>
  119. <div>
  120. <h4>Previous topic</h4>
  121. <p class="topless"><a href="typehints.html"
  122. title="previous chapter">Objects for Type Hinting</a></p>
  123. </div>
  124. <div>
  125. <h4>Next topic</h4>
  126. <p class="topless"><a href="init_config.html"
  127. title="next chapter">Python Initialization Configuration</a></p>
  128. </div>
  129. <div role="note" aria-label="source link">
  130. <h3>This Page</h3>
  131. <ul class="this-page-menu">
  132. <li><a href="../bugs.html">Report a Bug</a></li>
  133. <li>
  134. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/init.rst"
  135. rel="nofollow">Show Source
  136. </a>
  137. </li>
  138. </ul>
  139. </div>
  140. </nav>
  141. </div>
  142. </div>
  143. <div class="related" role="navigation" aria-label="related navigation">
  144. <h3>Navigation</h3>
  145. <ul>
  146. <li class="right" style="margin-right: 10px">
  147. <a href="../genindex.html" title="General Index"
  148. accesskey="I">index</a></li>
  149. <li class="right" >
  150. <a href="../py-modindex.html" title="Python Module Index"
  151. >modules</a> |</li>
  152. <li class="right" >
  153. <a href="init_config.html" title="Python Initialization Configuration"
  154. accesskey="N">next</a> |</li>
  155. <li class="right" >
  156. <a href="typehints.html" title="Objects for Type Hinting"
  157. accesskey="P">previous</a> |</li>
  158. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  159. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  160. <li class="switchers">
  161. <div class="language_switcher_placeholder"></div>
  162. <div class="version_switcher_placeholder"></div>
  163. </li>
  164. <li>
  165. </li>
  166. <li id="cpython-language-and-version">
  167. <a href="../index.html">3.12.3 Documentation</a> &#187;
  168. </li>
  169. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python/C API Reference Manual</a> &#187;</li>
  170. <li class="nav-item nav-item-this"><a href="">Initialization, Finalization, and Threads</a></li>
  171. <li class="right">
  172. <div class="inline-search" role="search">
  173. <form class="inline-search" action="../search.html" method="get">
  174. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  175. <input type="submit" value="Go" />
  176. </form>
  177. </div>
  178. |
  179. </li>
  180. <li class="right">
  181. <label class="theme-selector-label">
  182. Theme
  183. <select class="theme-selector" oninput="activateTheme(this.value)">
  184. <option value="auto" selected>Auto</option>
  185. <option value="light">Light</option>
  186. <option value="dark">Dark</option>
  187. </select>
  188. </label> |</li>
  189. </ul>
  190. </div>
  191. <div class="document">
  192. <div class="documentwrapper">
  193. <div class="bodywrapper">
  194. <div class="body" role="main">
  195. <section id="initialization-finalization-and-threads">
  196. <span id="initialization"></span><h1>Initialization, Finalization, and Threads<a class="headerlink" href="#initialization-finalization-and-threads" title="Link to this heading">¶</a></h1>
  197. <p>See also <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  198. <section id="before-python-initialization">
  199. <span id="pre-init-safe"></span><h2>Before Python Initialization<a class="headerlink" href="#before-python-initialization" title="Link to this heading">¶</a></h2>
  200. <p>In an application embedding Python, the <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> function must
  201. be called before using any other Python/C API functions; with the exception of
  202. a few functions and the <a class="reference internal" href="#global-conf-vars"><span class="std std-ref">global configuration variables</span></a>.</p>
  203. <p>The following functions can be safely called before Python is initialized:</p>
  204. <ul class="simple">
  205. <li><p>Configuration functions:</p>
  206. <ul>
  207. <li><p><a class="reference internal" href="import.html#c.PyImport_AppendInittab" title="PyImport_AppendInittab"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_AppendInittab()</span></code></a></p></li>
  208. <li><p><a class="reference internal" href="import.html#c.PyImport_ExtendInittab" title="PyImport_ExtendInittab"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyImport_ExtendInittab()</span></code></a></p></li>
  209. <li><p><code class="xref c c-func docutils literal notranslate"><span class="pre">PyInitFrozenExtensions()</span></code></p></li>
  210. <li><p><a class="reference internal" href="memory.html#c.PyMem_SetAllocator" title="PyMem_SetAllocator"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetAllocator()</span></code></a></p></li>
  211. <li><p><a class="reference internal" href="memory.html#c.PyMem_SetupDebugHooks" title="PyMem_SetupDebugHooks"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_SetupDebugHooks()</span></code></a></p></li>
  212. <li><p><a class="reference internal" href="memory.html#c.PyObject_SetArenaAllocator" title="PyObject_SetArenaAllocator"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_SetArenaAllocator()</span></code></a></p></li>
  213. <li><p><a class="reference internal" href="#c.Py_SetPath" title="Py_SetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetPath()</span></code></a></p></li>
  214. <li><p><a class="reference internal" href="#c.Py_SetProgramName" title="Py_SetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code></a></p></li>
  215. <li><p><a class="reference internal" href="#c.Py_SetPythonHome" title="Py_SetPythonHome"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetPythonHome()</span></code></a></p></li>
  216. <li><p><a class="reference internal" href="#c.Py_SetStandardStreamEncoding" title="Py_SetStandardStreamEncoding"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetStandardStreamEncoding()</span></code></a></p></li>
  217. <li><p><a class="reference internal" href="sys.html#c.PySys_AddWarnOption" title="PySys_AddWarnOption"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_AddWarnOption()</span></code></a></p></li>
  218. <li><p><a class="reference internal" href="sys.html#c.PySys_AddXOption" title="PySys_AddXOption"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_AddXOption()</span></code></a></p></li>
  219. <li><p><a class="reference internal" href="sys.html#c.PySys_ResetWarnOptions" title="PySys_ResetWarnOptions"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_ResetWarnOptions()</span></code></a></p></li>
  220. </ul>
  221. </li>
  222. <li><p>Informative functions:</p>
  223. <ul>
  224. <li><p><a class="reference internal" href="#c.Py_IsInitialized" title="Py_IsInitialized"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_IsInitialized()</span></code></a></p></li>
  225. <li><p><a class="reference internal" href="memory.html#c.PyMem_GetAllocator" title="PyMem_GetAllocator"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_GetAllocator()</span></code></a></p></li>
  226. <li><p><a class="reference internal" href="memory.html#c.PyObject_GetArenaAllocator" title="PyObject_GetArenaAllocator"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_GetArenaAllocator()</span></code></a></p></li>
  227. <li><p><a class="reference internal" href="#c.Py_GetBuildInfo" title="Py_GetBuildInfo"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetBuildInfo()</span></code></a></p></li>
  228. <li><p><a class="reference internal" href="#c.Py_GetCompiler" title="Py_GetCompiler"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetCompiler()</span></code></a></p></li>
  229. <li><p><a class="reference internal" href="#c.Py_GetCopyright" title="Py_GetCopyright"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetCopyright()</span></code></a></p></li>
  230. <li><p><a class="reference internal" href="#c.Py_GetPlatform" title="Py_GetPlatform"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPlatform()</span></code></a></p></li>
  231. <li><p><a class="reference internal" href="#c.Py_GetVersion" title="Py_GetVersion"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetVersion()</span></code></a></p></li>
  232. </ul>
  233. </li>
  234. <li><p>Utilities:</p>
  235. <ul>
  236. <li><p><a class="reference internal" href="sys.html#c.Py_DecodeLocale" title="Py_DecodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code></a></p></li>
  237. </ul>
  238. </li>
  239. <li><p>Memory allocators:</p>
  240. <ul>
  241. <li><p><a class="reference internal" href="memory.html#c.PyMem_RawMalloc" title="PyMem_RawMalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawMalloc()</span></code></a></p></li>
  242. <li><p><a class="reference internal" href="memory.html#c.PyMem_RawRealloc" title="PyMem_RawRealloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawRealloc()</span></code></a></p></li>
  243. <li><p><a class="reference internal" href="memory.html#c.PyMem_RawCalloc" title="PyMem_RawCalloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawCalloc()</span></code></a></p></li>
  244. <li><p><a class="reference internal" href="memory.html#c.PyMem_RawFree" title="PyMem_RawFree"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyMem_RawFree()</span></code></a></p></li>
  245. </ul>
  246. </li>
  247. </ul>
  248. <div class="admonition note">
  249. <p class="admonition-title">Note</p>
  250. <p>The following functions <strong>should not be called</strong> before
  251. <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>: <a class="reference internal" href="sys.html#c.Py_EncodeLocale" title="Py_EncodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_EncodeLocale()</span></code></a>, <a class="reference internal" href="#c.Py_GetPath" title="Py_GetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPath()</span></code></a>,
  252. <a class="reference internal" href="#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="#c.Py_GetExecPrefix" title="Py_GetExecPrefix"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetExecPrefix()</span></code></a>,
  253. <a class="reference internal" href="#c.Py_GetProgramFullPath" title="Py_GetProgramFullPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetProgramFullPath()</span></code></a>, <a class="reference internal" href="#c.Py_GetPythonHome" title="Py_GetPythonHome"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPythonHome()</span></code></a>,
  254. <a class="reference internal" href="#c.Py_GetProgramName" title="Py_GetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetProgramName()</span></code></a> and <a class="reference internal" href="#c.PyEval_InitThreads" title="PyEval_InitThreads"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_InitThreads()</span></code></a>.</p>
  255. </div>
  256. </section>
  257. <section id="global-configuration-variables">
  258. <span id="global-conf-vars"></span><h2>Global configuration variables<a class="headerlink" href="#global-configuration-variables" title="Link to this heading">¶</a></h2>
  259. <p>Python has variables for the global configuration to control different features
  260. and options. By default, these flags are controlled by <a class="reference internal" href="../using/cmdline.html#using-on-interface-options"><span class="std std-ref">command line
  261. options</span></a>.</p>
  262. <p>When a flag is set by an option, the value of the flag is the number of times
  263. that the option was set. For example, <code class="docutils literal notranslate"><span class="pre">-b</span></code> sets <a class="reference internal" href="#c.Py_BytesWarningFlag" title="Py_BytesWarningFlag"><code class="xref c c-data docutils literal notranslate"><span class="pre">Py_BytesWarningFlag</span></code></a>
  264. to 1 and <code class="docutils literal notranslate"><span class="pre">-bb</span></code> sets <a class="reference internal" href="#c.Py_BytesWarningFlag" title="Py_BytesWarningFlag"><code class="xref c c-data docutils literal notranslate"><span class="pre">Py_BytesWarningFlag</span></code></a> to 2.</p>
  265. <dl class="c var">
  266. <dt class="sig sig-object c" id="c.Py_BytesWarningFlag">
  267. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_BytesWarningFlag</span></span></span><a class="headerlink" href="#c.Py_BytesWarningFlag" title="Link to this definition">¶</a><br /></dt>
  268. <dd><p>This API is kept for backward compatibility: setting
  269. <a class="reference internal" href="init_config.html#c.PyConfig.bytes_warning" title="PyConfig.bytes_warning"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.bytes_warning</span></code></a> should be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python
  270. Initialization Configuration</span></a>.</p>
  271. <p>Issue a warning when comparing <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> or <a class="reference internal" href="../library/stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a> with
  272. <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> with <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>. Issue an error if greater
  273. or equal to <code class="docutils literal notranslate"><span class="pre">2</span></code>.</p>
  274. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-b"><code class="xref std std-option docutils literal notranslate"><span class="pre">-b</span></code></a> option.</p>
  275. <div class="deprecated">
  276. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  277. </div>
  278. </dd></dl>
  279. <dl class="c var">
  280. <dt class="sig sig-object c" id="c.Py_DebugFlag">
  281. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_DebugFlag</span></span></span><a class="headerlink" href="#c.Py_DebugFlag" title="Link to this definition">¶</a><br /></dt>
  282. <dd><p>This API is kept for backward compatibility: setting
  283. <a class="reference internal" href="init_config.html#c.PyConfig.parser_debug" title="PyConfig.parser_debug"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.parser_debug</span></code></a> should be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python
  284. Initialization Configuration</span></a>.</p>
  285. <p>Turn on parser debugging output (for expert only, depending on compilation
  286. options).</p>
  287. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-d"><code class="xref std std-option docutils literal notranslate"><span class="pre">-d</span></code></a> option and the <span class="target" id="index-0"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONDEBUG"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONDEBUG</span></code></a> environment
  288. variable.</p>
  289. <div class="deprecated">
  290. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  291. </div>
  292. </dd></dl>
  293. <dl class="c var">
  294. <dt class="sig sig-object c" id="c.Py_DontWriteBytecodeFlag">
  295. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_DontWriteBytecodeFlag</span></span></span><a class="headerlink" href="#c.Py_DontWriteBytecodeFlag" title="Link to this definition">¶</a><br /></dt>
  296. <dd><p>This API is kept for backward compatibility: setting
  297. <a class="reference internal" href="init_config.html#c.PyConfig.write_bytecode" title="PyConfig.write_bytecode"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.write_bytecode</span></code></a> should be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python
  298. Initialization Configuration</span></a>.</p>
  299. <p>If set to non-zero, Python won’t try to write <code class="docutils literal notranslate"><span class="pre">.pyc</span></code> files on the
  300. import of source modules.</p>
  301. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-B"><code class="xref std std-option docutils literal notranslate"><span class="pre">-B</span></code></a> option and the <span class="target" id="index-1"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONDONTWRITEBYTECODE</span></code></a>
  302. environment variable.</p>
  303. <div class="deprecated">
  304. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  305. </div>
  306. </dd></dl>
  307. <dl class="c var">
  308. <dt class="sig sig-object c" id="c.Py_FrozenFlag">
  309. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_FrozenFlag</span></span></span><a class="headerlink" href="#c.Py_FrozenFlag" title="Link to this definition">¶</a><br /></dt>
  310. <dd><p>This API is kept for backward compatibility: setting
  311. <a class="reference internal" href="init_config.html#c.PyConfig.pathconfig_warnings" title="PyConfig.pathconfig_warnings"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.pathconfig_warnings</span></code></a> should be used instead, see
  312. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  313. <p>Suppress error messages when calculating the module search path in
  314. <a class="reference internal" href="#c.Py_GetPath" title="Py_GetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPath()</span></code></a>.</p>
  315. <p>Private flag used by <code class="docutils literal notranslate"><span class="pre">_freeze_module</span></code> and <code class="docutils literal notranslate"><span class="pre">frozenmain</span></code> programs.</p>
  316. <div class="deprecated">
  317. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  318. </div>
  319. </dd></dl>
  320. <dl class="c var">
  321. <dt class="sig sig-object c" id="c.Py_HashRandomizationFlag">
  322. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_HashRandomizationFlag</span></span></span><a class="headerlink" href="#c.Py_HashRandomizationFlag" title="Link to this definition">¶</a><br /></dt>
  323. <dd><p>This API is kept for backward compatibility: setting
  324. <a class="reference internal" href="init_config.html#c.PyConfig.hash_seed" title="PyConfig.hash_seed"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.hash_seed</span></code></a> and <a class="reference internal" href="init_config.html#c.PyConfig.use_hash_seed" title="PyConfig.use_hash_seed"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.use_hash_seed</span></code></a> should
  325. be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  326. <p>Set to <code class="docutils literal notranslate"><span class="pre">1</span></code> if the <span class="target" id="index-2"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONHASHSEED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></a> environment variable is set to
  327. a non-empty string.</p>
  328. <p>If the flag is non-zero, read the <span class="target" id="index-3"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONHASHSEED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONHASHSEED</span></code></a> environment
  329. variable to initialize the secret hash seed.</p>
  330. <div class="deprecated">
  331. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  332. </div>
  333. </dd></dl>
  334. <dl class="c var">
  335. <dt class="sig sig-object c" id="c.Py_IgnoreEnvironmentFlag">
  336. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_IgnoreEnvironmentFlag</span></span></span><a class="headerlink" href="#c.Py_IgnoreEnvironmentFlag" title="Link to this definition">¶</a><br /></dt>
  337. <dd><p>This API is kept for backward compatibility: setting
  338. <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> should be used instead, see
  339. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  340. <p>Ignore all <code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHON*</span></code> environment variables, e.g.
  341. <span class="target" id="index-4"></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> and <span class="target" id="index-5"></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>, that might be set.</p>
  342. <p>Set by the <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> and <a class="reference internal" href="../using/cmdline.html#cmdoption-I"><code class="xref std std-option docutils literal notranslate"><span class="pre">-I</span></code></a> options.</p>
  343. <div class="deprecated">
  344. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  345. </div>
  346. </dd></dl>
  347. <dl class="c var">
  348. <dt class="sig sig-object c" id="c.Py_InspectFlag">
  349. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_InspectFlag</span></span></span><a class="headerlink" href="#c.Py_InspectFlag" title="Link to this definition">¶</a><br /></dt>
  350. <dd><p>This API is kept for backward compatibility: setting
  351. <a class="reference internal" href="init_config.html#c.PyConfig.inspect" title="PyConfig.inspect"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.inspect</span></code></a> should be used instead, see
  352. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  353. <p>When a script is passed as first argument or the <a class="reference internal" href="../using/cmdline.html#cmdoption-c"><code class="xref std std-option docutils literal notranslate"><span class="pre">-c</span></code></a> option is used,
  354. enter interactive mode after executing the script or the command, even when
  355. <a class="reference internal" href="../library/sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a> does not appear to be a terminal.</p>
  356. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-i"><code class="xref std std-option docutils literal notranslate"><span class="pre">-i</span></code></a> option and the <span class="target" id="index-6"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONINSPECT"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONINSPECT</span></code></a> environment
  357. variable.</p>
  358. <div class="deprecated">
  359. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  360. </div>
  361. </dd></dl>
  362. <dl class="c var">
  363. <dt class="sig sig-object c" id="c.Py_InteractiveFlag">
  364. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_InteractiveFlag</span></span></span><a class="headerlink" href="#c.Py_InteractiveFlag" title="Link to this definition">¶</a><br /></dt>
  365. <dd><p>This API is kept for backward compatibility: setting
  366. <a class="reference internal" href="init_config.html#c.PyConfig.interactive" title="PyConfig.interactive"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.interactive</span></code></a> should be used instead, see
  367. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  368. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-i"><code class="xref std std-option docutils literal notranslate"><span class="pre">-i</span></code></a> option.</p>
  369. <div class="deprecated">
  370. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  371. </div>
  372. </dd></dl>
  373. <dl class="c var">
  374. <dt class="sig sig-object c" id="c.Py_IsolatedFlag">
  375. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_IsolatedFlag</span></span></span><a class="headerlink" href="#c.Py_IsolatedFlag" title="Link to this definition">¶</a><br /></dt>
  376. <dd><p>This API is kept for backward compatibility: setting
  377. <a class="reference internal" href="init_config.html#c.PyConfig.isolated" title="PyConfig.isolated"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.isolated</span></code></a> should be used instead, see
  378. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  379. <p>Run Python in isolated mode. In isolated mode <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> contains
  380. neither the script’s directory nor the user’s site-packages directory.</p>
  381. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-I"><code class="xref std std-option docutils literal notranslate"><span class="pre">-I</span></code></a> option.</p>
  382. <div class="versionadded">
  383. <p><span class="versionmodified added">New in version 3.4.</span></p>
  384. </div>
  385. <div class="deprecated">
  386. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  387. </div>
  388. </dd></dl>
  389. <dl class="c var">
  390. <dt class="sig sig-object c" id="c.Py_LegacyWindowsFSEncodingFlag">
  391. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_LegacyWindowsFSEncodingFlag</span></span></span><a class="headerlink" href="#c.Py_LegacyWindowsFSEncodingFlag" title="Link to this definition">¶</a><br /></dt>
  392. <dd><p>This API is kept for backward compatibility: setting
  393. <a class="reference internal" href="init_config.html#c.PyPreConfig.legacy_windows_fs_encoding" title="PyPreConfig.legacy_windows_fs_encoding"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyPreConfig.legacy_windows_fs_encoding</span></code></a> should be used instead, see
  394. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  395. <p>If the flag is non-zero, use the <code class="docutils literal notranslate"><span class="pre">mbcs</span></code> encoding with <code class="docutils literal notranslate"><span class="pre">replace</span></code> error
  396. handler, instead of the UTF-8 encoding with <code class="docutils literal notranslate"><span class="pre">surrogatepass</span></code> error handler,
  397. for the <a class="reference internal" href="../glossary.html#term-filesystem-encoding-and-error-handler"><span class="xref std std-term">filesystem encoding and error handler</span></a>.</p>
  398. <p>Set to <code class="docutils literal notranslate"><span class="pre">1</span></code> if the <span class="target" id="index-7"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONLEGACYWINDOWSFSENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONLEGACYWINDOWSFSENCODING</span></code></a> environment
  399. variable is set to a non-empty string.</p>
  400. <p>See <span class="target" id="index-8"></span><a class="pep reference external" href="https://peps.python.org/pep-0529/"><strong>PEP 529</strong></a> for more details.</p>
  401. <div class="availability docutils container">
  402. <p><a class="reference internal" href="../library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  403. </div>
  404. <div class="deprecated">
  405. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  406. </div>
  407. </dd></dl>
  408. <dl class="c var">
  409. <dt class="sig sig-object c" id="c.Py_LegacyWindowsStdioFlag">
  410. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_LegacyWindowsStdioFlag</span></span></span><a class="headerlink" href="#c.Py_LegacyWindowsStdioFlag" title="Link to this definition">¶</a><br /></dt>
  411. <dd><p>This API is kept for backward compatibility: setting
  412. <a class="reference internal" href="init_config.html#c.PyConfig.legacy_windows_stdio" title="PyConfig.legacy_windows_stdio"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.legacy_windows_stdio</span></code></a> should be used instead, see
  413. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  414. <p>If the flag is non-zero, use <a class="reference internal" href="../library/io.html#io.FileIO" title="io.FileIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.FileIO</span></code></a> instead of
  415. <code class="xref py py-class docutils literal notranslate"><span class="pre">io._WindowsConsoleIO</span></code> for <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> standard streams.</p>
  416. <p>Set to <code class="docutils literal notranslate"><span class="pre">1</span></code> if the <span class="target" id="index-9"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONLEGACYWINDOWSSTDIO"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONLEGACYWINDOWSSTDIO</span></code></a> environment
  417. variable is set to a non-empty string.</p>
  418. <p>See <span class="target" id="index-10"></span><a class="pep reference external" href="https://peps.python.org/pep-0528/"><strong>PEP 528</strong></a> for more details.</p>
  419. <div class="availability docutils container">
  420. <p><a class="reference internal" href="../library/intro.html#availability"><span class="std std-ref">Availability</span></a>: Windows.</p>
  421. </div>
  422. <div class="deprecated">
  423. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  424. </div>
  425. </dd></dl>
  426. <dl class="c var">
  427. <dt class="sig sig-object c" id="c.Py_NoSiteFlag">
  428. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_NoSiteFlag</span></span></span><a class="headerlink" href="#c.Py_NoSiteFlag" title="Link to this definition">¶</a><br /></dt>
  429. <dd><p>This API is kept for backward compatibility: setting
  430. <a class="reference internal" href="init_config.html#c.PyConfig.site_import" title="PyConfig.site_import"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.site_import</span></code></a> should be used instead, see
  431. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  432. <p>Disable the import of the module <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> and the site-dependent
  433. manipulations of <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> that it entails. Also disable these
  434. manipulations if <a class="reference internal" href="../library/site.html#module-site" title="site: Module responsible for site-specific configuration."><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code></a> is explicitly imported later (call
  435. <a class="reference internal" href="../library/site.html#site.main" title="site.main"><code class="xref py py-func docutils literal notranslate"><span class="pre">site.main()</span></code></a> if you want them to be triggered).</p>
  436. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-S"><code class="xref std std-option docutils literal notranslate"><span class="pre">-S</span></code></a> option.</p>
  437. <div class="deprecated">
  438. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  439. </div>
  440. </dd></dl>
  441. <dl class="c var">
  442. <dt class="sig sig-object c" id="c.Py_NoUserSiteDirectory">
  443. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_NoUserSiteDirectory</span></span></span><a class="headerlink" href="#c.Py_NoUserSiteDirectory" title="Link to this definition">¶</a><br /></dt>
  444. <dd><p>This API is kept for backward compatibility: setting
  445. <a class="reference internal" href="init_config.html#c.PyConfig.user_site_directory" title="PyConfig.user_site_directory"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.user_site_directory</span></code></a> should be used instead, see
  446. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  447. <p>Don’t add the <a class="reference internal" href="../library/site.html#site.USER_SITE" title="site.USER_SITE"><code class="xref py py-data docutils literal notranslate"><span class="pre">user</span> <span class="pre">site-packages</span> <span class="pre">directory</span></code></a> to
  448. <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>.</p>
  449. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-s"><code class="xref std std-option docutils literal notranslate"><span class="pre">-s</span></code></a> and <a class="reference internal" href="../using/cmdline.html#cmdoption-I"><code class="xref std std-option docutils literal notranslate"><span class="pre">-I</span></code></a> options, and the
  450. <span class="target" id="index-11"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONNOUSERSITE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONNOUSERSITE</span></code></a> environment variable.</p>
  451. <div class="deprecated">
  452. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  453. </div>
  454. </dd></dl>
  455. <dl class="c var">
  456. <dt class="sig sig-object c" id="c.Py_OptimizeFlag">
  457. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_OptimizeFlag</span></span></span><a class="headerlink" href="#c.Py_OptimizeFlag" title="Link to this definition">¶</a><br /></dt>
  458. <dd><p>This API is kept for backward compatibility: setting
  459. <a class="reference internal" href="init_config.html#c.PyConfig.optimization_level" title="PyConfig.optimization_level"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.optimization_level</span></code></a> should be used instead, see
  460. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  461. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-O"><code class="xref std std-option docutils literal notranslate"><span class="pre">-O</span></code></a> option and the <span class="target" id="index-12"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONOPTIMIZE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONOPTIMIZE</span></code></a> environment
  462. variable.</p>
  463. <div class="deprecated">
  464. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  465. </div>
  466. </dd></dl>
  467. <dl class="c var">
  468. <dt class="sig sig-object c" id="c.Py_QuietFlag">
  469. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_QuietFlag</span></span></span><a class="headerlink" href="#c.Py_QuietFlag" title="Link to this definition">¶</a><br /></dt>
  470. <dd><p>This API is kept for backward compatibility: setting
  471. <a class="reference internal" href="init_config.html#c.PyConfig.quiet" title="PyConfig.quiet"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.quiet</span></code></a> should be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python
  472. Initialization Configuration</span></a>.</p>
  473. <p>Don’t display the copyright and version messages even in interactive mode.</p>
  474. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-q"><code class="xref std std-option docutils literal notranslate"><span class="pre">-q</span></code></a> option.</p>
  475. <div class="versionadded">
  476. <p><span class="versionmodified added">New in version 3.2.</span></p>
  477. </div>
  478. <div class="deprecated">
  479. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  480. </div>
  481. </dd></dl>
  482. <dl class="c var">
  483. <dt class="sig sig-object c" id="c.Py_UnbufferedStdioFlag">
  484. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_UnbufferedStdioFlag</span></span></span><a class="headerlink" href="#c.Py_UnbufferedStdioFlag" title="Link to this definition">¶</a><br /></dt>
  485. <dd><p>This API is kept for backward compatibility: setting
  486. <a class="reference internal" href="init_config.html#c.PyConfig.buffered_stdio" title="PyConfig.buffered_stdio"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.buffered_stdio</span></code></a> should be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python
  487. Initialization Configuration</span></a>.</p>
  488. <p>Force the stdout and stderr streams to be unbuffered.</p>
  489. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-u"><code class="xref std std-option docutils literal notranslate"><span class="pre">-u</span></code></a> option and the <span class="target" id="index-13"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONUNBUFFERED"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONUNBUFFERED</span></code></a>
  490. environment variable.</p>
  491. <div class="deprecated">
  492. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  493. </div>
  494. </dd></dl>
  495. <dl class="c var">
  496. <dt class="sig sig-object c" id="c.Py_VerboseFlag">
  497. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_VerboseFlag</span></span></span><a class="headerlink" href="#c.Py_VerboseFlag" title="Link to this definition">¶</a><br /></dt>
  498. <dd><p>This API is kept for backward compatibility: setting
  499. <a class="reference internal" href="init_config.html#c.PyConfig.verbose" title="PyConfig.verbose"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.verbose</span></code></a> should be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python
  500. Initialization Configuration</span></a>.</p>
  501. <p>Print a message each time a module is initialized, showing the place
  502. (filename or built-in module) from which it is loaded. If greater or equal
  503. to <code class="docutils literal notranslate"><span class="pre">2</span></code>, print a message for each file that is checked for when
  504. searching for a module. Also provides information on module cleanup at exit.</p>
  505. <p>Set by the <a class="reference internal" href="../using/cmdline.html#cmdoption-v"><code class="xref std std-option docutils literal notranslate"><span class="pre">-v</span></code></a> option and the <span class="target" id="index-14"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONVERBOSE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONVERBOSE</span></code></a> environment
  506. variable.</p>
  507. <div class="deprecated">
  508. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  509. </div>
  510. </dd></dl>
  511. </section>
  512. <section id="initializing-and-finalizing-the-interpreter">
  513. <h2>Initializing and finalizing the interpreter<a class="headerlink" href="#initializing-and-finalizing-the-interpreter" title="Link to this heading">¶</a></h2>
  514. <dl class="c function">
  515. <dt class="sig sig-object c" id="c.Py_Initialize">
  516. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_Initialize</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_Initialize" title="Link to this definition">¶</a><br /></dt>
  517. <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 id="index-15">Initialize the Python interpreter. In an application embedding Python,
  518. this should be called before using any other Python/C API functions; see
  519. <a class="reference internal" href="#pre-init-safe"><span class="std std-ref">Before Python Initialization</span></a> for the few exceptions.</p>
  520. <p>This initializes
  521. the table of loaded modules (<code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>), and creates the fundamental
  522. modules <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 initializes
  523. the module search path (<code class="docutils literal notranslate"><span class="pre">sys.path</span></code>). It does not set <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code>; use
  524. <a class="reference internal" href="#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgvEx()</span></code></a> for that. This is a no-op when called for a second time
  525. (without calling <a class="reference internal" href="#c.Py_FinalizeEx" title="Py_FinalizeEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FinalizeEx()</span></code></a> first). There is no return value; it is a
  526. fatal error if the initialization fails.</p>
  527. <p>Use the <a class="reference internal" href="init_config.html#c.Py_InitializeFromConfig" title="Py_InitializeFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_InitializeFromConfig()</span></code></a> function to customize the
  528. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  529. <div class="admonition note">
  530. <p class="admonition-title">Note</p>
  531. <p>On Windows, changes the console mode from <code class="docutils literal notranslate"><span class="pre">O_TEXT</span></code> to <code class="docutils literal notranslate"><span class="pre">O_BINARY</span></code>, which will
  532. also affect non-Python uses of the console using the C Runtime.</p>
  533. </div>
  534. </dd></dl>
  535. <dl class="c function">
  536. <dt class="sig sig-object c" id="c.Py_InitializeEx">
  537. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_InitializeEx</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">initsigs</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_InitializeEx" title="Link to this definition">¶</a><br /></dt>
  538. <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>This function works like <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> if <em>initsigs</em> is <code class="docutils literal notranslate"><span class="pre">1</span></code>. If
  539. <em>initsigs</em> is <code class="docutils literal notranslate"><span class="pre">0</span></code>, it skips initialization registration of signal handlers, which
  540. might be useful when Python is embedded.</p>
  541. <p>Use the <a class="reference internal" href="init_config.html#c.Py_InitializeFromConfig" title="Py_InitializeFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_InitializeFromConfig()</span></code></a> function to customize the
  542. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  543. </dd></dl>
  544. <dl class="c function">
  545. <dt class="sig sig-object c" id="c.Py_IsInitialized">
  546. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_IsInitialized</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_IsInitialized" title="Link to this definition">¶</a><br /></dt>
  547. <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>Return true (nonzero) when the Python interpreter has been initialized, false
  548. (zero) if not. After <a class="reference internal" href="#c.Py_FinalizeEx" title="Py_FinalizeEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FinalizeEx()</span></code></a> is called, this returns false until
  549. <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> is called again.</p>
  550. </dd></dl>
  551. <dl class="c function">
  552. <dt class="sig sig-object c" id="c.Py_FinalizeEx">
  553. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_FinalizeEx</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_FinalizeEx" title="Link to this definition">¶</a><br /></dt>
  554. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.6.</em><p>Undo all initializations made by <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> and subsequent use of
  555. Python/C API functions, and destroy all sub-interpreters (see
  556. <a class="reference internal" href="#c.Py_NewInterpreter" title="Py_NewInterpreter"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_NewInterpreter()</span></code></a> below) that were created and not yet destroyed since
  557. the last call to <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>. Ideally, this frees all memory
  558. allocated by the Python interpreter. This is a no-op when called for a second
  559. time (without calling <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> again first). Normally the
  560. return value is <code class="docutils literal notranslate"><span class="pre">0</span></code>. If there were errors during finalization
  561. (flushing buffered data), <code class="docutils literal notranslate"><span class="pre">-1</span></code> is returned.</p>
  562. <p>This function is provided for a number of reasons. An embedding application
  563. might want to restart Python without having to restart the application itself.
  564. An application that has loaded the Python interpreter from a dynamically
  565. loadable library (or DLL) might want to free all memory allocated by Python
  566. before unloading the DLL. During a hunt for memory leaks in an application a
  567. developer might want to free all memory allocated by Python before exiting from
  568. the application.</p>
  569. <p><strong>Bugs and caveats:</strong> The destruction of modules and objects in modules is done
  570. in random order; this may cause destructors (<a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__del__()</span></code></a> methods) to fail
  571. when they depend on other objects (even functions) or modules. Dynamically
  572. loaded extension modules loaded by Python are not unloaded. Small amounts of
  573. memory allocated by the Python interpreter may not be freed (if you find a leak,
  574. please report it). Memory tied up in circular references between objects is not
  575. freed. Some memory allocated by extension modules may not be freed. Some
  576. extensions may not work properly if their initialization routine is called more
  577. than once; this can happen if an application calls <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> and
  578. <a class="reference internal" href="#c.Py_FinalizeEx" title="Py_FinalizeEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FinalizeEx()</span></code></a> more than once.</p>
  579. <p class="audit-hook">Raises an <a class="reference internal" href="../library/sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">cpython._PySys_ClearAuditHooks</span></code> with no arguments.</p>
  580. <div class="versionadded">
  581. <p><span class="versionmodified added">New in version 3.6.</span></p>
  582. </div>
  583. </dd></dl>
  584. <dl class="c function">
  585. <dt class="sig sig-object c" id="c.Py_Finalize">
  586. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_Finalize</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_Finalize" title="Link to this definition">¶</a><br /></dt>
  587. <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>This is a backwards-compatible version of <a class="reference internal" href="#c.Py_FinalizeEx" title="Py_FinalizeEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FinalizeEx()</span></code></a> that
  588. disregards the return value.</p>
  589. </dd></dl>
  590. </section>
  591. <section id="process-wide-parameters">
  592. <h2>Process-wide parameters<a class="headerlink" href="#process-wide-parameters" title="Link to this heading">¶</a></h2>
  593. <dl class="c function">
  594. <dt class="sig sig-object c" id="c.Py_SetStandardStreamEncoding">
  595. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_SetStandardStreamEncoding</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">encoding</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">errors</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_SetStandardStreamEncoding" title="Link to this definition">¶</a><br /></dt>
  596. <dd><p id="index-16">This API is kept for backward compatibility: setting
  597. <a class="reference internal" href="init_config.html#c.PyConfig.stdio_encoding" title="PyConfig.stdio_encoding"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.stdio_encoding</span></code></a> and <a class="reference internal" href="init_config.html#c.PyConfig.stdio_errors" title="PyConfig.stdio_errors"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.stdio_errors</span></code></a>
  598. should be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  599. <p>This function should be called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>, if it is
  600. called at all. It specifies which encoding and error handling to use
  601. with standard IO, with the same meanings as in <a class="reference internal" href="../library/stdtypes.html#str.encode" title="str.encode"><code class="xref py py-func docutils literal notranslate"><span class="pre">str.encode()</span></code></a>.</p>
  602. <p>It overrides <span class="target" id="index-17"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONIOENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONIOENCODING</span></code></a> values, and allows embedding code
  603. to control IO encoding when the environment variable does not work.</p>
  604. <p><em>encoding</em> and/or <em>errors</em> may be <code class="docutils literal notranslate"><span class="pre">NULL</span></code> to use
  605. <span class="target" id="index-18"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONIOENCODING"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONIOENCODING</span></code></a> and/or default values (depending on other
  606. settings).</p>
  607. <p>Note that <a class="reference internal" href="../library/sys.html#sys.stderr" title="sys.stderr"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stderr</span></code></a> always uses the “backslashreplace” error
  608. handler, regardless of this (or any other) setting.</p>
  609. <p>If <a class="reference internal" href="#c.Py_FinalizeEx" title="Py_FinalizeEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FinalizeEx()</span></code></a> is called, this function will need to be called
  610. again in order to affect subsequent calls to <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  611. <p>Returns <code class="docutils literal notranslate"><span class="pre">0</span></code> if successful, a nonzero value on error (e.g. calling after the
  612. interpreter has already been initialized).</p>
  613. <div class="versionadded">
  614. <p><span class="versionmodified added">New in version 3.4.</span></p>
  615. </div>
  616. <div class="deprecated">
  617. <p><span class="versionmodified deprecated">Deprecated since version 3.11.</span></p>
  618. </div>
  619. </dd></dl>
  620. <dl class="c function">
  621. <dt class="sig sig-object c" id="c.Py_SetProgramName">
  622. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_SetProgramName</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="n"><span class="pre">wchar_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_SetProgramName" title="Link to this definition">¶</a><br /></dt>
  623. <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 id="index-19">This API is kept for backward compatibility: setting
  624. <a class="reference internal" href="init_config.html#c.PyConfig.program_name" title="PyConfig.program_name"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.program_name</span></code></a> should be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python
  625. Initialization Configuration</span></a>.</p>
  626. <p>This function should be called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> is called for
  627. the first time, if it is called at all. It tells the interpreter the value
  628. of the <code class="docutils literal notranslate"><span class="pre">argv[0]</span></code> argument to the <code class="xref c c-func docutils literal notranslate"><span class="pre">main()</span></code> function of the program
  629. (converted to wide characters).
  630. This is used by <a class="reference internal" href="#c.Py_GetPath" title="Py_GetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPath()</span></code></a> and some other functions below to find
  631. the Python run-time libraries relative to the interpreter executable. The
  632. default value is <code class="docutils literal notranslate"><span class="pre">'python'</span></code>. The argument should point to a
  633. zero-terminated wide character string in static storage whose contents will not
  634. change for the duration of the program’s execution. No code in the Python
  635. interpreter will change the contents of this storage.</p>
  636. <p>Use <a class="reference internal" href="sys.html#c.Py_DecodeLocale" title="Py_DecodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code></a> to decode a bytes string to get a
  637. <span class="c-expr sig sig-inline c"><span class="n">wchar_t</span><span class="p">*</span></span> string.</p>
  638. <div class="deprecated">
  639. <p><span class="versionmodified deprecated">Deprecated since version 3.11.</span></p>
  640. </div>
  641. </dd></dl>
  642. <dl class="c function">
  643. <dt class="sig sig-object c" id="c.Py_GetProgramName">
  644. <span class="n"><span class="pre">wchar_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetProgramName</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetProgramName" title="Link to this definition">¶</a><br /></dt>
  645. <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 id="index-20">Return the program name set with <a class="reference internal" href="#c.Py_SetProgramName" title="Py_SetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code></a>, or the default.
  646. The returned string points into static storage; the caller should not modify its
  647. value.</p>
  648. <p>This function should not be called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>, otherwise
  649. it returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  650. <div class="versionchanged">
  651. <p><span class="versionmodified changed">Changed in version 3.10: </span>It now returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  652. </div>
  653. </dd></dl>
  654. <dl class="c function">
  655. <dt class="sig sig-object c" id="c.Py_GetPrefix">
  656. <span class="n"><span class="pre">wchar_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetPrefix</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetPrefix" title="Link to this definition">¶</a><br /></dt>
  657. <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>Return the <em>prefix</em> for installed platform-independent files. This is derived
  658. through a number of complicated rules from the program name set with
  659. <a class="reference internal" href="#c.Py_SetProgramName" title="Py_SetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code></a> and some environment variables; for example, if the
  660. program name is <code class="docutils literal notranslate"><span class="pre">'/usr/local/bin/python'</span></code>, the prefix is <code class="docutils literal notranslate"><span class="pre">'/usr/local'</span></code>. The
  661. returned string points into static storage; the caller should not modify its
  662. value. This corresponds to the <strong class="makevar">prefix</strong> variable in the top-level
  663. <code class="file docutils literal notranslate"><span class="pre">Makefile</span></code> and the <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> argument to the <strong class="program">configure</strong>
  664. script at build time. The value is available to Python code as <code class="docutils literal notranslate"><span class="pre">sys.prefix</span></code>.
  665. It is only useful on Unix. See also the next function.</p>
  666. <p>This function should not be called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>, otherwise
  667. it returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  668. <div class="versionchanged">
  669. <p><span class="versionmodified changed">Changed in version 3.10: </span>It now returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  670. </div>
  671. </dd></dl>
  672. <dl class="c function">
  673. <dt class="sig sig-object c" id="c.Py_GetExecPrefix">
  674. <span class="n"><span class="pre">wchar_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetExecPrefix</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetExecPrefix" title="Link to this definition">¶</a><br /></dt>
  675. <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>Return the <em>exec-prefix</em> for installed platform-<em>dependent</em> files. This is
  676. derived through a number of complicated rules from the program name set with
  677. <a class="reference internal" href="#c.Py_SetProgramName" title="Py_SetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code></a> and some environment variables; for example, if the
  678. program name is <code class="docutils literal notranslate"><span class="pre">'/usr/local/bin/python'</span></code>, the exec-prefix is
  679. <code class="docutils literal notranslate"><span class="pre">'/usr/local'</span></code>. The returned string points into static storage; the caller
  680. should not modify its value. This corresponds to the <strong class="makevar">exec_prefix</strong>
  681. variable in the top-level <code class="file docutils literal notranslate"><span class="pre">Makefile</span></code> and the <code class="docutils literal notranslate"><span class="pre">--exec-prefix</span></code>
  682. argument to the <strong class="program">configure</strong> script at build time. The value is
  683. available to Python code as <code class="docutils literal notranslate"><span class="pre">sys.exec_prefix</span></code>. It is only useful on Unix.</p>
  684. <p>Background: The exec-prefix differs from the prefix when platform dependent
  685. files (such as executables and shared libraries) are installed in a different
  686. directory tree. In a typical installation, platform dependent files may be
  687. installed in the <code class="file docutils literal notranslate"><span class="pre">/usr/local/plat</span></code> subtree while platform independent may
  688. be installed in <code class="file docutils literal notranslate"><span class="pre">/usr/local</span></code>.</p>
  689. <p>Generally speaking, a platform is a combination of hardware and software
  690. families, e.g. Sparc machines running the Solaris 2.x operating system are
  691. considered the same platform, but Intel machines running Solaris 2.x are another
  692. platform, and Intel machines running Linux are yet another platform. Different
  693. major revisions of the same operating system generally also form different
  694. platforms. Non-Unix operating systems are a different story; the installation
  695. strategies on those systems are so different that the prefix and exec-prefix are
  696. meaningless, and set to the empty string. Note that compiled Python bytecode
  697. files are platform independent (but not independent from the Python version by
  698. which they were compiled!).</p>
  699. <p>System administrators will know how to configure the <strong class="program">mount</strong> or
  700. <strong class="program">automount</strong> programs to share <code class="file docutils literal notranslate"><span class="pre">/usr/local</span></code> between platforms
  701. while having <code class="file docutils literal notranslate"><span class="pre">/usr/local/plat</span></code> be a different filesystem for each
  702. platform.</p>
  703. <p>This function should not be called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>, otherwise
  704. it returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  705. <div class="versionchanged">
  706. <p><span class="versionmodified changed">Changed in version 3.10: </span>It now returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  707. </div>
  708. </dd></dl>
  709. <dl class="c function">
  710. <dt class="sig sig-object c" id="c.Py_GetProgramFullPath">
  711. <span class="n"><span class="pre">wchar_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetProgramFullPath</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetProgramFullPath" title="Link to this definition">¶</a><br /></dt>
  712. <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 id="index-21">Return the full program name of the Python executable; this is computed as a
  713. side-effect of deriving the default module search path from the program name
  714. (set by <a class="reference internal" href="#c.Py_SetProgramName" title="Py_SetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code></a> above). The returned string points into
  715. static storage; the caller should not modify its value. The value is available
  716. to Python code as <code class="docutils literal notranslate"><span class="pre">sys.executable</span></code>.</p>
  717. <p>This function should not be called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>, otherwise
  718. it returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  719. <div class="versionchanged">
  720. <p><span class="versionmodified changed">Changed in version 3.10: </span>It now returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  721. </div>
  722. </dd></dl>
  723. <dl class="c function">
  724. <dt class="sig sig-object c" id="c.Py_GetPath">
  725. <span class="n"><span class="pre">wchar_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetPath</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetPath" title="Link to this definition">¶</a><br /></dt>
  726. <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 id="index-22">Return the default module search path; this is computed from the program name
  727. (set by <a class="reference internal" href="#c.Py_SetProgramName" title="Py_SetProgramName"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetProgramName()</span></code></a> above) and some environment variables.
  728. The returned string consists of a series of directory names separated by a
  729. platform dependent delimiter character. The delimiter character is <code class="docutils literal notranslate"><span class="pre">':'</span></code>
  730. on Unix and macOS, <code class="docutils literal notranslate"><span class="pre">';'</span></code> on Windows. The returned string points into
  731. static storage; the caller should not modify its value. The list
  732. <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> is initialized with this value on interpreter startup; it
  733. can be (and usually is) modified later to change the search path for loading
  734. modules.</p>
  735. <p>This function should not be called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>, otherwise
  736. it returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  737. <div class="versionchanged">
  738. <p><span class="versionmodified changed">Changed in version 3.10: </span>It now returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  739. </div>
  740. </dd></dl>
  741. <dl class="c function">
  742. <dt class="sig sig-object c" id="c.Py_SetPath">
  743. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_SetPath</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="n"><span class="pre">wchar_t</span></span><span class="p"><span class="pre">*</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_SetPath" title="Link to this definition">¶</a><br /></dt>
  744. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p id="index-23">This API is kept for backward compatibility: setting
  745. <a class="reference internal" href="init_config.html#c.PyConfig.module_search_paths" title="PyConfig.module_search_paths"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.module_search_paths</span></code></a> and
  746. <a class="reference internal" href="init_config.html#c.PyConfig.module_search_paths_set" title="PyConfig.module_search_paths_set"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.module_search_paths_set</span></code></a> should be used instead, see
  747. <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  748. <p>Set the default module search path. If this function is called before
  749. <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>, then <a class="reference internal" href="#c.Py_GetPath" title="Py_GetPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetPath()</span></code></a> won’t attempt to compute a
  750. default search path but uses the one provided instead. This is useful if
  751. Python is embedded by an application that has full knowledge of the location
  752. of all modules. The path components should be separated by the platform
  753. dependent delimiter character, which is <code class="docutils literal notranslate"><span class="pre">':'</span></code> on Unix and macOS, <code class="docutils literal notranslate"><span class="pre">';'</span></code>
  754. on Windows.</p>
  755. <p>This also causes <a class="reference internal" href="../library/sys.html#sys.executable" title="sys.executable"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.executable</span></code></a> to be set to the program
  756. full path (see <a class="reference internal" href="#c.Py_GetProgramFullPath" title="Py_GetProgramFullPath"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_GetProgramFullPath()</span></code></a>) and for <a class="reference internal" href="../library/sys.html#sys.prefix" title="sys.prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.prefix</span></code></a> and
  757. <a class="reference internal" href="../library/sys.html#sys.exec_prefix" title="sys.exec_prefix"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.exec_prefix</span></code></a> to be empty. It is up to the caller to modify these
  758. if required after calling <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  759. <p>Use <a class="reference internal" href="sys.html#c.Py_DecodeLocale" title="Py_DecodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code></a> to decode a bytes string to get a
  760. <span class="c-expr sig sig-inline c"><span class="n">wchar_</span><span class="p">*</span></span> string.</p>
  761. <p>The path argument is copied internally, so the caller may free it after the
  762. call completes.</p>
  763. <div class="versionchanged">
  764. <p><span class="versionmodified changed">Changed in version 3.8: </span>The program full path is now used for <a class="reference internal" href="../library/sys.html#sys.executable" title="sys.executable"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.executable</span></code></a>, instead
  765. of the program name.</p>
  766. </div>
  767. <div class="deprecated">
  768. <p><span class="versionmodified deprecated">Deprecated since version 3.11.</span></p>
  769. </div>
  770. </dd></dl>
  771. <dl class="c function">
  772. <dt class="sig sig-object c" id="c.Py_GetVersion">
  773. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetVersion</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetVersion" title="Link to this definition">¶</a><br /></dt>
  774. <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>Return the version of this Python interpreter. This is a string that looks
  775. something like</p>
  776. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="s">&quot;3.0a5+ (py3k:63103M, May 12 2008, 00:53:55) </span><span class="se">\n</span><span class="s">[GCC 4.2.3]&quot;</span>
  777. </pre></div>
  778. </div>
  779. <p id="index-24">The first word (up to the first space character) is the current Python version;
  780. the first characters are the major and minor version separated by a
  781. period. The returned string points into static storage; the caller should not
  782. modify its value. The value is available to Python code as <a class="reference internal" href="../library/sys.html#sys.version" title="sys.version"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.version</span></code></a>.</p>
  783. <p>See also the <a class="reference internal" href="apiabiversion.html#c.Py_Version" title="Py_Version"><code class="xref c c-var docutils literal notranslate"><span class="pre">Py_Version</span></code></a> constant.</p>
  784. </dd></dl>
  785. <dl class="c function">
  786. <dt class="sig sig-object c" id="c.Py_GetPlatform">
  787. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetPlatform</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetPlatform" title="Link to this definition">¶</a><br /></dt>
  788. <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 id="index-25">Return the platform identifier for the current platform. On Unix, this is
  789. formed from the “official” name of the operating system, converted to lower
  790. case, followed by the major revision number; e.g., for Solaris 2.x, which is
  791. also known as SunOS 5.x, the value is <code class="docutils literal notranslate"><span class="pre">'sunos5'</span></code>. On macOS, it is
  792. <code class="docutils literal notranslate"><span class="pre">'darwin'</span></code>. On Windows, it is <code class="docutils literal notranslate"><span class="pre">'win'</span></code>. The returned string points into
  793. static storage; the caller should not modify its value. The value is available
  794. to Python code as <code class="docutils literal notranslate"><span class="pre">sys.platform</span></code>.</p>
  795. </dd></dl>
  796. <dl class="c function">
  797. <dt class="sig sig-object c" id="c.Py_GetCopyright">
  798. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetCopyright</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetCopyright" title="Link to this definition">¶</a><br /></dt>
  799. <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>Return the official copyright string for the current Python version, for example</p>
  800. <p><code class="docutils literal notranslate"><span class="pre">'Copyright</span> <span class="pre">1991-1995</span> <span class="pre">Stichting</span> <span class="pre">Mathematisch</span> <span class="pre">Centrum,</span> <span class="pre">Amsterdam'</span></code></p>
  801. <p id="index-26">The returned string points into static storage; the caller should not modify its
  802. value. The value is available to Python code as <code class="docutils literal notranslate"><span class="pre">sys.copyright</span></code>.</p>
  803. </dd></dl>
  804. <dl class="c function">
  805. <dt class="sig sig-object c" id="c.Py_GetCompiler">
  806. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetCompiler</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetCompiler" title="Link to this definition">¶</a><br /></dt>
  807. <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>Return an indication of the compiler used to build the current Python version,
  808. in square brackets, for example:</p>
  809. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="s">&quot;[GCC 2.7.2.2]&quot;</span>
  810. </pre></div>
  811. </div>
  812. <p id="index-27">The returned string points into static storage; the caller should not modify its
  813. value. The value is available to Python code as part of the variable
  814. <code class="docutils literal notranslate"><span class="pre">sys.version</span></code>.</p>
  815. </dd></dl>
  816. <dl class="c function">
  817. <dt class="sig sig-object c" id="c.Py_GetBuildInfo">
  818. <span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="kt"><span class="pre">char</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetBuildInfo</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetBuildInfo" title="Link to this definition">¶</a><br /></dt>
  819. <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>Return information about the sequence number and build date and time of the
  820. current Python interpreter instance, for example</p>
  821. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="s">&quot;#67, Aug 1 1997, 22:34:28&quot;</span>
  822. </pre></div>
  823. </div>
  824. <p id="index-28">The returned string points into static storage; the caller should not modify its
  825. value. The value is available to Python code as part of the variable
  826. <code class="docutils literal notranslate"><span class="pre">sys.version</span></code>.</p>
  827. </dd></dl>
  828. <dl class="c function">
  829. <dt class="sig sig-object c" id="c.PySys_SetArgvEx">
  830. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PySys_SetArgvEx</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">argc</span></span>, <span class="n"><span class="pre">wchar_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">argv</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">updatepath</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PySys_SetArgvEx" title="Link to this definition">¶</a><br /></dt>
  831. <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 id="index-29">This API is kept for backward compatibility: setting
  832. <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>, <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> and
  833. <a class="reference internal" href="init_config.html#c.PyConfig.safe_path" title="PyConfig.safe_path"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.safe_path</span></code></a> should be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python
  834. Initialization Configuration</span></a>.</p>
  835. <p>Set <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> based on <em>argc</em> and <em>argv</em>. These parameters are
  836. similar to those passed to the program’s <code class="xref c c-func docutils literal notranslate"><span class="pre">main()</span></code> function with the
  837. difference that the first entry should refer to the script file to be
  838. executed rather than the executable hosting the Python interpreter. If there
  839. isn’t a script that will be run, the first entry in <em>argv</em> can be an empty
  840. string. If this function fails to initialize <a class="reference internal" href="../library/sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a>, a fatal
  841. condition is signalled using <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>.</p>
  842. <p>If <em>updatepath</em> is zero, this is all the function does. If <em>updatepath</em>
  843. is non-zero, the function also modifies <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> according to the
  844. following algorithm:</p>
  845. <ul class="simple">
  846. <li><p>If the name of an existing script is passed in <code class="docutils literal notranslate"><span class="pre">argv[0]</span></code>, the absolute
  847. path of the directory where the script is located is prepended to
  848. <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>.</p></li>
  849. <li><p>Otherwise (that is, if <em>argc</em> is <code class="docutils literal notranslate"><span class="pre">0</span></code> or <code class="docutils literal notranslate"><span class="pre">argv[0]</span></code> doesn’t point
  850. to an existing file name), an empty string is prepended to
  851. <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a>, which is the same as prepending the current working
  852. directory (<code class="docutils literal notranslate"><span class="pre">&quot;.&quot;</span></code>).</p></li>
  853. </ul>
  854. <p>Use <a class="reference internal" href="sys.html#c.Py_DecodeLocale" title="Py_DecodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code></a> to decode a bytes string to get a
  855. <span class="c-expr sig sig-inline c"><span class="n">wchar_</span><span class="p">*</span></span> string.</p>
  856. <p>See also <a class="reference internal" href="init_config.html#c.PyConfig.orig_argv" title="PyConfig.orig_argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.orig_argv</span></code></a> and <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>
  857. members of the <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  858. <div class="admonition note">
  859. <p class="admonition-title">Note</p>
  860. <p>It is recommended that applications embedding the Python interpreter
  861. for purposes other than executing a single script pass <code class="docutils literal notranslate"><span class="pre">0</span></code> as <em>updatepath</em>,
  862. and update <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> themselves if desired.
  863. See <a class="reference external" href="https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983">CVE-2008-5983</a>.</p>
  864. <p>On versions before 3.1.3, you can achieve the same effect by manually
  865. popping the first <a class="reference internal" href="../library/sys.html#sys.path" title="sys.path"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.path</span></code></a> element after having called
  866. <a class="reference internal" href="#c.PySys_SetArgv" title="PySys_SetArgv"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgv()</span></code></a>, for example using:</p>
  867. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyRun_SimpleString</span><span class="p">(</span><span class="s">&quot;import sys; sys.path.pop(0)</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
  868. </pre></div>
  869. </div>
  870. </div>
  871. <div class="versionadded">
  872. <p><span class="versionmodified added">New in version 3.1.3.</span></p>
  873. </div>
  874. <div class="deprecated">
  875. <p><span class="versionmodified deprecated">Deprecated since version 3.11.</span></p>
  876. </div>
  877. </dd></dl>
  878. <dl class="c function">
  879. <dt class="sig sig-object c" id="c.PySys_SetArgv">
  880. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PySys_SetArgv</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">argc</span></span>, <span class="n"><span class="pre">wchar_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">argv</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PySys_SetArgv" title="Link to this definition">¶</a><br /></dt>
  881. <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>This API is kept for backward compatibility: setting
  882. <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> should be used
  883. instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  884. <p>This function works like <a class="reference internal" href="#c.PySys_SetArgvEx" title="PySys_SetArgvEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">PySys_SetArgvEx()</span></code></a> with <em>updatepath</em> set
  885. to <code class="docutils literal notranslate"><span class="pre">1</span></code> unless the <strong class="program">python</strong> interpreter was started with the
  886. <a class="reference internal" href="../using/cmdline.html#cmdoption-I"><code class="xref std std-option docutils literal notranslate"><span class="pre">-I</span></code></a>.</p>
  887. <p>Use <a class="reference internal" href="sys.html#c.Py_DecodeLocale" title="Py_DecodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code></a> to decode a bytes string to get a
  888. <span class="c-expr sig sig-inline c"><span class="n">wchar_</span><span class="p">*</span></span> string.</p>
  889. <p>See also <a class="reference internal" href="init_config.html#c.PyConfig.orig_argv" title="PyConfig.orig_argv"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.orig_argv</span></code></a> and <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>
  890. members of the <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python Initialization Configuration</span></a>.</p>
  891. <div class="versionchanged">
  892. <p><span class="versionmodified changed">Changed in version 3.4: </span>The <em>updatepath</em> value depends on <a class="reference internal" href="../using/cmdline.html#cmdoption-I"><code class="xref std std-option docutils literal notranslate"><span class="pre">-I</span></code></a>.</p>
  893. </div>
  894. <div class="deprecated">
  895. <p><span class="versionmodified deprecated">Deprecated since version 3.11.</span></p>
  896. </div>
  897. </dd></dl>
  898. <dl class="c function">
  899. <dt class="sig sig-object c" id="c.Py_SetPythonHome">
  900. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_SetPythonHome</span></span></span><span class="sig-paren">(</span><span class="k"><span class="pre">const</span></span><span class="w"> </span><span class="n"><span class="pre">wchar_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">home</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_SetPythonHome" title="Link to this definition">¶</a><br /></dt>
  901. <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>This API is kept for backward compatibility: setting
  902. <a class="reference internal" href="init_config.html#c.PyConfig.home" title="PyConfig.home"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyConfig.home</span></code></a> should be used instead, see <a class="reference internal" href="init_config.html#init-config"><span class="std std-ref">Python
  903. Initialization Configuration</span></a>.</p>
  904. <p>Set the default “home” directory, that is, the location of the standard
  905. Python libraries. See <span class="target" id="index-30"></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> for the meaning of the
  906. argument string.</p>
  907. <p>The argument should point to a zero-terminated character string in static
  908. storage whose contents will not change for the duration of the program’s
  909. execution. No code in the Python interpreter will change the contents of
  910. this storage.</p>
  911. <p>Use <a class="reference internal" href="sys.html#c.Py_DecodeLocale" title="Py_DecodeLocale"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_DecodeLocale()</span></code></a> to decode a bytes string to get a
  912. <span class="c-expr sig sig-inline c"><span class="n">wchar_</span><span class="p">*</span></span> string.</p>
  913. <div class="deprecated">
  914. <p><span class="versionmodified deprecated">Deprecated since version 3.11.</span></p>
  915. </div>
  916. </dd></dl>
  917. <dl class="c function">
  918. <dt class="sig sig-object c" id="c.Py_GetPythonHome">
  919. <span class="n"><span class="pre">wchar_t</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_GetPythonHome</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_GetPythonHome" title="Link to this definition">¶</a><br /></dt>
  920. <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>Return the default “home”, that is, the value set by a previous call to
  921. <a class="reference internal" href="#c.Py_SetPythonHome" title="Py_SetPythonHome"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_SetPythonHome()</span></code></a>, or the value of the <span class="target" id="index-31"></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>
  922. environment variable if it is set.</p>
  923. <p>This function should not be called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>, otherwise
  924. it returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  925. <div class="versionchanged">
  926. <p><span class="versionmodified changed">Changed in version 3.10: </span>It now returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  927. </div>
  928. </dd></dl>
  929. </section>
  930. <section id="thread-state-and-the-global-interpreter-lock">
  931. <span id="threads"></span><h2>Thread State and the Global Interpreter Lock<a class="headerlink" href="#thread-state-and-the-global-interpreter-lock" title="Link to this heading">¶</a></h2>
  932. <p id="index-32">The Python interpreter is not fully thread-safe. In order to support
  933. multi-threaded Python programs, there’s a global lock, called the <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">global
  934. interpreter lock</span></a> or <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a>, that must be held by the current thread before
  935. it can safely access Python objects. Without the lock, even the simplest
  936. operations could cause problems in a multi-threaded program: for example, when
  937. two threads simultaneously increment the reference count of the same object, the
  938. reference count could end up being incremented only once instead of twice.</p>
  939. <p id="index-33">Therefore, the rule exists that only the thread that has acquired the
  940. <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a> may operate on Python objects or call Python/C API functions.
  941. In order to emulate concurrency of execution, the interpreter regularly
  942. tries to switch threads (see <a class="reference internal" href="../library/sys.html#sys.setswitchinterval" title="sys.setswitchinterval"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setswitchinterval()</span></code></a>). The lock is also
  943. released around potentially blocking I/O operations like reading or writing
  944. a file, so that other Python threads can run in the meantime.</p>
  945. <p id="index-34">The Python interpreter keeps some thread-specific bookkeeping information
  946. inside a data structure called <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyThreadState</span></code></a>. There’s also one
  947. global variable pointing to the current <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyThreadState</span></code></a>: it can
  948. be retrieved using <a class="reference internal" href="#c.PyThreadState_Get" title="PyThreadState_Get"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThreadState_Get()</span></code></a>.</p>
  949. <section id="releasing-the-gil-from-extension-code">
  950. <h3>Releasing the GIL from extension code<a class="headerlink" href="#releasing-the-gil-from-extension-code" title="Link to this heading">¶</a></h3>
  951. <p>Most extension code manipulating the <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a> has the following simple
  952. structure:</p>
  953. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Save</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="kr">thread</span><span class="w"> </span><span class="n">state</span><span class="w"> </span><span class="n">in</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">local</span><span class="w"> </span><span class="n">variable</span><span class="p">.</span>
  954. <span class="n">Release</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">global</span><span class="w"> </span><span class="n">interpreter</span><span class="w"> </span><span class="n">lock</span><span class="p">.</span>
  955. <span class="p">...</span><span class="w"> </span><span class="n">Do</span><span class="w"> </span><span class="n">some</span><span class="w"> </span><span class="n">blocking</span><span class="w"> </span><span class="n">I</span><span class="o">/</span><span class="n">O</span><span class="w"> </span><span class="n">operation</span><span class="w"> </span><span class="p">...</span>
  956. <span class="n">Reacquire</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">global</span><span class="w"> </span><span class="n">interpreter</span><span class="w"> </span><span class="n">lock</span><span class="p">.</span>
  957. <span class="n">Restore</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="kr">thread</span><span class="w"> </span><span class="n">state</span><span class="w"> </span><span class="n">from</span><span class="w"> </span><span class="n">the</span><span class="w"> </span><span class="n">local</span><span class="w"> </span><span class="n">variable</span><span class="p">.</span>
  958. </pre></div>
  959. </div>
  960. <p>This is so common that a pair of macros exists to simplify it:</p>
  961. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">Py_BEGIN_ALLOW_THREADS</span>
  962. <span class="p">...</span><span class="w"> </span><span class="n">Do</span><span class="w"> </span><span class="n">some</span><span class="w"> </span><span class="n">blocking</span><span class="w"> </span><span class="n">I</span><span class="o">/</span><span class="n">O</span><span class="w"> </span><span class="n">operation</span><span class="w"> </span><span class="p">...</span>
  963. <span class="n">Py_END_ALLOW_THREADS</span>
  964. </pre></div>
  965. </div>
  966. <p id="index-35">The <a class="reference internal" href="#c.Py_BEGIN_ALLOW_THREADS" title="Py_BEGIN_ALLOW_THREADS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_BEGIN_ALLOW_THREADS</span></code></a> macro opens a new block and declares a
  967. hidden local variable; the <a class="reference internal" href="#c.Py_END_ALLOW_THREADS" title="Py_END_ALLOW_THREADS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_END_ALLOW_THREADS</span></code></a> macro closes the
  968. block.</p>
  969. <p>The block above expands to the following code:</p>
  970. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyThreadState</span><span class="w"> </span><span class="o">*</span><span class="n">_save</span><span class="p">;</span>
  971. <span class="n">_save</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyEval_SaveThread</span><span class="p">();</span>
  972. <span class="p">...</span><span class="w"> </span><span class="n">Do</span><span class="w"> </span><span class="n">some</span><span class="w"> </span><span class="n">blocking</span><span class="w"> </span><span class="n">I</span><span class="o">/</span><span class="n">O</span><span class="w"> </span><span class="n">operation</span><span class="w"> </span><span class="p">...</span>
  973. <span class="n">PyEval_RestoreThread</span><span class="p">(</span><span class="n">_save</span><span class="p">);</span>
  974. </pre></div>
  975. </div>
  976. <p id="index-36">Here is how these functions work: the global interpreter lock is used to protect the pointer to the
  977. current thread state. When releasing the lock and saving the thread state,
  978. the current thread state pointer must be retrieved before the lock is released
  979. (since another thread could immediately acquire the lock and store its own thread
  980. state in the global variable). Conversely, when acquiring the lock and restoring
  981. the thread state, the lock must be acquired before storing the thread state
  982. pointer.</p>
  983. <div class="admonition note">
  984. <p class="admonition-title">Note</p>
  985. <p>Calling system I/O functions is the most common use case for releasing
  986. the GIL, but it can also be useful before calling long-running computations
  987. which don’t need access to Python objects, such as compression or
  988. cryptographic functions operating over memory buffers. For example, the
  989. standard <a class="reference internal" href="../library/zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> and <a class="reference internal" href="../library/hashlib.html#module-hashlib" title="hashlib: Secure hash and message digest algorithms."><code class="xref py py-mod docutils literal notranslate"><span class="pre">hashlib</span></code></a> modules release the GIL when
  990. compressing or hashing data.</p>
  991. </div>
  992. </section>
  993. <section id="non-python-created-threads">
  994. <span id="gilstate"></span><h3>Non-Python created threads<a class="headerlink" href="#non-python-created-threads" title="Link to this heading">¶</a></h3>
  995. <p>When threads are created using the dedicated Python APIs (such as the
  996. <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module), a thread state is automatically associated to them
  997. and the code showed above is therefore correct. However, when threads are
  998. created from C (for example by a third-party library with its own thread
  999. management), they don’t hold the GIL, nor is there a thread state structure
  1000. for them.</p>
  1001. <p>If you need to call Python code from these threads (often this will be part
  1002. of a callback API provided by the aforementioned third-party library),
  1003. you must first register these threads with the interpreter by
  1004. creating a thread state data structure, then acquiring the GIL, and finally
  1005. storing their thread state pointer, before you can start using the Python/C
  1006. API. When you are done, you should reset the thread state pointer, release
  1007. the GIL, and finally free the thread state data structure.</p>
  1008. <p>The <a class="reference internal" href="#c.PyGILState_Ensure" title="PyGILState_Ensure"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Ensure()</span></code></a> and <a class="reference internal" href="#c.PyGILState_Release" title="PyGILState_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Release()</span></code></a> functions do
  1009. all of the above automatically. The typical idiom for calling into Python
  1010. from a C thread is:</p>
  1011. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyGILState_STATE</span><span class="w"> </span><span class="n">gstate</span><span class="p">;</span>
  1012. <span class="n">gstate</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyGILState_Ensure</span><span class="p">();</span>
  1013. <span class="cm">/* Perform Python actions here. */</span>
  1014. <span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">CallSomeFunction</span><span class="p">();</span>
  1015. <span class="cm">/* evaluate result or handle exception */</span>
  1016. <span class="cm">/* Release the thread. No Python API allowed beyond this point. */</span>
  1017. <span class="n">PyGILState_Release</span><span class="p">(</span><span class="n">gstate</span><span class="p">);</span>
  1018. </pre></div>
  1019. </div>
  1020. <p>Note that the <code class="docutils literal notranslate"><span class="pre">PyGILState_*</span></code> functions assume there is only one global
  1021. interpreter (created automatically by <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>). Python
  1022. supports the creation of additional interpreters (using
  1023. <a class="reference internal" href="#c.Py_NewInterpreter" title="Py_NewInterpreter"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_NewInterpreter()</span></code></a>), but mixing multiple interpreters and the
  1024. <code class="docutils literal notranslate"><span class="pre">PyGILState_*</span></code> API is unsupported.</p>
  1025. </section>
  1026. <section id="cautions-about-fork">
  1027. <span id="fork-and-threads"></span><h3>Cautions about fork()<a class="headerlink" href="#cautions-about-fork" title="Link to this heading">¶</a></h3>
  1028. <p>Another important thing to note about threads is their behaviour in the face
  1029. of the C <code class="xref c c-func docutils literal notranslate"><span class="pre">fork()</span></code> call. On most systems with <code class="xref c c-func docutils literal notranslate"><span class="pre">fork()</span></code>, after a
  1030. process forks only the thread that issued the fork will exist. This has a
  1031. concrete impact both on how locks must be handled and on all stored state
  1032. in CPython’s runtime.</p>
  1033. <p>The fact that only the “current” thread remains
  1034. means any locks held by other threads will never be released. Python solves
  1035. this for <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> by acquiring the locks it uses internally before
  1036. the fork, and releasing them afterwards. In addition, it resets any
  1037. <a class="reference internal" href="../library/threading.html#lock-objects"><span class="std std-ref">Lock Objects</span></a> in the child. When extending or embedding Python, there
  1038. is no way to inform Python of additional (non-Python) locks that need to be
  1039. acquired before or reset after a fork. OS facilities such as
  1040. <code class="xref c c-func docutils literal notranslate"><span class="pre">pthread_atfork()</span></code> would need to be used to accomplish the same thing.
  1041. Additionally, when extending or embedding Python, calling <code class="xref c c-func docutils literal notranslate"><span class="pre">fork()</span></code>
  1042. directly rather than through <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a> (and returning to or calling
  1043. into Python) may result in a deadlock by one of Python’s internal locks
  1044. being held by a thread that is defunct after the fork.
  1045. <a class="reference internal" href="sys.html#c.PyOS_AfterFork_Child" title="PyOS_AfterFork_Child"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyOS_AfterFork_Child()</span></code></a> tries to reset the necessary locks, but is not
  1046. always able to.</p>
  1047. <p>The fact that all other threads go away also means that CPython’s
  1048. runtime state there must be cleaned up properly, which <a class="reference internal" href="../library/os.html#os.fork" title="os.fork"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fork()</span></code></a>
  1049. does. This means finalizing all other <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyThreadState</span></code></a> objects
  1050. belonging to the current interpreter and all other
  1051. <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyInterpreterState</span></code></a> objects. Due to this and the special
  1052. nature of the <a class="reference internal" href="#sub-interpreter-support"><span class="std std-ref">“main” interpreter</span></a>,
  1053. <code class="xref c c-func docutils literal notranslate"><span class="pre">fork()</span></code> should only be called in that interpreter’s “main”
  1054. thread, where the CPython global runtime was originally initialized.
  1055. The only exception is if <code class="xref c c-func docutils literal notranslate"><span class="pre">exec()</span></code> will be called immediately
  1056. after.</p>
  1057. </section>
  1058. <section id="high-level-api">
  1059. <h3>High-level API<a class="headerlink" href="#high-level-api" title="Link to this heading">¶</a></h3>
  1060. <p>These are the most commonly used types and functions when writing C extension
  1061. code, or when embedding the Python interpreter:</p>
  1062. <dl class="c type">
  1063. <dt class="sig sig-object c" id="c.PyInterpreterState">
  1064. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState</span></span></span><a class="headerlink" href="#c.PyInterpreterState" title="Link to this definition">¶</a><br /></dt>
  1065. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Limited API</span></a> (as an opaque struct).</em><p>This data structure represents the state shared by a number of cooperating
  1066. threads. Threads belonging to the same interpreter share their module
  1067. administration and a few other internal items. There are no public members in
  1068. this structure.</p>
  1069. <p>Threads belonging to different interpreters initially share nothing, except
  1070. process state like available memory, open file descriptors and such. The global
  1071. interpreter lock is also shared by all threads, regardless of to which
  1072. interpreter they belong.</p>
  1073. </dd></dl>
  1074. <dl class="c type">
  1075. <dt class="sig sig-object c" id="c.PyThreadState">
  1076. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState</span></span></span><a class="headerlink" href="#c.PyThreadState" title="Link to this definition">¶</a><br /></dt>
  1077. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Limited API</span></a> (as an opaque struct).</em><p>This data structure represents the state of a single thread. The only public
  1078. data member is:</p>
  1079. <dl class="c member">
  1080. <dt class="sig sig-object c" id="c.PyThreadState.interp">
  1081. <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">interp</span></span></span><a class="headerlink" href="#c.PyThreadState.interp" title="Link to this definition">¶</a><br /></dt>
  1082. <dd><p>This thread’s interpreter state.</p>
  1083. </dd></dl>
  1084. </dd></dl>
  1085. <dl class="c function">
  1086. <dt class="sig sig-object c" id="c.PyEval_InitThreads">
  1087. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_InitThreads</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_InitThreads" title="Link to this definition">¶</a><br /></dt>
  1088. <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 id="index-37">Deprecated function which does nothing.</p>
  1089. <p>In Python 3.6 and older, this function created the GIL if it didn’t exist.</p>
  1090. <div class="versionchanged">
  1091. <p><span class="versionmodified changed">Changed in version 3.9: </span>The function now does nothing.</p>
  1092. </div>
  1093. <div class="versionchanged">
  1094. <p><span class="versionmodified changed">Changed in version 3.7: </span>This function is now called by <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>, so you don’t
  1095. have to call it yourself anymore.</p>
  1096. </div>
  1097. <div class="versionchanged">
  1098. <p><span class="versionmodified changed">Changed in version 3.2: </span>This function cannot be called before <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> anymore.</p>
  1099. </div>
  1100. <div class="deprecated">
  1101. <p><span class="versionmodified deprecated">Deprecated since version 3.9.</span></p>
  1102. </div>
  1103. <span class="target" id="index-38"></span></dd></dl>
  1104. <dl class="c function">
  1105. <dt class="sig sig-object c" id="c.PyEval_ThreadsInitialized">
  1106. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_ThreadsInitialized</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_ThreadsInitialized" title="Link to this definition">¶</a><br /></dt>
  1107. <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>Returns a non-zero value if <a class="reference internal" href="#c.PyEval_InitThreads" title="PyEval_InitThreads"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_InitThreads()</span></code></a> has been called. This
  1108. function can be called without holding the GIL, and therefore can be used to
  1109. avoid calls to the locking API when running single-threaded.</p>
  1110. <div class="versionchanged">
  1111. <p><span class="versionmodified changed">Changed in version 3.7: </span>The <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a> is now initialized by <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  1112. </div>
  1113. <div class="deprecated">
  1114. <p><span class="versionmodified deprecated">Deprecated since version 3.9.</span></p>
  1115. </div>
  1116. </dd></dl>
  1117. <dl class="c function">
  1118. <dt class="sig sig-object c" id="c.PyEval_SaveThread">
  1119. <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_SaveThread</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_SaveThread" title="Link to this definition">¶</a><br /></dt>
  1120. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Release the global interpreter lock (if it has been created) and reset the
  1121. thread state to <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, returning the previous thread state (which is not
  1122. <code class="docutils literal notranslate"><span class="pre">NULL</span></code>). If the lock has been created, the current thread must have
  1123. acquired it.</p>
  1124. </dd></dl>
  1125. <dl class="c function">
  1126. <dt class="sig sig-object c" id="c.PyEval_RestoreThread">
  1127. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_RestoreThread</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_RestoreThread" title="Link to this definition">¶</a><br /></dt>
  1128. <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>Acquire the global interpreter lock (if it has been created) and set the
  1129. thread state to <em>tstate</em>, which must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. If the lock has been
  1130. created, the current thread must not have acquired it, otherwise deadlock
  1131. ensues.</p>
  1132. <div class="admonition note">
  1133. <p class="admonition-title">Note</p>
  1134. <p>Calling this function from a thread when the runtime is finalizing
  1135. will terminate the thread, even if the thread was not created by Python.
  1136. You can use <code class="xref c c-func docutils literal notranslate"><span class="pre">_Py_IsFinalizing()</span></code> or <a class="reference internal" href="../library/sys.html#sys.is_finalizing" title="sys.is_finalizing"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.is_finalizing()</span></code></a> to
  1137. check if the interpreter is in process of being finalized before calling
  1138. this function to avoid unwanted termination.</p>
  1139. </div>
  1140. </dd></dl>
  1141. <dl class="c function">
  1142. <dt class="sig sig-object c" id="c.PyThreadState_Get">
  1143. <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_Get</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_Get" title="Link to this definition">¶</a><br /></dt>
  1144. <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>Return the current thread state. The global interpreter lock must be held.
  1145. When the current thread state is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, this issues a fatal error (so that
  1146. the caller needn’t check for <code class="docutils literal notranslate"><span class="pre">NULL</span></code>).</p>
  1147. </dd></dl>
  1148. <dl class="c function">
  1149. <dt class="sig sig-object c" id="c.PyThreadState_Swap">
  1150. <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_Swap</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_Swap" title="Link to this definition">¶</a><br /></dt>
  1151. <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>Swap the current thread state with the thread state given by the argument
  1152. <em>tstate</em>, which may be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. The global interpreter lock must be held
  1153. and is not released.</p>
  1154. </dd></dl>
  1155. <p>The following functions use thread-local storage, and are not compatible
  1156. with sub-interpreters:</p>
  1157. <dl class="c function">
  1158. <dt class="sig sig-object c" id="c.PyGILState_Ensure">
  1159. <span class="n"><span class="pre">PyGILState_STATE</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyGILState_Ensure</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyGILState_Ensure" title="Link to this definition">¶</a><br /></dt>
  1160. <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>Ensure that the current thread is ready to call the Python C API regardless
  1161. of the current state of Python, or of the global interpreter lock. This may
  1162. be called as many times as desired by a thread as long as each call is
  1163. matched with a call to <a class="reference internal" href="#c.PyGILState_Release" title="PyGILState_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Release()</span></code></a>. In general, other
  1164. thread-related APIs may be used between <a class="reference internal" href="#c.PyGILState_Ensure" title="PyGILState_Ensure"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Ensure()</span></code></a> and
  1165. <a class="reference internal" href="#c.PyGILState_Release" title="PyGILState_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Release()</span></code></a> calls as long as the thread state is restored to
  1166. its previous state before the Release(). For example, normal usage of the
  1167. <a class="reference internal" href="#c.Py_BEGIN_ALLOW_THREADS" title="Py_BEGIN_ALLOW_THREADS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_BEGIN_ALLOW_THREADS</span></code></a> and <a class="reference internal" href="#c.Py_END_ALLOW_THREADS" title="Py_END_ALLOW_THREADS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_END_ALLOW_THREADS</span></code></a> macros is
  1168. acceptable.</p>
  1169. <p>The return value is an opaque “handle” to the thread state when
  1170. <a class="reference internal" href="#c.PyGILState_Ensure" title="PyGILState_Ensure"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Ensure()</span></code></a> was called, and must be passed to
  1171. <a class="reference internal" href="#c.PyGILState_Release" title="PyGILState_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Release()</span></code></a> to ensure Python is left in the same state. Even
  1172. though recursive calls are allowed, these handles <em>cannot</em> be shared - each
  1173. unique call to <a class="reference internal" href="#c.PyGILState_Ensure" title="PyGILState_Ensure"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Ensure()</span></code></a> must save the handle for its call
  1174. to <a class="reference internal" href="#c.PyGILState_Release" title="PyGILState_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Release()</span></code></a>.</p>
  1175. <p>When the function returns, the current thread will hold the GIL and be able
  1176. to call arbitrary Python code. Failure is a fatal error.</p>
  1177. <div class="admonition note">
  1178. <p class="admonition-title">Note</p>
  1179. <p>Calling this function from a thread when the runtime is finalizing
  1180. will terminate the thread, even if the thread was not created by Python.
  1181. You can use <code class="xref c c-func docutils literal notranslate"><span class="pre">_Py_IsFinalizing()</span></code> or <a class="reference internal" href="../library/sys.html#sys.is_finalizing" title="sys.is_finalizing"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.is_finalizing()</span></code></a> to
  1182. check if the interpreter is in process of being finalized before calling
  1183. this function to avoid unwanted termination.</p>
  1184. </div>
  1185. </dd></dl>
  1186. <dl class="c function">
  1187. <dt class="sig sig-object c" id="c.PyGILState_Release">
  1188. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyGILState_Release</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">PyGILState_STATE</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyGILState_Release" title="Link to this definition">¶</a><br /></dt>
  1189. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Release any resources previously acquired. After this call, Python’s state will
  1190. be the same as it was prior to the corresponding <a class="reference internal" href="#c.PyGILState_Ensure" title="PyGILState_Ensure"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Ensure()</span></code></a> call
  1191. (but generally this state will be unknown to the caller, hence the use of the
  1192. GILState API).</p>
  1193. <p>Every call to <a class="reference internal" href="#c.PyGILState_Ensure" title="PyGILState_Ensure"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Ensure()</span></code></a> must be matched by a call to
  1194. <a class="reference internal" href="#c.PyGILState_Release" title="PyGILState_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Release()</span></code></a> on the same thread.</p>
  1195. </dd></dl>
  1196. <dl class="c function">
  1197. <dt class="sig sig-object c" id="c.PyGILState_GetThisThreadState">
  1198. <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyGILState_GetThisThreadState</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyGILState_GetThisThreadState" title="Link to this definition">¶</a><br /></dt>
  1199. <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>Get the current thread state for this thread. May return <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if no
  1200. GILState API has been used on the current thread. Note that the main thread
  1201. always has such a thread-state, even if no auto-thread-state call has been
  1202. made on the main thread. This is mainly a helper/diagnostic function.</p>
  1203. </dd></dl>
  1204. <dl class="c function">
  1205. <dt class="sig sig-object c" id="c.PyGILState_Check">
  1206. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyGILState_Check</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyGILState_Check" title="Link to this definition">¶</a><br /></dt>
  1207. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">1</span></code> if the current thread is holding the GIL and <code class="docutils literal notranslate"><span class="pre">0</span></code> otherwise.
  1208. This function can be called from any thread at any time.
  1209. Only if it has had its Python thread state initialized and currently is
  1210. holding the GIL will it return <code class="docutils literal notranslate"><span class="pre">1</span></code>.
  1211. This is mainly a helper/diagnostic function. It can be useful
  1212. for example in callback contexts or memory allocation functions when
  1213. knowing that the GIL is locked can allow the caller to perform sensitive
  1214. actions or otherwise behave differently.</p>
  1215. <div class="versionadded">
  1216. <p><span class="versionmodified added">New in version 3.4.</span></p>
  1217. </div>
  1218. </dd></dl>
  1219. <p>The following macros are normally used without a trailing semicolon; look for
  1220. example usage in the Python source distribution.</p>
  1221. <dl class="c macro">
  1222. <dt class="sig sig-object c" id="c.Py_BEGIN_ALLOW_THREADS">
  1223. <span class="sig-name descname"><span class="n"><span class="pre">Py_BEGIN_ALLOW_THREADS</span></span></span><a class="headerlink" href="#c.Py_BEGIN_ALLOW_THREADS" title="Link to this definition">¶</a><br /></dt>
  1224. <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>This macro expands to <code class="docutils literal notranslate"><span class="pre">{</span> <span class="pre">PyThreadState</span> <span class="pre">*_save;</span> <span class="pre">_save</span> <span class="pre">=</span> <span class="pre">PyEval_SaveThread();</span></code>.
  1225. Note that it contains an opening brace; it must be matched with a following
  1226. <a class="reference internal" href="#c.Py_END_ALLOW_THREADS" title="Py_END_ALLOW_THREADS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_END_ALLOW_THREADS</span></code></a> macro. See above for further discussion of this
  1227. macro.</p>
  1228. </dd></dl>
  1229. <dl class="c macro">
  1230. <dt class="sig sig-object c" id="c.Py_END_ALLOW_THREADS">
  1231. <span class="sig-name descname"><span class="n"><span class="pre">Py_END_ALLOW_THREADS</span></span></span><a class="headerlink" href="#c.Py_END_ALLOW_THREADS" title="Link to this definition">¶</a><br /></dt>
  1232. <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>This macro expands to <code class="docutils literal notranslate"><span class="pre">PyEval_RestoreThread(_save);</span> <span class="pre">}</span></code>. Note that it contains
  1233. a closing brace; it must be matched with an earlier
  1234. <a class="reference internal" href="#c.Py_BEGIN_ALLOW_THREADS" title="Py_BEGIN_ALLOW_THREADS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_BEGIN_ALLOW_THREADS</span></code></a> macro. See above for further discussion of
  1235. this macro.</p>
  1236. </dd></dl>
  1237. <dl class="c macro">
  1238. <dt class="sig sig-object c" id="c.Py_BLOCK_THREADS">
  1239. <span class="sig-name descname"><span class="n"><span class="pre">Py_BLOCK_THREADS</span></span></span><a class="headerlink" href="#c.Py_BLOCK_THREADS" title="Link to this definition">¶</a><br /></dt>
  1240. <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>This macro expands to <code class="docutils literal notranslate"><span class="pre">PyEval_RestoreThread(_save);</span></code>: it is equivalent to
  1241. <a class="reference internal" href="#c.Py_END_ALLOW_THREADS" title="Py_END_ALLOW_THREADS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_END_ALLOW_THREADS</span></code></a> without the closing brace.</p>
  1242. </dd></dl>
  1243. <dl class="c macro">
  1244. <dt class="sig sig-object c" id="c.Py_UNBLOCK_THREADS">
  1245. <span class="sig-name descname"><span class="n"><span class="pre">Py_UNBLOCK_THREADS</span></span></span><a class="headerlink" href="#c.Py_UNBLOCK_THREADS" title="Link to this definition">¶</a><br /></dt>
  1246. <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>This macro expands to <code class="docutils literal notranslate"><span class="pre">_save</span> <span class="pre">=</span> <span class="pre">PyEval_SaveThread();</span></code>: it is equivalent to
  1247. <a class="reference internal" href="#c.Py_BEGIN_ALLOW_THREADS" title="Py_BEGIN_ALLOW_THREADS"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_BEGIN_ALLOW_THREADS</span></code></a> without the opening brace and variable
  1248. declaration.</p>
  1249. </dd></dl>
  1250. </section>
  1251. <section id="low-level-api">
  1252. <h3>Low-level API<a class="headerlink" href="#low-level-api" title="Link to this heading">¶</a></h3>
  1253. <p>All of the following functions must be called after <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>.</p>
  1254. <div class="versionchanged">
  1255. <p><span class="versionmodified changed">Changed in version 3.7: </span><a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a> now initializes the <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a>.</p>
  1256. </div>
  1257. <dl class="c function">
  1258. <dt class="sig sig-object c" id="c.PyInterpreterState_New">
  1259. <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState_New</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyInterpreterState_New" title="Link to this definition">¶</a><br /></dt>
  1260. <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>Create a new interpreter state object. The global interpreter lock need not
  1261. be held, but may be held if it is necessary to serialize calls to this
  1262. function.</p>
  1263. <p class="audit-hook">Raises an <a class="reference internal" href="../library/sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">cpython.PyInterpreterState_New</span></code> with no arguments.</p>
  1264. </dd></dl>
  1265. <dl class="c function">
  1266. <dt class="sig sig-object c" id="c.PyInterpreterState_Clear">
  1267. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState_Clear</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">interp</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyInterpreterState_Clear" title="Link to this definition">¶</a><br /></dt>
  1268. <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>Reset all information in an interpreter state object. The global interpreter
  1269. lock must be held.</p>
  1270. <p class="audit-hook">Raises an <a class="reference internal" href="../library/sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">cpython.PyInterpreterState_Clear</span></code> with no arguments.</p>
  1271. </dd></dl>
  1272. <dl class="c function">
  1273. <dt class="sig sig-object c" id="c.PyInterpreterState_Delete">
  1274. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState_Delete</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">interp</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyInterpreterState_Delete" title="Link to this definition">¶</a><br /></dt>
  1275. <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>Destroy an interpreter state object. The global interpreter lock need not be
  1276. held. The interpreter state must have been reset with a previous call to
  1277. <a class="reference internal" href="#c.PyInterpreterState_Clear" title="PyInterpreterState_Clear"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyInterpreterState_Clear()</span></code></a>.</p>
  1278. </dd></dl>
  1279. <dl class="c function">
  1280. <dt class="sig sig-object c" id="c.PyThreadState_New">
  1281. <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_New</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">interp</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_New" title="Link to this definition">¶</a><br /></dt>
  1282. <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>Create a new thread state object belonging to the given interpreter object.
  1283. The global interpreter lock need not be held, but may be held if it is
  1284. necessary to serialize calls to this function.</p>
  1285. </dd></dl>
  1286. <dl class="c function">
  1287. <dt class="sig sig-object c" id="c.PyThreadState_Clear">
  1288. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_Clear</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_Clear" title="Link to this definition">¶</a><br /></dt>
  1289. <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>Reset all information in a thread state object. The global interpreter lock
  1290. must be held.</p>
  1291. <div class="versionchanged">
  1292. <p><span class="versionmodified changed">Changed in version 3.9: </span>This function now calls the <code class="xref c c-member docutils literal notranslate"><span class="pre">PyThreadState.on_delete</span></code> callback.
  1293. Previously, that happened in <a class="reference internal" href="#c.PyThreadState_Delete" title="PyThreadState_Delete"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThreadState_Delete()</span></code></a>.</p>
  1294. </div>
  1295. </dd></dl>
  1296. <dl class="c function">
  1297. <dt class="sig sig-object c" id="c.PyThreadState_Delete">
  1298. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_Delete</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_Delete" title="Link to this definition">¶</a><br /></dt>
  1299. <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>Destroy a thread state object. The global interpreter lock need not be held.
  1300. The thread state must have been reset with a previous call to
  1301. <a class="reference internal" href="#c.PyThreadState_Clear" title="PyThreadState_Clear"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThreadState_Clear()</span></code></a>.</p>
  1302. </dd></dl>
  1303. <dl class="c function">
  1304. <dt class="sig sig-object c" id="c.PyThreadState_DeleteCurrent">
  1305. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_DeleteCurrent</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_DeleteCurrent" title="Link to this definition">¶</a><br /></dt>
  1306. <dd><p>Destroy the current thread state and release the global interpreter lock.
  1307. Like <a class="reference internal" href="#c.PyThreadState_Delete" title="PyThreadState_Delete"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThreadState_Delete()</span></code></a>, the global interpreter lock need not
  1308. be held. The thread state must have been reset with a previous call
  1309. to <a class="reference internal" href="#c.PyThreadState_Clear" title="PyThreadState_Clear"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThreadState_Clear()</span></code></a>.</p>
  1310. </dd></dl>
  1311. <dl class="c function">
  1312. <dt class="sig sig-object c" id="c.PyThreadState_GetFrame">
  1313. <a class="reference internal" href="frame.html#c.PyFrameObject" title="PyFrameObject"><span class="n"><span class="pre">PyFrameObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_GetFrame</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_GetFrame" title="Link to this definition">¶</a><br /></dt>
  1314. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.10.</em><p>Get the current frame of the Python thread state <em>tstate</em>.</p>
  1315. <p>Return a <a class="reference internal" href="../glossary.html#term-strong-reference"><span class="xref std std-term">strong reference</span></a>. Return <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if no frame is currently
  1316. executing.</p>
  1317. <p>See also <a class="reference internal" href="reflection.html#c.PyEval_GetFrame" title="PyEval_GetFrame"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_GetFrame()</span></code></a>.</p>
  1318. <p><em>tstate</em> must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  1319. <div class="versionadded">
  1320. <p><span class="versionmodified added">New in version 3.9.</span></p>
  1321. </div>
  1322. </dd></dl>
  1323. <dl class="c function">
  1324. <dt class="sig sig-object c" id="c.PyThreadState_GetID">
  1325. <span class="n"><span class="pre">uint64_t</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_GetID</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_GetID" title="Link to this definition">¶</a><br /></dt>
  1326. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.10.</em><p>Get the unique thread state identifier of the Python thread state <em>tstate</em>.</p>
  1327. <p><em>tstate</em> must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  1328. <div class="versionadded">
  1329. <p><span class="versionmodified added">New in version 3.9.</span></p>
  1330. </div>
  1331. </dd></dl>
  1332. <dl class="c function">
  1333. <dt class="sig sig-object c" id="c.PyThreadState_GetInterpreter">
  1334. <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_GetInterpreter</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_GetInterpreter" title="Link to this definition">¶</a><br /></dt>
  1335. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.10.</em><p>Get the interpreter of the Python thread state <em>tstate</em>.</p>
  1336. <p><em>tstate</em> must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  1337. <div class="versionadded">
  1338. <p><span class="versionmodified added">New in version 3.9.</span></p>
  1339. </div>
  1340. </dd></dl>
  1341. <dl class="c function">
  1342. <dt class="sig sig-object c" id="c.PyThreadState_EnterTracing">
  1343. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_EnterTracing</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_EnterTracing" title="Link to this definition">¶</a><br /></dt>
  1344. <dd><p>Suspend tracing and profiling in the Python thread state <em>tstate</em>.</p>
  1345. <p>Resume them using the <a class="reference internal" href="#c.PyThreadState_LeaveTracing" title="PyThreadState_LeaveTracing"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThreadState_LeaveTracing()</span></code></a> function.</p>
  1346. <div class="versionadded">
  1347. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1348. </div>
  1349. </dd></dl>
  1350. <dl class="c function">
  1351. <dt class="sig sig-object c" id="c.PyThreadState_LeaveTracing">
  1352. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_LeaveTracing</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_LeaveTracing" title="Link to this definition">¶</a><br /></dt>
  1353. <dd><p>Resume tracing and profiling in the Python thread state <em>tstate</em> suspended
  1354. by the <a class="reference internal" href="#c.PyThreadState_EnterTracing" title="PyThreadState_EnterTracing"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThreadState_EnterTracing()</span></code></a> function.</p>
  1355. <p>See also <a class="reference internal" href="#c.PyEval_SetTrace" title="PyEval_SetTrace"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetTrace()</span></code></a> and <a class="reference internal" href="#c.PyEval_SetProfile" title="PyEval_SetProfile"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetProfile()</span></code></a>
  1356. functions.</p>
  1357. <div class="versionadded">
  1358. <p><span class="versionmodified added">New in version 3.11.</span></p>
  1359. </div>
  1360. </dd></dl>
  1361. <dl class="c function">
  1362. <dt class="sig sig-object c" id="c.PyInterpreterState_Get">
  1363. <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState_Get</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyInterpreterState_Get" title="Link to this definition">¶</a><br /></dt>
  1364. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.9.</em><p>Get the current interpreter.</p>
  1365. <p>Issue a fatal error if there no current Python thread state or no current
  1366. interpreter. It cannot return NULL.</p>
  1367. <p>The caller must hold the GIL.</p>
  1368. <div class="versionadded">
  1369. <p><span class="versionmodified added">New in version 3.9.</span></p>
  1370. </div>
  1371. </dd></dl>
  1372. <dl class="c function">
  1373. <dt class="sig sig-object c" id="c.PyInterpreterState_GetID">
  1374. <span class="n"><span class="pre">int64_t</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState_GetID</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">interp</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyInterpreterState_GetID" title="Link to this definition">¶</a><br /></dt>
  1375. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Return the interpreter’s unique ID. If there was any error in doing
  1376. so then <code class="docutils literal notranslate"><span class="pre">-1</span></code> is returned and an error is set.</p>
  1377. <p>The caller must hold the GIL.</p>
  1378. <div class="versionadded">
  1379. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1380. </div>
  1381. </dd></dl>
  1382. <dl class="c function">
  1383. <dt class="sig sig-object c" id="c.PyInterpreterState_GetDict">
  1384. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState_GetDict</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">interp</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyInterpreterState_GetDict" title="Link to this definition">¶</a><br /></dt>
  1385. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.8.</em><p>Return a dictionary in which interpreter-specific data may be stored.
  1386. If this function returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> then no exception has been raised and
  1387. the caller should assume no interpreter-specific dict is available.</p>
  1388. <p>This is not a replacement for <a class="reference internal" href="module.html#c.PyModule_GetState" title="PyModule_GetState"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_GetState()</span></code></a>, which
  1389. extensions should use to store interpreter-specific state information.</p>
  1390. <div class="versionadded">
  1391. <p><span class="versionmodified added">New in version 3.8.</span></p>
  1392. </div>
  1393. </dd></dl>
  1394. <dl class="c type">
  1395. <dt class="sig sig-object c" id="c._PyFrameEvalFunction">
  1396. <span class="k"><span class="pre">typedef</span></span><span class="w"> </span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">_PyFrameEvalFunction</span></span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference internal" href="frame.html#c._PyInterpreterFrame" title="_PyInterpreterFrame"><span class="n"><span class="pre">_PyInterpreterFrame</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">frame</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">throwflag</span></span><span class="p"><span class="pre">)</span></span><a class="headerlink" href="#c._PyFrameEvalFunction" title="Link to this definition">¶</a><br /></dt>
  1397. <dd><p>Type of a frame evaluation function.</p>
  1398. <p>The <em>throwflag</em> parameter is used by the <code class="docutils literal notranslate"><span class="pre">throw()</span></code> method of generators:
  1399. if non-zero, handle the current exception.</p>
  1400. <div class="versionchanged">
  1401. <p><span class="versionmodified changed">Changed in version 3.9: </span>The function now takes a <em>tstate</em> parameter.</p>
  1402. </div>
  1403. <div class="versionchanged">
  1404. <p><span class="versionmodified changed">Changed in version 3.11: </span>The <em>frame</em> parameter changed from <code class="docutils literal notranslate"><span class="pre">PyFrameObject*</span></code> to <code class="docutils literal notranslate"><span class="pre">_PyInterpreterFrame*</span></code>.</p>
  1405. </div>
  1406. </dd></dl>
  1407. <dl class="c function">
  1408. <dt class="sig sig-object c" id="c._PyInterpreterState_GetEvalFrameFunc">
  1409. <a class="reference internal" href="#c._PyFrameEvalFunction" title="_PyFrameEvalFunction"><span class="n"><span class="pre">_PyFrameEvalFunction</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_PyInterpreterState_GetEvalFrameFunc</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">interp</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c._PyInterpreterState_GetEvalFrameFunc" title="Link to this definition">¶</a><br /></dt>
  1410. <dd><p>Get the frame evaluation function.</p>
  1411. <p>See the <span class="target" id="index-39"></span><a class="pep reference external" href="https://peps.python.org/pep-0523/"><strong>PEP 523</strong></a> “Adding a frame evaluation API to CPython”.</p>
  1412. <div class="versionadded">
  1413. <p><span class="versionmodified added">New in version 3.9.</span></p>
  1414. </div>
  1415. </dd></dl>
  1416. <dl class="c function">
  1417. <dt class="sig sig-object c" id="c._PyInterpreterState_SetEvalFrameFunc">
  1418. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">_PyInterpreterState_SetEvalFrameFunc</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">interp</span></span>, <a class="reference internal" href="#c._PyFrameEvalFunction" title="_PyFrameEvalFunction"><span class="n"><span class="pre">_PyFrameEvalFunction</span></span></a><span class="w"> </span><span class="n"><span class="pre">eval_frame</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c._PyInterpreterState_SetEvalFrameFunc" title="Link to this definition">¶</a><br /></dt>
  1419. <dd><p>Set the frame evaluation function.</p>
  1420. <p>See the <span class="target" id="index-40"></span><a class="pep reference external" href="https://peps.python.org/pep-0523/"><strong>PEP 523</strong></a> “Adding a frame evaluation API to CPython”.</p>
  1421. <div class="versionadded">
  1422. <p><span class="versionmodified added">New in version 3.9.</span></p>
  1423. </div>
  1424. </dd></dl>
  1425. <dl class="c function">
  1426. <dt class="sig sig-object c" id="c.PyThreadState_GetDict">
  1427. <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_GetDict</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_GetDict" title="Link to this definition">¶</a><br /></dt>
  1428. <dd><em class="refcount">Return value: Borrowed reference.</em><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>Return a dictionary in which extensions can store thread-specific state
  1429. information. Each extension should use a unique key to use to store state in
  1430. the dictionary. It is okay to call this function when no current thread state
  1431. is available. If this function returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, no exception has been raised and
  1432. the caller should assume no current thread state is available.</p>
  1433. </dd></dl>
  1434. <dl class="c function">
  1435. <dt class="sig sig-object c" id="c.PyThreadState_SetAsyncExc">
  1436. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_SetAsyncExc</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">unsigned</span></span><span class="w"> </span><span class="kt"><span class="pre">long</span></span><span class="w"> </span><span class="n"><span class="pre">id</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">exc</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_SetAsyncExc" title="Link to this definition">¶</a><br /></dt>
  1437. <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>Asynchronously raise an exception in a thread. The <em>id</em> argument is the thread
  1438. id of the target thread; <em>exc</em> is the exception object to be raised. This
  1439. function does not steal any references to <em>exc</em>. To prevent naive misuse, you
  1440. must write your own C extension to call this. Must be called with the GIL held.
  1441. Returns the number of thread states modified; this is normally one, but will be
  1442. zero if the thread id isn’t found. If <em>exc</em> is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the pending
  1443. exception (if any) for the thread is cleared. This raises no exceptions.</p>
  1444. <div class="versionchanged">
  1445. <p><span class="versionmodified changed">Changed in version 3.7: </span>The type of the <em>id</em> parameter changed from <span class="c-expr sig sig-inline c"><span class="kt">long</span></span> to
  1446. <span class="c-expr sig sig-inline c"><span class="kt">unsigned</span><span class="w"> </span><span class="kt">long</span></span>.</p>
  1447. </div>
  1448. </dd></dl>
  1449. <dl class="c function">
  1450. <dt class="sig sig-object c" id="c.PyEval_AcquireThread">
  1451. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_AcquireThread</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_AcquireThread" title="Link to this definition">¶</a><br /></dt>
  1452. <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>Acquire the global interpreter lock and set the current thread state to
  1453. <em>tstate</em>, which must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. The lock must have been created earlier.
  1454. If this thread already has the lock, deadlock ensues.</p>
  1455. <div class="admonition note">
  1456. <p class="admonition-title">Note</p>
  1457. <p>Calling this function from a thread when the runtime is finalizing
  1458. will terminate the thread, even if the thread was not created by Python.
  1459. You can use <code class="xref c c-func docutils literal notranslate"><span class="pre">_Py_IsFinalizing()</span></code> or <a class="reference internal" href="../library/sys.html#sys.is_finalizing" title="sys.is_finalizing"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.is_finalizing()</span></code></a> to
  1460. check if the interpreter is in process of being finalized before calling
  1461. this function to avoid unwanted termination.</p>
  1462. </div>
  1463. <div class="versionchanged">
  1464. <p><span class="versionmodified changed">Changed in version 3.8: </span>Updated to be consistent with <a class="reference internal" href="#c.PyEval_RestoreThread" title="PyEval_RestoreThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_RestoreThread()</span></code></a>,
  1465. <a class="reference internal" href="#c.Py_END_ALLOW_THREADS" title="Py_END_ALLOW_THREADS"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_END_ALLOW_THREADS()</span></code></a>, and <a class="reference internal" href="#c.PyGILState_Ensure" title="PyGILState_Ensure"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Ensure()</span></code></a>,
  1466. and terminate the current thread if called while the interpreter is finalizing.</p>
  1467. </div>
  1468. <p><a class="reference internal" href="#c.PyEval_RestoreThread" title="PyEval_RestoreThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_RestoreThread()</span></code></a> is a higher-level function which is always
  1469. available (even when threads have not been initialized).</p>
  1470. </dd></dl>
  1471. <dl class="c function">
  1472. <dt class="sig sig-object c" id="c.PyEval_ReleaseThread">
  1473. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_ReleaseThread</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_ReleaseThread" title="Link to this definition">¶</a><br /></dt>
  1474. <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>Reset the current thread state to <code class="docutils literal notranslate"><span class="pre">NULL</span></code> and release the global interpreter
  1475. lock. The lock must have been created earlier and must be held by the current
  1476. thread. The <em>tstate</em> argument, which must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, is only used to check
  1477. that it represents the current thread state — if it isn’t, a fatal error is
  1478. reported.</p>
  1479. <p><a class="reference internal" href="#c.PyEval_SaveThread" title="PyEval_SaveThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SaveThread()</span></code></a> is a higher-level function which is always
  1480. available (even when threads have not been initialized).</p>
  1481. </dd></dl>
  1482. <dl class="c function">
  1483. <dt class="sig sig-object c" id="c.PyEval_AcquireLock">
  1484. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_AcquireLock</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_AcquireLock" title="Link to this definition">¶</a><br /></dt>
  1485. <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>Acquire the global interpreter lock. The lock must have been created earlier.
  1486. If this thread already has the lock, a deadlock ensues.</p>
  1487. <div class="deprecated">
  1488. <p><span class="versionmodified deprecated">Deprecated since version 3.2: </span>This function does not update the current thread state. Please use
  1489. <a class="reference internal" href="#c.PyEval_RestoreThread" title="PyEval_RestoreThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_RestoreThread()</span></code></a> or <a class="reference internal" href="#c.PyEval_AcquireThread" title="PyEval_AcquireThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_AcquireThread()</span></code></a>
  1490. instead.</p>
  1491. </div>
  1492. <div class="admonition note">
  1493. <p class="admonition-title">Note</p>
  1494. <p>Calling this function from a thread when the runtime is finalizing
  1495. will terminate the thread, even if the thread was not created by Python.
  1496. You can use <code class="xref c c-func docutils literal notranslate"><span class="pre">_Py_IsFinalizing()</span></code> or <a class="reference internal" href="../library/sys.html#sys.is_finalizing" title="sys.is_finalizing"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.is_finalizing()</span></code></a> to
  1497. check if the interpreter is in process of being finalized before calling
  1498. this function to avoid unwanted termination.</p>
  1499. </div>
  1500. <div class="versionchanged">
  1501. <p><span class="versionmodified changed">Changed in version 3.8: </span>Updated to be consistent with <a class="reference internal" href="#c.PyEval_RestoreThread" title="PyEval_RestoreThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_RestoreThread()</span></code></a>,
  1502. <a class="reference internal" href="#c.Py_END_ALLOW_THREADS" title="Py_END_ALLOW_THREADS"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_END_ALLOW_THREADS()</span></code></a>, and <a class="reference internal" href="#c.PyGILState_Ensure" title="PyGILState_Ensure"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Ensure()</span></code></a>,
  1503. and terminate the current thread if called while the interpreter is finalizing.</p>
  1504. </div>
  1505. </dd></dl>
  1506. <dl class="c function">
  1507. <dt class="sig sig-object c" id="c.PyEval_ReleaseLock">
  1508. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_ReleaseLock</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_ReleaseLock" title="Link to this definition">¶</a><br /></dt>
  1509. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a>.</em><p>Release the global interpreter lock. The lock must have been created earlier.</p>
  1510. <div class="deprecated">
  1511. <p><span class="versionmodified deprecated">Deprecated since version 3.2: </span>This function does not update the current thread state. Please use
  1512. <a class="reference internal" href="#c.PyEval_SaveThread" title="PyEval_SaveThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SaveThread()</span></code></a> or <a class="reference internal" href="#c.PyEval_ReleaseThread" title="PyEval_ReleaseThread"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_ReleaseThread()</span></code></a>
  1513. instead.</p>
  1514. </div>
  1515. </dd></dl>
  1516. </section>
  1517. </section>
  1518. <section id="sub-interpreter-support">
  1519. <span id="id1"></span><h2>Sub-interpreter support<a class="headerlink" href="#sub-interpreter-support" title="Link to this heading">¶</a></h2>
  1520. <p>While in most uses, you will only embed a single Python interpreter, there
  1521. are cases where you need to create several independent interpreters in the
  1522. same process and perhaps even in the same thread. Sub-interpreters allow
  1523. you to do that.</p>
  1524. <p>The “main” interpreter is the first one created when the runtime initializes.
  1525. It is usually the only Python interpreter in a process. Unlike sub-interpreters,
  1526. the main interpreter has unique process-global responsibilities like signal
  1527. handling. It is also responsible for execution during runtime initialization and
  1528. is usually the active interpreter during runtime finalization. The
  1529. <a class="reference internal" href="#c.PyInterpreterState_Main" title="PyInterpreterState_Main"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyInterpreterState_Main()</span></code></a> function returns a pointer to its state.</p>
  1530. <p>You can switch between sub-interpreters using the <a class="reference internal" href="#c.PyThreadState_Swap" title="PyThreadState_Swap"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThreadState_Swap()</span></code></a>
  1531. function. You can create and destroy them using the following functions:</p>
  1532. <dl class="c type">
  1533. <dt class="sig sig-object c" id="c.PyInterpreterConfig">
  1534. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterConfig</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig" title="Link to this definition">¶</a><br /></dt>
  1535. <dd><p>Structure containing most parameters to configure a sub-interpreter.
  1536. Its values are used only in <a class="reference internal" href="#c.Py_NewInterpreterFromConfig" title="Py_NewInterpreterFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_NewInterpreterFromConfig()</span></code></a> and
  1537. never modified by the runtime.</p>
  1538. <div class="versionadded">
  1539. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1540. </div>
  1541. <p>Structure fields:</p>
  1542. <dl class="c member">
  1543. <dt class="sig sig-object c" id="c.PyInterpreterConfig.use_main_obmalloc">
  1544. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">use_main_obmalloc</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig.use_main_obmalloc" title="Link to this definition">¶</a><br /></dt>
  1545. <dd><p>If this is <code class="docutils literal notranslate"><span class="pre">0</span></code> then the sub-interpreter will use its own
  1546. “object” allocator state.
  1547. Otherwise it will use (share) the main interpreter’s.</p>
  1548. <p>If this is <code class="docutils literal notranslate"><span class="pre">0</span></code> then
  1549. <a class="reference internal" href="#c.PyInterpreterConfig.check_multi_interp_extensions" title="PyInterpreterConfig.check_multi_interp_extensions"><code class="xref c c-member docutils literal notranslate"><span class="pre">check_multi_interp_extensions</span></code></a>
  1550. must be <code class="docutils literal notranslate"><span class="pre">1</span></code> (non-zero).
  1551. If this is <code class="docutils literal notranslate"><span class="pre">1</span></code> then <a class="reference internal" href="#c.PyInterpreterConfig.gil" title="PyInterpreterConfig.gil"><code class="xref c c-member docutils literal notranslate"><span class="pre">gil</span></code></a>
  1552. must not be <a class="reference internal" href="#c.PyInterpreterConfig_OWN_GIL" title="PyInterpreterConfig_OWN_GIL"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyInterpreterConfig_OWN_GIL</span></code></a>.</p>
  1553. </dd></dl>
  1554. <dl class="c member">
  1555. <dt class="sig sig-object c" id="c.PyInterpreterConfig.allow_fork">
  1556. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">allow_fork</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig.allow_fork" title="Link to this definition">¶</a><br /></dt>
  1557. <dd><p>If this is <code class="docutils literal notranslate"><span class="pre">0</span></code> then the runtime will not support forking the
  1558. process in any thread where the sub-interpreter is currently active.
  1559. Otherwise fork is unrestricted.</p>
  1560. <p>Note that the <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module still works
  1561. when fork is disallowed.</p>
  1562. </dd></dl>
  1563. <dl class="c member">
  1564. <dt class="sig sig-object c" id="c.PyInterpreterConfig.allow_exec">
  1565. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">allow_exec</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig.allow_exec" title="Link to this definition">¶</a><br /></dt>
  1566. <dd><p>If this is <code class="docutils literal notranslate"><span class="pre">0</span></code> then the runtime will not support replacing the
  1567. current process via exec (e.g. <a class="reference internal" href="../library/os.html#os.execv" title="os.execv"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.execv()</span></code></a>) in any thread
  1568. where the sub-interpreter is currently active.
  1569. Otherwise exec is unrestricted.</p>
  1570. <p>Note that the <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><code class="xref py py-mod docutils literal notranslate"><span class="pre">subprocess</span></code></a> module still works
  1571. when exec is disallowed.</p>
  1572. </dd></dl>
  1573. <dl class="c member">
  1574. <dt class="sig sig-object c" id="c.PyInterpreterConfig.allow_threads">
  1575. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">allow_threads</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig.allow_threads" title="Link to this definition">¶</a><br /></dt>
  1576. <dd><p>If this is <code class="docutils literal notranslate"><span class="pre">0</span></code> then the sub-interpreter’s <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module
  1577. won’t create threads.
  1578. Otherwise threads are allowed.</p>
  1579. </dd></dl>
  1580. <dl class="c member">
  1581. <dt class="sig sig-object c" id="c.PyInterpreterConfig.allow_daemon_threads">
  1582. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">allow_daemon_threads</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig.allow_daemon_threads" title="Link to this definition">¶</a><br /></dt>
  1583. <dd><p>If this is <code class="docutils literal notranslate"><span class="pre">0</span></code> then the sub-interpreter’s <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Thread-based parallelism."><code class="xref py py-mod docutils literal notranslate"><span class="pre">threading</span></code></a> module
  1584. won’t create daemon threads.
  1585. Otherwise daemon threads are allowed (as long as
  1586. <a class="reference internal" href="#c.PyInterpreterConfig.allow_threads" title="PyInterpreterConfig.allow_threads"><code class="xref c c-member docutils literal notranslate"><span class="pre">allow_threads</span></code></a> is non-zero).</p>
  1587. </dd></dl>
  1588. <dl class="c member">
  1589. <dt class="sig sig-object c" id="c.PyInterpreterConfig.check_multi_interp_extensions">
  1590. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">check_multi_interp_extensions</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig.check_multi_interp_extensions" title="Link to this definition">¶</a><br /></dt>
  1591. <dd><p>If this is <code class="docutils literal notranslate"><span class="pre">0</span></code> then all extension modules may be imported,
  1592. including legacy (single-phase init) modules,
  1593. in any thread where the sub-interpreter is currently active.
  1594. Otherwise only multi-phase init extension modules
  1595. (see <span class="target" id="index-41"></span><a class="pep reference external" href="https://peps.python.org/pep-0489/"><strong>PEP 489</strong></a>) may be imported.
  1596. (Also see <a class="reference internal" href="module.html#c.Py_mod_multiple_interpreters" title="Py_mod_multiple_interpreters"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_mod_multiple_interpreters</span></code></a>.)</p>
  1597. <p>This must be <code class="docutils literal notranslate"><span class="pre">1</span></code> (non-zero) if
  1598. <a class="reference internal" href="#c.PyInterpreterConfig.use_main_obmalloc" title="PyInterpreterConfig.use_main_obmalloc"><code class="xref c c-member docutils literal notranslate"><span class="pre">use_main_obmalloc</span></code></a> is <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
  1599. </dd></dl>
  1600. <dl class="c member">
  1601. <dt class="sig sig-object c" id="c.PyInterpreterConfig.gil">
  1602. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">gil</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig.gil" title="Link to this definition">¶</a><br /></dt>
  1603. <dd><p>This determines the operation of the GIL for the sub-interpreter.
  1604. It may be one of the following:</p>
  1605. <dl class="c macro">
  1606. <dt class="sig sig-object c" id="c.PyInterpreterConfig_DEFAULT_GIL">
  1607. <span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterConfig_DEFAULT_GIL</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig_DEFAULT_GIL" title="Link to this definition">¶</a><br /></dt>
  1608. <dd><p>Use the default selection (<a class="reference internal" href="#c.PyInterpreterConfig_SHARED_GIL" title="PyInterpreterConfig_SHARED_GIL"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyInterpreterConfig_SHARED_GIL</span></code></a>).</p>
  1609. </dd></dl>
  1610. <dl class="c macro">
  1611. <dt class="sig sig-object c" id="c.PyInterpreterConfig_SHARED_GIL">
  1612. <span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterConfig_SHARED_GIL</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig_SHARED_GIL" title="Link to this definition">¶</a><br /></dt>
  1613. <dd><p>Use (share) the main interpreter’s GIL.</p>
  1614. </dd></dl>
  1615. <dl class="c macro">
  1616. <dt class="sig sig-object c" id="c.PyInterpreterConfig_OWN_GIL">
  1617. <span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterConfig_OWN_GIL</span></span></span><a class="headerlink" href="#c.PyInterpreterConfig_OWN_GIL" title="Link to this definition">¶</a><br /></dt>
  1618. <dd><p>Use the sub-interpreter’s own GIL.</p>
  1619. </dd></dl>
  1620. <p>If this is <a class="reference internal" href="#c.PyInterpreterConfig_OWN_GIL" title="PyInterpreterConfig_OWN_GIL"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PyInterpreterConfig_OWN_GIL</span></code></a> then
  1621. <a class="reference internal" href="#c.PyInterpreterConfig.use_main_obmalloc" title="PyInterpreterConfig.use_main_obmalloc"><code class="xref c c-member docutils literal notranslate"><span class="pre">PyInterpreterConfig.use_main_obmalloc</span></code></a> must be <code class="docutils literal notranslate"><span class="pre">0</span></code>.</p>
  1622. </dd></dl>
  1623. </dd></dl>
  1624. <dl class="c function">
  1625. <dt class="sig sig-object c" id="c.Py_NewInterpreterFromConfig">
  1626. <a class="reference internal" href="init_config.html#c.PyStatus" title="PyStatus"><span class="n"><span class="pre">PyStatus</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_NewInterpreterFromConfig</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate_p</span></span>, <span class="k"><span class="pre">const</span></span><span class="w"> </span><a class="reference internal" href="#c.PyInterpreterConfig" title="PyInterpreterConfig"><span class="n"><span class="pre">PyInterpreterConfig</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">config</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_NewInterpreterFromConfig" title="Link to this definition">¶</a><br /></dt>
  1627. <dd><p id="index-42">Create a new sub-interpreter. This is an (almost) totally separate environment
  1628. for the execution of Python code. In particular, the new interpreter has
  1629. separate, independent versions of all imported modules, including the
  1630. fundamental modules <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>. The
  1631. table of loaded modules (<code class="docutils literal notranslate"><span class="pre">sys.modules</span></code>) and the module search path
  1632. (<code class="docutils literal notranslate"><span class="pre">sys.path</span></code>) are also separate. The new environment has no <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code>
  1633. variable. It has new standard I/O stream file objects <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>,
  1634. <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code> and <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> (however these refer to the same underlying
  1635. file descriptors).</p>
  1636. <p>The given <em>config</em> controls the options with which the interpreter
  1637. is initialized.</p>
  1638. <p>Upon success, <em>tstate_p</em> will be set to the first thread state
  1639. created in the new
  1640. sub-interpreter. This thread state is made in the current thread state.
  1641. Note that no actual thread is created; see the discussion of thread states
  1642. below. If creation of the new interpreter is unsuccessful,
  1643. <em>tstate_p</em> is set to <code class="docutils literal notranslate"><span class="pre">NULL</span></code>;
  1644. no exception is set since the exception state is stored in the
  1645. current thread state and there may not be a current thread state.</p>
  1646. <p>Like all other Python/C API functions, the global interpreter lock
  1647. must be held before calling this function and is still held when it
  1648. returns. Likewise a current thread state must be set on entry. On
  1649. success, the returned thread state will be set as current. If the
  1650. sub-interpreter is created with its own GIL then the GIL of the
  1651. calling interpreter will be released. When the function returns,
  1652. the new interpreter’s GIL will be held by the current thread and
  1653. the previously interpreter’s GIL will remain released here.</p>
  1654. <div class="versionadded">
  1655. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1656. </div>
  1657. <p>Sub-interpreters are most effective when isolated from each other,
  1658. with certain functionality restricted:</p>
  1659. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyInterpreterConfig</span><span class="w"> </span><span class="n">config</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span>
  1660. <span class="w"> </span><span class="p">.</span><span class="n">use_main_obmalloc</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span>
  1661. <span class="w"> </span><span class="p">.</span><span class="n">allow_fork</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span>
  1662. <span class="w"> </span><span class="p">.</span><span class="n">allow_exec</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span>
  1663. <span class="w"> </span><span class="p">.</span><span class="n">allow_threads</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span>
  1664. <span class="w"> </span><span class="p">.</span><span class="n">allow_daemon_threads</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span>
  1665. <span class="w"> </span><span class="p">.</span><span class="n">check_multi_interp_extensions</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span>
  1666. <span class="w"> </span><span class="p">.</span><span class="n">gil</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">PyInterpreterConfig_OWN_GIL</span><span class="p">,</span>
  1667. <span class="p">};</span>
  1668. <span class="n">PyThreadState</span><span class="w"> </span><span class="o">*</span><span class="n">tstate</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Py_NewInterpreterFromConfig</span><span class="p">(</span><span class="o">&amp;</span><span class="n">config</span><span class="p">);</span>
  1669. </pre></div>
  1670. </div>
  1671. <p>Note that the config is used only briefly and does not get modified.
  1672. During initialization the config’s values are converted into various
  1673. <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyInterpreterState</span></code></a> values. A read-only copy of the config
  1674. may be stored internally on the <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyInterpreterState</span></code></a>.</p>
  1675. <p id="index-43">Extension modules are shared between (sub-)interpreters as follows:</p>
  1676. <ul>
  1677. <li><p>For modules using multi-phase initialization,
  1678. e.g. <a class="reference internal" href="module.html#c.PyModule_FromDefAndSpec" title="PyModule_FromDefAndSpec"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_FromDefAndSpec()</span></code></a>, a separate module object is
  1679. created and initialized for each interpreter.
  1680. Only C-level static and global variables are shared between these
  1681. module objects.</p></li>
  1682. <li><p>For modules using single-phase initialization,
  1683. e.g. <a class="reference internal" href="module.html#c.PyModule_Create" title="PyModule_Create"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyModule_Create()</span></code></a>, the first time a particular extension
  1684. is imported, it is initialized normally, and a (shallow) copy of its
  1685. module’s dictionary is squirreled away.
  1686. When the same extension is imported by another (sub-)interpreter, a new
  1687. module is initialized and filled with the contents of this copy; the
  1688. extension’s <code class="docutils literal notranslate"><span class="pre">init</span></code> function is not called.
  1689. Objects in the module’s dictionary thus end up shared across
  1690. (sub-)interpreters, which might cause unwanted behavior (see
  1691. <a class="reference internal" href="#bugs-and-caveats">Bugs and caveats</a> below).</p>
  1692. <p>Note that this is different from what happens when an extension is
  1693. imported after the interpreter has been completely re-initialized by
  1694. calling <a class="reference internal" href="#c.Py_FinalizeEx" title="Py_FinalizeEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FinalizeEx()</span></code></a> and <a class="reference internal" href="#c.Py_Initialize" title="Py_Initialize"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_Initialize()</span></code></a>; in that
  1695. case, the extension’s <code class="docutils literal notranslate"><span class="pre">initmodule</span></code> function <em>is</em> called again.
  1696. As with multi-phase initialization, this means that only C-level static
  1697. and global variables are shared between these modules.</p>
  1698. </li>
  1699. </ul>
  1700. <span class="target" id="index-44"></span></dd></dl>
  1701. <dl class="c function">
  1702. <dt class="sig sig-object c" id="c.Py_NewInterpreter">
  1703. <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_NewInterpreter</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_NewInterpreter" title="Link to this definition">¶</a><br /></dt>
  1704. <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 id="index-45">Create a new sub-interpreter. This is essentially just a wrapper
  1705. around <a class="reference internal" href="#c.Py_NewInterpreterFromConfig" title="Py_NewInterpreterFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_NewInterpreterFromConfig()</span></code></a> with a config that
  1706. preserves the existing behavior. The result is an unisolated
  1707. sub-interpreter that shares the main interpreter’s GIL, allows
  1708. fork/exec, allows daemon threads, and allows single-phase init
  1709. modules.</p>
  1710. </dd></dl>
  1711. <dl class="c function">
  1712. <dt class="sig sig-object c" id="c.Py_EndInterpreter">
  1713. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_EndInterpreter</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_EndInterpreter" title="Link to this definition">¶</a><br /></dt>
  1714. <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 id="index-46">Destroy the (sub-)interpreter represented by the given thread state.
  1715. The given thread state must be the current thread state. See the
  1716. discussion of thread states below. When the call returns,
  1717. the current thread state is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. All thread states associated
  1718. with this interpreter are destroyed. The global interpreter lock
  1719. used by the target interpreter must be held before calling this
  1720. function. No GIL is held when it returns.</p>
  1721. <p><a class="reference internal" href="#c.Py_FinalizeEx" title="Py_FinalizeEx"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_FinalizeEx()</span></code></a> will destroy all sub-interpreters that
  1722. haven’t been explicitly destroyed at that point.</p>
  1723. </dd></dl>
  1724. <section id="a-per-interpreter-gil">
  1725. <h3>A Per-Interpreter GIL<a class="headerlink" href="#a-per-interpreter-gil" title="Link to this heading">¶</a></h3>
  1726. <p>Using <a class="reference internal" href="#c.Py_NewInterpreterFromConfig" title="Py_NewInterpreterFromConfig"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_NewInterpreterFromConfig()</span></code></a> you can create
  1727. a sub-interpreter that is completely isolated from other interpreters,
  1728. including having its own GIL. The most important benefit of this
  1729. isolation is that such an interpreter can execute Python code without
  1730. being blocked by other interpreters or blocking any others. Thus a
  1731. single Python process can truly take advantage of multiple CPU cores
  1732. when running Python code. The isolation also encourages a different
  1733. approach to concurrency than that of just using threads.
  1734. (See <span class="target" id="index-47"></span><a class="pep reference external" href="https://peps.python.org/pep-0554/"><strong>PEP 554</strong></a>.)</p>
  1735. <p>Using an isolated interpreter requires vigilance in preserving that
  1736. isolation. That especially means not sharing any objects or mutable
  1737. state without guarantees about thread-safety. Even objects that are
  1738. otherwise immutable (e.g. <code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="docutils literal notranslate"><span class="pre">(1,</span> <span class="pre">5)</span></code>) can’t normally be shared
  1739. because of the refcount. One simple but less-efficient approach around
  1740. this is to use a global lock around all use of some state (or object).
  1741. Alternately, effectively immutable objects (like integers or strings)
  1742. can be made safe in spite of their refcounts by making them “immortal”.
  1743. In fact, this has been done for the builtin singletons, small integers,
  1744. and a number of other builtin objects.</p>
  1745. <p>If you preserve isolation then you will have access to proper multi-core
  1746. computing without the complications that come with free-threading.
  1747. Failure to preserve isolation will expose you to the full consequences
  1748. of free-threading, including races and hard-to-debug crashes.</p>
  1749. <p>Aside from that, one of the main challenges of using multiple isolated
  1750. interpreters is how to communicate between them safely (not break
  1751. isolation) and efficiently. The runtime and stdlib do not provide
  1752. any standard approach to this yet. A future stdlib module would help
  1753. mitigate the effort of preserving isolation and expose effective tools
  1754. for communicating (and sharing) data between interpreters.</p>
  1755. <div class="versionadded">
  1756. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1757. </div>
  1758. </section>
  1759. <section id="bugs-and-caveats">
  1760. <h3>Bugs and caveats<a class="headerlink" href="#bugs-and-caveats" title="Link to this heading">¶</a></h3>
  1761. <p>Because sub-interpreters (and the main interpreter) are part of the same
  1762. process, the insulation between them isn’t perfect — for example, using
  1763. low-level file operations like <a class="reference internal" href="../library/os.html#os.close" title="os.close"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.close()</span></code></a> they can
  1764. (accidentally or maliciously) affect each other’s open files. Because of the
  1765. way extensions are shared between (sub-)interpreters, some extensions may not
  1766. work properly; this is especially likely when using single-phase initialization
  1767. or (static) global variables.
  1768. It is possible to insert objects created in one sub-interpreter into
  1769. a namespace of another (sub-)interpreter; this should be avoided if possible.</p>
  1770. <p>Special care should be taken to avoid sharing user-defined functions,
  1771. methods, instances or classes between sub-interpreters, since import
  1772. operations executed by such objects may affect the wrong (sub-)interpreter’s
  1773. dictionary of loaded modules. It is equally important to avoid sharing
  1774. objects from which the above are reachable.</p>
  1775. <p>Also note that combining this functionality with <code class="docutils literal notranslate"><span class="pre">PyGILState_*</span></code> APIs
  1776. is delicate, because these APIs assume a bijection between Python thread states
  1777. and OS-level threads, an assumption broken by the presence of sub-interpreters.
  1778. It is highly recommended that you don’t switch sub-interpreters between a pair
  1779. of matching <a class="reference internal" href="#c.PyGILState_Ensure" title="PyGILState_Ensure"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Ensure()</span></code></a> and <a class="reference internal" href="#c.PyGILState_Release" title="PyGILState_Release"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyGILState_Release()</span></code></a> calls.
  1780. Furthermore, extensions (such as <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ctypes</span></code></a>) using these APIs to allow calling
  1781. of Python code from non-Python created threads will probably be broken when using
  1782. sub-interpreters.</p>
  1783. </section>
  1784. </section>
  1785. <section id="asynchronous-notifications">
  1786. <h2>Asynchronous Notifications<a class="headerlink" href="#asynchronous-notifications" title="Link to this heading">¶</a></h2>
  1787. <p>A mechanism is provided to make asynchronous notifications to the main
  1788. interpreter thread. These notifications take the form of a function
  1789. pointer and a void pointer argument.</p>
  1790. <dl class="c function">
  1791. <dt class="sig sig-object c" id="c.Py_AddPendingCall">
  1792. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">Py_AddPendingCall</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">func</span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></span><span class="kt"><span class="pre">void</span></span><span class="p"><span class="pre">*</span></span><span class="p"><span class="pre">)</span></span>, <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">arg</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.Py_AddPendingCall" title="Link to this definition">¶</a><br /></dt>
  1793. <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>Schedule a function to be called from the main interpreter thread. On
  1794. success, <code class="docutils literal notranslate"><span class="pre">0</span></code> is returned and <em>func</em> is queued for being called in the
  1795. main thread. On failure, <code class="docutils literal notranslate"><span class="pre">-1</span></code> is returned without setting any exception.</p>
  1796. <p>When successfully queued, <em>func</em> will be <em>eventually</em> called from the
  1797. main interpreter thread with the argument <em>arg</em>. It will be called
  1798. asynchronously with respect to normally running Python code, but with
  1799. both these conditions met:</p>
  1800. <ul class="simple">
  1801. <li><p>on a <a class="reference internal" href="../glossary.html#term-bytecode"><span class="xref std std-term">bytecode</span></a> boundary;</p></li>
  1802. <li><p>with the main thread holding the <a class="reference internal" href="../glossary.html#term-global-interpreter-lock"><span class="xref std std-term">global interpreter lock</span></a>
  1803. (<em>func</em> can therefore use the full C API).</p></li>
  1804. </ul>
  1805. <p><em>func</em> must return <code class="docutils literal notranslate"><span class="pre">0</span></code> on success, or <code class="docutils literal notranslate"><span class="pre">-1</span></code> on failure with an exception
  1806. set. <em>func</em> won’t be interrupted to perform another asynchronous
  1807. notification recursively, but it can still be interrupted to switch
  1808. threads if the global interpreter lock is released.</p>
  1809. <p>This function doesn’t need a current thread state to run, and it doesn’t
  1810. need the global interpreter lock.</p>
  1811. <p>To call this function in a subinterpreter, the caller must hold the GIL.
  1812. Otherwise, the function <em>func</em> can be scheduled to be called from the wrong
  1813. interpreter.</p>
  1814. <div class="admonition warning">
  1815. <p class="admonition-title">Warning</p>
  1816. <p>This is a low-level function, only useful for very special cases.
  1817. There is no guarantee that <em>func</em> will be called as quick as
  1818. possible. If the main thread is busy executing a system call,
  1819. <em>func</em> won’t be called before the system call returns. This
  1820. function is generally <strong>not</strong> suitable for calling Python code from
  1821. arbitrary C threads. Instead, use the <a class="reference internal" href="#gilstate"><span class="std std-ref">PyGILState API</span></a>.</p>
  1822. </div>
  1823. <div class="versionadded">
  1824. <p><span class="versionmodified added">New in version 3.1.</span></p>
  1825. </div>
  1826. <div class="versionchanged">
  1827. <p><span class="versionmodified changed">Changed in version 3.9: </span>If this function is called in a subinterpreter, the function <em>func</em> is
  1828. now scheduled to be called from the subinterpreter, rather than being
  1829. called from the main interpreter. Each subinterpreter now has its own
  1830. list of scheduled calls.</p>
  1831. </div>
  1832. </dd></dl>
  1833. </section>
  1834. <section id="profiling-and-tracing">
  1835. <span id="profiling"></span><h2>Profiling and Tracing<a class="headerlink" href="#profiling-and-tracing" title="Link to this heading">¶</a></h2>
  1836. <p>The Python interpreter provides some low-level support for attaching profiling
  1837. and execution tracing facilities. These are used for profiling, debugging, and
  1838. coverage analysis tools.</p>
  1839. <p>This C interface allows the profiling or tracing code to avoid the overhead of
  1840. calling through Python-level callable objects, making a direct C function call
  1841. instead. The essential attributes of the facility have not changed; the
  1842. interface allows trace functions to be installed per-thread, and the basic
  1843. events reported to the trace function are the same as had been reported to the
  1844. Python-level trace functions in previous versions.</p>
  1845. <dl class="c type">
  1846. <dt class="sig sig-object c" id="c.Py_tracefunc">
  1847. <span class="k"><span class="pre">typedef</span></span><span class="w"> </span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="p"><span class="pre">(</span></span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">Py_tracefunc</span></span></span><span class="p"><span class="pre">)</span></span><span class="p"><span class="pre">(</span></span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference internal" href="frame.html#c.PyFrameObject" title="PyFrameObject"><span class="n"><span class="pre">PyFrameObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">frame</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">what</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">arg</span></span><span class="p"><span class="pre">)</span></span><a class="headerlink" href="#c.Py_tracefunc" title="Link to this definition">¶</a><br /></dt>
  1848. <dd><p>The type of the trace function registered using <a class="reference internal" href="#c.PyEval_SetProfile" title="PyEval_SetProfile"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetProfile()</span></code></a> and
  1849. <a class="reference internal" href="#c.PyEval_SetTrace" title="PyEval_SetTrace"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetTrace()</span></code></a>. The first parameter is the object passed to the
  1850. registration function as <em>obj</em>, <em>frame</em> is the frame object to which the event
  1851. pertains, <em>what</em> is one of the constants <a class="reference internal" href="#c.PyTrace_CALL" title="PyTrace_CALL"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_CALL</span></code></a>,
  1852. <a class="reference internal" href="#c.PyTrace_EXCEPTION" title="PyTrace_EXCEPTION"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_EXCEPTION</span></code></a>, <a class="reference internal" href="#c.PyTrace_LINE" title="PyTrace_LINE"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_LINE</span></code></a>, <a class="reference internal" href="#c.PyTrace_RETURN" title="PyTrace_RETURN"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_RETURN</span></code></a>,
  1853. <a class="reference internal" href="#c.PyTrace_C_CALL" title="PyTrace_C_CALL"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_C_CALL</span></code></a>, <a class="reference internal" href="#c.PyTrace_C_EXCEPTION" title="PyTrace_C_EXCEPTION"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_C_EXCEPTION</span></code></a>, <a class="reference internal" href="#c.PyTrace_C_RETURN" title="PyTrace_C_RETURN"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_C_RETURN</span></code></a>,
  1854. or <a class="reference internal" href="#c.PyTrace_OPCODE" title="PyTrace_OPCODE"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_OPCODE</span></code></a>, and <em>arg</em> depends on the value of <em>what</em>:</p>
  1855. <table class="docutils align-default">
  1856. <thead>
  1857. <tr class="row-odd"><th class="head"><p>Value of <em>what</em></p></th>
  1858. <th class="head"><p>Meaning of <em>arg</em></p></th>
  1859. </tr>
  1860. </thead>
  1861. <tbody>
  1862. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyTrace_CALL" title="PyTrace_CALL"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_CALL</span></code></a></p></td>
  1863. <td><p>Always <a class="reference internal" href="none.html#c.Py_None" title="Py_None"><code class="xref c c-data docutils literal notranslate"><span class="pre">Py_None</span></code></a>.</p></td>
  1864. </tr>
  1865. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyTrace_EXCEPTION" title="PyTrace_EXCEPTION"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_EXCEPTION</span></code></a></p></td>
  1866. <td><p>Exception information as returned by
  1867. <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>.</p></td>
  1868. </tr>
  1869. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyTrace_LINE" title="PyTrace_LINE"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_LINE</span></code></a></p></td>
  1870. <td><p>Always <a class="reference internal" href="none.html#c.Py_None" title="Py_None"><code class="xref c c-data docutils literal notranslate"><span class="pre">Py_None</span></code></a>.</p></td>
  1871. </tr>
  1872. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyTrace_RETURN" title="PyTrace_RETURN"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_RETURN</span></code></a></p></td>
  1873. <td><p>Value being returned to the caller,
  1874. or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if caused by an exception.</p></td>
  1875. </tr>
  1876. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyTrace_C_CALL" title="PyTrace_C_CALL"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_C_CALL</span></code></a></p></td>
  1877. <td><p>Function object being called.</p></td>
  1878. </tr>
  1879. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyTrace_C_EXCEPTION" title="PyTrace_C_EXCEPTION"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_C_EXCEPTION</span></code></a></p></td>
  1880. <td><p>Function object being called.</p></td>
  1881. </tr>
  1882. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyTrace_C_RETURN" title="PyTrace_C_RETURN"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_C_RETURN</span></code></a></p></td>
  1883. <td><p>Function object being called.</p></td>
  1884. </tr>
  1885. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyTrace_OPCODE" title="PyTrace_OPCODE"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_OPCODE</span></code></a></p></td>
  1886. <td><p>Always <a class="reference internal" href="none.html#c.Py_None" title="Py_None"><code class="xref c c-data docutils literal notranslate"><span class="pre">Py_None</span></code></a>.</p></td>
  1887. </tr>
  1888. </tbody>
  1889. </table>
  1890. </dd></dl>
  1891. <dl class="c var">
  1892. <dt class="sig sig-object c" id="c.PyTrace_CALL">
  1893. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTrace_CALL</span></span></span><a class="headerlink" href="#c.PyTrace_CALL" title="Link to this definition">¶</a><br /></dt>
  1894. <dd><p>The value of the <em>what</em> parameter to a <a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tracefunc</span></code></a> function when a new
  1895. call to a function or method is being reported, or a new entry into a generator.
  1896. Note that the creation of the iterator for a generator function is not reported
  1897. as there is no control transfer to the Python bytecode in the corresponding
  1898. frame.</p>
  1899. </dd></dl>
  1900. <dl class="c var">
  1901. <dt class="sig sig-object c" id="c.PyTrace_EXCEPTION">
  1902. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTrace_EXCEPTION</span></span></span><a class="headerlink" href="#c.PyTrace_EXCEPTION" title="Link to this definition">¶</a><br /></dt>
  1903. <dd><p>The value of the <em>what</em> parameter to a <a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tracefunc</span></code></a> function when an
  1904. exception has been raised. The callback function is called with this value for
  1905. <em>what</em> when after any bytecode is processed after which the exception becomes
  1906. set within the frame being executed. The effect of this is that as exception
  1907. propagation causes the Python stack to unwind, the callback is called upon
  1908. return to each frame as the exception propagates. Only trace functions receives
  1909. these events; they are not needed by the profiler.</p>
  1910. </dd></dl>
  1911. <dl class="c var">
  1912. <dt class="sig sig-object c" id="c.PyTrace_LINE">
  1913. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTrace_LINE</span></span></span><a class="headerlink" href="#c.PyTrace_LINE" title="Link to this definition">¶</a><br /></dt>
  1914. <dd><p>The value passed as the <em>what</em> parameter to a <a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tracefunc</span></code></a> function
  1915. (but not a profiling function) when a line-number event is being reported.
  1916. It may be disabled for a frame by setting <a class="reference internal" href="../reference/datamodel.html#frame.f_trace_lines" title="frame.f_trace_lines"><code class="xref py py-attr docutils literal notranslate"><span class="pre">f_trace_lines</span></code></a> to
  1917. <em>0</em> on that frame.</p>
  1918. </dd></dl>
  1919. <dl class="c var">
  1920. <dt class="sig sig-object c" id="c.PyTrace_RETURN">
  1921. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTrace_RETURN</span></span></span><a class="headerlink" href="#c.PyTrace_RETURN" title="Link to this definition">¶</a><br /></dt>
  1922. <dd><p>The value for the <em>what</em> parameter to <a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tracefunc</span></code></a> functions when a
  1923. call is about to return.</p>
  1924. </dd></dl>
  1925. <dl class="c var">
  1926. <dt class="sig sig-object c" id="c.PyTrace_C_CALL">
  1927. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTrace_C_CALL</span></span></span><a class="headerlink" href="#c.PyTrace_C_CALL" title="Link to this definition">¶</a><br /></dt>
  1928. <dd><p>The value for the <em>what</em> parameter to <a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tracefunc</span></code></a> functions when a C
  1929. function is about to be called.</p>
  1930. </dd></dl>
  1931. <dl class="c var">
  1932. <dt class="sig sig-object c" id="c.PyTrace_C_EXCEPTION">
  1933. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTrace_C_EXCEPTION</span></span></span><a class="headerlink" href="#c.PyTrace_C_EXCEPTION" title="Link to this definition">¶</a><br /></dt>
  1934. <dd><p>The value for the <em>what</em> parameter to <a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tracefunc</span></code></a> functions when a C
  1935. function has raised an exception.</p>
  1936. </dd></dl>
  1937. <dl class="c var">
  1938. <dt class="sig sig-object c" id="c.PyTrace_C_RETURN">
  1939. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTrace_C_RETURN</span></span></span><a class="headerlink" href="#c.PyTrace_C_RETURN" title="Link to this definition">¶</a><br /></dt>
  1940. <dd><p>The value for the <em>what</em> parameter to <a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tracefunc</span></code></a> functions when a C
  1941. function has returned.</p>
  1942. </dd></dl>
  1943. <dl class="c var">
  1944. <dt class="sig sig-object c" id="c.PyTrace_OPCODE">
  1945. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyTrace_OPCODE</span></span></span><a class="headerlink" href="#c.PyTrace_OPCODE" title="Link to this definition">¶</a><br /></dt>
  1946. <dd><p>The value for the <em>what</em> parameter to <a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tracefunc</span></code></a> functions (but not
  1947. profiling functions) when a new opcode is about to be executed. This event is
  1948. not emitted by default: it must be explicitly requested by setting
  1949. <a class="reference internal" href="../reference/datamodel.html#frame.f_trace_opcodes" title="frame.f_trace_opcodes"><code class="xref py py-attr docutils literal notranslate"><span class="pre">f_trace_opcodes</span></code></a> to <em>1</em> on the frame.</p>
  1950. </dd></dl>
  1951. <dl class="c function">
  1952. <dt class="sig sig-object c" id="c.PyEval_SetProfile">
  1953. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_SetProfile</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><span class="n"><span class="pre">Py_tracefunc</span></span></a><span class="w"> </span><span class="n"><span class="pre">func</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_SetProfile" title="Link to this definition">¶</a><br /></dt>
  1954. <dd><p>Set the profiler function to <em>func</em>. The <em>obj</em> parameter is passed to the
  1955. function as its first parameter, and may be any Python object, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. If
  1956. the profile function needs to maintain state, using a different value for <em>obj</em>
  1957. for each thread provides a convenient and thread-safe place to store it. The
  1958. profile function is called for all monitored events except <a class="reference internal" href="#c.PyTrace_LINE" title="PyTrace_LINE"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_LINE</span></code></a>
  1959. <a class="reference internal" href="#c.PyTrace_OPCODE" title="PyTrace_OPCODE"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_OPCODE</span></code></a> and <a class="reference internal" href="#c.PyTrace_EXCEPTION" title="PyTrace_EXCEPTION"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_EXCEPTION</span></code></a>.</p>
  1960. <p>See also the <a class="reference internal" href="../library/sys.html#sys.setprofile" title="sys.setprofile"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.setprofile()</span></code></a> function.</p>
  1961. <p>The caller must hold the <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a>.</p>
  1962. </dd></dl>
  1963. <dl class="c function">
  1964. <dt class="sig sig-object c" id="c.PyEval_SetProfileAllThreads">
  1965. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_SetProfileAllThreads</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><span class="n"><span class="pre">Py_tracefunc</span></span></a><span class="w"> </span><span class="n"><span class="pre">func</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_SetProfileAllThreads" title="Link to this definition">¶</a><br /></dt>
  1966. <dd><p>Like <a class="reference internal" href="#c.PyEval_SetProfile" title="PyEval_SetProfile"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetProfile()</span></code></a> but sets the profile function in all running threads
  1967. belonging to the current interpreter instead of the setting it only on the current thread.</p>
  1968. <p>The caller must hold the <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a>.</p>
  1969. <p>As <a class="reference internal" href="#c.PyEval_SetProfile" title="PyEval_SetProfile"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetProfile()</span></code></a>, this function ignores any exceptions raised while
  1970. setting the profile functions in all threads.</p>
  1971. </dd></dl>
  1972. <div class="versionadded">
  1973. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1974. </div>
  1975. <dl class="c function">
  1976. <dt class="sig sig-object c" id="c.PyEval_SetTrace">
  1977. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_SetTrace</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><span class="n"><span class="pre">Py_tracefunc</span></span></a><span class="w"> </span><span class="n"><span class="pre">func</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_SetTrace" title="Link to this definition">¶</a><br /></dt>
  1978. <dd><p>Set the tracing function to <em>func</em>. This is similar to
  1979. <a class="reference internal" href="#c.PyEval_SetProfile" title="PyEval_SetProfile"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetProfile()</span></code></a>, except the tracing function does receive line-number
  1980. events and per-opcode events, but does not receive any event related to C function
  1981. objects being called. Any trace function registered using <a class="reference internal" href="#c.PyEval_SetTrace" title="PyEval_SetTrace"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetTrace()</span></code></a>
  1982. will not receive <a class="reference internal" href="#c.PyTrace_C_CALL" title="PyTrace_C_CALL"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_C_CALL</span></code></a>, <a class="reference internal" href="#c.PyTrace_C_EXCEPTION" title="PyTrace_C_EXCEPTION"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_C_EXCEPTION</span></code></a> or
  1983. <a class="reference internal" href="#c.PyTrace_C_RETURN" title="PyTrace_C_RETURN"><code class="xref c c-data docutils literal notranslate"><span class="pre">PyTrace_C_RETURN</span></code></a> as a value for the <em>what</em> parameter.</p>
  1984. <p>See also the <a class="reference internal" href="../library/sys.html#sys.settrace" title="sys.settrace"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.settrace()</span></code></a> function.</p>
  1985. <p>The caller must hold the <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a>.</p>
  1986. </dd></dl>
  1987. <dl class="c function">
  1988. <dt class="sig sig-object c" id="c.PyEval_SetTraceAllThreads">
  1989. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyEval_SetTraceAllThreads</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_tracefunc" title="Py_tracefunc"><span class="n"><span class="pre">Py_tracefunc</span></span></a><span class="w"> </span><span class="n"><span class="pre">func</span></span>, <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyEval_SetTraceAllThreads" title="Link to this definition">¶</a><br /></dt>
  1990. <dd><p>Like <a class="reference internal" href="#c.PyEval_SetTrace" title="PyEval_SetTrace"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetTrace()</span></code></a> but sets the tracing function in all running threads
  1991. belonging to the current interpreter instead of the setting it only on the current thread.</p>
  1992. <p>The caller must hold the <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a>.</p>
  1993. <p>As <a class="reference internal" href="#c.PyEval_SetTrace" title="PyEval_SetTrace"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyEval_SetTrace()</span></code></a>, this function ignores any exceptions raised while
  1994. setting the trace functions in all threads.</p>
  1995. </dd></dl>
  1996. <div class="versionadded">
  1997. <p><span class="versionmodified added">New in version 3.12.</span></p>
  1998. </div>
  1999. </section>
  2000. <section id="advanced-debugger-support">
  2001. <span id="advanced-debugging"></span><h2>Advanced Debugger Support<a class="headerlink" href="#advanced-debugger-support" title="Link to this heading">¶</a></h2>
  2002. <p>These functions are only intended to be used by advanced debugging tools.</p>
  2003. <dl class="c function">
  2004. <dt class="sig sig-object c" id="c.PyInterpreterState_Head">
  2005. <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState_Head</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyInterpreterState_Head" title="Link to this definition">¶</a><br /></dt>
  2006. <dd><p>Return the interpreter state object at the head of the list of all such objects.</p>
  2007. </dd></dl>
  2008. <dl class="c function">
  2009. <dt class="sig sig-object c" id="c.PyInterpreterState_Main">
  2010. <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState_Main</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyInterpreterState_Main" title="Link to this definition">¶</a><br /></dt>
  2011. <dd><p>Return the main interpreter state object.</p>
  2012. </dd></dl>
  2013. <dl class="c function">
  2014. <dt class="sig sig-object c" id="c.PyInterpreterState_Next">
  2015. <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState_Next</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">interp</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyInterpreterState_Next" title="Link to this definition">¶</a><br /></dt>
  2016. <dd><p>Return the next interpreter state object after <em>interp</em> from the list of all
  2017. such objects.</p>
  2018. </dd></dl>
  2019. <dl class="c function">
  2020. <dt class="sig sig-object c" id="c.PyInterpreterState_ThreadHead">
  2021. <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyInterpreterState_ThreadHead</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><span class="n"><span class="pre">PyInterpreterState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">interp</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyInterpreterState_ThreadHead" title="Link to this definition">¶</a><br /></dt>
  2022. <dd><p>Return the pointer to the first <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyThreadState</span></code></a> object in the list of
  2023. threads associated with the interpreter <em>interp</em>.</p>
  2024. </dd></dl>
  2025. <dl class="c function">
  2026. <dt class="sig sig-object c" id="c.PyThreadState_Next">
  2027. <a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyThreadState_Next</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.PyThreadState" title="PyThreadState"><span class="n"><span class="pre">PyThreadState</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tstate</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThreadState_Next" title="Link to this definition">¶</a><br /></dt>
  2028. <dd><p>Return the next thread state object after <em>tstate</em> from the list of all such
  2029. objects belonging to the same <a class="reference internal" href="#c.PyInterpreterState" title="PyInterpreterState"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyInterpreterState</span></code></a> object.</p>
  2030. </dd></dl>
  2031. </section>
  2032. <section id="thread-local-storage-support">
  2033. <span id="thread-local-storage"></span><h2>Thread Local Storage Support<a class="headerlink" href="#thread-local-storage-support" title="Link to this heading">¶</a></h2>
  2034. <p>The Python interpreter provides low-level support for thread-local storage
  2035. (TLS) which wraps the underlying native TLS implementation to support the
  2036. Python-level thread local storage API (<a class="reference internal" href="../library/threading.html#threading.local" title="threading.local"><code class="xref py py-class docutils literal notranslate"><span class="pre">threading.local</span></code></a>). The
  2037. CPython C level APIs are similar to those offered by pthreads and Windows:
  2038. use a thread key and functions to associate a <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> value per
  2039. thread.</p>
  2040. <p>The GIL does <em>not</em> need to be held when calling these functions; they supply
  2041. their own locking.</p>
  2042. <p>Note that <code class="file docutils literal notranslate"><span class="pre">Python.h</span></code> does not include the declaration of the TLS APIs,
  2043. you need to include <code class="file docutils literal notranslate"><span class="pre">pythread.h</span></code> to use thread-local storage.</p>
  2044. <div class="admonition note">
  2045. <p class="admonition-title">Note</p>
  2046. <p>None of these API functions handle memory management on behalf of the
  2047. <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> values. You need to allocate and deallocate them yourself.
  2048. If the <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> values happen to be <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>, these
  2049. functions don’t do refcount operations on them either.</p>
  2050. </div>
  2051. <section id="thread-specific-storage-tss-api">
  2052. <span id="thread-specific-storage-api"></span><h3>Thread Specific Storage (TSS) API<a class="headerlink" href="#thread-specific-storage-tss-api" title="Link to this heading">¶</a></h3>
  2053. <p>TSS API is introduced to supersede the use of the existing TLS API within the
  2054. CPython interpreter. This API uses a new type <a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tss_t</span></code></a> instead of
  2055. <span class="c-expr sig sig-inline c"><span class="kt">int</span></span> to represent thread keys.</p>
  2056. <div class="versionadded">
  2057. <p><span class="versionmodified added">New in version 3.7.</span></p>
  2058. </div>
  2059. <div class="admonition seealso">
  2060. <p class="admonition-title">See also</p>
  2061. <p>“A New C-API for Thread-Local Storage in CPython” (<span class="target" id="index-48"></span><a class="pep reference external" href="https://peps.python.org/pep-0539/"><strong>PEP 539</strong></a>)</p>
  2062. </div>
  2063. <dl class="c type">
  2064. <dt class="sig sig-object c" id="c.Py_tss_t">
  2065. <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_tss_t</span></span></span><a class="headerlink" href="#c.Py_tss_t" title="Link to this definition">¶</a><br /></dt>
  2066. <dd><p>This data structure represents the state of a thread key, the definition of
  2067. which may depend on the underlying TLS implementation, and it has an
  2068. internal field representing the key’s initialization state. There are no
  2069. public members in this structure.</p>
  2070. <p>When <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Py_LIMITED_API</span></a> is not defined, static allocation of
  2071. this type by <a class="reference internal" href="#c.Py_tss_NEEDS_INIT" title="Py_tss_NEEDS_INIT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_tss_NEEDS_INIT</span></code></a> is allowed.</p>
  2072. </dd></dl>
  2073. <dl class="c macro">
  2074. <dt class="sig sig-object c" id="c.Py_tss_NEEDS_INIT">
  2075. <span class="sig-name descname"><span class="n"><span class="pre">Py_tss_NEEDS_INIT</span></span></span><a class="headerlink" href="#c.Py_tss_NEEDS_INIT" title="Link to this definition">¶</a><br /></dt>
  2076. <dd><p>This macro expands to the initializer for <a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tss_t</span></code></a> variables.
  2077. Note that this macro won’t be defined with <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Py_LIMITED_API</span></a>.</p>
  2078. </dd></dl>
  2079. <section id="dynamic-allocation">
  2080. <h4>Dynamic Allocation<a class="headerlink" href="#dynamic-allocation" title="Link to this heading">¶</a></h4>
  2081. <p>Dynamic allocation of the <a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tss_t</span></code></a>, required in extension modules
  2082. built with <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Py_LIMITED_API</span></a>, where static allocation of this type
  2083. is not possible due to its implementation being opaque at build time.</p>
  2084. <dl class="c function">
  2085. <dt class="sig sig-object c" id="c.PyThread_tss_alloc">
  2086. <a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><span class="n"><span class="pre">Py_tss_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_tss_alloc</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_tss_alloc" title="Link to this definition">¶</a><br /></dt>
  2087. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Return a value which is the same state as a value initialized with
  2088. <a class="reference internal" href="#c.Py_tss_NEEDS_INIT" title="Py_tss_NEEDS_INIT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_tss_NEEDS_INIT</span></code></a>, or <code class="docutils literal notranslate"><span class="pre">NULL</span></code> in the case of dynamic allocation
  2089. failure.</p>
  2090. </dd></dl>
  2091. <dl class="c function">
  2092. <dt class="sig sig-object c" id="c.PyThread_tss_free">
  2093. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_tss_free</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><span class="n"><span class="pre">Py_tss_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">key</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_tss_free" title="Link to this definition">¶</a><br /></dt>
  2094. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Free the given <em>key</em> allocated by <a class="reference internal" href="#c.PyThread_tss_alloc" title="PyThread_tss_alloc"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThread_tss_alloc()</span></code></a>, after
  2095. first calling <a class="reference internal" href="#c.PyThread_tss_delete" title="PyThread_tss_delete"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThread_tss_delete()</span></code></a> to ensure any associated
  2096. thread locals have been unassigned. This is a no-op if the <em>key</em>
  2097. argument is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  2098. <div class="admonition note">
  2099. <p class="admonition-title">Note</p>
  2100. <p>A freed key becomes a dangling pointer. You should reset the key to
  2101. <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  2102. </div>
  2103. </dd></dl>
  2104. </section>
  2105. <section id="methods">
  2106. <h4>Methods<a class="headerlink" href="#methods" title="Link to this heading">¶</a></h4>
  2107. <p>The parameter <em>key</em> of these functions must not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. Moreover, the
  2108. behaviors of <a class="reference internal" href="#c.PyThread_tss_set" title="PyThread_tss_set"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThread_tss_set()</span></code></a> and <a class="reference internal" href="#c.PyThread_tss_get" title="PyThread_tss_get"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThread_tss_get()</span></code></a> are
  2109. undefined if the given <a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tss_t</span></code></a> has not been initialized by
  2110. <a class="reference internal" href="#c.PyThread_tss_create" title="PyThread_tss_create"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThread_tss_create()</span></code></a>.</p>
  2111. <dl class="c function">
  2112. <dt class="sig sig-object c" id="c.PyThread_tss_is_created">
  2113. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_tss_is_created</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><span class="n"><span class="pre">Py_tss_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">key</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_tss_is_created" title="Link to this definition">¶</a><br /></dt>
  2114. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Return a non-zero value if the given <a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_tss_t</span></code></a> has been initialized
  2115. by <a class="reference internal" href="#c.PyThread_tss_create" title="PyThread_tss_create"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThread_tss_create()</span></code></a>.</p>
  2116. </dd></dl>
  2117. <dl class="c function">
  2118. <dt class="sig sig-object c" id="c.PyThread_tss_create">
  2119. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_tss_create</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><span class="n"><span class="pre">Py_tss_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">key</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_tss_create" title="Link to this definition">¶</a><br /></dt>
  2120. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Return a zero value on successful initialization of a TSS key. The behavior
  2121. is undefined if the value pointed to by the <em>key</em> argument is not
  2122. initialized by <a class="reference internal" href="#c.Py_tss_NEEDS_INIT" title="Py_tss_NEEDS_INIT"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_tss_NEEDS_INIT</span></code></a>. This function can be called
  2123. repeatedly on the same key – calling it on an already initialized key is a
  2124. no-op and immediately returns success.</p>
  2125. </dd></dl>
  2126. <dl class="c function">
  2127. <dt class="sig sig-object c" id="c.PyThread_tss_delete">
  2128. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_tss_delete</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><span class="n"><span class="pre">Py_tss_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">key</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_tss_delete" title="Link to this definition">¶</a><br /></dt>
  2129. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Destroy a TSS key to forget the values associated with the key across all
  2130. threads, and change the key’s initialization state to uninitialized. A
  2131. destroyed key is able to be initialized again by
  2132. <a class="reference internal" href="#c.PyThread_tss_create" title="PyThread_tss_create"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThread_tss_create()</span></code></a>. This function can be called repeatedly on
  2133. the same key – calling it on an already destroyed key is a no-op.</p>
  2134. </dd></dl>
  2135. <dl class="c function">
  2136. <dt class="sig sig-object c" id="c.PyThread_tss_set">
  2137. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_tss_set</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><span class="n"><span class="pre">Py_tss_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">key</span></span>, <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_tss_set" title="Link to this definition">¶</a><br /></dt>
  2138. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Return a zero value to indicate successfully associating a <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span>
  2139. value with a TSS key in the current thread. Each thread has a distinct
  2140. mapping of the key to a <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> value.</p>
  2141. </dd></dl>
  2142. <dl class="c function">
  2143. <dt class="sig sig-object c" id="c.PyThread_tss_get">
  2144. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_tss_get</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="#c.Py_tss_t" title="Py_tss_t"><span class="n"><span class="pre">Py_tss_t</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">key</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_tss_get" title="Link to this definition">¶</a><br /></dt>
  2145. <dd><em class="stableabi"> Part of the <a class="reference internal" href="stable.html#stable"><span class="std std-ref">Stable ABI</span></a> since version 3.7.</em><p>Return the <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span> value associated with a TSS key in the current
  2146. thread. This returns <code class="docutils literal notranslate"><span class="pre">NULL</span></code> if no value is associated with the key in the
  2147. current thread.</p>
  2148. </dd></dl>
  2149. </section>
  2150. </section>
  2151. <section id="thread-local-storage-tls-api">
  2152. <span id="thread-local-storage-api"></span><h3>Thread Local Storage (TLS) API<a class="headerlink" href="#thread-local-storage-tls-api" title="Link to this heading">¶</a></h3>
  2153. <div class="deprecated">
  2154. <p><span class="versionmodified deprecated">Deprecated since version 3.7: </span>This API is superseded by
  2155. <a class="reference internal" href="#thread-specific-storage-api"><span class="std std-ref">Thread Specific Storage (TSS) API</span></a>.</p>
  2156. </div>
  2157. <div class="admonition note">
  2158. <p class="admonition-title">Note</p>
  2159. <p>This version of the API does not support platforms where the native TLS key
  2160. is defined in a way that cannot be safely cast to <code class="docutils literal notranslate"><span class="pre">int</span></code>. On such platforms,
  2161. <a class="reference internal" href="#c.PyThread_create_key" title="PyThread_create_key"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyThread_create_key()</span></code></a> will return immediately with a failure status,
  2162. and the other TLS functions will all be no-ops on such platforms.</p>
  2163. </div>
  2164. <p>Due to the compatibility problem noted above, this version of the API should not
  2165. be used in new code.</p>
  2166. <dl class="c function">
  2167. <dt class="sig sig-object c" id="c.PyThread_create_key">
  2168. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_create_key</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_create_key" title="Link to this definition">¶</a><br /></dt>
  2169. <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></dd></dl>
  2170. <dl class="c function">
  2171. <dt class="sig sig-object c" id="c.PyThread_delete_key">
  2172. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_delete_key</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">key</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_delete_key" title="Link to this definition">¶</a><br /></dt>
  2173. <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></dd></dl>
  2174. <dl class="c function">
  2175. <dt class="sig sig-object c" id="c.PyThread_set_key_value">
  2176. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_set_key_value</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">key</span></span>, <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">value</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_set_key_value" title="Link to this definition">¶</a><br /></dt>
  2177. <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></dd></dl>
  2178. <dl class="c function">
  2179. <dt class="sig sig-object c" id="c.PyThread_get_key_value">
  2180. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_get_key_value</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">key</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_get_key_value" title="Link to this definition">¶</a><br /></dt>
  2181. <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></dd></dl>
  2182. <dl class="c function">
  2183. <dt class="sig sig-object c" id="c.PyThread_delete_key_value">
  2184. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_delete_key_value</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">key</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_delete_key_value" title="Link to this definition">¶</a><br /></dt>
  2185. <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></dd></dl>
  2186. <dl class="c function">
  2187. <dt class="sig sig-object c" id="c.PyThread_ReInitTLS">
  2188. <span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyThread_ReInitTLS</span></span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyThread_ReInitTLS" title="Link to this definition">¶</a><br /></dt>
  2189. <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></dd></dl>
  2190. </section>
  2191. </section>
  2192. </section>
  2193. <div class="clearer"></div>
  2194. </div>
  2195. </div>
  2196. </div>
  2197. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  2198. <div class="sphinxsidebarwrapper">
  2199. <div>
  2200. <h3><a href="../contents.html">Table of Contents</a></h3>
  2201. <ul>
  2202. <li><a class="reference internal" href="#">Initialization, Finalization, and Threads</a><ul>
  2203. <li><a class="reference internal" href="#before-python-initialization">Before Python Initialization</a></li>
  2204. <li><a class="reference internal" href="#global-configuration-variables">Global configuration variables</a></li>
  2205. <li><a class="reference internal" href="#initializing-and-finalizing-the-interpreter">Initializing and finalizing the interpreter</a></li>
  2206. <li><a class="reference internal" href="#process-wide-parameters">Process-wide parameters</a></li>
  2207. <li><a class="reference internal" href="#thread-state-and-the-global-interpreter-lock">Thread State and the Global Interpreter Lock</a><ul>
  2208. <li><a class="reference internal" href="#releasing-the-gil-from-extension-code">Releasing the GIL from extension code</a></li>
  2209. <li><a class="reference internal" href="#non-python-created-threads">Non-Python created threads</a></li>
  2210. <li><a class="reference internal" href="#cautions-about-fork">Cautions about fork()</a></li>
  2211. <li><a class="reference internal" href="#high-level-api">High-level API</a></li>
  2212. <li><a class="reference internal" href="#low-level-api">Low-level API</a></li>
  2213. </ul>
  2214. </li>
  2215. <li><a class="reference internal" href="#sub-interpreter-support">Sub-interpreter support</a><ul>
  2216. <li><a class="reference internal" href="#a-per-interpreter-gil">A Per-Interpreter GIL</a></li>
  2217. <li><a class="reference internal" href="#bugs-and-caveats">Bugs and caveats</a></li>
  2218. </ul>
  2219. </li>
  2220. <li><a class="reference internal" href="#asynchronous-notifications">Asynchronous Notifications</a></li>
  2221. <li><a class="reference internal" href="#profiling-and-tracing">Profiling and Tracing</a></li>
  2222. <li><a class="reference internal" href="#advanced-debugger-support">Advanced Debugger Support</a></li>
  2223. <li><a class="reference internal" href="#thread-local-storage-support">Thread Local Storage Support</a><ul>
  2224. <li><a class="reference internal" href="#thread-specific-storage-tss-api">Thread Specific Storage (TSS) API</a><ul>
  2225. <li><a class="reference internal" href="#dynamic-allocation">Dynamic Allocation</a></li>
  2226. <li><a class="reference internal" href="#methods">Methods</a></li>
  2227. </ul>
  2228. </li>
  2229. <li><a class="reference internal" href="#thread-local-storage-tls-api">Thread Local Storage (TLS) API</a></li>
  2230. </ul>
  2231. </li>
  2232. </ul>
  2233. </li>
  2234. </ul>
  2235. </div>
  2236. <div>
  2237. <h4>Previous topic</h4>
  2238. <p class="topless"><a href="typehints.html"
  2239. title="previous chapter">Objects for Type Hinting</a></p>
  2240. </div>
  2241. <div>
  2242. <h4>Next topic</h4>
  2243. <p class="topless"><a href="init_config.html"
  2244. title="next chapter">Python Initialization Configuration</a></p>
  2245. </div>
  2246. <div role="note" aria-label="source link">
  2247. <h3>This Page</h3>
  2248. <ul class="this-page-menu">
  2249. <li><a href="../bugs.html">Report a Bug</a></li>
  2250. <li>
  2251. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/init.rst"
  2252. rel="nofollow">Show Source
  2253. </a>
  2254. </li>
  2255. </ul>
  2256. </div>
  2257. </div>
  2258. <div id="sidebarbutton" title="Collapse sidebar">
  2259. <span>«</span>
  2260. </div>
  2261. </div>
  2262. <div class="clearer"></div>
  2263. </div>
  2264. <div class="related" role="navigation" aria-label="related navigation">
  2265. <h3>Navigation</h3>
  2266. <ul>
  2267. <li class="right" style="margin-right: 10px">
  2268. <a href="../genindex.html" title="General Index"
  2269. >index</a></li>
  2270. <li class="right" >
  2271. <a href="../py-modindex.html" title="Python Module Index"
  2272. >modules</a> |</li>
  2273. <li class="right" >
  2274. <a href="init_config.html" title="Python Initialization Configuration"
  2275. >next</a> |</li>
  2276. <li class="right" >
  2277. <a href="typehints.html" title="Objects for Type Hinting"
  2278. >previous</a> |</li>
  2279. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  2280. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  2281. <li class="switchers">
  2282. <div class="language_switcher_placeholder"></div>
  2283. <div class="version_switcher_placeholder"></div>
  2284. </li>
  2285. <li>
  2286. </li>
  2287. <li id="cpython-language-and-version">
  2288. <a href="../index.html">3.12.3 Documentation</a> &#187;
  2289. </li>
  2290. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  2291. <li class="nav-item nav-item-this"><a href="">Initialization, Finalization, and Threads</a></li>
  2292. <li class="right">
  2293. <div class="inline-search" role="search">
  2294. <form class="inline-search" action="../search.html" method="get">
  2295. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  2296. <input type="submit" value="Go" />
  2297. </form>
  2298. </div>
  2299. |
  2300. </li>
  2301. <li class="right">
  2302. <label class="theme-selector-label">
  2303. Theme
  2304. <select class="theme-selector" oninput="activateTheme(this.value)">
  2305. <option value="auto" selected>Auto</option>
  2306. <option value="light">Light</option>
  2307. <option value="dark">Dark</option>
  2308. </select>
  2309. </label> |</li>
  2310. </ul>
  2311. </div>
  2312. <div class="footer">
  2313. &copy;
  2314. <a href="../copyright.html">
  2315. Copyright
  2316. </a>
  2317. 2001-2024, Python Software Foundation.
  2318. <br />
  2319. This page is licensed under the Python Software Foundation License Version 2.
  2320. <br />
  2321. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2322. <br />
  2323. See <a href="/license.html">History and License</a> for more information.<br />
  2324. <br />
  2325. The Python Software Foundation is a non-profit corporation.
  2326. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2327. <br />
  2328. <br />
  2329. Last updated on Apr 09, 2024 (13:47 UTC).
  2330. <a href="/bugs.html">Found a bug</a>?
  2331. <br />
  2332. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2333. </div>
  2334. </body>
  2335. </html>
上海开阖软件有限公司 沪ICP备12045867号-1