1.. highlight:: c
2
3.. _call:
4
5Call Protocol
6=============
7
8CPython supports two different calling protocols:
9*tp_call* and vectorcall.
10
11The *tp_call* Protocol
12----------------------
13
14Instances of classes that set :c:member:`~PyTypeObject.tp_call` are callable.
15The signature of the slot is::
16
17    PyObject *tp_call(PyObject *callable, PyObject *args, PyObject *kwargs);
18
19A call is made using a tuple for the positional arguments
20and a dict for the keyword arguments, similarly to
21``callable(*args, **kwargs)`` in Python code.
22*args* must be non-NULL (use an empty tuple if there are no arguments)
23but *kwargs* may be *NULL* if there are no keyword arguments.
24
25This convention is not only used by *tp_call*:
26:c:member:`~PyTypeObject.tp_new` and :c:member:`~PyTypeObject.tp_init`
27also pass arguments this way.
28
29To call an object, use :c:func:`PyObject_Call` or other
30:ref:`call API <capi-call>`.
31
32
33.. _vectorcall:
34
35The Vectorcall Protocol
36-----------------------
37
38.. versionadded:: 3.9
39
40The vectorcall protocol was introduced in :pep:`590` as an additional protocol
41for making calls more efficient.
42
43As rule of thumb, CPython will prefer the vectorcall for internal calls
44if the callable supports it. However, this is not a hard rule.
45Additionally, some third-party extensions use *tp_call* directly
46(rather than using :c:func:`PyObject_Call`).
47Therefore, a class supporting vectorcall must also implement
48:c:member:`~PyTypeObject.tp_call`.
49Moreover, the callable must behave the same
50regardless of which protocol is used.
51The recommended way to achieve this is by setting
52:c:member:`~PyTypeObject.tp_call` to :c:func:`PyVectorcall_Call`.
53This bears repeating:
54
55.. warning::
56
57   A class supporting vectorcall **must** also implement
58   :c:member:`~PyTypeObject.tp_call` with the same semantics.
59
60A class should not implement vectorcall if that would be slower
61than *tp_call*. For example, if the callee needs to convert
62the arguments to an args tuple and kwargs dict anyway, then there is no point
63in implementing vectorcall.
64
65Classes can implement the vectorcall protocol by enabling the
66:const:`Py_TPFLAGS_HAVE_VECTORCALL` flag and setting
67:c:member:`~PyTypeObject.tp_vectorcall_offset` to the offset inside the
68object structure where a *vectorcallfunc* appears.
69This is a pointer to a function with the following signature:
70
71.. c:type:: PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)
72
73- *callable* is the object being called.
74- *args* is a C array consisting of the positional arguments followed by the
75   values of the keyword arguments.
76   This can be *NULL* if there are no arguments.
77- *nargsf* is the number of positional arguments plus possibly the
78   :const:`PY_VECTORCALL_ARGUMENTS_OFFSET` flag.
79   To get the actual number of positional arguments from *nargsf*,
80   use :c:func:`PyVectorcall_NARGS`.
81- *kwnames* is a tuple containing the names of the keyword arguments;
82   in other words, the keys of the kwargs dict.
83   These names must be strings (instances of ``str`` or a subclass)
84   and they must be unique.
85   If there are no keyword arguments, then *kwnames* can instead be *NULL*.
86
87.. c:macro:: PY_VECTORCALL_ARGUMENTS_OFFSET
88
89   If this flag is set in a vectorcall *nargsf* argument, the callee is allowed
90   to temporarily change ``args[-1]``. In other words, *args* points to
91   argument 1 (not 0) in the allocated vector.
92   The callee must restore the value of ``args[-1]`` before returning.
93
94   For :c:func:`PyObject_VectorcallMethod`, this flag means instead that
95   ``args[0]`` may be changed.
96
97   Whenever they can do so cheaply (without additional allocation), callers
98   are encouraged to use :const:`PY_VECTORCALL_ARGUMENTS_OFFSET`.
99   Doing so will allow callables such as bound methods to make their onward
100   calls (which include a prepended *self* argument) very efficiently.
101
102To call an object that implements vectorcall, use a :ref:`call API <capi-call>`
103function as with any other callable.
104:c:func:`PyObject_Vectorcall` will usually be most efficient.
105
106
107.. note::
108
109   In CPython 3.8, the vectorcall API and related functions were available
110   provisionally under names with a leading underscore:
111   ``_PyObject_Vectorcall``, ``_Py_TPFLAGS_HAVE_VECTORCALL``,
112   ``_PyObject_VectorcallMethod``, ``_PyVectorcall_Function``,
113   ``_PyObject_CallOneArg``, ``_PyObject_CallMethodNoArgs``,
114   ``_PyObject_CallMethodOneArg``.
115   Additionally, ``PyObject_VectorcallDict`` was available as
116   ``_PyObject_FastCallDict``.
117   The old names are still defined as aliases of the new, non-underscored names.
118
119
120Recursion Control
121.................
122
123When using *tp_call*, callees do not need to worry about
124:ref:`recursion <recursion>`: CPython uses
125:c:func:`Py_EnterRecursiveCall` and :c:func:`Py_LeaveRecursiveCall`
126for calls made using *tp_call*.
127
128For efficiency, this is not the case for calls done using vectorcall:
129the callee should use *Py_EnterRecursiveCall* and *Py_LeaveRecursiveCall*
130if needed.
131
132
133Vectorcall Support API
134......................
135
136.. c:function:: Py_ssize_t PyVectorcall_NARGS(size_t nargsf)
137
138   Given a vectorcall *nargsf* argument, return the actual number of
139   arguments.
140   Currently equivalent to::
141
142      (Py_ssize_t)(nargsf & ~PY_VECTORCALL_ARGUMENTS_OFFSET)
143
144   However, the function ``PyVectorcall_NARGS`` should be used to allow
145   for future extensions.
146
147   This function is not part of the :ref:`limited API <stable>`.
148
149   .. versionadded:: 3.8
150
151.. c:function:: vectorcallfunc PyVectorcall_Function(PyObject *op)
152
153   If *op* does not support the vectorcall protocol (either because the type
154   does not or because the specific instance does not), return *NULL*.
155   Otherwise, return the vectorcall function pointer stored in *op*.
156   This function never raises an exception.
157
158   This is mostly useful to check whether or not *op* supports vectorcall,
159   which can be done by checking ``PyVectorcall_Function(op) != NULL``.
160
161   This function is not part of the :ref:`limited API <stable>`.
162
163   .. versionadded:: 3.8
164
165.. c:function:: PyObject* PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict)
166
167   Call *callable*'s :c:type:`vectorcallfunc` with positional and keyword
168   arguments given in a tuple and dict, respectively.
169
170   This is a specialized function, intended to be put in the
171   :c:member:`~PyTypeObject.tp_call` slot or be used in an implementation of ``tp_call``.
172   It does not check the :const:`Py_TPFLAGS_HAVE_VECTORCALL` flag
173   and it does not fall back to ``tp_call``.
174
175   This function is not part of the :ref:`limited API <stable>`.
176
177   .. versionadded:: 3.8
178
179
180.. _capi-call:
181
182Object Calling API
183------------------
184
185Various functions are available for calling a Python object.
186Each converts its arguments to a convention supported by the called object –
187either *tp_call* or vectorcall.
188In order to do as litle conversion as possible, pick one that best fits
189the format of data you have available.
190
191The following table summarizes the available functions;
192please see individual documentation for details.
193
194+------------------------------------------+------------------+--------------------+---------------+
195| Function                                 | callable         | args               | kwargs        |
196+==========================================+==================+====================+===============+
197| :c:func:`PyObject_Call`                  | ``PyObject *``   | tuple              | dict/``NULL`` |
198+------------------------------------------+------------------+--------------------+---------------+
199| :c:func:`PyObject_CallNoArgs`            | ``PyObject *``   | ---                | ---           |
200+------------------------------------------+------------------+--------------------+---------------+
201| :c:func:`PyObject_CallOneArg`            | ``PyObject *``   | 1 object           | ---           |
202+------------------------------------------+------------------+--------------------+---------------+
203| :c:func:`PyObject_CallObject`            | ``PyObject *``   | tuple/``NULL``     | ---           |
204+------------------------------------------+------------------+--------------------+---------------+
205| :c:func:`PyObject_CallFunction`          | ``PyObject *``   | format             | ---           |
206+------------------------------------------+------------------+--------------------+---------------+
207| :c:func:`PyObject_CallMethod`            | obj + ``char*``  | format             | ---           |
208+------------------------------------------+------------------+--------------------+---------------+
209| :c:func:`PyObject_CallFunctionObjArgs`   | ``PyObject *``   | variadic           | ---           |
210+------------------------------------------+------------------+--------------------+---------------+
211| :c:func:`PyObject_CallMethodObjArgs`     | obj + name       | variadic           | ---           |
212+------------------------------------------+------------------+--------------------+---------------+
213| :c:func:`PyObject_CallMethodNoArgs`      | obj + name       | ---                | ---           |
214+------------------------------------------+------------------+--------------------+---------------+
215| :c:func:`PyObject_CallMethodOneArg`      | obj + name       | 1 object           | ---           |
216+------------------------------------------+------------------+--------------------+---------------+
217| :c:func:`PyObject_Vectorcall`            | ``PyObject *``   | vectorcall         | vectorcall    |
218+------------------------------------------+------------------+--------------------+---------------+
219| :c:func:`PyObject_VectorcallDict`        | ``PyObject *``   | vectorcall         | dict/``NULL`` |
220+------------------------------------------+------------------+--------------------+---------------+
221| :c:func:`PyObject_VectorcallMethod`      | arg + name       | vectorcall         | vectorcall    |
222+------------------------------------------+------------------+--------------------+---------------+
223
224
225.. c:function:: PyObject* PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
226
227   Call a callable Python object *callable*, with arguments given by the
228   tuple *args*, and named arguments given by the dictionary *kwargs*.
229
230   *args* must not be *NULL*; use an empty tuple if no arguments are needed.
231   If no named arguments are needed, *kwargs* can be *NULL*.
232
233   Return the result of the call on success, or raise an exception and return
234   *NULL* on failure.
235
236   This is the equivalent of the Python expression:
237   ``callable(*args, **kwargs)``.
238
239
240.. c:function:: PyObject* PyObject_CallNoArgs(PyObject *callable)
241
242   Call a callable Python object *callable* without any arguments. It is the
243   most efficient way to call a callable Python object without any argument.
244
245   Return the result of the call on success, or raise an exception and return
246   *NULL* on failure.
247
248   .. versionadded:: 3.9
249
250
251.. c:function:: PyObject* PyObject_CallOneArg(PyObject *callable, PyObject *arg)
252
253   Call a callable Python object *callable* with exactly 1 positional argument
254   *arg* and no keyword arguments.
255
256   Return the result of the call on success, or raise an exception and return
257   *NULL* on failure.
258
259   This function is not part of the :ref:`limited API <stable>`.
260
261   .. versionadded:: 3.9
262
263
264.. c:function:: PyObject* PyObject_CallObject(PyObject *callable, PyObject *args)
265
266   Call a callable Python object *callable*, with arguments given by the
267   tuple *args*.  If no arguments are needed, then *args* can be *NULL*.
268
269   Return the result of the call on success, or raise an exception and return
270   *NULL* on failure.
271
272   This is the equivalent of the Python expression: ``callable(*args)``.
273
274
275.. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, const char *format, ...)
276
277   Call a callable Python object *callable*, with a variable number of C arguments.
278   The C arguments are described using a :c:func:`Py_BuildValue` style format
279   string.  The format can be *NULL*, indicating that no arguments are provided.
280
281   Return the result of the call on success, or raise an exception and return
282   *NULL* on failure.
283
284   This is the equivalent of the Python expression: ``callable(*args)``.
285
286   Note that if you only pass :c:type:`PyObject *` args,
287   :c:func:`PyObject_CallFunctionObjArgs` is a faster alternative.
288
289   .. versionchanged:: 3.4
290      The type of *format* was changed from ``char *``.
291
292
293.. c:function:: PyObject* PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
294
295   Call the method named *name* of object *obj* with a variable number of C
296   arguments.  The C arguments are described by a :c:func:`Py_BuildValue` format
297   string that should produce a tuple.
298
299   The format can be *NULL*, indicating that no arguments are provided.
300
301   Return the result of the call on success, or raise an exception and return
302   *NULL* on failure.
303
304   This is the equivalent of the Python expression:
305   ``obj.name(arg1, arg2, ...)``.
306
307   Note that if you only pass :c:type:`PyObject *` args,
308   :c:func:`PyObject_CallMethodObjArgs` is a faster alternative.
309
310   .. versionchanged:: 3.4
311      The types of *name* and *format* were changed from ``char *``.
312
313
314.. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ...)
315
316   Call a callable Python object *callable*, with a variable number of
317   :c:type:`PyObject *` arguments.  The arguments are provided as a variable number
318   of parameters followed by *NULL*.
319
320   Return the result of the call on success, or raise an exception and return
321   *NULL* on failure.
322
323   This is the equivalent of the Python expression:
324   ``callable(arg1, arg2, ...)``.
325
326
327.. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
328
329   Call a method of the Python object *obj*, where the name of the method is given as a
330   Python string object in *name*.  It is called with a variable number of
331   :c:type:`PyObject *` arguments.  The arguments are provided as a variable number
332   of parameters followed by *NULL*.
333
334   Return the result of the call on success, or raise an exception and return
335   *NULL* on failure.
336
337
338.. c:function:: PyObject* PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name)
339
340   Call a method of the Python object *obj* without arguments,
341   where the name of the method is given as a Python string object in *name*.
342
343   Return the result of the call on success, or raise an exception and return
344   *NULL* on failure.
345
346   This function is not part of the :ref:`limited API <stable>`.
347
348   .. versionadded:: 3.9
349
350
351.. c:function:: PyObject* PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg)
352
353   Call a method of the Python object *obj* with a single positional argument
354   *arg*, where the name of the method is given as a Python string object in
355   *name*.
356
357   Return the result of the call on success, or raise an exception and return
358   *NULL* on failure.
359
360   This function is not part of the :ref:`limited API <stable>`.
361
362   .. versionadded:: 3.9
363
364
365.. c:function:: PyObject* PyObject_Vectorcall(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)
366
367   Call a callable Python object *callable*.
368   The arguments are the same as for :c:type:`vectorcallfunc`.
369   If *callable* supports vectorcall_, this directly calls
370   the vectorcall function stored in *callable*.
371
372   Return the result of the call on success, or raise an exception and return
373   *NULL* on failure.
374
375   This function is not part of the :ref:`limited API <stable>`.
376
377   .. versionadded:: 3.9
378
379.. c:function:: PyObject* PyObject_VectorcallDict(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwdict)
380
381   Call *callable* with positional arguments passed exactly as in the vectorcall_ protocol,
382   but with keyword arguments passed as a dictionary *kwdict*.
383   The *args* array contains only the positional arguments.
384
385   Regardless of which protocol is used internally,
386   a conversion of arguments needs to be done.
387   Therefore, this function should only be used if the caller
388   already has a dictionary ready to use for the keyword arguments,
389   but not a tuple for the positional arguments.
390
391   This function is not part of the :ref:`limited API <stable>`.
392
393   .. versionadded:: 3.9
394
395.. c:function:: PyObject* PyObject_VectorcallMethod(PyObject *name, PyObject *const *args, size_t nargsf, PyObject *kwnames)
396
397   Call a method using the vectorcall calling convention. The name of the method
398   is given as a Python string *name*. The object whose method is called is
399   *args[0]*, and the *args* array starting at *args[1]* represents the arguments
400   of the call. There must be at least one positional argument.
401   *nargsf* is the number of positional arguments including *args[0]*,
402   plus :const:`PY_VECTORCALL_ARGUMENTS_OFFSET` if the value of ``args[0]`` may
403   temporarily be changed. Keyword arguments can be passed just like in
404   :c:func:`PyObject_Vectorcall`.
405
406   If the object has the :const:`Py_TPFLAGS_METHOD_DESCRIPTOR` feature,
407   this will call the unbound method object with the full
408   *args* vector as arguments.
409
410   Return the result of the call on success, or raise an exception and return
411   *NULL* on failure.
412
413   This function is not part of the :ref:`limited API <stable>`.
414
415   .. versionadded:: 3.9
416
417
418Call Support API
419----------------
420
421.. c:function:: int PyCallable_Check(PyObject *o)
422
423   Determine if the object *o* is callable.  Return ``1`` if the object is callable
424   and ``0`` otherwise.  This function always succeeds.
425