You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1397 lines
37 KiB

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