Skip to content

Commit fde9cf7

Browse files
committed
bpo-36974: separate docs for call protocol
1 parent 0567786 commit fde9cf7

File tree

3 files changed

+247
-240
lines changed

3 files changed

+247
-240
lines changed

Doc/c-api/abstract.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ but whose items have not been set to some non-\ ``NULL`` value yet.
1818
.. toctree::
1919

2020
object.rst
21+
call.rst
2122
number.rst
2223
sequence.rst
2324
mapping.rst

Doc/c-api/call.rst

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
.. highlight:: c
2+
3+
.. _call:
4+
5+
Call Protocol
6+
=============
7+
8+
.. c:function:: int PyCallable_Check(PyObject *o)
9+
10+
Determine if the object *o* is callable. Return ``1`` if the object is callable
11+
and ``0`` otherwise. This function always succeeds.
12+
13+
14+
.. c:function:: PyObject* PyObject_CallNoArgs(PyObject *callable)
15+
16+
Call a callable Python object *callable* without any arguments. It is the
17+
most efficient way to call a callable Python object without any argument.
18+
19+
Return the result of the call on success, or raise an exception and return
20+
*NULL* on failure.
21+
22+
.. versionadded:: 3.9
23+
24+
25+
.. c:function:: PyObject* _PyObject_CallOneArg(PyObject *callable, PyObject *arg)
26+
27+
Call a callable Python object *callable* with exactly 1 positional argument
28+
*arg* and no keyword arguments.
29+
30+
Return the result of the call on success, or raise an exception and return
31+
*NULL* on failure.
32+
33+
.. versionadded:: 3.9
34+
35+
36+
.. c:function:: PyObject* PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
37+
38+
Call a callable Python object *callable*, with arguments given by the
39+
tuple *args*, and named arguments given by the dictionary *kwargs*.
40+
41+
*args* must not be *NULL*, use an empty tuple if no arguments are needed.
42+
If no named arguments are needed, *kwargs* can be *NULL*.
43+
44+
Return the result of the call on success, or raise an exception and return
45+
*NULL* on failure.
46+
47+
This is the equivalent of the Python expression:
48+
``callable(*args, **kwargs)``.
49+
50+
51+
.. c:function:: PyObject* PyObject_CallObject(PyObject *callable, PyObject *args)
52+
53+
Call a callable Python object *callable*, with arguments given by the
54+
tuple *args*. If no arguments are needed, then *args* can be *NULL*.
55+
56+
Return the result of the call on success, or raise an exception and return
57+
*NULL* on failure.
58+
59+
This is the equivalent of the Python expression: ``callable(*args)``.
60+
61+
62+
.. c:function:: PyObject* PyObject_CallFunction(PyObject *callable, const char *format, ...)
63+
64+
Call a callable Python object *callable*, with a variable number of C arguments.
65+
The C arguments are described using a :c:func:`Py_BuildValue` style format
66+
string. The format can be *NULL*, indicating that no arguments are provided.
67+
68+
Return the result of the call on success, or raise an exception and return
69+
*NULL* on failure.
70+
71+
This is the equivalent of the Python expression: ``callable(*args)``.
72+
73+
Note that if you only pass :c:type:`PyObject \*` args,
74+
:c:func:`PyObject_CallFunctionObjArgs` is a faster alternative.
75+
76+
.. versionchanged:: 3.4
77+
The type of *format* was changed from ``char *``.
78+
79+
80+
.. c:function:: PyObject* PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
81+
82+
Call the method named *name* of object *obj* with a variable number of C
83+
arguments. The C arguments are described by a :c:func:`Py_BuildValue` format
84+
string that should produce a tuple.
85+
86+
The format can be *NULL*, indicating that no arguments are provided.
87+
88+
Return the result of the call on success, or raise an exception and return
89+
*NULL* on failure.
90+
91+
This is the equivalent of the Python expression:
92+
``obj.name(arg1, arg2, ...)``.
93+
94+
Note that if you only pass :c:type:`PyObject \*` args,
95+
:c:func:`PyObject_CallMethodObjArgs` is a faster alternative.
96+
97+
.. versionchanged:: 3.4
98+
The types of *name* and *format* were changed from ``char *``.
99+
100+
101+
.. c:function:: PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ..., NULL)
102+
103+
Call a callable Python object *callable*, with a variable number of
104+
:c:type:`PyObject\*` arguments. The arguments are provided as a variable number
105+
of parameters followed by *NULL*.
106+
107+
Return the result of the call on success, or raise an exception and return
108+
*NULL* on failure.
109+
110+
This is the equivalent of the Python expression:
111+
``callable(arg1, arg2, ...)``.
112+
113+
114+
.. c:function:: PyObject* PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ..., NULL)
115+
116+
Calls a method of the Python object *obj*, where the name of the method is given as a
117+
Python string object in *name*. It is called with a variable number of
118+
:c:type:`PyObject\*` arguments. The arguments are provided as a variable number
119+
of parameters followed by *NULL*.
120+
121+
Return the result of the call on success, or raise an exception and return
122+
*NULL* on failure.
123+
124+
125+
.. c:function:: PyObject* _PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name)
126+
127+
Call a method of the Python object *obj* without arguments,
128+
where the name of the method is given as a Python string object in *name*.
129+
130+
Return the result of the call on success, or raise an exception and return
131+
*NULL* on failure.
132+
133+
.. versionadded:: 3.9
134+
135+
136+
.. c:function:: PyObject* _PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg)
137+
138+
Call a method of the Python object *obj* with a single positional argument
139+
*arg*, where the name of the method is given as a Python string object in
140+
*name*.
141+
142+
Return the result of the call on success, or raise an exception and return
143+
*NULL* on failure.
144+
145+
.. versionadded:: 3.9
146+
147+
148+
.. c:function:: PyObject* _PyObject_Vectorcall(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)
149+
150+
Call a callable Python object *callable*, using
151+
:c:data:`vectorcall <PyTypeObject.tp_vectorcall_offset>` if possible.
152+
153+
*args* is a C array with the positional arguments.
154+
155+
*nargsf* is the number of positional arguments plus optionally the flag
156+
:const:`PY_VECTORCALL_ARGUMENTS_OFFSET` (see below).
157+
To get actual number of arguments, use
158+
:c:func:`PyVectorcall_NARGS(nargsf) <PyVectorcall_NARGS>`.
159+
160+
*kwnames* can be either NULL (no keyword arguments) or a tuple of keyword
161+
names, which must be strings. In the latter case, the values of the keyword
162+
arguments are stored in *args* after the positional arguments.
163+
The number of keyword arguments does not influence *nargsf*.
164+
165+
*kwnames* must contain only objects of type ``str`` (not a subclass),
166+
and all keys must be unique.
167+
168+
Return the result of the call on success, or raise an exception and return
169+
*NULL* on failure.
170+
171+
This uses the vectorcall protocol if the callable supports it;
172+
otherwise, the arguments are converted to use
173+
:c:member:`~PyTypeObject.tp_call`.
174+
175+
.. note::
176+
177+
This function is provisional and expected to become public in Python 3.9,
178+
with a different name and, possibly, changed semantics.
179+
If you use the function, plan for updating your code for Python 3.9.
180+
181+
.. versionadded:: 3.8
182+
183+
.. c:var:: PY_VECTORCALL_ARGUMENTS_OFFSET
184+
185+
If set in a vectorcall *nargsf* argument, the callee is allowed to
186+
temporarily change ``args[-1]``. In other words, *args* points to
187+
argument 1 (not 0) in the allocated vector.
188+
The callee must restore the value of ``args[-1]`` before returning.
189+
190+
For :c:func:`_PyObject_VectorcallMethod`, this flag means instead that
191+
``args[0]`` may be changed.
192+
193+
Whenever they can do so cheaply (without additional allocation), callers
194+
are encouraged to use :const:`PY_VECTORCALL_ARGUMENTS_OFFSET`.
195+
Doing so will allow callables such as bound methods to make their onward
196+
calls (which include a prepended *self* argument) cheaply.
197+
198+
.. versionadded:: 3.8
199+
200+
.. c:function:: Py_ssize_t PyVectorcall_NARGS(size_t nargsf)
201+
202+
Given a vectorcall *nargsf* argument, return the actual number of
203+
arguments.
204+
Currently equivalent to ``nargsf & ~PY_VECTORCALL_ARGUMENTS_OFFSET``.
205+
206+
.. versionadded:: 3.8
207+
208+
.. c:function:: PyObject* _PyObject_FastCallDict(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwdict)
209+
210+
Same as :c:func:`_PyObject_Vectorcall` except that the keyword arguments
211+
are passed as a dictionary in *kwdict*. This may be *NULL* if there
212+
are no keyword arguments.
213+
214+
For callables supporting :c:data:`vectorcall <PyTypeObject.tp_vectorcall_offset>`,
215+
the arguments are internally converted to the vectorcall convention.
216+
Therefore, this function adds some overhead compared to
217+
:c:func:`_PyObject_Vectorcall`.
218+
It should only be used if the caller already has a dictionary ready to use.
219+
220+
.. note::
221+
222+
This function is provisional and expected to become public in Python 3.9,
223+
with a different name and, possibly, changed semantics.
224+
If you use the function, plan for updating your code for Python 3.9.
225+
226+
.. versionadded:: 3.8
227+
228+
.. c:function:: PyObject* _PyObject_VectorcallMethod(PyObject *name, PyObject *const *args, size_t nargsf, PyObject *kwnames)
229+
230+
Call a method using the vectorcall calling convention. The name of the method
231+
is given as Python string *name*. The object whose method is called is
232+
*args[0]* and the *args* array starting at *args[1]* represents the arguments
233+
of the call. There must be at least one positional argument.
234+
*nargsf* is the number of positional arguments including *args[0]*,
235+
plus :const:`PY_VECTORCALL_ARGUMENTS_OFFSET` if the value of ``args[0]`` may
236+
temporarily be changed. Keyword arguments can be passed just like in
237+
:c:func:`_PyObject_Vectorcall`.
238+
239+
If the object has the :const:`Py_TPFLAGS_METHOD_DESCRIPTOR` feature,
240+
this will actually call the unbound method object with the full
241+
*args* vector as 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

0 commit comments

Comments
 (0)