blob: a715bcbee4addfb475c06f880a5ee63e85acb0a6 [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 Demeyerd4efd912019-07-02 11:49:40 +02008static PyObject *const *
9_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
10 PyObject **p_kwnames);
11
12static void
13_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
14 PyObject *kwnames);
15
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020016
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010017static PyObject *
18null_error(void)
19{
20 if (!PyErr_Occurred())
21 PyErr_SetString(PyExc_SystemError,
22 "null argument to internal routine");
23 return NULL;
24}
25
26
27PyObject*
28_Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where)
29{
30 int err_occurred = (PyErr_Occurred() != NULL);
31
32 assert((callable != NULL) ^ (where != NULL));
33
34 if (result == NULL) {
35 if (!err_occurred) {
36 if (callable)
37 PyErr_Format(PyExc_SystemError,
38 "%R returned NULL without setting an error",
39 callable);
40 else
41 PyErr_Format(PyExc_SystemError,
42 "%s returned NULL without setting an error",
43 where);
44#ifdef Py_DEBUG
45 /* Ensure that the bug is caught in debug mode */
46 Py_FatalError("a function returned NULL without setting an error");
47#endif
48 return NULL;
49 }
50 }
51 else {
52 if (err_occurred) {
53 Py_DECREF(result);
54
55 if (callable) {
56 _PyErr_FormatFromCause(PyExc_SystemError,
57 "%R returned a result with an error set",
58 callable);
59 }
60 else {
61 _PyErr_FormatFromCause(PyExc_SystemError,
62 "%s returned a result with an error set",
63 where);
64 }
65#ifdef Py_DEBUG
66 /* Ensure that the bug is caught in debug mode */
67 Py_FatalError("a function returned a result with an error set");
68#endif
69 return NULL;
70 }
71 }
72 return result;
73}
74
75
76/* --- Core PyObject call functions ------------------------------- */
77
Victor Stinner2ff58a22019-06-17 14:27:23 +020078/* Call a callable Python object without any arguments */
79PyObject *
80PyObject_CallNoArgs(PyObject *func)
81{
82 return _PyObject_CallNoArg(func);
83}
84
85
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010086PyObject *
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020087_PyObject_FastCallDict(PyObject *callable, PyObject *const *args,
88 size_t nargsf, PyObject *kwargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010089{
90 /* _PyObject_FastCallDict() must not be called with an exception set,
91 because it can clear it (directly or indirectly) and so the
92 caller loses its exception */
93 assert(!PyErr_Occurred());
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010094 assert(callable != NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +020095
96 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010097 assert(nargs >= 0);
98 assert(nargs == 0 || args != NULL);
99 assert(kwargs == NULL || PyDict_Check(kwargs));
100
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200101 vectorcallfunc func = _PyVectorcall_Function(callable);
102 if (func == NULL) {
103 /* Use tp_call instead */
104 return _PyObject_MakeTpCall(callable, args, nargs, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100105 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200106
107 PyObject *res;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200108 if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200109 res = func(callable, args, nargsf, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100110 }
111 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200112 PyObject *kwnames;
113 PyObject *const *newargs;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200114 newargs = _PyStack_UnpackDict(args, nargs, kwargs, &kwnames);
115 if (newargs == NULL) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100116 return NULL;
117 }
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200118 res = func(callable, newargs,
119 nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
120 _PyStack_UnpackDict_Free(newargs, nargs, kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100121 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200122 return _Py_CheckFunctionResult(callable, res, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100123}
124
125
126PyObject *
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200127_PyObject_MakeTpCall(PyObject *callable, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100128{
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200129 /* Slow path: build a temporary tuple for positional arguments and a
130 * temporary dictionary for keyword arguments (if any) */
131 ternaryfunc call = Py_TYPE(callable)->tp_call;
132 if (call == NULL) {
133 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
134 Py_TYPE(callable)->tp_name);
135 return NULL;
136 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100137
138 assert(nargs >= 0);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200139 assert(nargs == 0 || args != NULL);
140 assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));
141 PyObject *argstuple = _PyTuple_FromArray(args, nargs);
142 if (argstuple == NULL) {
143 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100144 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200145
146 PyObject *kwdict;
147 if (keywords == NULL || PyDict_Check(keywords)) {
148 kwdict = keywords;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100149 }
150 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200151 if (PyTuple_GET_SIZE(keywords)) {
152 assert(args != NULL);
153 kwdict = _PyStack_AsDict(args + nargs, keywords);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100154 if (kwdict == NULL) {
155 Py_DECREF(argstuple);
156 return NULL;
157 }
158 }
159 else {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200160 keywords = kwdict = NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100161 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100162 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200163
164 PyObject *result = NULL;
165 if (Py_EnterRecursiveCall(" while calling a Python object") == 0)
166 {
167 result = call(callable, argstuple, kwdict);
168 Py_LeaveRecursiveCall();
169 }
170
171 Py_DECREF(argstuple);
172 if (kwdict != keywords) {
173 Py_DECREF(kwdict);
174 }
175
176 result = _Py_CheckFunctionResult(callable, result, NULL);
177 return result;
178}
179
180
181PyObject *
182PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
183{
Petr Viktorinfb9423f2019-06-02 23:52:20 +0200184 /* get vectorcallfunc as in _PyVectorcall_Function, but without
185 * the _Py_TPFLAGS_HAVE_VECTORCALL check */
186 Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
Jeroen Demeyera8b27e62019-06-24 12:41:05 +0200187 if (offset <= 0) {
Petr Viktorinfb9423f2019-06-02 23:52:20 +0200188 PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
189 Py_TYPE(callable)->tp_name);
190 return NULL;
191 }
192 vectorcallfunc func = *(vectorcallfunc *)(((char *)callable) + offset);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200193 if (func == NULL) {
194 PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
195 Py_TYPE(callable)->tp_name);
196 return NULL;
197 }
Petr Viktorinfb9423f2019-06-02 23:52:20 +0200198
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200199 Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200200
201 /* Fast path for no keywords */
202 if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
203 return func(callable, _PyTuple_ITEMS(tuple), nargs, NULL);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200204 }
205
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200206 /* Convert arguments & call */
207 PyObject *const *args;
208 PyObject *kwnames;
209 args = _PyStack_UnpackDict(_PyTuple_ITEMS(tuple), nargs, kwargs, &kwnames);
210 if (args == NULL) {
211 return NULL;
212 }
213 PyObject *result = func(callable, args,
214 nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
215 _PyStack_UnpackDict_Free(args, nargs, kwnames);
Jeroen Demeyer0d722f32019-07-05 14:48:24 +0200216 return _Py_CheckFunctionResult(callable, result, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100217}
218
219
220PyObject *
221PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
222{
223 ternaryfunc call;
224 PyObject *result;
225
226 /* PyObject_Call() must not be called with an exception set,
227 because it can clear it (directly or indirectly) and so the
228 caller loses its exception */
229 assert(!PyErr_Occurred());
230 assert(PyTuple_Check(args));
231 assert(kwargs == NULL || PyDict_Check(kwargs));
232
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200233 if (_PyVectorcall_Function(callable) != NULL) {
234 return PyVectorcall_Call(callable, args, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100235 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100236 else {
237 call = callable->ob_type->tp_call;
238 if (call == NULL) {
239 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
240 callable->ob_type->tp_name);
241 return NULL;
242 }
243
244 if (Py_EnterRecursiveCall(" while calling a Python object"))
245 return NULL;
246
247 result = (*call)(callable, args, kwargs);
248
249 Py_LeaveRecursiveCall();
250
251 return _Py_CheckFunctionResult(callable, result, NULL);
252 }
253}
254
255
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +0200256PyObject *
257PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
258{
259 return PyObject_Call(callable, args, kwargs);
260}
261
262
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100263/* --- PyFunction call functions ---------------------------------- */
264
265static PyObject* _Py_HOT_FUNCTION
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200266function_code_fastcall(PyCodeObject *co, PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100267 PyObject *globals)
268{
269 PyFrameObject *f;
Victor Stinner50b48572018-11-01 01:51:40 +0100270 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100271 PyObject **fastlocals;
272 Py_ssize_t i;
273 PyObject *result;
274
275 assert(globals != NULL);
276 /* XXX Perhaps we should create a specialized
277 _PyFrame_New_NoTrack() that doesn't take locals, but does
278 take builtins without sanity checking them.
279 */
280 assert(tstate != NULL);
281 f = _PyFrame_New_NoTrack(tstate, co, globals, NULL);
282 if (f == NULL) {
283 return NULL;
284 }
285
286 fastlocals = f->f_localsplus;
287
288 for (i = 0; i < nargs; i++) {
289 Py_INCREF(*args);
290 fastlocals[i] = *args++;
291 }
292 result = PyEval_EvalFrameEx(f,0);
293
294 if (Py_REFCNT(f) > 1) {
295 Py_DECREF(f);
296 _PyObject_GC_TRACK(f);
297 }
298 else {
299 ++tstate->recursion_depth;
300 Py_DECREF(f);
301 --tstate->recursion_depth;
302 }
303 return result;
304}
305
306
307PyObject *
Jeroen Demeyer37788bc2019-05-30 15:11:22 +0200308_PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
309 size_t nargsf, PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100310{
311 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
312 PyObject *globals = PyFunction_GET_GLOBALS(func);
313 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
314 PyObject *kwdefs, *closure, *name, *qualname;
315 PyObject **d;
316 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
317 Py_ssize_t nd;
318
319 assert(PyFunction_Check(func));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200320 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100321 assert(nargs >= 0);
322 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
323 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Jeroen Demeyer05677862019-08-16 12:41:27 +0200324 /* kwnames must only contain strings and all keys must be unique */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100325
326 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner086c3ae2017-10-25 05:26:17 -0700327 (co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100328 {
Pablo Galindocd74e662019-06-01 18:08:04 +0100329 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100330 return function_code_fastcall(co, stack, nargs, globals);
331 }
332 else if (nargs == 0 && argdefs != NULL
Pablo Galindocd74e662019-06-01 18:08:04 +0100333 && co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100334 /* function called with no arguments, but all parameters have
335 a default value: use default values as arguments .*/
Victor Stinnerd17a6932018-11-09 16:56:48 +0100336 stack = _PyTuple_ITEMS(argdefs);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200337 return function_code_fastcall(co, stack, PyTuple_GET_SIZE(argdefs),
338 globals);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100339 }
340 }
341
342 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
343 closure = PyFunction_GET_CLOSURE(func);
344 name = ((PyFunctionObject *)func) -> func_name;
345 qualname = ((PyFunctionObject *)func) -> func_qualname;
346
347 if (argdefs != NULL) {
Victor Stinnerd17a6932018-11-09 16:56:48 +0100348 d = _PyTuple_ITEMS(argdefs);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200349 nd = PyTuple_GET_SIZE(argdefs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100350 }
351 else {
352 d = NULL;
353 nd = 0;
354 }
355 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
356 stack, nargs,
Victor Stinnerd17a6932018-11-09 16:56:48 +0100357 nkwargs ? _PyTuple_ITEMS(kwnames) : NULL,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100358 stack + nargs,
359 nkwargs, 1,
360 d, (int)nd, kwdefs,
361 closure, name, qualname);
362}
363
364
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100365/* --- More complex call functions -------------------------------- */
366
367/* External interface to call any callable object.
368 The args must be a tuple or NULL. The kwargs must be a dict or NULL. */
369PyObject *
370PyEval_CallObjectWithKeywords(PyObject *callable,
371 PyObject *args, PyObject *kwargs)
372{
373#ifdef Py_DEBUG
374 /* PyEval_CallObjectWithKeywords() must not be called with an exception
375 set. It raises a new exception if parameters are invalid or if
376 PyTuple_New() fails, and so the original exception is lost. */
377 assert(!PyErr_Occurred());
378#endif
379
INADA Naoki3824cd82017-03-01 20:41:03 +0900380 if (args != NULL && !PyTuple_Check(args)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100381 PyErr_SetString(PyExc_TypeError,
382 "argument list must be a tuple");
383 return NULL;
384 }
385
386 if (kwargs != NULL && !PyDict_Check(kwargs)) {
387 PyErr_SetString(PyExc_TypeError,
388 "keyword list must be a dictionary");
389 return NULL;
390 }
391
INADA Naoki3824cd82017-03-01 20:41:03 +0900392 if (args == NULL) {
393 return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
394 }
395 else {
396 return PyObject_Call(callable, args, kwargs);
397 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100398}
399
400
401PyObject *
402PyObject_CallObject(PyObject *callable, PyObject *args)
403{
Jeroen Demeyer1dbd0842019-07-11 17:57:32 +0200404 assert(!PyErr_Occurred());
405 if (args == NULL) {
406 return _PyObject_CallNoArg(callable);
407 }
408 if (!PyTuple_Check(args)) {
409 PyErr_SetString(PyExc_TypeError,
410 "argument list must be a tuple");
411 return NULL;
412 }
413 return PyObject_Call(callable, args, NULL);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100414}
415
416
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100417/* Call callable(obj, *args, **kwargs). */
418PyObject *
419_PyObject_Call_Prepend(PyObject *callable,
420 PyObject *obj, PyObject *args, PyObject *kwargs)
421{
422 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
423 PyObject **stack;
424 Py_ssize_t argcount;
425 PyObject *result;
426
427 assert(PyTuple_Check(args));
428
429 argcount = PyTuple_GET_SIZE(args);
430 if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
431 stack = small_stack;
432 }
433 else {
434 stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
435 if (stack == NULL) {
436 PyErr_NoMemory();
437 return NULL;
438 }
439 }
440
441 /* use borrowed references */
442 stack[0] = obj;
443 memcpy(&stack[1],
Victor Stinnerd17a6932018-11-09 16:56:48 +0100444 _PyTuple_ITEMS(args),
445 argcount * sizeof(PyObject *));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100446
447 result = _PyObject_FastCallDict(callable,
448 stack, argcount + 1,
449 kwargs);
450 if (stack != small_stack) {
451 PyMem_Free(stack);
452 }
453 return result;
454}
455
456
457/* --- Call with a format string ---------------------------------- */
458
459static PyObject *
460_PyObject_CallFunctionVa(PyObject *callable, const char *format,
461 va_list va, int is_size_t)
462{
463 PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
464 const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
465 PyObject **stack;
466 Py_ssize_t nargs, i;
467 PyObject *result;
468
469 if (callable == NULL) {
470 return null_error();
471 }
472
473 if (!format || !*format) {
474 return _PyObject_CallNoArg(callable);
475 }
476
477 if (is_size_t) {
478 stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
479 format, va, &nargs);
480 }
481 else {
482 stack = _Py_VaBuildStack(small_stack, small_stack_len,
483 format, va, &nargs);
484 }
485 if (stack == NULL) {
486 return NULL;
487 }
488
489 if (nargs == 1 && PyTuple_Check(stack[0])) {
490 /* Special cases for backward compatibility:
491 - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
492 - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
493 func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
494 PyObject *args = stack[0];
495 result = _PyObject_FastCall(callable,
Victor Stinnerd17a6932018-11-09 16:56:48 +0100496 _PyTuple_ITEMS(args),
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100497 PyTuple_GET_SIZE(args));
498 }
499 else {
500 result = _PyObject_FastCall(callable, stack, nargs);
501 }
502
503 for (i = 0; i < nargs; ++i) {
504 Py_DECREF(stack[i]);
505 }
506 if (stack != small_stack) {
507 PyMem_Free(stack);
508 }
509 return result;
510}
511
512
513PyObject *
514PyObject_CallFunction(PyObject *callable, const char *format, ...)
515{
516 va_list va;
517 PyObject *result;
518
519 va_start(va, format);
520 result = _PyObject_CallFunctionVa(callable, format, va, 0);
521 va_end(va);
522
523 return result;
524}
525
526
INADA Naokiaa289a52017-03-14 18:00:59 +0900527/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
528 * This function is kept for backward compatibility.
529 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100530PyObject *
531PyEval_CallFunction(PyObject *callable, const char *format, ...)
532{
INADA Naokiaa289a52017-03-14 18:00:59 +0900533 va_list va;
534 PyObject *result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100535
INADA Naokiaa289a52017-03-14 18:00:59 +0900536 va_start(va, format);
537 result = _PyObject_CallFunctionVa(callable, format, va, 0);
538 va_end(va);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100539
INADA Naokiaa289a52017-03-14 18:00:59 +0900540 return result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100541}
542
543
544PyObject *
545_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
546{
547 va_list va;
548 PyObject *result;
549
550 va_start(va, format);
551 result = _PyObject_CallFunctionVa(callable, format, va, 1);
552 va_end(va);
553
554 return result;
555}
556
557
558static PyObject*
559callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
560{
561 assert(callable != NULL);
562
563 if (!PyCallable_Check(callable)) {
564 PyErr_Format(PyExc_TypeError,
565 "attribute of type '%.200s' is not callable",
566 Py_TYPE(callable)->tp_name);
567 return NULL;
568 }
569
570 return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
571}
572
573
574PyObject *
575PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
576{
577 va_list va;
578 PyObject *callable, *retval;
579
580 if (obj == NULL || name == NULL) {
581 return null_error();
582 }
583
584 callable = PyObject_GetAttrString(obj, name);
585 if (callable == NULL)
586 return NULL;
587
588 va_start(va, format);
589 retval = callmethod(callable, format, va, 0);
590 va_end(va);
591
592 Py_DECREF(callable);
593 return retval;
594}
595
596
INADA Naokiaa289a52017-03-14 18:00:59 +0900597/* PyEval_CallMethod is exact copy of PyObject_CallMethod.
598 * This function is kept for backward compatibility.
599 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100600PyObject *
601PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
602{
INADA Naokiaa289a52017-03-14 18:00:59 +0900603 va_list va;
604 PyObject *callable, *retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100605
INADA Naokiaa289a52017-03-14 18:00:59 +0900606 if (obj == NULL || name == NULL) {
607 return null_error();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100608 }
609
INADA Naokiaa289a52017-03-14 18:00:59 +0900610 callable = PyObject_GetAttrString(obj, name);
611 if (callable == NULL)
612 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100613
INADA Naokiaa289a52017-03-14 18:00:59 +0900614 va_start(va, format);
615 retval = callmethod(callable, format, va, 0);
616 va_end(va);
617
618 Py_DECREF(callable);
619 return retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100620}
621
622
623PyObject *
624_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
625 const char *format, ...)
626{
627 va_list va;
628 PyObject *callable, *retval;
629
630 if (obj == NULL || name == NULL) {
631 return null_error();
632 }
633
634 callable = _PyObject_GetAttrId(obj, name);
635 if (callable == NULL)
636 return NULL;
637
638 va_start(va, format);
639 retval = callmethod(callable, format, va, 0);
640 va_end(va);
641
642 Py_DECREF(callable);
643 return retval;
644}
645
646
647PyObject *
648_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
649 const char *format, ...)
650{
651 va_list va;
652 PyObject *callable, *retval;
653
654 if (obj == NULL || name == NULL) {
655 return null_error();
656 }
657
658 callable = PyObject_GetAttrString(obj, name);
659 if (callable == NULL)
660 return NULL;
661
662 va_start(va, format);
663 retval = callmethod(callable, format, va, 1);
664 va_end(va);
665
666 Py_DECREF(callable);
667 return retval;
668}
669
670
671PyObject *
672_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
673 const char *format, ...)
674{
675 va_list va;
676 PyObject *callable, *retval;
677
678 if (obj == NULL || name == NULL) {
679 return null_error();
680 }
681
682 callable = _PyObject_GetAttrId(obj, name);
683 if (callable == NULL) {
684 return NULL;
685 }
686
687 va_start(va, format);
688 retval = callmethod(callable, format, va, 1);
689 va_end(va);
690
691 Py_DECREF(callable);
692 return retval;
693}
694
695
696/* --- Call with "..." arguments ---------------------------------- */
697
698static PyObject *
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700699object_vacall(PyObject *base, PyObject *callable, va_list vargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100700{
701 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
702 PyObject **stack;
703 Py_ssize_t nargs;
704 PyObject *result;
705 Py_ssize_t i;
706 va_list countva;
707
708 if (callable == NULL) {
709 return null_error();
710 }
711
712 /* Count the number of arguments */
713 va_copy(countva, vargs);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700714 nargs = base ? 1 : 0;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100715 while (1) {
716 PyObject *arg = va_arg(countva, PyObject *);
717 if (arg == NULL) {
718 break;
719 }
720 nargs++;
721 }
722 va_end(countva);
723
724 /* Copy arguments */
725 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
726 stack = small_stack;
727 }
728 else {
729 stack = PyMem_Malloc(nargs * sizeof(stack[0]));
730 if (stack == NULL) {
731 PyErr_NoMemory();
732 return NULL;
733 }
734 }
735
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700736 i = 0;
737 if (base) {
738 stack[i++] = base;
739 }
740
741 for (; i < nargs; ++i) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100742 stack[i] = va_arg(vargs, PyObject *);
743 }
744
745 /* Call the function */
746 result = _PyObject_FastCall(callable, stack, nargs);
747
748 if (stack != small_stack) {
749 PyMem_Free(stack);
750 }
751 return result;
752}
753
754
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700755PyObject *
Jeroen Demeyerb1263d52019-06-28 11:49:00 +0200756_PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
757 size_t nargsf, PyObject *kwnames)
758{
759 assert(name != NULL);
760 assert(args != NULL);
761 assert(PyVectorcall_NARGS(nargsf) >= 1);
762
763 PyObject *callable = NULL;
764 /* Use args[0] as "self" argument */
765 int unbound = _PyObject_GetMethod(args[0], name, &callable);
766 if (callable == NULL) {
767 return NULL;
768 }
769
770 if (unbound) {
771 /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
772 * that would be interpreted as allowing to change args[-1] */
773 nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
774 }
775 else {
776 /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
777 * args[-1] in the onward call is args[0] here. */
778 args++;
779 nargsf--;
780 }
781 PyObject *result = _PyObject_Vectorcall(callable, args, nargsf, kwnames);
782 Py_DECREF(callable);
783 return result;
784}
785
786
787PyObject *
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700788PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
789{
790 if (obj == NULL || name == NULL) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100791 return null_error();
792 }
793
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700794 PyObject *callable = NULL;
795 int is_method = _PyObject_GetMethod(obj, name, &callable);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100796 if (callable == NULL) {
797 return NULL;
798 }
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700799 obj = is_method ? obj : NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100800
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700801 va_list vargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100802 va_start(vargs, name);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700803 PyObject *result = object_vacall(obj, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100804 va_end(vargs);
805
806 Py_DECREF(callable);
807 return result;
808}
809
810
811PyObject *
812_PyObject_CallMethodIdObjArgs(PyObject *obj,
813 struct _Py_Identifier *name, ...)
814{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100815 if (obj == NULL || name == NULL) {
816 return null_error();
817 }
818
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700819 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
820 if (!oname) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100821 return NULL;
822 }
823
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700824 PyObject *callable = NULL;
825 int is_method = _PyObject_GetMethod(obj, oname, &callable);
826 if (callable == NULL) {
827 return NULL;
828 }
829 obj = is_method ? obj : NULL;
830
831 va_list vargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100832 va_start(vargs, name);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700833 PyObject *result = object_vacall(obj, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100834 va_end(vargs);
835
836 Py_DECREF(callable);
837 return result;
838}
839
840
841PyObject *
842PyObject_CallFunctionObjArgs(PyObject *callable, ...)
843{
844 va_list vargs;
845 PyObject *result;
846
847 va_start(vargs, callable);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -0700848 result = object_vacall(NULL, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100849 va_end(vargs);
850
851 return result;
852}
853
854
855/* --- PyStack functions ------------------------------------------ */
856
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100857PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200858_PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100859{
860 Py_ssize_t nkwargs;
861 PyObject *kwdict;
862 Py_ssize_t i;
863
864 assert(kwnames != NULL);
865 nkwargs = PyTuple_GET_SIZE(kwnames);
866 kwdict = _PyDict_NewPresized(nkwargs);
867 if (kwdict == NULL) {
868 return NULL;
869 }
870
871 for (i = 0; i < nkwargs; i++) {
872 PyObject *key = PyTuple_GET_ITEM(kwnames, i);
873 PyObject *value = *values++;
874 /* If key already exists, replace it with the new value */
875 if (PyDict_SetItem(kwdict, key, value)) {
876 Py_DECREF(kwdict);
877 return NULL;
878 }
879 }
880 return kwdict;
881}
882
883
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200884/* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
885
886 Allocate a new argument vector and keyword names tuple. Return the argument
887 vector; return NULL with exception set on error. Return the keyword names
888 tuple in *p_kwnames.
889
Jeroen Demeyer05677862019-08-16 12:41:27 +0200890 This also checks that all keyword names are strings. If not, a TypeError is
891 raised.
892
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200893 The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
894
Jeroen Demeyer05677862019-08-16 12:41:27 +0200895 When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames) */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200896static PyObject *const *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200897_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200898 PyObject **p_kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100899{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100900 assert(nargs >= 0);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200901 assert(kwargs != NULL);
902 assert(PyDict_Check(kwargs));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100903
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200904 Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs);
905 /* Check for overflow in the PyMem_Malloc() call below. The subtraction
906 * in this check cannot overflow: both maxnargs and nkwargs are
907 * non-negative signed integers, so their difference fits in the type. */
908 Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
909 if (nargs > maxnargs - nkwargs) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100910 PyErr_NoMemory();
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200911 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100912 }
913
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200914 /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
915 PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100916 if (stack == NULL) {
917 PyErr_NoMemory();
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200918 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100919 }
920
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200921 PyObject *kwnames = PyTuple_New(nkwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100922 if (kwnames == NULL) {
923 PyMem_Free(stack);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200924 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100925 }
926
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200927 stack++; /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
928
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200929 /* Copy positional arguments */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200930 for (Py_ssize_t i = 0; i < nargs; i++) {
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200931 Py_INCREF(args[i]);
932 stack[i] = args[i];
933 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100934
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200935 PyObject **kwstack = stack + nargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100936 /* This loop doesn't support lookup function mutating the dictionary
937 to change its size. It's a deliberate choice for speed, this function is
938 called in the performance critical hot code. */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200939 Py_ssize_t pos = 0, i = 0;
940 PyObject *key, *value;
Jeroen Demeyer05677862019-08-16 12:41:27 +0200941 unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100942 while (PyDict_Next(kwargs, &pos, &key, &value)) {
Jeroen Demeyer05677862019-08-16 12:41:27 +0200943 keys_are_strings &= Py_TYPE(key)->tp_flags;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100944 Py_INCREF(key);
Jeroen Demeyer77aa3962019-05-22 13:09:35 +0200945 Py_INCREF(value);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100946 PyTuple_SET_ITEM(kwnames, i, key);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100947 kwstack[i] = value;
948 i++;
949 }
950
Jeroen Demeyer05677862019-08-16 12:41:27 +0200951 /* keys_are_strings has the value Py_TPFLAGS_UNICODE_SUBCLASS if that
952 * flag is set for all keys. Otherwise, keys_are_strings equals 0.
953 * We do this check once at the end instead of inside the loop above
954 * because it simplifies the deallocation in the failing case.
955 * It happens to also make the loop above slightly more efficient. */
956 if (!keys_are_strings) {
957 PyErr_SetString(PyExc_TypeError,
958 "keywords must be strings");
959 _PyStack_UnpackDict_Free(stack, nargs, kwnames);
960 return NULL;
961 }
962
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100963 *p_kwnames = kwnames;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200964 return stack;
965}
966
967static void
968_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
969 PyObject *kwnames)
970{
971 Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
972 for (Py_ssize_t i = 0; i < n; i++) {
973 Py_DECREF(stack[i]);
974 }
975 PyMem_Free((PyObject **)stack - 1);
976 Py_DECREF(kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100977}