getargsfast.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* getargskeywordsfast implementation copied from Python 3.9 and stripped down to
  2. * only include the functionality we need.
  3. *
  4. * We also add support for required kwonly args and accepting *args / **kwargs.
  5. *
  6. * DOCUMENTATION OF THE EXTENSIONS:
  7. * - Arguments given after a @ format specify required keyword-only arguments.
  8. * The | and $ specifiers must both appear before @.
  9. * - If the first character of a format string is %, then the function can support
  10. * *args and/or **kwargs. In this case the parser will consume two arguments,
  11. * which should be pointers to variables to store the *args and **kwargs, respectively.
  12. * Either pointer can be NULL, in which case the function doesn't take that
  13. * variety of vararg.
  14. * Unlike most format specifiers, the caller takes ownership of these objects
  15. * and is responsible for decrefing them.
  16. */
  17. #include <Python.h>
  18. #include "CPy.h"
  19. #define PARSER_INITED(parser) ((parser)->kwtuple != NULL)
  20. /* Forward */
  21. static int
  22. vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
  23. PyObject *kwargs, PyObject *kwnames,
  24. CPyArg_Parser *parser,
  25. va_list *p_va);
  26. static void skipitem_fast(const char **, va_list *);
  27. /* Parse args for an arbitrary signature */
  28. int
  29. CPyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
  30. CPyArg_Parser *parser, ...)
  31. {
  32. int retval;
  33. va_list va;
  34. va_start(va, parser);
  35. retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va);
  36. va_end(va);
  37. return retval;
  38. }
  39. /* Parse args for a function that takes no args */
  40. int
  41. CPyArg_ParseStackAndKeywordsNoArgs(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
  42. CPyArg_Parser *parser, ...)
  43. {
  44. int retval;
  45. va_list va;
  46. va_start(va, parser);
  47. if (nargs == 0 && kwnames == NULL) {
  48. // Fast path: no arguments
  49. retval = 1;
  50. } else {
  51. retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va);
  52. }
  53. va_end(va);
  54. return retval;
  55. }
  56. /* Parse args for a function that takes one arg */
  57. int
  58. CPyArg_ParseStackAndKeywordsOneArg(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
  59. CPyArg_Parser *parser, ...)
  60. {
  61. int retval;
  62. va_list va;
  63. va_start(va, parser);
  64. if (kwnames == NULL && nargs == 1) {
  65. // Fast path: one positional argument
  66. PyObject **p;
  67. p = va_arg(va, PyObject **);
  68. *p = args[0];
  69. retval = 1;
  70. } else {
  71. retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va);
  72. }
  73. va_end(va);
  74. return retval;
  75. }
  76. /* Parse args for a function that takes no keyword-only args, *args or **kwargs */
  77. int
  78. CPyArg_ParseStackAndKeywordsSimple(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
  79. CPyArg_Parser *parser, ...)
  80. {
  81. int retval;
  82. va_list va;
  83. va_start(va, parser);
  84. if (kwnames == NULL && PARSER_INITED(parser) &&
  85. nargs >= parser->min && nargs <= parser->max) {
  86. // Fast path: correct number of positional arguments only
  87. PyObject **p;
  88. Py_ssize_t i;
  89. for (i = 0; i < nargs; i++) {
  90. p = va_arg(va, PyObject **);
  91. *p = args[i];
  92. }
  93. retval = 1;
  94. } else {
  95. retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va);
  96. }
  97. va_end(va);
  98. return retval;
  99. }
  100. #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
  101. /* List of static parsers. */
  102. static struct CPyArg_Parser *static_arg_parsers = NULL;
  103. static int
  104. parser_init(CPyArg_Parser *parser)
  105. {
  106. const char * const *keywords;
  107. const char *format, *msg;
  108. int i, len, min, max, nkw;
  109. PyObject *kwtuple;
  110. assert(parser->keywords != NULL);
  111. if (PARSER_INITED(parser)) {
  112. return 1;
  113. }
  114. keywords = parser->keywords;
  115. /* scan keywords and count the number of positional-only parameters */
  116. for (i = 0; keywords[i] && !*keywords[i]; i++) {
  117. }
  118. parser->pos = i;
  119. /* scan keywords and get greatest possible nbr of args */
  120. for (; keywords[i]; i++) {
  121. if (!*keywords[i]) {
  122. PyErr_SetString(PyExc_SystemError,
  123. "Empty keyword parameter name");
  124. return 0;
  125. }
  126. }
  127. len = i;
  128. parser->required_kwonly_start = INT_MAX;
  129. if (*parser->format == '%') {
  130. parser->format++;
  131. parser->varargs = 1;
  132. }
  133. format = parser->format;
  134. if (format) {
  135. /* grab the function name or custom error msg first (mutually exclusive) */
  136. parser->fname = strchr(parser->format, ':');
  137. if (parser->fname) {
  138. parser->fname++;
  139. parser->custom_msg = NULL;
  140. }
  141. else {
  142. parser->custom_msg = strchr(parser->format,';');
  143. if (parser->custom_msg)
  144. parser->custom_msg++;
  145. }
  146. min = max = INT_MAX;
  147. for (i = 0; i < len; i++) {
  148. if (*format == '|') {
  149. if (min != INT_MAX) {
  150. PyErr_SetString(PyExc_SystemError,
  151. "Invalid format string (| specified twice)");
  152. return 0;
  153. }
  154. if (max != INT_MAX) {
  155. PyErr_SetString(PyExc_SystemError,
  156. "Invalid format string ($ before |)");
  157. return 0;
  158. }
  159. min = i;
  160. format++;
  161. }
  162. if (*format == '$') {
  163. if (max != INT_MAX) {
  164. PyErr_SetString(PyExc_SystemError,
  165. "Invalid format string ($ specified twice)");
  166. return 0;
  167. }
  168. if (i < parser->pos) {
  169. PyErr_SetString(PyExc_SystemError,
  170. "Empty parameter name after $");
  171. return 0;
  172. }
  173. max = i;
  174. format++;
  175. }
  176. if (*format == '@') {
  177. if (parser->required_kwonly_start != INT_MAX) {
  178. PyErr_SetString(PyExc_SystemError,
  179. "Invalid format string (@ specified twice)");
  180. return 0;
  181. }
  182. if (min == INT_MAX && max == INT_MAX) {
  183. PyErr_SetString(PyExc_SystemError,
  184. "Invalid format string "
  185. "(@ without preceding | and $)");
  186. return 0;
  187. }
  188. format++;
  189. parser->has_required_kws = 1;
  190. parser->required_kwonly_start = i;
  191. }
  192. if (IS_END_OF_FORMAT(*format)) {
  193. PyErr_Format(PyExc_SystemError,
  194. "More keyword list entries (%d) than "
  195. "format specifiers (%d)", len, i);
  196. return 0;
  197. }
  198. skipitem_fast(&format, NULL);
  199. }
  200. parser->min = Py_MIN(min, len);
  201. parser->max = Py_MIN(max, len);
  202. if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
  203. PyErr_Format(PyExc_SystemError,
  204. "more argument specifiers than keyword list entries "
  205. "(remaining format:'%s')", format);
  206. return 0;
  207. }
  208. }
  209. nkw = len - parser->pos;
  210. kwtuple = PyTuple_New(nkw);
  211. if (kwtuple == NULL) {
  212. return 0;
  213. }
  214. keywords = parser->keywords + parser->pos;
  215. for (i = 0; i < nkw; i++) {
  216. PyObject *str = PyUnicode_FromString(keywords[i]);
  217. if (str == NULL) {
  218. Py_DECREF(kwtuple);
  219. return 0;
  220. }
  221. PyUnicode_InternInPlace(&str);
  222. PyTuple_SET_ITEM(kwtuple, i, str);
  223. }
  224. parser->kwtuple = kwtuple;
  225. assert(parser->next == NULL);
  226. parser->next = static_arg_parsers;
  227. static_arg_parsers = parser;
  228. return 1;
  229. }
  230. static PyObject*
  231. find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
  232. {
  233. Py_ssize_t i, nkwargs;
  234. nkwargs = PyTuple_GET_SIZE(kwnames);
  235. for (i = 0; i < nkwargs; i++) {
  236. PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
  237. /* kwname == key will normally find a match in since keyword keys
  238. should be interned strings; if not retry below in a new loop. */
  239. if (kwname == key) {
  240. return kwstack[i];
  241. }
  242. }
  243. for (i = 0; i < nkwargs; i++) {
  244. PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
  245. assert(PyUnicode_Check(kwname));
  246. if (_PyUnicode_EQ(kwname, key)) {
  247. return kwstack[i];
  248. }
  249. }
  250. return NULL;
  251. }
  252. static int
  253. vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
  254. PyObject *kwargs, PyObject *kwnames,
  255. CPyArg_Parser *parser,
  256. va_list *p_va)
  257. {
  258. PyObject *kwtuple;
  259. const char *format;
  260. PyObject *keyword;
  261. int i, pos, len;
  262. Py_ssize_t nkwargs;
  263. PyObject *current_arg;
  264. PyObject *const *kwstack = NULL;
  265. int bound_pos_args;
  266. PyObject **p_args = NULL, **p_kwargs = NULL;
  267. assert(kwargs == NULL || PyDict_Check(kwargs));
  268. assert(kwargs == NULL || kwnames == NULL);
  269. assert(p_va != NULL);
  270. if (!parser_init(parser)) {
  271. return 0;
  272. }
  273. kwtuple = parser->kwtuple;
  274. pos = parser->pos;
  275. len = pos + (int)PyTuple_GET_SIZE(kwtuple);
  276. if (parser->varargs) {
  277. p_args = va_arg(*p_va, PyObject **);
  278. p_kwargs = va_arg(*p_va, PyObject **);
  279. }
  280. if (kwargs != NULL) {
  281. nkwargs = PyDict_GET_SIZE(kwargs);
  282. }
  283. else if (kwnames != NULL) {
  284. nkwargs = PyTuple_GET_SIZE(kwnames);
  285. kwstack = args + nargs;
  286. }
  287. else {
  288. nkwargs = 0;
  289. }
  290. if (nargs + nkwargs > len && !p_args && !p_kwargs) {
  291. /* Adding "keyword" (when nargs == 0) prevents producing wrong error
  292. messages in some special cases (see bpo-31229). */
  293. PyErr_Format(PyExc_TypeError,
  294. "%.200s%s takes at most %d %sargument%s (%zd given)",
  295. (parser->fname == NULL) ? "function" : parser->fname,
  296. (parser->fname == NULL) ? "" : "()",
  297. len,
  298. (nargs == 0) ? "keyword " : "",
  299. (len == 1) ? "" : "s",
  300. nargs + nkwargs);
  301. return 0;
  302. }
  303. if (parser->max < nargs && !p_args) {
  304. if (parser->max == 0) {
  305. PyErr_Format(PyExc_TypeError,
  306. "%.200s%s takes no positional arguments",
  307. (parser->fname == NULL) ? "function" : parser->fname,
  308. (parser->fname == NULL) ? "" : "()");
  309. }
  310. else {
  311. PyErr_Format(PyExc_TypeError,
  312. "%.200s%s takes %s %d positional argument%s (%zd given)",
  313. (parser->fname == NULL) ? "function" : parser->fname,
  314. (parser->fname == NULL) ? "" : "()",
  315. (parser->min < parser->max) ? "at most" : "exactly",
  316. parser->max,
  317. parser->max == 1 ? "" : "s",
  318. nargs);
  319. }
  320. return 0;
  321. }
  322. format = parser->format;
  323. /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
  324. for (i = 0; i < len; i++) {
  325. if (*format == '|') {
  326. format++;
  327. }
  328. if (*format == '$') {
  329. format++;
  330. }
  331. if (*format == '@') {
  332. format++;
  333. }
  334. assert(!IS_END_OF_FORMAT(*format));
  335. if (i < nargs && i < parser->max) {
  336. current_arg = args[i];
  337. }
  338. else if (nkwargs && i >= pos) {
  339. keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
  340. if (kwargs != NULL) {
  341. current_arg = PyDict_GetItemWithError(kwargs, keyword);
  342. if (!current_arg && PyErr_Occurred()) {
  343. return 0;
  344. }
  345. }
  346. else {
  347. current_arg = find_keyword(kwnames, kwstack, keyword);
  348. }
  349. if (current_arg) {
  350. --nkwargs;
  351. }
  352. }
  353. else {
  354. current_arg = NULL;
  355. }
  356. if (current_arg) {
  357. PyObject **p = va_arg(*p_va, PyObject **);
  358. *p = current_arg;
  359. format++;
  360. continue;
  361. }
  362. if (i < parser->min || i >= parser->required_kwonly_start) {
  363. /* Less arguments than required */
  364. if (i < pos) {
  365. Py_ssize_t min = Py_MIN(pos, parser->min);
  366. PyErr_Format(PyExc_TypeError,
  367. "%.200s%s takes %s %d positional argument%s"
  368. " (%zd given)",
  369. (parser->fname == NULL) ? "function" : parser->fname,
  370. (parser->fname == NULL) ? "" : "()",
  371. min < parser->max ? "at least" : "exactly",
  372. min,
  373. min == 1 ? "" : "s",
  374. nargs);
  375. }
  376. else {
  377. keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
  378. if (i >= parser->max) {
  379. PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
  380. "keyword-only argument '%U'",
  381. (parser->fname == NULL) ? "function" : parser->fname,
  382. (parser->fname == NULL) ? "" : "()",
  383. keyword);
  384. }
  385. else {
  386. PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
  387. "argument '%U' (pos %d)",
  388. (parser->fname == NULL) ? "function" : parser->fname,
  389. (parser->fname == NULL) ? "" : "()",
  390. keyword, i+1);
  391. }
  392. }
  393. return 0;
  394. }
  395. /* current code reports success when all required args
  396. * fulfilled and no keyword args left, with no further
  397. * validation. XXX Maybe skip this in debug build ?
  398. */
  399. if (!nkwargs && !parser->has_required_kws && !p_args && !p_kwargs) {
  400. return 1;
  401. }
  402. /* We are into optional args, skip through to any remaining
  403. * keyword args */
  404. skipitem_fast(&format, p_va);
  405. }
  406. assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
  407. bound_pos_args = Py_MIN(nargs, Py_MIN(parser->max, len));
  408. if (p_args) {
  409. *p_args = PyTuple_New(nargs - bound_pos_args);
  410. if (!*p_args) {
  411. return 0;
  412. }
  413. for (i = bound_pos_args; i < nargs; i++) {
  414. PyObject *arg = args[i];
  415. Py_INCREF(arg);
  416. PyTuple_SET_ITEM(*p_args, i - bound_pos_args, arg);
  417. }
  418. }
  419. if (p_kwargs) {
  420. /* This unfortunately needs to be special cased because if len is 0 then we
  421. * never go through the main loop. */
  422. if (nargs > 0 && len == 0 && !p_args) {
  423. PyErr_Format(PyExc_TypeError,
  424. "%.200s%s takes no positional arguments",
  425. (parser->fname == NULL) ? "function" : parser->fname,
  426. (parser->fname == NULL) ? "" : "()");
  427. return 0;
  428. }
  429. *p_kwargs = PyDict_New();
  430. if (!*p_kwargs) {
  431. goto latefail;
  432. }
  433. }
  434. if (nkwargs > 0) {
  435. Py_ssize_t j;
  436. PyObject *value;
  437. /* make sure there are no arguments given by name and position */
  438. for (i = pos; i < bound_pos_args; i++) {
  439. keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
  440. if (kwargs != NULL) {
  441. current_arg = PyDict_GetItemWithError(kwargs, keyword);
  442. if (!current_arg && PyErr_Occurred()) {
  443. goto latefail;
  444. }
  445. }
  446. else {
  447. current_arg = find_keyword(kwnames, kwstack, keyword);
  448. }
  449. if (current_arg) {
  450. /* arg present in tuple and in dict */
  451. PyErr_Format(PyExc_TypeError,
  452. "argument for %.200s%s given by name ('%U') "
  453. "and position (%d)",
  454. (parser->fname == NULL) ? "function" : parser->fname,
  455. (parser->fname == NULL) ? "" : "()",
  456. keyword, i+1);
  457. goto latefail;
  458. }
  459. }
  460. /* make sure there are no extraneous keyword arguments */
  461. j = 0;
  462. while (1) {
  463. int match;
  464. if (kwargs != NULL) {
  465. if (!PyDict_Next(kwargs, &j, &keyword, &value))
  466. break;
  467. }
  468. else {
  469. if (j >= PyTuple_GET_SIZE(kwnames))
  470. break;
  471. keyword = PyTuple_GET_ITEM(kwnames, j);
  472. value = kwstack[j];
  473. j++;
  474. }
  475. match = PySequence_Contains(kwtuple, keyword);
  476. if (match <= 0) {
  477. if (!match) {
  478. if (!p_kwargs) {
  479. PyErr_Format(PyExc_TypeError,
  480. "'%S' is an invalid keyword "
  481. "argument for %.200s%s",
  482. keyword,
  483. (parser->fname == NULL) ? "this function" : parser->fname,
  484. (parser->fname == NULL) ? "" : "()");
  485. goto latefail;
  486. } else {
  487. if (PyDict_SetItem(*p_kwargs, keyword, value) < 0) {
  488. goto latefail;
  489. }
  490. }
  491. } else {
  492. goto latefail;
  493. }
  494. }
  495. }
  496. }
  497. return 1;
  498. /* Handle failures that have happened after we have tried to
  499. * create *args and **kwargs, if they exist. */
  500. latefail:
  501. if (p_args) {
  502. Py_XDECREF(*p_args);
  503. }
  504. if (p_kwargs) {
  505. Py_XDECREF(*p_kwargs);
  506. }
  507. return 0;
  508. }
  509. static void
  510. skipitem_fast(const char **p_format, va_list *p_va)
  511. {
  512. const char *format = *p_format;
  513. char c = *format++;
  514. if (p_va != NULL) {
  515. (void) va_arg(*p_va, PyObject **);
  516. }
  517. *p_format = format;
  518. }