blob: 6c8a640cc59f21d0ec396e2f978af00d724a1b4d [file] [log] [blame]
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001#include "Python.h"
2#include "frameobject.h"
3
4
Victor Stinner0f7b0b32017-03-14 21:37:20 +01005int
6_PyObject_HasFastCall(PyObject *callable)
7{
8 if (PyFunction_Check(callable)) {
9 return 1;
10 }
11 else if (PyCFunction_Check(callable)) {
12 return !(PyCFunction_GET_FLAGS(callable) & METH_VARARGS);
13 }
14 else {
15 assert (PyCallable_Check(callable));
16 return 0;
17 }
18}
19
20
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010021static PyObject *
22null_error(void)
23{
24 if (!PyErr_Occurred())
25 PyErr_SetString(PyExc_SystemError,
26 "null argument to internal routine");
27 return NULL;
28}
29
30
31PyObject*
32_Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where)
33{
34 int err_occurred = (PyErr_Occurred() != NULL);
35
36 assert((callable != NULL) ^ (where != NULL));
37
38 if (result == NULL) {
39 if (!err_occurred) {
40 if (callable)
41 PyErr_Format(PyExc_SystemError,
42 "%R returned NULL without setting an error",
43 callable);
44 else
45 PyErr_Format(PyExc_SystemError,
46 "%s returned NULL without setting an error",
47 where);
48#ifdef Py_DEBUG
49 /* Ensure that the bug is caught in debug mode */
50 Py_FatalError("a function returned NULL without setting an error");
51#endif
52 return NULL;
53 }
54 }
55 else {
56 if (err_occurred) {
57 Py_DECREF(result);
58
59 if (callable) {
60 _PyErr_FormatFromCause(PyExc_SystemError,
61 "%R returned a result with an error set",
62 callable);
63 }
64 else {
65 _PyErr_FormatFromCause(PyExc_SystemError,
66 "%s returned a result with an error set",
67 where);
68 }
69#ifdef Py_DEBUG
70 /* Ensure that the bug is caught in debug mode */
71 Py_FatalError("a function returned a result with an error set");
72#endif
73 return NULL;
74 }
75 }
76 return result;
77}
78
79
80/* --- Core PyObject call functions ------------------------------- */
81
82PyObject *
83_PyObject_FastCallDict(PyObject *callable, PyObject **args, Py_ssize_t nargs,
84 PyObject *kwargs)
85{
86 /* _PyObject_FastCallDict() must not be called with an exception set,
87 because it can clear it (directly or indirectly) and so the
88 caller loses its exception */
89 assert(!PyErr_Occurred());
90
91 assert(callable != NULL);
92 assert(nargs >= 0);
93 assert(nargs == 0 || args != NULL);
94 assert(kwargs == NULL || PyDict_Check(kwargs));
95
96 if (PyFunction_Check(callable)) {
97 return _PyFunction_FastCallDict(callable, args, nargs, kwargs);
98 }
99 else if (PyCFunction_Check(callable)) {
100 return _PyCFunction_FastCallDict(callable, args, nargs, kwargs);
101 }
102 else {
103 PyObject *argstuple, *result;
104 ternaryfunc call;
105
106 /* Slow-path: build a temporary tuple */
107 call = callable->ob_type->tp_call;
108 if (call == NULL) {
109 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
110 callable->ob_type->tp_name);
111 return NULL;
112 }
113
114 argstuple = _PyStack_AsTuple(args, nargs);
115 if (argstuple == NULL) {
116 return NULL;
117 }
118
119 if (Py_EnterRecursiveCall(" while calling a Python object")) {
120 Py_DECREF(argstuple);
121 return NULL;
122 }
123
124 result = (*call)(callable, argstuple, kwargs);
125
126 Py_LeaveRecursiveCall();
127 Py_DECREF(argstuple);
128
129 result = _Py_CheckFunctionResult(callable, result, NULL);
130 return result;
131 }
132}
133
134
135PyObject *
136_PyObject_FastCallKeywords(PyObject *callable, PyObject **stack, Py_ssize_t nargs,
137 PyObject *kwnames)
138{
139 /* _PyObject_FastCallKeywords() must not be called with an exception set,
140 because it can clear it (directly or indirectly) and so the
141 caller loses its exception */
142 assert(!PyErr_Occurred());
143
144 assert(nargs >= 0);
145 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
146
147 /* kwnames must only contains str strings, no subclass, and all keys must
148 be unique: these checks are implemented in Python/ceval.c and
149 _PyArg_ParseStackAndKeywords(). */
150
151 if (PyFunction_Check(callable)) {
152 return _PyFunction_FastCallKeywords(callable, stack, nargs, kwnames);
153 }
154 if (PyCFunction_Check(callable)) {
155 return _PyCFunction_FastCallKeywords(callable, stack, nargs, kwnames);
156 }
157 else {
158 /* Slow-path: build a temporary tuple for positional arguments and a
159 temporary dictionary for keyword arguments (if any) */
160
161 ternaryfunc call;
162 PyObject *argstuple;
163 PyObject *kwdict, *result;
164 Py_ssize_t nkwargs;
165
166 nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
167 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
168
169 call = callable->ob_type->tp_call;
170 if (call == NULL) {
171 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
172 callable->ob_type->tp_name);
173 return NULL;
174 }
175
176 argstuple = _PyStack_AsTuple(stack, nargs);
177 if (argstuple == NULL) {
178 return NULL;
179 }
180
181 if (nkwargs > 0) {
182 kwdict = _PyStack_AsDict(stack + nargs, kwnames);
183 if (kwdict == NULL) {
184 Py_DECREF(argstuple);
185 return NULL;
186 }
187 }
188 else {
189 kwdict = NULL;
190 }
191
192 if (Py_EnterRecursiveCall(" while calling a Python object")) {
193 Py_DECREF(argstuple);
194 Py_XDECREF(kwdict);
195 return NULL;
196 }
197
198 result = (*call)(callable, argstuple, kwdict);
199
200 Py_LeaveRecursiveCall();
201
202 Py_DECREF(argstuple);
203 Py_XDECREF(kwdict);
204
205 result = _Py_CheckFunctionResult(callable, result, NULL);
206 return result;
207 }
208}
209
210
211PyObject *
212PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
213{
214 ternaryfunc call;
215 PyObject *result;
216
217 /* PyObject_Call() must not be called with an exception set,
218 because it can clear it (directly or indirectly) and so the
219 caller loses its exception */
220 assert(!PyErr_Occurred());
221 assert(PyTuple_Check(args));
222 assert(kwargs == NULL || PyDict_Check(kwargs));
223
224 if (PyFunction_Check(callable)) {
225 return _PyFunction_FastCallDict(callable,
226 &PyTuple_GET_ITEM(args, 0),
227 PyTuple_GET_SIZE(args),
228 kwargs);
229 }
230 else if (PyCFunction_Check(callable)) {
231 return PyCFunction_Call(callable, args, kwargs);
232 }
233 else {
234 call = callable->ob_type->tp_call;
235 if (call == NULL) {
236 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
237 callable->ob_type->tp_name);
238 return NULL;
239 }
240
241 if (Py_EnterRecursiveCall(" while calling a Python object"))
242 return NULL;
243
244 result = (*call)(callable, args, kwargs);
245
246 Py_LeaveRecursiveCall();
247
248 return _Py_CheckFunctionResult(callable, result, NULL);
249 }
250}
251
252
253/* --- PyFunction call functions ---------------------------------- */
254
255static PyObject* _Py_HOT_FUNCTION
256function_code_fastcall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
257 PyObject *globals)
258{
259 PyFrameObject *f;
260 PyThreadState *tstate = PyThreadState_GET();
261 PyObject **fastlocals;
262 Py_ssize_t i;
263 PyObject *result;
264
265 assert(globals != NULL);
266 /* XXX Perhaps we should create a specialized
267 _PyFrame_New_NoTrack() that doesn't take locals, but does
268 take builtins without sanity checking them.
269 */
270 assert(tstate != NULL);
271 f = _PyFrame_New_NoTrack(tstate, co, globals, NULL);
272 if (f == NULL) {
273 return NULL;
274 }
275
276 fastlocals = f->f_localsplus;
277
278 for (i = 0; i < nargs; i++) {
279 Py_INCREF(*args);
280 fastlocals[i] = *args++;
281 }
282 result = PyEval_EvalFrameEx(f,0);
283
284 if (Py_REFCNT(f) > 1) {
285 Py_DECREF(f);
286 _PyObject_GC_TRACK(f);
287 }
288 else {
289 ++tstate->recursion_depth;
290 Py_DECREF(f);
291 --tstate->recursion_depth;
292 }
293 return result;
294}
295
296
297PyObject *
298_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
299 PyObject *kwargs)
300{
301 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
302 PyObject *globals = PyFunction_GET_GLOBALS(func);
303 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
304 PyObject *kwdefs, *closure, *name, *qualname;
305 PyObject *kwtuple, **k;
306 PyObject **d;
307 Py_ssize_t nd, nk;
308 PyObject *result;
309
310 assert(func != NULL);
311 assert(nargs >= 0);
312 assert(nargs == 0 || args != NULL);
313 assert(kwargs == NULL || PyDict_Check(kwargs));
314
315 if (co->co_kwonlyargcount == 0 &&
316 (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) &&
317 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
318 {
319 /* Fast paths */
320 if (argdefs == NULL && co->co_argcount == nargs) {
321 return function_code_fastcall(co, args, nargs, globals);
322 }
323 else if (nargs == 0 && argdefs != NULL
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200324 && co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100325 /* function called with no arguments, but all parameters have
326 a default value: use default values as arguments .*/
327 args = &PyTuple_GET_ITEM(argdefs, 0);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200328 return function_code_fastcall(co, args, PyTuple_GET_SIZE(argdefs),
329 globals);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100330 }
331 }
332
333 nk = (kwargs != NULL) ? PyDict_GET_SIZE(kwargs) : 0;
334 if (nk != 0) {
335 Py_ssize_t pos, i;
336
Victor Stinner561ca802017-02-23 18:26:43 +0100337 /* bpo-29318, bpo-27840: Caller and callee functions must not share
338 the dictionary: kwargs must be copied. */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100339 kwtuple = PyTuple_New(2 * nk);
340 if (kwtuple == NULL) {
341 return NULL;
342 }
343
344 k = &PyTuple_GET_ITEM(kwtuple, 0);
345 pos = i = 0;
346 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
347 /* We must hold strong references because keyword arguments can be
348 indirectly modified while the function is called:
349 see issue #2016 and test_extcall */
350 Py_INCREF(k[i]);
351 Py_INCREF(k[i+1]);
352 i += 2;
353 }
354 nk = i / 2;
355 }
356 else {
357 kwtuple = NULL;
358 k = NULL;
359 }
360
361 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
362 closure = PyFunction_GET_CLOSURE(func);
363 name = ((PyFunctionObject *)func) -> func_name;
364 qualname = ((PyFunctionObject *)func) -> func_qualname;
365
366 if (argdefs != NULL) {
367 d = &PyTuple_GET_ITEM(argdefs, 0);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200368 nd = PyTuple_GET_SIZE(argdefs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100369 }
370 else {
371 d = NULL;
372 nd = 0;
373 }
374
375 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
376 args, nargs,
377 k, k + 1, nk, 2,
378 d, nd, kwdefs,
379 closure, name, qualname);
380 Py_XDECREF(kwtuple);
381 return result;
382}
383
384PyObject *
385_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
386 Py_ssize_t nargs, PyObject *kwnames)
387{
388 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
389 PyObject *globals = PyFunction_GET_GLOBALS(func);
390 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
391 PyObject *kwdefs, *closure, *name, *qualname;
392 PyObject **d;
393 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
394 Py_ssize_t nd;
395
396 assert(PyFunction_Check(func));
397 assert(nargs >= 0);
398 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
399 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
400 /* kwnames must only contains str strings, no subclass, and all keys must
401 be unique */
402
403 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
404 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
405 {
406 if (argdefs == NULL && co->co_argcount == nargs) {
407 return function_code_fastcall(co, stack, nargs, globals);
408 }
409 else if (nargs == 0 && argdefs != NULL
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200410 && co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100411 /* function called with no arguments, but all parameters have
412 a default value: use default values as arguments .*/
413 stack = &PyTuple_GET_ITEM(argdefs, 0);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200414 return function_code_fastcall(co, stack, PyTuple_GET_SIZE(argdefs),
415 globals);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100416 }
417 }
418
419 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
420 closure = PyFunction_GET_CLOSURE(func);
421 name = ((PyFunctionObject *)func) -> func_name;
422 qualname = ((PyFunctionObject *)func) -> func_qualname;
423
424 if (argdefs != NULL) {
425 d = &PyTuple_GET_ITEM(argdefs, 0);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200426 nd = PyTuple_GET_SIZE(argdefs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100427 }
428 else {
429 d = NULL;
430 nd = 0;
431 }
432 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
433 stack, nargs,
434 nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
435 stack + nargs,
436 nkwargs, 1,
437 d, (int)nd, kwdefs,
438 closure, name, qualname);
439}
440
441
442/* --- PyCFunction call functions --------------------------------- */
443
444PyObject *
445_PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self, PyObject **args,
446 Py_ssize_t nargs, PyObject *kwargs)
447{
448 /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
449 because it can clear it (directly or indirectly) and so the
450 caller loses its exception */
451 assert(!PyErr_Occurred());
452
453 assert(method != NULL);
454 assert(nargs >= 0);
455 assert(nargs == 0 || args != NULL);
456 assert(kwargs == NULL || PyDict_Check(kwargs));
457
458 PyCFunction meth = method->ml_meth;
459 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
460 PyObject *result = NULL;
461
462 if (Py_EnterRecursiveCall(" while calling a Python object")) {
463 return NULL;
464 }
465
466 switch (flags)
467 {
468 case METH_NOARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300469 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
470 goto no_keyword_error;
471 }
472
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100473 if (nargs != 0) {
474 PyErr_Format(PyExc_TypeError,
475 "%.200s() takes no arguments (%zd given)",
476 method->ml_name, nargs);
477 goto exit;
478 }
479
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100480 result = (*meth) (self, NULL);
481 break;
482
483 case METH_O:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300484 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
485 goto no_keyword_error;
486 }
487
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100488 if (nargs != 1) {
489 PyErr_Format(PyExc_TypeError,
490 "%.200s() takes exactly one argument (%zd given)",
491 method->ml_name, nargs);
492 goto exit;
493 }
494
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100495 result = (*meth) (self, args[0]);
496 break;
497
498 case METH_VARARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300499 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100500 goto no_keyword_error;
501 }
502 /* fall through next case */
503
504 case METH_VARARGS | METH_KEYWORDS:
505 {
506 /* Slow-path: create a temporary tuple for positional arguments */
507 PyObject *argstuple = _PyStack_AsTuple(args, nargs);
508 if (argstuple == NULL) {
509 goto exit;
510 }
511
512 if (flags & METH_KEYWORDS) {
513 result = (*(PyCFunctionWithKeywords)meth) (self, argstuple, kwargs);
514 }
515 else {
516 result = (*meth) (self, argstuple);
517 }
518 Py_DECREF(argstuple);
519 break;
520 }
521
522 case METH_FASTCALL:
523 {
524 PyObject **stack;
525 PyObject *kwnames;
526 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
527
528 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
529 goto exit;
530 }
531
532 result = (*fastmeth) (self, stack, nargs, kwnames);
533 if (stack != args) {
534 PyMem_Free(stack);
535 }
536 Py_XDECREF(kwnames);
537 break;
538 }
539
540 default:
541 PyErr_SetString(PyExc_SystemError,
542 "Bad call flags in _PyMethodDef_RawFastCallDict. "
543 "METH_OLDARGS is no longer supported!");
544 goto exit;
545 }
546
547 goto exit;
548
549no_keyword_error:
550 PyErr_Format(PyExc_TypeError,
551 "%.200s() takes no keyword arguments",
Sylvaind67a1032017-03-27 23:36:08 +0200552 method->ml_name);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100553
554exit:
555 Py_LeaveRecursiveCall();
556 return result;
557}
558
559
560PyObject *
561_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
562 PyObject *kwargs)
563{
564 PyObject *result;
565
566 assert(func != NULL);
567 assert(PyCFunction_Check(func));
568
569 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
570 PyCFunction_GET_SELF(func),
571 args, nargs, kwargs);
572 result = _Py_CheckFunctionResult(func, result, NULL);
573 return result;
574}
575
576
577PyObject *
578_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args,
579 Py_ssize_t nargs, PyObject *kwnames)
580{
581 /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
582 because it can clear it (directly or indirectly) and so the
583 caller loses its exception */
584 assert(!PyErr_Occurred());
585
586 assert(method != NULL);
587 assert(nargs >= 0);
588 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
589 /* kwnames must only contains str strings, no subclass, and all keys must
590 be unique */
591
592 PyCFunction meth = method->ml_meth;
593 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300594 Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100595 PyObject *result = NULL;
596
597 if (Py_EnterRecursiveCall(" while calling a Python object")) {
598 return NULL;
599 }
600
601 switch (flags)
602 {
603 case METH_NOARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300604 if (nkwargs) {
605 goto no_keyword_error;
606 }
607
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100608 if (nargs != 0) {
609 PyErr_Format(PyExc_TypeError,
610 "%.200s() takes no arguments (%zd given)",
611 method->ml_name, nargs);
612 goto exit;
613 }
614
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100615 result = (*meth) (self, NULL);
616 break;
617
618 case METH_O:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300619 if (nkwargs) {
620 goto no_keyword_error;
621 }
622
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100623 if (nargs != 1) {
624 PyErr_Format(PyExc_TypeError,
625 "%.200s() takes exactly one argument (%zd given)",
626 method->ml_name, nargs);
627 goto exit;
628 }
629
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100630 result = (*meth) (self, args[0]);
631 break;
632
633 case METH_FASTCALL:
634 /* Fast-path: avoid temporary dict to pass keyword arguments */
635 result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames);
636 break;
637
638 case METH_VARARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300639 if (nkwargs) {
640 goto no_keyword_error;
641 }
642 /* fall through next case */
643
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100644 case METH_VARARGS | METH_KEYWORDS:
645 {
646 /* Slow-path: create a temporary tuple for positional arguments
647 and a temporary dict for keyword arguments */
648 PyObject *argtuple;
649
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100650 argtuple = _PyStack_AsTuple(args, nargs);
651 if (argtuple == NULL) {
652 goto exit;
653 }
654
655 if (flags & METH_KEYWORDS) {
656 PyObject *kwdict;
657
658 if (nkwargs > 0) {
659 kwdict = _PyStack_AsDict(args + nargs, kwnames);
660 if (kwdict == NULL) {
661 Py_DECREF(argtuple);
662 goto exit;
663 }
664 }
665 else {
666 kwdict = NULL;
667 }
668
669 result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
670 Py_XDECREF(kwdict);
671 }
672 else {
673 result = (*meth) (self, argtuple);
674 }
675 Py_DECREF(argtuple);
676 break;
677 }
678
679 default:
680 PyErr_SetString(PyExc_SystemError,
681 "Bad call flags in _PyCFunction_FastCallKeywords. "
682 "METH_OLDARGS is no longer supported!");
683 goto exit;
684 }
685
686 goto exit;
687
688no_keyword_error:
689 PyErr_Format(PyExc_TypeError,
690 "%.200s() takes no keyword arguments",
691 method->ml_name);
692
693exit:
694 Py_LeaveRecursiveCall();
695 return result;
696}
697
698
699PyObject *
700_PyCFunction_FastCallKeywords(PyObject *func, PyObject **args,
701 Py_ssize_t nargs, PyObject *kwnames)
702{
703 PyObject *result;
704
705 assert(func != NULL);
706 assert(PyCFunction_Check(func));
707
708 result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml,
709 PyCFunction_GET_SELF(func),
710 args, nargs, kwnames);
711 result = _Py_CheckFunctionResult(func, result, NULL);
712 return result;
713}
714
715
716static PyObject *
717cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs)
718{
719 assert(!PyErr_Occurred());
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300720 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100721
722 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
723 PyObject *self = PyCFunction_GET_SELF(func);
724 PyObject *result;
725
726 if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) {
727 if (Py_EnterRecursiveCall(" while calling a Python object")) {
728 return NULL;
729 }
730
731 result = (*(PyCFunctionWithKeywords)meth)(self, args, kwargs);
732
733 Py_LeaveRecursiveCall();
734 }
735 else {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300736 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100737 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
738 ((PyCFunctionObject*)func)->m_ml->ml_name);
739 return NULL;
740 }
741
742 if (Py_EnterRecursiveCall(" while calling a Python object")) {
743 return NULL;
744 }
745
746 result = (*meth)(self, args);
747
748 Py_LeaveRecursiveCall();
749 }
750
751 return _Py_CheckFunctionResult(func, result, NULL);
752}
753
754
755PyObject *
756PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
757{
758 /* first try METH_VARARGS to pass directly args tuple unchanged.
759 _PyMethodDef_RawFastCallDict() creates a new temporary tuple
760 for METH_VARARGS. */
761 if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) {
762 return cfunction_call_varargs(func, args, kwargs);
763 }
764 else {
765 return _PyCFunction_FastCallDict(func,
766 &PyTuple_GET_ITEM(args, 0),
767 PyTuple_GET_SIZE(args),
768 kwargs);
769 }
770}
771
772
773/* --- More complex call functions -------------------------------- */
774
775/* External interface to call any callable object.
776 The args must be a tuple or NULL. The kwargs must be a dict or NULL. */
777PyObject *
778PyEval_CallObjectWithKeywords(PyObject *callable,
779 PyObject *args, PyObject *kwargs)
780{
781#ifdef Py_DEBUG
782 /* PyEval_CallObjectWithKeywords() must not be called with an exception
783 set. It raises a new exception if parameters are invalid or if
784 PyTuple_New() fails, and so the original exception is lost. */
785 assert(!PyErr_Occurred());
786#endif
787
INADA Naoki3824cd82017-03-01 20:41:03 +0900788 if (args != NULL && !PyTuple_Check(args)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100789 PyErr_SetString(PyExc_TypeError,
790 "argument list must be a tuple");
791 return NULL;
792 }
793
794 if (kwargs != NULL && !PyDict_Check(kwargs)) {
795 PyErr_SetString(PyExc_TypeError,
796 "keyword list must be a dictionary");
797 return NULL;
798 }
799
INADA Naoki3824cd82017-03-01 20:41:03 +0900800 if (args == NULL) {
801 return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
802 }
803 else {
804 return PyObject_Call(callable, args, kwargs);
805 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100806}
807
808
809PyObject *
810PyObject_CallObject(PyObject *callable, PyObject *args)
811{
812 return PyEval_CallObjectWithKeywords(callable, args, NULL);
813}
814
815
816/* Positional arguments are obj followed by args:
817 call callable(obj, *args, **kwargs) */
818PyObject *
819_PyObject_FastCall_Prepend(PyObject *callable,
820 PyObject *obj, PyObject **args, Py_ssize_t nargs)
821{
822 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
823 PyObject **args2;
824 PyObject *result;
825
826 nargs++;
827 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
828 args2 = small_stack;
829 }
830 else {
831 args2 = PyMem_Malloc(nargs * sizeof(PyObject *));
832 if (args2 == NULL) {
833 PyErr_NoMemory();
834 return NULL;
835 }
836 }
837
838 /* use borrowed references */
839 args2[0] = obj;
840 memcpy(&args2[1],
841 args,
842 (nargs - 1)* sizeof(PyObject *));
843
844 result = _PyObject_FastCall(callable, args2, nargs);
845 if (args2 != small_stack) {
846 PyMem_Free(args2);
847 }
848 return result;
849}
850
851
852/* Call callable(obj, *args, **kwargs). */
853PyObject *
854_PyObject_Call_Prepend(PyObject *callable,
855 PyObject *obj, PyObject *args, PyObject *kwargs)
856{
857 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
858 PyObject **stack;
859 Py_ssize_t argcount;
860 PyObject *result;
861
862 assert(PyTuple_Check(args));
863
864 argcount = PyTuple_GET_SIZE(args);
865 if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
866 stack = small_stack;
867 }
868 else {
869 stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
870 if (stack == NULL) {
871 PyErr_NoMemory();
872 return NULL;
873 }
874 }
875
876 /* use borrowed references */
877 stack[0] = obj;
878 memcpy(&stack[1],
879 &PyTuple_GET_ITEM(args, 0),
880 argcount * sizeof(PyObject *));
881
882 result = _PyObject_FastCallDict(callable,
883 stack, argcount + 1,
884 kwargs);
885 if (stack != small_stack) {
886 PyMem_Free(stack);
887 }
888 return result;
889}
890
891
892/* --- Call with a format string ---------------------------------- */
893
894static PyObject *
895_PyObject_CallFunctionVa(PyObject *callable, const char *format,
896 va_list va, int is_size_t)
897{
898 PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
899 const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
900 PyObject **stack;
901 Py_ssize_t nargs, i;
902 PyObject *result;
903
904 if (callable == NULL) {
905 return null_error();
906 }
907
908 if (!format || !*format) {
909 return _PyObject_CallNoArg(callable);
910 }
911
912 if (is_size_t) {
913 stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
914 format, va, &nargs);
915 }
916 else {
917 stack = _Py_VaBuildStack(small_stack, small_stack_len,
918 format, va, &nargs);
919 }
920 if (stack == NULL) {
921 return NULL;
922 }
923
924 if (nargs == 1 && PyTuple_Check(stack[0])) {
925 /* Special cases for backward compatibility:
926 - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
927 - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
928 func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
929 PyObject *args = stack[0];
930 result = _PyObject_FastCall(callable,
931 &PyTuple_GET_ITEM(args, 0),
932 PyTuple_GET_SIZE(args));
933 }
934 else {
935 result = _PyObject_FastCall(callable, stack, nargs);
936 }
937
938 for (i = 0; i < nargs; ++i) {
939 Py_DECREF(stack[i]);
940 }
941 if (stack != small_stack) {
942 PyMem_Free(stack);
943 }
944 return result;
945}
946
947
948PyObject *
949PyObject_CallFunction(PyObject *callable, const char *format, ...)
950{
951 va_list va;
952 PyObject *result;
953
954 va_start(va, format);
955 result = _PyObject_CallFunctionVa(callable, format, va, 0);
956 va_end(va);
957
958 return result;
959}
960
961
INADA Naokiaa289a52017-03-14 18:00:59 +0900962/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
963 * This function is kept for backward compatibility.
964 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100965PyObject *
966PyEval_CallFunction(PyObject *callable, const char *format, ...)
967{
INADA Naokiaa289a52017-03-14 18:00:59 +0900968 va_list va;
969 PyObject *result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100970
INADA Naokiaa289a52017-03-14 18:00:59 +0900971 va_start(va, format);
972 result = _PyObject_CallFunctionVa(callable, format, va, 0);
973 va_end(va);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100974
INADA Naokiaa289a52017-03-14 18:00:59 +0900975 return result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100976}
977
978
979PyObject *
980_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
981{
982 va_list va;
983 PyObject *result;
984
985 va_start(va, format);
986 result = _PyObject_CallFunctionVa(callable, format, va, 1);
987 va_end(va);
988
989 return result;
990}
991
992
993static PyObject*
994callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
995{
996 assert(callable != NULL);
997
998 if (!PyCallable_Check(callable)) {
999 PyErr_Format(PyExc_TypeError,
1000 "attribute of type '%.200s' is not callable",
1001 Py_TYPE(callable)->tp_name);
1002 return NULL;
1003 }
1004
1005 return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
1006}
1007
1008
1009PyObject *
1010PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
1011{
1012 va_list va;
1013 PyObject *callable, *retval;
1014
1015 if (obj == NULL || name == NULL) {
1016 return null_error();
1017 }
1018
1019 callable = PyObject_GetAttrString(obj, name);
1020 if (callable == NULL)
1021 return NULL;
1022
1023 va_start(va, format);
1024 retval = callmethod(callable, format, va, 0);
1025 va_end(va);
1026
1027 Py_DECREF(callable);
1028 return retval;
1029}
1030
1031
INADA Naokiaa289a52017-03-14 18:00:59 +09001032/* PyEval_CallMethod is exact copy of PyObject_CallMethod.
1033 * This function is kept for backward compatibility.
1034 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001035PyObject *
1036PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
1037{
INADA Naokiaa289a52017-03-14 18:00:59 +09001038 va_list va;
1039 PyObject *callable, *retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001040
INADA Naokiaa289a52017-03-14 18:00:59 +09001041 if (obj == NULL || name == NULL) {
1042 return null_error();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001043 }
1044
INADA Naokiaa289a52017-03-14 18:00:59 +09001045 callable = PyObject_GetAttrString(obj, name);
1046 if (callable == NULL)
1047 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001048
INADA Naokiaa289a52017-03-14 18:00:59 +09001049 va_start(va, format);
1050 retval = callmethod(callable, format, va, 0);
1051 va_end(va);
1052
1053 Py_DECREF(callable);
1054 return retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001055}
1056
1057
1058PyObject *
1059_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
1060 const char *format, ...)
1061{
1062 va_list va;
1063 PyObject *callable, *retval;
1064
1065 if (obj == NULL || name == NULL) {
1066 return null_error();
1067 }
1068
1069 callable = _PyObject_GetAttrId(obj, name);
1070 if (callable == NULL)
1071 return NULL;
1072
1073 va_start(va, format);
1074 retval = callmethod(callable, format, va, 0);
1075 va_end(va);
1076
1077 Py_DECREF(callable);
1078 return retval;
1079}
1080
1081
1082PyObject *
1083_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
1084 const char *format, ...)
1085{
1086 va_list va;
1087 PyObject *callable, *retval;
1088
1089 if (obj == NULL || name == NULL) {
1090 return null_error();
1091 }
1092
1093 callable = PyObject_GetAttrString(obj, name);
1094 if (callable == NULL)
1095 return NULL;
1096
1097 va_start(va, format);
1098 retval = callmethod(callable, format, va, 1);
1099 va_end(va);
1100
1101 Py_DECREF(callable);
1102 return retval;
1103}
1104
1105
1106PyObject *
1107_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
1108 const char *format, ...)
1109{
1110 va_list va;
1111 PyObject *callable, *retval;
1112
1113 if (obj == NULL || name == NULL) {
1114 return null_error();
1115 }
1116
1117 callable = _PyObject_GetAttrId(obj, name);
1118 if (callable == NULL) {
1119 return NULL;
1120 }
1121
1122 va_start(va, format);
1123 retval = callmethod(callable, format, va, 1);
1124 va_end(va);
1125
1126 Py_DECREF(callable);
1127 return retval;
1128}
1129
1130
1131/* --- Call with "..." arguments ---------------------------------- */
1132
1133static PyObject *
1134object_vacall(PyObject *callable, va_list vargs)
1135{
1136 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
1137 PyObject **stack;
1138 Py_ssize_t nargs;
1139 PyObject *result;
1140 Py_ssize_t i;
1141 va_list countva;
1142
1143 if (callable == NULL) {
1144 return null_error();
1145 }
1146
1147 /* Count the number of arguments */
1148 va_copy(countva, vargs);
1149 nargs = 0;
1150 while (1) {
1151 PyObject *arg = va_arg(countva, PyObject *);
1152 if (arg == NULL) {
1153 break;
1154 }
1155 nargs++;
1156 }
1157 va_end(countva);
1158
1159 /* Copy arguments */
1160 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1161 stack = small_stack;
1162 }
1163 else {
1164 stack = PyMem_Malloc(nargs * sizeof(stack[0]));
1165 if (stack == NULL) {
1166 PyErr_NoMemory();
1167 return NULL;
1168 }
1169 }
1170
1171 for (i = 0; i < nargs; ++i) {
1172 stack[i] = va_arg(vargs, PyObject *);
1173 }
1174
1175 /* Call the function */
1176 result = _PyObject_FastCall(callable, stack, nargs);
1177
1178 if (stack != small_stack) {
1179 PyMem_Free(stack);
1180 }
1181 return result;
1182}
1183
1184
1185PyObject *
1186PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
1187{
1188 va_list vargs;
1189 PyObject *result;
1190
1191 if (callable == NULL || name == NULL) {
1192 return null_error();
1193 }
1194
1195 callable = PyObject_GetAttr(callable, name);
1196 if (callable == NULL) {
1197 return NULL;
1198 }
1199
1200 va_start(vargs, name);
1201 result = object_vacall(callable, vargs);
1202 va_end(vargs);
1203
1204 Py_DECREF(callable);
1205 return result;
1206}
1207
1208
1209PyObject *
1210_PyObject_CallMethodIdObjArgs(PyObject *obj,
1211 struct _Py_Identifier *name, ...)
1212{
1213 va_list vargs;
1214 PyObject *callable, *result;
1215
1216 if (obj == NULL || name == NULL) {
1217 return null_error();
1218 }
1219
1220 callable = _PyObject_GetAttrId(obj, name);
1221 if (callable == NULL) {
1222 return NULL;
1223 }
1224
1225 va_start(vargs, name);
1226 result = object_vacall(callable, vargs);
1227 va_end(vargs);
1228
1229 Py_DECREF(callable);
1230 return result;
1231}
1232
1233
1234PyObject *
1235PyObject_CallFunctionObjArgs(PyObject *callable, ...)
1236{
1237 va_list vargs;
1238 PyObject *result;
1239
1240 va_start(vargs, callable);
1241 result = object_vacall(callable, vargs);
1242 va_end(vargs);
1243
1244 return result;
1245}
1246
1247
1248/* --- PyStack functions ------------------------------------------ */
1249
1250/* Issue #29234: Inlining _PyStack_AsTuple() into callers increases their
1251 stack consumption, Disable inlining to optimize the stack consumption. */
1252PyObject* _Py_NO_INLINE
1253_PyStack_AsTuple(PyObject **stack, Py_ssize_t nargs)
1254{
1255 PyObject *args;
1256 Py_ssize_t i;
1257
1258 args = PyTuple_New(nargs);
1259 if (args == NULL) {
1260 return NULL;
1261 }
1262
1263 for (i=0; i < nargs; i++) {
1264 PyObject *item = stack[i];
1265 Py_INCREF(item);
1266 PyTuple_SET_ITEM(args, i, item);
1267 }
1268 return args;
1269}
1270
1271
1272PyObject*
1273_PyStack_AsTupleSlice(PyObject **stack, Py_ssize_t nargs,
1274 Py_ssize_t start, Py_ssize_t end)
1275{
1276 PyObject *args;
1277 Py_ssize_t i;
1278
1279 assert(0 <= start);
1280 assert(end <= nargs);
1281 assert(start <= end);
1282
1283 args = PyTuple_New(end - start);
1284 if (args == NULL) {
1285 return NULL;
1286 }
1287
1288 for (i=start; i < end; i++) {
1289 PyObject *item = stack[i];
1290 Py_INCREF(item);
1291 PyTuple_SET_ITEM(args, i - start, item);
1292 }
1293 return args;
1294}
1295
1296
1297PyObject *
1298_PyStack_AsDict(PyObject **values, PyObject *kwnames)
1299{
1300 Py_ssize_t nkwargs;
1301 PyObject *kwdict;
1302 Py_ssize_t i;
1303
1304 assert(kwnames != NULL);
1305 nkwargs = PyTuple_GET_SIZE(kwnames);
1306 kwdict = _PyDict_NewPresized(nkwargs);
1307 if (kwdict == NULL) {
1308 return NULL;
1309 }
1310
1311 for (i = 0; i < nkwargs; i++) {
1312 PyObject *key = PyTuple_GET_ITEM(kwnames, i);
1313 PyObject *value = *values++;
1314 /* If key already exists, replace it with the new value */
1315 if (PyDict_SetItem(kwdict, key, value)) {
1316 Py_DECREF(kwdict);
1317 return NULL;
1318 }
1319 }
1320 return kwdict;
1321}
1322
1323
1324int
1325_PyStack_UnpackDict(PyObject **args, Py_ssize_t nargs, PyObject *kwargs,
1326 PyObject ***p_stack, PyObject **p_kwnames)
1327{
1328 PyObject **stack, **kwstack;
1329 Py_ssize_t nkwargs;
1330 Py_ssize_t pos, i;
1331 PyObject *key, *value;
1332 PyObject *kwnames;
1333
1334 assert(nargs >= 0);
1335 assert(kwargs == NULL || PyDict_CheckExact(kwargs));
1336
1337 if (kwargs == NULL || (nkwargs = PyDict_GET_SIZE(kwargs)) == 0) {
1338 *p_stack = args;
1339 *p_kwnames = NULL;
1340 return 0;
1341 }
1342
1343 if ((size_t)nargs > PY_SSIZE_T_MAX / sizeof(stack[0]) - (size_t)nkwargs) {
1344 PyErr_NoMemory();
1345 return -1;
1346 }
1347
1348 stack = PyMem_Malloc((nargs + nkwargs) * sizeof(stack[0]));
1349 if (stack == NULL) {
1350 PyErr_NoMemory();
1351 return -1;
1352 }
1353
1354 kwnames = PyTuple_New(nkwargs);
1355 if (kwnames == NULL) {
1356 PyMem_Free(stack);
1357 return -1;
1358 }
1359
1360 /* Copy position arguments (borrowed references) */
1361 memcpy(stack, args, nargs * sizeof(stack[0]));
1362
1363 kwstack = stack + nargs;
1364 pos = i = 0;
1365 /* This loop doesn't support lookup function mutating the dictionary
1366 to change its size. It's a deliberate choice for speed, this function is
1367 called in the performance critical hot code. */
1368 while (PyDict_Next(kwargs, &pos, &key, &value)) {
1369 Py_INCREF(key);
1370 PyTuple_SET_ITEM(kwnames, i, key);
1371 /* The stack contains borrowed references */
1372 kwstack[i] = value;
1373 i++;
1374 }
1375
1376 *p_stack = stack;
1377 *p_kwnames = kwnames;
1378 return 0;
1379}