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

800 行
69KB

  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="Call Protocol" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/c-api/call.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="CPython supports two different calling protocols: tp_call and vectorcall. The tp_call Protocol: Instances of classes that set tp_call are callable. The signature of the slot is: A call is made usin..." />
  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="CPython supports two different calling protocols: tp_call and vectorcall. The tp_call Protocol: Instances of classes that set tp_call are callable. The signature of the slot is: A call is made usin..." />
  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>Call Protocol &#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="Number Protocol" href="number.html" />
  33. <link rel="prev" title="Object Protocol" href="object.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/c-api/call.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="#">Call Protocol</a><ul>
  86. <li><a class="reference internal" href="#the-tp-call-protocol">The <em>tp_call</em> Protocol</a></li>
  87. <li><a class="reference internal" href="#the-vectorcall-protocol">The Vectorcall Protocol</a><ul>
  88. <li><a class="reference internal" href="#recursion-control">Recursion Control</a></li>
  89. <li><a class="reference internal" href="#vectorcall-support-api">Vectorcall Support API</a></li>
  90. </ul>
  91. </li>
  92. <li><a class="reference internal" href="#object-calling-api">Object Calling API</a></li>
  93. <li><a class="reference internal" href="#call-support-api">Call Support API</a></li>
  94. </ul>
  95. </li>
  96. </ul>
  97. </div>
  98. <div>
  99. <h4>Previous topic</h4>
  100. <p class="topless"><a href="object.html"
  101. title="previous chapter">Object Protocol</a></p>
  102. </div>
  103. <div>
  104. <h4>Next topic</h4>
  105. <p class="topless"><a href="number.html"
  106. title="next chapter">Number Protocol</a></p>
  107. </div>
  108. <div role="note" aria-label="source link">
  109. <h3>This Page</h3>
  110. <ul class="this-page-menu">
  111. <li><a href="../bugs.html">Report a Bug</a></li>
  112. <li>
  113. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/call.rst"
  114. rel="nofollow">Show Source
  115. </a>
  116. </li>
  117. </ul>
  118. </div>
  119. </nav>
  120. </div>
  121. </div>
  122. <div class="related" role="navigation" aria-label="related navigation">
  123. <h3>Navigation</h3>
  124. <ul>
  125. <li class="right" style="margin-right: 10px">
  126. <a href="../genindex.html" title="General Index"
  127. accesskey="I">index</a></li>
  128. <li class="right" >
  129. <a href="../py-modindex.html" title="Python Module Index"
  130. >modules</a> |</li>
  131. <li class="right" >
  132. <a href="number.html" title="Number Protocol"
  133. accesskey="N">next</a> |</li>
  134. <li class="right" >
  135. <a href="object.html" title="Object Protocol"
  136. accesskey="P">previous</a> |</li>
  137. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  138. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  139. <li class="switchers">
  140. <div class="language_switcher_placeholder"></div>
  141. <div class="version_switcher_placeholder"></div>
  142. </li>
  143. <li>
  144. </li>
  145. <li id="cpython-language-and-version">
  146. <a href="../index.html">3.12.3 Documentation</a> &#187;
  147. </li>
  148. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  149. <li class="nav-item nav-item-2"><a href="abstract.html" accesskey="U">Abstract Objects Layer</a> &#187;</li>
  150. <li class="nav-item nav-item-this"><a href="">Call Protocol</a></li>
  151. <li class="right">
  152. <div class="inline-search" role="search">
  153. <form class="inline-search" action="../search.html" method="get">
  154. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  155. <input type="submit" value="Go" />
  156. </form>
  157. </div>
  158. |
  159. </li>
  160. <li class="right">
  161. <label class="theme-selector-label">
  162. Theme
  163. <select class="theme-selector" oninput="activateTheme(this.value)">
  164. <option value="auto" selected>Auto</option>
  165. <option value="light">Light</option>
  166. <option value="dark">Dark</option>
  167. </select>
  168. </label> |</li>
  169. </ul>
  170. </div>
  171. <div class="document">
  172. <div class="documentwrapper">
  173. <div class="bodywrapper">
  174. <div class="body" role="main">
  175. <section id="call-protocol">
  176. <span id="call"></span><h1>Call Protocol<a class="headerlink" href="#call-protocol" title="Link to this heading">¶</a></h1>
  177. <p>CPython supports two different calling protocols:
  178. <em>tp_call</em> and vectorcall.</p>
  179. <section id="the-tp-call-protocol">
  180. <h2>The <em>tp_call</em> Protocol<a class="headerlink" href="#the-tp-call-protocol" title="Link to this heading">¶</a></h2>
  181. <p>Instances of classes that set <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_call" title="PyTypeObject.tp_call"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_call</span></code></a> are callable.
  182. The signature of the slot is:</p>
  183. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="nf">tp_call</span><span class="p">(</span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">callable</span><span class="p">,</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">args</span><span class="p">,</span><span class="w"> </span><span class="n">PyObject</span><span class="w"> </span><span class="o">*</span><span class="n">kwargs</span><span class="p">);</span>
  184. </pre></div>
  185. </div>
  186. <p>A call is made using a tuple for the positional arguments
  187. and a dict for the keyword arguments, similarly to
  188. <code class="docutils literal notranslate"><span class="pre">callable(*args,</span> <span class="pre">**kwargs)</span></code> in Python code.
  189. <em>args</em> must be non-NULL (use an empty tuple if there are no arguments)
  190. but <em>kwargs</em> may be <em>NULL</em> if there are no keyword arguments.</p>
  191. <p>This convention is not only used by <em>tp_call</em>:
  192. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_new" title="PyTypeObject.tp_new"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_new</span></code></a> and <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_init" title="PyTypeObject.tp_init"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_init</span></code></a>
  193. also pass arguments this way.</p>
  194. <p>To call an object, use <a class="reference internal" href="#c.PyObject_Call" title="PyObject_Call"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Call()</span></code></a> or another
  195. <a class="reference internal" href="#capi-call"><span class="std std-ref">call API</span></a>.</p>
  196. </section>
  197. <section id="the-vectorcall-protocol">
  198. <span id="vectorcall"></span><h2>The Vectorcall Protocol<a class="headerlink" href="#the-vectorcall-protocol" title="Link to this heading">¶</a></h2>
  199. <div class="versionadded">
  200. <p><span class="versionmodified added">New in version 3.9.</span></p>
  201. </div>
  202. <p>The vectorcall protocol was introduced in <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0590/"><strong>PEP 590</strong></a> as an additional protocol
  203. for making calls more efficient.</p>
  204. <p>As rule of thumb, CPython will prefer the vectorcall for internal calls
  205. if the callable supports it. However, this is not a hard rule.
  206. Additionally, some third-party extensions use <em>tp_call</em> directly
  207. (rather than using <a class="reference internal" href="#c.PyObject_Call" title="PyObject_Call"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Call()</span></code></a>).
  208. Therefore, a class supporting vectorcall must also implement
  209. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_call" title="PyTypeObject.tp_call"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_call</span></code></a>.
  210. Moreover, the callable must behave the same
  211. regardless of which protocol is used.
  212. The recommended way to achieve this is by setting
  213. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_call" title="PyTypeObject.tp_call"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_call</span></code></a> to <a class="reference internal" href="#c.PyVectorcall_Call" title="PyVectorcall_Call"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyVectorcall_Call()</span></code></a>.
  214. This bears repeating:</p>
  215. <div class="admonition warning">
  216. <p class="admonition-title">Warning</p>
  217. <p>A class supporting vectorcall <strong>must</strong> also implement
  218. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_call" title="PyTypeObject.tp_call"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_call</span></code></a> with the same semantics.</p>
  219. </div>
  220. <div class="versionchanged">
  221. <p><span class="versionmodified changed">Changed in version 3.12: </span>The <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_HAVE_VECTORCALL" title="Py_TPFLAGS_HAVE_VECTORCALL"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_HAVE_VECTORCALL</span></code></a> flag is now removed from a class
  222. when the class’s <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__call__()</span></code></a> method is reassigned.
  223. (This internally sets <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_call" title="PyTypeObject.tp_call"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_call</span></code></a> only, and thus
  224. may make it behave differently than the vectorcall function.)
  225. In earlier Python versions, vectorcall should only be used with
  226. <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_IMMUTABLETYPE" title="Py_TPFLAGS_IMMUTABLETYPE"><code class="xref c c-macro docutils literal notranslate"><span class="pre">immutable</span></code></a> or static types.</p>
  227. </div>
  228. <p>A class should not implement vectorcall if that would be slower
  229. than <em>tp_call</em>. For example, if the callee needs to convert
  230. the arguments to an args tuple and kwargs dict anyway, then there is no point
  231. in implementing vectorcall.</p>
  232. <p>Classes can implement the vectorcall protocol by enabling the
  233. <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_HAVE_VECTORCALL" title="Py_TPFLAGS_HAVE_VECTORCALL"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_HAVE_VECTORCALL</span></code></a> flag and setting
  234. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_vectorcall_offset" title="PyTypeObject.tp_vectorcall_offset"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_vectorcall_offset</span></code></a> to the offset inside the
  235. object structure where a <em>vectorcallfunc</em> appears.
  236. This is a pointer to a function with the following signature:</p>
  237. <dl class="c type">
  238. <dt class="sig sig-object c" id="c.vectorcallfunc">
  239. <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">vectorcallfunc</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">callable</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="k"><span class="pre">const</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span><span class="p"><span class="pre">,</span></span><span class="w"> </span><span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">nargsf</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">kwnames</span></span><span class="p"><span class="pre">)</span></span><a class="headerlink" href="#c.vectorcallfunc" title="Link to this definition">¶</a><br /></dt>
  240. <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.12.</em></dd></dl>
  241. <ul class="simple">
  242. <li><p><em>callable</em> is the object being called.</p></li>
  243. <li><dl class="simple">
  244. <dt><em>args</em> is a C array consisting of the positional arguments followed by the</dt><dd><p>values of the keyword arguments.
  245. This can be <em>NULL</em> if there are no arguments.</p>
  246. </dd>
  247. </dl>
  248. </li>
  249. <li><dl class="simple">
  250. <dt><em>nargsf</em> is the number of positional arguments plus possibly the</dt><dd><p><a class="reference internal" href="#c.PY_VECTORCALL_ARGUMENTS_OFFSET" title="PY_VECTORCALL_ARGUMENTS_OFFSET"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PY_VECTORCALL_ARGUMENTS_OFFSET</span></code></a> flag.
  251. To get the actual number of positional arguments from <em>nargsf</em>,
  252. use <a class="reference internal" href="#c.PyVectorcall_NARGS" title="PyVectorcall_NARGS"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyVectorcall_NARGS()</span></code></a>.</p>
  253. </dd>
  254. </dl>
  255. </li>
  256. <li><dl class="simple">
  257. <dt><em>kwnames</em> is a tuple containing the names of the keyword arguments;</dt><dd><p>in other words, the keys of the kwargs dict.
  258. These names must be strings (instances of <code class="docutils literal notranslate"><span class="pre">str</span></code> or a subclass)
  259. and they must be unique.
  260. If there are no keyword arguments, then <em>kwnames</em> can instead be <em>NULL</em>.</p>
  261. </dd>
  262. </dl>
  263. </li>
  264. </ul>
  265. <dl class="c macro">
  266. <dt class="sig sig-object c" id="c.PY_VECTORCALL_ARGUMENTS_OFFSET">
  267. <span class="sig-name descname"><span class="n"><span class="pre">PY_VECTORCALL_ARGUMENTS_OFFSET</span></span></span><a class="headerlink" href="#c.PY_VECTORCALL_ARGUMENTS_OFFSET" title="Link to this definition">¶</a><br /></dt>
  268. <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.12.</em><p>If this flag is set in a vectorcall <em>nargsf</em> argument, the callee is allowed
  269. to temporarily change <code class="docutils literal notranslate"><span class="pre">args[-1]</span></code>. In other words, <em>args</em> points to
  270. argument 1 (not 0) in the allocated vector.
  271. The callee must restore the value of <code class="docutils literal notranslate"><span class="pre">args[-1]</span></code> before returning.</p>
  272. <p>For <a class="reference internal" href="#c.PyObject_VectorcallMethod" title="PyObject_VectorcallMethod"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_VectorcallMethod()</span></code></a>, this flag means instead that
  273. <code class="docutils literal notranslate"><span class="pre">args[0]</span></code> may be changed.</p>
  274. <p>Whenever they can do so cheaply (without additional allocation), callers
  275. are encouraged to use <a class="reference internal" href="#c.PY_VECTORCALL_ARGUMENTS_OFFSET" title="PY_VECTORCALL_ARGUMENTS_OFFSET"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PY_VECTORCALL_ARGUMENTS_OFFSET</span></code></a>.
  276. Doing so will allow callables such as bound methods to make their onward
  277. calls (which include a prepended <em>self</em> argument) very efficiently.</p>
  278. <div class="versionadded">
  279. <p><span class="versionmodified added">New in version 3.8.</span></p>
  280. </div>
  281. </dd></dl>
  282. <p>To call an object that implements vectorcall, use a <a class="reference internal" href="#capi-call"><span class="std std-ref">call API</span></a>
  283. function as with any other callable.
  284. <a class="reference internal" href="#c.PyObject_Vectorcall" title="PyObject_Vectorcall"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Vectorcall()</span></code></a> will usually be most efficient.</p>
  285. <div class="admonition note">
  286. <p class="admonition-title">Note</p>
  287. <p>In CPython 3.8, the vectorcall API and related functions were available
  288. provisionally under names with a leading underscore:
  289. <code class="docutils literal notranslate"><span class="pre">_PyObject_Vectorcall</span></code>, <code class="docutils literal notranslate"><span class="pre">_Py_TPFLAGS_HAVE_VECTORCALL</span></code>,
  290. <code class="docutils literal notranslate"><span class="pre">_PyObject_VectorcallMethod</span></code>, <code class="docutils literal notranslate"><span class="pre">_PyVectorcall_Function</span></code>,
  291. <code class="docutils literal notranslate"><span class="pre">_PyObject_CallOneArg</span></code>, <code class="docutils literal notranslate"><span class="pre">_PyObject_CallMethodNoArgs</span></code>,
  292. <code class="docutils literal notranslate"><span class="pre">_PyObject_CallMethodOneArg</span></code>.
  293. Additionally, <code class="docutils literal notranslate"><span class="pre">PyObject_VectorcallDict</span></code> was available as
  294. <code class="docutils literal notranslate"><span class="pre">_PyObject_FastCallDict</span></code>.
  295. The old names are still defined as aliases of the new, non-underscored names.</p>
  296. </div>
  297. <section id="recursion-control">
  298. <h3>Recursion Control<a class="headerlink" href="#recursion-control" title="Link to this heading">¶</a></h3>
  299. <p>When using <em>tp_call</em>, callees do not need to worry about
  300. <a class="reference internal" href="exceptions.html#recursion"><span class="std std-ref">recursion</span></a>: CPython uses
  301. <a class="reference internal" href="exceptions.html#c.Py_EnterRecursiveCall" title="Py_EnterRecursiveCall"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_EnterRecursiveCall()</span></code></a> and <a class="reference internal" href="exceptions.html#c.Py_LeaveRecursiveCall" title="Py_LeaveRecursiveCall"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_LeaveRecursiveCall()</span></code></a>
  302. for calls made using <em>tp_call</em>.</p>
  303. <p>For efficiency, this is not the case for calls done using vectorcall:
  304. the callee should use <em>Py_EnterRecursiveCall</em> and <em>Py_LeaveRecursiveCall</em>
  305. if needed.</p>
  306. </section>
  307. <section id="vectorcall-support-api">
  308. <h3>Vectorcall Support API<a class="headerlink" href="#vectorcall-support-api" title="Link to this heading">¶</a></h3>
  309. <dl class="c function">
  310. <dt class="sig sig-object c" id="c.PyVectorcall_NARGS">
  311. <a class="reference internal" href="intro.html#c.Py_ssize_t" title="Py_ssize_t"><span class="n"><span class="pre">Py_ssize_t</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyVectorcall_NARGS</span></span></span><span class="sig-paren">(</span><span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">nargsf</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyVectorcall_NARGS" title="Link to this definition">¶</a><br /></dt>
  312. <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.12.</em><p>Given a vectorcall <em>nargsf</em> argument, return the actual number of
  313. arguments.
  314. Currently equivalent to:</p>
  315. <div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">Py_ssize_t</span><span class="p">)(</span><span class="n">nargsf</span><span class="w"> </span><span class="o">&amp;</span><span class="w"> </span><span class="o">~</span><span class="n">PY_VECTORCALL_ARGUMENTS_OFFSET</span><span class="p">)</span>
  316. </pre></div>
  317. </div>
  318. <p>However, the function <code class="docutils literal notranslate"><span class="pre">PyVectorcall_NARGS</span></code> should be used to allow
  319. for future extensions.</p>
  320. <div class="versionadded">
  321. <p><span class="versionmodified added">New in version 3.8.</span></p>
  322. </div>
  323. </dd></dl>
  324. <dl class="c function">
  325. <dt class="sig sig-object c" id="c.PyVectorcall_Function">
  326. <a class="reference internal" href="#c.vectorcallfunc" title="vectorcallfunc"><span class="n"><span class="pre">vectorcallfunc</span></span></a><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyVectorcall_Function</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">op</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyVectorcall_Function" title="Link to this definition">¶</a><br /></dt>
  327. <dd><p>If <em>op</em> does not support the vectorcall protocol (either because the type
  328. does not or because the specific instance does not), return <em>NULL</em>.
  329. Otherwise, return the vectorcall function pointer stored in <em>op</em>.
  330. This function never raises an exception.</p>
  331. <p>This is mostly useful to check whether or not <em>op</em> supports vectorcall,
  332. which can be done by checking <code class="docutils literal notranslate"><span class="pre">PyVectorcall_Function(op)</span> <span class="pre">!=</span> <span class="pre">NULL</span></code>.</p>
  333. <div class="versionadded">
  334. <p><span class="versionmodified added">New in version 3.9.</span></p>
  335. </div>
  336. </dd></dl>
  337. <dl class="c function">
  338. <dt class="sig sig-object c" id="c.PyVectorcall_Call">
  339. <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">PyVectorcall_Call</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">callable</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">tuple</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">dict</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyVectorcall_Call" title="Link to this definition">¶</a><br /></dt>
  340. <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.12.</em><p>Call <em>callable</em>’s <a class="reference internal" href="#c.vectorcallfunc" title="vectorcallfunc"><code class="xref c c-type docutils literal notranslate"><span class="pre">vectorcallfunc</span></code></a> with positional and keyword
  341. arguments given in a tuple and dict, respectively.</p>
  342. <p>This is a specialized function, intended to be put in the
  343. <a class="reference internal" href="typeobj.html#c.PyTypeObject.tp_call" title="PyTypeObject.tp_call"><code class="xref c c-member docutils literal notranslate"><span class="pre">tp_call</span></code></a> slot or be used in an implementation of <code class="docutils literal notranslate"><span class="pre">tp_call</span></code>.
  344. It does not check the <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_HAVE_VECTORCALL" title="Py_TPFLAGS_HAVE_VECTORCALL"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_HAVE_VECTORCALL</span></code></a> flag
  345. and it does not fall back to <code class="docutils literal notranslate"><span class="pre">tp_call</span></code>.</p>
  346. <div class="versionadded">
  347. <p><span class="versionmodified added">New in version 3.8.</span></p>
  348. </div>
  349. </dd></dl>
  350. </section>
  351. </section>
  352. <section id="object-calling-api">
  353. <span id="capi-call"></span><h2>Object Calling API<a class="headerlink" href="#object-calling-api" title="Link to this heading">¶</a></h2>
  354. <p>Various functions are available for calling a Python object.
  355. Each converts its arguments to a convention supported by the called object –
  356. either <em>tp_call</em> or vectorcall.
  357. In order to do as little conversion as possible, pick one that best fits
  358. the format of data you have available.</p>
  359. <p>The following table summarizes the available functions;
  360. please see individual documentation for details.</p>
  361. <table class="docutils align-default">
  362. <thead>
  363. <tr class="row-odd"><th class="head"><p>Function</p></th>
  364. <th class="head"><p>callable</p></th>
  365. <th class="head"><p>args</p></th>
  366. <th class="head"><p>kwargs</p></th>
  367. </tr>
  368. </thead>
  369. <tbody>
  370. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyObject_Call" title="PyObject_Call"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Call()</span></code></a></p></td>
  371. <td><p><code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code></p></td>
  372. <td><p>tuple</p></td>
  373. <td><p>dict/<code class="docutils literal notranslate"><span class="pre">NULL</span></code></p></td>
  374. </tr>
  375. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyObject_CallNoArgs" title="PyObject_CallNoArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallNoArgs()</span></code></a></p></td>
  376. <td><p><code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code></p></td>
  377. <td><p>—</p></td>
  378. <td><p>—</p></td>
  379. </tr>
  380. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyObject_CallOneArg" title="PyObject_CallOneArg"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallOneArg()</span></code></a></p></td>
  381. <td><p><code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code></p></td>
  382. <td><p>1 object</p></td>
  383. <td><p>—</p></td>
  384. </tr>
  385. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyObject_CallObject" title="PyObject_CallObject"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallObject()</span></code></a></p></td>
  386. <td><p><code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code></p></td>
  387. <td><p>tuple/<code class="docutils literal notranslate"><span class="pre">NULL</span></code></p></td>
  388. <td><p>—</p></td>
  389. </tr>
  390. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyObject_CallFunction" title="PyObject_CallFunction"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallFunction()</span></code></a></p></td>
  391. <td><p><code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code></p></td>
  392. <td><p>format</p></td>
  393. <td><p>—</p></td>
  394. </tr>
  395. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyObject_CallMethod" title="PyObject_CallMethod"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallMethod()</span></code></a></p></td>
  396. <td><p>obj + <code class="docutils literal notranslate"><span class="pre">char*</span></code></p></td>
  397. <td><p>format</p></td>
  398. <td><p>—</p></td>
  399. </tr>
  400. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyObject_CallFunctionObjArgs" title="PyObject_CallFunctionObjArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallFunctionObjArgs()</span></code></a></p></td>
  401. <td><p><code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code></p></td>
  402. <td><p>variadic</p></td>
  403. <td><p>—</p></td>
  404. </tr>
  405. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyObject_CallMethodObjArgs" title="PyObject_CallMethodObjArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallMethodObjArgs()</span></code></a></p></td>
  406. <td><p>obj + name</p></td>
  407. <td><p>variadic</p></td>
  408. <td><p>—</p></td>
  409. </tr>
  410. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyObject_CallMethodNoArgs" title="PyObject_CallMethodNoArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallMethodNoArgs()</span></code></a></p></td>
  411. <td><p>obj + name</p></td>
  412. <td><p>—</p></td>
  413. <td><p>—</p></td>
  414. </tr>
  415. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyObject_CallMethodOneArg" title="PyObject_CallMethodOneArg"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallMethodOneArg()</span></code></a></p></td>
  416. <td><p>obj + name</p></td>
  417. <td><p>1 object</p></td>
  418. <td><p>—</p></td>
  419. </tr>
  420. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyObject_Vectorcall" title="PyObject_Vectorcall"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Vectorcall()</span></code></a></p></td>
  421. <td><p><code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code></p></td>
  422. <td><p>vectorcall</p></td>
  423. <td><p>vectorcall</p></td>
  424. </tr>
  425. <tr class="row-odd"><td><p><a class="reference internal" href="#c.PyObject_VectorcallDict" title="PyObject_VectorcallDict"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_VectorcallDict()</span></code></a></p></td>
  426. <td><p><code class="docutils literal notranslate"><span class="pre">PyObject</span> <span class="pre">*</span></code></p></td>
  427. <td><p>vectorcall</p></td>
  428. <td><p>dict/<code class="docutils literal notranslate"><span class="pre">NULL</span></code></p></td>
  429. </tr>
  430. <tr class="row-even"><td><p><a class="reference internal" href="#c.PyObject_VectorcallMethod" title="PyObject_VectorcallMethod"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_VectorcallMethod()</span></code></a></p></td>
  431. <td><p>arg + name</p></td>
  432. <td><p>vectorcall</p></td>
  433. <td><p>vectorcall</p></td>
  434. </tr>
  435. </tbody>
  436. </table>
  437. <dl class="c function">
  438. <dt class="sig sig-object c" id="c.PyObject_Call">
  439. <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">PyObject_Call</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">callable</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">args</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">kwargs</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_Call" title="Link to this definition">¶</a><br /></dt>
  440. <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>Call a callable Python object <em>callable</em>, with arguments given by the
  441. tuple <em>args</em>, and named arguments given by the dictionary <em>kwargs</em>.</p>
  442. <p><em>args</em> must not be <em>NULL</em>; use an empty tuple if no arguments are needed.
  443. If no named arguments are needed, <em>kwargs</em> can be <em>NULL</em>.</p>
  444. <p>Return the result of the call on success, or raise an exception and return
  445. <em>NULL</em> on failure.</p>
  446. <p>This is the equivalent of the Python expression:
  447. <code class="docutils literal notranslate"><span class="pre">callable(*args,</span> <span class="pre">**kwargs)</span></code>.</p>
  448. </dd></dl>
  449. <dl class="c function">
  450. <dt class="sig sig-object c" id="c.PyObject_CallNoArgs">
  451. <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">PyObject_CallNoArgs</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">callable</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CallNoArgs" title="Link to this definition">¶</a><br /></dt>
  452. <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> since version 3.10.</em><p>Call a callable Python object <em>callable</em> without any arguments. It is the
  453. most efficient way to call a callable Python object without any argument.</p>
  454. <p>Return the result of the call on success, or raise an exception and return
  455. <em>NULL</em> on failure.</p>
  456. <div class="versionadded">
  457. <p><span class="versionmodified added">New in version 3.9.</span></p>
  458. </div>
  459. </dd></dl>
  460. <dl class="c function">
  461. <dt class="sig sig-object c" id="c.PyObject_CallOneArg">
  462. <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">PyObject_CallOneArg</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">callable</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">arg</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CallOneArg" title="Link to this definition">¶</a><br /></dt>
  463. <dd><em class="refcount">Return value: New reference.</em><p>Call a callable Python object <em>callable</em> with exactly 1 positional argument
  464. <em>arg</em> and no keyword arguments.</p>
  465. <p>Return the result of the call on success, or raise an exception and return
  466. <em>NULL</em> on failure.</p>
  467. <div class="versionadded">
  468. <p><span class="versionmodified added">New in version 3.9.</span></p>
  469. </div>
  470. </dd></dl>
  471. <dl class="c function">
  472. <dt class="sig sig-object c" id="c.PyObject_CallObject">
  473. <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">PyObject_CallObject</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">callable</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">args</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CallObject" title="Link to this definition">¶</a><br /></dt>
  474. <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>Call a callable Python object <em>callable</em>, with arguments given by the
  475. tuple <em>args</em>. If no arguments are needed, then <em>args</em> can be <em>NULL</em>.</p>
  476. <p>Return the result of the call on success, or raise an exception and return
  477. <em>NULL</em> on failure.</p>
  478. <p>This is the equivalent of the Python expression: <code class="docutils literal notranslate"><span class="pre">callable(*args)</span></code>.</p>
  479. </dd></dl>
  480. <dl class="c function">
  481. <dt class="sig sig-object c" id="c.PyObject_CallFunction">
  482. <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">PyObject_CallFunction</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">callable</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">format</span></span>, <span class="p"><span class="pre">...</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CallFunction" title="Link to this definition">¶</a><br /></dt>
  483. <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>Call a callable Python object <em>callable</em>, with a variable number of C arguments.
  484. The C arguments are described using a <a class="reference internal" href="arg.html#c.Py_BuildValue" title="Py_BuildValue"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_BuildValue()</span></code></a> style format
  485. string. The format can be <em>NULL</em>, indicating that no arguments are provided.</p>
  486. <p>Return the result of the call on success, or raise an exception and return
  487. <em>NULL</em> on failure.</p>
  488. <p>This is the equivalent of the Python expression: <code class="docutils literal notranslate"><span class="pre">callable(*args)</span></code>.</p>
  489. <p>Note that if you only pass <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> args,
  490. <a class="reference internal" href="#c.PyObject_CallFunctionObjArgs" title="PyObject_CallFunctionObjArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallFunctionObjArgs()</span></code></a> is a faster alternative.</p>
  491. <div class="versionchanged">
  492. <p><span class="versionmodified changed">Changed in version 3.4: </span>The type of <em>format</em> was changed from <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">*</span></code>.</p>
  493. </div>
  494. </dd></dl>
  495. <dl class="c function">
  496. <dt class="sig sig-object c" id="c.PyObject_CallMethod">
  497. <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">PyObject_CallMethod</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span>, <span class="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="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">format</span></span>, <span class="p"><span class="pre">...</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CallMethod" title="Link to this definition">¶</a><br /></dt>
  498. <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>Call the method named <em>name</em> of object <em>obj</em> with a variable number of C
  499. arguments. The C arguments are described by a <a class="reference internal" href="arg.html#c.Py_BuildValue" title="Py_BuildValue"><code class="xref c c-func docutils literal notranslate"><span class="pre">Py_BuildValue()</span></code></a> format
  500. string that should produce a tuple.</p>
  501. <p>The format can be <em>NULL</em>, indicating that no arguments are provided.</p>
  502. <p>Return the result of the call on success, or raise an exception and return
  503. <em>NULL</em> on failure.</p>
  504. <p>This is the equivalent of the Python expression:
  505. <code class="docutils literal notranslate"><span class="pre">obj.name(arg1,</span> <span class="pre">arg2,</span> <span class="pre">...)</span></code>.</p>
  506. <p>Note that if you only pass <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> args,
  507. <a class="reference internal" href="#c.PyObject_CallMethodObjArgs" title="PyObject_CallMethodObjArgs"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_CallMethodObjArgs()</span></code></a> is a faster alternative.</p>
  508. <div class="versionchanged">
  509. <p><span class="versionmodified changed">Changed in version 3.4: </span>The types of <em>name</em> and <em>format</em> were changed from <code class="docutils literal notranslate"><span class="pre">char</span> <span class="pre">*</span></code>.</p>
  510. </div>
  511. </dd></dl>
  512. <dl class="c function">
  513. <dt class="sig sig-object c" id="c.PyObject_CallFunctionObjArgs">
  514. <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">PyObject_CallFunctionObjArgs</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">callable</span></span>, <span class="p"><span class="pre">...</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CallFunctionObjArgs" title="Link to this definition">¶</a><br /></dt>
  515. <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>Call a callable Python object <em>callable</em>, with a variable number of
  516. <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> arguments. The arguments are provided as a variable number
  517. of parameters followed by <em>NULL</em>.</p>
  518. <p>Return the result of the call on success, or raise an exception and return
  519. <em>NULL</em> on failure.</p>
  520. <p>This is the equivalent of the Python expression:
  521. <code class="docutils literal notranslate"><span class="pre">callable(arg1,</span> <span class="pre">arg2,</span> <span class="pre">...)</span></code>.</p>
  522. </dd></dl>
  523. <dl class="c function">
  524. <dt class="sig sig-object c" id="c.PyObject_CallMethodObjArgs">
  525. <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">PyObject_CallMethodObjArgs</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span>, <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">name</span></span>, <span class="p"><span class="pre">...</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CallMethodObjArgs" title="Link to this definition">¶</a><br /></dt>
  526. <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>Call a method of the Python object <em>obj</em>, where the name of the method is given as a
  527. Python string object in <em>name</em>. It is called with a variable number of
  528. <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> arguments. The arguments are provided as a variable number
  529. of parameters followed by <em>NULL</em>.</p>
  530. <p>Return the result of the call on success, or raise an exception and return
  531. <em>NULL</em> on failure.</p>
  532. </dd></dl>
  533. <dl class="c function">
  534. <dt class="sig sig-object c" id="c.PyObject_CallMethodNoArgs">
  535. <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">PyObject_CallMethodNoArgs</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span>, <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">name</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CallMethodNoArgs" title="Link to this definition">¶</a><br /></dt>
  536. <dd><p>Call a method of the Python object <em>obj</em> without arguments,
  537. where the name of the method is given as a Python string object in <em>name</em>.</p>
  538. <p>Return the result of the call on success, or raise an exception and return
  539. <em>NULL</em> on failure.</p>
  540. <div class="versionadded">
  541. <p><span class="versionmodified added">New in version 3.9.</span></p>
  542. </div>
  543. </dd></dl>
  544. <dl class="c function">
  545. <dt class="sig sig-object c" id="c.PyObject_CallMethodOneArg">
  546. <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">PyObject_CallMethodOneArg</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span>, <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">name</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">arg</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_CallMethodOneArg" title="Link to this definition">¶</a><br /></dt>
  547. <dd><p>Call a method of the Python object <em>obj</em> with a single positional argument
  548. <em>arg</em>, where the name of the method is given as a Python string object in
  549. <em>name</em>.</p>
  550. <p>Return the result of the call on success, or raise an exception and return
  551. <em>NULL</em> on failure.</p>
  552. <div class="versionadded">
  553. <p><span class="versionmodified added">New in version 3.9.</span></p>
  554. </div>
  555. </dd></dl>
  556. <dl class="c function">
  557. <dt class="sig sig-object c" id="c.PyObject_Vectorcall">
  558. <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">PyObject_Vectorcall</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">callable</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="k"><span class="pre">const</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">nargsf</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">kwnames</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_Vectorcall" title="Link to this definition">¶</a><br /></dt>
  559. <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.12.</em><p>Call a callable Python object <em>callable</em>.
  560. The arguments are the same as for <a class="reference internal" href="#c.vectorcallfunc" title="vectorcallfunc"><code class="xref c c-type docutils literal notranslate"><span class="pre">vectorcallfunc</span></code></a>.
  561. If <em>callable</em> supports <a class="reference internal" href="#vectorcall">vectorcall</a>, this directly calls
  562. the vectorcall function stored in <em>callable</em>.</p>
  563. <p>Return the result of the call on success, or raise an exception and return
  564. <em>NULL</em> on failure.</p>
  565. <div class="versionadded">
  566. <p><span class="versionmodified added">New in version 3.9.</span></p>
  567. </div>
  568. </dd></dl>
  569. <dl class="c function">
  570. <dt class="sig sig-object c" id="c.PyObject_VectorcallDict">
  571. <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">PyObject_VectorcallDict</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">callable</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="k"><span class="pre">const</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">nargsf</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">kwdict</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_VectorcallDict" title="Link to this definition">¶</a><br /></dt>
  572. <dd><p>Call <em>callable</em> with positional arguments passed exactly as in the <a class="reference internal" href="#vectorcall">vectorcall</a> protocol,
  573. but with keyword arguments passed as a dictionary <em>kwdict</em>.
  574. The <em>args</em> array contains only the positional arguments.</p>
  575. <p>Regardless of which protocol is used internally,
  576. a conversion of arguments needs to be done.
  577. Therefore, this function should only be used if the caller
  578. already has a dictionary ready to use for the keyword arguments,
  579. but not a tuple for the positional arguments.</p>
  580. <div class="versionadded">
  581. <p><span class="versionmodified added">New in version 3.9.</span></p>
  582. </div>
  583. </dd></dl>
  584. <dl class="c function">
  585. <dt class="sig sig-object c" id="c.PyObject_VectorcallMethod">
  586. <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">PyObject_VectorcallMethod</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">name</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="k"><span class="pre">const</span></span><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span>, <span class="n"><span class="pre">size_t</span></span><span class="w"> </span><span class="n"><span class="pre">nargsf</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">kwnames</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyObject_VectorcallMethod" 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> since version 3.12.</em><p>Call a method using the vectorcall calling convention. The name of the method
  588. is given as a Python string <em>name</em>. The object whose method is called is
  589. <em>args[0]</em>, and the <em>args</em> array starting at <em>args[1]</em> represents the arguments
  590. of the call. There must be at least one positional argument.
  591. <em>nargsf</em> is the number of positional arguments including <em>args[0]</em>,
  592. plus <a class="reference internal" href="#c.PY_VECTORCALL_ARGUMENTS_OFFSET" title="PY_VECTORCALL_ARGUMENTS_OFFSET"><code class="xref c c-macro docutils literal notranslate"><span class="pre">PY_VECTORCALL_ARGUMENTS_OFFSET</span></code></a> if the value of <code class="docutils literal notranslate"><span class="pre">args[0]</span></code> may
  593. temporarily be changed. Keyword arguments can be passed just like in
  594. <a class="reference internal" href="#c.PyObject_Vectorcall" title="PyObject_Vectorcall"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyObject_Vectorcall()</span></code></a>.</p>
  595. <p>If the object has the <a class="reference internal" href="typeobj.html#c.Py_TPFLAGS_METHOD_DESCRIPTOR" title="Py_TPFLAGS_METHOD_DESCRIPTOR"><code class="xref c c-macro docutils literal notranslate"><span class="pre">Py_TPFLAGS_METHOD_DESCRIPTOR</span></code></a> feature,
  596. this will call the unbound method object with the full
  597. <em>args</em> vector as arguments.</p>
  598. <p>Return the result of the call on success, or raise an exception and return
  599. <em>NULL</em> on failure.</p>
  600. <div class="versionadded">
  601. <p><span class="versionmodified added">New in version 3.9.</span></p>
  602. </div>
  603. </dd></dl>
  604. </section>
  605. <section id="call-support-api">
  606. <h2>Call Support API<a class="headerlink" href="#call-support-api" title="Link to this heading">¶</a></h2>
  607. <dl class="c function">
  608. <dt class="sig sig-object c" id="c.PyCallable_Check">
  609. <span class="kt"><span class="pre">int</span></span><span class="w"> </span><span class="sig-name descname"><span class="n"><span class="pre">PyCallable_Check</span></span></span><span class="sig-paren">(</span><a class="reference internal" href="structures.html#c.PyObject" title="PyObject"><span class="n"><span class="pre">PyObject</span></span></a><span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">o</span></span><span class="sig-paren">)</span><a class="headerlink" href="#c.PyCallable_Check" title="Link to this definition">¶</a><br /></dt>
  610. <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>Determine if the object <em>o</em> is callable. Return <code class="docutils literal notranslate"><span class="pre">1</span></code> if the object is callable
  611. and <code class="docutils literal notranslate"><span class="pre">0</span></code> otherwise. This function always succeeds.</p>
  612. </dd></dl>
  613. </section>
  614. </section>
  615. <div class="clearer"></div>
  616. </div>
  617. </div>
  618. </div>
  619. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  620. <div class="sphinxsidebarwrapper">
  621. <div>
  622. <h3><a href="../contents.html">Table of Contents</a></h3>
  623. <ul>
  624. <li><a class="reference internal" href="#">Call Protocol</a><ul>
  625. <li><a class="reference internal" href="#the-tp-call-protocol">The <em>tp_call</em> Protocol</a></li>
  626. <li><a class="reference internal" href="#the-vectorcall-protocol">The Vectorcall Protocol</a><ul>
  627. <li><a class="reference internal" href="#recursion-control">Recursion Control</a></li>
  628. <li><a class="reference internal" href="#vectorcall-support-api">Vectorcall Support API</a></li>
  629. </ul>
  630. </li>
  631. <li><a class="reference internal" href="#object-calling-api">Object Calling API</a></li>
  632. <li><a class="reference internal" href="#call-support-api">Call Support API</a></li>
  633. </ul>
  634. </li>
  635. </ul>
  636. </div>
  637. <div>
  638. <h4>Previous topic</h4>
  639. <p class="topless"><a href="object.html"
  640. title="previous chapter">Object Protocol</a></p>
  641. </div>
  642. <div>
  643. <h4>Next topic</h4>
  644. <p class="topless"><a href="number.html"
  645. title="next chapter">Number Protocol</a></p>
  646. </div>
  647. <div role="note" aria-label="source link">
  648. <h3>This Page</h3>
  649. <ul class="this-page-menu">
  650. <li><a href="../bugs.html">Report a Bug</a></li>
  651. <li>
  652. <a href="https://github.com/python/cpython/blob/main/Doc/c-api/call.rst"
  653. rel="nofollow">Show Source
  654. </a>
  655. </li>
  656. </ul>
  657. </div>
  658. </div>
  659. <div id="sidebarbutton" title="Collapse sidebar">
  660. <span>«</span>
  661. </div>
  662. </div>
  663. <div class="clearer"></div>
  664. </div>
  665. <div class="related" role="navigation" aria-label="related navigation">
  666. <h3>Navigation</h3>
  667. <ul>
  668. <li class="right" style="margin-right: 10px">
  669. <a href="../genindex.html" title="General Index"
  670. >index</a></li>
  671. <li class="right" >
  672. <a href="../py-modindex.html" title="Python Module Index"
  673. >modules</a> |</li>
  674. <li class="right" >
  675. <a href="number.html" title="Number Protocol"
  676. >next</a> |</li>
  677. <li class="right" >
  678. <a href="object.html" title="Object Protocol"
  679. >previous</a> |</li>
  680. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  681. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  682. <li class="switchers">
  683. <div class="language_switcher_placeholder"></div>
  684. <div class="version_switcher_placeholder"></div>
  685. </li>
  686. <li>
  687. </li>
  688. <li id="cpython-language-and-version">
  689. <a href="../index.html">3.12.3 Documentation</a> &#187;
  690. </li>
  691. <li class="nav-item nav-item-1"><a href="index.html" >Python/C API Reference Manual</a> &#187;</li>
  692. <li class="nav-item nav-item-2"><a href="abstract.html" >Abstract Objects Layer</a> &#187;</li>
  693. <li class="nav-item nav-item-this"><a href="">Call Protocol</a></li>
  694. <li class="right">
  695. <div class="inline-search" role="search">
  696. <form class="inline-search" action="../search.html" method="get">
  697. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  698. <input type="submit" value="Go" />
  699. </form>
  700. </div>
  701. |
  702. </li>
  703. <li class="right">
  704. <label class="theme-selector-label">
  705. Theme
  706. <select class="theme-selector" oninput="activateTheme(this.value)">
  707. <option value="auto" selected>Auto</option>
  708. <option value="light">Light</option>
  709. <option value="dark">Dark</option>
  710. </select>
  711. </label> |</li>
  712. </ul>
  713. </div>
  714. <div class="footer">
  715. &copy;
  716. <a href="../copyright.html">
  717. Copyright
  718. </a>
  719. 2001-2024, Python Software Foundation.
  720. <br />
  721. This page is licensed under the Python Software Foundation License Version 2.
  722. <br />
  723. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  724. <br />
  725. See <a href="/license.html">History and License</a> for more information.<br />
  726. <br />
  727. The Python Software Foundation is a non-profit corporation.
  728. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  729. <br />
  730. <br />
  731. Last updated on Apr 09, 2024 (13:47 UTC).
  732. <a href="/bugs.html">Found a bug</a>?
  733. <br />
  734. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  735. </div>
  736. </body>
  737. </html>
上海开阖软件有限公司 沪ICP备12045867号-1