Coverage Report

Created: 2025-12-14 07:06

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