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