blob: 8e0d271ab78a2c60aefbab53cf07b278a2947849 [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 Demeyeraacc77f2019-05-29 20:31:52 +0200219 return result;
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
369PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200370_PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self,
371 PyObject *const *args, Py_ssize_t nargs,
372 PyObject *kwargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100373{
374 /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
375 because it can clear it (directly or indirectly) and so the
376 caller loses its exception */
377 assert(!PyErr_Occurred());
378
379 assert(method != NULL);
380 assert(nargs >= 0);
381 assert(nargs == 0 || args != NULL);
382 assert(kwargs == NULL || PyDict_Check(kwargs));
383
384 PyCFunction meth = method->ml_meth;
385 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
386 PyObject *result = NULL;
387
388 if (Py_EnterRecursiveCall(" while calling a Python object")) {
389 return NULL;
390 }
391
392 switch (flags)
393 {
394 case METH_NOARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300395 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
396 goto no_keyword_error;
397 }
398
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100399 if (nargs != 0) {
400 PyErr_Format(PyExc_TypeError,
401 "%.200s() takes no arguments (%zd given)",
402 method->ml_name, nargs);
403 goto exit;
404 }
405
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100406 result = (*meth) (self, NULL);
407 break;
408
409 case METH_O:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300410 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
411 goto no_keyword_error;
412 }
413
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100414 if (nargs != 1) {
415 PyErr_Format(PyExc_TypeError,
416 "%.200s() takes exactly one argument (%zd given)",
417 method->ml_name, nargs);
418 goto exit;
419 }
420
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100421 result = (*meth) (self, args[0]);
422 break;
423
424 case METH_VARARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300425 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100426 goto no_keyword_error;
427 }
Stefan Krahf432a322017-08-21 13:09:59 +0200428 /* fall through */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100429
430 case METH_VARARGS | METH_KEYWORDS:
431 {
432 /* Slow-path: create a temporary tuple for positional arguments */
Sergey Fedoseevf1b9abe2019-02-26 02:37:26 +0500433 PyObject *argstuple = _PyTuple_FromArray(args, nargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100434 if (argstuple == NULL) {
435 goto exit;
436 }
437
438 if (flags & METH_KEYWORDS) {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200439 result = (*(PyCFunctionWithKeywords)(void(*)(void))meth) (self, argstuple, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100440 }
441 else {
442 result = (*meth) (self, argstuple);
443 }
444 Py_DECREF(argstuple);
445 break;
446 }
447
448 case METH_FASTCALL:
449 {
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300450 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
451 goto no_keyword_error;
452 }
453
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200454 result = (*(_PyCFunctionFast)(void(*)(void))meth) (self, args, nargs);
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300455 break;
456 }
457
458 case METH_FASTCALL | METH_KEYWORDS:
459 {
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200460 _PyCFunctionFastWithKeywords fastmeth = (_PyCFunctionFastWithKeywords)(void(*)(void))meth;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100461
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200462 /* Fast path for no keywords */
463 if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
464 result = (*fastmeth) (self, args, nargs, NULL);
465 break;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100466 }
467
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200468 PyObject *const *stack;
469 PyObject *kwnames;
470 stack = _PyStack_UnpackDict(args, nargs, kwargs, &kwnames);
471 if (stack == NULL) {
472 goto exit;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100473 }
Jeroen Demeyerd4efd912019-07-02 11:49:40 +0200474 result = (*fastmeth) (self, stack, nargs, kwnames);
475 _PyStack_UnpackDict_Free(stack, nargs, kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100476 break;
477 }
478
479 default:
480 PyErr_SetString(PyExc_SystemError,
481 "Bad call flags in _PyMethodDef_RawFastCallDict. "
482 "METH_OLDARGS is no longer supported!");
483 goto exit;
484 }
485
486 goto exit;
487
488no_keyword_error:
489 PyErr_Format(PyExc_TypeError,
490 "%.200s() takes no keyword arguments",
Sylvaind67a1032017-03-27 23:36:08 +0200491 method->ml_name);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100492
493exit:
494 Py_LeaveRecursiveCall();
495 return result;
496}
497
498
499PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200500_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self,
501 PyObject *const *args, Py_ssize_t nargs,
502 PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100503{
504 /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
505 because it can clear it (directly or indirectly) and so the
506 caller loses its exception */
507 assert(!PyErr_Occurred());
508
509 assert(method != NULL);
510 assert(nargs >= 0);
511 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
512 /* kwnames must only contains str strings, no subclass, and all keys must
513 be unique */
514
515 PyCFunction meth = method->ml_meth;
516 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300517 Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100518 PyObject *result = NULL;
519
520 if (Py_EnterRecursiveCall(" while calling a Python object")) {
521 return NULL;
522 }
523
524 switch (flags)
525 {
526 case METH_NOARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300527 if (nkwargs) {
528 goto no_keyword_error;
529 }
530
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100531 if (nargs != 0) {
532 PyErr_Format(PyExc_TypeError,
533 "%.200s() takes no arguments (%zd given)",
534 method->ml_name, nargs);
535 goto exit;
536 }
537
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100538 result = (*meth) (self, NULL);
539 break;
540
541 case METH_O:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300542 if (nkwargs) {
543 goto no_keyword_error;
544 }
545
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100546 if (nargs != 1) {
547 PyErr_Format(PyExc_TypeError,
548 "%.200s() takes exactly one argument (%zd given)",
549 method->ml_name, nargs);
550 goto exit;
551 }
552
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100553 result = (*meth) (self, args[0]);
554 break;
555
556 case METH_FASTCALL:
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300557 if (nkwargs) {
558 goto no_keyword_error;
559 }
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200560 result = ((_PyCFunctionFast)(void(*)(void))meth) (self, args, nargs);
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300561 break;
562
563 case METH_FASTCALL | METH_KEYWORDS:
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100564 /* Fast-path: avoid temporary dict to pass keyword arguments */
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200565 result = ((_PyCFunctionFastWithKeywords)(void(*)(void))meth) (self, args, nargs, kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100566 break;
567
568 case METH_VARARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300569 if (nkwargs) {
570 goto no_keyword_error;
571 }
Stefan Krahf432a322017-08-21 13:09:59 +0200572 /* fall through */
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300573
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100574 case METH_VARARGS | METH_KEYWORDS:
575 {
576 /* Slow-path: create a temporary tuple for positional arguments
577 and a temporary dict for keyword arguments */
578 PyObject *argtuple;
579
Sergey Fedoseevf1b9abe2019-02-26 02:37:26 +0500580 argtuple = _PyTuple_FromArray(args, nargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100581 if (argtuple == NULL) {
582 goto exit;
583 }
584
585 if (flags & METH_KEYWORDS) {
586 PyObject *kwdict;
587
588 if (nkwargs > 0) {
589 kwdict = _PyStack_AsDict(args + nargs, kwnames);
590 if (kwdict == NULL) {
591 Py_DECREF(argtuple);
592 goto exit;
593 }
594 }
595 else {
596 kwdict = NULL;
597 }
598
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200599 result = (*(PyCFunctionWithKeywords)(void(*)(void))meth) (self, argtuple, kwdict);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100600 Py_XDECREF(kwdict);
601 }
602 else {
603 result = (*meth) (self, argtuple);
604 }
605 Py_DECREF(argtuple);
606 break;
607 }
608
609 default:
610 PyErr_SetString(PyExc_SystemError,
Jeroen Demeyera8b46942019-05-17 12:21:35 +0200611 "Bad call flags in _PyMethodDef_RawFastCallKeywords. "
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100612 "METH_OLDARGS is no longer supported!");
613 goto exit;
614 }
615
616 goto exit;
617
618no_keyword_error:
619 PyErr_Format(PyExc_TypeError,
620 "%.200s() takes no keyword arguments",
621 method->ml_name);
622
623exit:
624 Py_LeaveRecursiveCall();
625 return result;
626}
627
628
629PyObject *
Jeroen Demeyer37788bc2019-05-30 15:11:22 +0200630_PyCFunction_Vectorcall(PyObject *func,
631 PyObject *const *args, size_t nargsf,
632 PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100633{
634 PyObject *result;
635
636 assert(func != NULL);
637 assert(PyCFunction_Check(func));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200638 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100639
640 result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml,
641 PyCFunction_GET_SELF(func),
642 args, nargs, kwnames);
643 result = _Py_CheckFunctionResult(func, result, NULL);
644 return result;
645}
646
647
648static PyObject *
649cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs)
650{
651 assert(!PyErr_Occurred());
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300652 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100653
654 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
655 PyObject *self = PyCFunction_GET_SELF(func);
656 PyObject *result;
657
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200658 assert(PyCFunction_GET_FLAGS(func) & METH_VARARGS);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100659 if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) {
660 if (Py_EnterRecursiveCall(" while calling a Python object")) {
661 return NULL;
662 }
663
Serhiy Storchaka62be7422018-11-27 13:27:31 +0200664 result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100665
666 Py_LeaveRecursiveCall();
667 }
668 else {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300669 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100670 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
671 ((PyCFunctionObject*)func)->m_ml->ml_name);
672 return NULL;
673 }
674
675 if (Py_EnterRecursiveCall(" while calling a Python object")) {
676 return NULL;
677 }
678
679 result = (*meth)(self, args);
680
681 Py_LeaveRecursiveCall();
682 }
683
684 return _Py_CheckFunctionResult(func, result, NULL);
685}
686
687
688PyObject *
689PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
690{
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200691 /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
692 * is NULL. This is intentional, since vectorcall would be slower. */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100693 if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) {
694 return cfunction_call_varargs(func, args, kwargs);
695 }
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200696 return PyVectorcall_Call(func, args, kwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100697}
698
699
700/* --- More complex call functions -------------------------------- */
701
702/* External interface to call any callable object.
703 The args must be a tuple or NULL. The kwargs must be a dict or NULL. */
704PyObject *
705PyEval_CallObjectWithKeywords(PyObject *callable,
706 PyObject *args, PyObject *kwargs)
707{
708#ifdef Py_DEBUG
709 /* PyEval_CallObjectWithKeywords() must not be called with an exception
710 set. It raises a new exception if parameters are invalid or if
711 PyTuple_New() fails, and so the original exception is lost. */
712 assert(!PyErr_Occurred());
713#endif
714
INADA Naoki3824cd82017-03-01 20:41:03 +0900715 if (args != NULL && !PyTuple_Check(args)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100716 PyErr_SetString(PyExc_TypeError,
717 "argument list must be a tuple");
718 return NULL;
719 }
720
721 if (kwargs != NULL && !PyDict_Check(kwargs)) {
722 PyErr_SetString(PyExc_TypeError,
723 "keyword list must be a dictionary");
724 return NULL;
725 }
726
INADA Naoki3824cd82017-03-01 20:41:03 +0900727 if (args == NULL) {
728 return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
729 }
730 else {
731 return PyObject_Call(callable, args, kwargs);
732 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100733}
734
735
736PyObject *
737PyObject_CallObject(PyObject *callable, PyObject *args)
738{
739 return PyEval_CallObjectWithKeywords(callable, args, NULL);
740}
741
742
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100743/* Call callable(obj, *args, **kwargs). */
744PyObject *
745_PyObject_Call_Prepend(PyObject *callable,
746 PyObject *obj, PyObject *args, PyObject *kwargs)
747{
748 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
749 PyObject **stack;
750 Py_ssize_t argcount;
751 PyObject *result;
752
753 assert(PyTuple_Check(args));
754
755 argcount = PyTuple_GET_SIZE(args);
756 if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
757 stack = small_stack;
758 }
759 else {
760 stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
761 if (stack == NULL) {
762 PyErr_NoMemory();
763 return NULL;
764 }
765 }
766
767 /* use borrowed references */
768 stack[0] = obj;
769 memcpy(&stack[1],
Victor Stinnerd17a6932018-11-09 16:56:48 +0100770 _PyTuple_ITEMS(args),
771 argcount * sizeof(PyObject *));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100772
773 result = _PyObject_FastCallDict(callable,
774 stack, argcount + 1,
775 kwargs);
776 if (stack != small_stack) {
777 PyMem_Free(stack);
778 }
779 return result;
780}
781
782
783/* --- Call with a format string ---------------------------------- */
784
785static PyObject *
786_PyObject_CallFunctionVa(PyObject *callable, const char *format,
787 va_list va, int is_size_t)
788{
789 PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
790 const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
791 PyObject **stack;
792 Py_ssize_t nargs, i;
793 PyObject *result;
794
795 if (callable == NULL) {
796 return null_error();
797 }
798
799 if (!format || !*format) {
800 return _PyObject_CallNoArg(callable);
801 }
802
803 if (is_size_t) {
804 stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
805 format, va, &nargs);
806 }
807 else {
808 stack = _Py_VaBuildStack(small_stack, small_stack_len,
809 format, va, &nargs);
810 }
811 if (stack == NULL) {
812 return NULL;
813 }
814
815 if (nargs == 1 && PyTuple_Check(stack[0])) {
816 /* Special cases for backward compatibility:
817 - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
818 - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
819 func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
820 PyObject *args = stack[0];
821 result = _PyObject_FastCall(callable,
Victor Stinnerd17a6932018-11-09 16:56:48 +0100822 _PyTuple_ITEMS(args),
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100823 PyTuple_GET_SIZE(args));
824 }
825 else {
826 result = _PyObject_FastCall(callable, stack, nargs);
827 }
828
829 for (i = 0; i < nargs; ++i) {
830 Py_DECREF(stack[i]);
831 }
832 if (stack != small_stack) {
833 PyMem_Free(stack);
834 }
835 return result;
836}
837
838
839PyObject *
840PyObject_CallFunction(PyObject *callable, const char *format, ...)
841{
842 va_list va;
843 PyObject *result;
844
845 va_start(va, format);
846 result = _PyObject_CallFunctionVa(callable, format, va, 0);
847 va_end(va);
848
849 return result;
850}
851
852
INADA Naokiaa289a52017-03-14 18:00:59 +0900853/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
854 * This function is kept for backward compatibility.
855 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100856PyObject *
857PyEval_CallFunction(PyObject *callable, const char *format, ...)
858{
INADA Naokiaa289a52017-03-14 18:00:59 +0900859 va_list va;
860 PyObject *result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100861
INADA Naokiaa289a52017-03-14 18:00:59 +0900862 va_start(va, format);
863 result = _PyObject_CallFunctionVa(callable, format, va, 0);
864 va_end(va);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100865
INADA Naokiaa289a52017-03-14 18:00:59 +0900866 return result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100867}
868
869
870PyObject *
871_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
872{
873 va_list va;
874 PyObject *result;
875
876 va_start(va, format);
877 result = _PyObject_CallFunctionVa(callable, format, va, 1);
878 va_end(va);
879
880 return result;
881}
882
883
884static PyObject*
885callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
886{
887 assert(callable != NULL);
888
889 if (!PyCallable_Check(callable)) {
890 PyErr_Format(PyExc_TypeError,
891 "attribute of type '%.200s' is not callable",
892 Py_TYPE(callable)->tp_name);
893 return NULL;
894 }
895
896 return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
897}
898
899
900PyObject *
901PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
902{
903 va_list va;
904 PyObject *callable, *retval;
905
906 if (obj == NULL || name == NULL) {
907 return null_error();
908 }
909
910 callable = PyObject_GetAttrString(obj, name);
911 if (callable == NULL)
912 return NULL;
913
914 va_start(va, format);
915 retval = callmethod(callable, format, va, 0);
916 va_end(va);
917
918 Py_DECREF(callable);
919 return retval;
920}
921
922
INADA Naokiaa289a52017-03-14 18:00:59 +0900923/* PyEval_CallMethod is exact copy of PyObject_CallMethod.
924 * This function is kept for backward compatibility.
925 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100926PyObject *
927PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
928{
INADA Naokiaa289a52017-03-14 18:00:59 +0900929 va_list va;
930 PyObject *callable, *retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100931
INADA Naokiaa289a52017-03-14 18:00:59 +0900932 if (obj == NULL || name == NULL) {
933 return null_error();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100934 }
935
INADA Naokiaa289a52017-03-14 18:00:59 +0900936 callable = PyObject_GetAttrString(obj, name);
937 if (callable == NULL)
938 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100939
INADA Naokiaa289a52017-03-14 18:00:59 +0900940 va_start(va, format);
941 retval = callmethod(callable, format, va, 0);
942 va_end(va);
943
944 Py_DECREF(callable);
945 return retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100946}
947
948
949PyObject *
950_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
951 const char *format, ...)
952{
953 va_list va;
954 PyObject *callable, *retval;
955
956 if (obj == NULL || name == NULL) {
957 return null_error();
958 }
959
960 callable = _PyObject_GetAttrId(obj, name);
961 if (callable == NULL)
962 return NULL;
963
964 va_start(va, format);
965 retval = callmethod(callable, format, va, 0);
966 va_end(va);
967
968 Py_DECREF(callable);
969 return retval;
970}
971
972
973PyObject *
974_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
975 const char *format, ...)
976{
977 va_list va;
978 PyObject *callable, *retval;
979
980 if (obj == NULL || name == NULL) {
981 return null_error();
982 }
983
984 callable = PyObject_GetAttrString(obj, name);
985 if (callable == NULL)
986 return NULL;
987
988 va_start(va, format);
989 retval = callmethod(callable, format, va, 1);
990 va_end(va);
991
992 Py_DECREF(callable);
993 return retval;
994}
995
996
997PyObject *
998_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
999 const char *format, ...)
1000{
1001 va_list va;
1002 PyObject *callable, *retval;
1003
1004 if (obj == NULL || name == NULL) {
1005 return null_error();
1006 }
1007
1008 callable = _PyObject_GetAttrId(obj, name);
1009 if (callable == NULL) {
1010 return NULL;
1011 }
1012
1013 va_start(va, format);
1014 retval = callmethod(callable, format, va, 1);
1015 va_end(va);
1016
1017 Py_DECREF(callable);
1018 return retval;
1019}
1020
1021
1022/* --- Call with "..." arguments ---------------------------------- */
1023
1024static PyObject *
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001025object_vacall(PyObject *base, PyObject *callable, va_list vargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001026{
1027 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
1028 PyObject **stack;
1029 Py_ssize_t nargs;
1030 PyObject *result;
1031 Py_ssize_t i;
1032 va_list countva;
1033
1034 if (callable == NULL) {
1035 return null_error();
1036 }
1037
1038 /* Count the number of arguments */
1039 va_copy(countva, vargs);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001040 nargs = base ? 1 : 0;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001041 while (1) {
1042 PyObject *arg = va_arg(countva, PyObject *);
1043 if (arg == NULL) {
1044 break;
1045 }
1046 nargs++;
1047 }
1048 va_end(countva);
1049
1050 /* Copy arguments */
1051 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1052 stack = small_stack;
1053 }
1054 else {
1055 stack = PyMem_Malloc(nargs * sizeof(stack[0]));
1056 if (stack == NULL) {
1057 PyErr_NoMemory();
1058 return NULL;
1059 }
1060 }
1061
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001062 i = 0;
1063 if (base) {
1064 stack[i++] = base;
1065 }
1066
1067 for (; i < nargs; ++i) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001068 stack[i] = va_arg(vargs, PyObject *);
1069 }
1070
1071 /* Call the function */
1072 result = _PyObject_FastCall(callable, stack, nargs);
1073
1074 if (stack != small_stack) {
1075 PyMem_Free(stack);
1076 }
1077 return result;
1078}
1079
1080
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001081PyObject *
Jeroen Demeyerb1263d52019-06-28 11:49:00 +02001082_PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
1083 size_t nargsf, PyObject *kwnames)
1084{
1085 assert(name != NULL);
1086 assert(args != NULL);
1087 assert(PyVectorcall_NARGS(nargsf) >= 1);
1088
1089 PyObject *callable = NULL;
1090 /* Use args[0] as "self" argument */
1091 int unbound = _PyObject_GetMethod(args[0], name, &callable);
1092 if (callable == NULL) {
1093 return NULL;
1094 }
1095
1096 if (unbound) {
1097 /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
1098 * that would be interpreted as allowing to change args[-1] */
1099 nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
1100 }
1101 else {
1102 /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
1103 * args[-1] in the onward call is args[0] here. */
1104 args++;
1105 nargsf--;
1106 }
1107 PyObject *result = _PyObject_Vectorcall(callable, args, nargsf, kwnames);
1108 Py_DECREF(callable);
1109 return result;
1110}
1111
1112
1113PyObject *
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001114PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
1115{
1116 if (obj == NULL || name == NULL) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001117 return null_error();
1118 }
1119
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001120 PyObject *callable = NULL;
1121 int is_method = _PyObject_GetMethod(obj, name, &callable);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001122 if (callable == NULL) {
1123 return NULL;
1124 }
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001125 obj = is_method ? obj : NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001126
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001127 va_list vargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001128 va_start(vargs, name);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001129 PyObject *result = object_vacall(obj, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001130 va_end(vargs);
1131
1132 Py_DECREF(callable);
1133 return result;
1134}
1135
1136
1137PyObject *
1138_PyObject_CallMethodIdObjArgs(PyObject *obj,
1139 struct _Py_Identifier *name, ...)
1140{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001141 if (obj == NULL || name == NULL) {
1142 return null_error();
1143 }
1144
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001145 PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
1146 if (!oname) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001147 return NULL;
1148 }
1149
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001150 PyObject *callable = NULL;
1151 int is_method = _PyObject_GetMethod(obj, oname, &callable);
1152 if (callable == NULL) {
1153 return NULL;
1154 }
1155 obj = is_method ? obj : NULL;
1156
1157 va_list vargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001158 va_start(vargs, name);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001159 PyObject *result = object_vacall(obj, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001160 va_end(vargs);
1161
1162 Py_DECREF(callable);
1163 return result;
1164}
1165
1166
1167PyObject *
1168PyObject_CallFunctionObjArgs(PyObject *callable, ...)
1169{
1170 va_list vargs;
1171 PyObject *result;
1172
1173 va_start(vargs, callable);
Michael J. Sullivan47dd2f92019-05-26 00:23:34 -07001174 result = object_vacall(NULL, callable, vargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001175 va_end(vargs);
1176
1177 return result;
1178}
1179
1180
1181/* --- PyStack functions ------------------------------------------ */
1182
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001183PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001184_PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001185{
1186 Py_ssize_t nkwargs;
1187 PyObject *kwdict;
1188 Py_ssize_t i;
1189
1190 assert(kwnames != NULL);
1191 nkwargs = PyTuple_GET_SIZE(kwnames);
1192 kwdict = _PyDict_NewPresized(nkwargs);
1193 if (kwdict == NULL) {
1194 return NULL;
1195 }
1196
1197 for (i = 0; i < nkwargs; i++) {
1198 PyObject *key = PyTuple_GET_ITEM(kwnames, i);
1199 PyObject *value = *values++;
1200 /* If key already exists, replace it with the new value */
1201 if (PyDict_SetItem(kwdict, key, value)) {
1202 Py_DECREF(kwdict);
1203 return NULL;
1204 }
1205 }
1206 return kwdict;
1207}
1208
1209
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001210/* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
1211
1212 Allocate a new argument vector and keyword names tuple. Return the argument
1213 vector; return NULL with exception set on error. Return the keyword names
1214 tuple in *p_kwnames.
1215
1216 The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
1217
1218 When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames)
1219
1220 The type of keyword keys is not checked, these checks should be done
1221 later (ex: _PyArg_ParseStackAndKeywords). */
1222static PyObject *const *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001223_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001224 PyObject **p_kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001225{
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001226 assert(nargs >= 0);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001227 assert(kwargs != NULL);
1228 assert(PyDict_Check(kwargs));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001229
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001230 Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs);
1231 /* Check for overflow in the PyMem_Malloc() call below. The subtraction
1232 * in this check cannot overflow: both maxnargs and nkwargs are
1233 * non-negative signed integers, so their difference fits in the type. */
1234 Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
1235 if (nargs > maxnargs - nkwargs) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001236 PyErr_NoMemory();
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001237 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001238 }
1239
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001240 /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
1241 PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001242 if (stack == NULL) {
1243 PyErr_NoMemory();
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001244 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001245 }
1246
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001247 PyObject *kwnames = PyTuple_New(nkwargs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001248 if (kwnames == NULL) {
1249 PyMem_Free(stack);
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001250 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001251 }
1252
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001253 stack++; /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
1254
Jeroen Demeyer77aa3962019-05-22 13:09:35 +02001255 /* Copy positional arguments */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001256 for (Py_ssize_t i = 0; i < nargs; i++) {
Jeroen Demeyer77aa3962019-05-22 13:09:35 +02001257 Py_INCREF(args[i]);
1258 stack[i] = args[i];
1259 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001260
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001261 PyObject **kwstack = stack + nargs;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001262 /* This loop doesn't support lookup function mutating the dictionary
1263 to change its size. It's a deliberate choice for speed, this function is
1264 called in the performance critical hot code. */
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001265 Py_ssize_t pos = 0, i = 0;
1266 PyObject *key, *value;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001267 while (PyDict_Next(kwargs, &pos, &key, &value)) {
1268 Py_INCREF(key);
Jeroen Demeyer77aa3962019-05-22 13:09:35 +02001269 Py_INCREF(value);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001270 PyTuple_SET_ITEM(kwnames, i, key);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001271 kwstack[i] = value;
1272 i++;
1273 }
1274
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001275 *p_kwnames = kwnames;
Jeroen Demeyerd4efd912019-07-02 11:49:40 +02001276 return stack;
1277}
1278
1279static void
1280_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
1281 PyObject *kwnames)
1282{
1283 Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
1284 for (Py_ssize_t i = 0; i < n; i++) {
1285 Py_DECREF(stack[i]);
1286 }
1287 PyMem_Free((PyObject **)stack - 1);
1288 Py_DECREF(kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001289}