blob: 4c74eab44f5e4386b57f3b5741c7a73c25eaaf5c [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:
469 if (nargs != 0) {
470 PyErr_Format(PyExc_TypeError,
471 "%.200s() takes no arguments (%zd given)",
472 method->ml_name, nargs);
473 goto exit;
474 }
475
476 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
477 goto no_keyword_error;
478 }
479
480 result = (*meth) (self, NULL);
481 break;
482
483 case METH_O:
484 if (nargs != 1) {
485 PyErr_Format(PyExc_TypeError,
486 "%.200s() takes exactly one argument (%zd given)",
487 method->ml_name, nargs);
488 goto exit;
489 }
490
491 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
492 goto no_keyword_error;
493 }
494
495 result = (*meth) (self, args[0]);
496 break;
497
498 case METH_VARARGS:
499 if (!(flags & METH_KEYWORDS)
500 && kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
501 goto no_keyword_error;
502 }
503 /* fall through next case */
504
505 case METH_VARARGS | METH_KEYWORDS:
506 {
507 /* Slow-path: create a temporary tuple for positional arguments */
508 PyObject *argstuple = _PyStack_AsTuple(args, nargs);
509 if (argstuple == NULL) {
510 goto exit;
511 }
512
513 if (flags & METH_KEYWORDS) {
514 result = (*(PyCFunctionWithKeywords)meth) (self, argstuple, kwargs);
515 }
516 else {
517 result = (*meth) (self, argstuple);
518 }
519 Py_DECREF(argstuple);
520 break;
521 }
522
523 case METH_FASTCALL:
524 {
525 PyObject **stack;
526 PyObject *kwnames;
527 _PyCFunctionFast fastmeth = (_PyCFunctionFast)meth;
528
529 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
530 goto exit;
531 }
532
533 result = (*fastmeth) (self, stack, nargs, kwnames);
534 if (stack != args) {
535 PyMem_Free(stack);
536 }
537 Py_XDECREF(kwnames);
538 break;
539 }
540
541 default:
542 PyErr_SetString(PyExc_SystemError,
543 "Bad call flags in _PyMethodDef_RawFastCallDict. "
544 "METH_OLDARGS is no longer supported!");
545 goto exit;
546 }
547
548 goto exit;
549
550no_keyword_error:
551 PyErr_Format(PyExc_TypeError,
552 "%.200s() takes no keyword arguments",
Sylvaind67a1032017-03-27 23:36:08 +0200553 method->ml_name);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100554
555exit:
556 Py_LeaveRecursiveCall();
557 return result;
558}
559
560
561PyObject *
562_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
563 PyObject *kwargs)
564{
565 PyObject *result;
566
567 assert(func != NULL);
568 assert(PyCFunction_Check(func));
569
570 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
571 PyCFunction_GET_SELF(func),
572 args, nargs, kwargs);
573 result = _Py_CheckFunctionResult(func, result, NULL);
574 return result;
575}
576
577
578PyObject *
579_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args,
580 Py_ssize_t nargs, PyObject *kwnames)
581{
582 /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
583 because it can clear it (directly or indirectly) and so the
584 caller loses its exception */
585 assert(!PyErr_Occurred());
586
587 assert(method != NULL);
588 assert(nargs >= 0);
589 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
590 /* kwnames must only contains str strings, no subclass, and all keys must
591 be unique */
592
593 PyCFunction meth = method->ml_meth;
594 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
595 Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_Size(kwnames);
596 PyObject *result = NULL;
597
598 if (Py_EnterRecursiveCall(" while calling a Python object")) {
599 return NULL;
600 }
601
602 switch (flags)
603 {
604 case METH_NOARGS:
605 if (nargs != 0) {
606 PyErr_Format(PyExc_TypeError,
607 "%.200s() takes no arguments (%zd given)",
608 method->ml_name, nargs);
609 goto exit;
610 }
611
612 if (nkwargs) {
613 goto no_keyword_error;
614 }
615
616 result = (*meth) (self, NULL);
617 break;
618
619 case METH_O:
620 if (nargs != 1) {
621 PyErr_Format(PyExc_TypeError,
622 "%.200s() takes exactly one argument (%zd given)",
623 method->ml_name, nargs);
624 goto exit;
625 }
626
627 if (nkwargs) {
628 goto no_keyword_error;
629 }
630
631 result = (*meth) (self, args[0]);
632 break;
633
634 case METH_FASTCALL:
635 /* Fast-path: avoid temporary dict to pass keyword arguments */
636 result = ((_PyCFunctionFast)meth) (self, args, nargs, kwnames);
637 break;
638
639 case METH_VARARGS:
640 case METH_VARARGS | METH_KEYWORDS:
641 {
642 /* Slow-path: create a temporary tuple for positional arguments
643 and a temporary dict for keyword arguments */
644 PyObject *argtuple;
645
646 if (!(flags & METH_KEYWORDS) && nkwargs) {
647 goto no_keyword_error;
648 }
649
650 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());
720
721 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
722 PyObject *self = PyCFunction_GET_SELF(func);
723 PyObject *result;
724
725 if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) {
726 if (Py_EnterRecursiveCall(" while calling a Python object")) {
727 return NULL;
728 }
729
730 result = (*(PyCFunctionWithKeywords)meth)(self, args, kwargs);
731
732 Py_LeaveRecursiveCall();
733 }
734 else {
735 if (kwargs != NULL && PyDict_Size(kwargs) != 0) {
736 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
737 ((PyCFunctionObject*)func)->m_ml->ml_name);
738 return NULL;
739 }
740
741 if (Py_EnterRecursiveCall(" while calling a Python object")) {
742 return NULL;
743 }
744
745 result = (*meth)(self, args);
746
747 Py_LeaveRecursiveCall();
748 }
749
750 return _Py_CheckFunctionResult(func, result, NULL);
751}
752
753
754PyObject *
755PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
756{
757 /* first try METH_VARARGS to pass directly args tuple unchanged.
758 _PyMethodDef_RawFastCallDict() creates a new temporary tuple
759 for METH_VARARGS. */
760 if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) {
761 return cfunction_call_varargs(func, args, kwargs);
762 }
763 else {
764 return _PyCFunction_FastCallDict(func,
765 &PyTuple_GET_ITEM(args, 0),
766 PyTuple_GET_SIZE(args),
767 kwargs);
768 }
769}
770
771
772/* --- More complex call functions -------------------------------- */
773
774/* External interface to call any callable object.
775 The args must be a tuple or NULL. The kwargs must be a dict or NULL. */
776PyObject *
777PyEval_CallObjectWithKeywords(PyObject *callable,
778 PyObject *args, PyObject *kwargs)
779{
780#ifdef Py_DEBUG
781 /* PyEval_CallObjectWithKeywords() must not be called with an exception
782 set. It raises a new exception if parameters are invalid or if
783 PyTuple_New() fails, and so the original exception is lost. */
784 assert(!PyErr_Occurred());
785#endif
786
INADA Naoki3824cd82017-03-01 20:41:03 +0900787 if (args != NULL && !PyTuple_Check(args)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100788 PyErr_SetString(PyExc_TypeError,
789 "argument list must be a tuple");
790 return NULL;
791 }
792
793 if (kwargs != NULL && !PyDict_Check(kwargs)) {
794 PyErr_SetString(PyExc_TypeError,
795 "keyword list must be a dictionary");
796 return NULL;
797 }
798
INADA Naoki3824cd82017-03-01 20:41:03 +0900799 if (args == NULL) {
800 return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
801 }
802 else {
803 return PyObject_Call(callable, args, kwargs);
804 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100805}
806
807
808PyObject *
809PyObject_CallObject(PyObject *callable, PyObject *args)
810{
811 return PyEval_CallObjectWithKeywords(callable, args, NULL);
812}
813
814
815/* Positional arguments are obj followed by args:
816 call callable(obj, *args, **kwargs) */
817PyObject *
818_PyObject_FastCall_Prepend(PyObject *callable,
819 PyObject *obj, PyObject **args, Py_ssize_t nargs)
820{
821 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
822 PyObject **args2;
823 PyObject *result;
824
825 nargs++;
826 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
827 args2 = small_stack;
828 }
829 else {
830 args2 = PyMem_Malloc(nargs * sizeof(PyObject *));
831 if (args2 == NULL) {
832 PyErr_NoMemory();
833 return NULL;
834 }
835 }
836
837 /* use borrowed references */
838 args2[0] = obj;
839 memcpy(&args2[1],
840 args,
841 (nargs - 1)* sizeof(PyObject *));
842
843 result = _PyObject_FastCall(callable, args2, nargs);
844 if (args2 != small_stack) {
845 PyMem_Free(args2);
846 }
847 return result;
848}
849
850
851/* Call callable(obj, *args, **kwargs). */
852PyObject *
853_PyObject_Call_Prepend(PyObject *callable,
854 PyObject *obj, PyObject *args, PyObject *kwargs)
855{
856 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
857 PyObject **stack;
858 Py_ssize_t argcount;
859 PyObject *result;
860
861 assert(PyTuple_Check(args));
862
863 argcount = PyTuple_GET_SIZE(args);
864 if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
865 stack = small_stack;
866 }
867 else {
868 stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
869 if (stack == NULL) {
870 PyErr_NoMemory();
871 return NULL;
872 }
873 }
874
875 /* use borrowed references */
876 stack[0] = obj;
877 memcpy(&stack[1],
878 &PyTuple_GET_ITEM(args, 0),
879 argcount * sizeof(PyObject *));
880
881 result = _PyObject_FastCallDict(callable,
882 stack, argcount + 1,
883 kwargs);
884 if (stack != small_stack) {
885 PyMem_Free(stack);
886 }
887 return result;
888}
889
890
891/* --- Call with a format string ---------------------------------- */
892
893static PyObject *
894_PyObject_CallFunctionVa(PyObject *callable, const char *format,
895 va_list va, int is_size_t)
896{
897 PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
898 const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
899 PyObject **stack;
900 Py_ssize_t nargs, i;
901 PyObject *result;
902
903 if (callable == NULL) {
904 return null_error();
905 }
906
907 if (!format || !*format) {
908 return _PyObject_CallNoArg(callable);
909 }
910
911 if (is_size_t) {
912 stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
913 format, va, &nargs);
914 }
915 else {
916 stack = _Py_VaBuildStack(small_stack, small_stack_len,
917 format, va, &nargs);
918 }
919 if (stack == NULL) {
920 return NULL;
921 }
922
923 if (nargs == 1 && PyTuple_Check(stack[0])) {
924 /* Special cases for backward compatibility:
925 - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
926 - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
927 func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
928 PyObject *args = stack[0];
929 result = _PyObject_FastCall(callable,
930 &PyTuple_GET_ITEM(args, 0),
931 PyTuple_GET_SIZE(args));
932 }
933 else {
934 result = _PyObject_FastCall(callable, stack, nargs);
935 }
936
937 for (i = 0; i < nargs; ++i) {
938 Py_DECREF(stack[i]);
939 }
940 if (stack != small_stack) {
941 PyMem_Free(stack);
942 }
943 return result;
944}
945
946
947PyObject *
948PyObject_CallFunction(PyObject *callable, const char *format, ...)
949{
950 va_list va;
951 PyObject *result;
952
953 va_start(va, format);
954 result = _PyObject_CallFunctionVa(callable, format, va, 0);
955 va_end(va);
956
957 return result;
958}
959
960
INADA Naokiaa289a52017-03-14 18:00:59 +0900961/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
962 * This function is kept for backward compatibility.
963 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100964PyObject *
965PyEval_CallFunction(PyObject *callable, const char *format, ...)
966{
INADA Naokiaa289a52017-03-14 18:00:59 +0900967 va_list va;
968 PyObject *result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100969
INADA Naokiaa289a52017-03-14 18:00:59 +0900970 va_start(va, format);
971 result = _PyObject_CallFunctionVa(callable, format, va, 0);
972 va_end(va);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100973
INADA Naokiaa289a52017-03-14 18:00:59 +0900974 return result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100975}
976
977
978PyObject *
979_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
980{
981 va_list va;
982 PyObject *result;
983
984 va_start(va, format);
985 result = _PyObject_CallFunctionVa(callable, format, va, 1);
986 va_end(va);
987
988 return result;
989}
990
991
992static PyObject*
993callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
994{
995 assert(callable != NULL);
996
997 if (!PyCallable_Check(callable)) {
998 PyErr_Format(PyExc_TypeError,
999 "attribute of type '%.200s' is not callable",
1000 Py_TYPE(callable)->tp_name);
1001 return NULL;
1002 }
1003
1004 return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
1005}
1006
1007
1008PyObject *
1009PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
1010{
1011 va_list va;
1012 PyObject *callable, *retval;
1013
1014 if (obj == NULL || name == NULL) {
1015 return null_error();
1016 }
1017
1018 callable = PyObject_GetAttrString(obj, name);
1019 if (callable == NULL)
1020 return NULL;
1021
1022 va_start(va, format);
1023 retval = callmethod(callable, format, va, 0);
1024 va_end(va);
1025
1026 Py_DECREF(callable);
1027 return retval;
1028}
1029
1030
INADA Naokiaa289a52017-03-14 18:00:59 +09001031/* PyEval_CallMethod is exact copy of PyObject_CallMethod.
1032 * This function is kept for backward compatibility.
1033 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001034PyObject *
1035PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
1036{
INADA Naokiaa289a52017-03-14 18:00:59 +09001037 va_list va;
1038 PyObject *callable, *retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001039
INADA Naokiaa289a52017-03-14 18:00:59 +09001040 if (obj == NULL || name == NULL) {
1041 return null_error();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001042 }
1043
INADA Naokiaa289a52017-03-14 18:00:59 +09001044 callable = PyObject_GetAttrString(obj, name);
1045 if (callable == NULL)
1046 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001047
INADA Naokiaa289a52017-03-14 18:00:59 +09001048 va_start(va, format);
1049 retval = callmethod(callable, format, va, 0);
1050 va_end(va);
1051
1052 Py_DECREF(callable);
1053 return retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001054}
1055
1056
1057PyObject *
1058_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
1059 const char *format, ...)
1060{
1061 va_list va;
1062 PyObject *callable, *retval;
1063
1064 if (obj == NULL || name == NULL) {
1065 return null_error();
1066 }
1067
1068 callable = _PyObject_GetAttrId(obj, name);
1069 if (callable == NULL)
1070 return NULL;
1071
1072 va_start(va, format);
1073 retval = callmethod(callable, format, va, 0);
1074 va_end(va);
1075
1076 Py_DECREF(callable);
1077 return retval;
1078}
1079
1080
1081PyObject *
1082_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
1083 const char *format, ...)
1084{
1085 va_list va;
1086 PyObject *callable, *retval;
1087
1088 if (obj == NULL || name == NULL) {
1089 return null_error();
1090 }
1091
1092 callable = PyObject_GetAttrString(obj, name);
1093 if (callable == NULL)
1094 return NULL;
1095
1096 va_start(va, format);
1097 retval = callmethod(callable, format, va, 1);
1098 va_end(va);
1099
1100 Py_DECREF(callable);
1101 return retval;
1102}
1103
1104
1105PyObject *
1106_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
1107 const char *format, ...)
1108{
1109 va_list va;
1110 PyObject *callable, *retval;
1111
1112 if (obj == NULL || name == NULL) {
1113 return null_error();
1114 }
1115
1116 callable = _PyObject_GetAttrId(obj, name);
1117 if (callable == NULL) {
1118 return NULL;
1119 }
1120
1121 va_start(va, format);
1122 retval = callmethod(callable, format, va, 1);
1123 va_end(va);
1124
1125 Py_DECREF(callable);
1126 return retval;
1127}
1128
1129
1130/* --- Call with "..." arguments ---------------------------------- */
1131
1132static PyObject *
1133object_vacall(PyObject *callable, va_list vargs)
1134{
1135 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
1136 PyObject **stack;
1137 Py_ssize_t nargs;
1138 PyObject *result;
1139 Py_ssize_t i;
1140 va_list countva;
1141
1142 if (callable == NULL) {
1143 return null_error();
1144 }
1145
1146 /* Count the number of arguments */
1147 va_copy(countva, vargs);
1148 nargs = 0;
1149 while (1) {
1150 PyObject *arg = va_arg(countva, PyObject *);
1151 if (arg == NULL) {
1152 break;
1153 }
1154 nargs++;
1155 }
1156 va_end(countva);
1157
1158 /* Copy arguments */
1159 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1160 stack = small_stack;
1161 }
1162 else {
1163 stack = PyMem_Malloc(nargs * sizeof(stack[0]));
1164 if (stack == NULL) {
1165 PyErr_NoMemory();
1166 return NULL;
1167 }
1168 }
1169
1170 for (i = 0; i < nargs; ++i) {
1171 stack[i] = va_arg(vargs, PyObject *);
1172 }
1173
1174 /* Call the function */
1175 result = _PyObject_FastCall(callable, stack, nargs);
1176
1177 if (stack != small_stack) {
1178 PyMem_Free(stack);
1179 }
1180 return result;
1181}
1182
1183
1184PyObject *
1185PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
1186{
1187 va_list vargs;
1188 PyObject *result;
1189
1190 if (callable == NULL || name == NULL) {
1191 return null_error();
1192 }
1193
1194 callable = PyObject_GetAttr(callable, name);
1195 if (callable == NULL) {
1196 return NULL;
1197 }
1198
1199 va_start(vargs, name);
1200 result = object_vacall(callable, vargs);
1201 va_end(vargs);
1202
1203 Py_DECREF(callable);
1204 return result;
1205}
1206
1207
1208PyObject *
1209_PyObject_CallMethodIdObjArgs(PyObject *obj,
1210 struct _Py_Identifier *name, ...)
1211{
1212 va_list vargs;
1213 PyObject *callable, *result;
1214
1215 if (obj == NULL || name == NULL) {
1216 return null_error();
1217 }
1218
1219 callable = _PyObject_GetAttrId(obj, name);
1220 if (callable == NULL) {
1221 return NULL;
1222 }
1223
1224 va_start(vargs, name);
1225 result = object_vacall(callable, vargs);
1226 va_end(vargs);
1227
1228 Py_DECREF(callable);
1229 return result;
1230}
1231
1232
1233PyObject *
1234PyObject_CallFunctionObjArgs(PyObject *callable, ...)
1235{
1236 va_list vargs;
1237 PyObject *result;
1238
1239 va_start(vargs, callable);
1240 result = object_vacall(callable, vargs);
1241 va_end(vargs);
1242
1243 return result;
1244}
1245
1246
1247/* --- PyStack functions ------------------------------------------ */
1248
1249/* Issue #29234: Inlining _PyStack_AsTuple() into callers increases their
1250 stack consumption, Disable inlining to optimize the stack consumption. */
1251PyObject* _Py_NO_INLINE
1252_PyStack_AsTuple(PyObject **stack, Py_ssize_t nargs)
1253{
1254 PyObject *args;
1255 Py_ssize_t i;
1256
1257 args = PyTuple_New(nargs);
1258 if (args == NULL) {
1259 return NULL;
1260 }
1261
1262 for (i=0; i < nargs; i++) {
1263 PyObject *item = stack[i];
1264 Py_INCREF(item);
1265 PyTuple_SET_ITEM(args, i, item);
1266 }
1267 return args;
1268}
1269
1270
1271PyObject*
1272_PyStack_AsTupleSlice(PyObject **stack, Py_ssize_t nargs,
1273 Py_ssize_t start, Py_ssize_t end)
1274{
1275 PyObject *args;
1276 Py_ssize_t i;
1277
1278 assert(0 <= start);
1279 assert(end <= nargs);
1280 assert(start <= end);
1281
1282 args = PyTuple_New(end - start);
1283 if (args == NULL) {
1284 return NULL;
1285 }
1286
1287 for (i=start; i < end; i++) {
1288 PyObject *item = stack[i];
1289 Py_INCREF(item);
1290 PyTuple_SET_ITEM(args, i - start, item);
1291 }
1292 return args;
1293}
1294
1295
1296PyObject *
1297_PyStack_AsDict(PyObject **values, PyObject *kwnames)
1298{
1299 Py_ssize_t nkwargs;
1300 PyObject *kwdict;
1301 Py_ssize_t i;
1302
1303 assert(kwnames != NULL);
1304 nkwargs = PyTuple_GET_SIZE(kwnames);
1305 kwdict = _PyDict_NewPresized(nkwargs);
1306 if (kwdict == NULL) {
1307 return NULL;
1308 }
1309
1310 for (i = 0; i < nkwargs; i++) {
1311 PyObject *key = PyTuple_GET_ITEM(kwnames, i);
1312 PyObject *value = *values++;
1313 /* If key already exists, replace it with the new value */
1314 if (PyDict_SetItem(kwdict, key, value)) {
1315 Py_DECREF(kwdict);
1316 return NULL;
1317 }
1318 }
1319 return kwdict;
1320}
1321
1322
1323int
1324_PyStack_UnpackDict(PyObject **args, Py_ssize_t nargs, PyObject *kwargs,
1325 PyObject ***p_stack, PyObject **p_kwnames)
1326{
1327 PyObject **stack, **kwstack;
1328 Py_ssize_t nkwargs;
1329 Py_ssize_t pos, i;
1330 PyObject *key, *value;
1331 PyObject *kwnames;
1332
1333 assert(nargs >= 0);
1334 assert(kwargs == NULL || PyDict_CheckExact(kwargs));
1335
1336 if (kwargs == NULL || (nkwargs = PyDict_GET_SIZE(kwargs)) == 0) {
1337 *p_stack = args;
1338 *p_kwnames = NULL;
1339 return 0;
1340 }
1341
1342 if ((size_t)nargs > PY_SSIZE_T_MAX / sizeof(stack[0]) - (size_t)nkwargs) {
1343 PyErr_NoMemory();
1344 return -1;
1345 }
1346
1347 stack = PyMem_Malloc((nargs + nkwargs) * sizeof(stack[0]));
1348 if (stack == NULL) {
1349 PyErr_NoMemory();
1350 return -1;
1351 }
1352
1353 kwnames = PyTuple_New(nkwargs);
1354 if (kwnames == NULL) {
1355 PyMem_Free(stack);
1356 return -1;
1357 }
1358
1359 /* Copy position arguments (borrowed references) */
1360 memcpy(stack, args, nargs * sizeof(stack[0]));
1361
1362 kwstack = stack + nargs;
1363 pos = i = 0;
1364 /* This loop doesn't support lookup function mutating the dictionary
1365 to change its size. It's a deliberate choice for speed, this function is
1366 called in the performance critical hot code. */
1367 while (PyDict_Next(kwargs, &pos, &key, &value)) {
1368 Py_INCREF(key);
1369 PyTuple_SET_ITEM(kwnames, i, key);
1370 /* The stack contains borrowed references */
1371 kwstack[i] = value;
1372 i++;
1373 }
1374
1375 *p_stack = stack;
1376 *p_kwnames = kwnames;
1377 return 0;
1378}