Line data Source code
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 0 : null_error(PyThreadState *tstate)
22 : {
23 0 : if (!_PyErr_Occurred(tstate)) {
24 0 : _PyErr_SetString(tstate, PyExc_SystemError,
25 : "null argument to internal routine");
26 : }
27 0 : return NULL;
28 : }
29 :
30 :
31 : PyObject*
32 334776000 : _Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
33 : PyObject *result, const char *where)
34 : {
35 334776000 : assert((callable != NULL) ^ (where != NULL));
36 :
37 334776000 : if (result == NULL) {
38 1009100 : if (!_PyErr_Occurred(tstate)) {
39 0 : if (callable)
40 0 : _PyErr_Format(tstate, PyExc_SystemError,
41 : "%R returned NULL without setting an exception",
42 : callable);
43 : else
44 0 : _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 0 : Py_FatalError("a function returned NULL without setting an exception");
51 : #endif
52 : return NULL;
53 : }
54 : }
55 : else {
56 333766000 : if (_PyErr_Occurred(tstate)) {
57 0 : Py_DECREF(result);
58 :
59 0 : if (callable) {
60 0 : _PyErr_FormatFromCauseTstate(
61 : tstate, PyExc_SystemError,
62 : "%R returned a result with an exception set", callable);
63 : }
64 : else {
65 0 : _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 0 : Py_FatalError("a function returned a result with an exception set");
73 : #endif
74 : return NULL;
75 : }
76 : }
77 334776000 : return result;
78 : }
79 :
80 :
81 : int
82 385045000 : _Py_CheckSlotResult(PyObject *obj, const char *slot_name, int success)
83 : {
84 385045000 : PyThreadState *tstate = _PyThreadState_GET();
85 385045000 : if (!success) {
86 1252930 : if (!_PyErr_Occurred(tstate)) {
87 0 : _Py_FatalErrorFormat(__func__,
88 : "Slot %s of type %s failed "
89 : "without setting an exception",
90 0 : slot_name, Py_TYPE(obj)->tp_name);
91 : }
92 : }
93 : else {
94 383793000 : if (_PyErr_Occurred(tstate)) {
95 0 : _Py_FatalErrorFormat(__func__,
96 : "Slot %s of type %s succeeded "
97 : "with an exception set",
98 0 : slot_name, Py_TYPE(obj)->tp_name);
99 : }
100 : }
101 385045000 : return 1;
102 : }
103 :
104 :
105 : /* --- Core PyObject call functions ------------------------------- */
106 :
107 : /* Call a callable Python object without any arguments */
108 : PyObject *
109 189528 : PyObject_CallNoArgs(PyObject *func)
110 : {
111 : EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
112 189528 : PyThreadState *tstate = _PyThreadState_GET();
113 189528 : return _PyObject_VectorcallTstate(tstate, func, NULL, 0, NULL);
114 : }
115 :
116 :
117 : PyObject *
118 26905800 : _PyObject_FastCallDictTstate(PyThreadState *tstate, PyObject *callable,
119 : PyObject *const *args, size_t nargsf,
120 : PyObject *kwargs)
121 : {
122 26905800 : 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 26905800 : assert(!_PyErr_Occurred(tstate));
128 :
129 26905800 : Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
130 26905800 : assert(nargs >= 0);
131 26905800 : assert(nargs == 0 || args != NULL);
132 26905800 : assert(kwargs == NULL || PyDict_Check(kwargs));
133 :
134 26905800 : vectorcallfunc func = _PyVectorcall_Function(callable);
135 26905800 : if (func == NULL) {
136 : /* Use tp_call instead */
137 223464 : return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwargs);
138 : }
139 :
140 : PyObject *res;
141 26682400 : if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
142 23874100 : res = func(callable, args, nargsf, NULL);
143 : }
144 : else {
145 : PyObject *kwnames;
146 : PyObject *const *newargs;
147 2808280 : newargs = _PyStack_UnpackDict(tstate,
148 : args, nargs,
149 : kwargs, &kwnames);
150 2808280 : if (newargs == NULL) {
151 0 : return NULL;
152 : }
153 2808280 : res = func(callable, newargs,
154 : nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
155 2808280 : _PyStack_UnpackDict_Free(newargs, nargs, kwnames);
156 : }
157 26682400 : return _Py_CheckFunctionResult(tstate, callable, res, NULL);
158 : }
159 :
160 :
161 : PyObject *
162 3137550 : PyObject_VectorcallDict(PyObject *callable, PyObject *const *args,
163 : size_t nargsf, PyObject *kwargs)
164 : {
165 3137550 : PyThreadState *tstate = _PyThreadState_GET();
166 3137550 : return _PyObject_FastCallDictTstate(tstate, callable, args, nargsf, kwargs);
167 : }
168 :
169 :
170 : PyObject *
171 76949600 : _PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable,
172 : PyObject *const *args, Py_ssize_t nargs,
173 : PyObject *keywords)
174 : {
175 76949600 : assert(nargs >= 0);
176 76949600 : assert(nargs == 0 || args != NULL);
177 76949600 : 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 76949600 : ternaryfunc call = Py_TYPE(callable)->tp_call;
182 76949600 : if (call == NULL) {
183 141 : _PyErr_Format(tstate, PyExc_TypeError,
184 : "'%.200s' object is not callable",
185 141 : Py_TYPE(callable)->tp_name);
186 141 : return NULL;
187 : }
188 :
189 76949500 : PyObject *argstuple = _PyTuple_FromArray(args, nargs);
190 76949500 : if (argstuple == NULL) {
191 0 : return NULL;
192 : }
193 :
194 : PyObject *kwdict;
195 76949500 : if (keywords == NULL || PyDict_Check(keywords)) {
196 73541600 : kwdict = keywords;
197 : }
198 : else {
199 3407900 : if (PyTuple_GET_SIZE(keywords)) {
200 3407890 : assert(args != NULL);
201 3407890 : kwdict = _PyStack_AsDict(args + nargs, keywords);
202 3407890 : if (kwdict == NULL) {
203 0 : Py_DECREF(argstuple);
204 0 : return NULL;
205 : }
206 : }
207 : else {
208 18 : keywords = kwdict = NULL;
209 : }
210 : }
211 :
212 76949500 : PyObject *result = NULL;
213 76949500 : if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object") == 0)
214 : {
215 76949500 : result = _PyCFunctionWithKeywords_TrampolineCall(
216 : (PyCFunctionWithKeywords)call, callable, argstuple, kwdict);
217 76949500 : _Py_LeaveRecursiveCallTstate(tstate);
218 : }
219 :
220 76949500 : Py_DECREF(argstuple);
221 76949500 : if (kwdict != keywords) {
222 3407880 : Py_DECREF(kwdict);
223 : }
224 :
225 76949500 : return _Py_CheckFunctionResult(tstate, callable, result, NULL);
226 : }
227 :
228 :
229 : vectorcallfunc
230 42076100 : PyVectorcall_Function(PyObject *callable)
231 : {
232 42076100 : return _PyVectorcall_FunctionInline(callable);
233 : }
234 :
235 :
236 : static PyObject *
237 7602010 : _PyVectorcall_Call(PyThreadState *tstate, vectorcallfunc func,
238 : PyObject *callable, PyObject *tuple, PyObject *kwargs)
239 : {
240 7602010 : assert(func != NULL);
241 :
242 7602010 : Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);
243 :
244 : /* Fast path for no keywords */
245 7602010 : if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
246 7008870 : return func(callable, _PyTuple_ITEMS(tuple), nargs, NULL);
247 : }
248 :
249 : /* Convert arguments & call */
250 : PyObject *const *args;
251 : PyObject *kwnames;
252 593141 : args = _PyStack_UnpackDict(tstate,
253 593141 : _PyTuple_ITEMS(tuple), nargs,
254 : kwargs, &kwnames);
255 593141 : if (args == NULL) {
256 9 : return NULL;
257 : }
258 593132 : PyObject *result = func(callable, args,
259 : nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
260 593126 : _PyStack_UnpackDict_Free(args, nargs, kwnames);
261 :
262 593126 : return _Py_CheckFunctionResult(tstate, callable, result, NULL);
263 : }
264 :
265 :
266 : PyObject *
267 341770 : PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
268 : {
269 341770 : PyThreadState *tstate = _PyThreadState_GET();
270 :
271 : /* get vectorcallfunc as in _PyVectorcall_Function, but without
272 : * the Py_TPFLAGS_HAVE_VECTORCALL check */
273 341770 : Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
274 341770 : if (offset <= 0) {
275 0 : _PyErr_Format(tstate, PyExc_TypeError,
276 : "'%.200s' object does not support vectorcall",
277 0 : Py_TYPE(callable)->tp_name);
278 0 : return NULL;
279 : }
280 341770 : assert(PyCallable_Check(callable));
281 :
282 : vectorcallfunc func;
283 341770 : memcpy(&func, (char *) callable + offset, sizeof(func));
284 341770 : if (func == NULL) {
285 0 : _PyErr_Format(tstate, PyExc_TypeError,
286 : "'%.200s' object does not support vectorcall",
287 0 : Py_TYPE(callable)->tp_name);
288 0 : return NULL;
289 : }
290 :
291 341770 : return _PyVectorcall_Call(tstate, func, callable, tuple, kwargs);
292 : }
293 :
294 :
295 : PyObject *
296 145145000 : PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
297 : size_t nargsf, PyObject *kwnames)
298 : {
299 145145000 : PyThreadState *tstate = _PyThreadState_GET();
300 145145000 : return _PyObject_VectorcallTstate(tstate, callable,
301 : args, nargsf, kwnames);
302 : }
303 :
304 :
305 : PyObject *
306 1361320 : _PyObject_FastCall(PyObject *func, PyObject *const *args, Py_ssize_t nargs)
307 : {
308 1361320 : PyThreadState *tstate = _PyThreadState_GET();
309 1361320 : return _PyObject_FastCallTstate(tstate, func, args, nargs);
310 : }
311 :
312 :
313 : PyObject *
314 15070000 : _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 15070000 : assert(!_PyErr_Occurred(tstate));
324 15070000 : assert(PyTuple_Check(args));
325 15070000 : assert(kwargs == NULL || PyDict_Check(kwargs));
326 : EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable);
327 15070000 : vectorcallfunc vector_func = _PyVectorcall_Function(callable);
328 15070000 : if (vector_func != NULL) {
329 7260240 : return _PyVectorcall_Call(tstate, vector_func, callable, args, kwargs);
330 : }
331 : else {
332 7809740 : call = Py_TYPE(callable)->tp_call;
333 7809740 : if (call == NULL) {
334 35 : _PyErr_Format(tstate, PyExc_TypeError,
335 : "'%.200s' object is not callable",
336 35 : Py_TYPE(callable)->tp_name);
337 35 : return NULL;
338 : }
339 :
340 7809700 : if (_Py_EnterRecursiveCallTstate(tstate, " while calling a Python object")) {
341 2 : return NULL;
342 : }
343 :
344 7809700 : result = (*call)(callable, args, kwargs);
345 :
346 7809700 : _Py_LeaveRecursiveCallTstate(tstate);
347 :
348 7809700 : return _Py_CheckFunctionResult(tstate, callable, result, NULL);
349 : }
350 : }
351 :
352 : PyObject *
353 13442700 : PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
354 : {
355 13442700 : PyThreadState *tstate = _PyThreadState_GET();
356 13442700 : return _PyObject_Call(tstate, callable, args, kwargs);
357 : }
358 :
359 :
360 : PyObject *
361 0 : PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
362 : {
363 0 : PyThreadState *tstate = _PyThreadState_GET();
364 0 : return _PyObject_Call(tstate, callable, args, kwargs);
365 : }
366 :
367 :
368 : PyObject *
369 20355500 : PyObject_CallOneArg(PyObject *func, PyObject *arg)
370 : {
371 : EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, func);
372 20355500 : assert(arg != NULL);
373 : PyObject *_args[2];
374 20355500 : PyObject **args = _args + 1; // For PY_VECTORCALL_ARGUMENTS_OFFSET
375 20355500 : args[0] = arg;
376 20355500 : PyThreadState *tstate = _PyThreadState_GET();
377 20355500 : size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
378 20355500 : return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL);
379 : }
380 :
381 :
382 : /* --- PyFunction call functions ---------------------------------- */
383 :
384 : PyObject *
385 69922500 : _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
386 : size_t nargsf, PyObject *kwnames)
387 : {
388 69922500 : assert(PyFunction_Check(func));
389 69922500 : PyFunctionObject *f = (PyFunctionObject *)func;
390 69922500 : Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
391 69922500 : assert(nargs >= 0);
392 69922500 : PyThreadState *tstate = _PyThreadState_GET();
393 69922500 : assert(nargs == 0 || stack != NULL);
394 : EVAL_CALL_STAT_INC(EVAL_CALL_FUNCTION_VECTORCALL);
395 69922500 : if (((PyCodeObject *)f->func_code)->co_flags & CO_OPTIMIZED) {
396 69922500 : return _PyEval_Vector(tstate, f, NULL, stack, nargs, kwnames);
397 : }
398 : else {
399 0 : 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 0 : PyEval_CallObjectWithKeywords(PyObject *callable,
409 : PyObject *args, PyObject *kwargs)
410 : {
411 0 : 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 0 : assert(!_PyErr_Occurred(tstate));
417 : #endif
418 :
419 0 : if (args != NULL && !PyTuple_Check(args)) {
420 0 : _PyErr_SetString(tstate, PyExc_TypeError,
421 : "argument list must be a tuple");
422 0 : return NULL;
423 : }
424 :
425 0 : if (kwargs != NULL && !PyDict_Check(kwargs)) {
426 0 : _PyErr_SetString(tstate, PyExc_TypeError,
427 : "keyword list must be a dictionary");
428 0 : return NULL;
429 : }
430 :
431 0 : if (args == NULL) {
432 0 : return _PyObject_FastCallDictTstate(tstate, callable, NULL, 0, kwargs);
433 : }
434 : else {
435 0 : return _PyObject_Call(tstate, callable, args, kwargs);
436 : }
437 : }
438 :
439 :
440 : PyObject *
441 1635640 : PyObject_CallObject(PyObject *callable, PyObject *args)
442 : {
443 1635640 : PyThreadState *tstate = _PyThreadState_GET();
444 1635640 : assert(!_PyErr_Occurred(tstate));
445 1635640 : if (args == NULL) {
446 41499 : return _PyObject_CallNoArgsTstate(tstate, callable);
447 : }
448 1594140 : if (!PyTuple_Check(args)) {
449 2 : _PyErr_SetString(tstate, PyExc_TypeError,
450 : "argument list must be a tuple");
451 2 : return NULL;
452 : }
453 1594140 : return _PyObject_Call(tstate, callable, args, NULL);
454 : }
455 :
456 :
457 : /* Call callable(obj, *args, **kwargs). */
458 : PyObject *
459 23768300 : _PyObject_Call_Prepend(PyThreadState *tstate, PyObject *callable,
460 : PyObject *obj, PyObject *args, PyObject *kwargs)
461 : {
462 23768300 : assert(PyTuple_Check(args));
463 :
464 : PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
465 : PyObject **stack;
466 :
467 23768300 : Py_ssize_t argcount = PyTuple_GET_SIZE(args);
468 23768300 : if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
469 16153300 : stack = small_stack;
470 : }
471 : else {
472 7615030 : stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
473 7615030 : if (stack == NULL) {
474 0 : PyErr_NoMemory();
475 0 : return NULL;
476 : }
477 : }
478 :
479 : /* use borrowed references */
480 23768300 : stack[0] = obj;
481 47536600 : memcpy(&stack[1],
482 23768300 : _PyTuple_ITEMS(args),
483 : argcount * sizeof(PyObject *));
484 :
485 23768300 : PyObject *result = _PyObject_FastCallDictTstate(tstate, callable,
486 23768300 : stack, argcount + 1,
487 : kwargs);
488 23768300 : if (stack != small_stack) {
489 7615030 : PyMem_Free(stack);
490 : }
491 23768300 : return result;
492 : }
493 :
494 :
495 : /* --- Call with a format string ---------------------------------- */
496 :
497 : static PyObject *
498 2304620 : _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 2304620 : 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 2304620 : if (callable == NULL) {
508 0 : return null_error(tstate);
509 : }
510 :
511 2304620 : if (!format || !*format) {
512 105629 : return _PyObject_CallNoArgsTstate(tstate, callable);
513 : }
514 :
515 2198990 : if (is_size_t) {
516 1310360 : stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
517 : format, va, &nargs);
518 : }
519 : else {
520 888627 : stack = _Py_VaBuildStack(small_stack, small_stack_len,
521 : format, va, &nargs);
522 : }
523 2198990 : if (stack == NULL) {
524 0 : return NULL;
525 : }
526 : EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_API, callable);
527 2214760 : if (nargs == 1 && PyTuple_Check(stack[0])) {
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 15774 : PyObject *args = stack[0];
533 31548 : result = _PyObject_VectorcallTstate(tstate, callable,
534 15774 : _PyTuple_ITEMS(args),
535 15774 : PyTuple_GET_SIZE(args),
536 : NULL);
537 : }
538 : else {
539 2183220 : result = _PyObject_VectorcallTstate(tstate, callable,
540 : stack, nargs, NULL);
541 : }
542 :
543 9016970 : for (i = 0; i < nargs; ++i) {
544 6818000 : Py_DECREF(stack[i]);
545 : }
546 2198970 : if (stack != small_stack) {
547 21275 : PyMem_Free(stack);
548 : }
549 2198970 : return result;
550 : }
551 :
552 :
553 : PyObject *
554 851754 : PyObject_CallFunction(PyObject *callable, const char *format, ...)
555 : {
556 : va_list va;
557 : PyObject *result;
558 851754 : PyThreadState *tstate = _PyThreadState_GET();
559 :
560 851754 : va_start(va, format);
561 851754 : result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
562 851754 : va_end(va);
563 :
564 851754 : 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 0 : PyEval_CallFunction(PyObject *callable, const char *format, ...)
573 : {
574 : va_list va;
575 : PyObject *result;
576 0 : PyThreadState *tstate = _PyThreadState_GET();
577 :
578 0 : va_start(va, format);
579 0 : result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
580 0 : va_end(va);
581 :
582 0 : return result;
583 : }
584 :
585 :
586 : PyObject *
587 953497 : _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
588 : {
589 953497 : PyThreadState *tstate = _PyThreadState_GET();
590 :
591 : va_list va;
592 953497 : va_start(va, format);
593 953497 : PyObject *result = _PyObject_CallFunctionVa(tstate, callable, format, va, 1);
594 953478 : va_end(va);
595 :
596 953478 : return result;
597 : }
598 :
599 :
600 : static PyObject*
601 499370 : callmethod(PyThreadState *tstate, PyObject* callable, const char *format, va_list va, int is_size_t)
602 : {
603 499370 : assert(callable != NULL);
604 499370 : if (!PyCallable_Check(callable)) {
605 1 : _PyErr_Format(tstate, PyExc_TypeError,
606 : "attribute of type '%.200s' is not callable",
607 1 : Py_TYPE(callable)->tp_name);
608 1 : return NULL;
609 : }
610 :
611 499369 : return _PyObject_CallFunctionVa(tstate, callable, format, va, is_size_t);
612 : }
613 :
614 : PyObject *
615 140731 : PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
616 : {
617 140731 : PyThreadState *tstate = _PyThreadState_GET();
618 :
619 140731 : if (obj == NULL || name == NULL) {
620 0 : return null_error(tstate);
621 : }
622 :
623 140731 : PyObject *callable = PyObject_GetAttrString(obj, name);
624 140731 : if (callable == NULL) {
625 0 : return NULL;
626 : }
627 :
628 : va_list va;
629 140731 : va_start(va, format);
630 140731 : PyObject *retval = callmethod(tstate, callable, format, va, 0);
631 140731 : va_end(va);
632 :
633 140731 : Py_DECREF(callable);
634 140731 : 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 0 : PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
643 : {
644 0 : PyThreadState *tstate = _PyThreadState_GET();
645 0 : if (obj == NULL || name == NULL) {
646 0 : return null_error(tstate);
647 : }
648 :
649 0 : PyObject *callable = PyObject_GetAttrString(obj, name);
650 0 : if (callable == NULL) {
651 0 : return NULL;
652 : }
653 :
654 : va_list va;
655 0 : va_start(va, format);
656 0 : PyObject *retval = callmethod(tstate, callable, format, va, 0);
657 0 : va_end(va);
658 :
659 0 : Py_DECREF(callable);
660 0 : return retval;
661 : }
662 :
663 :
664 : PyObject *
665 210921 : _PyObject_CallMethod(PyObject *obj, PyObject *name,
666 : const char *format, ...)
667 : {
668 210921 : PyThreadState *tstate = _PyThreadState_GET();
669 210921 : if (obj == NULL || name == NULL) {
670 0 : return null_error(tstate);
671 : }
672 :
673 210921 : PyObject *callable = PyObject_GetAttr(obj, name);
674 210921 : if (callable == NULL) {
675 0 : return NULL;
676 : }
677 :
678 : va_list va;
679 210921 : va_start(va, format);
680 210921 : PyObject *retval = callmethod(tstate, callable, format, va, 1);
681 210921 : va_end(va);
682 :
683 210921 : Py_DECREF(callable);
684 210921 : return retval;
685 : }
686 :
687 :
688 : PyObject *
689 861 : _PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
690 : const char *format, ...)
691 : {
692 861 : PyThreadState *tstate = _PyThreadState_GET();
693 861 : if (obj == NULL || name == NULL) {
694 0 : return null_error(tstate);
695 : }
696 :
697 861 : PyObject *callable = _PyObject_GetAttrId(obj, name);
698 861 : if (callable == NULL) {
699 0 : return NULL;
700 : }
701 :
702 : va_list va;
703 861 : va_start(va, format);
704 861 : PyObject *retval = callmethod(tstate, callable, format, va, 0);
705 861 : va_end(va);
706 :
707 861 : Py_DECREF(callable);
708 861 : return retval;
709 : }
710 :
711 :
712 164 : PyObject * _PyObject_CallMethodFormat(PyThreadState *tstate, PyObject *callable,
713 : const char *format, ...)
714 : {
715 : va_list va;
716 164 : va_start(va, format);
717 164 : PyObject *retval = callmethod(tstate, callable, format, va, 0);
718 164 : va_end(va);
719 164 : return retval;
720 : }
721 :
722 :
723 : PyObject *
724 146693 : _PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
725 : const char *format, ...)
726 : {
727 146693 : PyThreadState *tstate = _PyThreadState_GET();
728 146693 : if (obj == NULL || name == NULL) {
729 0 : return null_error(tstate);
730 : }
731 :
732 146693 : PyObject *callable = PyObject_GetAttrString(obj, name);
733 146693 : if (callable == NULL) {
734 0 : return NULL;
735 : }
736 :
737 : va_list va;
738 146693 : va_start(va, format);
739 146693 : PyObject *retval = callmethod(tstate, callable, format, va, 1);
740 146693 : va_end(va);
741 :
742 146693 : Py_DECREF(callable);
743 146693 : return retval;
744 : }
745 :
746 :
747 : PyObject *
748 0 : _PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
749 : const char *format, ...)
750 : {
751 0 : PyThreadState *tstate = _PyThreadState_GET();
752 0 : if (obj == NULL || name == NULL) {
753 0 : return null_error(tstate);
754 : }
755 :
756 0 : PyObject *callable = _PyObject_GetAttrId(obj, name);
757 0 : if (callable == NULL) {
758 0 : return NULL;
759 : }
760 :
761 : va_list va;
762 0 : va_start(va, format);
763 0 : PyObject *retval = callmethod(tstate, callable, format, va, 1);
764 0 : va_end(va);
765 :
766 0 : Py_DECREF(callable);
767 0 : return retval;
768 : }
769 :
770 :
771 : /* --- Call with "..." arguments ---------------------------------- */
772 :
773 : static PyObject *
774 3537210 : 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 3537210 : if (callable == NULL) {
785 0 : return null_error(tstate);
786 : }
787 :
788 : /* Count the number of arguments */
789 3537210 : va_copy(countva, vargs);
790 3537210 : nargs = base ? 1 : 0;
791 6477530 : while (1) {
792 10014700 : PyObject *arg = va_arg(countva, PyObject *);
793 10014700 : if (arg == NULL) {
794 3537210 : break;
795 : }
796 6477530 : nargs++;
797 : }
798 3537210 : va_end(countva);
799 :
800 : /* Copy arguments */
801 3537210 : if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
802 3533820 : stack = small_stack;
803 : }
804 : else {
805 3391 : stack = PyMem_Malloc(nargs * sizeof(stack[0]));
806 3391 : if (stack == NULL) {
807 0 : PyErr_NoMemory();
808 0 : return NULL;
809 : }
810 : }
811 :
812 3537210 : i = 0;
813 3537210 : if (base) {
814 793297 : stack[i++] = base;
815 : }
816 :
817 10014700 : for (; i < nargs; ++i) {
818 6477530 : 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 3537210 : result = _PyObject_VectorcallTstate(tstate, callable, stack, nargs, NULL);
828 :
829 3537210 : if (stack != small_stack) {
830 3391 : PyMem_Free(stack);
831 : }
832 3537210 : return result;
833 : }
834 :
835 :
836 : PyObject *
837 6493470 : PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
838 : size_t nargsf, PyObject *kwnames)
839 : {
840 6493470 : assert(name != NULL);
841 6493470 : assert(args != NULL);
842 6493470 : assert(PyVectorcall_NARGS(nargsf) >= 1);
843 :
844 6493470 : PyThreadState *tstate = _PyThreadState_GET();
845 6493470 : PyObject *callable = NULL;
846 : /* Use args[0] as "self" argument */
847 6493470 : int unbound = _PyObject_GetMethod(args[0], name, &callable);
848 6493470 : if (callable == NULL) {
849 11856 : return NULL;
850 : }
851 :
852 6481620 : if (unbound) {
853 : /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
854 : * that would be interpreted as allowing to change args[-1] */
855 5909470 : 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 572144 : args++;
861 572144 : nargsf--;
862 : }
863 : EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_METHOD, callable);
864 6481620 : PyObject *result = _PyObject_VectorcallTstate(tstate, callable,
865 : args, nargsf, kwnames);
866 6481600 : Py_DECREF(callable);
867 6481600 : return result;
868 : }
869 :
870 :
871 : PyObject *
872 1204780 : PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
873 : {
874 1204780 : PyThreadState *tstate = _PyThreadState_GET();
875 1204780 : if (obj == NULL || name == NULL) {
876 0 : return null_error(tstate);
877 : }
878 :
879 1204780 : PyObject *callable = NULL;
880 1204780 : int is_method = _PyObject_GetMethod(obj, name, &callable);
881 1204780 : if (callable == NULL) {
882 0 : return NULL;
883 : }
884 1204780 : obj = is_method ? obj : NULL;
885 :
886 : va_list vargs;
887 1204780 : va_start(vargs, name);
888 1204780 : PyObject *result = object_vacall(tstate, obj, callable, vargs);
889 1204780 : va_end(vargs);
890 :
891 1204780 : Py_DECREF(callable);
892 1204780 : return result;
893 : }
894 :
895 :
896 : PyObject *
897 852 : _PyObject_CallMethodIdObjArgs(PyObject *obj, _Py_Identifier *name, ...)
898 : {
899 852 : PyThreadState *tstate = _PyThreadState_GET();
900 852 : if (obj == NULL || name == NULL) {
901 0 : return null_error(tstate);
902 : }
903 :
904 852 : PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
905 852 : if (!oname) {
906 0 : return NULL;
907 : }
908 :
909 852 : PyObject *callable = NULL;
910 852 : int is_method = _PyObject_GetMethod(obj, oname, &callable);
911 852 : if (callable == NULL) {
912 0 : return NULL;
913 : }
914 852 : obj = is_method ? obj : NULL;
915 :
916 : va_list vargs;
917 852 : va_start(vargs, name);
918 852 : PyObject *result = object_vacall(tstate, obj, callable, vargs);
919 852 : va_end(vargs);
920 :
921 852 : Py_DECREF(callable);
922 852 : return result;
923 : }
924 :
925 :
926 : PyObject *
927 2331580 : PyObject_CallFunctionObjArgs(PyObject *callable, ...)
928 : {
929 2331580 : PyThreadState *tstate = _PyThreadState_GET();
930 : va_list vargs;
931 : PyObject *result;
932 :
933 2331580 : va_start(vargs, callable);
934 2331580 : result = object_vacall(tstate, NULL, callable, vargs);
935 2331580 : va_end(vargs);
936 :
937 2331580 : return result;
938 : }
939 :
940 :
941 : /* --- PyStack functions ------------------------------------------ */
942 :
943 : PyObject *
944 3510160 : _PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
945 : {
946 : Py_ssize_t nkwargs;
947 :
948 3510160 : assert(kwnames != NULL);
949 3510160 : nkwargs = PyTuple_GET_SIZE(kwnames);
950 3510160 : 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 3401430 : _PyStack_UnpackDict(PyThreadState *tstate,
969 : PyObject *const *args, Py_ssize_t nargs,
970 : PyObject *kwargs, PyObject **p_kwnames)
971 : {
972 3401430 : assert(nargs >= 0);
973 3401430 : assert(kwargs != NULL);
974 3401430 : assert(PyDict_Check(kwargs));
975 :
976 3401430 : 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 3401430 : Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
981 3401430 : if (nargs > maxnargs - nkwargs) {
982 0 : _PyErr_NoMemory(tstate);
983 0 : return NULL;
984 : }
985 :
986 : /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
987 3401430 : PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
988 3401430 : if (stack == NULL) {
989 0 : _PyErr_NoMemory(tstate);
990 0 : return NULL;
991 : }
992 :
993 3401430 : PyObject *kwnames = PyTuple_New(nkwargs);
994 3401430 : if (kwnames == NULL) {
995 0 : PyMem_Free(stack);
996 0 : return NULL;
997 : }
998 :
999 3401430 : stack++; /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
1000 :
1001 : /* Copy positional arguments */
1002 12772800 : for (Py_ssize_t i = 0; i < nargs; i++) {
1003 9371350 : Py_INCREF(args[i]);
1004 9371350 : stack[i] = args[i];
1005 : }
1006 :
1007 3401430 : 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 3401430 : Py_ssize_t pos = 0, i = 0;
1012 : PyObject *key, *value;
1013 3401430 : unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
1014 8501050 : while (PyDict_Next(kwargs, &pos, &key, &value)) {
1015 5099620 : keys_are_strings &= Py_TYPE(key)->tp_flags;
1016 5099620 : Py_INCREF(key);
1017 5099620 : Py_INCREF(value);
1018 5099620 : PyTuple_SET_ITEM(kwnames, i, key);
1019 5099620 : kwstack[i] = value;
1020 5099620 : 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 3401430 : if (!keys_are_strings) {
1029 9 : _PyErr_SetString(tstate, PyExc_TypeError,
1030 : "keywords must be strings");
1031 9 : _PyStack_UnpackDict_Free(stack, nargs, kwnames);
1032 9 : return NULL;
1033 : }
1034 :
1035 3401420 : *p_kwnames = kwnames;
1036 3401420 : return stack;
1037 : }
1038 :
1039 : static void
1040 3401410 : _PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
1041 : PyObject *kwnames)
1042 : {
1043 3401410 : Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
1044 17872300 : for (Py_ssize_t i = 0; i < n; i++) {
1045 14470900 : Py_DECREF(stack[i]);
1046 : }
1047 3401410 : PyMem_Free((PyObject **)stack - 1);
1048 3401410 : Py_DECREF(kwnames);
1049 3401410 : }
|