blob: 1209ed3977c72a0aedc5a0be9c1842cb550f02a9 [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 *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020084_PyObject_FastCallDict(PyObject *callable, PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +010085 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 *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200137_PyObject_FastCallKeywords(PyObject *callable, PyObject *const *stack, Py_ssize_t nargs,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100138 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
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200257function_code_fastcall(PyCodeObject *co, PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100258 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 *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200299_PyFunction_FastCallDict(PyObject *func, PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100300 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) &&
Victor Stinner086c3ae2017-10-25 05:26:17 -0700318 (co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100319 {
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 *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200386_PyFunction_FastCallKeywords(PyObject *func, PyObject *const *stack,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100387 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 &&
Victor Stinner086c3ae2017-10-25 05:26:17 -0700405 (co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100406 {
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 *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200446_PyMethodDef_RawFastCallDict(PyMethodDef *method, PyObject *self,
447 PyObject *const *args, Py_ssize_t nargs,
448 PyObject *kwargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100449{
450 /* _PyMethodDef_RawFastCallDict() must not be called with an exception set,
451 because it can clear it (directly or indirectly) and so the
452 caller loses its exception */
453 assert(!PyErr_Occurred());
454
455 assert(method != NULL);
456 assert(nargs >= 0);
457 assert(nargs == 0 || args != NULL);
458 assert(kwargs == NULL || PyDict_Check(kwargs));
459
460 PyCFunction meth = method->ml_meth;
461 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
462 PyObject *result = NULL;
463
464 if (Py_EnterRecursiveCall(" while calling a Python object")) {
465 return NULL;
466 }
467
468 switch (flags)
469 {
470 case METH_NOARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300471 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
472 goto no_keyword_error;
473 }
474
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100475 if (nargs != 0) {
476 PyErr_Format(PyExc_TypeError,
477 "%.200s() takes no arguments (%zd given)",
478 method->ml_name, nargs);
479 goto exit;
480 }
481
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100482 result = (*meth) (self, NULL);
483 break;
484
485 case METH_O:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300486 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
487 goto no_keyword_error;
488 }
489
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100490 if (nargs != 1) {
491 PyErr_Format(PyExc_TypeError,
492 "%.200s() takes exactly one argument (%zd given)",
493 method->ml_name, nargs);
494 goto exit;
495 }
496
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100497 result = (*meth) (self, args[0]);
498 break;
499
500 case METH_VARARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300501 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100502 goto no_keyword_error;
503 }
Stefan Krahf432a322017-08-21 13:09:59 +0200504 /* fall through */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100505
506 case METH_VARARGS | METH_KEYWORDS:
507 {
508 /* Slow-path: create a temporary tuple for positional arguments */
509 PyObject *argstuple = _PyStack_AsTuple(args, nargs);
510 if (argstuple == NULL) {
511 goto exit;
512 }
513
514 if (flags & METH_KEYWORDS) {
515 result = (*(PyCFunctionWithKeywords)meth) (self, argstuple, kwargs);
516 }
517 else {
518 result = (*meth) (self, argstuple);
519 }
520 Py_DECREF(argstuple);
521 break;
522 }
523
524 case METH_FASTCALL:
525 {
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300526 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
527 goto no_keyword_error;
528 }
529
530 result = (*(_PyCFunctionFast)meth) (self, args, nargs);
531 break;
532 }
533
534 case METH_FASTCALL | METH_KEYWORDS:
535 {
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200536 PyObject *const *stack;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100537 PyObject *kwnames;
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300538 _PyCFunctionFastWithKeywords fastmeth = (_PyCFunctionFastWithKeywords)meth;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100539
540 if (_PyStack_UnpackDict(args, nargs, kwargs, &stack, &kwnames) < 0) {
541 goto exit;
542 }
543
544 result = (*fastmeth) (self, stack, nargs, kwnames);
Jeroen Demeyerd092caf2019-05-22 14:52:13 +0200545 if (kwnames != NULL) {
546 Py_ssize_t i, n = nargs + PyTuple_GET_SIZE(kwnames);
547 for (i = 0; i < n; i++) {
548 Py_DECREF(stack[i]);
549 }
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200550 PyMem_Free((PyObject **)stack);
Jeroen Demeyerd092caf2019-05-22 14:52:13 +0200551 Py_DECREF(kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100552 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100553 break;
554 }
555
556 default:
557 PyErr_SetString(PyExc_SystemError,
558 "Bad call flags in _PyMethodDef_RawFastCallDict. "
559 "METH_OLDARGS is no longer supported!");
560 goto exit;
561 }
562
563 goto exit;
564
565no_keyword_error:
566 PyErr_Format(PyExc_TypeError,
567 "%.200s() takes no keyword arguments",
Sylvaind67a1032017-03-27 23:36:08 +0200568 method->ml_name);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100569
570exit:
571 Py_LeaveRecursiveCall();
572 return result;
573}
574
575
576PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200577_PyCFunction_FastCallDict(PyObject *func,
578 PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100579 PyObject *kwargs)
580{
581 PyObject *result;
582
583 assert(func != NULL);
584 assert(PyCFunction_Check(func));
585
586 result = _PyMethodDef_RawFastCallDict(((PyCFunctionObject*)func)->m_ml,
587 PyCFunction_GET_SELF(func),
588 args, nargs, kwargs);
589 result = _Py_CheckFunctionResult(func, result, NULL);
590 return result;
591}
592
593
594PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200595_PyMethodDef_RawFastCallKeywords(PyMethodDef *method, PyObject *self,
596 PyObject *const *args, Py_ssize_t nargs,
597 PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100598{
599 /* _PyMethodDef_RawFastCallKeywords() must not be called with an exception set,
600 because it can clear it (directly or indirectly) and so the
601 caller loses its exception */
602 assert(!PyErr_Occurred());
603
604 assert(method != NULL);
605 assert(nargs >= 0);
606 assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
607 /* kwnames must only contains str strings, no subclass, and all keys must
608 be unique */
609
610 PyCFunction meth = method->ml_meth;
611 int flags = method->ml_flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST);
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300612 Py_ssize_t nkwargs = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100613 PyObject *result = NULL;
614
615 if (Py_EnterRecursiveCall(" while calling a Python object")) {
616 return NULL;
617 }
618
619 switch (flags)
620 {
621 case METH_NOARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300622 if (nkwargs) {
623 goto no_keyword_error;
624 }
625
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100626 if (nargs != 0) {
627 PyErr_Format(PyExc_TypeError,
628 "%.200s() takes no arguments (%zd given)",
629 method->ml_name, nargs);
630 goto exit;
631 }
632
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100633 result = (*meth) (self, NULL);
634 break;
635
636 case METH_O:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300637 if (nkwargs) {
638 goto no_keyword_error;
639 }
640
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100641 if (nargs != 1) {
642 PyErr_Format(PyExc_TypeError,
643 "%.200s() takes exactly one argument (%zd given)",
644 method->ml_name, nargs);
645 goto exit;
646 }
647
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100648 result = (*meth) (self, args[0]);
649 break;
650
651 case METH_FASTCALL:
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300652 if (nkwargs) {
653 goto no_keyword_error;
654 }
655 result = ((_PyCFunctionFast)meth) (self, args, nargs);
656 break;
657
658 case METH_FASTCALL | METH_KEYWORDS:
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100659 /* Fast-path: avoid temporary dict to pass keyword arguments */
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300660 result = ((_PyCFunctionFastWithKeywords)meth) (self, args, nargs, kwnames);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100661 break;
662
663 case METH_VARARGS:
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300664 if (nkwargs) {
665 goto no_keyword_error;
666 }
Stefan Krahf432a322017-08-21 13:09:59 +0200667 /* fall through */
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300668
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100669 case METH_VARARGS | METH_KEYWORDS:
670 {
671 /* Slow-path: create a temporary tuple for positional arguments
672 and a temporary dict for keyword arguments */
673 PyObject *argtuple;
674
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100675 argtuple = _PyStack_AsTuple(args, nargs);
676 if (argtuple == NULL) {
677 goto exit;
678 }
679
680 if (flags & METH_KEYWORDS) {
681 PyObject *kwdict;
682
683 if (nkwargs > 0) {
684 kwdict = _PyStack_AsDict(args + nargs, kwnames);
685 if (kwdict == NULL) {
686 Py_DECREF(argtuple);
687 goto exit;
688 }
689 }
690 else {
691 kwdict = NULL;
692 }
693
694 result = (*(PyCFunctionWithKeywords)meth) (self, argtuple, kwdict);
695 Py_XDECREF(kwdict);
696 }
697 else {
698 result = (*meth) (self, argtuple);
699 }
700 Py_DECREF(argtuple);
701 break;
702 }
703
704 default:
705 PyErr_SetString(PyExc_SystemError,
Stéphane Wirtel9050aaf2019-05-17 13:34:47 +0200706 "Bad call flags in _PyMethodDef_RawFastCallKeywords. "
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100707 "METH_OLDARGS is no longer supported!");
708 goto exit;
709 }
710
711 goto exit;
712
713no_keyword_error:
714 PyErr_Format(PyExc_TypeError,
715 "%.200s() takes no keyword arguments",
716 method->ml_name);
717
718exit:
719 Py_LeaveRecursiveCall();
720 return result;
721}
722
723
724PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200725_PyCFunction_FastCallKeywords(PyObject *func,
726 PyObject *const *args, Py_ssize_t nargs,
727 PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100728{
729 PyObject *result;
730
731 assert(func != NULL);
732 assert(PyCFunction_Check(func));
733
734 result = _PyMethodDef_RawFastCallKeywords(((PyCFunctionObject*)func)->m_ml,
735 PyCFunction_GET_SELF(func),
736 args, nargs, kwnames);
737 result = _Py_CheckFunctionResult(func, result, NULL);
738 return result;
739}
740
741
742static PyObject *
743cfunction_call_varargs(PyObject *func, PyObject *args, PyObject *kwargs)
744{
745 assert(!PyErr_Occurred());
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300746 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100747
748 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
749 PyObject *self = PyCFunction_GET_SELF(func);
750 PyObject *result;
751
752 if (PyCFunction_GET_FLAGS(func) & METH_KEYWORDS) {
753 if (Py_EnterRecursiveCall(" while calling a Python object")) {
754 return NULL;
755 }
756
757 result = (*(PyCFunctionWithKeywords)meth)(self, args, kwargs);
758
759 Py_LeaveRecursiveCall();
760 }
761 else {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +0300762 if (kwargs != NULL && PyDict_GET_SIZE(kwargs) != 0) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100763 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
764 ((PyCFunctionObject*)func)->m_ml->ml_name);
765 return NULL;
766 }
767
768 if (Py_EnterRecursiveCall(" while calling a Python object")) {
769 return NULL;
770 }
771
772 result = (*meth)(self, args);
773
774 Py_LeaveRecursiveCall();
775 }
776
777 return _Py_CheckFunctionResult(func, result, NULL);
778}
779
780
781PyObject *
782PyCFunction_Call(PyObject *func, PyObject *args, PyObject *kwargs)
783{
784 /* first try METH_VARARGS to pass directly args tuple unchanged.
785 _PyMethodDef_RawFastCallDict() creates a new temporary tuple
786 for METH_VARARGS. */
787 if (PyCFunction_GET_FLAGS(func) & METH_VARARGS) {
788 return cfunction_call_varargs(func, args, kwargs);
789 }
790 else {
791 return _PyCFunction_FastCallDict(func,
792 &PyTuple_GET_ITEM(args, 0),
793 PyTuple_GET_SIZE(args),
794 kwargs);
795 }
796}
797
798
799/* --- More complex call functions -------------------------------- */
800
801/* External interface to call any callable object.
802 The args must be a tuple or NULL. The kwargs must be a dict or NULL. */
803PyObject *
804PyEval_CallObjectWithKeywords(PyObject *callable,
805 PyObject *args, PyObject *kwargs)
806{
807#ifdef Py_DEBUG
808 /* PyEval_CallObjectWithKeywords() must not be called with an exception
809 set. It raises a new exception if parameters are invalid or if
810 PyTuple_New() fails, and so the original exception is lost. */
811 assert(!PyErr_Occurred());
812#endif
813
INADA Naoki3824cd82017-03-01 20:41:03 +0900814 if (args != NULL && !PyTuple_Check(args)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100815 PyErr_SetString(PyExc_TypeError,
816 "argument list must be a tuple");
817 return NULL;
818 }
819
820 if (kwargs != NULL && !PyDict_Check(kwargs)) {
821 PyErr_SetString(PyExc_TypeError,
822 "keyword list must be a dictionary");
823 return NULL;
824 }
825
INADA Naoki3824cd82017-03-01 20:41:03 +0900826 if (args == NULL) {
827 return _PyObject_FastCallDict(callable, NULL, 0, kwargs);
828 }
829 else {
830 return PyObject_Call(callable, args, kwargs);
831 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100832}
833
834
835PyObject *
836PyObject_CallObject(PyObject *callable, PyObject *args)
837{
838 return PyEval_CallObjectWithKeywords(callable, args, NULL);
839}
840
841
842/* Positional arguments are obj followed by args:
843 call callable(obj, *args, **kwargs) */
844PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200845_PyObject_FastCall_Prepend(PyObject *callable, PyObject *obj,
846 PyObject *const *args, Py_ssize_t nargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100847{
848 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
849 PyObject **args2;
850 PyObject *result;
851
852 nargs++;
853 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
854 args2 = small_stack;
855 }
856 else {
857 args2 = PyMem_Malloc(nargs * sizeof(PyObject *));
858 if (args2 == NULL) {
859 PyErr_NoMemory();
860 return NULL;
861 }
862 }
863
864 /* use borrowed references */
865 args2[0] = obj;
Benjamin Petersona3070d52017-09-04 22:23:42 -0700866 if (nargs > 1) {
867 memcpy(&args2[1], args, (nargs - 1) * sizeof(PyObject *));
868 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100869
870 result = _PyObject_FastCall(callable, args2, nargs);
871 if (args2 != small_stack) {
872 PyMem_Free(args2);
873 }
874 return result;
875}
876
877
878/* Call callable(obj, *args, **kwargs). */
879PyObject *
880_PyObject_Call_Prepend(PyObject *callable,
881 PyObject *obj, PyObject *args, PyObject *kwargs)
882{
883 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
884 PyObject **stack;
885 Py_ssize_t argcount;
886 PyObject *result;
887
888 assert(PyTuple_Check(args));
889
890 argcount = PyTuple_GET_SIZE(args);
891 if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
892 stack = small_stack;
893 }
894 else {
895 stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
896 if (stack == NULL) {
897 PyErr_NoMemory();
898 return NULL;
899 }
900 }
901
902 /* use borrowed references */
903 stack[0] = obj;
904 memcpy(&stack[1],
905 &PyTuple_GET_ITEM(args, 0),
906 argcount * sizeof(PyObject *));
907
908 result = _PyObject_FastCallDict(callable,
909 stack, argcount + 1,
910 kwargs);
911 if (stack != small_stack) {
912 PyMem_Free(stack);
913 }
914 return result;
915}
916
917
918/* --- Call with a format string ---------------------------------- */
919
920static PyObject *
921_PyObject_CallFunctionVa(PyObject *callable, const char *format,
922 va_list va, int is_size_t)
923{
924 PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
925 const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
926 PyObject **stack;
927 Py_ssize_t nargs, i;
928 PyObject *result;
929
930 if (callable == NULL) {
931 return null_error();
932 }
933
934 if (!format || !*format) {
935 return _PyObject_CallNoArg(callable);
936 }
937
938 if (is_size_t) {
939 stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
940 format, va, &nargs);
941 }
942 else {
943 stack = _Py_VaBuildStack(small_stack, small_stack_len,
944 format, va, &nargs);
945 }
946 if (stack == NULL) {
947 return NULL;
948 }
949
950 if (nargs == 1 && PyTuple_Check(stack[0])) {
951 /* Special cases for backward compatibility:
952 - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
953 - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
954 func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
955 PyObject *args = stack[0];
956 result = _PyObject_FastCall(callable,
957 &PyTuple_GET_ITEM(args, 0),
958 PyTuple_GET_SIZE(args));
959 }
960 else {
961 result = _PyObject_FastCall(callable, stack, nargs);
962 }
963
964 for (i = 0; i < nargs; ++i) {
965 Py_DECREF(stack[i]);
966 }
967 if (stack != small_stack) {
968 PyMem_Free(stack);
969 }
970 return result;
971}
972
973
974PyObject *
975PyObject_CallFunction(PyObject *callable, const char *format, ...)
976{
977 va_list va;
978 PyObject *result;
979
980 va_start(va, format);
981 result = _PyObject_CallFunctionVa(callable, format, va, 0);
982 va_end(va);
983
984 return result;
985}
986
987
INADA Naokiaa289a52017-03-14 18:00:59 +0900988/* PyEval_CallFunction is exact copy of PyObject_CallFunction.
989 * This function is kept for backward compatibility.
990 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100991PyObject *
992PyEval_CallFunction(PyObject *callable, const char *format, ...)
993{
INADA Naokiaa289a52017-03-14 18:00:59 +0900994 va_list va;
995 PyObject *result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +0100996
INADA Naokiaa289a52017-03-14 18:00:59 +0900997 va_start(va, format);
998 result = _PyObject_CallFunctionVa(callable, format, va, 0);
999 va_end(va);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001000
INADA Naokiaa289a52017-03-14 18:00:59 +09001001 return result;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001002}
1003
1004
1005PyObject *
1006_PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
1007{
1008 va_list va;
1009 PyObject *result;
1010
1011 va_start(va, format);
1012 result = _PyObject_CallFunctionVa(callable, format, va, 1);
1013 va_end(va);
1014
1015 return result;
1016}
1017
1018
1019static PyObject*
1020callmethod(PyObject* callable, const char *format, va_list va, int is_size_t)
1021{
1022 assert(callable != NULL);
1023
1024 if (!PyCallable_Check(callable)) {
1025 PyErr_Format(PyExc_TypeError,
1026 "attribute of type '%.200s' is not callable",
1027 Py_TYPE(callable)->tp_name);
1028 return NULL;
1029 }
1030
1031 return _PyObject_CallFunctionVa(callable, format, va, is_size_t);
1032}
1033
1034
1035PyObject *
1036PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
1037{
1038 va_list va;
1039 PyObject *callable, *retval;
1040
1041 if (obj == NULL || name == NULL) {
1042 return null_error();
1043 }
1044
1045 callable = PyObject_GetAttrString(obj, name);
1046 if (callable == NULL)
1047 return NULL;
1048
1049 va_start(va, format);
1050 retval = callmethod(callable, format, va, 0);
1051 va_end(va);
1052
1053 Py_DECREF(callable);
1054 return retval;
1055}
1056
1057
INADA Naokiaa289a52017-03-14 18:00:59 +09001058/* PyEval_CallMethod is exact copy of PyObject_CallMethod.
1059 * This function is kept for backward compatibility.
1060 */
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001061PyObject *
1062PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
1063{
INADA Naokiaa289a52017-03-14 18:00:59 +09001064 va_list va;
1065 PyObject *callable, *retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001066
INADA Naokiaa289a52017-03-14 18:00:59 +09001067 if (obj == NULL || name == NULL) {
1068 return null_error();
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001069 }
1070
INADA Naokiaa289a52017-03-14 18:00:59 +09001071 callable = PyObject_GetAttrString(obj, name);
1072 if (callable == NULL)
1073 return NULL;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001074
INADA Naokiaa289a52017-03-14 18:00:59 +09001075 va_start(va, format);
1076 retval = callmethod(callable, format, va, 0);
1077 va_end(va);
1078
1079 Py_DECREF(callable);
1080 return retval;
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001081}
1082
1083
1084PyObject *
1085_PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
1086 const char *format, ...)
1087{
1088 va_list va;
1089 PyObject *callable, *retval;
1090
1091 if (obj == NULL || name == NULL) {
1092 return null_error();
1093 }
1094
1095 callable = _PyObject_GetAttrId(obj, name);
1096 if (callable == NULL)
1097 return NULL;
1098
1099 va_start(va, format);
1100 retval = callmethod(callable, format, va, 0);
1101 va_end(va);
1102
1103 Py_DECREF(callable);
1104 return retval;
1105}
1106
1107
1108PyObject *
1109_PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
1110 const char *format, ...)
1111{
1112 va_list va;
1113 PyObject *callable, *retval;
1114
1115 if (obj == NULL || name == NULL) {
1116 return null_error();
1117 }
1118
1119 callable = PyObject_GetAttrString(obj, name);
1120 if (callable == NULL)
1121 return NULL;
1122
1123 va_start(va, format);
1124 retval = callmethod(callable, format, va, 1);
1125 va_end(va);
1126
1127 Py_DECREF(callable);
1128 return retval;
1129}
1130
1131
1132PyObject *
1133_PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
1134 const char *format, ...)
1135{
1136 va_list va;
1137 PyObject *callable, *retval;
1138
1139 if (obj == NULL || name == NULL) {
1140 return null_error();
1141 }
1142
1143 callable = _PyObject_GetAttrId(obj, name);
1144 if (callable == NULL) {
1145 return NULL;
1146 }
1147
1148 va_start(va, format);
1149 retval = callmethod(callable, format, va, 1);
1150 va_end(va);
1151
1152 Py_DECREF(callable);
1153 return retval;
1154}
1155
1156
1157/* --- Call with "..." arguments ---------------------------------- */
1158
1159static PyObject *
1160object_vacall(PyObject *callable, va_list vargs)
1161{
1162 PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
1163 PyObject **stack;
1164 Py_ssize_t nargs;
1165 PyObject *result;
1166 Py_ssize_t i;
1167 va_list countva;
1168
1169 if (callable == NULL) {
1170 return null_error();
1171 }
1172
1173 /* Count the number of arguments */
1174 va_copy(countva, vargs);
1175 nargs = 0;
1176 while (1) {
1177 PyObject *arg = va_arg(countva, PyObject *);
1178 if (arg == NULL) {
1179 break;
1180 }
1181 nargs++;
1182 }
1183 va_end(countva);
1184
1185 /* Copy arguments */
1186 if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
1187 stack = small_stack;
1188 }
1189 else {
1190 stack = PyMem_Malloc(nargs * sizeof(stack[0]));
1191 if (stack == NULL) {
1192 PyErr_NoMemory();
1193 return NULL;
1194 }
1195 }
1196
1197 for (i = 0; i < nargs; ++i) {
1198 stack[i] = va_arg(vargs, PyObject *);
1199 }
1200
1201 /* Call the function */
1202 result = _PyObject_FastCall(callable, stack, nargs);
1203
1204 if (stack != small_stack) {
1205 PyMem_Free(stack);
1206 }
1207 return result;
1208}
1209
1210
1211PyObject *
1212PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
1213{
1214 va_list vargs;
1215 PyObject *result;
1216
1217 if (callable == NULL || name == NULL) {
1218 return null_error();
1219 }
1220
1221 callable = PyObject_GetAttr(callable, name);
1222 if (callable == NULL) {
1223 return NULL;
1224 }
1225
1226 va_start(vargs, name);
1227 result = object_vacall(callable, vargs);
1228 va_end(vargs);
1229
1230 Py_DECREF(callable);
1231 return result;
1232}
1233
1234
1235PyObject *
1236_PyObject_CallMethodIdObjArgs(PyObject *obj,
1237 struct _Py_Identifier *name, ...)
1238{
1239 va_list vargs;
1240 PyObject *callable, *result;
1241
1242 if (obj == NULL || name == NULL) {
1243 return null_error();
1244 }
1245
1246 callable = _PyObject_GetAttrId(obj, name);
1247 if (callable == NULL) {
1248 return NULL;
1249 }
1250
1251 va_start(vargs, name);
1252 result = object_vacall(callable, vargs);
1253 va_end(vargs);
1254
1255 Py_DECREF(callable);
1256 return result;
1257}
1258
1259
1260PyObject *
1261PyObject_CallFunctionObjArgs(PyObject *callable, ...)
1262{
1263 va_list vargs;
1264 PyObject *result;
1265
1266 va_start(vargs, callable);
1267 result = object_vacall(callable, vargs);
1268 va_end(vargs);
1269
1270 return result;
1271}
1272
1273
1274/* --- PyStack functions ------------------------------------------ */
1275
1276/* Issue #29234: Inlining _PyStack_AsTuple() into callers increases their
1277 stack consumption, Disable inlining to optimize the stack consumption. */
1278PyObject* _Py_NO_INLINE
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001279_PyStack_AsTuple(PyObject *const *stack, Py_ssize_t nargs)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001280{
1281 PyObject *args;
1282 Py_ssize_t i;
1283
1284 args = PyTuple_New(nargs);
1285 if (args == NULL) {
1286 return NULL;
1287 }
1288
1289 for (i=0; i < nargs; i++) {
1290 PyObject *item = stack[i];
1291 Py_INCREF(item);
1292 PyTuple_SET_ITEM(args, i, item);
1293 }
1294 return args;
1295}
1296
1297
1298PyObject*
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001299_PyStack_AsTupleSlice(PyObject *const *stack, Py_ssize_t nargs,
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001300 Py_ssize_t start, Py_ssize_t end)
1301{
1302 PyObject *args;
1303 Py_ssize_t i;
1304
1305 assert(0 <= start);
1306 assert(end <= nargs);
1307 assert(start <= end);
1308
1309 args = PyTuple_New(end - start);
1310 if (args == NULL) {
1311 return NULL;
1312 }
1313
1314 for (i=start; i < end; i++) {
1315 PyObject *item = stack[i];
1316 Py_INCREF(item);
1317 PyTuple_SET_ITEM(args, i - start, item);
1318 }
1319 return args;
1320}
1321
1322
1323PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001324_PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001325{
1326 Py_ssize_t nkwargs;
1327 PyObject *kwdict;
1328 Py_ssize_t i;
1329
1330 assert(kwnames != NULL);
1331 nkwargs = PyTuple_GET_SIZE(kwnames);
1332 kwdict = _PyDict_NewPresized(nkwargs);
1333 if (kwdict == NULL) {
1334 return NULL;
1335 }
1336
1337 for (i = 0; i < nkwargs; i++) {
1338 PyObject *key = PyTuple_GET_ITEM(kwnames, i);
1339 PyObject *value = *values++;
1340 /* If key already exists, replace it with the new value */
1341 if (PyDict_SetItem(kwdict, key, value)) {
1342 Py_DECREF(kwdict);
1343 return NULL;
1344 }
1345 }
1346 return kwdict;
1347}
1348
1349
1350int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001351_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs,
1352 PyObject *const **p_stack, PyObject **p_kwnames)
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001353{
1354 PyObject **stack, **kwstack;
1355 Py_ssize_t nkwargs;
1356 Py_ssize_t pos, i;
1357 PyObject *key, *value;
1358 PyObject *kwnames;
1359
1360 assert(nargs >= 0);
1361 assert(kwargs == NULL || PyDict_CheckExact(kwargs));
1362
1363 if (kwargs == NULL || (nkwargs = PyDict_GET_SIZE(kwargs)) == 0) {
1364 *p_stack = args;
1365 *p_kwnames = NULL;
1366 return 0;
1367 }
1368
1369 if ((size_t)nargs > PY_SSIZE_T_MAX / sizeof(stack[0]) - (size_t)nkwargs) {
1370 PyErr_NoMemory();
1371 return -1;
1372 }
1373
1374 stack = PyMem_Malloc((nargs + nkwargs) * sizeof(stack[0]));
1375 if (stack == NULL) {
1376 PyErr_NoMemory();
1377 return -1;
1378 }
1379
1380 kwnames = PyTuple_New(nkwargs);
1381 if (kwnames == NULL) {
1382 PyMem_Free(stack);
1383 return -1;
1384 }
1385
Jeroen Demeyerd092caf2019-05-22 14:52:13 +02001386 /* Copy positional arguments */
1387 for (i = 0; i < nargs; i++) {
1388 Py_INCREF(args[i]);
1389 stack[i] = args[i];
1390 }
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001391
1392 kwstack = stack + nargs;
1393 pos = i = 0;
1394 /* This loop doesn't support lookup function mutating the dictionary
1395 to change its size. It's a deliberate choice for speed, this function is
1396 called in the performance critical hot code. */
1397 while (PyDict_Next(kwargs, &pos, &key, &value)) {
1398 Py_INCREF(key);
Jeroen Demeyerd092caf2019-05-22 14:52:13 +02001399 Py_INCREF(value);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001400 PyTuple_SET_ITEM(kwnames, i, key);
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01001401 kwstack[i] = value;
1402 i++;
1403 }
1404
1405 *p_stack = stack;
1406 *p_kwnames = kwnames;
1407 return 0;
1408}