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