Coverage Report

Created: 2022-07-08 09:39

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