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.

1026 lines
29 KiB

  1. #include "Python.h"
  2. #include "pycore_call.h"
  3. #include "pycore_ceval.h" /* _PyEval_EvalFrame() */
  4. #include "pycore_object.h"
  5. #include "pycore_pyerrors.h"
  6. #include "pycore_pystate.h"
  7. #include "pycore_tupleobject.h"
  8. #include "frameobject.h"
  9. static PyObject *const *
  10. _PyStack_UnpackDict(PyThreadState *tstate,
  11. PyObject *const *args, Py_ssize_t nargs,
  12. PyObject *kwargs, PyObject **p_kwnames);
  13. static void
  14. _PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
  15. PyObject *kwnames);
  16. static PyObject *
  17. null_error(PyThreadState *tstate)
  18. {
  19. if (!_PyErr_Occurred(tstate)) {
  20. _PyErr_SetString(tstate, PyExc_SystemError,
  21. "null argument to internal routine");
  22. }
  23. return NULL;
  24. }
  25. PyObject*
  26. _Py_CheckFunctionResult(PyThreadState *tstate, PyObject *callable,
  27. PyObject *result, const char *where)
  28. {
  29. assert((callable != NULL) ^ (where != NULL));
  30. if (result == NULL) {
  31. if (!_PyErr_Occurred(tstate)) {
  32. if (callable)
  33. _PyErr_Format(tstate, PyExc_SystemError,
  34. "%R returned NULL without setting an error",
  35. callable);
  36. else
  37. _PyErr_Format(tstate, PyExc_SystemError,
  38. "%s returned NULL without setting an error",
  39. where);
  40. #ifdef Py_DEBUG
  41. /* Ensure that the bug is caught in debug mode */
  42. Py_FatalError("a function returned NULL without setting an error");
  43. #endif
  44. return NULL;
  45. }
  46. }
  47. else {
  48. if (_PyErr_Occurred(tstate)) {
  49. Py_DECREF(result);
  50. if (callable) {
  51. _PyErr_FormatFromCauseTstate(
  52. tstate, PyExc_SystemError,
  53. "%R returned a result with an error set", callable);
  54. }
  55. else {
  56. _PyErr_FormatFromCauseTstate(
  57. tstate, PyExc_SystemError,
  58. "%s returned a result with an error set", where);
  59. }
  60. #ifdef Py_DEBUG
  61. /* Ensure that the bug is caught in debug mode */
  62. Py_FatalError("a function returned a result with an error set");
  63. #endif
  64. return NULL;
  65. }
  66. }
  67. return result;
  68. }
  69. /* --- Core PyObject call functions ------------------------------- */
  70. /* Call a callable Python object without any arguments */
  71. PyObject *
  72. PyObject_CallNoArgs(PyObject *func)
  73. {
  74. PyThreadState *tstate = _PyThreadState_GET();
  75. return _PyObject_CallNoArgTstate(tstate, func);
  76. }
  77. PyObject *
  78. _PyObject_FastCallDictTstate(PyThreadState *tstate, PyObject *callable,
  79. PyObject *const *args, size_t nargsf,
  80. PyObject *kwargs)
  81. {
  82. assert(callable != NULL);
  83. /* _PyObject_FastCallDict() must not be called with an exception set,
  84. because it can clear it (directly or indirectly) and so the
  85. caller loses its exception */
  86. assert(!_PyErr_Occurred(tstate));
  87. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  88. assert(nargs >= 0);
  89. assert(nargs == 0 || args != NULL);
  90. assert(kwargs == NULL || PyDict_Check(kwargs));
  91. vectorcallfunc func = _PyVectorcall_Function(callable);
  92. if (func == NULL) {
  93. /* Use tp_call instead */
  94. return _PyObject_MakeTpCall(tstate, callable, args, nargs, kwargs);
  95. }
  96. PyObject *res;
  97. if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
  98. res = func(callable, args, nargsf, NULL);
  99. }
  100. else {
  101. PyObject *kwnames;
  102. PyObject *const *newargs;
  103. newargs = _PyStack_UnpackDict(tstate,
  104. args, nargs,
  105. kwargs, &kwnames);
  106. if (newargs == NULL) {
  107. return NULL;
  108. }
  109. res = func(callable, newargs,
  110. nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
  111. _PyStack_UnpackDict_Free(newargs, nargs, kwnames);
  112. }
  113. return _Py_CheckFunctionResult(tstate, callable, res, NULL);
  114. }
  115. PyObject *
  116. _PyObject_FastCallDict(PyObject *callable, PyObject *const *args,
  117. size_t nargsf, PyObject *kwargs)
  118. {
  119. PyThreadState *tstate = _PyThreadState_GET();
  120. return _PyObject_FastCallDictTstate(tstate, callable, args, nargsf, kwargs);
  121. }
  122. PyObject *
  123. _PyObject_MakeTpCall(PyThreadState *tstate, PyObject *callable,
  124. PyObject *const *args, Py_ssize_t nargs,
  125. PyObject *keywords)
  126. {
  127. assert(nargs >= 0);
  128. assert(nargs == 0 || args != NULL);
  129. assert(keywords == NULL || PyTuple_Check(keywords) || PyDict_Check(keywords));
  130. /* Slow path: build a temporary tuple for positional arguments and a
  131. * temporary dictionary for keyword arguments (if any) */
  132. ternaryfunc call = Py_TYPE(callable)->tp_call;
  133. if (call == NULL) {
  134. _PyErr_Format(tstate, PyExc_TypeError,
  135. "'%.200s' object is not callable",
  136. Py_TYPE(callable)->tp_name);
  137. return NULL;
  138. }
  139. PyObject *argstuple = _PyTuple_FromArray(args, nargs);
  140. if (argstuple == NULL) {
  141. return NULL;
  142. }
  143. PyObject *kwdict;
  144. if (keywords == NULL || PyDict_Check(keywords)) {
  145. kwdict = keywords;
  146. }
  147. else {
  148. if (PyTuple_GET_SIZE(keywords)) {
  149. assert(args != NULL);
  150. kwdict = _PyStack_AsDict(args + nargs, keywords);
  151. if (kwdict == NULL) {
  152. Py_DECREF(argstuple);
  153. return NULL;
  154. }
  155. }
  156. else {
  157. keywords = kwdict = NULL;
  158. }
  159. }
  160. PyObject *result = NULL;
  161. if (_Py_EnterRecursiveCall(tstate, " while calling a Python object") == 0)
  162. {
  163. result = call(callable, argstuple, kwdict);
  164. _Py_LeaveRecursiveCall(tstate);
  165. }
  166. Py_DECREF(argstuple);
  167. if (kwdict != keywords) {
  168. Py_DECREF(kwdict);
  169. }
  170. return _Py_CheckFunctionResult(tstate, callable, result, NULL);
  171. }
  172. PyObject *
  173. PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs)
  174. {
  175. PyThreadState *tstate = _PyThreadState_GET();
  176. /* get vectorcallfunc as in _PyVectorcall_Function, but without
  177. * the _Py_TPFLAGS_HAVE_VECTORCALL check */
  178. Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset;
  179. if (offset <= 0) {
  180. _PyErr_Format(tstate, PyExc_TypeError,
  181. "'%.200s' object does not support vectorcall",
  182. Py_TYPE(callable)->tp_name);
  183. return NULL;
  184. }
  185. vectorcallfunc func = *(vectorcallfunc *)(((char *)callable) + offset);
  186. if (func == NULL) {
  187. _PyErr_Format(tstate, PyExc_TypeError,
  188. "'%.200s' object does not support vectorcall",
  189. Py_TYPE(callable)->tp_name);
  190. return NULL;
  191. }
  192. Py_ssize_t nargs = PyTuple_GET_SIZE(tuple);
  193. /* Fast path for no keywords */
  194. if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
  195. return func(callable, _PyTuple_ITEMS(tuple), nargs, NULL);
  196. }
  197. /* Convert arguments & call */
  198. PyObject *const *args;
  199. PyObject *kwnames;
  200. args = _PyStack_UnpackDict(tstate,
  201. _PyTuple_ITEMS(tuple), nargs,
  202. kwargs, &kwnames);
  203. if (args == NULL) {
  204. return NULL;
  205. }
  206. PyObject *result = func(callable, args,
  207. nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
  208. _PyStack_UnpackDict_Free(args, nargs, kwnames);
  209. return _Py_CheckFunctionResult(tstate, callable, result, NULL);
  210. }
  211. PyObject *
  212. _PyObject_Call(PyThreadState *tstate, PyObject *callable,
  213. PyObject *args, PyObject *kwargs)
  214. {
  215. ternaryfunc call;
  216. PyObject *result;
  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(tstate));
  221. assert(PyTuple_Check(args));
  222. assert(kwargs == NULL || PyDict_Check(kwargs));
  223. if (_PyVectorcall_Function(callable) != NULL) {
  224. return PyVectorcall_Call(callable, args, kwargs);
  225. }
  226. else {
  227. call = callable->ob_type->tp_call;
  228. if (call == NULL) {
  229. _PyErr_Format(tstate, PyExc_TypeError,
  230. "'%.200s' object is not callable",
  231. callable->ob_type->tp_name);
  232. return NULL;
  233. }
  234. if (_Py_EnterRecursiveCall(tstate, " while calling a Python object")) {
  235. return NULL;
  236. }
  237. result = (*call)(callable, args, kwargs);
  238. _Py_LeaveRecursiveCall(tstate);
  239. return _Py_CheckFunctionResult(tstate, callable, result, NULL);
  240. }
  241. }
  242. PyObject *
  243. PyObject_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
  244. {
  245. PyThreadState *tstate = _PyThreadState_GET();
  246. return _PyObject_Call(tstate, callable, args, kwargs);
  247. }
  248. PyObject *
  249. PyCFunction_Call(PyObject *callable, PyObject *args, PyObject *kwargs)
  250. {
  251. PyThreadState *tstate = _PyThreadState_GET();
  252. return _PyObject_Call(tstate, callable, args, kwargs);
  253. }
  254. /* --- PyFunction call functions ---------------------------------- */
  255. static PyObject* _Py_HOT_FUNCTION
  256. function_code_fastcall(PyThreadState *tstate, PyCodeObject *co,
  257. PyObject *const *args, Py_ssize_t nargs,
  258. PyObject *globals)
  259. {
  260. assert(tstate != NULL);
  261. assert(globals != NULL);
  262. /* XXX Perhaps we should create a specialized
  263. _PyFrame_New_NoTrack() that doesn't take locals, but does
  264. take builtins without sanity checking them.
  265. */
  266. PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, NULL);
  267. if (f == NULL) {
  268. return NULL;
  269. }
  270. PyObject **fastlocals = f->f_localsplus;
  271. for (Py_ssize_t i = 0; i < nargs; i++) {
  272. Py_INCREF(*args);
  273. fastlocals[i] = *args++;
  274. }
  275. PyObject *result = _PyEval_EvalFrame(tstate, f, 0);
  276. if (Py_REFCNT(f) > 1) {
  277. Py_DECREF(f);
  278. _PyObject_GC_TRACK(f);
  279. }
  280. else {
  281. ++tstate->recursion_depth;
  282. Py_DECREF(f);
  283. --tstate->recursion_depth;
  284. }
  285. return result;
  286. }
  287. PyObject *
  288. _PyFunction_Vectorcall(PyObject *func, PyObject* const* stack,
  289. size_t nargsf, PyObject *kwnames)
  290. {
  291. assert(PyFunction_Check(func));
  292. assert(kwnames == NULL || PyTuple_CheckExact(kwnames));
  293. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  294. assert(nargs >= 0);
  295. Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
  296. assert((nargs == 0 && nkwargs == 0) || stack != NULL);
  297. /* kwnames must only contain strings and all keys must be unique */
  298. PyThreadState *tstate = _PyThreadState_GET();
  299. PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
  300. PyObject *globals = PyFunction_GET_GLOBALS(func);
  301. PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
  302. if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
  303. (co->co_flags & ~PyCF_MASK) == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
  304. {
  305. if (argdefs == NULL && co->co_argcount == nargs) {
  306. return function_code_fastcall(tstate, co, stack, nargs, globals);
  307. }
  308. else if (nargs == 0 && argdefs != NULL
  309. && co->co_argcount == PyTuple_GET_SIZE(argdefs)) {
  310. /* function called with no arguments, but all parameters have
  311. a default value: use default values as arguments .*/
  312. stack = _PyTuple_ITEMS(argdefs);
  313. return function_code_fastcall(tstate, co,
  314. stack, PyTuple_GET_SIZE(argdefs),
  315. globals);
  316. }
  317. }
  318. PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
  319. PyObject *closure = PyFunction_GET_CLOSURE(func);
  320. PyObject *name = ((PyFunctionObject *)func) -> func_name;
  321. PyObject *qualname = ((PyFunctionObject *)func) -> func_qualname;
  322. PyObject **d;
  323. Py_ssize_t nd;
  324. if (argdefs != NULL) {
  325. d = _PyTuple_ITEMS(argdefs);
  326. nd = PyTuple_GET_SIZE(argdefs);
  327. assert(nd <= INT_MAX);
  328. }
  329. else {
  330. d = NULL;
  331. nd = 0;
  332. }
  333. return _PyEval_EvalCode(tstate,
  334. (PyObject*)co, globals, (PyObject *)NULL,
  335. stack, nargs,
  336. nkwargs ? _PyTuple_ITEMS(kwnames) : NULL,
  337. stack + nargs,
  338. nkwargs, 1,
  339. d, (int)nd, kwdefs,
  340. closure, name, qualname);
  341. }
  342. /* --- More complex call functions -------------------------------- */
  343. /* External interface to call any callable object.
  344. The args must be a tuple or NULL. The kwargs must be a dict or NULL. */
  345. PyObject *
  346. PyEval_CallObjectWithKeywords(PyObject *callable,
  347. PyObject *args, PyObject *kwargs)
  348. {
  349. PyThreadState *tstate = _PyThreadState_GET();
  350. #ifdef Py_DEBUG
  351. /* PyEval_CallObjectWithKeywords() must not be called with an exception
  352. set. It raises a new exception if parameters are invalid or if
  353. PyTuple_New() fails, and so the original exception is lost. */
  354. assert(!_PyErr_Occurred(tstate));
  355. #endif
  356. if (args != NULL && !PyTuple_Check(args)) {
  357. _PyErr_SetString(tstate, PyExc_TypeError,
  358. "argument list must be a tuple");
  359. return NULL;
  360. }
  361. if (kwargs != NULL && !PyDict_Check(kwargs)) {
  362. _PyErr_SetString(tstate, PyExc_TypeError,
  363. "keyword list must be a dictionary");
  364. return NULL;
  365. }
  366. if (args == NULL) {
  367. return _PyObject_FastCallDictTstate(tstate, callable, NULL, 0, kwargs);
  368. }
  369. else {
  370. return _PyObject_Call(tstate, callable, args, kwargs);
  371. }
  372. }
  373. PyObject *
  374. PyObject_CallObject(PyObject *callable, PyObject *args)
  375. {
  376. PyThreadState *tstate = _PyThreadState_GET();
  377. assert(!_PyErr_Occurred(tstate));
  378. if (args == NULL) {
  379. return _PyObject_CallNoArgTstate(tstate, callable);
  380. }
  381. if (!PyTuple_Check(args)) {
  382. _PyErr_SetString(tstate, PyExc_TypeError,
  383. "argument list must be a tuple");
  384. return NULL;
  385. }
  386. return _PyObject_Call(tstate, callable, args, NULL);
  387. }
  388. /* Call callable(obj, *args, **kwargs). */
  389. PyObject *
  390. _PyObject_Call_Prepend(PyThreadState *tstate, PyObject *callable,
  391. PyObject *obj, PyObject *args, PyObject *kwargs)
  392. {
  393. assert(PyTuple_Check(args));
  394. PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
  395. PyObject **stack;
  396. Py_ssize_t argcount = PyTuple_GET_SIZE(args);
  397. if (argcount + 1 <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
  398. stack = small_stack;
  399. }
  400. else {
  401. stack = PyMem_Malloc((argcount + 1) * sizeof(PyObject *));
  402. if (stack == NULL) {
  403. PyErr_NoMemory();
  404. return NULL;
  405. }
  406. }
  407. /* use borrowed references */
  408. stack[0] = obj;
  409. memcpy(&stack[1],
  410. _PyTuple_ITEMS(args),
  411. argcount * sizeof(PyObject *));
  412. PyObject *result = _PyObject_FastCallDictTstate(tstate, callable,
  413. stack, argcount + 1,
  414. kwargs);
  415. if (stack != small_stack) {
  416. PyMem_Free(stack);
  417. }
  418. return result;
  419. }
  420. /* --- Call with a format string ---------------------------------- */
  421. static PyObject *
  422. _PyObject_CallFunctionVa(PyThreadState *tstate, PyObject *callable,
  423. const char *format, va_list va, int is_size_t)
  424. {
  425. PyObject* small_stack[_PY_FASTCALL_SMALL_STACK];
  426. const Py_ssize_t small_stack_len = Py_ARRAY_LENGTH(small_stack);
  427. PyObject **stack;
  428. Py_ssize_t nargs, i;
  429. PyObject *result;
  430. if (callable == NULL) {
  431. return null_error(tstate);
  432. }
  433. if (!format || !*format) {
  434. return _PyObject_CallNoArgTstate(tstate, callable);
  435. }
  436. if (is_size_t) {
  437. stack = _Py_VaBuildStack_SizeT(small_stack, small_stack_len,
  438. format, va, &nargs);
  439. }
  440. else {
  441. stack = _Py_VaBuildStack(small_stack, small_stack_len,
  442. format, va, &nargs);
  443. }
  444. if (stack == NULL) {
  445. return NULL;
  446. }
  447. if (nargs == 1 && PyTuple_Check(stack[0])) {
  448. /* Special cases for backward compatibility:
  449. - PyObject_CallFunction(func, "O", tuple) calls func(*tuple)
  450. - PyObject_CallFunction(func, "(OOO)", arg1, arg2, arg3) calls
  451. func(*(arg1, arg2, arg3)): func(arg1, arg2, arg3) */
  452. PyObject *args = stack[0];
  453. result = _PyObject_VectorcallTstate(tstate, callable,
  454. _PyTuple_ITEMS(args),
  455. PyTuple_GET_SIZE(args),
  456. NULL);
  457. }
  458. else {
  459. result = _PyObject_VectorcallTstate(tstate, callable,
  460. stack, nargs, NULL);
  461. }
  462. for (i = 0; i < nargs; ++i) {
  463. Py_DECREF(stack[i]);
  464. }
  465. if (stack != small_stack) {
  466. PyMem_Free(stack);
  467. }
  468. return result;
  469. }
  470. PyObject *
  471. PyObject_CallFunction(PyObject *callable, const char *format, ...)
  472. {
  473. va_list va;
  474. PyObject *result;
  475. PyThreadState *tstate = _PyThreadState_GET();
  476. va_start(va, format);
  477. result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
  478. va_end(va);
  479. return result;
  480. }
  481. /* PyEval_CallFunction is exact copy of PyObject_CallFunction.
  482. * This function is kept for backward compatibility.
  483. */
  484. PyObject *
  485. PyEval_CallFunction(PyObject *callable, const char *format, ...)
  486. {
  487. va_list va;
  488. PyObject *result;
  489. PyThreadState *tstate = _PyThreadState_GET();
  490. va_start(va, format);
  491. result = _PyObject_CallFunctionVa(tstate, callable, format, va, 0);
  492. va_end(va);
  493. return result;
  494. }
  495. PyObject *
  496. _PyObject_CallFunction_SizeT(PyObject *callable, const char *format, ...)
  497. {
  498. PyThreadState *tstate = _PyThreadState_GET();
  499. va_list va;
  500. va_start(va, format);
  501. PyObject *result = _PyObject_CallFunctionVa(tstate, callable, format, va, 1);
  502. va_end(va);
  503. return result;
  504. }
  505. static PyObject*
  506. callmethod(PyThreadState *tstate, PyObject* callable, const char *format, va_list va, int is_size_t)
  507. {
  508. assert(callable != NULL);
  509. if (!PyCallable_Check(callable)) {
  510. _PyErr_Format(tstate, PyExc_TypeError,
  511. "attribute of type '%.200s' is not callable",
  512. Py_TYPE(callable)->tp_name);
  513. return NULL;
  514. }
  515. return _PyObject_CallFunctionVa(tstate, callable, format, va, is_size_t);
  516. }
  517. PyObject *
  518. PyObject_CallMethod(PyObject *obj, const char *name, const char *format, ...)
  519. {
  520. PyThreadState *tstate = _PyThreadState_GET();
  521. if (obj == NULL || name == NULL) {
  522. return null_error(tstate);
  523. }
  524. PyObject *callable = PyObject_GetAttrString(obj, name);
  525. if (callable == NULL) {
  526. return NULL;
  527. }
  528. va_list va;
  529. va_start(va, format);
  530. PyObject *retval = callmethod(tstate, callable, format, va, 0);
  531. va_end(va);
  532. Py_DECREF(callable);
  533. return retval;
  534. }
  535. /* PyEval_CallMethod is exact copy of PyObject_CallMethod.
  536. * This function is kept for backward compatibility.
  537. */
  538. PyObject *
  539. PyEval_CallMethod(PyObject *obj, const char *name, const char *format, ...)
  540. {
  541. PyThreadState *tstate = _PyThreadState_GET();
  542. if (obj == NULL || name == NULL) {
  543. return null_error(tstate);
  544. }
  545. PyObject *callable = PyObject_GetAttrString(obj, name);
  546. if (callable == NULL) {
  547. return NULL;
  548. }
  549. va_list va;
  550. va_start(va, format);
  551. PyObject *retval = callmethod(tstate, callable, format, va, 0);
  552. va_end(va);
  553. Py_DECREF(callable);
  554. return retval;
  555. }
  556. PyObject *
  557. _PyObject_CallMethodId(PyObject *obj, _Py_Identifier *name,
  558. const char *format, ...)
  559. {
  560. PyThreadState *tstate = _PyThreadState_GET();
  561. if (obj == NULL || name == NULL) {
  562. return null_error(tstate);
  563. }
  564. PyObject *callable = _PyObject_GetAttrId(obj, name);
  565. if (callable == NULL) {
  566. return NULL;
  567. }
  568. va_list va;
  569. va_start(va, format);
  570. PyObject *retval = callmethod(tstate, callable, format, va, 0);
  571. va_end(va);
  572. Py_DECREF(callable);
  573. return retval;
  574. }
  575. PyObject *
  576. _PyObject_CallMethod_SizeT(PyObject *obj, const char *name,
  577. const char *format, ...)
  578. {
  579. PyThreadState *tstate = _PyThreadState_GET();
  580. if (obj == NULL || name == NULL) {
  581. return null_error(tstate);
  582. }
  583. PyObject *callable = PyObject_GetAttrString(obj, name);
  584. if (callable == NULL) {
  585. return NULL;
  586. }
  587. va_list va;
  588. va_start(va, format);
  589. PyObject *retval = callmethod(tstate, callable, format, va, 1);
  590. va_end(va);
  591. Py_DECREF(callable);
  592. return retval;
  593. }
  594. PyObject *
  595. _PyObject_CallMethodId_SizeT(PyObject *obj, _Py_Identifier *name,
  596. const char *format, ...)
  597. {
  598. PyThreadState *tstate = _PyThreadState_GET();
  599. if (obj == NULL || name == NULL) {
  600. return null_error(tstate);
  601. }
  602. PyObject *callable = _PyObject_GetAttrId(obj, name);
  603. if (callable == NULL) {
  604. return NULL;
  605. }
  606. va_list va;
  607. va_start(va, format);
  608. PyObject *retval = callmethod(tstate, callable, format, va, 1);
  609. va_end(va);
  610. Py_DECREF(callable);
  611. return retval;
  612. }
  613. /* --- Call with "..." arguments ---------------------------------- */
  614. static PyObject *
  615. object_vacall(PyThreadState *tstate, PyObject *base,
  616. PyObject *callable, va_list vargs)
  617. {
  618. PyObject *small_stack[_PY_FASTCALL_SMALL_STACK];
  619. PyObject **stack;
  620. Py_ssize_t nargs;
  621. PyObject *result;
  622. Py_ssize_t i;
  623. va_list countva;
  624. if (callable == NULL) {
  625. return null_error(tstate);
  626. }
  627. /* Count the number of arguments */
  628. va_copy(countva, vargs);
  629. nargs = base ? 1 : 0;
  630. while (1) {
  631. PyObject *arg = va_arg(countva, PyObject *);
  632. if (arg == NULL) {
  633. break;
  634. }
  635. nargs++;
  636. }
  637. va_end(countva);
  638. /* Copy arguments */
  639. if (nargs <= (Py_ssize_t)Py_ARRAY_LENGTH(small_stack)) {
  640. stack = small_stack;
  641. }
  642. else {
  643. stack = PyMem_Malloc(nargs * sizeof(stack[0]));
  644. if (stack == NULL) {
  645. PyErr_NoMemory();
  646. return NULL;
  647. }
  648. }
  649. i = 0;
  650. if (base) {
  651. stack[i++] = base;
  652. }
  653. for (; i < nargs; ++i) {
  654. stack[i] = va_arg(vargs, PyObject *);
  655. }
  656. /* Call the function */
  657. result = _PyObject_VectorcallTstate(tstate, callable, stack, nargs, NULL);
  658. if (stack != small_stack) {
  659. PyMem_Free(stack);
  660. }
  661. return result;
  662. }
  663. PyObject *
  664. _PyObject_VectorcallMethod(PyObject *name, PyObject *const *args,
  665. size_t nargsf, PyObject *kwnames)
  666. {
  667. assert(name != NULL);
  668. assert(args != NULL);
  669. assert(PyVectorcall_NARGS(nargsf) >= 1);
  670. PyThreadState *tstate = _PyThreadState_GET();
  671. PyObject *callable = NULL;
  672. /* Use args[0] as "self" argument */
  673. int unbound = _PyObject_GetMethod(args[0], name, &callable);
  674. if (callable == NULL) {
  675. return NULL;
  676. }
  677. if (unbound) {
  678. /* We must remove PY_VECTORCALL_ARGUMENTS_OFFSET since
  679. * that would be interpreted as allowing to change args[-1] */
  680. nargsf &= ~PY_VECTORCALL_ARGUMENTS_OFFSET;
  681. }
  682. else {
  683. /* Skip "self". We can keep PY_VECTORCALL_ARGUMENTS_OFFSET since
  684. * args[-1] in the onward call is args[0] here. */
  685. args++;
  686. nargsf--;
  687. }
  688. PyObject *result = _PyObject_VectorcallTstate(tstate, callable,
  689. args, nargsf, kwnames);
  690. Py_DECREF(callable);
  691. return result;
  692. }
  693. PyObject *
  694. PyObject_CallMethodObjArgs(PyObject *obj, PyObject *name, ...)
  695. {
  696. PyThreadState *tstate = _PyThreadState_GET();
  697. if (obj == NULL || name == NULL) {
  698. return null_error(tstate);
  699. }
  700. PyObject *callable = NULL;
  701. int is_method = _PyObject_GetMethod(obj, name, &callable);
  702. if (callable == NULL) {
  703. return NULL;
  704. }
  705. obj = is_method ? obj : NULL;
  706. va_list vargs;
  707. va_start(vargs, name);
  708. PyObject *result = object_vacall(tstate, obj, callable, vargs);
  709. va_end(vargs);
  710. Py_DECREF(callable);
  711. return result;
  712. }
  713. PyObject *
  714. _PyObject_CallMethodIdObjArgs(PyObject *obj,
  715. struct _Py_Identifier *name, ...)
  716. {
  717. PyThreadState *tstate = _PyThreadState_GET();
  718. if (obj == NULL || name == NULL) {
  719. return null_error(tstate);
  720. }
  721. PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
  722. if (!oname) {
  723. return NULL;
  724. }
  725. PyObject *callable = NULL;
  726. int is_method = _PyObject_GetMethod(obj, oname, &callable);
  727. if (callable == NULL) {
  728. return NULL;
  729. }
  730. obj = is_method ? obj : NULL;
  731. va_list vargs;
  732. va_start(vargs, name);
  733. PyObject *result = object_vacall(tstate, obj, callable, vargs);
  734. va_end(vargs);
  735. Py_DECREF(callable);
  736. return result;
  737. }
  738. PyObject *
  739. PyObject_CallFunctionObjArgs(PyObject *callable, ...)
  740. {
  741. PyThreadState *tstate = _PyThreadState_GET();
  742. va_list vargs;
  743. PyObject *result;
  744. va_start(vargs, callable);
  745. result = object_vacall(tstate, NULL, callable, vargs);
  746. va_end(vargs);
  747. return result;
  748. }
  749. /* --- PyStack functions ------------------------------------------ */
  750. PyObject *
  751. _PyStack_AsDict(PyObject *const *values, PyObject *kwnames)
  752. {
  753. Py_ssize_t nkwargs;
  754. PyObject *kwdict;
  755. Py_ssize_t i;
  756. assert(kwnames != NULL);
  757. nkwargs = PyTuple_GET_SIZE(kwnames);
  758. kwdict = _PyDict_NewPresized(nkwargs);
  759. if (kwdict == NULL) {
  760. return NULL;
  761. }
  762. for (i = 0; i < nkwargs; i++) {
  763. PyObject *key = PyTuple_GET_ITEM(kwnames, i);
  764. PyObject *value = *values++;
  765. /* If key already exists, replace it with the new value */
  766. if (PyDict_SetItem(kwdict, key, value)) {
  767. Py_DECREF(kwdict);
  768. return NULL;
  769. }
  770. }
  771. return kwdict;
  772. }
  773. /* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
  774. Allocate a new argument vector and keyword names tuple. Return the argument
  775. vector; return NULL with exception set on error. Return the keyword names
  776. tuple in *p_kwnames.
  777. This also checks that all keyword names are strings. If not, a TypeError is
  778. raised.
  779. The newly allocated argument vector supports PY_VECTORCALL_ARGUMENTS_OFFSET.
  780. When done, you must call _PyStack_UnpackDict_Free(stack, nargs, kwnames) */
  781. static PyObject *const *
  782. _PyStack_UnpackDict(PyThreadState *tstate,
  783. PyObject *const *args, Py_ssize_t nargs,
  784. PyObject *kwargs, PyObject **p_kwnames)
  785. {
  786. assert(nargs >= 0);
  787. assert(kwargs != NULL);
  788. assert(PyDict_Check(kwargs));
  789. Py_ssize_t nkwargs = PyDict_GET_SIZE(kwargs);
  790. /* Check for overflow in the PyMem_Malloc() call below. The subtraction
  791. * in this check cannot overflow: both maxnargs and nkwargs are
  792. * non-negative signed integers, so their difference fits in the type. */
  793. Py_ssize_t maxnargs = PY_SSIZE_T_MAX / sizeof(args[0]) - 1;
  794. if (nargs > maxnargs - nkwargs) {
  795. _PyErr_NoMemory(tstate);
  796. return NULL;
  797. }
  798. /* Add 1 to support PY_VECTORCALL_ARGUMENTS_OFFSET */
  799. PyObject **stack = PyMem_Malloc((1 + nargs + nkwargs) * sizeof(args[0]));
  800. if (stack == NULL) {
  801. _PyErr_NoMemory(tstate);
  802. return NULL;
  803. }
  804. PyObject *kwnames = PyTuple_New(nkwargs);
  805. if (kwnames == NULL) {
  806. PyMem_Free(stack);
  807. return NULL;
  808. }
  809. stack++; /* For PY_VECTORCALL_ARGUMENTS_OFFSET */
  810. /* Copy positional arguments */
  811. for (Py_ssize_t i = 0; i < nargs; i++) {
  812. Py_INCREF(args[i]);
  813. stack[i] = args[i];
  814. }
  815. PyObject **kwstack = stack + nargs;
  816. /* This loop doesn't support lookup function mutating the dictionary
  817. to change its size. It's a deliberate choice for speed, this function is
  818. called in the performance critical hot code. */
  819. Py_ssize_t pos = 0, i = 0;
  820. PyObject *key, *value;
  821. unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
  822. while (PyDict_Next(kwargs, &pos, &key, &value)) {
  823. keys_are_strings &= Py_TYPE(key)->tp_flags;
  824. Py_INCREF(key);
  825. Py_INCREF(value);
  826. PyTuple_SET_ITEM(kwnames, i, key);
  827. kwstack[i] = value;
  828. i++;
  829. }
  830. /* keys_are_strings has the value Py_TPFLAGS_UNICODE_SUBCLASS if that
  831. * flag is set for all keys. Otherwise, keys_are_strings equals 0.
  832. * We do this check once at the end instead of inside the loop above
  833. * because it simplifies the deallocation in the failing case.
  834. * It happens to also make the loop above slightly more efficient. */
  835. if (!keys_are_strings) {
  836. _PyErr_SetString(tstate, PyExc_TypeError,
  837. "keywords must be strings");
  838. _PyStack_UnpackDict_Free(stack, nargs, kwnames);
  839. return NULL;
  840. }
  841. *p_kwnames = kwnames;
  842. return stack;
  843. }
  844. static void
  845. _PyStack_UnpackDict_Free(PyObject *const *stack, Py_ssize_t nargs,
  846. PyObject *kwnames)
  847. {
  848. Py_ssize_t n = PyTuple_GET_SIZE(kwnames) + nargs;
  849. for (Py_ssize_t i = 0; i < n; i++) {
  850. Py_DECREF(stack[i]);
  851. }
  852. PyMem_Free((PyObject **)stack - 1);
  853. Py_DECREF(kwnames);
  854. }