Debian Codegraph @

lib_pypy/hpy/devel/src/runtime/argparse.c

raw symbols SCIP
1 /**
2 * Implementation of HPyArg_Parse and HPyArg_ParseKeywords.
3 *
4 * Note: those functions are runtime helper functions, i.e., they are not part
5 * of the HPy context, but are available to HPy extensions to incorporate at
6 * compile time.
7 *
8 * HPyArg_Parse parses positional arguments and replaces PyArg_ParseTuple.
9 * HPyArg_ParseKeywords parses positional and keyword arguments and
10 * replaces PyArg_ParseTupleAndKeywords.
11 *
12 * HPy intends to only support the simpler format string types (numbers, bools)
13 * and handles. More complex types (e.g. buffers) should be retrieved as
14 * handles and then processed further as needed.
15 *
16 * Supported Formatting Strings
17 * ----------------------------
18 *
19 * Numbers
20 * ~~~~~~~
21 *
22 * ``b (int) [unsigned char]``
23 * Convert a nonnegative Python integer to an unsigned tiny int, stored in a C unsigned char.
24 *
25 * ``B (int) [unsigned char]``
26 * Convert a Python integer to a tiny int without overflow checking, stored in a C unsigned char.
27 *
28 * ``h (int) [short int]``
29 * Convert a Python integer to a C short int.
30 *
31 * ``H (int) [unsigned short int]``
32 * Convert a Python integer to a C unsigned short int, without overflow checking.
33 *
34 * ``i (int) [int]``
35 * Convert a Python integer to a plain C int.
36 *
37 * ``I (int) [unsigned int]``
38 * Convert a Python integer to a C unsigned int, without overflow checking.
39 *
40 * ``l (int) [long int]``
41 * Convert a Python integer to a C long int.
42 *
43 * ``k (int) [unsigned long]``
44 * Convert a Python integer to a C unsigned long without overflow checking.
45 *
46 * ``L (int) [long long]``
47 * Convert a Python integer to a C long long.
48 *
49 * ``K (int) [unsigned long long]``
50 * Convert a Python integer to a C unsigned long long without overflow checking.
51 *
52 * ``n (int) [HPy_ssize_t]``
53 * Convert a Python integer to a C HPy_ssize_t.
54 *
55 * ``f (float) [float]``
56 * Convert a Python floating point number to a C float.
57 *
58 * ``d (float) [double]``
59 * Convert a Python floating point number to a C double.
60 *
61 * Strings and buffers
62 * ~~~~~~~~~~~~~~~~~~~~~~~~
63 *
64 * These formats allow accessing an object as a contiguous chunk of memory.
65 * You don't have to provide raw storage for the returned unicode or bytes
66 * area.
67 *
68 * In general, when a format sets a pointer to a buffer, the pointer is valid
69 * only until the corresponding HPy handle is closed.
70 *
71 * ``s (unicode) [const char*]``
72 *
73 * Convert a Unicode object to a C pointer to a character string.
74 * A pointer to an existing string is stored in the character pointer
75 * variable whose address you pass. The C string is NUL-terminated.
76 * The Python string must not contain embedded null code points; if it does,
77 * a `ValueError` exception is raised. Unicode objects are converted
78 * to C strings using 'utf-8' encoding. If this conversion fails,
79 * a `UnicodeError` is raised.
80 *
81 * Note: This format does not accept bytes-like objects and is therefore
82 * not suitable for filesystem paths.
83 *
84 * Handles (Python Objects)
85 * ~~~~~~~~~~~~~~~~~~~~~~~~
86 *
87 * ``O (object) [HPy]``
88 * Store a handle pointing to a generic Python object.
89 *
90 * When using O with HPyArg_ParseKeywords, an HPyTracker is created and
91 * returned via the parameter `ht`. If HPyArg_ParseKeywords returns
92 * successfully, you must call HPyTracker_Close on `ht` once the
93 * returned handles are no longer needed. This will close all the handles
94 * created during argument parsing. There is no need to call
95 * `HPyTracker_Close` on failure -- the argument parser does this for you.
96 *
97 * Miscellaneous
98 * ~~~~~~~~~~~~~
99 *
100 * ``p (bool) [int]``
101 * Tests the value passed in for truth (a boolean predicate) and converts
102 * the result to its equivalent C true/false integer value. Sets the int to
103 * 1 if the expression was true and 0 if it was false. This accepts any
104 * valid Python value. See
105 * `Truth Value Testing <https://docs.python.org/3/library/stdtypes.html#truth>`_
106 * for more information about how Python tests values for truth.
107 *
108 * Options
109 * ~~~~~~~
110 *
111 * ``|``
112 * Indicates that the remaining arguments in the argument list are optional.
113 * The C variables corresponding to optional arguments should be initialized
114 * to their default value — when an optional argument is not specified, the
115 * contents of the corresponding C variable is not modified.
116 *
117 * ``$``
118 * HPyArg_ParseKeywords() only: Indicates that the remaining arguments in
119 * the argument list are keyword-only. Currently, all keyword-only arguments
120 * must also be optional arguments, so | must always be specified before $
121 * in the format string.
122 *
123 * ``:``
124 * The list of format units ends here; the string after the colon is used as
125 * the function name in error messages. : and ; are mutually exclusive and
126 * whichever occurs first takes precedence.
127 *
128 * ``;``
129 * The list of format units ends here; the string after the semicolon is
130 * used as the error message instead of the default error message. : and ;
131 * are mutually exclusive and whichever occurs first takes precedence.
132 *
133 * Argument Parsing API
134 * --------------------
135 *
136 */
137
138 #include "hpy.h"
139 #include <limits.h>
140 #include <stdio.h>
141
142 #define _BREAK_IF_OPTIONAL(current_arg) if (HPy_IsNull(current_arg)) break;
143 #define _ERR_STRING_MAX_LENGTH 512
144
145
146 static const char *
147 parse_err_fmt(const char *fmt, const char **err_fmt)
148 {
149 const char *fmt1 = fmt;
150
151 for (; *fmt1 != 0; fmt1++) {
152 if (*fmt1 == ':' || *fmt1 == ';') {
153 *err_fmt = fmt1;
154 break;
155 }
156 }
157 return fmt1;
158 }
159
160
161 static void
162 set_error(HPyContext *ctx, HPy exc, const char *err_fmt, const char *msg) {
163 char err_buf[_ERR_STRING_MAX_LENGTH];
164 if (err_fmt == NULL) {
165 snprintf(err_buf, _ERR_STRING_MAX_LENGTH, "function %.256s", msg);
166 }
167 else if (*err_fmt == ':') {
168 snprintf(err_buf, _ERR_STRING_MAX_LENGTH, "%.200s() %.256s", err_fmt + 1, msg);
169 }
170 else {
171 snprintf(err_buf, _ERR_STRING_MAX_LENGTH, "%s", err_fmt + 1);
172 }
173 HPyErr_SetString(ctx, exc, err_buf);
174 }
175
176
177 static int
178 parse_item(HPyContext *ctx, HPyTracker *ht, HPy current_arg, int current_arg_tmp, const char **fmt, va_list *vl, const char *err_fmt)
179 {
180 switch (*(*fmt)++) {
181
182 case 'b': { /* unsigned byte -- very short int */
183 char *output = va_arg(*vl, char *);
184 _BREAK_IF_OPTIONAL(current_arg);
185 long value = HPyLong_AsLong(ctx, current_arg);
186 if (value == -1 && HPyErr_Occurred(ctx))
187 return 0;
188 if (value < 0) {
189 set_error(ctx, ctx->h_OverflowError, err_fmt,
190 "unsigned byte integer is less than minimum");
191 return 0;
192 }
193 if (value > UCHAR_MAX) {
194 set_error(ctx, ctx->h_OverflowError, err_fmt,
195 "unsigned byte integer is greater than maximum");
196 return 0;
197 }
198 *output = (char) value;
199 break;
200 }
201
202 case 'B': { /* byte sized bitfield - both signed and unsigned
203 values allowed */
204 char *output = va_arg(*vl, char *);
205 _BREAK_IF_OPTIONAL(current_arg);
206 unsigned long value = HPyLong_AsUnsignedLongMask(ctx, current_arg);
207 if (value == (unsigned long)-1 && HPyErr_Occurred(ctx))
208 return 0;
209 *output = (unsigned char) value;
210 break;
211 }
212
213 case 'h': { /* signed short int */
214 short *output = va_arg(*vl, short *);
215 _BREAK_IF_OPTIONAL(current_arg);
216 long value = HPyLong_AsLong(ctx, current_arg);
217 if (value == -1 && HPyErr_Occurred(ctx))
218 return 0;
219 if (value < SHRT_MIN) {
220 set_error(ctx, ctx->h_OverflowError, err_fmt,
221 "signed short integer is less than minimum");
222 return 0;
223 }
224 if (value > SHRT_MAX) {
225 set_error(ctx, ctx->h_OverflowError, err_fmt,
226 "signed short integer is greater than maximum");
227 return 0;
228 }
229 *output = (short) value;
230 break;
231 }
232
233 case 'H': { /* short int sized bitfield, both signed and
234 unsigned allowed */
235 unsigned short *output = va_arg(*vl, unsigned short *);
236 _BREAK_IF_OPTIONAL(current_arg);
237 unsigned long value = HPyLong_AsUnsignedLongMask(ctx, current_arg);
238 if (value == (unsigned long)-1 && HPyErr_Occurred(ctx))
239 return 0;
240 *output = (unsigned short) value;
241 break;
242 }
243
244 case 'i': { /* signed int */
245 int *output = va_arg(*vl, int *);
246 _BREAK_IF_OPTIONAL(current_arg);
247 long value = HPyLong_AsLong(ctx, current_arg);
248 if (value == -1 && HPyErr_Occurred(ctx))
249 return 0;
250 if (value > INT_MAX) {
251 set_error(ctx, ctx->h_OverflowError, err_fmt,
252 "signed integer is greater than maximum");
253 return 0;
254 }
255 if (value < INT_MIN) {
256 set_error(ctx, ctx->h_OverflowError, err_fmt,
257 "signed integer is less than minimum");
258 return 0;
259 }
260 *output = (int)value;
261 break;
262 }
263
264 case 'I': { /* int sized bitfield, both signed and
265 unsigned allowed */
266 unsigned int *output = va_arg(*vl, unsigned int *);
267 _BREAK_IF_OPTIONAL(current_arg);
268 unsigned long value = HPyLong_AsUnsignedLongMask(ctx, current_arg);
269 if (value == (unsigned long)-1 && HPyErr_Occurred(ctx))
270 return 0;
271 *output = (unsigned int) value;
272 break;
273 }
274
275 case 'l': {
276 long *output = va_arg(*vl, long *);
277 _BREAK_IF_OPTIONAL(current_arg);
278 long value = HPyLong_AsLong(ctx, current_arg);
279 if (value == -1 && HPyErr_Occurred(ctx))
280 return 0;
281 *output = value;
282 break;
283 }
284
285 case 'k': { /* long sized bitfield */
286 unsigned long *output = va_arg(*vl, unsigned long *);
287 _BREAK_IF_OPTIONAL(current_arg);
288 unsigned long value = HPyLong_AsUnsignedLongMask(ctx, current_arg);
289 if (value == (unsigned long)-1 && HPyErr_Occurred(ctx))
290 return 0;
291 *output = value;
292 break;
293 }
294
295 case 'L': { /* long long */
296 long long *output = va_arg(*vl, long long *);
297 _BREAK_IF_OPTIONAL(current_arg);
298 long long value = HPyLong_AsLongLong(ctx, current_arg);
299 if (value == (long long)-1 && HPyErr_Occurred(ctx))
300 return 0;
301 *output = value;
302 break;
303 }
304
305 case 'K': { /* long long sized bitfield */
306 unsigned long long *output = va_arg(*vl, unsigned long long *);
307 _BREAK_IF_OPTIONAL(current_arg);
308 unsigned long long value = HPyLong_AsUnsignedLongLongMask(ctx, current_arg);
309 if (value == (unsigned long long)-1 && HPyErr_Occurred(ctx))
310 return 0;
311 *output = value;
312 break;
313 }
314
315 case 'n': { /* HPy_ssize_t */
316 HPy_ssize_t *output = va_arg(*vl, HPy_ssize_t *);
317 _BREAK_IF_OPTIONAL(current_arg);
318 HPy_ssize_t value = HPyLong_AsSsize_t(ctx, current_arg);
319 if (value == (HPy_ssize_t)-1 && HPyErr_Occurred(ctx))
320 return 0;
321 *output = value;
322 break;
323 }
324
325 case 'f': { /* float */
326 float *output = va_arg(*vl, float *);
327 _BREAK_IF_OPTIONAL(current_arg);
328 double value = HPyFloat_AsDouble(ctx, current_arg);
329 if (value == -1.0 && HPyErr_Occurred(ctx))
330 return 0;
331 *output = (float) value;
332 break;
333 }
334
335 case 'd': { /* double */
336 double* output = va_arg(*vl, double *);
337 _BREAK_IF_OPTIONAL(current_arg);
338 double value = HPyFloat_AsDouble(ctx, current_arg);
339 if (value == -1.0 && HPyErr_Occurred(ctx))
340 return 0;
341 *output = value;
342 break;
343 }
344
345 case 'O': {
346 HPy *output = va_arg(*vl, HPy *);
347 _BREAK_IF_OPTIONAL(current_arg);
348 if (current_arg_tmp) {
349 *output = HPy_Dup(ctx, current_arg);
350 HPyTracker_Add(ctx, *ht, *output);
351 }
352 else {
353 *output = current_arg;
354 }
355 break;
356 }
357
358 case 'p': { /* boolean *p*redicate */
359 int *output = va_arg(*vl, int *);
360 int value = HPy_IsTrue(ctx, current_arg);
361 if (value < 0)
362 return 0;
363 *output = (value > 0) ? 1 : 0;
364 break;
365 }
366
367 case 's': {
368 const char **output = va_arg(*vl, const char **);
369 if (!HPyUnicode_Check(ctx, current_arg)) {
370 set_error(ctx, ctx->h_TypeError, err_fmt, "a str is required");
371 return 0;
372 }
373 HPy_ssize_t size;
374 const char *data = HPyUnicode_AsUTF8AndSize(ctx, current_arg, &size);
375 if (data == NULL) {
376 set_error(ctx, ctx->h_SystemError, err_fmt, "unicode conversion error");
377 return 0;
378 }
379 // loop bounded by size is more robust/paranoid than strlen
380 HPy_ssize_t i;
381 for (i = 0; i < size; ++i) {
382 if (data[i] == '\0') {
383 set_error(ctx, ctx->h_ValueError, err_fmt, "embedded null character");
384 return 0;
385 }
386 }
387 if (data[i] != '\0') {
388 set_error(ctx, ctx->h_SystemError, err_fmt, "missing terminating null character");
389 return 0;
390 }
391 *output = data;
392 break;
393 }
394
395 default: {
396 set_error(ctx, ctx->h_SystemError, err_fmt, "unknown arg format code");
397 return 0;
398 }
399
400 } // switch
401
402 return 1;
403 }
404
405
406 /**
407 * Parse positional arguments.
408 *
409 * :param ctx:
410 * The execution context.
411 * :param ht:
412 * An optional pointer to an HPyTracker. If the format string never
413 * results in new handles being created, ``ht`` may be ``NULL``. Currently
414 * only the ``O`` formatting option to this function requires an HPyTracker.
415 * :param args:
416 * The array of positional arguments to parse.
417 * :param nargs:
418 * The number of elements in ``args``.
419 * :param fmt:
420 * The format string to use to parse the arguments.
421 * :param ...:
422 * A va_list of references to variables in which to store the parsed
423 * arguments. The number and types of the arguments should match the
424 * the format string, ``fmt``.
425 *
426 * :returns: ``0`` on failure, ``1`` on success.
427 *
428 * If a ``NULL`` pointer is passed to ``ht`` and an HPyTracker is required by
429 * the format string, a ``SystemError`` will be raised.
430 *
431 * If a pointer is provided to ``ht``, the HPyTracker will always be created
432 * and must be closed with ``HPyTracker_Close`` if parsing succeeds (after all
433 * handles returned are no longer needed). If parsing fails, this function
434 * will close the HPyTracker automatically.
435 *
436 * Examples:
437 *
438 * Using ``HPyArg_Parse`` without an HPyTracker:
439 *
440 * .. code-block:: c
441 *
442 * long a, b;
443 * if (!HPyArg_Parse(ctx, NULL, args, nargs, "ll", &a, &b))
444 * return HPy_NULL;
445 * ...
446 *
447 * Using ``HPyArg_Parse`` with an HPyTracker:
448 *
449 * .. code-block:: c
450 *
451 * long a, b;
452 * HPyTracker ht;
453 * if (!HPyArg_Parse(ctx, &ht, args, nargs, "ll", &a, &b))
454 * return HPy_NULL;
455 * ...
456 * HPyTracker_Close(ctx, ht);
457 * ...
458 *
459 * .. note::
460 *
461 * Currently ``HPyArg_Parse`` never requires the use of an HPyTracker.
462 * The option exists only to support releasing temporary storage used by
463 * future format string codes (e.g. for character strings).
464 */
465 HPyAPI_HELPER int
466 HPyArg_Parse(HPyContext *ctx, HPyTracker *ht, const HPy *args, size_t nargs, const char *fmt, ...)
467 {
468 const char *fmt1 = fmt;
469 const char *err_fmt = NULL;
470 const char *fmt_end = NULL;
471
472 int optional = 0;
473 size_t i = 0;
474 HPy current_arg;
475
476 fmt_end = parse_err_fmt(fmt, &err_fmt);
477
478 if (ht != NULL) {
479 *ht = HPyTracker_New(ctx, 0);
480 if (HPy_IsNull(*ht)) {
481 return 0;
482 }
483 }
484
485 va_list vl;
486 va_start(vl, fmt);
487
488 while (fmt1 != fmt_end) {
489 if (*fmt1 == '|') {
490 optional = 1;
491 fmt1++;
492 continue;
493 }
494 current_arg = HPy_NULL;
495 if (i < nargs) {
496 current_arg = args[i];
497 }
498 if (!HPy_IsNull(current_arg) || optional) {
499 if (!parse_item(ctx, ht, current_arg, 0, &fmt1, &vl, err_fmt)) {
500 goto error;
501 }
502 }
503 else {
504 set_error(ctx, ctx->h_TypeError, err_fmt,
505 "required positional argument missing");
506 goto error;
507 }
508 i++;
509 }
510 if (i < nargs) {
511 set_error(ctx, ctx->h_TypeError, err_fmt,
512 "mismatched args (too many arguments for fmt)");
513 goto error;
514 }
515
516 va_end(vl);
517 return 1;
518
519 error:
520 va_end(vl);
521 if (ht != NULL) {
522 HPyTracker_Close(ctx, *ht);
523 }
524 return 0;
525 }
526
527 static HPy
528 find_keyword(HPyContext *ctx, HPy kwnames, HPy_ssize_t n_kwnames, const HPy *args, const char *key)
529 {
530 for (HPy_ssize_t i = 0; i < n_kwnames; i++) {
531 HPy kwname = HPy_GetItem_i(ctx, kwnames, i);
532 HPy h_key = HPyUnicode_FromString(ctx, key);
533 int eq = HPy_RichCompareBool(ctx, kwname, h_key, HPy_EQ);
534 HPy_Close(ctx, h_key);
535 HPy_Close(ctx, kwname);
536 if (eq) {
537 return args[i];
538 }
539 }
540 return HPy_NULL;
541 }
542
543 static int
544 parse_keywords(HPyContext *ctx, HPyTracker *ht, const HPy *args,
545 size_t nargs, HPy kw, int kwnames, const char *fmt,
546 const char *keywords[], va_list *p_va)
547 {
548 const char *fmt1 = fmt;
549 const char *err_fmt = NULL;
550 const char *fmt_end = NULL;
551
552 int optional = 0;
553 int keyword_only = 0;
554 size_t i = 0;
555 size_t nkw = 0;
556 HPy_ssize_t n_kwnames = 0;
557 HPy current_arg;
558 int current_arg_needs_closing = 0;
559
560 assert(p_va != NULL);
561
562 fmt_end = parse_err_fmt(fmt, &err_fmt);
563
564 // first count positional only arguments
565 while (keywords[nkw] != NULL && !*keywords[nkw]) {
566 nkw++;
567 }
568 // then check and count the rest
569 while (keywords[nkw] != NULL) {
570 if (!*keywords[nkw]) {
571 set_error(ctx, ctx->h_SystemError, err_fmt,
572 "empty keyword parameter name");
573 return 0;
574 }
575 nkw++;
576 }
577
578 if (ht != NULL) {
579 *ht = HPyTracker_New(ctx, 0);
580 if (HPy_IsNull(*ht)) {
581 return 0;
582 }
583 }
584
585 if (kwnames && !HPy_IsNull(kw)) {
586 n_kwnames = HPy_Length(ctx, kw);
587 if (n_kwnames < 0) {
588 set_error(ctx, ctx->h_SystemError, err_fmt,
589 "could not retrieve length of keyword names tuple");
590 return 0;
591 }
592 }
593
594 while (fmt1 != fmt_end) {
595 if (*fmt1 == '|') {
596 optional = 1;
597 fmt1++;
598 continue;
599 }
600 if (*fmt1 == '$') {
601 optional = 1;
602 keyword_only = 1;
603 fmt1++;
604 continue;
605 }
606 if (*fmt1 == 'O' && ht == NULL) {
607 set_error(ctx, ctx->h_SystemError, err_fmt,
608 "HPyArg_ParseKeywords cannot use the format character 'O' unless"
609 " an HPyTracker is provided. Please supply an HPyTracker.");
610 goto error;
611 }
612 if (i >= nkw) {
613 set_error(ctx, ctx->h_TypeError, err_fmt,
614 "mismatched args (too few keywords for fmt)");
615 goto error;
616 }
617 current_arg = HPy_NULL;
618 if (i < nargs) {
619 if (keyword_only) {
620 set_error(ctx, ctx->h_TypeError, err_fmt,
621 "keyword only argument passed as positional argument");
622 goto error;
623 }
624 current_arg = args[i];
625 }
626 else if (!HPy_IsNull(kw) && *keywords[i]) {
627 if (kwnames) {
628 current_arg = find_keyword(ctx, kw, n_kwnames, args+nargs,
629 keywords[i]);
630 /*
631 * Clear any error that was raised. If an error was raised
632 * current_arg will be HPy_NULL and will be handled
633 * appropriately below depending on whether the current argument
634 * is optional or not.
635 */
636 if (HPy_IsNull(current_arg)) {
637 HPyErr_Clear(ctx);
638 }
639 } else {
640 current_arg = HPy_GetItem_s(ctx, kw, keywords[i]);
641 /*
642 * Track the handle or clear any KeyError that was raised (for
643 * the same reason as explained above).
644 */
645 if (!HPy_IsNull(current_arg)) {
646 current_arg_needs_closing = 1;
647 } else {
648 HPyErr_Clear(ctx);
649 }
650 }
651 }
652 if (!HPy_IsNull(current_arg) || optional) {
653 if (!parse_item(ctx, ht, current_arg, 1, &fmt1, p_va, err_fmt)) {
654 goto error;
655 }
656 }
657 else {
658 set_error(ctx, ctx->h_TypeError, err_fmt,
659 "no value for required argument");
660 goto error;
661 }
662 if (current_arg_needs_closing) {
663 HPy_Close(ctx, current_arg);
664 current_arg_needs_closing = 0;
665 }
666 i++;
667 }
668 if (i != nkw) {
669 set_error(ctx, ctx->h_TypeError, err_fmt,
670 "mismatched args (too many keywords for fmt)");
671 goto error;
672 }
673 return 1;
674
675 error:
676 if (ht != NULL) {
677 HPyTracker_Close(ctx, *ht);
678 }
679 if (current_arg_needs_closing) {
680 HPy_Close(ctx, current_arg);
681 }
682 return 0;
683 }
684
685
686 /**
687 * Parse positional and keyword arguments.
688 *
689 * :param ctx:
690 * The execution context.
691 * :param ht:
692 * An optional pointer to an HPyTracker. If the format string never
693 * results in new handles being created, ``ht`` may be ``NULL``. Currently
694 * only the ``O`` formatting option to this function requires an HPyTracker.
695 * :param args:
696 * The array of positional arguments to parse.
697 * :param nargs:
698 * The number of elements in ``args``.
699 * :param kwnames:
700 * A handle to the tuple of keyword argument names (may be ``HPy_NULL``).
701 * The values of the keyword arguments are appended to ``args``. Argument
702 * ``nargs`` does not include the keyword argument count.
703 * :param fmt:
704 * The format string to use to parse the arguments.
705 * :param keywords:
706 * A ``NULL``-terminated array of argument names. The number of names
707 * should match the format string provided. Positional only arguments
708 * should have the name ``""`` (i.e. the null-terminated empty string).
709 * Positional only arguments must preceded all other arguments.
710 * :param ...:
711 * A va_list of references to variables in which to store the parsed
712 * arguments. The number and types of the arguments should match the
713 * the format string, ``fmt``.
714 *
715 * :returns: ``0`` on failure, ``1`` on success.
716 *
717 * If a ``NULL`` pointer is passed to ``ht`` and an HPyTracker is required by
718 * the format string, a ``SystemError`` will be raised.
719 *
720 * If a pointer is provided to ``ht``, the HPyTracker will always be created and
721 * must be closed with ``HPyTracker_Close`` if parsing succeeds (after all
722 * handles returned are no longer needed). If parsing fails, this function will
723 * close the HPyTracker automatically.
724 *
725 * Examples:
726 *
727 * Using `HPyArg_ParseKeywords` without an `HPyTracker`:
728 *
729 * .. code-block:: c
730 *
731 * long a, b;
732 * if (!HPyArg_ParseKeywords(ctx, NULL, args, nargs, kwnames, "ll", &a, &b))
733 * return HPy_NULL;
734 * ...
735 *
736 * Using `HPyArg_ParseKeywords` with an `HPyTracker`:
737 *
738 * .. code-block:: c
739 *
740 * HPy a, b;
741 * HPyTracker ht;
742 * if (!HPyArg_ParseKeywords(ctx, &ht, args, nargs, kwnames, "OO", &a, &b))
743 * return HPy_NULL;
744 * ...
745 * HPyTracker_Close(ctx, ht);
746 * ...
747 *
748 * .. note::
749 *
750 * Currently ``HPyArg_ParseKeywords`` only requires the use of an
751 * ``HPyTracker`` when the ``O`` format is used. In future other new format
752 * string codes (e.g. for character strings) may also require it.
753 */
754 HPyAPI_HELPER int
755 HPyArg_ParseKeywords(HPyContext *ctx, HPyTracker *ht, const HPy *args,
756 size_t nargs, HPy kwnames, const char *fmt,
757 const char *keywords[], ...)
758 {
759 int retval;
760 va_list va;
761
762 va_start(va, keywords);
763 retval = parse_keywords(ctx, ht, args, nargs, kwnames, 1, fmt, keywords, &va);
764 va_end(va);
765 return retval;
766 }
767
768 /**
769 * Parse positional arguments and keyword arguments in a dict.
770 *
771 * :param ctx:
772 * The execution context.
773 * :param ht:
774 * An optional pointer to an HPyTracker. If the format string never
775 * results in new handles being created, ``ht`` may be ``NULL``. Currently
776 * only the ``O`` formatting option to this function requires an HPyTracker.
777 * :param args:
778 * The array of positional arguments to parse.
779 * :param nargs:
780 * The number of elements in ``args``.
781 * :param kw:
782 * A handle to the dictionary of keyword arguments (may be ``HPy_NULL``).
783 * :param fmt:
784 * The format string to use to parse the arguments.
785 * :param keywords:
786 * A ``NULL``-terminated array of argument names. The number of names
787 * should match the format string provided. Positional only arguments
788 * should have the name ``""`` (i.e. the null-terminated empty string).
789 * Positional only arguments must preceded all other arguments.
790 * :param ...:
791 * A va_list of references to variables in which to store the parsed
792 * arguments. The number and types of the arguments should match the
793 * the format string, ``fmt``.
794 *
795 * :returns: ``0`` on failure, ``1`` on success.
796 *
797 * If a ``NULL`` pointer is passed to ``ht`` and an HPyTracker is required by
798 * the format string, a ``SystemError`` will be raised.
799 *
800 * If a pointer is provided to ``ht``, the HPyTracker will always be created and
801 * must be closed with ``HPyTracker_Close`` if parsing succeeds (after all
802 * handles returned are no longer needed). If parsing fails, this function will
803 * close the HPyTracker automatically.
804 *
805 * For examples, see :c:func:`HPyArg_ParseKeywords`.
806 */
807 HPyAPI_HELPER int
808 HPyArg_ParseKeywordsDict(HPyContext *ctx, HPyTracker *ht, const HPy *args,
809 HPy_ssize_t nargs, HPy kw, const char *fmt,
810 const char *keywords[], ...)
811 {
812 int retval;
813 va_list va;
814
815 va_start(va, keywords);
816 retval = parse_keywords(ctx, ht, args, nargs, kw, 0, fmt, keywords, &va);
817 va_end(va);
818 return retval;
819 }