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