• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "Python.h"
2 #include "pycore_object.h"
3 #include "pycore_pystate.h"
4 #include "pycore_tupleobject.h"
5 #include "frameobject.h"
6 
7 
8 static PyObject *
9 cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs);
10 
11 
12 static PyObject *
null_error(void)13 null_error(void)
14 {
15     if (!PyErr_Occurred())
16         PyErr_SetString(PyExc_SystemError,
17                         "null argument to internal routine");
18     return NULL;
19 }
20 
21 
22 PyObject*
_Py_CheckFunctionResult(PyObject * callable,PyObject * result,const char * where)23 _Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where)
24 {
25     int err_occurred = (PyErr_Occurred() != NULL);
26 
27     assert((callable != NULL) ^ (where != NULL));
28 
29     if (result == NULL) {
30         if (!err_occurred) {
31             if (callable)
32                 PyErr_Format(PyExc_SystemError,
33                              "%R returned NULL without setting an error",
34                              callable);
35             else
36                 PyErr_Format(PyExc_SystemError,
37                              "%s returned NULL without setting an error",
38                              where);
39 #ifdef Py_DEBUG
40             /* Ensure that the bug is caught in debug mode */
41             Py_FatalError("a function returned NULL without setting an error");
42 #endif
43             return NULL;
44         }
45     }
46     else {
47         if (err_occurred) {
48             Py_DECREF(result);
49 
50             if (callable) {
51                 _PyErr_FormatFromCause(PyExc_SystemError,
52                         "%R returned a result with an error set",
53                         callable);
54             }
55             else {
56                 _PyErr_FormatFromCause(PyExc_SystemError,
57                         "%s returned a result with an error set",
58                         where);
59             }
60 #ifdef Py_DEBUG
61             /* Ensure that the bug is caught in debug mode */
62             Py_FatalError("a function returned a result with an error set");
63 #endif
64             return NULL;
65         }
66     }
67     return result;
68 }
69 
70 
71 /* --- Core PyObject call functions ------------------------------- */
72 
73 PyObject *
_PyObject_FastCallDict(PyObject * callable,PyObject * const * args,size_t nargsf,PyObject * kwargs)74 _PyObject_FastCallDict(PyObject *callable, PyObject *const *args,
75                        size_t nargsf, PyObject *kwargs)
76 {
77     /* _PyObject_FastCallDict() must not be called with an exception set,
78        because it can clear it (directly or indirectly) and so the
79        caller loses its exception */
80     assert(!PyErr_Occurred());
81     assert(callable != NULL);
82 
83     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
84     assert(nargs >= 0);
85     assert(nargs == 0 || args != NULL);
86     assert(kwargs == NULL || PyDict_Check(kwargs));
87 
88     vectorcallfunc func = _PyVectorcall_Function(callable);
89     if (func == NULL) {
90         /* Use tp_call instead */
91         return _PyObject_MakeTpCall(callable, args, nargs, kwargs);
92     }
93 
94     PyObject *res;
95     if (kwargs == NULL) {
96         res = func(callable, args, nargsf, NULL);
97     }
98     else {
99         PyObject *kwnames;
100         PyObject *const *newargs;
101         if (_PyStack_UnpackDict(args, nargs, kwargs, &newargs, &kwnames) < 0) {
102             return NULL;
103         }
104         res = func(callable, newargs, nargs, kwnames);
105         if (kwnames != NULL) {
106             Py_ssize_t i, n = PyTuple_GET_SIZE(kwnames) + nargs;
107             for (i = 0; i < n; i++) {
108                 Py_DECREF(newargs[i]);
109             }
110             PyMem_Free((PyObject **)newargs);
111             Py_DECREF(kwnames);
112         }
113     }
114     return _Py_CheckFunctionResult(callable, res, NULL);
115 }
116 
117 
118 PyObject *
_PyObject_MakeTpCall(PyObject * callable,PyObject * const * args,Py_ssize_t nargs,PyObject * keywords)119 _PyObject_MakeTpCall(PyObject *callable, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
120 {
121     /* Slow path: build a temporary tuple for positional arguments and a
122      * temporary dictionary for keyword arguments (if any) */
123     ternaryfunc call = Py_TYPE(callable)->tp_call;
124     if (call == NULL) {
125         PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
126                      Py_TYPE(callable)->tp_name);
127         return NULL;
128     }
129 
130     assert(nargs >= 0);
131     assert(nargs == 0 || args != NULL);
132     assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));
133     PyObject *argstuple = _PyTuple_FromArray(args, nargs);
134     if (argstuple == NULL) {
135         return NULL;
136     }
137 
138     PyObject *kwdict;
139     if (keywords == NULL || PyDict_Check(keywords)) {
140         kwdict = keywords;
141     }
142     else {
143         if (PyTuple_GET_SIZE(keywords)) {
144             assert(args != NULL);
145             kwdict = _PyStack_AsDict(args + nargs, keywords);
146             if (kwdict == NULL) {
147                 Py_DECREF(argstuple);
148                 return NULL;
149             }
150         }
151         else {
152             keywords = kwdict = NULL;
153         }
154     }
155 
156     PyObject *result = NULL;
157     if (Py_EnterRecursiveCall(" while calling a Python object") == 0)
158     {
159         result = call(callable, argstuple, kwdict);
160         Py_LeaveRecursiveCall();
161     }
162 
163     Py_DECREF(argstuple);
164     if (kwdict != keywords) {
165         Py_DECREF(kwdict);
166     }
167 
168     result = _Py_CheckFunctionResult(callable, result, NULL);
169     return result;
170 }
171 
172 
173 PyObject *
PyVectorcall_Call(PyObject * callable,PyObject * tuple,PyObject * kwargs)174 PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
175 {
176     /* get vectorcallfunc as in _PyVectorcall_Function, but without
177      * the _Py_TPFLAGS_HAVE_VECTORCALL check */
178     Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
179     if (offset <= 0) {
180         PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
181                      Py_TYPE(callable)->tp_name);
182         return NULL;
183     }
184     vectorcallfunc func = *(vectorcallfunc *)(((char *)callable) + offset);
185     if (func == NULL) {
186         PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall",
187                      Py_TYPE(callable)->tp_name);
188         return NULL;
189     }
190 
191     /* Convert arguments & call */
192     PyObject *const *args;
193     Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);
194     PyObject *kwnames;
195     if (_PyStack_UnpackDict(_PyTuple_ITEMS(tuple), nargs,
196         kwargs, &args, &kwnames) < 0) {
197         return NULL;
198     }
199     PyObject *result = func(callable, args, nargs, kwnames);
200     if (kwnames != NULL) {
201         Py_ssize_t i, n = PyTuple_GET_SIZE(kwnames) + nargs;
202         for (i = 0; i < n; i++) {
203             Py_DECREF(args[i]);
204         }
205         PyMem_Free((PyObject **)args);
206         Py_DECREF(kwnames);
207     }
208 
209     return _Py_CheckFunctionResult(callable, result, NULL);
210 }
211 
212 
213 PyObject *
PyObject_Call(PyObject * callable,PyObject * args,PyObject * kwargs)214 PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
215 {
216     ternaryfunc call;
217     PyObject *result;
218 
219     /* PyObject_Call() must not be called with an exception set,
220        because it can clear it (directly or indirectly) and so the
221        caller loses its exception */
222     assert(!PyErr_Occurred());
223     assert(PyTuple_Check(args));
224     assert(kwargs == NULL || PyDict_Check(kwargs));
225 
226     if (_PyVectorcall_Function(callable) != NULL) {
227         return PyVectorcall_Call(callable, args, kwargs);
228     }
229     else if (PyCFunction_Check(callable)) {
230         /* This must be a METH_VARARGS function, otherwise we would be
231          * in the previous case */
232         return cfunction_call_varargs(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_ITEMS(argdefs);
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_ITEMS(kwtuple);
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         assert(i / 2 == nk);
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_ITEMS(argdefs);
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 
386 PyObject *
_PyFunction_Vectorcall(PyObject * func,PyObject * const * stack,size_t nargsf,PyObject * kwnames)387 _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
388                        size_t nargsf, PyObject *kwnames)
389 {
390     PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
391     PyObject *globals = PyFunction_GET_GLOBALS(func);
392     PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
393     PyObject *kwdefs, *closure, *name, *qualname;
394     PyObject **d;
395     Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
396     Py_ssize_t nd;
397 
398     assert(PyFunction_Check(func));
399     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
400     assert(nargs >= 0);
401     assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
402     assert((nargs == 0 && nkwargs == 0) || stack != NULL);
403     /* kwnames must only contains str strings, no subclass, and all keys must
404        be unique */
405 
406     if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
407         (co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
408     {
409         if (argdefs == NULL && co->co_argcount == nargs) {
410             return function_code_fastcall(co, stack, nargs, globals);
411         }
412         else if (nargs == 0 && argdefs != NULL
413                  && co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
414             /* function called with no arguments, but all parameters have
415                a default value: use default values as arguments .*/
416             stack = _PyTuple_ITEMS(argdefs);
417             return function_code_fastcall(co, stack, PyTuple_GET_SIZE(argdefs),
418                                           globals);
419         }
420     }
421 
422     kwdefs = PyFunction_GET_KW_DEFAULTS(func);
423     closure = PyFunction_GET_CLOSURE(func);
424     name = ((PyFunctionObject *)func) -> func_name;
425     qualname = ((PyFunctionObject *)func) -> func_qualname;
426 
427     if (argdefs != NULL) {
428         d = _PyTuple_ITEMS(argdefs);
429         nd = PyTuple_GET_SIZE(argdefs);
430     }
431     else {
432         d = NULL;
433         nd = 0;
434     }
435     return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
436                                     stack, nargs,
437                                     nkwargs ? _PyTuple_ITEMS(kwnames) : NULL,
438                                     stack + nargs,
439                                     nkwargs, 1,
440                                     d, (int)nd, kwdefs,
441                                     closure, name, qualname);
442 }
443 
444 
445 /* --- PyCFunction call functions --------------------------------- */
446 
447 PyObject *
_PyMethodDef_RawFastCallDict(PyMethodDef * method,PyObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwargs)448 _PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self,
449                              PyObject *const *args, Py_ssize_t nargs,
450                              PyObject *kwargs)
451 {
452     /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
453        because it can clear it (directly or indirectly) and so the
454        caller loses its exception */
455     assert(!PyErr_Occurred());
456 
457     assert(method != NULL);
458     assert(nargs >= 0);
459     assert(nargs == 0 || args != NULL);
460     assert(kwargs == NULL || PyDict_Check(kwargs));
461 
462     PyCFunction meth = method->ml_meth;
463     int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
464     PyObject *result = NULL;
465 
466     if (Py_EnterRecursiveCall(" while calling a Python object")) {
467         return NULL;
468     }
469 
470     switch (flags)
471     {
472     case METH_NOARGS:
473         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
474             goto no_keyword_error;
475         }
476 
477         if (nargs != 0) {
478             PyErr_Format(PyExc_TypeError,
479                 "%.200s() takes no arguments (%zd given)",
480                 method->ml_name, nargs);
481             goto exit;
482         }
483 
484         result = (*meth) (self, NULL);
485         break;
486 
487     case METH_O:
488         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
489             goto no_keyword_error;
490         }
491 
492         if (nargs != 1) {
493             PyErr_Format(PyExc_TypeError,
494                 "%.200s() takes exactly one argument (%zd given)",
495                 method->ml_name, nargs);
496             goto exit;
497         }
498 
499         result = (*meth) (self, args[0]);
500         break;
501 
502     case METH_VARARGS:
503         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
504             goto no_keyword_error;
505         }
506         /* fall through */
507 
508     case METH_VARARGS | METH_KEYWORDS:
509     {
510         /* Slow-path: create a temporary tuple for positional arguments */
511         PyObject *argstuple = _PyTuple_FromArray(args, nargs);
512         if (argstuple == NULL) {
513             goto exit;
514         }
515 
516         if (flags & METH_KEYWORDS) {
517             result = (*(PyCFunctionWithKeywords)(void(*)(void))meth) (self, argstuple, kwargs);
518         }
519         else {
520             result = (*meth) (self, argstuple);
521         }
522         Py_DECREF(argstuple);
523         break;
524     }
525 
526     case METH_FASTCALL:
527     {
528         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
529             goto no_keyword_error;
530         }
531 
532         result = (*(_PyCFunctionFast)(void(*)(void))meth) (self, args, nargs);
533         break;
534     }
535 
536     case METH_FASTCALL | METH_KEYWORDS:
537     {
538         PyObject *const *stack;
539         PyObject *kwnames;
540         _PyCFunctionFastWithKeywords fastmeth = (_PyCFunctionFastWithKeywords)(void(*)(void))meth;
541 
542         if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
543             goto exit;
544         }
545 
546         result = (*fastmeth) (self, stack, nargs, kwnames);
547         if (kwnames != NULL) {
548             Py_ssize_t i, n = nargs + PyTuple_GET_SIZE(kwnames);
549             for (i = 0; i < n; i++) {
550                 Py_DECREF(stack[i]);
551             }
552             PyMem_Free((PyObject **)stack);
553             Py_DECREF(kwnames);
554         }
555         break;
556     }
557 
558     default:
559         PyErr_SetString(PyExc_SystemError,
560                         "Bad call flags in _PyMethodDef_RawFastCallDict. "
561                         "METH_OLDARGS is no longer supported!");
562         goto exit;
563     }
564 
565     goto exit;
566 
567 no_keyword_error:
568     PyErr_Format(PyExc_TypeError,
569                  "%.200s() takes no keyword arguments",
570                  method->ml_name);
571 
572 exit:
573     Py_LeaveRecursiveCall();
574     return result;
575 }
576 
577 
578 PyObject *
_PyCFunction_FastCallDict(PyObject * func,PyObject * const * args,Py_ssize_t nargs,PyObject * kwargs)579 _PyCFunction_FastCallDict(PyObject *func,
580                           PyObject *const *args, Py_ssize_t nargs,
581                           PyObject *kwargs)
582 {
583     PyObject *result;
584 
585     assert(func != NULL);
586     assert(PyCFunction_Check(func));
587 
588     result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
589                                           PyCFunction_GET_SELF(func),
590                                           args, nargs, kwargs);
591     result = _Py_CheckFunctionResult(func, result, NULL);
592     return result;
593 }
594 
595 
596 PyObject *
_PyMethodDef_RawFastCallKeywords(PyMethodDef * method,PyObject * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)597 _PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self,
598                                  PyObject *const *args, Py_ssize_t nargs,
599                                  PyObject *kwnames)
600 {
601     /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
602        because it can clear it (directly or indirectly) and so the
603        caller loses its exception */
604     assert(!PyErr_Occurred());
605 
606     assert(method != NULL);
607     assert(nargs >= 0);
608     assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
609     /* kwnames must only contains str strings, no subclass, and all keys must
610        be unique */
611 
612     PyCFunction meth = method->ml_meth;
613     int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
614     Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames);
615     PyObject *result = NULL;
616 
617     if (Py_EnterRecursiveCall(" while calling a Python object")) {
618         return NULL;
619     }
620 
621     switch (flags)
622     {
623     case METH_NOARGS:
624         if (nkwargs) {
625             goto no_keyword_error;
626         }
627 
628         if (nargs != 0) {
629             PyErr_Format(PyExc_TypeError,
630                 "%.200s() takes no arguments (%zd given)",
631                 method->ml_name, nargs);
632             goto exit;
633         }
634 
635         result = (*meth) (self, NULL);
636         break;
637 
638     case METH_O:
639         if (nkwargs) {
640             goto no_keyword_error;
641         }
642 
643         if (nargs != 1) {
644             PyErr_Format(PyExc_TypeError,
645                 "%.200s() takes exactly one argument (%zd given)",
646                 method->ml_name, nargs);
647             goto exit;
648         }
649 
650         result = (*meth) (self, args[0]);
651         break;
652 
653     case METH_FASTCALL:
654         if (nkwargs) {
655             goto no_keyword_error;
656         }
657         result = ((_PyCFunctionFast)(void(*)(void))meth) (self, args, nargs);
658         break;
659 
660     case METH_FASTCALL | METH_KEYWORDS:
661         /* Fast-path: avoid temporary dict to pass keyword arguments */
662         result = ((_PyCFunctionFastWithKeywords)(void(*)(void))meth) (self, args, nargs, kwnames);
663         break;
664 
665     case METH_VARARGS:
666         if (nkwargs) {
667             goto no_keyword_error;
668         }
669         /* fall through */
670 
671     case METH_VARARGS | METH_KEYWORDS:
672     {
673         /* Slow-path: create a temporary tuple for positional arguments
674            and a temporary dict for keyword arguments */
675         PyObject *argtuple;
676 
677         argtuple = _PyTuple_FromArray(args, nargs);
678         if (argtuple == NULL) {
679             goto exit;
680         }
681 
682         if (flags & METH_KEYWORDS) {
683             PyObject *kwdict;
684 
685             if (nkwargs > 0) {
686                 kwdict = _PyStack_AsDict(args + nargs, kwnames);
687                 if (kwdict == NULL) {
688                     Py_DECREF(argtuple);
689                     goto exit;
690                 }
691             }
692             else {
693                 kwdict = NULL;
694             }
695 
696             result = (*(PyCFunctionWithKeywords)(void(*)(void))meth) (self, argtuple, kwdict);
697             Py_XDECREF(kwdict);
698         }
699         else {
700             result = (*meth) (self, argtuple);
701         }
702         Py_DECREF(argtuple);
703         break;
704     }
705 
706     default:
707         PyErr_SetString(PyExc_SystemError,
708                         "Bad call flags in _PyMethodDef_RawFastCallKeywords. "
709                         "METH_OLDARGS is no longer supported!");
710         goto exit;
711     }
712 
713     goto exit;
714 
715 no_keyword_error:
716     PyErr_Format(PyExc_TypeError,
717                  "%.200s() takes no keyword arguments",
718                  method->ml_name);
719 
720 exit:
721     Py_LeaveRecursiveCall();
722     return result;
723 }
724 
725 
726 static PyObject *
cfunction_call_varargs(PyObject * func,PyObject * args,PyObject * kwargs)727 cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs)
728 {
729     assert(!PyErr_Occurred());
730     assert(kwargs == NULL || PyDict_Check(kwargs));
731 
732     PyCFunction meth = PyCFunction_GET_FUNCTION(func);
733     PyObject *self = PyCFunction_GET_SELF(func);
734     PyObject *result;
735 
736     assert(PyCFunction_GET_FLAGS(func) & METH_VARARGS);
737     if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) {
738         if (Py_EnterRecursiveCall(" while calling a Python object")) {
739             return NULL;
740         }
741 
742         result = (*(PyCFunctionWithKeywords)(void(*)(void))meth)(self, args, kwargs);
743 
744         Py_LeaveRecursiveCall();
745     }
746     else {
747         if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
748             PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
749                          ((PyCFunctionObject*)func)->m_ml->ml_name);
750             return NULL;
751         }
752 
753         if (Py_EnterRecursiveCall(" while calling a Python object")) {
754             return NULL;
755         }
756 
757         result = (*meth)(self, args);
758 
759         Py_LeaveRecursiveCall();
760     }
761 
762     return _Py_CheckFunctionResult(func, result, NULL);
763 }
764 
765 
766 PyObject *
PyCFunction_Call(PyObject * func,PyObject * args,PyObject * kwargs)767 PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
768 {
769     /* For METH_VARARGS, we cannot use vectorcall as the vectorcall pointer
770      * is NULL. This is intentional, since vectorcall would be slower. */
771     if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) {
772         return cfunction_call_varargs(func, args, kwargs);
773     }
774     return PyVectorcall_Call(func, args, kwargs);
775 }
776 
777 
778 /* --- More complex call functions -------------------------------- */
779 
780 /* External interface to call any callable object.
781    The args must be a tuple or NULL.  The kwargs must be a dict or NULL. */
782 PyObject *
PyEval_CallObjectWithKeywords(PyObject * callable,PyObject * args,PyObject * kwargs)783 PyEval_CallObjectWithKeywords(PyObject *callable,
784                               PyObject *args, PyObject *kwargs)
785 {
786 #ifdef Py_DEBUG
787     /* PyEval_CallObjectWithKeywords() must not be called with an exception
788        set. It raises a new exception if parameters are invalid or if
789        PyTuple_New() fails, and so the original exception is lost. */
790     assert(!PyErr_Occurred());
791 #endif
792 
793     if (args != NULL && !PyTuple_Check(args)) {
794         PyErr_SetString(PyExc_TypeError,
795                         "argument list must be a tuple");
796         return NULL;
797     }
798 
799     if (kwargs != NULL && !PyDict_Check(kwargs)) {
800         PyErr_SetString(PyExc_TypeError,
801                         "keyword list must be a dictionary");
802         return NULL;
803     }
804 
805     if (args == NULL) {
806         return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
807     }
808     else {
809         return PyObject_Call(callable, args, kwargs);
810     }
811 }
812 
813 
814 PyObject *
PyObject_CallObject(PyObject * callable,PyObject * args)815 PyObject_CallObject(PyObject *callable, PyObject *args)
816 {
817     return PyEval_CallObjectWithKeywords(callable, args, NULL);
818 }
819 
820 
821 /* Positional arguments are obj followed by args:
822    call callable(obj, *args, **kwargs) */
823 PyObject *
_PyObject_FastCall_Prepend(PyObject * callable,PyObject * obj,PyObject * const * args,Py_ssize_t nargs)824 _PyObject_FastCall_Prepend(PyObject *callable, PyObject *obj,
825                            PyObject *const *args, Py_ssize_t nargs)
826 {
827     PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
828     PyObject **args2;
829     PyObject *result;
830 
831     nargs++;
832     if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
833         args2 = small_stack;
834     }
835     else {
836         args2 = PyMem_Malloc(nargs * sizeof(PyObject *));
837         if (args2 == NULL) {
838             PyErr_NoMemory();
839             return NULL;
840         }
841     }
842 
843     /* use borrowed references */
844     args2[0] = obj;
845     if (nargs > 1) {
846         memcpy(&args2[1], args, (nargs - 1) * sizeof(PyObject *));
847     }
848 
849     result = _PyObject_FastCall(callable, args2, nargs);
850     if (args2 != small_stack) {
851         PyMem_Free(args2);
852     }
853     return result;
854 }
855 
856 
857 /* Call callable(obj, *args, **kwargs). */
858 PyObject *
_PyObject_Call_Prepend(PyObject * callable,PyObject * obj,PyObject * args,PyObject * kwargs)859 _PyObject_Call_Prepend(PyObject *callable,
860                        PyObject *obj, PyObject *args, PyObject *kwargs)
861 {
862     PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
863     PyObject **stack;
864     Py_ssize_t argcount;
865     PyObject *result;
866 
867     assert(PyTuple_Check(args));
868 
869     argcount = PyTuple_GET_SIZE(args);
870     if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
871         stack = small_stack;
872     }
873     else {
874         stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
875         if (stack == NULL) {
876             PyErr_NoMemory();
877             return NULL;
878         }
879     }
880 
881     /* use borrowed references */
882     stack[0] = obj;
883     memcpy(&stack[1],
884            _PyTuple_ITEMS(args),
885            argcount * sizeof(PyObject *));
886 
887     result = _PyObject_FastCallDict(callable,
888                                     stack, argcount + 1,
889                                     kwargs);
890     if (stack != small_stack) {
891         PyMem_Free(stack);
892     }
893     return result;
894 }
895 
896 
897 /* --- Call with a format string ---------------------------------- */
898 
899 static PyObject *
_PyObject_CallFunctionVa(PyObject * callable,const char * format,va_list va,int is_size_t)900 _PyObject_CallFunctionVa(PyObject *callable, const char *format,
901                          va_list va, int is_size_t)
902 {
903     PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
904     const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
905     PyObject **stack;
906     Py_ssize_t nargs, i;
907     PyObject *result;
908 
909     if (callable == NULL) {
910         return null_error();
911     }
912 
913     if (!format || !*format) {
914         return _PyObject_CallNoArg(callable);
915     }
916 
917     if (is_size_t) {
918         stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
919                                        format, va, &nargs);
920     }
921     else {
922         stack = _Py_VaBuildStack(small_stack, small_stack_len,
923                                  format, va, &nargs);
924     }
925     if (stack == NULL) {
926         return NULL;
927     }
928 
929     if (nargs == 1 && PyTuple_Check(stack[0])) {
930         /* Special cases for backward compatibility:
931            - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
932            - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
933              func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
934         PyObject *args = stack[0];
935         result = _PyObject_FastCall(callable,
936                                     _PyTuple_ITEMS(args),
937                                     PyTuple_GET_SIZE(args));
938     }
939     else {
940         result = _PyObject_FastCall(callable, stack, nargs);
941     }
942 
943     for (i = 0; i < nargs; ++i) {
944         Py_DECREF(stack[i]);
945     }
946     if (stack != small_stack) {
947         PyMem_Free(stack);
948     }
949     return result;
950 }
951 
952 
953 PyObject *
PyObject_CallFunction(PyObject * callable,const char * format,...)954 PyObject_CallFunction(PyObject *callable, const char *format, ...)
955 {
956     va_list va;
957     PyObject *result;
958 
959     va_start(va, format);
960     result = _PyObject_CallFunctionVa(callable, format, va, 0);
961     va_end(va);
962 
963     return result;
964 }
965 
966 
967 /* PyEval_CallFunction is exact copy of PyObject_CallFunction.
968  * This function is kept for backward compatibility.
969  */
970 PyObject *
PyEval_CallFunction(PyObject * callable,const char * format,...)971 PyEval_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 PyObject *
_PyObject_CallFunction_SizeT(PyObject * callable,const char * format,...)985 _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
986 {
987     va_list va;
988     PyObject *result;
989 
990     va_start(va, format);
991     result = _PyObject_CallFunctionVa(callable, format, va, 1);
992     va_end(va);
993 
994     return result;
995 }
996 
997 
998 static PyObject*
callmethod(PyObject * callable,const char * format,va_list va,int is_size_t)999 callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
1000 {
1001     assert(callable != NULL);
1002 
1003     if (!PyCallable_Check(callable)) {
1004         PyErr_Format(PyExc_TypeError,
1005                      "attribute of type '%.200s' is not callable",
1006                      Py_TYPE(callable)->tp_name);
1007         return NULL;
1008     }
1009 
1010     return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
1011 }
1012 
1013 
1014 PyObject *
PyObject_CallMethod(PyObject * obj,const char * name,const char * format,...)1015 PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
1016 {
1017     va_list va;
1018     PyObject *callable, *retval;
1019 
1020     if (obj == NULL || name == NULL) {
1021         return null_error();
1022     }
1023 
1024     callable = PyObject_GetAttrString(obj, name);
1025     if (callable == NULL)
1026         return NULL;
1027 
1028     va_start(va, format);
1029     retval = callmethod(callable, format, va, 0);
1030     va_end(va);
1031 
1032     Py_DECREF(callable);
1033     return retval;
1034 }
1035 
1036 
1037 /* PyEval_CallMethod is exact copy of PyObject_CallMethod.
1038  * This function is kept for backward compatibility.
1039  */
1040 PyObject *
PyEval_CallMethod(PyObject * obj,const char * name,const char * format,...)1041 PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
1042 {
1043     va_list va;
1044     PyObject *callable, *retval;
1045 
1046     if (obj == NULL || name == NULL) {
1047         return null_error();
1048     }
1049 
1050     callable = PyObject_GetAttrString(obj, name);
1051     if (callable == NULL)
1052         return NULL;
1053 
1054     va_start(va, format);
1055     retval = callmethod(callable, format, va, 0);
1056     va_end(va);
1057 
1058     Py_DECREF(callable);
1059     return retval;
1060 }
1061 
1062 
1063 PyObject *
_PyObject_CallMethodId(PyObject * obj,_Py_Identifier * name,const char * format,...)1064 _PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
1065                        const char *format, ...)
1066 {
1067     va_list va;
1068     PyObject *callable, *retval;
1069 
1070     if (obj == NULL || name == NULL) {
1071         return null_error();
1072     }
1073 
1074     callable = _PyObject_GetAttrId(obj, name);
1075     if (callable == NULL)
1076         return NULL;
1077 
1078     va_start(va, format);
1079     retval = callmethod(callable, format, va, 0);
1080     va_end(va);
1081 
1082     Py_DECREF(callable);
1083     return retval;
1084 }
1085 
1086 
1087 PyObject *
_PyObject_CallMethod_SizeT(PyObject * obj,const char * name,const char * format,...)1088 _PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
1089                            const char *format, ...)
1090 {
1091     va_list va;
1092     PyObject *callable, *retval;
1093 
1094     if (obj == NULL || name == NULL) {
1095         return null_error();
1096     }
1097 
1098     callable = PyObject_GetAttrString(obj, name);
1099     if (callable == NULL)
1100         return NULL;
1101 
1102     va_start(va, format);
1103     retval = callmethod(callable, format, va, 1);
1104     va_end(va);
1105 
1106     Py_DECREF(callable);
1107     return retval;
1108 }
1109 
1110 
1111 PyObject *
_PyObject_CallMethodId_SizeT(PyObject * obj,_Py_Identifier * name,const char * format,...)1112 _PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
1113                              const char *format, ...)
1114 {
1115     va_list va;
1116     PyObject *callable, *retval;
1117 
1118     if (obj == NULL || name == NULL) {
1119         return null_error();
1120     }
1121 
1122     callable = _PyObject_GetAttrId(obj, name);
1123     if (callable == NULL) {
1124         return NULL;
1125     }
1126 
1127     va_start(va, format);
1128     retval = callmethod(callable, format, va, 1);
1129     va_end(va);
1130 
1131     Py_DECREF(callable);
1132     return retval;
1133 }
1134 
1135 
1136 /* --- Call with "..." arguments ---------------------------------- */
1137 
1138 static PyObject *
object_vacall(PyObject * base,PyObject * callable,va_list vargs)1139 object_vacall(PyObject *base, PyObject *callable, va_list vargs)
1140 {
1141     PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
1142     PyObject **stack;
1143     Py_ssize_t nargs;
1144     PyObject *result;
1145     Py_ssize_t i;
1146     va_list countva;
1147 
1148     if (callable == NULL) {
1149         return null_error();
1150     }
1151 
1152     /* Count the number of arguments */
1153     va_copy(countva, vargs);
1154     nargs = base ? 1 : 0;
1155     while (1) {
1156         PyObject *arg = va_arg(countva, PyObject *);
1157         if (arg == NULL) {
1158             break;
1159         }
1160         nargs++;
1161     }
1162     va_end(countva);
1163 
1164     /* Copy arguments */
1165     if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1166         stack = small_stack;
1167     }
1168     else {
1169         stack = PyMem_Malloc(nargs * sizeof(stack[0]));
1170         if (stack == NULL) {
1171             PyErr_NoMemory();
1172             return NULL;
1173         }
1174     }
1175 
1176     i = 0;
1177     if (base) {
1178         stack[i++] = base;
1179     }
1180 
1181     for (; i < nargs; ++i) {
1182         stack[i] = va_arg(vargs, PyObject *);
1183     }
1184 
1185     /* Call the function */
1186     result = _PyObject_FastCall(callable, stack, nargs);
1187 
1188     if (stack != small_stack) {
1189         PyMem_Free(stack);
1190     }
1191     return result;
1192 }
1193 
1194 
1195 /* Private API for the LOAD_METHOD opcode. */
1196 extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
1197 
1198 PyObject *
PyObject_CallMethodObjArgs(PyObject * obj,PyObject * name,...)1199 PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
1200 {
1201     if (obj == NULL || name == NULL) {
1202         return null_error();
1203     }
1204 
1205     PyObject *callable = NULL;
1206     int is_method = _PyObject_GetMethod(obj, name, &callable);
1207     if (callable == NULL) {
1208         return NULL;
1209     }
1210     obj = is_method ? obj : NULL;
1211 
1212     va_list vargs;
1213     va_start(vargs, name);
1214     PyObject *result = object_vacall(obj, callable, vargs);
1215     va_end(vargs);
1216 
1217     Py_DECREF(callable);
1218     return result;
1219 }
1220 
1221 
1222 PyObject *
_PyObject_CallMethodIdObjArgs(PyObject * obj,struct _Py_Identifier * name,...)1223 _PyObject_CallMethodIdObjArgs(PyObject *obj,
1224                               struct _Py_Identifier *name, ...)
1225 {
1226     if (obj == NULL || name == NULL) {
1227         return null_error();
1228     }
1229 
1230     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
1231     if (!oname) {
1232         return NULL;
1233     }
1234 
1235     PyObject *callable = NULL;
1236     int is_method = _PyObject_GetMethod(obj, oname, &callable);
1237     if (callable == NULL) {
1238         return NULL;
1239     }
1240     obj = is_method ? obj : NULL;
1241 
1242     va_list vargs;
1243     va_start(vargs, name);
1244     PyObject *result = object_vacall(obj, callable, vargs);
1245     va_end(vargs);
1246 
1247     Py_DECREF(callable);
1248     return result;
1249 }
1250 
1251 
1252 PyObject *
PyObject_CallFunctionObjArgs(PyObject * callable,...)1253 PyObject_CallFunctionObjArgs(PyObject *callable, ...)
1254 {
1255     va_list vargs;
1256     PyObject *result;
1257 
1258     va_start(vargs, callable);
1259     result = object_vacall(NULL, callable, vargs);
1260     va_end(vargs);
1261 
1262     return result;
1263 }
1264 
1265 
1266 /* --- PyStack functions ------------------------------------------ */
1267 
1268 PyObject *
_PyStack_AsDict(PyObject * const * values,PyObject * kwnames)1269 _PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
1270 {
1271     Py_ssize_t nkwargs;
1272     PyObject *kwdict;
1273     Py_ssize_t i;
1274 
1275     assert(kwnames != NULL);
1276     nkwargs = PyTuple_GET_SIZE(kwnames);
1277     kwdict = _PyDict_NewPresized(nkwargs);
1278     if (kwdict == NULL) {
1279         return NULL;
1280     }
1281 
1282     for (i = 0; i < nkwargs; i++) {
1283         PyObject *key = PyTuple_GET_ITEM(kwnames, i);
1284         PyObject *value = *values++;
1285         /* If key already exists, replace it with the new value */
1286         if (PyDict_SetItem(kwdict, key, value)) {
1287             Py_DECREF(kwdict);
1288             return NULL;
1289         }
1290     }
1291     return kwdict;
1292 }
1293 
1294 
1295 int
_PyStack_UnpackDict(PyObject * const * args,Py_ssize_t nargs,PyObject * kwargs,PyObject * const ** p_stack,PyObject ** p_kwnames)1296 _PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
1297                     PyObject *const **p_stack, PyObject **p_kwnames)
1298 {
1299     PyObject **stack, **kwstack;
1300     Py_ssize_t nkwargs;
1301     Py_ssize_t pos, i;
1302     PyObject *key, *value;
1303     PyObject *kwnames;
1304 
1305     assert(nargs >= 0);
1306     assert(kwargs == NULL || PyDict_CheckExact(kwargs));
1307 
1308     if (kwargs == NULL || (nkwargs = PyDict_GET_SIZE(kwargs)) == 0) {
1309         *p_stack = args;
1310         *p_kwnames = NULL;
1311         return 0;
1312     }
1313 
1314     if ((size_t)nargs > PY_SSIZE_T_MAX / sizeof(stack[0]) - (size_t)nkwargs) {
1315         PyErr_NoMemory();
1316         return -1;
1317     }
1318 
1319     stack = PyMem_Malloc((nargs + nkwargs) * sizeof(stack[0]));
1320     if (stack == NULL) {
1321         PyErr_NoMemory();
1322         return -1;
1323     }
1324 
1325     kwnames = PyTuple_New(nkwargs);
1326     if (kwnames == NULL) {
1327         PyMem_Free(stack);
1328         return -1;
1329     }
1330 
1331     /* Copy positional arguments */
1332     for (i = 0; i < nargs; i++) {
1333         Py_INCREF(args[i]);
1334         stack[i] = args[i];
1335     }
1336 
1337     kwstack = stack + nargs;
1338     pos = i = 0;
1339     /* This loop doesn't support lookup function mutating the dictionary
1340        to change its size. It's a deliberate choice for speed, this function is
1341        called in the performance critical hot code. */
1342     while (PyDict_Next(kwargs, &pos, &key, &value)) {
1343         Py_INCREF(key);
1344         Py_INCREF(value);
1345         PyTuple_SET_ITEM(kwnames, i, key);
1346         kwstack[i] = value;
1347         i++;
1348     }
1349 
1350     *p_stack = stack;
1351     *p_kwnames = kwnames;
1352     return 0;
1353 }
1354