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