1 #include "Python.h"
2 #include "pycore_call.h"
3 #include "pycore_ceval.h"        // _PyEval_EvalFrame()
4 #include "pycore_object.h"
5 #include "pycore_pyerrors.h"
6 #include "pycore_pystate.h"      // _PyThreadState_GET()
7 #include "pycore_tupleobject.h"
8 #include "frameobject.h"
9 
10 
11 static PyObject *const *
12 _PyStack_UnpackDict(PyThreadState *tstate,
13                     PyObject *const *args, Py_ssize_t nargs,
14                     PyObject *kwargs, PyObject **p_kwnames);
15 
16 static void
17 _PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
18                          PyObject *kwnames);
19 
20 
21 static PyObject *
null_error(PyThreadState * tstate)22 null_error(PyThreadState *tstate)
23 {
24     if (!_PyErr_Occurred(tstate)) {
25         _PyErr_SetString(tstate, PyExc_SystemError,
26                          "null argument to internal routine");
27     }
28     return NULL;
29 }
30 
31 
32 PyObject*
_Py_CheckFunctionResult(PyThreadState * tstate,PyObject * callable,PyObject * result,const char * where)33 _Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
34                         PyObject *result, const char *where)
35 {
36     assert((callable != NULL) ^ (where != NULL));
37 
38     if (result == NULL) {
39         if (!_PyErr_Occurred(tstate)) {
40             if (callable)
41                 _PyErr_Format(tstate, PyExc_SystemError,
42                               "%R returned NULL without setting an error",
43                               callable);
44             else
45                 _PyErr_Format(tstate, PyExc_SystemError,
46                               "%s returned NULL without setting an error",
47                               where);
48 #ifdef Py_DEBUG
49             /* Ensure that the bug is caught in debug mode.
50                Py_FatalError() logs the SystemError exception raised above. */
51             Py_FatalError("a function returned NULL without setting an error");
52 #endif
53             return NULL;
54         }
55     }
56     else {
57         if (_PyErr_Occurred(tstate)) {
58             Py_DECREF(result);
59 
60             if (callable) {
61                 _PyErr_FormatFromCauseTstate(
62                     tstate, PyExc_SystemError,
63                     "%R returned a result with an error set", callable);
64             }
65             else {
66                 _PyErr_FormatFromCauseTstate(
67                     tstate, PyExc_SystemError,
68                     "%s returned a result with an error set", where);
69             }
70 #ifdef Py_DEBUG
71             /* Ensure that the bug is caught in debug mode.
72                Py_FatalError() logs the SystemError exception raised above. */
73             Py_FatalError("a function returned a result with an error set");
74 #endif
75             return NULL;
76         }
77     }
78     return result;
79 }
80 
81 
82 /* --- Core PyObject call functions ------------------------------- */
83 
84 /* Call a callable Python object without any arguments */
85 PyObject *
PyObject_CallNoArgs(PyObject * func)86 PyObject_CallNoArgs(PyObject *func)
87 {
88     PyThreadState *tstate = _PyThreadState_GET();
89     return _PyObject_CallNoArgTstate(tstate, func);
90 }
91 
92 
93 PyObject *
_PyObject_FastCallDictTstate(PyThreadState * tstate,PyObject * callable,PyObject * const * args,size_t nargsf,PyObject * kwargs)94 _PyObject_FastCallDictTstate(PyThreadState *tstate, PyObject *callable,
95                              PyObject *const *args, size_t nargsf,
96                              PyObject *kwargs)
97 {
98     assert(callable != NULL);
99 
100     /* PyObject_VectorcallDict() must not be called with an exception set,
101        because it can clear it (directly or indirectly) and so the
102        caller loses its exception */
103     assert(!_PyErr_Occurred(tstate));
104 
105     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
106     assert(nargs >= 0);
107     assert(nargs == 0 || args != NULL);
108     assert(kwargs == NULL || PyDict_Check(kwargs));
109 
110     vectorcallfunc func = PyVectorcall_Function(callable);
111     if (func == NULL) {
112         /* Use tp_call instead */
113         return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwargs);
114     }
115 
116     PyObject *res;
117     if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
118         res = func(callable, args, nargsf, NULL);
119     }
120     else {
121         PyObject *kwnames;
122         PyObject *const *newargs;
123         newargs = _PyStack_UnpackDict(tstate,
124                                       args, nargs,
125                                       kwargs, &kwnames);
126         if (newargs == NULL) {
127             return NULL;
128         }
129         res = func(callable, newargs,
130                    nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
131         _PyStack_UnpackDict_Free(newargs, nargs, kwnames);
132     }
133     return _Py_CheckFunctionResult(tstate, callable, res, NULL);
134 }
135 
136 
137 PyObject *
PyObject_VectorcallDict(PyObject * callable,PyObject * const * args,size_t nargsf,PyObject * kwargs)138 PyObject_VectorcallDict(PyObject *callable, PyObject *const *args,
139                        size_t nargsf, PyObject *kwargs)
140 {
141     PyThreadState *tstate = _PyThreadState_GET();
142     return _PyObject_FastCallDictTstate(tstate, callable, args, nargsf, kwargs);
143 }
144 
145 
146 PyObject *
_PyObject_MakeTpCall(PyThreadState * tstate,PyObject * callable,PyObject * const * args,Py_ssize_t nargs,PyObject * keywords)147 _PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable,
148                      PyObject *const *args, Py_ssize_t nargs,
149                      PyObject *keywords)
150 {
151     assert(nargs >= 0);
152     assert(nargs == 0 || args != NULL);
153     assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));
154 
155     /* Slow path: build a temporary tuple for positional arguments and a
156      * temporary dictionary for keyword arguments (if any) */
157     ternaryfunc call = Py_TYPE(callable)->tp_call;
158     if (call == NULL) {
159         _PyErr_Format(tstate, PyExc_TypeError,
160                       "'%.200s' object is not callable",
161                       Py_TYPE(callable)->tp_name);
162         return NULL;
163     }
164 
165     PyObject *argstuple = _PyTuple_FromArray(args, nargs);
166     if (argstuple == NULL) {
167         return NULL;
168     }
169 
170     PyObject *kwdict;
171     if (keywords == NULL || PyDict_Check(keywords)) {
172         kwdict = keywords;
173     }
174     else {
175         if (PyTuple_GET_SIZE(keywords)) {
176             assert(args != NULL);
177             kwdict = _PyStack_AsDict(args + nargs, keywords);
178             if (kwdict == NULL) {
179                 Py_DECREF(argstuple);
180                 return NULL;
181             }
182         }
183         else {
184             keywords = kwdict = NULL;
185         }
186     }
187 
188     PyObject *result = NULL;
189     if (_Py_EnterRecursiveCall(tstate, " while calling a Python object") == 0)
190     {
191         result = call(callable, argstuple, kwdict);
192         _Py_LeaveRecursiveCall(tstate);
193     }
194 
195     Py_DECREF(argstuple);
196     if (kwdict != keywords) {
197         Py_DECREF(kwdict);
198     }
199 
200     return _Py_CheckFunctionResult(tstate, callable, result, NULL);
201 }
202 
203 
204 PyObject *
PyVectorcall_Call(PyObject * callable,PyObject * tuple,PyObject * kwargs)205 PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
206 {
207     PyThreadState *tstate = _PyThreadState_GET();
208 
209     /* get vectorcallfunc as in PyVectorcall_Function, but without
210      * the Py_TPFLAGS_HAVE_VECTORCALL check */
211     Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
212     if (offset <= 0) {
213         _PyErr_Format(tstate, PyExc_TypeError,
214                       "'%.200s' object does not support vectorcall",
215                       Py_TYPE(callable)->tp_name);
216         return NULL;
217     }
218     vectorcallfunc func = *(vectorcallfunc *)(((char *)callable) + offset);
219     if (func == NULL) {
220         _PyErr_Format(tstate, PyExc_TypeError,
221                       "'%.200s' object does not support vectorcall",
222                       Py_TYPE(callable)->tp_name);
223         return NULL;
224     }
225 
226     Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);
227 
228     /* Fast path for no keywords */
229     if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
230         return func(callable, _PyTuple_ITEMS(tuple), nargs, NULL);
231     }
232 
233     /* Convert arguments & call */
234     PyObject *const *args;
235     PyObject *kwnames;
236     args = _PyStack_UnpackDict(tstate,
237                                _PyTuple_ITEMS(tuple), nargs,
238                                kwargs, &kwnames);
239     if (args == NULL) {
240         return NULL;
241     }
242     PyObject *result = func(callable, args,
243                             nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
244     _PyStack_UnpackDict_Free(args, nargs, kwnames);
245 
246     return _Py_CheckFunctionResult(tstate, callable, result, NULL);
247 }
248 
249 
250 PyObject *
_PyObject_Call(PyThreadState * tstate,PyObject * callable,PyObject * args,PyObject * kwargs)251 _PyObject_Call(PyThreadState *tstate, PyObject *callable,
252                PyObject *args, PyObject *kwargs)
253 {
254     ternaryfunc call;
255     PyObject *result;
256 
257     /* PyObject_Call() must not be called with an exception set,
258        because it can clear it (directly or indirectly) and so the
259        caller loses its exception */
260     assert(!_PyErr_Occurred(tstate));
261     assert(PyTuple_Check(args));
262     assert(kwargs == NULL || PyDict_Check(kwargs));
263 
264     if (PyVectorcall_Function(callable) != NULL) {
265         return PyVectorcall_Call(callable, args, kwargs);
266     }
267     else {
268         call = Py_TYPE(callable)->tp_call;
269         if (call == NULL) {
270             _PyErr_Format(tstate, PyExc_TypeError,
271                           "'%.200s' object is not callable",
272                           Py_TYPE(callable)->tp_name);
273             return NULL;
274         }
275 
276         if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) {
277             return NULL;
278         }
279 
280         result = (*call)(callable, args, kwargs);
281 
282         _Py_LeaveRecursiveCall(tstate);
283 
284         return _Py_CheckFunctionResult(tstate, callable, result, NULL);
285     }
286 }
287 
288 PyObject *
PyObject_Call(PyObject * callable,PyObject * args,PyObject * kwargs)289 PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
290 {
291     PyThreadState *tstate = _PyThreadState_GET();
292     return _PyObject_Call(tstate, callable, args, kwargs);
293 }
294 
295 
296 PyObject *
PyCFunction_Call(PyObject * callable,PyObject * args,PyObject * kwargs)297 PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
298 {
299     PyThreadState *tstate = _PyThreadState_GET();
300     return _PyObject_Call(tstate, callable, args, kwargs);
301 }
302 
303 
304 /* --- PyFunction call functions ---------------------------------- */
305 
306 static PyObject* _Py_HOT_FUNCTION
function_code_fastcall(PyThreadState * tstate,PyCodeObject * co,PyObject * const * args,Py_ssize_t nargs,PyObject * globals)307 function_code_fastcall(PyThreadState *tstate, PyCodeObject *co,
308                        PyObject *const *args, Py_ssize_t nargs,
309                        PyObject *globals)
310 {
311     assert(tstate != NULL);
312     assert(globals != NULL);
313 
314     /* XXX Perhaps we should create a specialized
315        _PyFrame_New_NoTrack() that doesn't take locals, but does
316        take builtins without sanity checking them.
317        */
318     PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, NULL);
319     if (f == NULL) {
320         return NULL;
321     }
322 
323     PyObject **fastlocals = f->f_localsplus;
324 
325     for (Py_ssize_t i = 0; i < nargs; i++) {
326         Py_INCREF(*args);
327         fastlocals[i] = *args++;
328     }
329     PyObject *result = _PyEval_EvalFrame(tstate, f, 0);
330 
331     if (Py_REFCNT(f) > 1) {
332         Py_DECREF(f);
333         _PyObject_GC_TRACK(f);
334     }
335     else {
336         ++tstate->recursion_depth;
337         Py_DECREF(f);
338         --tstate->recursion_depth;
339     }
340     return result;
341 }
342 
343 
344 PyObject *
_PyFunction_Vectorcall(PyObject * func,PyObject * const * stack,size_t nargsf,PyObject * kwnames)345 _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
346                        size_t nargsf, PyObject *kwnames)
347 {
348     assert(PyFunction_Check(func));
349     assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
350 
351     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
352     assert(nargs >= 0);
353     Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
354     assert((nargs == 0 && nkwargs == 0) || stack != NULL);
355     /* kwnames must only contain strings and all keys must be unique */
356 
357     PyThreadState *tstate = _PyThreadState_GET();
358     PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
359     PyObject *globals = PyFunction_GET_GLOBALS(func);
360     PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
361 
362     if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
363         (co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
364     {
365         if (argdefs == NULL && co->co_argcount == nargs) {
366             return function_code_fastcall(tstate, co, stack, nargs, globals);
367         }
368         else if (nargs == 0 && argdefs != NULL
369                  && co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
370             /* function called with no arguments, but all parameters have
371                a default value: use default values as arguments .*/
372             stack = _PyTuple_ITEMS(argdefs);
373             return function_code_fastcall(tstate, co,
374                                           stack, PyTuple_GET_SIZE(argdefs),
375                                           globals);
376         }
377     }
378 
379     PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
380     PyObject *closure = PyFunction_GET_CLOSURE(func);
381     PyObject *name = ((PyFunctionObject *)func) -> func_name;
382     PyObject *qualname = ((PyFunctionObject *)func) -> func_qualname;
383 
384     PyObject **d;
385     Py_ssize_t nd;
386     if (argdefs != NULL) {
387         d = _PyTuple_ITEMS(argdefs);
388         nd = PyTuple_GET_SIZE(argdefs);
389         assert(nd <= INT_MAX);
390     }
391     else {
392         d = NULL;
393         nd = 0;
394     }
395     return _PyEval_EvalCode(tstate,
396                 (PyObject*)co, globals, (PyObject *)NULL,
397                 stack, nargs,
398                 nkwargs ? _PyTuple_ITEMS(kwnames) : NULL,
399                 stack + nargs,
400                 nkwargs, 1,
401                 d, (int)nd, kwdefs,
402                 closure, name, qualname);
403 }
404 
405 
406 /* --- More complex call functions -------------------------------- */
407 
408 /* External interface to call any callable object.
409    The args must be a tuple or NULL.  The kwargs must be a dict or NULL. */
410 PyObject *
PyEval_CallObjectWithKeywords(PyObject * callable,PyObject * args,PyObject * kwargs)411 PyEval_CallObjectWithKeywords(PyObject *callable,
412                               PyObject *args, PyObject *kwargs)
413 {
414     PyThreadState *tstate = _PyThreadState_GET();
415 #ifdef Py_DEBUG
416     /* PyEval_CallObjectWithKeywords() must not be called with an exception
417        set. It raises a new exception if parameters are invalid or if
418        PyTuple_New() fails, and so the original exception is lost. */
419     assert(!_PyErr_Occurred(tstate));
420 #endif
421 
422     if (args != NULL && !PyTuple_Check(args)) {
423         _PyErr_SetString(tstate, PyExc_TypeError,
424                          "argument list must be a tuple");
425         return NULL;
426     }
427 
428     if (kwargs != NULL && !PyDict_Check(kwargs)) {
429         _PyErr_SetString(tstate, PyExc_TypeError,
430                          "keyword list must be a dictionary");
431         return NULL;
432     }
433 
434     if (args == NULL) {
435         return _PyObject_FastCallDictTstate(tstate, callable, NULL, 0, kwargs);
436     }
437     else {
438         return _PyObject_Call(tstate, callable, args, kwargs);
439     }
440 }
441 
442 
443 PyObject *
PyObject_CallObject(PyObject * callable,PyObject * args)444 PyObject_CallObject(PyObject *callable, PyObject *args)
445 {
446     PyThreadState *tstate = _PyThreadState_GET();
447     assert(!_PyErr_Occurred(tstate));
448     if (args == NULL) {
449         return _PyObject_CallNoArgTstate(tstate, callable);
450     }
451     if (!PyTuple_Check(args)) {
452         _PyErr_SetString(tstate, PyExc_TypeError,
453                          "argument list must be a tuple");
454         return NULL;
455     }
456     return _PyObject_Call(tstate, callable, args, NULL);
457 }
458 
459 
460 /* Call callable(obj, *args, **kwargs). */
461 PyObject *
_PyObject_Call_Prepend(PyThreadState * tstate,PyObject * callable,PyObject * obj,PyObject * args,PyObject * kwargs)462 _PyObject_Call_Prepend(PyThreadState *tstate, PyObject *callable,
463                        PyObject *obj, PyObject *args, PyObject *kwargs)
464 {
465     assert(PyTuple_Check(args));
466 
467     PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
468     PyObject **stack;
469 
470     Py_ssize_t argcount = PyTuple_GET_SIZE(args);
471     if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
472         stack = small_stack;
473     }
474     else {
475         stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
476         if (stack == NULL) {
477             PyErr_NoMemory();
478             return NULL;
479         }
480     }
481 
482     /* use borrowed references */
483     stack[0] = obj;
484     memcpy(&stack[1],
485            _PyTuple_ITEMS(args),
486            argcount * sizeof(PyObject *));
487 
488     PyObject *result = _PyObject_FastCallDictTstate(tstate, callable,
489                                                     stack, argcount + 1,
490                                                     kwargs);
491     if (stack != small_stack) {
492         PyMem_Free(stack);
493     }
494     return result;
495 }
496 
497 
498 /* --- Call with a format string ---------------------------------- */
499 
500 static PyObject *
_PyObject_CallFunctionVa(PyThreadState * tstate,PyObject * callable,const char * format,va_list va,int is_size_t)501 _PyObject_CallFunctionVa(PyThreadState *tstate, PyObject *callable,
502                          const char *format, va_list va, int is_size_t)
503 {
504     PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
505     const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
506     PyObject **stack;
507     Py_ssize_t nargs, i;
508     PyObject *result;
509 
510     if (callable == NULL) {
511         return null_error(tstate);
512     }
513 
514     if (!format || !*format) {
515         return _PyObject_CallNoArgTstate(tstate, callable);
516     }
517 
518     if (is_size_t) {
519         stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
520                                        format, va, &nargs);
521     }
522     else {
523         stack = _Py_VaBuildStack(small_stack, small_stack_len,
524                                  format, va, &nargs);
525     }
526     if (stack == NULL) {
527         return NULL;
528     }
529 
530     if (nargs == 1 && PyTuple_Check(stack[0])) {
531         /* Special cases for backward compatibility:
532            - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
533            - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
534              func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
535         PyObject *args = stack[0];
536         result = _PyObject_VectorcallTstate(tstate, callable,
537                                             _PyTuple_ITEMS(args),
538                                             PyTuple_GET_SIZE(args),
539                                             NULL);
540     }
541     else {
542         result = _PyObject_VectorcallTstate(tstate, callable,
543                                             stack, nargs, NULL);
544     }
545 
546     for (i = 0; i < nargs; ++i) {
547         Py_DECREF(stack[i]);
548     }
549     if (stack != small_stack) {
550         PyMem_Free(stack);
551     }
552     return result;
553 }
554 
555 
556 PyObject *
PyObject_CallFunction(PyObject * callable,const char * format,...)557 PyObject_CallFunction(PyObject *callable, const char *format, ...)
558 {
559     va_list va;
560     PyObject *result;
561     PyThreadState *tstate = _PyThreadState_GET();
562 
563     va_start(va, format);
564     result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
565     va_end(va);
566 
567     return result;
568 }
569 
570 
571 /* PyEval_CallFunction is exact copy of PyObject_CallFunction.
572  * This function is kept for backward compatibility.
573  */
574 PyObject *
PyEval_CallFunction(PyObject * callable,const char * format,...)575 PyEval_CallFunction(PyObject *callable, const char *format, ...)
576 {
577     va_list va;
578     PyObject *result;
579     PyThreadState *tstate = _PyThreadState_GET();
580 
581     va_start(va, format);
582     result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
583     va_end(va);
584 
585     return result;
586 }
587 
588 
589 PyObject *
_PyObject_CallFunction_SizeT(PyObject * callable,const char * format,...)590 _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
591 {
592     PyThreadState *tstate = _PyThreadState_GET();
593 
594     va_list va;
595     va_start(va, format);
596     PyObject *result = _PyObject_CallFunctionVa(tstate, callable, format, va, 1);
597     va_end(va);
598 
599     return result;
600 }
601 
602 
603 static PyObject*
callmethod(PyThreadState * tstate,PyObject * callable,const char * format,va_list va,int is_size_t)604 callmethod(PyThreadState *tstate, PyObject* callable, const char *format, va_list va, int is_size_t)
605 {
606     assert(callable != NULL);
607     if (!PyCallable_Check(callable)) {
608         _PyErr_Format(tstate, PyExc_TypeError,
609                       "attribute of type '%.200s' is not callable",
610                       Py_TYPE(callable)->tp_name);
611         return NULL;
612     }
613 
614     return _PyObject_CallFunctionVa(tstate, callable, format, va, is_size_t);
615 }
616 
617 
618 PyObject *
PyObject_CallMethod(PyObject * obj,const char * name,const char * format,...)619 PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
620 {
621     PyThreadState *tstate = _PyThreadState_GET();
622 
623     if (obj == NULL || name == NULL) {
624         return null_error(tstate);
625     }
626 
627     PyObject *callable = PyObject_GetAttrString(obj, name);
628     if (callable == NULL) {
629         return NULL;
630     }
631 
632     va_list va;
633     va_start(va, format);
634     PyObject *retval = callmethod(tstate, callable, format, va, 0);
635     va_end(va);
636 
637     Py_DECREF(callable);
638     return retval;
639 }
640 
641 
642 /* PyEval_CallMethod is exact copy of PyObject_CallMethod.
643  * This function is kept for backward compatibility.
644  */
645 PyObject *
PyEval_CallMethod(PyObject * obj,const char * name,const char * format,...)646 PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
647 {
648     PyThreadState *tstate = _PyThreadState_GET();
649     if (obj == NULL || name == NULL) {
650         return null_error(tstate);
651     }
652 
653     PyObject *callable = PyObject_GetAttrString(obj, name);
654     if (callable == NULL) {
655         return NULL;
656     }
657 
658     va_list va;
659     va_start(va, format);
660     PyObject *retval = callmethod(tstate, callable, format, va, 0);
661     va_end(va);
662 
663     Py_DECREF(callable);
664     return retval;
665 }
666 
667 
668 PyObject *
_PyObject_CallMethodId(PyObject * obj,_Py_Identifier * name,const char * format,...)669 _PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
670                        const char *format, ...)
671 {
672     PyThreadState *tstate = _PyThreadState_GET();
673     if (obj == NULL || name == NULL) {
674         return null_error(tstate);
675     }
676 
677     PyObject *callable = _PyObject_GetAttrId(obj, name);
678     if (callable == NULL) {
679         return NULL;
680     }
681 
682     va_list va;
683     va_start(va, format);
684     PyObject *retval = callmethod(tstate, callable, format, va, 0);
685     va_end(va);
686 
687     Py_DECREF(callable);
688     return retval;
689 }
690 
691 
692 PyObject *
_PyObject_CallMethod_SizeT(PyObject * obj,const char * name,const char * format,...)693 _PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
694                            const char *format, ...)
695 {
696     PyThreadState *tstate = _PyThreadState_GET();
697     if (obj == NULL || name == NULL) {
698         return null_error(tstate);
699     }
700 
701     PyObject *callable = PyObject_GetAttrString(obj, name);
702     if (callable == NULL) {
703         return NULL;
704     }
705 
706     va_list va;
707     va_start(va, format);
708     PyObject *retval = callmethod(tstate, callable, format, va, 1);
709     va_end(va);
710 
711     Py_DECREF(callable);
712     return retval;
713 }
714 
715 
716 PyObject *
_PyObject_CallMethodId_SizeT(PyObject * obj,_Py_Identifier * name,const char * format,...)717 _PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
718                              const char *format, ...)
719 {
720     PyThreadState *tstate = _PyThreadState_GET();
721     if (obj == NULL || name == NULL) {
722         return null_error(tstate);
723     }
724 
725     PyObject *callable = _PyObject_GetAttrId(obj, name);
726     if (callable == NULL) {
727         return NULL;
728     }
729 
730     va_list va;
731     va_start(va, format);
732     PyObject *retval = callmethod(tstate, callable, format, va, 1);
733     va_end(va);
734 
735     Py_DECREF(callable);
736     return retval;
737 }
738 
739 
740 /* --- Call with "..." arguments ---------------------------------- */
741 
742 static PyObject *
object_vacall(PyThreadState * tstate,PyObject * base,PyObject * callable,va_list vargs)743 object_vacall(PyThreadState *tstate, PyObject *base,
744               PyObject *callable, va_list vargs)
745 {
746     PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
747     PyObject **stack;
748     Py_ssize_t nargs;
749     PyObject *result;
750     Py_ssize_t i;
751     va_list countva;
752 
753     if (callable == NULL) {
754         return null_error(tstate);
755     }
756 
757     /* Count the number of arguments */
758     va_copy(countva, vargs);
759     nargs = base ? 1 : 0;
760     while (1) {
761         PyObject *arg = va_arg(countva, PyObject *);
762         if (arg == NULL) {
763             break;
764         }
765         nargs++;
766     }
767     va_end(countva);
768 
769     /* Copy arguments */
770     if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
771         stack = small_stack;
772     }
773     else {
774         stack = PyMem_Malloc(nargs * sizeof(stack[0]));
775         if (stack == NULL) {
776             PyErr_NoMemory();
777             return NULL;
778         }
779     }
780 
781     i = 0;
782     if (base) {
783         stack[i++] = base;
784     }
785 
786     for (; i < nargs; ++i) {
787         stack[i] = va_arg(vargs, PyObject *);
788     }
789 
790     /* Call the function */
791     result = _PyObject_VectorcallTstate(tstate, callable, stack, nargs, NULL);
792 
793     if (stack != small_stack) {
794         PyMem_Free(stack);
795     }
796     return result;
797 }
798 
799 
800 PyObject *
PyObject_VectorcallMethod(PyObject * name,PyObject * const * args,size_t nargsf,PyObject * kwnames)801 PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
802                            size_t nargsf, PyObject *kwnames)
803 {
804     assert(name != NULL);
805     assert(args != NULL);
806     assert(PyVectorcall_NARGS(nargsf) >= 1);
807 
808     PyThreadState *tstate = _PyThreadState_GET();
809     PyObject *callable = NULL;
810     /* Use args[0] as "self" argument */
811     int unbound = _PyObject_GetMethod(args[0], name, &callable);
812     if (callable == NULL) {
813         return NULL;
814     }
815 
816     if (unbound) {
817         /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
818          * that would be interpreted as allowing to change args[-1] */
819         nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
820     }
821     else {
822         /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
823          * args[-1] in the onward call is args[0] here. */
824         args++;
825         nargsf--;
826     }
827     PyObject *result = _PyObject_VectorcallTstate(tstate, callable,
828                                                   args, nargsf, kwnames);
829     Py_DECREF(callable);
830     return result;
831 }
832 
833 
834 PyObject *
PyObject_CallMethodObjArgs(PyObject * obj,PyObject * name,...)835 PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
836 {
837     PyThreadState *tstate = _PyThreadState_GET();
838     if (obj == NULL || name == NULL) {
839         return null_error(tstate);
840     }
841 
842     PyObject *callable = NULL;
843     int is_method = _PyObject_GetMethod(obj, name, &callable);
844     if (callable == NULL) {
845         return NULL;
846     }
847     obj = is_method ? obj : NULL;
848 
849     va_list vargs;
850     va_start(vargs, name);
851     PyObject *result = object_vacall(tstate, obj, callable, vargs);
852     va_end(vargs);
853 
854     Py_DECREF(callable);
855     return result;
856 }
857 
858 
859 PyObject *
_PyObject_CallMethodIdObjArgs(PyObject * obj,struct _Py_Identifier * name,...)860 _PyObject_CallMethodIdObjArgs(PyObject *obj,
861                               struct _Py_Identifier *name, ...)
862 {
863     PyThreadState *tstate = _PyThreadState_GET();
864     if (obj == NULL || name == NULL) {
865         return null_error(tstate);
866     }
867 
868     PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
869     if (!oname) {
870         return NULL;
871     }
872 
873     PyObject *callable = NULL;
874     int is_method = _PyObject_GetMethod(obj, oname, &callable);
875     if (callable == NULL) {
876         return NULL;
877     }
878     obj = is_method ? obj : NULL;
879 
880     va_list vargs;
881     va_start(vargs, name);
882     PyObject *result = object_vacall(tstate, obj, callable, vargs);
883     va_end(vargs);
884 
885     Py_DECREF(callable);
886     return result;
887 }
888 
889 
890 PyObject *
PyObject_CallFunctionObjArgs(PyObject * callable,...)891 PyObject_CallFunctionObjArgs(PyObject *callable, ...)
892 {
893     PyThreadState *tstate = _PyThreadState_GET();
894     va_list vargs;
895     PyObject *result;
896 
897     va_start(vargs, callable);
898     result = object_vacall(tstate, NULL, callable, vargs);
899     va_end(vargs);
900 
901     return result;
902 }
903 
904 
905 /* --- PyStack functions ------------------------------------------ */
906 
907 PyObject *
_PyStack_AsDict(PyObject * const * values,PyObject * kwnames)908 _PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
909 {
910     Py_ssize_t nkwargs;
911     PyObject *kwdict;
912     Py_ssize_t i;
913 
914     assert(kwnames != NULL);
915     nkwargs = PyTuple_GET_SIZE(kwnames);
916     kwdict = _PyDict_NewPresized(nkwargs);
917     if (kwdict == NULL) {
918         return NULL;
919     }
920 
921     for (i = 0; i < nkwargs; i++) {
922         PyObject *key = PyTuple_GET_ITEM(kwnames, i);
923         PyObject *value = *values++;
924         /* If key already exists, replace it with the new value */
925         if (PyDict_SetItem(kwdict, key, value)) {
926             Py_DECREF(kwdict);
927             return NULL;
928         }
929     }
930     return kwdict;
931 }
932 
933 
934 /* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
935 
936    Allocate a new argument vector and keyword names tuple. Return the argument
937    vector; return NULL with exception set on error. Return the keyword names
938    tuple in *p_kwnames.
939 
940    This also checks that all keyword names are strings. If not, a TypeError is
941    raised.
942 
943    The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
944 
945    When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames) */
946 static PyObject *const *
_PyStack_UnpackDict(PyThreadState * tstate,PyObject * const * args,Py_ssize_t nargs,PyObject * kwargs,PyObject ** p_kwnames)947 _PyStack_UnpackDict(PyThreadState *tstate,
948                     PyObject *const *args, Py_ssize_t nargs,
949                     PyObject *kwargs, PyObject **p_kwnames)
950 {
951     assert(nargs >= 0);
952     assert(kwargs != NULL);
953     assert(PyDict_Check(kwargs));
954 
955     Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs);
956     /* Check for overflow in the PyMem_Malloc() call below. The subtraction
957      * in this check cannot overflow: both maxnargs and nkwargs are
958      * non-negative signed integers, so their difference fits in the type. */
959     Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
960     if (nargs > maxnargs - nkwargs) {
961         _PyErr_NoMemory(tstate);
962         return NULL;
963     }
964 
965     /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
966     PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
967     if (stack == NULL) {
968         _PyErr_NoMemory(tstate);
969         return NULL;
970     }
971 
972     PyObject *kwnames = PyTuple_New(nkwargs);
973     if (kwnames == NULL) {
974         PyMem_Free(stack);
975         return NULL;
976     }
977 
978     stack++;  /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
979 
980     /* Copy positional arguments */
981     for (Py_ssize_t i = 0; i < nargs; i++) {
982         Py_INCREF(args[i]);
983         stack[i] = args[i];
984     }
985 
986     PyObject **kwstack = stack + nargs;
987     /* This loop doesn't support lookup function mutating the dictionary
988        to change its size. It's a deliberate choice for speed, this function is
989        called in the performance critical hot code. */
990     Py_ssize_t pos = 0, i = 0;
991     PyObject *key, *value;
992     unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
993     while (PyDict_Next(kwargs, &pos, &key, &value)) {
994         keys_are_strings &= Py_TYPE(key)->tp_flags;
995         Py_INCREF(key);
996         Py_INCREF(value);
997         PyTuple_SET_ITEM(kwnames, i, key);
998         kwstack[i] = value;
999         i++;
1000     }
1001 
1002     /* keys_are_strings has the value Py_TPFLAGS_UNICODE_SUBCLASS if that
1003      * flag is set for all keys. Otherwise, keys_are_strings equals 0.
1004      * We do this check once at the end instead of inside the loop above
1005      * because it simplifies the deallocation in the failing case.
1006      * It happens to also make the loop above slightly more efficient. */
1007     if (!keys_are_strings) {
1008         _PyErr_SetString(tstate, PyExc_TypeError,
1009                          "keywords must be strings");
1010         _PyStack_UnpackDict_Free(stack, nargs, kwnames);
1011         return NULL;
1012     }
1013 
1014     *p_kwnames = kwnames;
1015     return stack;
1016 }
1017 
1018 static void
_PyStack_UnpackDict_Free(PyObject * const * stack,Py_ssize_t nargs,PyObject * kwnames)1019 _PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
1020                          PyObject *kwnames)
1021 {
1022     Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
1023     for (Py_ssize_t i = 0; i < n; i++) {
1024         Py_DECREF(stack[i]);
1025     }
1026     PyMem_Free((PyObject **)stack - 1);
1027     Py_DECREF(kwnames);
1028 }
1029