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

424 lines
17 KiB

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