call.c 31 KB

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