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