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