blob: 7d917891bc0cd9f08ffb00a9055450c7ba35b74d [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{
Jeroen Demeyer1dbd0842019-07-11 17:57:32 +0200460 assert(!PyErr_Occurred());
461 if (args == NULL) {
462 return _PyObject_CallNoArg(callable);
463 }
464 if (!PyTuple_Check(args)) {
465 PyErr_SetString(PyExc_TypeError,
466 "argument list must be a tuple");
467 return NULL;
468 }
469 return PyObject_Call(callable, args, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100470}
471
472
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100473/* Call callable(obj, *args, **kwargs). */
474PyObject *
475_PyObject_Call_Prepend(PyObject *callable,
476 PyObject *obj, PyObject *args, PyObject *kwargs)
477{
478 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
479 PyObject **stack;
480 Py_ssize_t argcount;
481 PyObject *result;
482
483 assert(PyTuple_Check(args));
484
485 argcount = PyTuple_GET_SIZE(args);
486 if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
487 stack = small_stack;
488 }
489 else {
490 stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
491 if (stack == NULL) {
492 PyErr_NoMemory();
493 return NULL;
494 }
495 }
496
497 /* use borrowed references */
498 stack[0] = obj;
499 memcpy(&stack[1],
Victor Stinnerd17a6932018-11-09 16:56:48 +0100500 _PyTuple_ITEMS(args),
501 argcount * sizeof(PyObject *));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100502
503 result = _PyObject_FastCallDict(callable,
504 stack, argcount + 1,
505 kwargs);
506 if (stack != small_stack) {
507 PyMem_Free(stack);
508 }
509 return result;
510}
511
512
513/* --- Call with a format string ---------------------------------- */
514
515static PyObject *
516_PyObject_CallFunctionVa(PyObject *callable, const char *format,
517 va_list va, int is_size_t)
518{
519 PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
520 const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
521 PyObject **stack;
522 Py_ssize_t nargs, i;
523 PyObject *result;
524
525 if (callable == NULL) {
526 return null_error();
527 }
528
529 if (!format || !*format) {
530 return _PyObject_CallNoArg(callable);
531 }
532
533 if (is_size_t) {
534 stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
535 format, va, &nargs);
536 }
537 else {
538 stack = _Py_VaBuildStack(small_stack, small_stack_len,
539 format, va, &nargs);
540 }
541 if (stack == NULL) {
542 return NULL;
543 }
544
545 if (nargs == 1 && PyTuple_Check(stack[0])) {
546 /* Special cases for backward compatibility:
547 - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
548 - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
549 func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
550 PyObject *args = stack[0];
551 result = _PyObject_FastCall(callable,
Victor Stinnerd17a6932018-11-09 16:56:48 +0100552 _PyTuple_ITEMS(args),
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100553 PyTuple_GET_SIZE(args));
554 }
555 else {
556 result = _PyObject_FastCall(callable, stack, nargs);
557 }
558
559 for (i = 0; i < nargs; ++i) {
560 Py_DECREF(stack[i]);
561 }
562 if (stack != small_stack) {
563 PyMem_Free(stack);
564 }
565 return result;
566}
567
568
569PyObject *
570PyObject_CallFunction(PyObject *callable, const char *format, ...)
571{
572 va_list va;
573 PyObject *result;
574
575 va_start(va, format);
576 result = _PyObject_CallFunctionVa(callable, format, va, 0);
577 va_end(va);
578
579 return result;
580}
581
582
INADA Naokiaa289a52017-03-14 18:00:59 +0900583/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
584 * This function is kept for backward compatibility.
585 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100586PyObject *
587PyEval_CallFunction(PyObject *callable, const char *format, ...)
588{
INADA Naokiaa289a52017-03-14 18:00:59 +0900589 va_list va;
590 PyObject *result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100591
INADA Naokiaa289a52017-03-14 18:00:59 +0900592 va_start(va, format);
593 result = _PyObject_CallFunctionVa(callable, format, va, 0);
594 va_end(va);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100595
INADA Naokiaa289a52017-03-14 18:00:59 +0900596 return result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100597}
598
599
600PyObject *
601_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
602{
603 va_list va;
604 PyObject *result;
605
606 va_start(va, format);
607 result = _PyObject_CallFunctionVa(callable, format, va, 1);
608 va_end(va);
609
610 return result;
611}
612
613
614static PyObject*
615callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
616{
617 assert(callable != NULL);
618
619 if (!PyCallable_Check(callable)) {
620 PyErr_Format(PyExc_TypeError,
621 "attribute of type '%.200s' is not callable",
622 Py_TYPE(callable)->tp_name);
623 return NULL;
624 }
625
626 return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
627}
628
629
630PyObject *
631PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
632{
633 va_list va;
634 PyObject *callable, *retval;
635
636 if (obj == NULL || name == NULL) {
637 return null_error();
638 }
639
640 callable = PyObject_GetAttrString(obj, name);
641 if (callable == NULL)
642 return NULL;
643
644 va_start(va, format);
645 retval = callmethod(callable, format, va, 0);
646 va_end(va);
647
648 Py_DECREF(callable);
649 return retval;
650}
651
652
INADA Naokiaa289a52017-03-14 18:00:59 +0900653/* PyEval_CallMethod is exact copy of PyObject_CallMethod.
654 * This function is kept for backward compatibility.
655 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100656PyObject *
657PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
658{
INADA Naokiaa289a52017-03-14 18:00:59 +0900659 va_list va;
660 PyObject *callable, *retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100661
INADA Naokiaa289a52017-03-14 18:00:59 +0900662 if (obj == NULL || name == NULL) {
663 return null_error();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100664 }
665
INADA Naokiaa289a52017-03-14 18:00:59 +0900666 callable = PyObject_GetAttrString(obj, name);
667 if (callable == NULL)
668 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100669
INADA Naokiaa289a52017-03-14 18:00:59 +0900670 va_start(va, format);
671 retval = callmethod(callable, format, va, 0);
672 va_end(va);
673
674 Py_DECREF(callable);
675 return retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100676}
677
678
679PyObject *
680_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
681 const char *format, ...)
682{
683 va_list va;
684 PyObject *callable, *retval;
685
686 if (obj == NULL || name == NULL) {
687 return null_error();
688 }
689
690 callable = _PyObject_GetAttrId(obj, name);
691 if (callable == NULL)
692 return NULL;
693
694 va_start(va, format);
695 retval = callmethod(callable, format, va, 0);
696 va_end(va);
697
698 Py_DECREF(callable);
699 return retval;
700}
701
702
703PyObject *
704_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
705 const char *format, ...)
706{
707 va_list va;
708 PyObject *callable, *retval;
709
710 if (obj == NULL || name == NULL) {
711 return null_error();
712 }
713
714 callable = PyObject_GetAttrString(obj, name);
715 if (callable == NULL)
716 return NULL;
717
718 va_start(va, format);
719 retval = callmethod(callable, format, va, 1);
720 va_end(va);
721
722 Py_DECREF(callable);
723 return retval;
724}
725
726
727PyObject *
728_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
729 const char *format, ...)
730{
731 va_list va;
732 PyObject *callable, *retval;
733
734 if (obj == NULL || name == NULL) {
735 return null_error();
736 }
737
738 callable = _PyObject_GetAttrId(obj, name);
739 if (callable == NULL) {
740 return NULL;
741 }
742
743 va_start(va, format);
744 retval = callmethod(callable, format, va, 1);
745 va_end(va);
746
747 Py_DECREF(callable);
748 return retval;
749}
750
751
752/* --- Call with "..." arguments ---------------------------------- */
753
754static PyObject *
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700755object_vacall(PyObject *base, PyObject *callable, va_list vargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100756{
757 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
758 PyObject **stack;
759 Py_ssize_t nargs;
760 PyObject *result;
761 Py_ssize_t i;
762 va_list countva;
763
764 if (callable == NULL) {
765 return null_error();
766 }
767
768 /* Count the number of arguments */
769 va_copy(countva, vargs);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700770 nargs = base ? 1 : 0;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100771 while (1) {
772 PyObject *arg = va_arg(countva, PyObject *);
773 if (arg == NULL) {
774 break;
775 }
776 nargs++;
777 }
778 va_end(countva);
779
780 /* Copy arguments */
781 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
782 stack = small_stack;
783 }
784 else {
785 stack = PyMem_Malloc(nargs * sizeof(stack[0]));
786 if (stack == NULL) {
787 PyErr_NoMemory();
788 return NULL;
789 }
790 }
791
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700792 i = 0;
793 if (base) {
794 stack[i++] = base;
795 }
796
797 for (; i < nargs; ++i) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100798 stack[i] = va_arg(vargs, PyObject *);
799 }
800
801 /* Call the function */
802 result = _PyObject_FastCall(callable, stack, nargs);
803
804 if (stack != small_stack) {
805 PyMem_Free(stack);
806 }
807 return result;
808}
809
810
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700811PyObject *
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200812_PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
813 size_t nargsf, PyObject *kwnames)
814{
815 assert(name != NULL);
816 assert(args != NULL);
817 assert(PyVectorcall_NARGS(nargsf) >= 1);
818
819 PyObject *callable = NULL;
820 /* Use args[0] as "self" argument */
821 int unbound = _PyObject_GetMethod(args[0], name, &callable);
822 if (callable == NULL) {
823 return NULL;
824 }
825
826 if (unbound) {
827 /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
828 * that would be interpreted as allowing to change args[-1] */
829 nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
830 }
831 else {
832 /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
833 * args[-1] in the onward call is args[0] here. */
834 args++;
835 nargsf--;
836 }
837 PyObject *result = _PyObject_Vectorcall(callable, args, nargsf, kwnames);
838 Py_DECREF(callable);
839 return result;
840}
841
842
843PyObject *
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700844PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
845{
846 if (obj == NULL || name == NULL) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100847 return null_error();
848 }
849
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700850 PyObject *callable = NULL;
851 int is_method = _PyObject_GetMethod(obj, name, &callable);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100852 if (callable == NULL) {
853 return NULL;
854 }
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700855 obj = is_method ? obj : NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100856
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700857 va_list vargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100858 va_start(vargs, name);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700859 PyObject *result = object_vacall(obj, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100860 va_end(vargs);
861
862 Py_DECREF(callable);
863 return result;
864}
865
866
867PyObject *
868_PyObject_CallMethodIdObjArgs(PyObject *obj,
869 struct _Py_Identifier *name, ...)
870{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100871 if (obj == NULL || name == NULL) {
872 return null_error();
873 }
874
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700875 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
876 if (!oname) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100877 return NULL;
878 }
879
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700880 PyObject *callable = NULL;
881 int is_method = _PyObject_GetMethod(obj, oname, &callable);
882 if (callable == NULL) {
883 return NULL;
884 }
885 obj = is_method ? obj : NULL;
886
887 va_list vargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100888 va_start(vargs, name);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700889 PyObject *result = object_vacall(obj, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100890 va_end(vargs);
891
892 Py_DECREF(callable);
893 return result;
894}
895
896
897PyObject *
898PyObject_CallFunctionObjArgs(PyObject *callable, ...)
899{
900 va_list vargs;
901 PyObject *result;
902
903 va_start(vargs, callable);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700904 result = object_vacall(NULL, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100905 va_end(vargs);
906
907 return result;
908}
909
910
911/* --- PyStack functions ------------------------------------------ */
912
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100913PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200914_PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100915{
916 Py_ssize_t nkwargs;
917 PyObject *kwdict;
918 Py_ssize_t i;
919
920 assert(kwnames != NULL);
921 nkwargs = PyTuple_GET_SIZE(kwnames);
922 kwdict = _PyDict_NewPresized(nkwargs);
923 if (kwdict == NULL) {
924 return NULL;
925 }
926
927 for (i = 0; i < nkwargs; i++) {
928 PyObject *key = PyTuple_GET_ITEM(kwnames, i);
929 PyObject *value = *values++;
930 /* If key already exists, replace it with the new value */
931 if (PyDict_SetItem(kwdict, key, value)) {
932 Py_DECREF(kwdict);
933 return NULL;
934 }
935 }
936 return kwdict;
937}
938
939
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200940/* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
941
942 Allocate a new argument vector and keyword names tuple. Return the argument
943 vector; return NULL with exception set on error. Return the keyword names
944 tuple in *p_kwnames.
945
946 The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
947
948 When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames)
949
950 The type of keyword keys is not checked, these checks should be done
951 later (ex: _PyArg_ParseStackAndKeywords). */
952static PyObject *const *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200953_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200954 PyObject **p_kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100955{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100956 assert(nargs >= 0);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200957 assert(kwargs != NULL);
958 assert(PyDict_Check(kwargs));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100959
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200960 Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs);
961 /* Check for overflow in the PyMem_Malloc() call below. The subtraction
962 * in this check cannot overflow: both maxnargs and nkwargs are
963 * non-negative signed integers, so their difference fits in the type. */
964 Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
965 if (nargs > maxnargs - nkwargs) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100966 PyErr_NoMemory();
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200967 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100968 }
969
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200970 /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
971 PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100972 if (stack == NULL) {
973 PyErr_NoMemory();
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200974 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100975 }
976
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200977 PyObject *kwnames = PyTuple_New(nkwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100978 if (kwnames == NULL) {
979 PyMem_Free(stack);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200980 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100981 }
982
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200983 stack++; /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
984
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200985 /* Copy positional arguments */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200986 for (Py_ssize_t i = 0; i < nargs; i++) {
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200987 Py_INCREF(args[i]);
988 stack[i] = args[i];
989 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100990
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200991 PyObject **kwstack = stack + nargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100992 /* This loop doesn't support lookup function mutating the dictionary
993 to change its size. It's a deliberate choice for speed, this function is
994 called in the performance critical hot code. */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200995 Py_ssize_t pos = 0, i = 0;
996 PyObject *key, *value;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100997 while (PyDict_Next(kwargs, &pos, &key, &value)) {
998 Py_INCREF(key);
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200999 Py_INCREF(value);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001000 PyTuple_SET_ITEM(kwnames, i, key);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001001 kwstack[i] = value;
1002 i++;
1003 }
1004
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001005 *p_kwnames = kwnames;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001006 return stack;
1007}
1008
1009static void
1010_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
1011 PyObject *kwnames)
1012{
1013 Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
1014 for (Py_ssize_t i = 0; i < n; i++) {
1015 Py_DECREF(stack[i]);
1016 }
1017 PyMem_Free((PyObject **)stack - 1);
1018 Py_DECREF(kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001019}