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