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