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