blob: df90595d6c6a530ed7665cfd2292b5faf8039c8f [file] [log] [blame]
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001#include "Python.h"
Victor Stinnerbcda8f12018-11-21 22:27:47 +01002#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01003#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +01004#include "pycore_tupleobject.h"
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005#include "frameobject.h"
6
7
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02008static PyObject *
9cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs);
10
Jeroen Demeyerd4efd912019-07-02 11:49:40 +020011static PyObject *const *
12_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
13 PyObject **p_kwnames);
14
15static void
16_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
17 PyObject *kwnames);
18
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020019
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010020static PyObject *
21null_error(void)
22{
23 if (!PyErr_Occurred())
24 PyErr_SetString(PyExc_SystemError,
25 "null argument to internal routine");
26 return NULL;
27}
28
29
30PyObject*
31_Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where)
32{
33 int err_occurred = (PyErr_Occurred() != NULL);
34
35 assert((callable != NULL) ^ (where != NULL));
36
37 if (result == NULL) {
38 if (!err_occurred) {
39 if (callable)
40 PyErr_Format(PyExc_SystemError,
41 "%R returned NULL without setting an error",
42 callable);
43 else
44 PyErr_Format(PyExc_SystemError,
45 "%s returned NULL without setting an error",
46 where);
47#ifdef Py_DEBUG
48 /* Ensure that the bug is caught in debug mode */
49 Py_FatalError("a function returned NULL without setting an error");
50#endif
51 return NULL;
52 }
53 }
54 else {
55 if (err_occurred) {
56 Py_DECREF(result);
57
58 if (callable) {
59 _PyErr_FormatFromCause(PyExc_SystemError,
60 "%R returned a result with an error set",
61 callable);
62 }
63 else {
64 _PyErr_FormatFromCause(PyExc_SystemError,
65 "%s returned a result with an error set",
66 where);
67 }
68#ifdef Py_DEBUG
69 /* Ensure that the bug is caught in debug mode */
70 Py_FatalError("a function returned a result with an error set");
71#endif
72 return NULL;
73 }
74 }
75 return result;
76}
77
78
79/* --- Core PyObject call functions ------------------------------- */
80
Victor Stinner2ff58a22019-06-17 14:27:23 +020081/* Call a callable Python object without any arguments */
82PyObject *
83PyObject_CallNoArgs(PyObject *func)
84{
85 return _PyObject_CallNoArg(func);
86}
87
88
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010089PyObject *
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020090_PyObject_FastCallDict(PyObject *callable, PyObject *const *args,
91 size_t nargsf, PyObject *kwargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010092{
93 /* _PyObject_FastCallDict() must not be called with an exception set,
94 because it can clear it (directly or indirectly) and so the
95 caller loses its exception */
96 assert(!PyErr_Occurred());
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010097 assert(callable != NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020098
99 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100100 assert(nargs >= 0);
101 assert(nargs == 0 || args != NULL);
102 assert(kwargs == NULL || PyDict_Check(kwargs));
103
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200104 vectorcallfunc func = _PyVectorcall_Function(callable);
105 if (func == NULL) {
106 /* Use tp_call instead */
107 return _PyObject_MakeTpCall(callable, args, nargs, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100108 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200109
110 PyObject *res;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200111 if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200112 res = func(callable, args, nargsf, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100113 }
114 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200115 PyObject *kwnames;
116 PyObject *const *newargs;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200117 newargs = _PyStack_UnpackDict(args, nargs, kwargs, &kwnames);
118 if (newargs == NULL) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100119 return NULL;
120 }
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200121 res = func(callable, newargs,
122 nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
123 _PyStack_UnpackDict_Free(newargs, nargs, kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100124 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200125 return _Py_CheckFunctionResult(callable, res, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100126}
127
128
129PyObject *
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200130_PyObject_MakeTpCall(PyObject *callable, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100131{
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200132 /* Slow path: build a temporary tuple for positional arguments and a
133 * temporary dictionary for keyword arguments (if any) */
134 ternaryfunc call = Py_TYPE(callable)->tp_call;
135 if (call == NULL) {
136 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
137 Py_TYPE(callable)->tp_name);
138 return NULL;
139 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100140
141 assert(nargs >= 0);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200142 assert(nargs == 0 || args != NULL);
143 assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));
144 PyObject *argstuple = _PyTuple_FromArray(args, nargs);
145 if (argstuple == NULL) {
146 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100147 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200148
149 PyObject *kwdict;
150 if (keywords == NULL || PyDict_Check(keywords)) {
151 kwdict = keywords;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100152 }
153 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200154 if (PyTuple_GET_SIZE(keywords)) {
155 assert(args != NULL);
156 kwdict = _PyStack_AsDict(args + nargs, keywords);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100157 if (kwdict == NULL) {
158 Py_DECREF(argstuple);
159 return NULL;
160 }
161 }
162 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200163 keywords = kwdict = NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100164 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100165 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200166
167 PyObject *result = NULL;
168 if (Py_EnterRecursiveCall(" while calling a Python object") == 0)
169 {
170 result = call(callable, argstuple, kwdict);
171 Py_LeaveRecursiveCall();
172 }
173
174 Py_DECREF(argstuple);
175 if (kwdict != keywords) {
176 Py_DECREF(kwdict);
177 }
178
179 result = _Py_CheckFunctionResult(callable, result, NULL);
180 return result;
181}
182
183
184PyObject *
185PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
186{
Petr Viktorinfb9423f2019-06-02 23:52:20 +0200187 /* get vectorcallfunc as in _PyVectorcall_Function, but without
188 * the _Py_TPFLAGS_HAVE_VECTORCALL check */
189 Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
Jeroen Demeyera8b27e62019-06-24 12:41:05 +0200190 if (offset <= 0) {
Petr Viktorinfb9423f2019-06-02 23:52:20 +0200191 PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
192 Py_TYPE(callable)->tp_name);
193 return NULL;
194 }
195 vectorcallfunc func = *(vectorcallfunc *)(((char *)callable) + offset);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200196 if (func == NULL) {
197 PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
198 Py_TYPE(callable)->tp_name);
199 return NULL;
200 }
Petr Viktorinfb9423f2019-06-02 23:52:20 +0200201
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200202 Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200203
204 /* Fast path for no keywords */
205 if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
206 return func(callable, _PyTuple_ITEMS(tuple), nargs, NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200207 }
208
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200209 /* Convert arguments & call */
210 PyObject *const *args;
211 PyObject *kwnames;
212 args = _PyStack_UnpackDict(_PyTuple_ITEMS(tuple), nargs, kwargs, &kwnames);
213 if (args == NULL) {
214 return NULL;
215 }
216 PyObject *result = func(callable, args,
217 nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
218 _PyStack_UnpackDict_Free(args, nargs, kwnames);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200219 return _Py_CheckFunctionResult(callable, result, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100220}
221
222
223PyObject *
224PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
225{
226 ternaryfunc call;
227 PyObject *result;
228
229 /* PyObject_Call() must not be called with an exception set,
230 because it can clear it (directly or indirectly) and so the
231 caller loses its exception */
232 assert(!PyErr_Occurred());
233 assert(PyTuple_Check(args));
234 assert(kwargs == NULL || PyDict_Check(kwargs));
235
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200236 if (_PyVectorcall_Function(callable) != NULL) {
237 return PyVectorcall_Call(callable, args, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100238 }
239 else if (PyCFunction_Check(callable)) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200240 /* This must be a METH_VARARGS function, otherwise we would be
241 * in the previous case */
242 return cfunction_call_varargs(callable, args, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100243 }
244 else {
245 call = callable->ob_type->tp_call;
246 if (call == NULL) {
247 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
248 callable->ob_type->tp_name);
249 return NULL;
250 }
251
252 if (Py_EnterRecursiveCall(" while calling a Python object"))
253 return NULL;
254
255 result = (*call)(callable, args, kwargs);
256
257 Py_LeaveRecursiveCall();
258
259 return _Py_CheckFunctionResult(callable, result, NULL);
260 }
261}
262
263
264/* --- PyFunction call functions ---------------------------------- */
265
266static PyObject* _Py_HOT_FUNCTION
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200267function_code_fastcall(PyCodeObject *co, PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100268 PyObject *globals)
269{
270 PyFrameObject *f;
Victor Stinner50b48572018-11-01 01:51:40 +0100271 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100272 PyObject **fastlocals;
273 Py_ssize_t i;
274 PyObject *result;
275
276 assert(globals != NULL);
277 /* XXX Perhaps we should create a specialized
278 _PyFrame_New_NoTrack() that doesn't take locals, but does
279 take builtins without sanity checking them.
280 */
281 assert(tstate != NULL);
282 f = _PyFrame_New_NoTrack(tstate, co, globals, NULL);
283 if (f == NULL) {
284 return NULL;
285 }
286
287 fastlocals = f->f_localsplus;
288
289 for (i = 0; i < nargs; i++) {
290 Py_INCREF(*args);
291 fastlocals[i] = *args++;
292 }
293 result = PyEval_EvalFrameEx(f,0);
294
295 if (Py_REFCNT(f) > 1) {
296 Py_DECREF(f);
297 _PyObject_GC_TRACK(f);
298 }
299 else {
300 ++tstate->recursion_depth;
301 Py_DECREF(f);
302 --tstate->recursion_depth;
303 }
304 return result;
305}
306
307
308PyObject *
Jeroen Demeyer37788bc2019-05-30 15:11:22 +0200309_PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
310 size_t nargsf, PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100311{
312 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
313 PyObject *globals = PyFunction_GET_GLOBALS(func);
314 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
315 PyObject *kwdefs, *closure, *name, *qualname;
316 PyObject **d;
317 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
318 Py_ssize_t nd;
319
320 assert(PyFunction_Check(func));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200321 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100322 assert(nargs >= 0);
323 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
324 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
325 /* kwnames must only contains str strings, no subclass, and all keys must
326 be unique */
327
328 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner086c3ae2017-10-25 05:26:17 -0700329 (co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100330 {
Pablo Galindocd74e662019-06-01 18:08:04 +0100331 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100332 return function_code_fastcall(co, stack, nargs, globals);
333 }
334 else if (nargs == 0 && argdefs != NULL
Pablo Galindocd74e662019-06-01 18:08:04 +0100335 && co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100336 /* function called with no arguments, but all parameters have
337 a default value: use default values as arguments .*/
Victor Stinnerd17a6932018-11-09 16:56:48 +0100338 stack = _PyTuple_ITEMS(argdefs);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200339 return function_code_fastcall(co, stack, PyTuple_GET_SIZE(argdefs),
340 globals);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100341 }
342 }
343
344 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
345 closure = PyFunction_GET_CLOSURE(func);
346 name = ((PyFunctionObject *)func) -> func_name;
347 qualname = ((PyFunctionObject *)func) -> func_qualname;
348
349 if (argdefs != NULL) {
Victor Stinnerd17a6932018-11-09 16:56:48 +0100350 d = _PyTuple_ITEMS(argdefs);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200351 nd = PyTuple_GET_SIZE(argdefs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100352 }
353 else {
354 d = NULL;
355 nd = 0;
356 }
357 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
358 stack, nargs,
Victor Stinnerd17a6932018-11-09 16:56:48 +0100359 nkwargs ? _PyTuple_ITEMS(kwnames) : NULL,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100360 stack + nargs,
361 nkwargs, 1,
362 d, (int)nd, kwdefs,
363 closure, name, qualname);
364}
365
366
367/* --- PyCFunction call functions --------------------------------- */
368
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100369static PyObject *
370cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs)
371{
372 assert(!PyErr_Occurred());
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300373 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100374
375 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
376 PyObject *self = PyCFunction_GET_SELF(func);
377 PyObject *result;
378
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200379 assert(PyCFunction_GET_FLAGS(func) & METH_VARARGS);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100380 if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) {
381 if (Py_EnterRecursiveCall(" while calling a Python object")) {
382 return NULL;
383 }
384
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200385 result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100386
387 Py_LeaveRecursiveCall();
388 }
389 else {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300390 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100391 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
392 ((PyCFunctionObject*)func)->m_ml->ml_name);
393 return NULL;
394 }
395
396 if (Py_EnterRecursiveCall(" while calling a Python object")) {
397 return NULL;
398 }
399
400 result = (*meth)(self, args);
401
402 Py_LeaveRecursiveCall();
403 }
404
405 return _Py_CheckFunctionResult(func, result, NULL);
406}
407
408
409PyObject *
410PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
411{
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200412 /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
413 * is NULL. This is intentional, since vectorcall would be slower. */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100414 if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) {
415 return cfunction_call_varargs(func, args, kwargs);
416 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200417 return PyVectorcall_Call(func, args, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100418}
419
420
421/* --- More complex call functions -------------------------------- */
422
423/* External interface to call any callable object.
424 The args must be a tuple or NULL. The kwargs must be a dict or NULL. */
425PyObject *
426PyEval_CallObjectWithKeywords(PyObject *callable,
427 PyObject *args, PyObject *kwargs)
428{
429#ifdef Py_DEBUG
430 /* PyEval_CallObjectWithKeywords() must not be called with an exception
431 set. It raises a new exception if parameters are invalid or if
432 PyTuple_New() fails, and so the original exception is lost. */
433 assert(!PyErr_Occurred());
434#endif
435
INADA Naoki3824cd82017-03-01 20:41:03 +0900436 if (args != NULL && !PyTuple_Check(args)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100437 PyErr_SetString(PyExc_TypeError,
438 "argument list must be a tuple");
439 return NULL;
440 }
441
442 if (kwargs != NULL && !PyDict_Check(kwargs)) {
443 PyErr_SetString(PyExc_TypeError,
444 "keyword list must be a dictionary");
445 return NULL;
446 }
447
INADA Naoki3824cd82017-03-01 20:41:03 +0900448 if (args == NULL) {
449 return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
450 }
451 else {
452 return PyObject_Call(callable, args, kwargs);
453 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100454}
455
456
457PyObject *
458PyObject_CallObject(PyObject *callable, PyObject *args)
459{
460 return PyEval_CallObjectWithKeywords(callable, args, NULL);
461}
462
463
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100464/* Call callable(obj, *args, **kwargs). */
465PyObject *
466_PyObject_Call_Prepend(PyObject *callable,
467 PyObject *obj, PyObject *args, PyObject *kwargs)
468{
469 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
470 PyObject **stack;
471 Py_ssize_t argcount;
472 PyObject *result;
473
474 assert(PyTuple_Check(args));
475
476 argcount = PyTuple_GET_SIZE(args);
477 if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
478 stack = small_stack;
479 }
480 else {
481 stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
482 if (stack == NULL) {
483 PyErr_NoMemory();
484 return NULL;
485 }
486 }
487
488 /* use borrowed references */
489 stack[0] = obj;
490 memcpy(&stack[1],
Victor Stinnerd17a6932018-11-09 16:56:48 +0100491 _PyTuple_ITEMS(args),
492 argcount * sizeof(PyObject *));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100493
494 result = _PyObject_FastCallDict(callable,
495 stack, argcount + 1,
496 kwargs);
497 if (stack != small_stack) {
498 PyMem_Free(stack);
499 }
500 return result;
501}
502
503
504/* --- Call with a format string ---------------------------------- */
505
506static PyObject *
507_PyObject_CallFunctionVa(PyObject *callable, const char *format,
508 va_list va, int is_size_t)
509{
510 PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
511 const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
512 PyObject **stack;
513 Py_ssize_t nargs, i;
514 PyObject *result;
515
516 if (callable == NULL) {
517 return null_error();
518 }
519
520 if (!format || !*format) {
521 return _PyObject_CallNoArg(callable);
522 }
523
524 if (is_size_t) {
525 stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
526 format, va, &nargs);
527 }
528 else {
529 stack = _Py_VaBuildStack(small_stack, small_stack_len,
530 format, va, &nargs);
531 }
532 if (stack == NULL) {
533 return NULL;
534 }
535
536 if (nargs == 1 && PyTuple_Check(stack[0])) {
537 /* Special cases for backward compatibility:
538 - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
539 - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
540 func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
541 PyObject *args = stack[0];
542 result = _PyObject_FastCall(callable,
Victor Stinnerd17a6932018-11-09 16:56:48 +0100543 _PyTuple_ITEMS(args),
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100544 PyTuple_GET_SIZE(args));
545 }
546 else {
547 result = _PyObject_FastCall(callable, stack, nargs);
548 }
549
550 for (i = 0; i < nargs; ++i) {
551 Py_DECREF(stack[i]);
552 }
553 if (stack != small_stack) {
554 PyMem_Free(stack);
555 }
556 return result;
557}
558
559
560PyObject *
561PyObject_CallFunction(PyObject *callable, const char *format, ...)
562{
563 va_list va;
564 PyObject *result;
565
566 va_start(va, format);
567 result = _PyObject_CallFunctionVa(callable, format, va, 0);
568 va_end(va);
569
570 return result;
571}
572
573
INADA Naokiaa289a52017-03-14 18:00:59 +0900574/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
575 * This function is kept for backward compatibility.
576 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100577PyObject *
578PyEval_CallFunction(PyObject *callable, const char *format, ...)
579{
INADA Naokiaa289a52017-03-14 18:00:59 +0900580 va_list va;
581 PyObject *result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100582
INADA Naokiaa289a52017-03-14 18:00:59 +0900583 va_start(va, format);
584 result = _PyObject_CallFunctionVa(callable, format, va, 0);
585 va_end(va);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100586
INADA Naokiaa289a52017-03-14 18:00:59 +0900587 return result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100588}
589
590
591PyObject *
592_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
593{
594 va_list va;
595 PyObject *result;
596
597 va_start(va, format);
598 result = _PyObject_CallFunctionVa(callable, format, va, 1);
599 va_end(va);
600
601 return result;
602}
603
604
605static PyObject*
606callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
607{
608 assert(callable != NULL);
609
610 if (!PyCallable_Check(callable)) {
611 PyErr_Format(PyExc_TypeError,
612 "attribute of type '%.200s' is not callable",
613 Py_TYPE(callable)->tp_name);
614 return NULL;
615 }
616
617 return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
618}
619
620
621PyObject *
622PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
623{
624 va_list va;
625 PyObject *callable, *retval;
626
627 if (obj == NULL || name == NULL) {
628 return null_error();
629 }
630
631 callable = PyObject_GetAttrString(obj, name);
632 if (callable == NULL)
633 return NULL;
634
635 va_start(va, format);
636 retval = callmethod(callable, format, va, 0);
637 va_end(va);
638
639 Py_DECREF(callable);
640 return retval;
641}
642
643
INADA Naokiaa289a52017-03-14 18:00:59 +0900644/* PyEval_CallMethod is exact copy of PyObject_CallMethod.
645 * This function is kept for backward compatibility.
646 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100647PyObject *
648PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
649{
INADA Naokiaa289a52017-03-14 18:00:59 +0900650 va_list va;
651 PyObject *callable, *retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100652
INADA Naokiaa289a52017-03-14 18:00:59 +0900653 if (obj == NULL || name == NULL) {
654 return null_error();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100655 }
656
INADA Naokiaa289a52017-03-14 18:00:59 +0900657 callable = PyObject_GetAttrString(obj, name);
658 if (callable == NULL)
659 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100660
INADA Naokiaa289a52017-03-14 18:00:59 +0900661 va_start(va, format);
662 retval = callmethod(callable, format, va, 0);
663 va_end(va);
664
665 Py_DECREF(callable);
666 return retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100667}
668
669
670PyObject *
671_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
672 const char *format, ...)
673{
674 va_list va;
675 PyObject *callable, *retval;
676
677 if (obj == NULL || name == NULL) {
678 return null_error();
679 }
680
681 callable = _PyObject_GetAttrId(obj, name);
682 if (callable == NULL)
683 return NULL;
684
685 va_start(va, format);
686 retval = callmethod(callable, format, va, 0);
687 va_end(va);
688
689 Py_DECREF(callable);
690 return retval;
691}
692
693
694PyObject *
695_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
696 const char *format, ...)
697{
698 va_list va;
699 PyObject *callable, *retval;
700
701 if (obj == NULL || name == NULL) {
702 return null_error();
703 }
704
705 callable = PyObject_GetAttrString(obj, name);
706 if (callable == NULL)
707 return NULL;
708
709 va_start(va, format);
710 retval = callmethod(callable, format, va, 1);
711 va_end(va);
712
713 Py_DECREF(callable);
714 return retval;
715}
716
717
718PyObject *
719_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
720 const char *format, ...)
721{
722 va_list va;
723 PyObject *callable, *retval;
724
725 if (obj == NULL || name == NULL) {
726 return null_error();
727 }
728
729 callable = _PyObject_GetAttrId(obj, name);
730 if (callable == NULL) {
731 return NULL;
732 }
733
734 va_start(va, format);
735 retval = callmethod(callable, format, va, 1);
736 va_end(va);
737
738 Py_DECREF(callable);
739 return retval;
740}
741
742
743/* --- Call with "..." arguments ---------------------------------- */
744
745static PyObject *
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700746object_vacall(PyObject *base, PyObject *callable, va_list vargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100747{
748 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
749 PyObject **stack;
750 Py_ssize_t nargs;
751 PyObject *result;
752 Py_ssize_t i;
753 va_list countva;
754
755 if (callable == NULL) {
756 return null_error();
757 }
758
759 /* Count the number of arguments */
760 va_copy(countva, vargs);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700761 nargs = base ? 1 : 0;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100762 while (1) {
763 PyObject *arg = va_arg(countva, PyObject *);
764 if (arg == NULL) {
765 break;
766 }
767 nargs++;
768 }
769 va_end(countva);
770
771 /* Copy arguments */
772 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
773 stack = small_stack;
774 }
775 else {
776 stack = PyMem_Malloc(nargs * sizeof(stack[0]));
777 if (stack == NULL) {
778 PyErr_NoMemory();
779 return NULL;
780 }
781 }
782
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700783 i = 0;
784 if (base) {
785 stack[i++] = base;
786 }
787
788 for (; i < nargs; ++i) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100789 stack[i] = va_arg(vargs, PyObject *);
790 }
791
792 /* Call the function */
793 result = _PyObject_FastCall(callable, stack, nargs);
794
795 if (stack != small_stack) {
796 PyMem_Free(stack);
797 }
798 return result;
799}
800
801
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700802PyObject *
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200803_PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
804 size_t nargsf, PyObject *kwnames)
805{
806 assert(name != NULL);
807 assert(args != NULL);
808 assert(PyVectorcall_NARGS(nargsf) >= 1);
809
810 PyObject *callable = NULL;
811 /* Use args[0] as "self" argument */
812 int unbound = _PyObject_GetMethod(args[0], name, &callable);
813 if (callable == NULL) {
814 return NULL;
815 }
816
817 if (unbound) {
818 /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
819 * that would be interpreted as allowing to change args[-1] */
820 nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
821 }
822 else {
823 /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
824 * args[-1] in the onward call is args[0] here. */
825 args++;
826 nargsf--;
827 }
828 PyObject *result = _PyObject_Vectorcall(callable, args, nargsf, kwnames);
829 Py_DECREF(callable);
830 return result;
831}
832
833
834PyObject *
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700835PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
836{
837 if (obj == NULL || name == NULL) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100838 return null_error();
839 }
840
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700841 PyObject *callable = NULL;
842 int is_method = _PyObject_GetMethod(obj, name, &callable);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100843 if (callable == NULL) {
844 return NULL;
845 }
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700846 obj = is_method ? obj : NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100847
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700848 va_list vargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100849 va_start(vargs, name);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700850 PyObject *result = object_vacall(obj, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100851 va_end(vargs);
852
853 Py_DECREF(callable);
854 return result;
855}
856
857
858PyObject *
859_PyObject_CallMethodIdObjArgs(PyObject *obj,
860 struct _Py_Identifier *name, ...)
861{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100862 if (obj == NULL || name == NULL) {
863 return null_error();
864 }
865
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700866 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
867 if (!oname) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100868 return NULL;
869 }
870
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700871 PyObject *callable = NULL;
872 int is_method = _PyObject_GetMethod(obj, oname, &callable);
873 if (callable == NULL) {
874 return NULL;
875 }
876 obj = is_method ? obj : NULL;
877
878 va_list vargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100879 va_start(vargs, name);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700880 PyObject *result = object_vacall(obj, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100881 va_end(vargs);
882
883 Py_DECREF(callable);
884 return result;
885}
886
887
888PyObject *
889PyObject_CallFunctionObjArgs(PyObject *callable, ...)
890{
891 va_list vargs;
892 PyObject *result;
893
894 va_start(vargs, callable);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700895 result = object_vacall(NULL, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100896 va_end(vargs);
897
898 return result;
899}
900
901
902/* --- PyStack functions ------------------------------------------ */
903
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100904PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200905_PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100906{
907 Py_ssize_t nkwargs;
908 PyObject *kwdict;
909 Py_ssize_t i;
910
911 assert(kwnames != NULL);
912 nkwargs = PyTuple_GET_SIZE(kwnames);
913 kwdict = _PyDict_NewPresized(nkwargs);
914 if (kwdict == NULL) {
915 return NULL;
916 }
917
918 for (i = 0; i < nkwargs; i++) {
919 PyObject *key = PyTuple_GET_ITEM(kwnames, i);
920 PyObject *value = *values++;
921 /* If key already exists, replace it with the new value */
922 if (PyDict_SetItem(kwdict, key, value)) {
923 Py_DECREF(kwdict);
924 return NULL;
925 }
926 }
927 return kwdict;
928}
929
930
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200931/* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
932
933 Allocate a new argument vector and keyword names tuple. Return the argument
934 vector; return NULL with exception set on error. Return the keyword names
935 tuple in *p_kwnames.
936
937 The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
938
939 When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames)
940
941 The type of keyword keys is not checked, these checks should be done
942 later (ex: _PyArg_ParseStackAndKeywords). */
943static PyObject *const *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200944_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200945 PyObject **p_kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100946{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100947 assert(nargs >= 0);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200948 assert(kwargs != NULL);
949 assert(PyDict_Check(kwargs));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100950
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200951 Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs);
952 /* Check for overflow in the PyMem_Malloc() call below. The subtraction
953 * in this check cannot overflow: both maxnargs and nkwargs are
954 * non-negative signed integers, so their difference fits in the type. */
955 Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
956 if (nargs > maxnargs - nkwargs) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100957 PyErr_NoMemory();
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200958 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100959 }
960
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200961 /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
962 PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100963 if (stack == NULL) {
964 PyErr_NoMemory();
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200965 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100966 }
967
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200968 PyObject *kwnames = PyTuple_New(nkwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100969 if (kwnames == NULL) {
970 PyMem_Free(stack);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200971 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100972 }
973
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200974 stack++; /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
975
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200976 /* Copy positional arguments */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200977 for (Py_ssize_t i = 0; i < nargs; i++) {
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200978 Py_INCREF(args[i]);
979 stack[i] = args[i];
980 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100981
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200982 PyObject **kwstack = stack + nargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100983 /* This loop doesn't support lookup function mutating the dictionary
984 to change its size. It's a deliberate choice for speed, this function is
985 called in the performance critical hot code. */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200986 Py_ssize_t pos = 0, i = 0;
987 PyObject *key, *value;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100988 while (PyDict_Next(kwargs, &pos, &key, &value)) {
989 Py_INCREF(key);
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200990 Py_INCREF(value);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100991 PyTuple_SET_ITEM(kwnames, i, key);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100992 kwstack[i] = value;
993 i++;
994 }
995
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100996 *p_kwnames = kwnames;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200997 return stack;
998}
999
1000static void
1001_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
1002 PyObject *kwnames)
1003{
1004 Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
1005 for (Py_ssize_t i = 0; i < n; i++) {
1006 Py_DECREF(stack[i]);
1007 }
1008 PyMem_Free((PyObject **)stack - 1);
1009 Py_DECREF(kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001010}