blob: 91e607831177c3128ae395949a9b96ae987ab9ea [file] [log] [blame]
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002#include "internal/pystate.h"
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003#include "frameobject.h"
4
5
Victor Stinner0f7b0b32017-03-14 21:37:20 +01006int
7_PyObject_HasFastCall(PyObject *callable)
8{
9 if (PyFunction_Check(callable)) {
10 return 1;
11 }
12 else if (PyCFunction_Check(callable)) {
13 return !(PyCFunction_GET_FLAGS(callable) & METH_VARARGS);
14 }
15 else {
16 assert (PyCallable_Check(callable));
17 return 0;
18 }
19}
20
21
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010022static PyObject *
23null_error(void)
24{
25 if (!PyErr_Occurred())
26 PyErr_SetString(PyExc_SystemError,
27 "null argument to internal routine");
28 return NULL;
29}
30
31
32PyObject*
33_Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where)
34{
35 int err_occurred = (PyErr_Occurred() != NULL);
36
37 assert((callable != NULL) ^ (where != NULL));
38
39 if (result == NULL) {
40 if (!err_occurred) {
41 if (callable)
42 PyErr_Format(PyExc_SystemError,
43 "%R returned NULL without setting an error",
44 callable);
45 else
46 PyErr_Format(PyExc_SystemError,
47 "%s returned NULL without setting an error",
48 where);
49#ifdef Py_DEBUG
50 /* Ensure that the bug is caught in debug mode */
51 Py_FatalError("a function returned NULL without setting an error");
52#endif
53 return NULL;
54 }
55 }
56 else {
57 if (err_occurred) {
58 Py_DECREF(result);
59
60 if (callable) {
61 _PyErr_FormatFromCause(PyExc_SystemError,
62 "%R returned a result with an error set",
63 callable);
64 }
65 else {
66 _PyErr_FormatFromCause(PyExc_SystemError,
67 "%s returned a result with an error set",
68 where);
69 }
70#ifdef Py_DEBUG
71 /* Ensure that the bug is caught in debug mode */
72 Py_FatalError("a function returned a result with an error set");
73#endif
74 return NULL;
75 }
76 }
77 return result;
78}
79
80
81/* --- Core PyObject call functions ------------------------------- */
82
83PyObject *
84_PyObject_FastCallDict(PyObject *callable, PyObject **args, Py_ssize_t nargs,
85 PyObject *kwargs)
86{
87 /* _PyObject_FastCallDict() must not be called with an exception set,
88 because it can clear it (directly or indirectly) and so the
89 caller loses its exception */
90 assert(!PyErr_Occurred());
91
92 assert(callable != NULL);
93 assert(nargs >= 0);
94 assert(nargs == 0 || args != NULL);
95 assert(kwargs == NULL || PyDict_Check(kwargs));
96
97 if (PyFunction_Check(callable)) {
98 return _PyFunction_FastCallDict(callable, args, nargs, kwargs);
99 }
100 else if (PyCFunction_Check(callable)) {
101 return _PyCFunction_FastCallDict(callable, args, nargs, kwargs);
102 }
103 else {
104 PyObject *argstuple, *result;
105 ternaryfunc call;
106
107 /* Slow-path: build a temporary tuple */
108 call = callable->ob_type->tp_call;
109 if (call == NULL) {
110 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
111 callable->ob_type->tp_name);
112 return NULL;
113 }
114
115 argstuple = _PyStack_AsTuple(args, nargs);
116 if (argstuple == NULL) {
117 return NULL;
118 }
119
120 if (Py_EnterRecursiveCall(" while calling a Python object")) {
121 Py_DECREF(argstuple);
122 return NULL;
123 }
124
125 result = (*call)(callable, argstuple, kwargs);
126
127 Py_LeaveRecursiveCall();
128 Py_DECREF(argstuple);
129
130 result = _Py_CheckFunctionResult(callable, result, NULL);
131 return result;
132 }
133}
134
135
136PyObject *
137_PyObject_FastCallKeywords(PyObject *callable, PyObject **stack, Py_ssize_t nargs,
138 PyObject *kwnames)
139{
140 /* _PyObject_FastCallKeywords() must not be called with an exception set,
141 because it can clear it (directly or indirectly) and so the
142 caller loses its exception */
143 assert(!PyErr_Occurred());
144
145 assert(nargs >= 0);
146 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
147
148 /* kwnames must only contains str strings, no subclass, and all keys must
149 be unique: these checks are implemented in Python/ceval.c and
150 _PyArg_ParseStackAndKeywords(). */
151
152 if (PyFunction_Check(callable)) {
153 return _PyFunction_FastCallKeywords(callable, stack, nargs, kwnames);
154 }
155 if (PyCFunction_Check(callable)) {
156 return _PyCFunction_FastCallKeywords(callable, stack, nargs, kwnames);
157 }
158 else {
159 /* Slow-path: build a temporary tuple for positional arguments and a
160 temporary dictionary for keyword arguments (if any) */
161
162 ternaryfunc call;
163 PyObject *argstuple;
164 PyObject *kwdict, *result;
165 Py_ssize_t nkwargs;
166
167 nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
168 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
169
170 call = callable->ob_type->tp_call;
171 if (call == NULL) {
172 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
173 callable->ob_type->tp_name);
174 return NULL;
175 }
176
177 argstuple = _PyStack_AsTuple(stack, nargs);
178 if (argstuple == NULL) {
179 return NULL;
180 }
181
182 if (nkwargs > 0) {
183 kwdict = _PyStack_AsDict(stack + nargs, kwnames);
184 if (kwdict == NULL) {
185 Py_DECREF(argstuple);
186 return NULL;
187 }
188 }
189 else {
190 kwdict = NULL;
191 }
192
193 if (Py_EnterRecursiveCall(" while calling a Python object")) {
194 Py_DECREF(argstuple);
195 Py_XDECREF(kwdict);
196 return NULL;
197 }
198
199 result = (*call)(callable, argstuple, kwdict);
200
201 Py_LeaveRecursiveCall();
202
203 Py_DECREF(argstuple);
204 Py_XDECREF(kwdict);
205
206 result = _Py_CheckFunctionResult(callable, result, NULL);
207 return result;
208 }
209}
210
211
212PyObject *
213PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
214{
215 ternaryfunc call;
216 PyObject *result;
217
218 /* PyObject_Call() must not be called with an exception set,
219 because it can clear it (directly or indirectly) and so the
220 caller loses its exception */
221 assert(!PyErr_Occurred());
222 assert(PyTuple_Check(args));
223 assert(kwargs == NULL || PyDict_Check(kwargs));
224
225 if (PyFunction_Check(callable)) {
226 return _PyFunction_FastCallDict(callable,
227 &PyTuple_GET_ITEM(args, 0),
228 PyTuple_GET_SIZE(args),
229 kwargs);
230 }
231 else if (PyCFunction_Check(callable)) {
232 return PyCFunction_Call(callable, args, kwargs);
233 }
234 else {
235 call = callable->ob_type->tp_call;
236 if (call == NULL) {
237 PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
238 callable->ob_type->tp_name);
239 return NULL;
240 }
241
242 if (Py_EnterRecursiveCall(" while calling a Python object"))
243 return NULL;
244
245 result = (*call)(callable, args, kwargs);
246
247 Py_LeaveRecursiveCall();
248
249 return _Py_CheckFunctionResult(callable, result, NULL);
250 }
251}
252
253
254/* --- PyFunction call functions ---------------------------------- */
255
256static PyObject* _Py_HOT_FUNCTION
257function_code_fastcall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
258 PyObject *globals)
259{
260 PyFrameObject *f;
261 PyThreadState *tstate = PyThreadState_GET();
262 PyObject **fastlocals;
263 Py_ssize_t i;
264 PyObject *result;
265
266 assert(globals != NULL);
267 /* XXX Perhaps we should create a specialized
268 _PyFrame_New_NoTrack() that doesn't take locals, but does
269 take builtins without sanity checking them.
270 */
271 assert(tstate != NULL);
272 f = _PyFrame_New_NoTrack(tstate, co, globals, NULL);
273 if (f == NULL) {
274 return NULL;
275 }
276
277 fastlocals = f->f_localsplus;
278
279 for (i = 0; i < nargs; i++) {
280 Py_INCREF(*args);
281 fastlocals[i] = *args++;
282 }
283 result = PyEval_EvalFrameEx(f,0);
284
285 if (Py_REFCNT(f) > 1) {
286 Py_DECREF(f);
287 _PyObject_GC_TRACK(f);
288 }
289 else {
290 ++tstate->recursion_depth;
291 Py_DECREF(f);
292 --tstate->recursion_depth;
293 }
294 return result;
295}
296
297
298PyObject *
299_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
300 PyObject *kwargs)
301{
302 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
303 PyObject *globals = PyFunction_GET_GLOBALS(func);
304 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
305 PyObject *kwdefs, *closure, *name, *qualname;
306 PyObject *kwtuple, **k;
307 PyObject **d;
308 Py_ssize_t nd, nk;
309 PyObject *result;
310
311 assert(func != NULL);
312 assert(nargs >= 0);
313 assert(nargs == 0 || args != NULL);
314 assert(kwargs == NULL || PyDict_Check(kwargs));
315
316 if (co->co_kwonlyargcount == 0 &&
317 (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) &&
318 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
319 {
320 /* Fast paths */
321 if (argdefs == NULL && co->co_argcount == nargs) {
322 return function_code_fastcall(co, args, nargs, globals);
323 }
324 else if (nargs == 0 && argdefs != NULL
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200325 && co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100326 /* function called with no arguments, but all parameters have
327 a default value: use default values as arguments .*/
328 args = &PyTuple_GET_ITEM(argdefs, 0);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200329 return function_code_fastcall(co, args, PyTuple_GET_SIZE(argdefs),
330 globals);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100331 }
332 }
333
334 nk = (kwargs != NULL) ? PyDict_GET_SIZE(kwargs) : 0;
335 if (nk != 0) {
336 Py_ssize_t pos, i;
337
Victor Stinner561ca802017-02-23 18:26:43 +0100338 /* bpo-29318, bpo-27840: Caller and callee functions must not share
339 the dictionary: kwargs must be copied. */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100340 kwtuple = PyTuple_New(2 * nk);
341 if (kwtuple == NULL) {
342 return NULL;
343 }
344
345 k = &PyTuple_GET_ITEM(kwtuple, 0);
346 pos = i = 0;
347 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
348 /* We must hold strong references because keyword arguments can be
349 indirectly modified while the function is called:
350 see issue #2016 and test_extcall */
351 Py_INCREF(k[i]);
352 Py_INCREF(k[i+1]);
353 i += 2;
354 }
355 nk = i / 2;
356 }
357 else {
358 kwtuple = NULL;
359 k = NULL;
360 }
361
362 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
363 closure = PyFunction_GET_CLOSURE(func);
364 name = ((PyFunctionObject *)func) -> func_name;
365 qualname = ((PyFunctionObject *)func) -> func_qualname;
366
367 if (argdefs != NULL) {
368 d = &PyTuple_GET_ITEM(argdefs, 0);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200369 nd = PyTuple_GET_SIZE(argdefs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100370 }
371 else {
372 d = NULL;
373 nd = 0;
374 }
375
376 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
377 args, nargs,
Zackery Spytzc6ea8972017-07-31 08:24:37 -0600378 k, k != NULL ? k + 1 : NULL, nk, 2,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100379 d, nd, kwdefs,
380 closure, name, qualname);
381 Py_XDECREF(kwtuple);
382 return result;
383}
384
385PyObject *
386_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
387 Py_ssize_t nargs, PyObject *kwnames)
388{
389 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
390 PyObject *globals = PyFunction_GET_GLOBALS(func);
391 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
392 PyObject *kwdefs, *closure, *name, *qualname;
393 PyObject **d;
394 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
395 Py_ssize_t nd;
396
397 assert(PyFunction_Check(func));
398 assert(nargs >= 0);
399 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
400 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
401 /* kwnames must only contains str strings, no subclass, and all keys must
402 be unique */
403
404 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
405 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
406 {
407 if (argdefs == NULL && co->co_argcount == nargs) {
408 return function_code_fastcall(co, stack, nargs, globals);
409 }
410 else if (nargs == 0 && argdefs != NULL
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200411 && co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100412 /* function called with no arguments, but all parameters have
413 a default value: use default values as arguments .*/
414 stack = &PyTuple_GET_ITEM(argdefs, 0);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200415 return function_code_fastcall(co, stack, PyTuple_GET_SIZE(argdefs),
416 globals);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100417 }
418 }
419
420 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
421 closure = PyFunction_GET_CLOSURE(func);
422 name = ((PyFunctionObject *)func) -> func_name;
423 qualname = ((PyFunctionObject *)func) -> func_qualname;
424
425 if (argdefs != NULL) {
426 d = &PyTuple_GET_ITEM(argdefs, 0);
Serhiy Storchakafff9a312017-03-21 08:53:25 +0200427 nd = PyTuple_GET_SIZE(argdefs);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100428 }
429 else {
430 d = NULL;
431 nd = 0;
432 }
433 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
434 stack, nargs,
435 nkwargs ? &PyTuple_GET_ITEM(kwnames, 0) : NULL,
436 stack + nargs,
437 nkwargs, 1,
438 d, (int)nd, kwdefs,
439 closure, name, qualname);
440}
441
442
443/* --- PyCFunction call functions --------------------------------- */
444
445PyObject *
446_PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self, PyObject **args,
447 Py_ssize_t nargs, PyObject *kwargs)
448{
449 /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
450 because it can clear it (directly or indirectly) and so the
451 caller loses its exception */
452 assert(!PyErr_Occurred());
453
454 assert(method != NULL);
455 assert(nargs >= 0);
456 assert(nargs == 0 || args != NULL);
457 assert(kwargs == NULL || PyDict_Check(kwargs));
458
459 PyCFunction meth = method->ml_meth;
460 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
461 PyObject *result = NULL;
462
463 if (Py_EnterRecursiveCall(" while calling a Python object")) {
464 return NULL;
465 }
466
467 switch (flags)
468 {
469 case METH_NOARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300470 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
471 goto no_keyword_error;
472 }
473
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100474 if (nargs != 0) {
475 PyErr_Format(PyExc_TypeError,
476 "%.200s() takes no arguments (%zd given)",
477 method->ml_name, nargs);
478 goto exit;
479 }
480
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100481 result = (*meth) (self, NULL);
482 break;
483
484 case METH_O:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300485 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
486 goto no_keyword_error;
487 }
488
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100489 if (nargs != 1) {
490 PyErr_Format(PyExc_TypeError,
491 "%.200s() takes exactly one argument (%zd given)",
492 method->ml_name, nargs);
493 goto exit;
494 }
495
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100496 result = (*meth) (self, args[0]);
497 break;
498
499 case METH_VARARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300500 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100501 goto no_keyword_error;
502 }
Stefan Krahf432a322017-08-21 13:09:59 +0200503 /* fall through */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100504
505 case METH_VARARGS | METH_KEYWORDS:
506 {
507 /* Slow-path: create a temporary tuple for positional arguments */
508 PyObject *argstuple = _PyStack_AsTuple(args, nargs);
509 if (argstuple == NULL) {
510 goto exit;
511 }
512
513 if (flags & METH_KEYWORDS) {
514 result = (*(PyCFunctionWithKeywords)meth) (self, argstuple, kwargs);
515 }
516 else {
517 result = (*meth) (self, argstuple);
518 }
519 Py_DECREF(argstuple);
520 break;
521 }
522
523 case METH_FASTCALL:
524 {
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300525 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
526 goto no_keyword_error;
527 }
528
529 result = (*(_PyCFunctionFast)meth) (self, args, nargs);
530 break;
531 }
532
533 case METH_FASTCALL | METH_KEYWORDS:
534 {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100535 PyObject **stack;
536 PyObject *kwnames;
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300537 _PyCFunctionFastWithKeywords fastmeth = (_PyCFunctionFastWithKeywords)meth;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100538
539 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
540 goto exit;
541 }
542
543 result = (*fastmeth) (self, stack, nargs, kwnames);
544 if (stack != args) {
545 PyMem_Free(stack);
546 }
547 Py_XDECREF(kwnames);
548 break;
549 }
550
551 default:
552 PyErr_SetString(PyExc_SystemError,
553 "Bad call flags in _PyMethodDef_RawFastCallDict. "
554 "METH_OLDARGS is no longer supported!");
555 goto exit;
556 }
557
558 goto exit;
559
560no_keyword_error:
561 PyErr_Format(PyExc_TypeError,
562 "%.200s() takes no keyword arguments",
Sylvaind67a1032017-03-27 23:36:08 +0200563 method->ml_name);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100564
565exit:
566 Py_LeaveRecursiveCall();
567 return result;
568}
569
570
571PyObject *
572_PyCFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
573 PyObject *kwargs)
574{
575 PyObject *result;
576
577 assert(func != NULL);
578 assert(PyCFunction_Check(func));
579
580 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
581 PyCFunction_GET_SELF(func),
582 args, nargs, kwargs);
583 result = _Py_CheckFunctionResult(func, result, NULL);
584 return result;
585}
586
587
588PyObject *
589_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self, PyObject **args,
590 Py_ssize_t nargs, PyObject *kwnames)
591{
592 /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
593 because it can clear it (directly or indirectly) and so the
594 caller loses its exception */
595 assert(!PyErr_Occurred());
596
597 assert(method != NULL);
598 assert(nargs >= 0);
599 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
600 /* kwnames must only contains str strings, no subclass, and all keys must
601 be unique */
602
603 PyCFunction meth = method->ml_meth;
604 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300605 Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100606 PyObject *result = NULL;
607
608 if (Py_EnterRecursiveCall(" while calling a Python object")) {
609 return NULL;
610 }
611
612 switch (flags)
613 {
614 case METH_NOARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300615 if (nkwargs) {
616 goto no_keyword_error;
617 }
618
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100619 if (nargs != 0) {
620 PyErr_Format(PyExc_TypeError,
621 "%.200s() takes no arguments (%zd given)",
622 method->ml_name, nargs);
623 goto exit;
624 }
625
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100626 result = (*meth) (self, NULL);
627 break;
628
629 case METH_O:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300630 if (nkwargs) {
631 goto no_keyword_error;
632 }
633
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100634 if (nargs != 1) {
635 PyErr_Format(PyExc_TypeError,
636 "%.200s() takes exactly one argument (%zd given)",
637 method->ml_name, nargs);
638 goto exit;
639 }
640
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100641 result = (*meth) (self, args[0]);
642 break;
643
644 case METH_FASTCALL:
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300645 if (nkwargs) {
646 goto no_keyword_error;
647 }
648 result = ((_PyCFunctionFast)meth) (self, args, nargs);
649 break;
650
651 case METH_FASTCALL | METH_KEYWORDS:
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100652 /* Fast-path: avoid temporary dict to pass keyword arguments */
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300653 result = ((_PyCFunctionFastWithKeywords)meth) (self, args, nargs, kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100654 break;
655
656 case METH_VARARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300657 if (nkwargs) {
658 goto no_keyword_error;
659 }
Stefan Krahf432a322017-08-21 13:09:59 +0200660 /* fall through */
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300661
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100662 case METH_VARARGS | METH_KEYWORDS:
663 {
664 /* Slow-path: create a temporary tuple for positional arguments
665 and a temporary dict for keyword arguments */
666 PyObject *argtuple;
667
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100668 argtuple = _PyStack_AsTuple(args, nargs);
669 if (argtuple == NULL) {
670 goto exit;
671 }
672
673 if (flags & METH_KEYWORDS) {
674 PyObject *kwdict;
675
676 if (nkwargs > 0) {
677 kwdict = _PyStack_AsDict(args + nargs, kwnames);
678 if (kwdict == NULL) {
679 Py_DECREF(argtuple);
680 goto exit;
681 }
682 }
683 else {
684 kwdict = NULL;
685 }
686
687 result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
688 Py_XDECREF(kwdict);
689 }
690 else {
691 result = (*meth) (self, argtuple);
692 }
693 Py_DECREF(argtuple);
694 break;
695 }
696
697 default:
698 PyErr_SetString(PyExc_SystemError,
699 "Bad call flags in _PyCFunction_FastCallKeywords. "
700 "METH_OLDARGS is no longer supported!");
701 goto exit;
702 }
703
704 goto exit;
705
706no_keyword_error:
707 PyErr_Format(PyExc_TypeError,
708 "%.200s() takes no keyword arguments",
709 method->ml_name);
710
711exit:
712 Py_LeaveRecursiveCall();
713 return result;
714}
715
716
717PyObject *
718_PyCFunction_FastCallKeywords(PyObject *func, PyObject **args,
719 Py_ssize_t nargs, PyObject *kwnames)
720{
721 PyObject *result;
722
723 assert(func != NULL);
724 assert(PyCFunction_Check(func));
725
726 result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml,
727 PyCFunction_GET_SELF(func),
728 args, nargs, kwnames);
729 result = _Py_CheckFunctionResult(func, result, NULL);
730 return result;
731}
732
733
734static PyObject *
735cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs)
736{
737 assert(!PyErr_Occurred());
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300738 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100739
740 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
741 PyObject *self = PyCFunction_GET_SELF(func);
742 PyObject *result;
743
744 if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) {
745 if (Py_EnterRecursiveCall(" while calling a Python object")) {
746 return NULL;
747 }
748
749 result = (*(PyCFunctionWithKeywords)meth)(self, args, kwargs);
750
751 Py_LeaveRecursiveCall();
752 }
753 else {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300754 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100755 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
756 ((PyCFunctionObject*)func)->m_ml->ml_name);
757 return NULL;
758 }
759
760 if (Py_EnterRecursiveCall(" while calling a Python object")) {
761 return NULL;
762 }
763
764 result = (*meth)(self, args);
765
766 Py_LeaveRecursiveCall();
767 }
768
769 return _Py_CheckFunctionResult(func, result, NULL);
770}
771
772
773PyObject *
774PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
775{
776 /* first try METH_VARARGS to pass directly args tuple unchanged.
777 _PyMethodDef_RawFastCallDict() creates a new temporary tuple
778 for METH_VARARGS. */
779 if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) {
780 return cfunction_call_varargs(func, args, kwargs);
781 }
782 else {
783 return _PyCFunction_FastCallDict(func,
784 &PyTuple_GET_ITEM(args, 0),
785 PyTuple_GET_SIZE(args),
786 kwargs);
787 }
788}
789
790
791/* --- More complex call functions -------------------------------- */
792
793/* External interface to call any callable object.
794 The args must be a tuple or NULL. The kwargs must be a dict or NULL. */
795PyObject *
796PyEval_CallObjectWithKeywords(PyObject *callable,
797 PyObject *args, PyObject *kwargs)
798{
799#ifdef Py_DEBUG
800 /* PyEval_CallObjectWithKeywords() must not be called with an exception
801 set. It raises a new exception if parameters are invalid or if
802 PyTuple_New() fails, and so the original exception is lost. */
803 assert(!PyErr_Occurred());
804#endif
805
INADA Naoki3824cd82017-03-01 20:41:03 +0900806 if (args != NULL && !PyTuple_Check(args)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100807 PyErr_SetString(PyExc_TypeError,
808 "argument list must be a tuple");
809 return NULL;
810 }
811
812 if (kwargs != NULL && !PyDict_Check(kwargs)) {
813 PyErr_SetString(PyExc_TypeError,
814 "keyword list must be a dictionary");
815 return NULL;
816 }
817
INADA Naoki3824cd82017-03-01 20:41:03 +0900818 if (args == NULL) {
819 return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
820 }
821 else {
822 return PyObject_Call(callable, args, kwargs);
823 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100824}
825
826
827PyObject *
828PyObject_CallObject(PyObject *callable, PyObject *args)
829{
830 return PyEval_CallObjectWithKeywords(callable, args, NULL);
831}
832
833
834/* Positional arguments are obj followed by args:
835 call callable(obj, *args, **kwargs) */
836PyObject *
837_PyObject_FastCall_Prepend(PyObject *callable,
838 PyObject *obj, PyObject **args, Py_ssize_t nargs)
839{
840 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
841 PyObject **args2;
842 PyObject *result;
843
844 nargs++;
845 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
846 args2 = small_stack;
847 }
848 else {
849 args2 = PyMem_Malloc(nargs * sizeof(PyObject *));
850 if (args2 == NULL) {
851 PyErr_NoMemory();
852 return NULL;
853 }
854 }
855
856 /* use borrowed references */
857 args2[0] = obj;
Benjamin Petersona3070d52017-09-04 22:23:42 -0700858 if (nargs > 1) {
859 memcpy(&args2[1], args, (nargs - 1) * sizeof(PyObject *));
860 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100861
862 result = _PyObject_FastCall(callable, args2, nargs);
863 if (args2 != small_stack) {
864 PyMem_Free(args2);
865 }
866 return result;
867}
868
869
870/* Call callable(obj, *args, **kwargs). */
871PyObject *
872_PyObject_Call_Prepend(PyObject *callable,
873 PyObject *obj, PyObject *args, PyObject *kwargs)
874{
875 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
876 PyObject **stack;
877 Py_ssize_t argcount;
878 PyObject *result;
879
880 assert(PyTuple_Check(args));
881
882 argcount = PyTuple_GET_SIZE(args);
883 if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
884 stack = small_stack;
885 }
886 else {
887 stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
888 if (stack == NULL) {
889 PyErr_NoMemory();
890 return NULL;
891 }
892 }
893
894 /* use borrowed references */
895 stack[0] = obj;
896 memcpy(&stack[1],
897 &PyTuple_GET_ITEM(args, 0),
898 argcount * sizeof(PyObject *));
899
900 result = _PyObject_FastCallDict(callable,
901 stack, argcount + 1,
902 kwargs);
903 if (stack != small_stack) {
904 PyMem_Free(stack);
905 }
906 return result;
907}
908
909
910/* --- Call with a format string ---------------------------------- */
911
912static PyObject *
913_PyObject_CallFunctionVa(PyObject *callable, const char *format,
914 va_list va, int is_size_t)
915{
916 PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
917 const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
918 PyObject **stack;
919 Py_ssize_t nargs, i;
920 PyObject *result;
921
922 if (callable == NULL) {
923 return null_error();
924 }
925
926 if (!format || !*format) {
927 return _PyObject_CallNoArg(callable);
928 }
929
930 if (is_size_t) {
931 stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
932 format, va, &nargs);
933 }
934 else {
935 stack = _Py_VaBuildStack(small_stack, small_stack_len,
936 format, va, &nargs);
937 }
938 if (stack == NULL) {
939 return NULL;
940 }
941
942 if (nargs == 1 && PyTuple_Check(stack[0])) {
943 /* Special cases for backward compatibility:
944 - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
945 - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
946 func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
947 PyObject *args = stack[0];
948 result = _PyObject_FastCall(callable,
949 &PyTuple_GET_ITEM(args, 0),
950 PyTuple_GET_SIZE(args));
951 }
952 else {
953 result = _PyObject_FastCall(callable, stack, nargs);
954 }
955
956 for (i = 0; i < nargs; ++i) {
957 Py_DECREF(stack[i]);
958 }
959 if (stack != small_stack) {
960 PyMem_Free(stack);
961 }
962 return result;
963}
964
965
966PyObject *
967PyObject_CallFunction(PyObject *callable, const char *format, ...)
968{
969 va_list va;
970 PyObject *result;
971
972 va_start(va, format);
973 result = _PyObject_CallFunctionVa(callable, format, va, 0);
974 va_end(va);
975
976 return result;
977}
978
979
INADA Naokiaa289a52017-03-14 18:00:59 +0900980/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
981 * This function is kept for backward compatibility.
982 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100983PyObject *
984PyEval_CallFunction(PyObject *callable, const char *format, ...)
985{
INADA Naokiaa289a52017-03-14 18:00:59 +0900986 va_list va;
987 PyObject *result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100988
INADA Naokiaa289a52017-03-14 18:00:59 +0900989 va_start(va, format);
990 result = _PyObject_CallFunctionVa(callable, format, va, 0);
991 va_end(va);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100992
INADA Naokiaa289a52017-03-14 18:00:59 +0900993 return result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100994}
995
996
997PyObject *
998_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
999{
1000 va_list va;
1001 PyObject *result;
1002
1003 va_start(va, format);
1004 result = _PyObject_CallFunctionVa(callable, format, va, 1);
1005 va_end(va);
1006
1007 return result;
1008}
1009
1010
1011static PyObject*
1012callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
1013{
1014 assert(callable != NULL);
1015
1016 if (!PyCallable_Check(callable)) {
1017 PyErr_Format(PyExc_TypeError,
1018 "attribute of type '%.200s' is not callable",
1019 Py_TYPE(callable)->tp_name);
1020 return NULL;
1021 }
1022
1023 return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
1024}
1025
1026
1027PyObject *
1028PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
1029{
1030 va_list va;
1031 PyObject *callable, *retval;
1032
1033 if (obj == NULL || name == NULL) {
1034 return null_error();
1035 }
1036
1037 callable = PyObject_GetAttrString(obj, name);
1038 if (callable == NULL)
1039 return NULL;
1040
1041 va_start(va, format);
1042 retval = callmethod(callable, format, va, 0);
1043 va_end(va);
1044
1045 Py_DECREF(callable);
1046 return retval;
1047}
1048
1049
INADA Naokiaa289a52017-03-14 18:00:59 +09001050/* PyEval_CallMethod is exact copy of PyObject_CallMethod.
1051 * This function is kept for backward compatibility.
1052 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001053PyObject *
1054PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
1055{
INADA Naokiaa289a52017-03-14 18:00:59 +09001056 va_list va;
1057 PyObject *callable, *retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001058
INADA Naokiaa289a52017-03-14 18:00:59 +09001059 if (obj == NULL || name == NULL) {
1060 return null_error();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001061 }
1062
INADA Naokiaa289a52017-03-14 18:00:59 +09001063 callable = PyObject_GetAttrString(obj, name);
1064 if (callable == NULL)
1065 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001066
INADA Naokiaa289a52017-03-14 18:00:59 +09001067 va_start(va, format);
1068 retval = callmethod(callable, format, va, 0);
1069 va_end(va);
1070
1071 Py_DECREF(callable);
1072 return retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001073}
1074
1075
1076PyObject *
1077_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
1078 const char *format, ...)
1079{
1080 va_list va;
1081 PyObject *callable, *retval;
1082
1083 if (obj == NULL || name == NULL) {
1084 return null_error();
1085 }
1086
1087 callable = _PyObject_GetAttrId(obj, name);
1088 if (callable == NULL)
1089 return NULL;
1090
1091 va_start(va, format);
1092 retval = callmethod(callable, format, va, 0);
1093 va_end(va);
1094
1095 Py_DECREF(callable);
1096 return retval;
1097}
1098
1099
1100PyObject *
1101_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
1102 const char *format, ...)
1103{
1104 va_list va;
1105 PyObject *callable, *retval;
1106
1107 if (obj == NULL || name == NULL) {
1108 return null_error();
1109 }
1110
1111 callable = PyObject_GetAttrString(obj, name);
1112 if (callable == NULL)
1113 return NULL;
1114
1115 va_start(va, format);
1116 retval = callmethod(callable, format, va, 1);
1117 va_end(va);
1118
1119 Py_DECREF(callable);
1120 return retval;
1121}
1122
1123
1124PyObject *
1125_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
1126 const char *format, ...)
1127{
1128 va_list va;
1129 PyObject *callable, *retval;
1130
1131 if (obj == NULL || name == NULL) {
1132 return null_error();
1133 }
1134
1135 callable = _PyObject_GetAttrId(obj, name);
1136 if (callable == NULL) {
1137 return NULL;
1138 }
1139
1140 va_start(va, format);
1141 retval = callmethod(callable, format, va, 1);
1142 va_end(va);
1143
1144 Py_DECREF(callable);
1145 return retval;
1146}
1147
1148
1149/* --- Call with "..." arguments ---------------------------------- */
1150
1151static PyObject *
1152object_vacall(PyObject *callable, va_list vargs)
1153{
1154 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
1155 PyObject **stack;
1156 Py_ssize_t nargs;
1157 PyObject *result;
1158 Py_ssize_t i;
1159 va_list countva;
1160
1161 if (callable == NULL) {
1162 return null_error();
1163 }
1164
1165 /* Count the number of arguments */
1166 va_copy(countva, vargs);
1167 nargs = 0;
1168 while (1) {
1169 PyObject *arg = va_arg(countva, PyObject *);
1170 if (arg == NULL) {
1171 break;
1172 }
1173 nargs++;
1174 }
1175 va_end(countva);
1176
1177 /* Copy arguments */
1178 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1179 stack = small_stack;
1180 }
1181 else {
1182 stack = PyMem_Malloc(nargs * sizeof(stack[0]));
1183 if (stack == NULL) {
1184 PyErr_NoMemory();
1185 return NULL;
1186 }
1187 }
1188
1189 for (i = 0; i < nargs; ++i) {
1190 stack[i] = va_arg(vargs, PyObject *);
1191 }
1192
1193 /* Call the function */
1194 result = _PyObject_FastCall(callable, stack, nargs);
1195
1196 if (stack != small_stack) {
1197 PyMem_Free(stack);
1198 }
1199 return result;
1200}
1201
1202
1203PyObject *
1204PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
1205{
1206 va_list vargs;
1207 PyObject *result;
1208
1209 if (callable == NULL || name == NULL) {
1210 return null_error();
1211 }
1212
1213 callable = PyObject_GetAttr(callable, name);
1214 if (callable == NULL) {
1215 return NULL;
1216 }
1217
1218 va_start(vargs, name);
1219 result = object_vacall(callable, vargs);
1220 va_end(vargs);
1221
1222 Py_DECREF(callable);
1223 return result;
1224}
1225
1226
1227PyObject *
1228_PyObject_CallMethodIdObjArgs(PyObject *obj,
1229 struct _Py_Identifier *name, ...)
1230{
1231 va_list vargs;
1232 PyObject *callable, *result;
1233
1234 if (obj == NULL || name == NULL) {
1235 return null_error();
1236 }
1237
1238 callable = _PyObject_GetAttrId(obj, name);
1239 if (callable == NULL) {
1240 return NULL;
1241 }
1242
1243 va_start(vargs, name);
1244 result = object_vacall(callable, vargs);
1245 va_end(vargs);
1246
1247 Py_DECREF(callable);
1248 return result;
1249}
1250
1251
1252PyObject *
1253PyObject_CallFunctionObjArgs(PyObject *callable, ...)
1254{
1255 va_list vargs;
1256 PyObject *result;
1257
1258 va_start(vargs, callable);
1259 result = object_vacall(callable, vargs);
1260 va_end(vargs);
1261
1262 return result;
1263}
1264
1265
1266/* --- PyStack functions ------------------------------------------ */
1267
1268/* Issue #29234: Inlining _PyStack_AsTuple() into callers increases their
1269 stack consumption, Disable inlining to optimize the stack consumption. */
1270PyObject* _Py_NO_INLINE
1271_PyStack_AsTuple(PyObject **stack, Py_ssize_t nargs)
1272{
1273 PyObject *args;
1274 Py_ssize_t i;
1275
1276 args = PyTuple_New(nargs);
1277 if (args == NULL) {
1278 return NULL;
1279 }
1280
1281 for (i=0; i < nargs; i++) {
1282 PyObject *item = stack[i];
1283 Py_INCREF(item);
1284 PyTuple_SET_ITEM(args, i, item);
1285 }
1286 return args;
1287}
1288
1289
1290PyObject*
1291_PyStack_AsTupleSlice(PyObject **stack, Py_ssize_t nargs,
1292 Py_ssize_t start, Py_ssize_t end)
1293{
1294 PyObject *args;
1295 Py_ssize_t i;
1296
1297 assert(0 <= start);
1298 assert(end <= nargs);
1299 assert(start <= end);
1300
1301 args = PyTuple_New(end - start);
1302 if (args == NULL) {
1303 return NULL;
1304 }
1305
1306 for (i=start; i < end; i++) {
1307 PyObject *item = stack[i];
1308 Py_INCREF(item);
1309 PyTuple_SET_ITEM(args, i - start, item);
1310 }
1311 return args;
1312}
1313
1314
1315PyObject *
1316_PyStack_AsDict(PyObject **values, PyObject *kwnames)
1317{
1318 Py_ssize_t nkwargs;
1319 PyObject *kwdict;
1320 Py_ssize_t i;
1321
1322 assert(kwnames != NULL);
1323 nkwargs = PyTuple_GET_SIZE(kwnames);
1324 kwdict = _PyDict_NewPresized(nkwargs);
1325 if (kwdict == NULL) {
1326 return NULL;
1327 }
1328
1329 for (i = 0; i < nkwargs; i++) {
1330 PyObject *key = PyTuple_GET_ITEM(kwnames, i);
1331 PyObject *value = *values++;
1332 /* If key already exists, replace it with the new value */
1333 if (PyDict_SetItem(kwdict, key, value)) {
1334 Py_DECREF(kwdict);
1335 return NULL;
1336 }
1337 }
1338 return kwdict;
1339}
1340
1341
1342int
1343_PyStack_UnpackDict(PyObject **args, Py_ssize_t nargs, PyObject *kwargs,
1344 PyObject ***p_stack, PyObject **p_kwnames)
1345{
1346 PyObject **stack, **kwstack;
1347 Py_ssize_t nkwargs;
1348 Py_ssize_t pos, i;
1349 PyObject *key, *value;
1350 PyObject *kwnames;
1351
1352 assert(nargs >= 0);
1353 assert(kwargs == NULL || PyDict_CheckExact(kwargs));
1354
1355 if (kwargs == NULL || (nkwargs = PyDict_GET_SIZE(kwargs)) == 0) {
1356 *p_stack = args;
1357 *p_kwnames = NULL;
1358 return 0;
1359 }
1360
1361 if ((size_t)nargs > PY_SSIZE_T_MAX / sizeof(stack[0]) - (size_t)nkwargs) {
1362 PyErr_NoMemory();
1363 return -1;
1364 }
1365
1366 stack = PyMem_Malloc((nargs + nkwargs) * sizeof(stack[0]));
1367 if (stack == NULL) {
1368 PyErr_NoMemory();
1369 return -1;
1370 }
1371
1372 kwnames = PyTuple_New(nkwargs);
1373 if (kwnames == NULL) {
1374 PyMem_Free(stack);
1375 return -1;
1376 }
1377
1378 /* Copy position arguments (borrowed references) */
1379 memcpy(stack, args, nargs * sizeof(stack[0]));
1380
1381 kwstack = stack + nargs;
1382 pos = i = 0;
1383 /* This loop doesn't support lookup function mutating the dictionary
1384 to change its size. It's a deliberate choice for speed, this function is
1385 called in the performance critical hot code. */
1386 while (PyDict_Next(kwargs, &pos, &key, &value)) {
1387 Py_INCREF(key);
1388 PyTuple_SET_ITEM(kwnames, i, key);
1389 /* The stack contains borrowed references */
1390 kwstack[i] = value;
1391 i++;
1392 }
1393
1394 *p_stack = stack;
1395 *p_kwnames = kwnames;
1396 return 0;
1397}