“Python/docs/3.9/c-api/call”的版本间差异

来自菜鸟教程
Python/docs/3.9/c-api/call
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:调用协议 — Python 文档}}
 
<div id="call-protocol" class="section">
 
<div id="call-protocol" class="section">
  
 
<span id="call"></span>
 
<span id="call"></span>
= Call Protocol =
+
= 呼叫协议 =
  
CPython supports two different calling protocols:
+
CPython 支持两种不同的调用协议:''tp_call'' 和 vectorcall。
''tp_call'' and vectorcall.
 
  
 
<div id="the-tp-call-protocol" class="section">
 
<div id="the-tp-call-protocol" class="section">
  
== The ''tp_call'' Protocol ==
+
== tp_call 协议 ==
  
Instances of classes that set [[../typeobj#c.PyTypeObject|<code>tp_call</code>]] are callable.
+
设置 [[../typeobj#c.PyTypeObject|tp_call]] 的类的实例是可调用的。 插槽的签名是:
The signature of the slot is:
 
  
 
<div class="highlight-c notranslate">
 
<div class="highlight-c notranslate">
第18行: 第17行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>PyObject *tp_call(PyObject *callable, PyObject *args, PyObject *kwargs);</pre>
+
<syntaxhighlight lang="c">PyObject *tp_call(PyObject *callable, PyObject *args, PyObject *kwargs);</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
A call is made using a tuple for the positional arguments
+
调用是使用元组作为位置参数和字典作为关键字参数,类似于 Python 代码中的 <code>callable(*args, **kwargs)</code>''args'' 必须是非 NULL(如果没有参数,则使用空元组)但 ''kwargs'' 可能是 ''NULL'' 如果没有关键字参数。
and a dict for the keyword arguments, similarly to
 
<code>callable(*args, **kwargs)</code> in Python code.
 
''args'' must be non-NULL (use an empty tuple if there are no arguments)
 
but ''kwargs'' may be ''NULL'' if there are no keyword arguments.
 
  
This convention is not only used by ''tp_call'':
+
这种约定不仅被 ''tp_call'' 使用:[[../typeobj#c.PyTypeObject|tp_new]] [[../typeobj#c.PyTypeObject|tp_init]] 也以这种方式传递参数。
[[../typeobj#c.PyTypeObject|<code>tp_new</code>]] and [[../typeobj#c.PyTypeObject|<code>tp_init</code>]]
 
also pass arguments this way.
 
  
To call an object, use [[#c.PyObject_Call|<code>PyObject_Call()</code>]] or other
+
要调用对象,请使用 [[#c.PyObject_Call|PyObject_Call()]] 或其他 [[#capi-call|调用 API]]
[[#capi-call|<span class="std std-ref">call API</span>]].
 
  
  
第41行: 第33行:
  
 
<span id="vectorcall"></span>
 
<span id="vectorcall"></span>
== The Vectorcall Protocol ==
+
== 矢量调用协议 ==
  
 
<div class="versionadded">
 
<div class="versionadded">
  
<span class="versionmodified added">3.9 新版功能.</span>
+
<span class="versionmodified added">3.9 版中的新功能。</span>
  
  
 
</div>
 
</div>
The vectorcall protocol was introduced in <span id="index-0" class="target"></span>[https://www.python.org/dev/peps/pep-0590 '''PEP 590'''] as an additional protocol
+
vectorcall 协议是在 <span id="index-0" class="target"></span>[https://www.python.org/dev/peps/pep-0590 PEP 590] 中引入的,作为提高调用效率的附加协议。
for making calls more efficient.
 
  
As rule of thumb, CPython will prefer the vectorcall for internal calls
+
根据经验,如果可调用对象支持,CPython 将更喜欢使用 vectorcall 进行内部调用。 然而,这不是硬性规定。 此外,一些第三方扩展直接使用 ''tp_call''(而不是使用 [[#c.PyObject_Call|PyObject_Call()]])。 因此,支持向量调用的类还必须实现 [[../typeobj#c.PyTypeObject|tp_call]]。 此外,无论使用哪种协议,可调用对象的行为都必须相同。 实现此目的的推荐方法是将 [[../typeobj#c.PyTypeObject|tp_call]] 设置为 [[#c.PyVectorcall_Call|PyVectorcall_Call()]]。 这值得重复:
if the callable supports it. However, this is not a hard rule.
 
Additionally, some third-party extensions use ''tp_call'' directly
 
(rather than using [[#c.PyObject_Call|<code>PyObject_Call()</code>]]).
 
Therefore, a class supporting vectorcall must also implement
 
[[../typeobj#c.PyTypeObject|<code>tp_call</code>]].
 
Moreover, the callable must behave the same
 
regardless of which protocol is used.
 
The recommended way to achieve this is by setting
 
[[../typeobj#c.PyTypeObject|<code>tp_call</code>]] to [[#c.PyVectorcall_Call|<code>PyVectorcall_Call()</code>]].
 
This bears repeating:
 
  
 
<div class="admonition warning">
 
<div class="admonition warning">
第68行: 第49行:
 
警告
 
警告
  
A class supporting vectorcall '''must''' also implement
+
支持 vectorcall '''must''' 的类也实现了具有相同语义的 [[../typeobj#c.PyTypeObject|tp_call]]
[[../typeobj#c.PyTypeObject|<code>tp_call</code>]] with the same semantics.
 
  
  
 
</div>
 
</div>
A class should not implement vectorcall if that would be slower
+
如果一个类比 ''tp_call'' 慢,则该类不应实现 vectorcall。 例如,如果被调用者无论如何都需要将参数转换为 args 元组和 kwargs dict,那么实现 vectorcall 就没有意义了。
than ''tp_call''. For example, if the callee needs to convert
 
the arguments to an args tuple and kwargs dict anyway, then there is no point
 
in implementing vectorcall.
 
  
Classes can implement the vectorcall protocol by enabling the
+
类可以通过启用 [[../typeobj#Py_TPFLAGS_HAVE_VECTORCALL|Py_TPFLAGS_HAVE_VECTORCALL]] 标志并将 [[../typeobj#c.PyTypeObject|tp_vectorcall_offset]] 设置为出现 ''vectorcallfunc'' 的对象结构内的偏移量来实现 vectorcall 协议。 这是一个指向具有以下签名的函数的指针:
[[../typeobj#Py_TPFLAGS_HAVE_VECTORCALL|<code>Py_TPFLAGS_HAVE_VECTORCALL</code>]] flag and setting
+
 
[[../typeobj#c.PyTypeObject|<code>tp_vectorcall_offset</code>]] to the offset inside the
+
; <span class="k"><span class="pre">typedef</span></span><span class="w"> </span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
object structure where a ''vectorcallfunc'' appears.
 
This is a pointer to a function with the following signature:
 
  
; ''typedef'' [[../structures#c|PyObject]] *(*<code>vectorcallfunc</code>)<span class="sig-paren">(</span>[[../structures#c|PyObject]] *callable, [[../structures#c|PyObject]] *''const'' *args, size_t nargsf, [[../structures#c|PyObject]] *kwnames<span class="sig-paren">)</span>
 
 
:  
 
:  
  
* ''callable'' is the object being called.
+
* ''callable'' 是被调用的对象。
* *; ''args'' is a C array consisting of the positional arguments followed by the
+
* *; ''args'' 是一个 C 数组,由位置参数后跟
*: values of the keyword arguments. This can be ''NULL'' if there are no arguments.
+
*: 关键字参数的值。 如果没有参数,这可以是 ''NULL''
* *; ''nargsf'' is the number of positional arguments plus possibly the
+
* *; ''nargsf'' 是位置参数的数量加上可能的
*: <code>PY_VECTORCALL_ARGUMENTS_OFFSET</code> flag. To get the actual number of positional arguments from ''nargsf'', use [[#c.PyVectorcall_NARGS|<code>PyVectorcall_NARGS()</code>]].
+
*: <code>PY_VECTORCALL_ARGUMENTS_OFFSET</code> 标志。 要从 ''nargsf'' 获取位置参数的实际数量,请使用 [[#c.PyVectorcall_NARGS|PyVectorcall_NARGS()]]
* *; ''kwnames'' is a tuple containing the names of the keyword arguments;
+
* *; ''kwnames'' 是一个包含关键字参数名称的元组;
*: in other words, the keys of the kwargs dict. These names must be strings (instances of <code>str</code> or a subclass) and they must be unique. If there are no keyword arguments, then ''kwnames'' can instead be ''NULL''.
+
*: 换句话说,kwargs 字典的键。 这些名称必须是字符串(<code>str</code> 的实例或子类)并且它们必须是唯一的。 如果没有关键字参数,则 ''kwnames'' 可以改为 ''NULL''
  
 
<dl>
 
<dl>
<dt><code>PY_VECTORCALL_ARGUMENTS_OFFSET</code></dt>
+
<dt><span class="sig-name descname"><span class="n"><span class="pre">PY_VECTORCALL_ARGUMENTS_OFFSET</span></span></span><br />
<dd><p>If this flag is set in a vectorcall ''nargsf'' argument, the callee is allowed
+
</dt>
to temporarily change <code>args[-1]</code>. In other words, ''args'' points to
+
<dd><p>如果在 vectorcall ''nargsf'' 参数中设置了此标志,则允许被调用者临时更改 <code>args[-1]</code>。 换句话说,''args'' 指向分配向量中的参数 1(不是 0)。 被调用者必须在返回前恢复 <code>args[-1]</code> 的值。</p>
argument 1 (not 0) in the allocated vector.
+
<p>对于 [[#c.PyObject_VectorcallMethod|PyObject_VectorcallMethod()]],这个标志意味着 <code>args[0]</code> 可以改变。</p>
The callee must restore the value of <code>args[-1]</code> before returning.</p>
+
<p>只要他们可以便宜地做到这一点(无需额外分配),就鼓励调用者使用 <code>PY_VECTORCALL_ARGUMENTS_OFFSET</code>。 这样做将允许诸如绑定方法之类的可调用对象非常有效地进行它们的前向调用(包括一个前置的 ''self'' 参数)。</p></dd></dl>
<p>For [[#c.PyObject_VectorcallMethod|<code>PyObject_VectorcallMethod()</code>]], this flag means instead that
 
<code>args[0]</code> may be changed.</p>
 
<p>Whenever they can do so cheaply (without additional allocation), callers
 
are encouraged to use <code>PY_VECTORCALL_ARGUMENTS_OFFSET</code>.
 
Doing so will allow callables such as bound methods to make their onward
 
calls (which include a prepended ''self'' argument) very efficiently.</p></dd></dl>
 
  
To call an object that implements vectorcall, use a [[#capi-call|<span class="std std-ref">call API</span>]]
+
要调用实现 vectorcall 的对象,请使用 [[#capi-call|call API]] 函数与任何其他可调用对象一样。 [[#c.PyObject_Vectorcall|PyObject_Vectorcall()]] 通常是最有效的。
function as with any other callable.
 
[[#c.PyObject_Vectorcall|<code>PyObject_Vectorcall()</code>]] will usually be most efficient.
 
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
In CPython 3.8, the vectorcall API and related functions were available
+
CPython 3.8 中,vectorcall API 和相关函数暂时可用以下划线开头的名称:<code>_PyObject_Vectorcall</code><code>_Py_TPFLAGS_HAVE_VECTORCALL</code><code>_PyObject_VectorcallMethod</code><code>_PyVectorcall_Function</code><code>_PyObject_CallOneArg</code><code>_PyObject_CallMethodNoArgs</code><code>_PyObject_CallMethodOneArg</code>。 此外,<code>PyObject_VectorcallDict</code> 可用作 <code>_PyObject_FastCallDict</code>。 旧名称仍定义为新的非下划线名称的别名。
provisionally under names with a leading underscore:
 
<code>_PyObject_Vectorcall</code>, <code>_Py_TPFLAGS_HAVE_VECTORCALL</code>,
 
<code>_PyObject_VectorcallMethod</code>, <code>_PyVectorcall_Function</code>,
 
<code>_PyObject_CallOneArg</code>, <code>_PyObject_CallMethodNoArgs</code>,
 
<code>_PyObject_CallMethodOneArg</code>.
 
Additionally, <code>PyObject_VectorcallDict</code> was available as
 
<code>_PyObject_FastCallDict</code>.
 
The old names are still defined as aliases of the new, non-underscored names.
 
  
  
第130行: 第88行:
 
<div id="recursion-control" class="section">
 
<div id="recursion-control" class="section">
  
=== Recursion Control ===
+
=== 递归控制 ===
  
When using ''tp_call'', callees do not need to worry about
+
使用''tp_call''时,被调用者无需担心[[../exceptions#recursion|递归]]:CPython使用[[../exceptions#c|Py_EnterRecursiveCall()]][[../exceptions#c|Py_LeaveRecursiveCall()]]进行调用''tp_call''
[[../exceptions#recursion|<span class="std std-ref">recursion</span>]]: CPython uses
 
[[../exceptions#c|<code>Py_EnterRecursiveCall()</code>]] and [[../exceptions#c|<code>Py_LeaveRecursiveCall()</code>]]
 
for calls made using ''tp_call''.
 
  
For efficiency, this is not the case for calls done using vectorcall:
+
为了提高效率,使用 vectorcall 完成的调用不是这种情况:如果需要,被调用者应该使用 ''Py_EnterRecursiveCall'' ''Py_LeaveRecursiveCall''
the callee should use ''Py_EnterRecursiveCall'' and ''Py_LeaveRecursiveCall''
 
if needed.
 
  
  
第145行: 第98行:
 
<div id="vectorcall-support-api" class="section">
 
<div id="vectorcall-support-api" class="section">
  
=== Vectorcall Support API ===
+
=== 矢量调用支持 API ===
  
 
<dl>
 
<dl>
<dt>Py_ssize_t <code>PyVectorcall_NARGS</code><span class="sig-paren">(</span>size_t ''nargsf''<span class="sig-paren">)</span></dt>
+
<dt><span class="n"><span class="pre">Py_ssize_t</span></span><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><br />
<dd><p>Given a vectorcall ''nargsf'' argument, return the actual number of
+
</dt>
arguments.
+
<dd><p>给定一个 vectorcall ''nargsf'' 参数,返回参数的实际数量。 目前相当于:</p>
Currently equivalent to:</p>
 
 
<div class="highlight-c notranslate">
 
<div class="highlight-c notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>(Py_ssize_t)(nargsf &amp; ~PY_VECTORCALL_ARGUMENTS_OFFSET)</pre>
+
<syntaxhighlight lang="c">(Py_ssize_t)(nargsf & ~PY_VECTORCALL_ARGUMENTS_OFFSET)</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>However, the function <code>PyVectorcall_NARGS</code> should be used to allow
+
<p>然而,函数 <code>PyVectorcall_NARGS</code> 应该用于允许未来的扩展。</p>
for future extensions.</p>
+
<p>此函数不是 [[../stable#stable|受限 API]] 的一部分。</p>
<p>This function is not part of the [[../stable#stable|<span class="std std-ref">limited API</span>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.8 新版功能.</span></p>
+
<p><span class="versionmodified added">3.8 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[#c.vectorcallfunc|vectorcallfunc]] <code>PyVectorcall_Function</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''op''<span class="sig-paren">)</span></dt>
+
<dt>[[#c.vectorcallfunc|<span class="n"><span class="pre">vectorcallfunc</span></span>]]<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>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">op</span></span><span class="sig-paren">)</span><br />
<dd><p>If ''op'' does not support the vectorcall protocol (either because the type
+
</dt>
does not or because the specific instance does not), return ''NULL''.
+
<dd><p>如果''op''不支持vectorcall协议(要么类型不支持,要么具体实例不支持),返回''NULL''。 否则,返回存储在 ''op'' 中的 vectorcall 函数指针。 此函数从不引发异常。</p>
Otherwise, return the vectorcall function pointer stored in ''op''.
+
<p>这对于检查 ''op'' 是否支持向量调用非常有用,这可以通过检查 <code>PyVectorcall_Function(op) != NULL</code> 来完成。</p>
This function never raises an exception.</p>
+
<p>此函数不是 [[../stable#stable|受限 API]] 的一部分。</p>
<p>This is mostly useful to check whether or not ''op'' supports vectorcall,
 
which can be done by checking <code>PyVectorcall_Function(op) != NULL</code>.</p>
 
<p>This function is not part of the [[../stable#stable|<span class="std std-ref">limited API</span>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.8 新版功能.</span></p>
+
<p><span class="versionmodified added">3.8 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyVectorcall_Call</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''callable'', [[../structures#c|PyObject]] *''tuple'', [[../structures#c|PyObject]] *''dict''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</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">PyVectorcall_Call</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">callable</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">tuple</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
<dd><p>Call ''callable'''s [[#c.vectorcallfunc|<code>vectorcallfunc</code>]] with positional and keyword
+
</dt>
arguments given in a tuple and dict, respectively.</p>
+
<dd><p>分别使用元组和字典中给出的位置和关键字参数调用 ''callable'' [[#c.vectorcallfunc|vectorcallfunc]]</p>
<p>This is a specialized function, intended to be put in the
+
<p>这是一个专门的函数,旨在放置在 [[../typeobj#c.PyTypeObject|tp_call]] 插槽中或用于 <code>tp_call</code> 的实现中。 它不会检查 [[../typeobj#Py_TPFLAGS_HAVE_VECTORCALL|Py_TPFLAGS_HAVE_VECTORCALL]] 标志,也不会回退到 <code>tp_call</code></p>
[[../typeobj#c.PyTypeObject|<code>tp_call</code>]] slot or be used in an implementation of <code>tp_call</code>.
+
<p>此函数不是 [[../stable#stable|受限 API]] 的一部分。</p>
It does not check the [[../typeobj#Py_TPFLAGS_HAVE_VECTORCALL|<code>Py_TPFLAGS_HAVE_VECTORCALL</code>]] flag
 
and it does not fall back to <code>tp_call</code>.</p>
 
<p>This function is not part of the [[../stable#stable|<span class="std std-ref">limited API</span>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.8 新版功能.</span></p>
+
<p><span class="versionmodified added">3.8 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第207行: 第152行:
  
 
<span id="capi-call"></span>
 
<span id="capi-call"></span>
== Object Calling API ==
+
== 对象调用API ==
  
Various functions are available for calling a Python object.
+
有多种函数可用于调用 Python 对象。 每个都将其参数转换为被调用对象支持的约定——''tp_call'' 或 vectorcall。 为了尽可能少地进行转换,请选择最适合您可用数据格式的转换。
Each converts its arguments to a convention supported by the called object –
 
either ''tp_call'' or vectorcall.
 
In order to do as litle conversion as possible, pick one that best fits
 
the format of data you have available.
 
  
The following table summarizes the available functions;
+
下表总结了可用的功能; 有关详细信息,请参阅个别文档。
please see individual documentation for details.
 
  
 
{|
 
{|
!width="44%"| Function
+
!width="44%"| 功能
!width="19%"| callable
+
!width="19%"| 可调用的
!width="21%"| args
+
!width="21%"| 参数
 
!width="16%"| kwargs
 
!width="16%"| kwargs
 
|-
 
|-
 
| [[#c.PyObject_Call|<code>PyObject_Call()</code>]]
 
| [[#c.PyObject_Call|<code>PyObject_Call()</code>]]
 
| <code>PyObject *</code>
 
| <code>PyObject *</code>
| tuple
+
| 元组
| dict/<code>NULL</code>
+
| 字典/<code>NULL</code>
 
|-
 
|-
 
| [[#c.PyObject_CallNoArgs|<code>PyObject_CallNoArgs()</code>]]
 
| [[#c.PyObject_CallNoArgs|<code>PyObject_CallNoArgs()</code>]]
 
| <code>PyObject *</code>
 
| <code>PyObject *</code>
| ---
+
|
| ---
+
|
 
|-
 
|-
 
| [[#c.PyObject_CallOneArg|<code>PyObject_CallOneArg()</code>]]
 
| [[#c.PyObject_CallOneArg|<code>PyObject_CallOneArg()</code>]]
 
| <code>PyObject *</code>
 
| <code>PyObject *</code>
| 1 object
+
| 1 个对象
| ---
+
|
 
|-
 
|-
 
| [[#c.PyObject_CallObject|<code>PyObject_CallObject()</code>]]
 
| [[#c.PyObject_CallObject|<code>PyObject_CallObject()</code>]]
 
| <code>PyObject *</code>
 
| <code>PyObject *</code>
| tuple/<code>NULL</code>
+
| 元组/<code>NULL</code>
| ---
+
|
 
|-
 
|-
 
| [[#c.PyObject_CallFunction|<code>PyObject_CallFunction()</code>]]
 
| [[#c.PyObject_CallFunction|<code>PyObject_CallFunction()</code>]]
 
| <code>PyObject *</code>
 
| <code>PyObject *</code>
| format
+
| 格式
| ---
+
|
 
|-
 
|-
 
| [[#c.PyObject_CallMethod|<code>PyObject_CallMethod()</code>]]
 
| [[#c.PyObject_CallMethod|<code>PyObject_CallMethod()</code>]]
| obj + <code>char*</code>
+
| 物镜 + <code>char*</code>
| format
+
| 格式
| ---
+
|
 
|-
 
|-
 
| [[#c.PyObject_CallFunctionObjArgs|<code>PyObject_CallFunctionObjArgs()</code>]]
 
| [[#c.PyObject_CallFunctionObjArgs|<code>PyObject_CallFunctionObjArgs()</code>]]
 
| <code>PyObject *</code>
 
| <code>PyObject *</code>
| variadic
+
| 可变参数
| ---
+
|
 
|-
 
|-
 
| [[#c.PyObject_CallMethodObjArgs|<code>PyObject_CallMethodObjArgs()</code>]]
 
| [[#c.PyObject_CallMethodObjArgs|<code>PyObject_CallMethodObjArgs()</code>]]
| obj + name
+
| 对象 + 名称
| variadic
+
| 可变参数
| ---
+
|
 
|-
 
|-
 
| [[#c.PyObject_CallMethodNoArgs|<code>PyObject_CallMethodNoArgs()</code>]]
 
| [[#c.PyObject_CallMethodNoArgs|<code>PyObject_CallMethodNoArgs()</code>]]
| obj + name
+
| 对象 + 名称
| ---
+
|
| ---
+
|
 
|-
 
|-
 
| [[#c.PyObject_CallMethodOneArg|<code>PyObject_CallMethodOneArg()</code>]]
 
| [[#c.PyObject_CallMethodOneArg|<code>PyObject_CallMethodOneArg()</code>]]
| obj + name
+
| 对象 + 名称
| 1 object
+
| 1 个对象
| ---
+
|
 
|-
 
|-
 
| [[#c.PyObject_Vectorcall|<code>PyObject_Vectorcall()</code>]]
 
| [[#c.PyObject_Vectorcall|<code>PyObject_Vectorcall()</code>]]
 
| <code>PyObject *</code>
 
| <code>PyObject *</code>
| vectorcall
+
| 向量调用
| vectorcall
+
| 向量调用
 
|-
 
|-
 
| [[#c.PyObject_VectorcallDict|<code>PyObject_VectorcallDict()</code>]]
 
| [[#c.PyObject_VectorcallDict|<code>PyObject_VectorcallDict()</code>]]
 
| <code>PyObject *</code>
 
| <code>PyObject *</code>
| vectorcall
+
| 向量调用
| dict/<code>NULL</code>
+
| 字典/<code>NULL</code>
 
|-
 
|-
 
| [[#c.PyObject_VectorcallMethod|<code>PyObject_VectorcallMethod()</code>]]
 
| [[#c.PyObject_VectorcallMethod|<code>PyObject_VectorcallMethod()</code>]]
| arg + name
+
| 参数 + 名称
| vectorcall
+
| 向量调用
| vectorcall
+
| 向量调用
 
|}
 
|}
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_Call</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''callable'', [[../structures#c|PyObject]] *''args'', [[../structures#c|PyObject]] *''kwargs''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_Call</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">callable</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Call a callable Python object ''callable'', with arguments given by the
+
<dd><p>调用一个可调用的 Python 对象 ''callable'',参数由元组 ''args'' 给出,命名参数由字典 ''kwargs'' 给出。</p>
tuple ''args'', and named arguments given by the dictionary ''kwargs''.</p>
+
<p>''args'' 不能是 ''NULL''; 如果不需要参数,请使用空元组。 如果不需要命名参数,''kwargs'' 可以是 ''NULL''</p>
<p>''args'' must not be ''NULL''; use an empty tuple if no arguments are needed.
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
If no named arguments are needed, ''kwargs'' can be ''NULL''.</p>
+
<p>这相当于 Python 表达式:<code>callable(*args, **kwargs)</code></p></dd></dl>
<p>Return the result of the call on success, or raise an exception and return
 
''NULL'' on failure.</p>
 
<p>This is the equivalent of the Python expression:
 
<code>callable(*args, **kwargs)</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_CallNoArgs</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''callable''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CallNoArgs</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
<dd><p>Call a callable Python object ''callable'' without any arguments. It is the
+
</dt>
most efficient way to call a callable Python object without any argument.</p>
+
<dd><p>不带任何参数调用可调用的 Python 对象 ''callable''。 这是在没有任何参数的情况下调用可调用 Python 对象的最有效方法。</p>
<p>Return the result of the call on success, or raise an exception and return
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
''NULL'' on failure.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.9 新版功能.</span></p>
+
<p><span class="versionmodified added">3.9 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_CallOneArg</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''callable'', [[../structures#c|PyObject]] *''arg''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CallOneArg</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">callable</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">arg</span></span><span class="sig-paren">)</span><br />
<dd><p>Call a callable Python object ''callable'' with exactly 1 positional argument
+
</dt>
''arg'' and no keyword arguments.</p>
+
<dd><p>使用恰好 1 个位置参数 ''arg'' 并且没有关键字参数调用可调用 Python 对象 ''callable''</p>
<p>Return the result of the call on success, or raise an exception and return
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
''NULL'' on failure.</p>
+
<p>此函数不是 [[../stable#stable|受限 API]] 的一部分。</p>
<p>This function is not part of the [[../stable#stable|<span class="std std-ref">limited API</span>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.9 新版功能.</span></p>
+
<p><span class="versionmodified added">3.9 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_CallObject</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''callable'', [[../structures#c|PyObject]] *''args''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CallObject</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">callable</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</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="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Call a callable Python object ''callable'', with arguments given by the
+
<dd><p>调用可调用 Python 对象 ''callable'',参数由元组 ''args'' 给出。 如果不需要参数,则 ''args'' 可以是 ''NULL''</p>
tuple ''args''. If no arguments are needed, then ''args'' can be ''NULL''.</p>
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
<p>Return the result of the call on success, or raise an exception and return
+
<p>这相当于 Python 表达式:<code>callable(*args)</code></p></dd></dl>
''NULL'' on failure.</p>
 
<p>This is the equivalent of the Python expression: <code>callable(*args)</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_CallFunction</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''callable'', ''const'' char *''format'', ...<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CallFunction</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Call a callable Python object ''callable'', with a variable number of C arguments.
+
<dd><p>使用可变数量的 C 参数调用可调用 Python 对象 ''callable''C 参数使用 [[../arg#c|Py_BuildValue()]] 样式格式字符串进行描述。 格式可以是''NULL'',表示不提供参数。</p>
The C arguments are described using a [[../arg#c|<code>Py_BuildValue()</code>]] style format
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
string. The format can be ''NULL'', indicating that no arguments are provided.</p>
+
<p>这相当于 Python 表达式:<code>callable(*args)</code></p>
<p>Return the result of the call on success, or raise an exception and return
+
<p>请注意,如果您只传递 <span class="xref c c-texpr">PyObject*</span> 参数,则 [[#c.PyObject_CallFunctionObjArgs|PyObject_CallFunctionObjArgs()]] 是一个更快的选择。</p>
''NULL'' on failure.</p>
 
<p>This is the equivalent of the Python expression: <code>callable(*args)</code>.</p>
 
<p>Note that if you only pass <span class="xref c c-texpr">[[../structures#c|PyObject]]*</span> args,
 
[[#c.PyObject_CallFunctionObjArgs|<code>PyObject_CallFunctionObjArgs()</code>]] is a faster alternative.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.4 版更改: </span>The type of ''format'' was changed from <code>char *</code>.</p>
+
<p><span class="versionmodified changed">3.4 版本变更:</span>''格式''的类型由<code>char *</code>变更。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_CallMethod</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''obj'', ''const'' char *''name'', ''const'' char *''format'', ...<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CallMethod</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Call the method named ''name'' of object ''obj'' with a variable number of C
+
<dd><p>使用可变数量的 C 参数调用对象 ''obj'' 的名为 ''name'' 的方法。 C 参数由应生成元组的 [[../arg#c|Py_BuildValue()]] 格式字符串描述。</p>
arguments. The C arguments are described by a [[../arg#c|<code>Py_BuildValue()</code>]] format
+
<p>格式可以是''NULL'',表示不提供参数。</p>
string that should produce a tuple.</p>
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
<p>The format can be ''NULL'', indicating that no arguments are provided.</p>
+
<p>这相当于 Python 表达式:<code>obj.name(arg1, arg2, ...)</code></p>
<p>Return the result of the call on success, or raise an exception and return
+
<p>请注意,如果您只传递 <span class="xref c c-texpr">PyObject*</span> 参数,则 [[#c.PyObject_CallMethodObjArgs|PyObject_CallMethodObjArgs()]] 是一个更快的选择。</p>
''NULL'' on failure.</p>
 
<p>This is the equivalent of the Python expression:
 
<code>obj.name(arg1, arg2, ...)</code>.</p>
 
<p>Note that if you only pass <span class="xref c c-texpr">[[../structures#c|PyObject]]*</span> args,
 
[[#c.PyObject_CallMethodObjArgs|<code>PyObject_CallMethodObjArgs()</code>]] is a faster alternative.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.4 版更改: </span>The types of ''name'' and ''format'' were changed from <code>char *</code>.</p>
+
<p><span class="versionmodified changed"> 3.4 版本变更: </span> ''name'' ''format'' 的类型由 <code>char *</code> 变更。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_CallFunctionObjArgs</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''callable'', ...<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CallFunctionObjArgs</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Call a callable Python object ''callable'', with a variable number of
+
<dd><p>使用可变数量的 <span class="xref c c-texpr">PyObject*</span> 参数调用可调用 Python 对象 ''callable''。 参数以可变数量的参数形式提供,后跟 ''NULL''</p>
<span class="xref c c-texpr">[[../structures#c|PyObject]]*</span> arguments. The arguments are provided as a variable number
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
of parameters followed by ''NULL''.</p>
+
<p>这相当于 Python 表达式:<code>callable(arg1, arg2, ...)</code></p></dd></dl>
<p>Return the result of the call on success, or raise an exception and return
 
''NULL'' on failure.</p>
 
<p>This is the equivalent of the Python expression:
 
<code>callable(arg1, arg2, ...)</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_CallMethodObjArgs</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''obj'', [[../structures#c|PyObject]] *''name'', ...<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CallMethodObjArgs</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</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="p"><span class="pre">...</span></span><span class="sig-paren">)</span><br />
<dd><p>''Return value: New reference.''</p>
+
</dt>
<p>Call a method of the Python object ''obj'', where the name of the method is given as a
+
<dd><p>调用 Python 对象 ''obj'' 的方法,其中方法的名称在 ''name'' 中作为 Python 字符串对象给出。 它使用可变数量的 <span class="xref c c-texpr">PyObject*</span> 参数调用。 参数以可变数量的参数形式提供,后跟 ''NULL''</p>
Python string object in ''name''. It is called with a variable number of
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p></dd></dl>
<span class="xref c c-texpr">[[../structures#c|PyObject]]*</span> arguments. The arguments are provided as a variable number
 
of parameters followed by ''NULL''.</p>
 
<p>Return the result of the call on success, or raise an exception and return
 
''NULL'' on failure.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_CallMethodNoArgs</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''obj'', [[../structures#c|PyObject]] *''name''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CallMethodNoArgs</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</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><br />
<dd><p>Call a method of the Python object ''obj'' without arguments,
+
</dt>
where the name of the method is given as a Python string object in ''name''.</p>
+
<dd><p>不带参数调用 Python 对象 ''obj'' 的方法,其中方法的名称在 ''name'' 中作为 Python 字符串对象给出。</p>
<p>Return the result of the call on success, or raise an exception and return
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
''NULL'' on failure.</p>
+
<p>此函数不是 [[../stable#stable|受限 API]] 的一部分。</p>
<p>This function is not part of the [[../stable#stable|<span class="std std-ref">limited API</span>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.9 新版功能.</span></p>
+
<p><span class="versionmodified added">3.9 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_CallMethodOneArg</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''obj'', [[../structures#c|PyObject]] *''name'', [[../structures#c|PyObject]] *''arg''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_CallMethodOneArg</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">obj</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">arg</span></span><span class="sig-paren">)</span><br />
<dd><p>Call a method of the Python object ''obj'' with a single positional argument
+
</dt>
''arg'', where the name of the method is given as a Python string object in
+
<dd><p>使用单个位置参数 ''arg'' 调用 Python 对象 ''obj'' 的方法,其中该方法的名称在 ''name'' 中作为 Python 字符串对象给出。</p>
''name''.</p>
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
<p>Return the result of the call on success, or raise an exception and return
+
<p>此函数不是 [[../stable#stable|受限 API]] 的一部分。</p>
''NULL'' on failure.</p>
 
<p>This function is not part of the [[../stable#stable|<span class="std std-ref">limited API</span>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.9 新版功能.</span></p>
+
<p><span class="versionmodified added">3.9 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_Vectorcall</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''callable'', [[../structures#c|PyObject]] *''const'' *''args'', size_t ''nargsf'', [[../structures#c|PyObject]] *''kwnames''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_Vectorcall</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">callable</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
<dd><p>Call a callable Python object ''callable''.
+
</dt>
The arguments are the same as for [[#c.vectorcallfunc|<code>vectorcallfunc</code>]].
+
<dd><p>调用一个可调用的 Python 对象 ''callable''。 参数与 [[#c.vectorcallfunc|vectorcallfunc]] 相同。 如果''callable''支持[[#vectorcall|vectorcall]],则直接调用存储在''callable''中的vectorcall函数。</p>
If ''callable'' supports [[#vectorcall|vectorcall]], this directly calls
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
the vectorcall function stored in ''callable''.</p>
+
<p>此函数不是 [[../stable#stable|受限 API]] 的一部分。</p>
<p>Return the result of the call on success, or raise an exception and return
 
''NULL'' on failure.</p>
 
<p>This function is not part of the [[../stable#stable|<span class="std std-ref">limited API</span>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.9 新版功能.</span></p>
+
<p><span class="versionmodified added">3.9 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_VectorcallDict</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''callable'', [[../structures#c|PyObject]] *''const'' *''args'', size_t ''nargsf'', [[../structures#c|PyObject]] *''kwdict''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_VectorcallDict</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">callable</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
<dd><p>Call ''callable'' with positional arguments passed exactly as in the [[#vectorcall|vectorcall]] protocol,
+
</dt>
but with keyword arguments passed as a dictionary ''kwdict''.
+
<dd><p>使用完全按照 [[#vectorcall|vectorcall]] 协议传递的位置参数调用 ''callable'',但使用作为字典 ''kwdict'' 传递的关键字参数。 ''args'' 数组只包含位置参数。</p>
The ''args'' array contains only the positional arguments.</p>
+
<p>无论内部使用哪种协议,都需要进行参数转换。 因此,仅当调用者已经准备好用于关键字参数的字典而不是位置参数的元组时,才应使用此函数。</p>
<p>Regardless of which protocol is used internally,
+
<p>此函数不是 [[../stable#stable|受限 API]] 的一部分。</p>
a conversion of arguments needs to be done.
 
Therefore, this function should only be used if the caller
 
already has a dictionary ready to use for the keyword arguments,
 
but not a tuple for the positional arguments.</p>
 
<p>This function is not part of the [[../stable#stable|<span class="std std-ref">limited API</span>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.9 新版功能.</span></p>
+
<p><span class="versionmodified added">3.9 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt>[[../structures#c|PyObject]] *<code>PyObject_VectorcallMethod</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''name'', [[../structures#c|PyObject]] *''const'' *''args'', size_t ''nargsf'', [[../structures#c|PyObject]] *''kwnames''<span class="sig-paren">)</span></dt>
+
<dt>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="sig-name descname"><span class="n"><span class="pre">PyObject_VectorcallMethod</span></span></span><span class="sig-paren">(</span>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<span class="w"> </span><span class="p"><span class="pre">*</span></span><span class="n"><span class="pre">name</span></span>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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>, [[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
<dd><p>Call a method using the vectorcall calling convention. The name of the method
+
</dt>
is given as a Python string ''name''. The object whose method is called is
+
<dd><p>使用 vectorcall 调用约定调用方法。 该方法的名称以 Python 字符串 ''name'' 的形式给出。 其方法被调用的对象是''args[0]'',从''args[1]''开始的''args''数组代表调用的参数。 必须至少有一个位置参数。 ''nargsf'' 是位置参数的数量,包括 ''args[0]'',如果 <code>args[0]</code> 的值可能会临时更改,则加上 <code>PY_VECTORCALL_ARGUMENTS_OFFSET</code>。 关键字参数可以像在 [[#c.PyObject_Vectorcall|PyObject_Vectorcall()]] 中一样传递。</p>
''args[0]'', and the ''args'' array starting at ''args[1]'' represents the arguments
+
<p>如果对象具有 [[../typeobj#Py_TPFLAGS_METHOD_DESCRIPTOR|Py_TPFLAGS_METHOD_DESCRIPTOR]] 功能,这将调用具有完整 ''args'' 向量作为参数的未绑定方法对象。</p>
of the call. There must be at least one positional argument.
+
<p>成功时返回调用结果,或引发异常并在失败时返回 ''NULL''</p>
''nargsf'' is the number of positional arguments including ''args[0]'',
+
<p>此函数不是 [[../stable#stable|受限 API]] 的一部分。</p>
plus <code>PY_VECTORCALL_ARGUMENTS_OFFSET</code> if the value of <code>args[0]</code> may
 
temporarily be changed. Keyword arguments can be passed just like in
 
[[#c.PyObject_Vectorcall|<code>PyObject_Vectorcall()</code>]].</p>
 
<p>If the object has the [[../typeobj#Py_TPFLAGS_METHOD_DESCRIPTOR|<code>Py_TPFLAGS_METHOD_DESCRIPTOR</code>]] feature,
 
this will call the unbound method object with the full
 
''args'' vector as arguments.</p>
 
<p>Return the result of the call on success, or raise an exception and return
 
''NULL'' on failure.</p>
 
<p>This function is not part of the [[../stable#stable|<span class="std std-ref">limited API</span>]].</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.9 新版功能.</span></p>
+
<p><span class="versionmodified added">3.9 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第478行: 第373行:
 
<div id="call-support-api" class="section">
 
<div id="call-support-api" class="section">
  
== Call Support API ==
+
== 呼叫支持 API ==
 +
 
 +
; <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>[[../structures#c|<span class="n"><span class="pre">PyObject</span></span>]]<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><br />
 +
 
 +
: 确定对象 ''o'' 是否可调用。 如果对象可调用,则返回 <code>1</code>,否则返回 <code>0</code>。 此功能总是成功。
  
; int <code>PyCallable_Check</code><span class="sig-paren">(</span>[[../structures#c|PyObject]] *''o''<span class="sig-paren">)</span>
 
: Determine if the object ''o'' is callable. Return <code>1</code> if the object is callable and <code>0</code> otherwise. This function always succeeds.
 
  
 +
</div>
  
 
</div>
 
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:50的最新版本

呼叫协议

CPython 支持两种不同的调用协议:tp_call 和 vectorcall。

tp_call 协议

设置 tp_call 的类的实例是可调用的。 插槽的签名是:

PyObject *tp_call(PyObject *callable, PyObject *args, PyObject *kwargs);

调用是使用元组作为位置参数和字典作为关键字参数,类似于 Python 代码中的 callable(*args, **kwargs)args 必须是非 NULL(如果没有参数,则使用空元组)但 kwargs 可能是 NULL 如果没有关键字参数。

这种约定不仅被 tp_call 使用:tp_newtp_init 也以这种方式传递参数。

要调用对象,请使用 PyObject_Call() 或其他 调用 API


矢量调用协议

3.9 版中的新功能。


vectorcall 协议是在 PEP 590 中引入的,作为提高调用效率的附加协议。

根据经验,如果可调用对象支持,CPython 将更喜欢使用 vectorcall 进行内部调用。 然而,这不是硬性规定。 此外,一些第三方扩展直接使用 tp_call(而不是使用 PyObject_Call())。 因此,支持向量调用的类还必须实现 tp_call。 此外,无论使用哪种协议,可调用对象的行为都必须相同。 实现此目的的推荐方法是将 tp_call 设置为 PyVectorcall_Call()。 这值得重复:

警告

支持 vectorcall must 的类也实现了具有相同语义的 tp_call


如果一个类比 tp_call 慢,则该类不应实现 vectorcall。 例如,如果被调用者无论如何都需要将参数转换为 args 元组和 kwargs dict,那么实现 vectorcall 就没有意义了。

类可以通过启用 Py_TPFLAGS_HAVE_VECTORCALL 标志并将 tp_vectorcall_offset 设置为出现 vectorcallfunc 的对象结构内的偏移量来实现 vectorcall 协议。 这是一个指向具有以下签名的函数的指针:

typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)
  • callable 是被调用的对象。
  • *; args 是一个 C 数组,由位置参数后跟
    关键字参数的值。 如果没有参数,这可以是 NULL
  • *; nargsf 是位置参数的数量加上可能的
    PY_VECTORCALL_ARGUMENTS_OFFSET 标志。 要从 nargsf 获取位置参数的实际数量,请使用 PyVectorcall_NARGS()
  • *; kwnames 是一个包含关键字参数名称的元组;
    换句话说,kwargs 字典的键。 这些名称必须是字符串(str 的实例或子类)并且它们必须是唯一的。 如果没有关键字参数,则 kwnames 可以改为 NULL
PY_VECTORCALL_ARGUMENTS_OFFSET

如果在 vectorcall nargsf 参数中设置了此标志,则允许被调用者临时更改 args[-1]。 换句话说,args 指向分配向量中的参数 1(不是 0)。 被调用者必须在返回前恢复 args[-1] 的值。

对于 PyObject_VectorcallMethod(),这个标志意味着 args[0] 可以改变。

只要他们可以便宜地做到这一点(无需额外分配),就鼓励调用者使用 PY_VECTORCALL_ARGUMENTS_OFFSET。 这样做将允许诸如绑定方法之类的可调用对象非常有效地进行它们的前向调用(包括一个前置的 self 参数)。

要调用实现 vectorcall 的对象,请使用 call API 函数与任何其他可调用对象一样。 PyObject_Vectorcall() 通常是最有效的。

笔记

在 CPython 3.8 中,vectorcall API 和相关函数暂时可用以下划线开头的名称:_PyObject_Vectorcall_Py_TPFLAGS_HAVE_VECTORCALL_PyObject_VectorcallMethod_PyVectorcall_Function_PyObject_CallOneArg_PyObject_CallMethodNoArgs_PyObject_CallMethodOneArg。 此外,PyObject_VectorcallDict 可用作 _PyObject_FastCallDict。 旧名称仍定义为新的非下划线名称的别名。


递归控制

使用tp_call时,被调用者无需担心递归:CPython使用Py_EnterRecursiveCall()Py_LeaveRecursiveCall()进行调用tp_call

为了提高效率,使用 vectorcall 完成的调用不是这种情况:如果需要,被调用者应该使用 Py_EnterRecursiveCallPy_LeaveRecursiveCall


矢量调用支持 API

Py_ssize_t PyVectorcall_NARGS(size_t nargsf)

给定一个 vectorcall nargsf 参数,返回参数的实际数量。 目前相当于:

(Py_ssize_t)(nargsf & ~PY_VECTORCALL_ARGUMENTS_OFFSET)

然而,函数 PyVectorcall_NARGS 应该用于允许未来的扩展。

此函数不是 受限 API 的一部分。

3.8 版中的新功能。

vectorcallfunc PyVectorcall_Function(PyObject *op)

如果op不支持vectorcall协议(要么类型不支持,要么具体实例不支持),返回NULL。 否则,返回存储在 op 中的 vectorcall 函数指针。 此函数从不引发异常。

这对于检查 op 是否支持向量调用非常有用,这可以通过检查 PyVectorcall_Function(op) != NULL 来完成。

此函数不是 受限 API 的一部分。

3.8 版中的新功能。

PyObject *PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict)

分别使用元组和字典中给出的位置和关键字参数调用 callablevectorcallfunc

这是一个专门的函数,旨在放置在 tp_call 插槽中或用于 tp_call 的实现中。 它不会检查 Py_TPFLAGS_HAVE_VECTORCALL 标志,也不会回退到 tp_call

此函数不是 受限 API 的一部分。

3.8 版中的新功能。


对象调用API

有多种函数可用于调用 Python 对象。 每个都将其参数转换为被调用对象支持的约定——tp_call 或 vectorcall。 为了尽可能少地进行转换,请选择最适合您可用数据格式的转换。

下表总结了可用的功能; 有关详细信息,请参阅个别文档。

功能 可调用的 参数 kwargs
PyObject_Call() PyObject * 元组 字典/NULL
PyObject_CallNoArgs() PyObject *
PyObject_CallOneArg() PyObject * 1 个对象
PyObject_CallObject() PyObject * 元组/NULL
PyObject_CallFunction() PyObject * 格式
PyObject_CallMethod() 物镜 + char* 格式
PyObject_CallFunctionObjArgs() PyObject * 可变参数
PyObject_CallMethodObjArgs() 对象 + 名称 可变参数
PyObject_CallMethodNoArgs() 对象 + 名称
PyObject_CallMethodOneArg() 对象 + 名称 1 个对象
PyObject_Vectorcall() PyObject * 向量调用 向量调用
PyObject_VectorcallDict() PyObject * 向量调用 字典/NULL
PyObject_VectorcallMethod() 参数 + 名称 向量调用 向量调用
PyObject *PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)

调用一个可调用的 Python 对象 callable,参数由元组 args 给出,命名参数由字典 kwargs 给出。

args 不能是 NULL; 如果不需要参数,请使用空元组。 如果不需要命名参数,kwargs 可以是 NULL

成功时返回调用结果,或引发异常并在失败时返回 NULL

这相当于 Python 表达式:callable(*args, **kwargs)

PyObject *PyObject_CallNoArgs(PyObject *callable)

不带任何参数调用可调用的 Python 对象 callable。 这是在没有任何参数的情况下调用可调用 Python 对象的最有效方法。

成功时返回调用结果,或引发异常并在失败时返回 NULL

3.9 版中的新功能。

PyObject *PyObject_CallOneArg(PyObject *callable, PyObject *arg)

使用恰好 1 个位置参数 arg 并且没有关键字参数调用可调用 Python 对象 callable

成功时返回调用结果,或引发异常并在失败时返回 NULL

此函数不是 受限 API 的一部分。

3.9 版中的新功能。

PyObject *PyObject_CallObject(PyObject *callable, PyObject *args)

调用可调用 Python 对象 callable,参数由元组 args 给出。 如果不需要参数,则 args 可以是 NULL

成功时返回调用结果,或引发异常并在失败时返回 NULL

这相当于 Python 表达式:callable(*args)

PyObject *PyObject_CallFunction(PyObject *callable, const char *format, ...)

使用可变数量的 C 参数调用可调用 Python 对象 callable。 C 参数使用 Py_BuildValue() 样式格式字符串进行描述。 格式可以是NULL,表示不提供参数。

成功时返回调用结果,或引发异常并在失败时返回 NULL

这相当于 Python 表达式:callable(*args)

请注意,如果您只传递 PyObject* 参数,则 PyObject_CallFunctionObjArgs() 是一个更快的选择。

3.4 版本变更:格式的类型由char *变更。

PyObject *PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)

使用可变数量的 C 参数调用对象 obj 的名为 name 的方法。 C 参数由应生成元组的 Py_BuildValue() 格式字符串描述。

格式可以是NULL,表示不提供参数。

成功时返回调用结果,或引发异常并在失败时返回 NULL

这相当于 Python 表达式:obj.name(arg1, arg2, ...)

请注意,如果您只传递 PyObject* 参数,则 PyObject_CallMethodObjArgs() 是一个更快的选择。

3.4 版本变更: nameformat 的类型由 char * 变更。

PyObject *PyObject_CallFunctionObjArgs(PyObject *callable, ...)

使用可变数量的 PyObject* 参数调用可调用 Python 对象 callable。 参数以可变数量的参数形式提供,后跟 NULL

成功时返回调用结果,或引发异常并在失败时返回 NULL

这相当于 Python 表达式:callable(arg1, arg2, ...)

PyObject *PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)

调用 Python 对象 obj 的方法,其中方法的名称在 name 中作为 Python 字符串对象给出。 它使用可变数量的 PyObject* 参数调用。 参数以可变数量的参数形式提供,后跟 NULL

成功时返回调用结果,或引发异常并在失败时返回 NULL

PyObject *PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name)

不带参数调用 Python 对象 obj 的方法,其中方法的名称在 name 中作为 Python 字符串对象给出。

成功时返回调用结果,或引发异常并在失败时返回 NULL

此函数不是 受限 API 的一部分。

3.9 版中的新功能。

PyObject *PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg)

使用单个位置参数 arg 调用 Python 对象 obj 的方法,其中该方法的名称在 name 中作为 Python 字符串对象给出。

成功时返回调用结果,或引发异常并在失败时返回 NULL

此函数不是 受限 API 的一部分。

3.9 版中的新功能。

PyObject *PyObject_Vectorcall(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)

调用一个可调用的 Python 对象 callable。 参数与 vectorcallfunc 相同。 如果callable支持vectorcall,则直接调用存储在callable中的vectorcall函数。

成功时返回调用结果,或引发异常并在失败时返回 NULL

此函数不是 受限 API 的一部分。

3.9 版中的新功能。

PyObject *PyObject_VectorcallDict(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwdict)

使用完全按照 vectorcall 协议传递的位置参数调用 callable,但使用作为字典 kwdict 传递的关键字参数。 args 数组只包含位置参数。

无论内部使用哪种协议,都需要进行参数转换。 因此,仅当调用者已经准备好用于关键字参数的字典而不是位置参数的元组时,才应使用此函数。

此函数不是 受限 API 的一部分。

3.9 版中的新功能。

PyObject *PyObject_VectorcallMethod(PyObject *name, PyObject *const *args, size_t nargsf, PyObject *kwnames)

使用 vectorcall 调用约定调用方法。 该方法的名称以 Python 字符串 name 的形式给出。 其方法被调用的对象是args[0],从args[1]开始的args数组代表调用的参数。 必须至少有一个位置参数。 nargsf 是位置参数的数量,包括 args[0],如果 args[0] 的值可能会临时更改,则加上 PY_VECTORCALL_ARGUMENTS_OFFSET。 关键字参数可以像在 PyObject_Vectorcall() 中一样传递。

如果对象具有 Py_TPFLAGS_METHOD_DESCRIPTOR 功能,这将调用具有完整 args 向量作为参数的未绑定方法对象。

成功时返回调用结果,或引发异常并在失败时返回 NULL

此函数不是 受限 API 的一部分。

3.9 版中的新功能。


呼叫支持 API

int PyCallable_Check(PyObject *o)
确定对象 o 是否可调用。 如果对象可调用,则返回 1,否则返回 0。 此功能总是成功。