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