blob: b7588b302fb7d0172592d5543f91336e3c5a8050 [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 Stinnerbe434dc2019-11-05 00:51:22 +01003#include "pycore_pyerrors.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01004#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +01005#include "pycore_tupleobject.h"
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01006#include "frameobject.h"
7
8
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02009static PyObject *const *
10_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
11 PyObject **p_kwnames);
12
13static void
14_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
15 PyObject *kwnames);
16
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020017
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010018static PyObject *
19null_error(void)
20{
21 if (!PyErr_Occurred())
22 PyErr_SetString(PyExc_SystemError,
23 "null argument to internal routine");
24 return NULL;
25}
26
27
28PyObject*
29_Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where)
30{
31 int err_occurred = (PyErr_Occurred() != NULL);
32
33 assert((callable != NULL) ^ (where != NULL));
34
35 if (result == NULL) {
36 if (!err_occurred) {
37 if (callable)
38 PyErr_Format(PyExc_SystemError,
39 "%R returned NULL without setting an error",
40 callable);
41 else
42 PyErr_Format(PyExc_SystemError,
43 "%s returned NULL without setting an error",
44 where);
45#ifdef Py_DEBUG
46 /* Ensure that the bug is caught in debug mode */
47 Py_FatalError("a function returned NULL without setting an error");
48#endif
49 return NULL;
50 }
51 }
52 else {
53 if (err_occurred) {
54 Py_DECREF(result);
55
56 if (callable) {
57 _PyErr_FormatFromCause(PyExc_SystemError,
58 "%R returned a result with an error set",
59 callable);
60 }
61 else {
62 _PyErr_FormatFromCause(PyExc_SystemError,
63 "%s returned a result with an error set",
64 where);
65 }
66#ifdef Py_DEBUG
67 /* Ensure that the bug is caught in debug mode */
68 Py_FatalError("a function returned a result with an error set");
69#endif
70 return NULL;
71 }
72 }
73 return result;
74}
75
76
77/* --- Core PyObject call functions ------------------------------- */
78
Victor Stinner2ff58a22019-06-17 14:27:23 +020079/* Call a callable Python object without any arguments */
80PyObject *
81PyObject_CallNoArgs(PyObject *func)
82{
83 return _PyObject_CallNoArg(func);
84}
85
86
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010087PyObject *
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020088_PyObject_FastCallDict(PyObject *callable, PyObject *const *args,
89 size_t nargsf, PyObject *kwargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010090{
91 /* _PyObject_FastCallDict() must not be called with an exception set,
92 because it can clear it (directly or indirectly) and so the
93 caller loses its exception */
94 assert(!PyErr_Occurred());
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010095 assert(callable != NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020096
97 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010098 assert(nargs >= 0);
99 assert(nargs == 0 || args != NULL);
100 assert(kwargs == NULL || PyDict_Check(kwargs));
101
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200102 vectorcallfunc func = _PyVectorcall_Function(callable);
103 if (func == NULL) {
104 /* Use tp_call instead */
105 return _PyObject_MakeTpCall(callable, args, nargs, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100106 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200107
108 PyObject *res;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200109 if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200110 res = func(callable, args, nargsf, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100111 }
112 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200113 PyObject *kwnames;
114 PyObject *const *newargs;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200115 newargs = _PyStack_UnpackDict(args, nargs, kwargs, &kwnames);
116 if (newargs == NULL) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100117 return NULL;
118 }
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200119 res = func(callable, newargs,
120 nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
121 _PyStack_UnpackDict_Free(newargs, nargs, kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100122 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200123 return _Py_CheckFunctionResult(callable, res, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100124}
125
126
127PyObject *
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200128_PyObject_MakeTpCall(PyObject *callable, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100129{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100130 PyThreadState *tstate = _PyThreadState_GET();
131
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) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100136 _PyErr_Format(tstate, PyExc_TypeError,
137 "'%.200s' object is not callable",
138 Py_TYPE(callable)->tp_name);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200139 return NULL;
140 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100141
142 assert(nargs >= 0);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200143 assert(nargs == 0 || args != NULL);
144 assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));
145 PyObject *argstuple = _PyTuple_FromArray(args, nargs);
146 if (argstuple == NULL) {
147 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100148 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200149
150 PyObject *kwdict;
151 if (keywords == NULL || PyDict_Check(keywords)) {
152 kwdict = keywords;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100153 }
154 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200155 if (PyTuple_GET_SIZE(keywords)) {
156 assert(args != NULL);
157 kwdict = _PyStack_AsDict(args + nargs, keywords);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100158 if (kwdict == NULL) {
159 Py_DECREF(argstuple);
160 return NULL;
161 }
162 }
163 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200164 keywords = kwdict = NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100165 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100166 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200167
168 PyObject *result = NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100169 if (_Py_EnterRecursiveCall(tstate, " while calling a Python object") == 0)
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200170 {
171 result = call(callable, argstuple, kwdict);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100172 _Py_LeaveRecursiveCall(tstate);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200173 }
174
175 Py_DECREF(argstuple);
176 if (kwdict != keywords) {
177 Py_DECREF(kwdict);
178 }
179
180 result = _Py_CheckFunctionResult(callable, result, NULL);
181 return result;
182}
183
184
185PyObject *
186PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
187{
Petr Viktorinfb9423f2019-06-02 23:52:20 +0200188 /* get vectorcallfunc as in _PyVectorcall_Function, but without
189 * the _Py_TPFLAGS_HAVE_VECTORCALL check */
190 Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
Jeroen Demeyera8b27e62019-06-24 12:41:05 +0200191 if (offset <= 0) {
Petr Viktorinfb9423f2019-06-02 23:52:20 +0200192 PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
193 Py_TYPE(callable)->tp_name);
194 return NULL;
195 }
196 vectorcallfunc func = *(vectorcallfunc *)(((char *)callable) + offset);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200197 if (func == NULL) {
198 PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
199 Py_TYPE(callable)->tp_name);
200 return NULL;
201 }
Petr Viktorinfb9423f2019-06-02 23:52:20 +0200202
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200203 Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200204
205 /* Fast path for no keywords */
206 if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
207 return func(callable, _PyTuple_ITEMS(tuple), nargs, NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200208 }
209
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200210 /* Convert arguments & call */
211 PyObject *const *args;
212 PyObject *kwnames;
213 args = _PyStack_UnpackDict(_PyTuple_ITEMS(tuple), nargs, kwargs, &kwnames);
214 if (args == NULL) {
215 return NULL;
216 }
217 PyObject *result = func(callable, args,
218 nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
219 _PyStack_UnpackDict_Free(args, nargs, kwnames);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200220 return _Py_CheckFunctionResult(callable, result, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100221}
222
223
224PyObject *
225PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
226{
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100227 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100228 ternaryfunc call;
229 PyObject *result;
230
231 /* PyObject_Call() must not be called with an exception set,
232 because it can clear it (directly or indirectly) and so the
233 caller loses its exception */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100234 assert(!_PyErr_Occurred(tstate));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100235 assert(PyTuple_Check(args));
236 assert(kwargs == NULL || PyDict_Check(kwargs));
237
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200238 if (_PyVectorcall_Function(callable) != NULL) {
239 return PyVectorcall_Call(callable, args, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100240 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100241 else {
242 call = callable->ob_type->tp_call;
243 if (call == NULL) {
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100244 _PyErr_Format(tstate, PyExc_TypeError,
245 "'%.200s' object is not callable",
246 callable->ob_type->tp_name);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100247 return NULL;
248 }
249
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100250 if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100251 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100252 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100253
254 result = (*call)(callable, args, kwargs);
255
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100256 _Py_LeaveRecursiveCall(tstate);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100257
258 return _Py_CheckFunctionResult(callable, result, NULL);
259 }
260}
261
262
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200263PyObject *
264PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
265{
266 return PyObject_Call(callable, args, kwargs);
267}
268
269
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100270/* --- PyFunction call functions ---------------------------------- */
271
272static PyObject* _Py_HOT_FUNCTION
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200273function_code_fastcall(PyCodeObject *co, PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100274 PyObject *globals)
275{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100276 assert(globals != NULL);
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100277
278 PyThreadState *tstate = _PyThreadState_GET();
279 assert(tstate != NULL);
280
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100281 /* XXX Perhaps we should create a specialized
282 _PyFrame_New_NoTrack() that doesn't take locals, but does
283 take builtins without sanity checking them.
284 */
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100285 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100286 if (f == NULL) {
287 return NULL;
288 }
289
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100290 PyObject **fastlocals = f->f_localsplus;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100291
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100292 for (Py_ssize_t i = 0; i < nargs; i++) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100293 Py_INCREF(*args);
294 fastlocals[i] = *args++;
295 }
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100296 PyObject *result = PyEval_EvalFrameEx(f, 0);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100297
298 if (Py_REFCNT(f) > 1) {
299 Py_DECREF(f);
300 _PyObject_GC_TRACK(f);
301 }
302 else {
303 ++tstate->recursion_depth;
304 Py_DECREF(f);
305 --tstate->recursion_depth;
306 }
307 return result;
308}
309
310
311PyObject *
Jeroen Demeyer37788bc2019-05-30 15:11:22 +0200312_PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
313 size_t nargsf, PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100314{
315 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
316 PyObject *globals = PyFunction_GET_GLOBALS(func);
317 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
318 PyObject *kwdefs, *closure, *name, *qualname;
319 PyObject **d;
320 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
321 Py_ssize_t nd;
322
323 assert(PyFunction_Check(func));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200324 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100325 assert(nargs >= 0);
326 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
327 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Jeroen Demeyer05677862019-08-16 12:41:27 +0200328 /* kwnames must only contain strings and all keys must be unique */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100329
330 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner086c3ae2017-10-25 05:26:17 -0700331 (co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100332 {
Pablo Galindocd74e662019-06-01 18:08:04 +0100333 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100334 return function_code_fastcall(co, stack, nargs, globals);
335 }
336 else if (nargs == 0 && argdefs != NULL
Pablo Galindocd74e662019-06-01 18:08:04 +0100337 && co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100338 /* function called with no arguments, but all parameters have
339 a default value: use default values as arguments .*/
Victor Stinnerd17a6932018-11-09 16:56:48 +0100340 stack = _PyTuple_ITEMS(argdefs);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200341 return function_code_fastcall(co, stack, PyTuple_GET_SIZE(argdefs),
342 globals);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100343 }
344 }
345
346 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
347 closure = PyFunction_GET_CLOSURE(func);
348 name = ((PyFunctionObject *)func) -> func_name;
349 qualname = ((PyFunctionObject *)func) -> func_qualname;
350
351 if (argdefs != NULL) {
Victor Stinnerd17a6932018-11-09 16:56:48 +0100352 d = _PyTuple_ITEMS(argdefs);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200353 nd = PyTuple_GET_SIZE(argdefs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100354 }
355 else {
356 d = NULL;
357 nd = 0;
358 }
359 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
360 stack, nargs,
Victor Stinnerd17a6932018-11-09 16:56:48 +0100361 nkwargs ? _PyTuple_ITEMS(kwnames) : NULL,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100362 stack + nargs,
363 nkwargs, 1,
364 d, (int)nd, kwdefs,
365 closure, name, qualname);
366}
367
368
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100369/* --- More complex call functions -------------------------------- */
370
371/* External interface to call any callable object.
372 The args must be a tuple or NULL. The kwargs must be a dict or NULL. */
373PyObject *
374PyEval_CallObjectWithKeywords(PyObject *callable,
375 PyObject *args, PyObject *kwargs)
376{
377#ifdef Py_DEBUG
378 /* PyEval_CallObjectWithKeywords() must not be called with an exception
379 set. It raises a new exception if parameters are invalid or if
380 PyTuple_New() fails, and so the original exception is lost. */
381 assert(!PyErr_Occurred());
382#endif
383
INADA Naoki3824cd82017-03-01 20:41:03 +0900384 if (args != NULL && !PyTuple_Check(args)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100385 PyErr_SetString(PyExc_TypeError,
386 "argument list must be a tuple");
387 return NULL;
388 }
389
390 if (kwargs != NULL && !PyDict_Check(kwargs)) {
391 PyErr_SetString(PyExc_TypeError,
392 "keyword list must be a dictionary");
393 return NULL;
394 }
395
INADA Naoki3824cd82017-03-01 20:41:03 +0900396 if (args == NULL) {
397 return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
398 }
399 else {
400 return PyObject_Call(callable, args, kwargs);
401 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100402}
403
404
405PyObject *
406PyObject_CallObject(PyObject *callable, PyObject *args)
407{
Jeroen Demeyer1dbd0842019-07-11 17:57:32 +0200408 assert(!PyErr_Occurred());
409 if (args == NULL) {
410 return _PyObject_CallNoArg(callable);
411 }
412 if (!PyTuple_Check(args)) {
413 PyErr_SetString(PyExc_TypeError,
414 "argument list must be a tuple");
415 return NULL;
416 }
417 return PyObject_Call(callable, args, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100418}
419
420
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100421/* Call callable(obj, *args, **kwargs). */
422PyObject *
423_PyObject_Call_Prepend(PyObject *callable,
424 PyObject *obj, PyObject *args, PyObject *kwargs)
425{
426 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
427 PyObject **stack;
428 Py_ssize_t argcount;
429 PyObject *result;
430
431 assert(PyTuple_Check(args));
432
433 argcount = PyTuple_GET_SIZE(args);
434 if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
435 stack = small_stack;
436 }
437 else {
438 stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
439 if (stack == NULL) {
440 PyErr_NoMemory();
441 return NULL;
442 }
443 }
444
445 /* use borrowed references */
446 stack[0] = obj;
447 memcpy(&stack[1],
Victor Stinnerd17a6932018-11-09 16:56:48 +0100448 _PyTuple_ITEMS(args),
449 argcount * sizeof(PyObject *));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100450
451 result = _PyObject_FastCallDict(callable,
452 stack, argcount + 1,
453 kwargs);
454 if (stack != small_stack) {
455 PyMem_Free(stack);
456 }
457 return result;
458}
459
460
461/* --- Call with a format string ---------------------------------- */
462
463static PyObject *
464_PyObject_CallFunctionVa(PyObject *callable, const char *format,
465 va_list va, int is_size_t)
466{
467 PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
468 const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
469 PyObject **stack;
470 Py_ssize_t nargs, i;
471 PyObject *result;
472
473 if (callable == NULL) {
474 return null_error();
475 }
476
477 if (!format || !*format) {
478 return _PyObject_CallNoArg(callable);
479 }
480
481 if (is_size_t) {
482 stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
483 format, va, &nargs);
484 }
485 else {
486 stack = _Py_VaBuildStack(small_stack, small_stack_len,
487 format, va, &nargs);
488 }
489 if (stack == NULL) {
490 return NULL;
491 }
492
493 if (nargs == 1 && PyTuple_Check(stack[0])) {
494 /* Special cases for backward compatibility:
495 - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
496 - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
497 func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
498 PyObject *args = stack[0];
499 result = _PyObject_FastCall(callable,
Victor Stinnerd17a6932018-11-09 16:56:48 +0100500 _PyTuple_ITEMS(args),
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100501 PyTuple_GET_SIZE(args));
502 }
503 else {
504 result = _PyObject_FastCall(callable, stack, nargs);
505 }
506
507 for (i = 0; i < nargs; ++i) {
508 Py_DECREF(stack[i]);
509 }
510 if (stack != small_stack) {
511 PyMem_Free(stack);
512 }
513 return result;
514}
515
516
517PyObject *
518PyObject_CallFunction(PyObject *callable, const char *format, ...)
519{
520 va_list va;
521 PyObject *result;
522
523 va_start(va, format);
524 result = _PyObject_CallFunctionVa(callable, format, va, 0);
525 va_end(va);
526
527 return result;
528}
529
530
INADA Naokiaa289a52017-03-14 18:00:59 +0900531/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
532 * This function is kept for backward compatibility.
533 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100534PyObject *
535PyEval_CallFunction(PyObject *callable, const char *format, ...)
536{
INADA Naokiaa289a52017-03-14 18:00:59 +0900537 va_list va;
538 PyObject *result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100539
INADA Naokiaa289a52017-03-14 18:00:59 +0900540 va_start(va, format);
541 result = _PyObject_CallFunctionVa(callable, format, va, 0);
542 va_end(va);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100543
INADA Naokiaa289a52017-03-14 18:00:59 +0900544 return result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100545}
546
547
548PyObject *
549_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
550{
551 va_list va;
552 PyObject *result;
553
554 va_start(va, format);
555 result = _PyObject_CallFunctionVa(callable, format, va, 1);
556 va_end(va);
557
558 return result;
559}
560
561
562static PyObject*
563callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
564{
565 assert(callable != NULL);
566
567 if (!PyCallable_Check(callable)) {
568 PyErr_Format(PyExc_TypeError,
569 "attribute of type '%.200s' is not callable",
570 Py_TYPE(callable)->tp_name);
571 return NULL;
572 }
573
574 return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
575}
576
577
578PyObject *
579PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
580{
581 va_list va;
582 PyObject *callable, *retval;
583
584 if (obj == NULL || name == NULL) {
585 return null_error();
586 }
587
588 callable = PyObject_GetAttrString(obj, name);
589 if (callable == NULL)
590 return NULL;
591
592 va_start(va, format);
593 retval = callmethod(callable, format, va, 0);
594 va_end(va);
595
596 Py_DECREF(callable);
597 return retval;
598}
599
600
INADA Naokiaa289a52017-03-14 18:00:59 +0900601/* PyEval_CallMethod is exact copy of PyObject_CallMethod.
602 * This function is kept for backward compatibility.
603 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100604PyObject *
605PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
606{
INADA Naokiaa289a52017-03-14 18:00:59 +0900607 va_list va;
608 PyObject *callable, *retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100609
INADA Naokiaa289a52017-03-14 18:00:59 +0900610 if (obj == NULL || name == NULL) {
611 return null_error();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100612 }
613
INADA Naokiaa289a52017-03-14 18:00:59 +0900614 callable = PyObject_GetAttrString(obj, name);
615 if (callable == NULL)
616 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100617
INADA Naokiaa289a52017-03-14 18:00:59 +0900618 va_start(va, format);
619 retval = callmethod(callable, format, va, 0);
620 va_end(va);
621
622 Py_DECREF(callable);
623 return retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100624}
625
626
627PyObject *
628_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
629 const char *format, ...)
630{
631 va_list va;
632 PyObject *callable, *retval;
633
634 if (obj == NULL || name == NULL) {
635 return null_error();
636 }
637
638 callable = _PyObject_GetAttrId(obj, name);
639 if (callable == NULL)
640 return NULL;
641
642 va_start(va, format);
643 retval = callmethod(callable, format, va, 0);
644 va_end(va);
645
646 Py_DECREF(callable);
647 return retval;
648}
649
650
651PyObject *
652_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
653 const char *format, ...)
654{
655 va_list va;
656 PyObject *callable, *retval;
657
658 if (obj == NULL || name == NULL) {
659 return null_error();
660 }
661
662 callable = PyObject_GetAttrString(obj, name);
663 if (callable == NULL)
664 return NULL;
665
666 va_start(va, format);
667 retval = callmethod(callable, format, va, 1);
668 va_end(va);
669
670 Py_DECREF(callable);
671 return retval;
672}
673
674
675PyObject *
676_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
677 const char *format, ...)
678{
679 va_list va;
680 PyObject *callable, *retval;
681
682 if (obj == NULL || name == NULL) {
683 return null_error();
684 }
685
686 callable = _PyObject_GetAttrId(obj, name);
687 if (callable == NULL) {
688 return NULL;
689 }
690
691 va_start(va, format);
692 retval = callmethod(callable, format, va, 1);
693 va_end(va);
694
695 Py_DECREF(callable);
696 return retval;
697}
698
699
700/* --- Call with "..." arguments ---------------------------------- */
701
702static PyObject *
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700703object_vacall(PyObject *base, PyObject *callable, va_list vargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100704{
705 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
706 PyObject **stack;
707 Py_ssize_t nargs;
708 PyObject *result;
709 Py_ssize_t i;
710 va_list countva;
711
712 if (callable == NULL) {
713 return null_error();
714 }
715
716 /* Count the number of arguments */
717 va_copy(countva, vargs);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700718 nargs = base ? 1 : 0;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100719 while (1) {
720 PyObject *arg = va_arg(countva, PyObject *);
721 if (arg == NULL) {
722 break;
723 }
724 nargs++;
725 }
726 va_end(countva);
727
728 /* Copy arguments */
729 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
730 stack = small_stack;
731 }
732 else {
733 stack = PyMem_Malloc(nargs * sizeof(stack[0]));
734 if (stack == NULL) {
735 PyErr_NoMemory();
736 return NULL;
737 }
738 }
739
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700740 i = 0;
741 if (base) {
742 stack[i++] = base;
743 }
744
745 for (; i < nargs; ++i) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100746 stack[i] = va_arg(vargs, PyObject *);
747 }
748
749 /* Call the function */
750 result = _PyObject_FastCall(callable, stack, nargs);
751
752 if (stack != small_stack) {
753 PyMem_Free(stack);
754 }
755 return result;
756}
757
758
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700759PyObject *
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200760_PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
761 size_t nargsf, PyObject *kwnames)
762{
763 assert(name != NULL);
764 assert(args != NULL);
765 assert(PyVectorcall_NARGS(nargsf) >= 1);
766
767 PyObject *callable = NULL;
768 /* Use args[0] as "self" argument */
769 int unbound = _PyObject_GetMethod(args[0], name, &callable);
770 if (callable == NULL) {
771 return NULL;
772 }
773
774 if (unbound) {
775 /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
776 * that would be interpreted as allowing to change args[-1] */
777 nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
778 }
779 else {
780 /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
781 * args[-1] in the onward call is args[0] here. */
782 args++;
783 nargsf--;
784 }
785 PyObject *result = _PyObject_Vectorcall(callable, args, nargsf, kwnames);
786 Py_DECREF(callable);
787 return result;
788}
789
790
791PyObject *
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700792PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
793{
794 if (obj == NULL || name == NULL) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100795 return null_error();
796 }
797
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700798 PyObject *callable = NULL;
799 int is_method = _PyObject_GetMethod(obj, name, &callable);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100800 if (callable == NULL) {
801 return NULL;
802 }
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700803 obj = is_method ? obj : NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100804
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700805 va_list vargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100806 va_start(vargs, name);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700807 PyObject *result = object_vacall(obj, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100808 va_end(vargs);
809
810 Py_DECREF(callable);
811 return result;
812}
813
814
815PyObject *
816_PyObject_CallMethodIdObjArgs(PyObject *obj,
817 struct _Py_Identifier *name, ...)
818{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100819 if (obj == NULL || name == NULL) {
820 return null_error();
821 }
822
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700823 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
824 if (!oname) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100825 return NULL;
826 }
827
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700828 PyObject *callable = NULL;
829 int is_method = _PyObject_GetMethod(obj, oname, &callable);
830 if (callable == NULL) {
831 return NULL;
832 }
833 obj = is_method ? obj : NULL;
834
835 va_list vargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100836 va_start(vargs, name);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700837 PyObject *result = object_vacall(obj, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100838 va_end(vargs);
839
840 Py_DECREF(callable);
841 return result;
842}
843
844
845PyObject *
846PyObject_CallFunctionObjArgs(PyObject *callable, ...)
847{
848 va_list vargs;
849 PyObject *result;
850
851 va_start(vargs, callable);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700852 result = object_vacall(NULL, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100853 va_end(vargs);
854
855 return result;
856}
857
858
859/* --- PyStack functions ------------------------------------------ */
860
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100861PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200862_PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100863{
864 Py_ssize_t nkwargs;
865 PyObject *kwdict;
866 Py_ssize_t i;
867
868 assert(kwnames != NULL);
869 nkwargs = PyTuple_GET_SIZE(kwnames);
870 kwdict = _PyDict_NewPresized(nkwargs);
871 if (kwdict == NULL) {
872 return NULL;
873 }
874
875 for (i = 0; i < nkwargs; i++) {
876 PyObject *key = PyTuple_GET_ITEM(kwnames, i);
877 PyObject *value = *values++;
878 /* If key already exists, replace it with the new value */
879 if (PyDict_SetItem(kwdict, key, value)) {
880 Py_DECREF(kwdict);
881 return NULL;
882 }
883 }
884 return kwdict;
885}
886
887
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200888/* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
889
890 Allocate a new argument vector and keyword names tuple. Return the argument
891 vector; return NULL with exception set on error. Return the keyword names
892 tuple in *p_kwnames.
893
Jeroen Demeyer05677862019-08-16 12:41:27 +0200894 This also checks that all keyword names are strings. If not, a TypeError is
895 raised.
896
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200897 The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
898
Jeroen Demeyer05677862019-08-16 12:41:27 +0200899 When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames) */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200900static PyObject *const *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200901_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200902 PyObject **p_kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100903{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100904 assert(nargs >= 0);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200905 assert(kwargs != NULL);
906 assert(PyDict_Check(kwargs));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100907
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200908 Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs);
909 /* Check for overflow in the PyMem_Malloc() call below. The subtraction
910 * in this check cannot overflow: both maxnargs and nkwargs are
911 * non-negative signed integers, so their difference fits in the type. */
912 Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
913 if (nargs > maxnargs - nkwargs) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100914 PyErr_NoMemory();
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200915 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100916 }
917
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200918 /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
919 PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100920 if (stack == NULL) {
921 PyErr_NoMemory();
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200922 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100923 }
924
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200925 PyObject *kwnames = PyTuple_New(nkwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100926 if (kwnames == NULL) {
927 PyMem_Free(stack);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200928 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100929 }
930
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200931 stack++; /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
932
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200933 /* Copy positional arguments */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200934 for (Py_ssize_t i = 0; i < nargs; i++) {
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200935 Py_INCREF(args[i]);
936 stack[i] = args[i];
937 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100938
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200939 PyObject **kwstack = stack + nargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100940 /* This loop doesn't support lookup function mutating the dictionary
941 to change its size. It's a deliberate choice for speed, this function is
942 called in the performance critical hot code. */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200943 Py_ssize_t pos = 0, i = 0;
944 PyObject *key, *value;
Jeroen Demeyer05677862019-08-16 12:41:27 +0200945 unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100946 while (PyDict_Next(kwargs, &pos, &key, &value)) {
Jeroen Demeyer05677862019-08-16 12:41:27 +0200947 keys_are_strings &= Py_TYPE(key)->tp_flags;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100948 Py_INCREF(key);
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200949 Py_INCREF(value);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100950 PyTuple_SET_ITEM(kwnames, i, key);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100951 kwstack[i] = value;
952 i++;
953 }
954
Jeroen Demeyer05677862019-08-16 12:41:27 +0200955 /* keys_are_strings has the value Py_TPFLAGS_UNICODE_SUBCLASS if that
956 * flag is set for all keys. Otherwise, keys_are_strings equals 0.
957 * We do this check once at the end instead of inside the loop above
958 * because it simplifies the deallocation in the failing case.
959 * It happens to also make the loop above slightly more efficient. */
960 if (!keys_are_strings) {
961 PyErr_SetString(PyExc_TypeError,
962 "keywords must be strings");
963 _PyStack_UnpackDict_Free(stack, nargs, kwnames);
964 return NULL;
965 }
966
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100967 *p_kwnames = kwnames;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200968 return stack;
969}
970
971static void
972_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
973 PyObject *kwnames)
974{
975 Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
976 for (Py_ssize_t i = 0; i < n; i++) {
977 Py_DECREF(stack[i]);
978 }
979 PyMem_Free((PyObject **)stack - 1);
980 Py_DECREF(kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100981}