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.

460 line
35KB

  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="Capsules" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/capsule.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Refer to Providing a C API for an Extension Module for more information on using these objects." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Refer to Providing a C API for an Extension Module for more information on using these objects." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>Capsules &#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="Frame Objects" href="frame.html" />
  33. <link rel="prev" title="Weak Reference Objects" href="weakref.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/c-api/capsule.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. <h4>Previous topic</h4>
  84. <p class="topless"><a href="weakref.html"
  85. title="previous chapter">Weak Reference Objects</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="frame.html"
  90. title="next chapter">Frame Objects</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/capsule.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="frame.html" title="Frame Objects"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="weakref.html" title="Weak Reference Objects"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="concrete.html" accesskey="U">Concrete Objects Layer</a> &#187;</li>
  134. <li class="nav-item nav-item-this"><a href="">Capsules</a></li>
  135. <li class="right">
  136. <div class="inline-search" role="search">
  137. <form class="inline-search" action="../search.html" method="get">
  138. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  139. <input type="submit" value="Go" />
  140. </form>
  141. </div>
  142. |
  143. </li>
  144. <li class="right">
  145. <label class="theme-selector-label">
  146. Theme
  147. <select class="theme-selector" oninput="activateTheme(this.value)">
  148. <option value="auto" selected>Auto</option>
  149. <option value="light">Light</option>
  150. <option value="dark">Dark</option>
  151. </select>
  152. </label> |</li>
  153. </ul>
  154. </div>
  155. <div class="document">
  156. <div class="documentwrapper">
  157. <div class="bodywrapper">
  158. <div class="body" role="main">
  159. <section id="capsules">
  160. <span id="id1"></span><h1>Capsules<a class="headerlink" href="#capsules" title="Link to this heading">¶</a></h1>
  161. <p id="index-0">Refer to <a class="reference internal" href="../extending/extending.html#using-capsules"><span class="std std-ref">Providing a C API for an Extension Module</span></a> for more information on using these objects.</p>
  162. <div class="versionadded">
  163. <p><span class="versionmodified added">New in version 3.1.</span></p>
  164. </div>
  165. <dl class="c type">
  166. <dt class="sig sig-object c" id="c.PyCapsule">
  167. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule</span></span></span><a class="headerlink" href="#c.PyCapsule" title="Link to this definition">¶</a><br /></dt>
  168. <dd><p>This subtype of <a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyObject</span></code></a> represents an opaque value, useful for C
  169. extension modules who need to pass an opaque value (as a <span class="c-expr sig sig-inline c"><span class="kt">void</span><span class="p">*</span></span>
  170. pointer) through Python code to other C code. It is often used to make a C
  171. function pointer defined in one module available to other modules, so the
  172. regular import mechanism can be used to access C APIs defined in dynamically
  173. loaded modules.</p>
  174. </dd></dl>
  175. <dl class="c type">
  176. <dt class="sig sig-object c" id="c.PyCapsule_Destructor">
  177. <span class="k"><span class="pre">type</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_Destructor</span></span></span><a class="headerlink" href="#c.PyCapsule_Destructor" title="Link to this definition">¶</a><br /></dt>
  178. <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>The type of a destructor callback for a capsule. Defined as:</p>
  179. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="k">typedef</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">PyCapsule_Destructor</span><span class="p">)(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="p">);</span>
  180. </pre></div>
  181. </div>
  182. <p>See <a class="reference internal" href="#c.PyCapsule_New" title="PyCapsule_New"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_New()</span></code></a> for the semantics of PyCapsule_Destructor
  183. callbacks.</p>
  184. </dd></dl>
  185. <dl class="c function">
  186. <dt class="sig sig-object c" id="c.PyCapsule_CheckExact">
  187. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_CheckExact</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">p</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_CheckExact" title="Link to this definition">¶</a><br /></dt>
  188. <dd><p>Return true if its argument is a <a class="reference internal" href="#c.PyCapsule" title="PyCapsule"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a>. This function always
  189. succeeds.</p>
  190. </dd></dl>
  191. <dl class="c function">
  192. <dt class="sig sig-object c" id="c.PyCapsule_New">
  193. <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">PyCapsule_New</span></span></span><span class="sig-paren">(</span><span class="kt"><span class="pre">void</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">pointer</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">name</span></span>, <a class="reference internal" href="#c.PyCapsule_Destructor" title="PyCapsule_Destructor"><span class="n"><span class="pre">PyCapsule_Destructor</span></span></a><span class="w"> </span><span class="n"><span class="pre">destructor</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_New" title="Link to this definition">¶</a><br /></dt>
  194. <dd><em class="refcount">Return value: New 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>Create a <a class="reference internal" href="#c.PyCapsule" title="PyCapsule"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyCapsule</span></code></a> encapsulating the <em>pointer</em>. The <em>pointer</em>
  195. argument may not be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  196. <p>On failure, set an exception and return <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  197. <p>The <em>name</em> string may either be <code class="docutils literal notranslate"><span class="pre">NULL</span></code> or a pointer to a valid C string. If
  198. non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>, this string must outlive the capsule. (Though it is permitted to
  199. free it inside the <em>destructor</em>.)</p>
  200. <p>If the <em>destructor</em> argument is not <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, it will be called with the
  201. capsule as its argument when it is destroyed.</p>
  202. <p>If this capsule will be stored as an attribute of a module, the <em>name</em> should
  203. be specified as <code class="docutils literal notranslate"><span class="pre">modulename.attributename</span></code>. This will enable other modules
  204. to import the capsule using <a class="reference internal" href="#c.PyCapsule_Import" title="PyCapsule_Import"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_Import()</span></code></a>.</p>
  205. </dd></dl>
  206. <dl class="c function">
  207. <dt class="sig sig-object c" id="c.PyCapsule_GetPointer">
  208. <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">PyCapsule_GetPointer</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</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">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_GetPointer" title="Link to this definition">¶</a><br /></dt>
  209. <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>Retrieve the <em>pointer</em> stored in the capsule. On failure, set an exception
  210. and return <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  211. <p>The <em>name</em> parameter must compare exactly to the name stored in the capsule.
  212. If the name stored in the capsule is <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the <em>name</em> passed in must also
  213. be <code class="docutils literal notranslate"><span class="pre">NULL</span></code>. Python uses the C function <code class="xref c c-func docutils literal notranslate"><span class="pre">strcmp()</span></code> to compare capsule
  214. names.</p>
  215. </dd></dl>
  216. <dl class="c function">
  217. <dt class="sig sig-object c" id="c.PyCapsule_GetDestructor">
  218. <a class="reference internal" href="#c.PyCapsule_Destructor" title="PyCapsule_Destructor"><span class="n"><span class="pre">PyCapsule_Destructor</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_GetDestructor</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_GetDestructor" title="Link to this definition">¶</a><br /></dt>
  219. <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 destructor stored in the capsule. On failure, set an
  220. exception and return <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  221. <p>It is legal for a capsule to have a <code class="docutils literal notranslate"><span class="pre">NULL</span></code> destructor. This makes a <code class="docutils literal notranslate"><span class="pre">NULL</span></code>
  222. return code somewhat ambiguous; use <a class="reference internal" href="#c.PyCapsule_IsValid" title="PyCapsule_IsValid"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_IsValid()</span></code></a> or
  223. <a class="reference internal" href="exceptions.html#c.PyErr_Occurred" title="PyErr_Occurred"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Occurred()</span></code></a> to disambiguate.</p>
  224. </dd></dl>
  225. <dl class="c function">
  226. <dt class="sig sig-object c" id="c.PyCapsule_GetContext">
  227. <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">PyCapsule_GetContext</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_GetContext" title="Link to this definition">¶</a><br /></dt>
  228. <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 context stored in the capsule. On failure, set an
  229. exception and return <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  230. <p>It is legal for a capsule to have a <code class="docutils literal notranslate"><span class="pre">NULL</span></code> context. This makes a <code class="docutils literal notranslate"><span class="pre">NULL</span></code>
  231. return code somewhat ambiguous; use <a class="reference internal" href="#c.PyCapsule_IsValid" title="PyCapsule_IsValid"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_IsValid()</span></code></a> or
  232. <a class="reference internal" href="exceptions.html#c.PyErr_Occurred" title="PyErr_Occurred"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Occurred()</span></code></a> to disambiguate.</p>
  233. </dd></dl>
  234. <dl class="c function">
  235. <dt class="sig sig-object c" id="c.PyCapsule_GetName">
  236. <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">PyCapsule_GetName</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_GetName" title="Link to this definition">¶</a><br /></dt>
  237. <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 name stored in the capsule. On failure, set an exception
  238. and return <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  239. <p>It is legal for a capsule to have a <code class="docutils literal notranslate"><span class="pre">NULL</span></code> name. This makes a <code class="docutils literal notranslate"><span class="pre">NULL</span></code> return
  240. code somewhat ambiguous; use <a class="reference internal" href="#c.PyCapsule_IsValid" title="PyCapsule_IsValid"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_IsValid()</span></code></a> or
  241. <a class="reference internal" href="exceptions.html#c.PyErr_Occurred" title="PyErr_Occurred"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyErr_Occurred()</span></code></a> to disambiguate.</p>
  242. </dd></dl>
  243. <dl class="c function">
  244. <dt class="sig sig-object c" id="c.PyCapsule_Import">
  245. <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">PyCapsule_Import</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">name</span></span>, <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="n"><span class="pre">no_block</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_Import" title="Link to this definition">¶</a><br /></dt>
  246. <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>Import a pointer to a C object from a capsule attribute in a module. The
  247. <em>name</em> parameter should specify the full name to the attribute, as in
  248. <code class="docutils literal notranslate"><span class="pre">module.attribute</span></code>. The <em>name</em> stored in the capsule must match this
  249. string exactly.</p>
  250. <p>Return the capsule’s internal <em>pointer</em> on success. On failure, set an
  251. exception and return <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  252. <div class="versionchanged">
  253. <p><span class="versionmodified changed">Changed in version 3.3: </span><em>no_block</em> has no effect anymore.</p>
  254. </div>
  255. </dd></dl>
  256. <dl class="c function">
  257. <dt class="sig sig-object c" id="c.PyCapsule_IsValid">
  258. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_IsValid</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</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">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_IsValid" title="Link to this definition">¶</a><br /></dt>
  259. <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>Determines whether or not <em>capsule</em> is a valid capsule. A valid capsule is
  260. non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>, passes <a class="reference internal" href="#c.PyCapsule_CheckExact" title="PyCapsule_CheckExact"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_CheckExact()</span></code></a>, has a non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code> pointer
  261. stored in it, and its internal name matches the <em>name</em> parameter. (See
  262. <a class="reference internal" href="#c.PyCapsule_GetPointer" title="PyCapsule_GetPointer"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_GetPointer()</span></code></a> for information on how capsule names are
  263. compared.)</p>
  264. <p>In other words, if <a class="reference internal" href="#c.PyCapsule_IsValid" title="PyCapsule_IsValid"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyCapsule_IsValid()</span></code></a> returns a true value, calls to
  265. any of the accessors (any function starting with <code class="docutils literal notranslate"><span class="pre">PyCapsule_Get</span></code>) are
  266. guaranteed to succeed.</p>
  267. <p>Return a nonzero value if the object is valid and matches the name passed in.
  268. Return <code class="docutils literal notranslate"><span class="pre">0</span></code> otherwise. This function will not fail.</p>
  269. </dd></dl>
  270. <dl class="c function">
  271. <dt class="sig sig-object c" id="c.PyCapsule_SetContext">
  272. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_SetContext</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</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">context</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_SetContext" title="Link to this definition">¶</a><br /></dt>
  273. <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>Set the context pointer inside <em>capsule</em> to <em>context</em>.</p>
  274. <p>Return <code class="docutils literal notranslate"><span class="pre">0</span></code> on success. Return nonzero and set an exception on failure.</p>
  275. </dd></dl>
  276. <dl class="c function">
  277. <dt class="sig sig-object c" id="c.PyCapsule_SetDestructor">
  278. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_SetDestructor</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</span></span>, <a class="reference internal" href="#c.PyCapsule_Destructor" title="PyCapsule_Destructor"><span class="n"><span class="pre">PyCapsule_Destructor</span></span></a><span class="w"> </span><span class="n"><span class="pre">destructor</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_SetDestructor" title="Link to this definition">¶</a><br /></dt>
  279. <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>Set the destructor inside <em>capsule</em> to <em>destructor</em>.</p>
  280. <p>Return <code class="docutils literal notranslate"><span class="pre">0</span></code> on success. Return nonzero and set an exception on failure.</p>
  281. </dd></dl>
  282. <dl class="c function">
  283. <dt class="sig sig-object c" id="c.PyCapsule_SetName">
  284. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_SetName</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</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">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_SetName" title="Link to this definition">¶</a><br /></dt>
  285. <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>Set the name inside <em>capsule</em> to <em>name</em>. If non-<code class="docutils literal notranslate"><span class="pre">NULL</span></code>, the name must
  286. outlive the capsule. If the previous <em>name</em> stored in the capsule was not
  287. <code class="docutils literal notranslate"><span class="pre">NULL</span></code>, no attempt is made to free it.</p>
  288. <p>Return <code class="docutils literal notranslate"><span class="pre">0</span></code> on success. Return nonzero and set an exception on failure.</p>
  289. </dd></dl>
  290. <dl class="c function">
  291. <dt class="sig sig-object c" id="c.PyCapsule_SetPointer">
  292. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCapsule_SetPointer</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">capsule</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">pointer</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCapsule_SetPointer" title="Link to this definition">¶</a><br /></dt>
  293. <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>Set the void pointer inside <em>capsule</em> to <em>pointer</em>. The pointer may not be
  294. <code class="docutils literal notranslate"><span class="pre">NULL</span></code>.</p>
  295. <p>Return <code class="docutils literal notranslate"><span class="pre">0</span></code> on success. Return nonzero and set an exception on failure.</p>
  296. </dd></dl>
  297. </section>
  298. <div class="clearer"></div>
  299. </div>
  300. </div>
  301. </div>
  302. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  303. <div class="sphinxsidebarwrapper">
  304. <div>
  305. <h4>Previous topic</h4>
  306. <p class="topless"><a href="weakref.html"
  307. title="previous chapter">Weak Reference Objects</a></p>
  308. </div>
  309. <div>
  310. <h4>Next topic</h4>
  311. <p class="topless"><a href="frame.html"
  312. title="next chapter">Frame Objects</a></p>
  313. </div>
  314. <div role="note" aria-label="source link">
  315. <h3>This Page</h3>
  316. <ul class="this-page-menu">
  317. <li><a href="../bugs.html">Report a Bug</a></li>
  318. <li>
  319. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/capsule.rst"
  320. rel="nofollow">Show Source
  321. </a>
  322. </li>
  323. </ul>
  324. </div>
  325. </div>
  326. <div id="sidebarbutton" title="Collapse sidebar">
  327. <span>«</span>
  328. </div>
  329. </div>
  330. <div class="clearer"></div>
  331. </div>
  332. <div class="related" role="navigation" aria-label="related navigation">
  333. <h3>Navigation</h3>
  334. <ul>
  335. <li class="right" style="margin-right: 10px">
  336. <a href="../genindex.html" title="General Index"
  337. >index</a></li>
  338. <li class="right" >
  339. <a href="../py-modindex.html" title="Python Module Index"
  340. >modules</a> |</li>
  341. <li class="right" >
  342. <a href="frame.html" title="Frame Objects"
  343. >next</a> |</li>
  344. <li class="right" >
  345. <a href="weakref.html" title="Weak Reference Objects"
  346. >previous</a> |</li>
  347. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  348. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  349. <li class="switchers">
  350. <div class="language_switcher_placeholder"></div>
  351. <div class="version_switcher_placeholder"></div>
  352. </li>
  353. <li>
  354. </li>
  355. <li id="cpython-language-and-version">
  356. <a href="../index.html">3.12.3 Documentation</a> &#187;
  357. </li>
  358. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  359. <li class="nav-item nav-item-2"><a href="concrete.html" >Concrete Objects Layer</a> &#187;</li>
  360. <li class="nav-item nav-item-this"><a href="">Capsules</a></li>
  361. <li class="right">
  362. <div class="inline-search" role="search">
  363. <form class="inline-search" action="../search.html" method="get">
  364. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  365. <input type="submit" value="Go" />
  366. </form>
  367. </div>
  368. |
  369. </li>
  370. <li class="right">
  371. <label class="theme-selector-label">
  372. Theme
  373. <select class="theme-selector" oninput="activateTheme(this.value)">
  374. <option value="auto" selected>Auto</option>
  375. <option value="light">Light</option>
  376. <option value="dark">Dark</option>
  377. </select>
  378. </label> |</li>
  379. </ul>
  380. </div>
  381. <div class="footer">
  382. &copy;
  383. <a href="../copyright.html">
  384. Copyright
  385. </a>
  386. 2001-2024, Python Software Foundation.
  387. <br />
  388. This page is licensed under the Python Software Foundation License Version 2.
  389. <br />
  390. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  391. <br />
  392. See <a href="/license.html">History and License</a> for more information.<br />
  393. <br />
  394. The Python Software Foundation is a non-profit corporation.
  395. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  396. <br />
  397. <br />
  398. Last updated on Apr 09, 2024 (13:47 UTC).
  399. <a href="/bugs.html">Found a bug</a>?
  400. <br />
  401. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  402. </div>
  403. </body>
  404. </html>
上海开阖软件有限公司 沪ICP备12045867号-1