xref: /third_party/python/Objects/call.c (revision 7db96d56)
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
11static PyObject *const *
12_PyStack_UnpackDict(PyThreadState *tstate,
13                    PyObject *const *args, Py_ssize_t nargs,
14                    PyObject *kwargs, PyObject **p_kwnames);
15
16static void
17_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
18                         PyObject *kwnames);
19
20
21static PyObject *
22null_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
32PyObject*
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
82int
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 */
109PyObject *
110PyObject_CallNoArgs(PyObject *func)
111{
112    return _PyObject_CallNoArgs(func);
113}
114
115
116PyObject *
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
160PyObject *
161PyObject_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
169PyObject *
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_EnterRecursiveCallTstate(tstate, " while calling a Python object") == 0)
213    {
214        result = _PyCFunctionWithKeywords_TrampolineCall(
215            (PyCFunctionWithKeywords)call, callable, argstuple, kwdict);
216        _Py_LeaveRecursiveCallTstate(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
228vectorcallfunc
229PyVectorcall_Function(PyObject *callable)
230{
231    return _PyVectorcall_FunctionInline(callable);
232}
233
234
235static PyObject *
236_PyVectorcall_Call(PyThreadState *tstate, vectorcallfunc func,
237                   PyObject *callable, PyObject *tuple, PyObject *kwargs)
238{
239    assert(func != NULL);
240
241    Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);
242
243    /* Fast path for no keywords */
244    if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
245        return func(callable, _PyTuple_ITEMS(tuple), nargs, NULL);
246    }
247
248    /* Convert arguments & call */
249    PyObject *const *args;
250    PyObject *kwnames;
251    args = _PyStack_UnpackDict(tstate,
252                               _PyTuple_ITEMS(tuple), nargs,
253                               kwargs, &kwnames);
254    if (args == NULL) {
255        return NULL;
256    }
257    PyObject *result = func(callable, args,
258                            nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
259    _PyStack_UnpackDict_Free(args, nargs, kwnames);
260
261    return _Py_CheckFunctionResult(tstate, callable, result, NULL);
262}
263
264
265PyObject *
266PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
267{
268    PyThreadState *tstate = _PyThreadState_GET();
269
270    /* get vectorcallfunc as in _PyVectorcall_Function, but without
271     * the Py_TPFLAGS_HAVE_VECTORCALL check */
272    Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
273    if (offset <= 0) {
274        _PyErr_Format(tstate, PyExc_TypeError,
275                      "'%.200s' object does not support vectorcall",
276                      Py_TYPE(callable)->tp_name);
277        return NULL;
278    }
279    assert(PyCallable_Check(callable));
280
281    vectorcallfunc func;
282    memcpy(&func, (char *) callable + offset, sizeof(func));
283    if (func == NULL) {
284        _PyErr_Format(tstate, PyExc_TypeError,
285                      "'%.200s' object does not support vectorcall",
286                      Py_TYPE(callable)->tp_name);
287        return NULL;
288    }
289
290    return _PyVectorcall_Call(tstate, func, callable, tuple, kwargs);
291}
292
293
294PyObject *
295PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
296                     size_t nargsf, PyObject *kwnames)
297{
298    PyThreadState *tstate = _PyThreadState_GET();
299    return _PyObject_VectorcallTstate(tstate, callable,
300                                      args, nargsf, kwnames);
301}
302
303
304PyObject *
305_PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
306{
307    PyThreadState *tstate = _PyThreadState_GET();
308    return _PyObject_FastCallTstate(tstate, func, args, nargs);
309}
310
311
312PyObject *
313_PyObject_Call(PyThreadState *tstate, PyObject *callable,
314               PyObject *args, PyObject *kwargs)
315{
316    ternaryfunc call;
317    PyObject *result;
318
319    /* PyObject_Call() must not be called with an exception set,
320       because it can clear it (directly or indirectly) and so the
321       caller loses its exception */
322    assert(!_PyErr_Occurred(tstate));
323    assert(PyTuple_Check(args));
324    assert(kwargs == NULL || PyDict_Check(kwargs));
325
326    vectorcallfunc vector_func = _PyVectorcall_Function(callable);
327    if (vector_func != NULL) {
328        return _PyVectorcall_Call(tstate, vector_func, callable, args, kwargs);
329    }
330    else {
331        call = Py_TYPE(callable)->tp_call;
332        if (call == NULL) {
333            _PyErr_Format(tstate, PyExc_TypeError,
334                          "'%.200s' object is not callable",
335                          Py_TYPE(callable)->tp_name);
336            return NULL;
337        }
338
339        if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
340            return NULL;
341        }
342
343        result = (*call)(callable, args, kwargs);
344
345        _Py_LeaveRecursiveCallTstate(tstate);
346
347        return _Py_CheckFunctionResult(tstate, callable, result, NULL);
348    }
349}
350
351PyObject *
352PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
353{
354    PyThreadState *tstate = _PyThreadState_GET();
355    return _PyObject_Call(tstate, callable, args, kwargs);
356}
357
358
359PyObject *
360PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
361{
362    PyThreadState *tstate = _PyThreadState_GET();
363    return _PyObject_Call(tstate, callable, args, kwargs);
364}
365
366
367PyObject *
368PyObject_CallOneArg(PyObject *func, PyObject *arg)
369{
370    assert(arg != NULL);
371    PyObject *_args[2];
372    PyObject **args = _args + 1;  // For PY_VECTORCALL_ARGUMENTS_OFFSET
373    args[0] = arg;
374    PyThreadState *tstate = _PyThreadState_GET();
375    size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
376    return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL);
377}
378
379
380/* --- PyFunction call functions ---------------------------------- */
381
382PyObject *
383_PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
384                       size_t nargsf, PyObject *kwnames)
385{
386    assert(PyFunction_Check(func));
387    PyFunctionObject *f = (PyFunctionObject *)func;
388    Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
389    assert(nargs >= 0);
390    PyThreadState *tstate = _PyThreadState_GET();
391    assert(nargs == 0 || stack != NULL);
392    if (((PyCodeObject *)f->func_code)->co_flags & CO_OPTIMIZED) {
393        return _PyEval_Vector(tstate, f, NULL, stack, nargs, kwnames);
394    }
395    else {
396        return _PyEval_Vector(tstate, f, f->func_globals, stack, nargs, kwnames);
397    }
398}
399
400/* --- More complex call functions -------------------------------- */
401
402/* External interface to call any callable object.
403   The args must be a tuple or NULL.  The kwargs must be a dict or NULL. */
404PyObject *
405PyEval_CallObjectWithKeywords(PyObject *callable,
406                              PyObject *args, PyObject *kwargs)
407{
408    PyThreadState *tstate = _PyThreadState_GET();
409#ifdef Py_DEBUG
410    /* PyEval_CallObjectWithKeywords() must not be called with an exception
411       set. It raises a new exception if parameters are invalid or if
412       PyTuple_New() fails, and so the original exception is lost. */
413    assert(!_PyErr_Occurred(tstate));
414#endif
415
416    if (args != NULL && !PyTuple_Check(args)) {
417        _PyErr_SetString(tstate, PyExc_TypeError,
418                         "argument list must be a tuple");
419        return NULL;
420    }
421
422    if (kwargs != NULL && !PyDict_Check(kwargs)) {
423        _PyErr_SetString(tstate, PyExc_TypeError,
424                         "keyword list must be a dictionary");
425        return NULL;
426    }
427
428    if (args == NULL) {
429        return _PyObject_FastCallDictTstate(tstate, callable, NULL, 0, kwargs);
430    }
431    else {
432        return _PyObject_Call(tstate, callable, args, kwargs);
433    }
434}
435
436
437PyObject *
438PyObject_CallObject(PyObject *callable, PyObject *args)
439{
440    PyThreadState *tstate = _PyThreadState_GET();
441    assert(!_PyErr_Occurred(tstate));
442    if (args == NULL) {
443        return _PyObject_CallNoArgsTstate(tstate, callable);
444    }
445    if (!PyTuple_Check(args)) {
446        _PyErr_SetString(tstate, PyExc_TypeError,
447                         "argument list must be a tuple");
448        return NULL;
449    }
450    return _PyObject_Call(tstate, callable, args, NULL);
451}
452
453
454/* Call callable(obj, *args, **kwargs). */
455PyObject *
456_PyObject_Call_Prepend(PyThreadState *tstate, PyObject *callable,
457                       PyObject *obj, PyObject *args, PyObject *kwargs)
458{
459    assert(PyTuple_Check(args));
460
461    PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
462    PyObject **stack;
463
464    Py_ssize_t argcount = PyTuple_GET_SIZE(args);
465    if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
466        stack = small_stack;
467    }
468    else {
469        stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
470        if (stack == NULL) {
471            PyErr_NoMemory();
472            return NULL;
473        }
474    }
475
476    /* use borrowed references */
477    stack[0] = obj;
478    memcpy(&stack[1],
479           _PyTuple_ITEMS(args),
480           argcount * sizeof(PyObject *));
481
482    PyObject *result = _PyObject_FastCallDictTstate(tstate, callable,
483                                                    stack, argcount + 1,
484                                                    kwargs);
485    if (stack != small_stack) {
486        PyMem_Free(stack);
487    }
488    return result;
489}
490
491
492/* --- Call with a format string ---------------------------------- */
493
494static PyObject *
495_PyObject_CallFunctionVa(PyThreadState *tstate, PyObject *callable,
496                         const char *format, va_list va, int is_size_t)
497{
498    PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
499    const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
500    PyObject **stack;
501    Py_ssize_t nargs, i;
502    PyObject *result;
503
504    if (callable == NULL) {
505        return null_error(tstate);
506    }
507
508    if (!format || !*format) {
509        return _PyObject_CallNoArgsTstate(tstate, callable);
510    }
511
512    if (is_size_t) {
513        stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
514                                       format, va, &nargs);
515    }
516    else {
517        stack = _Py_VaBuildStack(small_stack, small_stack_len,
518                                 format, va, &nargs);
519    }
520    if (stack == NULL) {
521        return NULL;
522    }
523
524    if (nargs == 1 && PyTuple_Check(stack[0])) {
525        /* Special cases for backward compatibility:
526           - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
527           - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
528             func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
529        PyObject *args = stack[0];
530        result = _PyObject_VectorcallTstate(tstate, callable,
531                                            _PyTuple_ITEMS(args),
532                                            PyTuple_GET_SIZE(args),
533                                            NULL);
534    }
535    else {
536        result = _PyObject_VectorcallTstate(tstate, callable,
537                                            stack, nargs, NULL);
538    }
539
540    for (i = 0; i < nargs; ++i) {
541        Py_DECREF(stack[i]);
542    }
543    if (stack != small_stack) {
544        PyMem_Free(stack);
545    }
546    return result;
547}
548
549
550PyObject *
551PyObject_CallFunction(PyObject *callable, const char *format, ...)
552{
553    va_list va;
554    PyObject *result;
555    PyThreadState *tstate = _PyThreadState_GET();
556
557    va_start(va, format);
558    result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
559    va_end(va);
560
561    return result;
562}
563
564
565/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
566 * This function is kept for backward compatibility.
567 */
568PyObject *
569PyEval_CallFunction(PyObject *callable, const char *format, ...)
570{
571    va_list va;
572    PyObject *result;
573    PyThreadState *tstate = _PyThreadState_GET();
574
575    va_start(va, format);
576    result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
577    va_end(va);
578
579    return result;
580}
581
582
583PyObject *
584_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
585{
586    PyThreadState *tstate = _PyThreadState_GET();
587
588    va_list va;
589    va_start(va, format);
590    PyObject *result = _PyObject_CallFunctionVa(tstate, callable, format, va, 1);
591    va_end(va);
592
593    return result;
594}
595
596
597static PyObject*
598callmethod(PyThreadState *tstate, PyObject* callable, const char *format, va_list va, int is_size_t)
599{
600    assert(callable != NULL);
601    if (!PyCallable_Check(callable)) {
602        _PyErr_Format(tstate, PyExc_TypeError,
603                      "attribute of type '%.200s' is not callable",
604                      Py_TYPE(callable)->tp_name);
605        return NULL;
606    }
607
608    return _PyObject_CallFunctionVa(tstate, callable, format, va, is_size_t);
609}
610
611PyObject *
612PyObject_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 */
638PyObject *
639PyEval_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
661PyObject *
662_PyObject_CallMethod(PyObject *obj, PyObject *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_GetAttr(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, 1);
678    va_end(va);
679
680    Py_DECREF(callable);
681    return retval;
682}
683
684
685PyObject *
686_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *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_GetAttrId(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, 0);
702    va_end(va);
703
704    Py_DECREF(callable);
705    return retval;
706}
707
708
709PyObject * _PyObject_CallMethodFormat(PyThreadState *tstate, PyObject *callable,
710                                      const char *format, ...)
711{
712    va_list va;
713    va_start(va, format);
714    PyObject *retval = callmethod(tstate, callable, format, va, 0);
715    va_end(va);
716    return retval;
717}
718
719
720PyObject *
721_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
722                           const char *format, ...)
723{
724    PyThreadState *tstate = _PyThreadState_GET();
725    if (obj == NULL || name == NULL) {
726        return null_error(tstate);
727    }
728
729    PyObject *callable = PyObject_GetAttrString(obj, name);
730    if (callable == NULL) {
731        return NULL;
732    }
733
734    va_list va;
735    va_start(va, format);
736    PyObject *retval = callmethod(tstate, callable, format, va, 1);
737    va_end(va);
738
739    Py_DECREF(callable);
740    return retval;
741}
742
743
744PyObject *
745_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
746                             const char *format, ...)
747{
748    PyThreadState *tstate = _PyThreadState_GET();
749    if (obj == NULL || name == NULL) {
750        return null_error(tstate);
751    }
752
753    PyObject *callable = _PyObject_GetAttrId(obj, name);
754    if (callable == NULL) {
755        return NULL;
756    }
757
758    va_list va;
759    va_start(va, format);
760    PyObject *retval = callmethod(tstate, callable, format, va, 1);
761    va_end(va);
762
763    Py_DECREF(callable);
764    return retval;
765}
766
767
768/* --- Call with "..." arguments ---------------------------------- */
769
770static PyObject *
771object_vacall(PyThreadState *tstate, PyObject *base,
772              PyObject *callable, va_list vargs)
773{
774    PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
775    PyObject **stack;
776    Py_ssize_t nargs;
777    PyObject *result;
778    Py_ssize_t i;
779    va_list countva;
780
781    if (callable == NULL) {
782        return null_error(tstate);
783    }
784
785    /* Count the number of arguments */
786    va_copy(countva, vargs);
787    nargs = base ? 1 : 0;
788    while (1) {
789        PyObject *arg = va_arg(countva, PyObject *);
790        if (arg == NULL) {
791            break;
792        }
793        nargs++;
794    }
795    va_end(countva);
796
797    /* Copy arguments */
798    if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
799        stack = small_stack;
800    }
801    else {
802        stack = PyMem_Malloc(nargs * sizeof(stack[0]));
803        if (stack == NULL) {
804            PyErr_NoMemory();
805            return NULL;
806        }
807    }
808
809    i = 0;
810    if (base) {
811        stack[i++] = base;
812    }
813
814    for (; i < nargs; ++i) {
815        stack[i] = va_arg(vargs, PyObject *);
816    }
817
818    /* Call the function */
819    result = _PyObject_VectorcallTstate(tstate, callable, stack, nargs, NULL);
820
821    if (stack != small_stack) {
822        PyMem_Free(stack);
823    }
824    return result;
825}
826
827
828PyObject *
829PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
830                           size_t nargsf, PyObject *kwnames)
831{
832    assert(name != NULL);
833    assert(args != NULL);
834    assert(PyVectorcall_NARGS(nargsf) >= 1);
835
836    PyThreadState *tstate = _PyThreadState_GET();
837    PyObject *callable = NULL;
838    /* Use args[0] as "self" argument */
839    int unbound = _PyObject_GetMethod(args[0], name, &callable);
840    if (callable == NULL) {
841        return NULL;
842    }
843
844    if (unbound) {
845        /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
846         * that would be interpreted as allowing to change args[-1] */
847        nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
848    }
849    else {
850        /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
851         * args[-1] in the onward call is args[0] here. */
852        args++;
853        nargsf--;
854    }
855    PyObject *result = _PyObject_VectorcallTstate(tstate, callable,
856                                                  args, nargsf, kwnames);
857    Py_DECREF(callable);
858    return result;
859}
860
861
862PyObject *
863PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
864{
865    PyThreadState *tstate = _PyThreadState_GET();
866    if (obj == NULL || name == NULL) {
867        return null_error(tstate);
868    }
869
870    PyObject *callable = NULL;
871    int is_method = _PyObject_GetMethod(obj, name, &callable);
872    if (callable == NULL) {
873        return NULL;
874    }
875    obj = is_method ? obj : NULL;
876
877    va_list vargs;
878    va_start(vargs, name);
879    PyObject *result = object_vacall(tstate, obj, callable, vargs);
880    va_end(vargs);
881
882    Py_DECREF(callable);
883    return result;
884}
885
886
887PyObject *
888_PyObject_CallMethodIdObjArgs(PyObject *obj, _Py_Identifier *name, ...)
889{
890    PyThreadState *tstate = _PyThreadState_GET();
891    if (obj == NULL || name == NULL) {
892        return null_error(tstate);
893    }
894
895    PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
896    if (!oname) {
897        return NULL;
898    }
899
900    PyObject *callable = NULL;
901    int is_method = _PyObject_GetMethod(obj, oname, &callable);
902    if (callable == NULL) {
903        return NULL;
904    }
905    obj = is_method ? obj : NULL;
906
907    va_list vargs;
908    va_start(vargs, name);
909    PyObject *result = object_vacall(tstate, obj, callable, vargs);
910    va_end(vargs);
911
912    Py_DECREF(callable);
913    return result;
914}
915
916
917PyObject *
918PyObject_CallFunctionObjArgs(PyObject *callable, ...)
919{
920    PyThreadState *tstate = _PyThreadState_GET();
921    va_list vargs;
922    PyObject *result;
923
924    va_start(vargs, callable);
925    result = object_vacall(tstate, NULL, callable, vargs);
926    va_end(vargs);
927
928    return result;
929}
930
931
932/* --- PyStack functions ------------------------------------------ */
933
934PyObject *
935_PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
936{
937    Py_ssize_t nkwargs;
938
939    assert(kwnames != NULL);
940    nkwargs = PyTuple_GET_SIZE(kwnames);
941    return _PyDict_FromItems(&PyTuple_GET_ITEM(kwnames, 0), 1,
942                             values, 1, nkwargs);
943}
944
945
946/* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
947
948   Allocate a new argument vector and keyword names tuple. Return the argument
949   vector; return NULL with exception set on error. Return the keyword names
950   tuple in *p_kwnames.
951
952   This also checks that all keyword names are strings. If not, a TypeError is
953   raised.
954
955   The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
956
957   When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames) */
958static PyObject *const *
959_PyStack_UnpackDict(PyThreadState *tstate,
960                    PyObject *const *args, Py_ssize_t nargs,
961                    PyObject *kwargs, PyObject **p_kwnames)
962{
963    assert(nargs >= 0);
964    assert(kwargs != NULL);
965    assert(PyDict_Check(kwargs));
966
967    Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs);
968    /* Check for overflow in the PyMem_Malloc() call below. The subtraction
969     * in this check cannot overflow: both maxnargs and nkwargs are
970     * non-negative signed integers, so their difference fits in the type. */
971    Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
972    if (nargs > maxnargs - nkwargs) {
973        _PyErr_NoMemory(tstate);
974        return NULL;
975    }
976
977    /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
978    PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
979    if (stack == NULL) {
980        _PyErr_NoMemory(tstate);
981        return NULL;
982    }
983
984    PyObject *kwnames = PyTuple_New(nkwargs);
985    if (kwnames == NULL) {
986        PyMem_Free(stack);
987        return NULL;
988    }
989
990    stack++;  /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
991
992    /* Copy positional arguments */
993    for (Py_ssize_t i = 0; i < nargs; i++) {
994        Py_INCREF(args[i]);
995        stack[i] = args[i];
996    }
997
998    PyObject **kwstack = stack + nargs;
999    /* This loop doesn't support lookup function mutating the dictionary
1000       to change its size. It's a deliberate choice for speed, this function is
1001       called in the performance critical hot code. */
1002    Py_ssize_t pos = 0, i = 0;
1003    PyObject *key, *value;
1004    unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
1005    while (PyDict_Next(kwargs, &pos, &key, &value)) {
1006        keys_are_strings &= Py_TYPE(key)->tp_flags;
1007        Py_INCREF(key);
1008        Py_INCREF(value);
1009        PyTuple_SET_ITEM(kwnames, i, key);
1010        kwstack[i] = value;
1011        i++;
1012    }
1013
1014    /* keys_are_strings has the value Py_TPFLAGS_UNICODE_SUBCLASS if that
1015     * flag is set for all keys. Otherwise, keys_are_strings equals 0.
1016     * We do this check once at the end instead of inside the loop above
1017     * because it simplifies the deallocation in the failing case.
1018     * It happens to also make the loop above slightly more efficient. */
1019    if (!keys_are_strings) {
1020        _PyErr_SetString(tstate, PyExc_TypeError,
1021                         "keywords must be strings");
1022        _PyStack_UnpackDict_Free(stack, nargs, kwnames);
1023        return NULL;
1024    }
1025
1026    *p_kwnames = kwnames;
1027    return stack;
1028}
1029
1030static void
1031_PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
1032                         PyObject *kwnames)
1033{
1034    Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
1035    for (Py_ssize_t i = 0; i < n; i++) {
1036        Py_DECREF(stack[i]);
1037    }
1038    PyMem_Free((PyObject **)stack - 1);
1039    Py_DECREF(kwnames);
1040}
1041