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