1
2/* New getargs implementation */
3
4#include "Python.h"
5#include "pycore_tuple.h" // _PyTuple_ITEMS()
6
7#include <ctype.h>
8#include <float.h>
9
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14int PyArg_Parse(PyObject *, const char *, ...);
15int PyArg_ParseTuple(PyObject *, const char *, ...);
16int PyArg_VaParse(PyObject *, const char *, va_list);
17
18int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
19 const char *, char **, ...);
20int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
21 const char *, char **, va_list);
22
23int _PyArg_ParseTupleAndKeywordsFast(PyObject *, PyObject *,
24 struct _PyArg_Parser *, ...);
25int _PyArg_VaParseTupleAndKeywordsFast(PyObject *, PyObject *,
26 struct _PyArg_Parser *, va_list);
27
28#ifdef HAVE_DECLSPEC_DLL
29/* Export functions */
30PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...);
31PyAPI_FUNC(int) _PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs,
32 const char *format, ...);
33PyAPI_FUNC(int) _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs,
34 PyObject *kwnames,
35 struct _PyArg_Parser *parser, ...);
36PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...);
37PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
38 const char *, char **, ...);
39PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
40PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list);
41PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
42 const char *, char **, va_list);
43
44PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
45 struct _PyArg_Parser *, ...);
46PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
47 struct _PyArg_Parser *, va_list);
48#endif
49
50#define FLAG_COMPAT 1
51#define FLAG_SIZE_T 2
52
53typedef int (*destr_t)(PyObject *, void *);
54
55
56/* Keep track of "objects" that have been allocated or initialized and
57 which will need to be deallocated or cleaned up somehow if overall
58 parsing fails.
59*/
60typedef struct {
61 void *item;
62 destr_t destructor;
63} freelistentry_t;
64
65typedef struct {
66 freelistentry_t *entries;
67 int first_available;
68 int entries_malloced;
69} freelist_t;
70
71#define STATIC_FREELIST_ENTRIES 8
72
73/* Forward */
74static int vgetargs1_impl(PyObject *args, PyObject *const *stack, Py_ssize_t nargs,
75 const char *format, va_list *p_va, int flags);
76static int vgetargs1(PyObject *, const char *, va_list *, int);
77static void seterror(Py_ssize_t, const char *, int *, const char *, const char *);
78static const char *convertitem(PyObject *, const char **, va_list *, int, int *,
79 char *, size_t, freelist_t *);
80static const char *converttuple(PyObject *, const char **, va_list *, int,
81 int *, char *, size_t, int, freelist_t *);
82static const char *convertsimple(PyObject *, const char **, va_list *, int,
83 char *, size_t, freelist_t *);
84static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **);
85static int getbuffer(PyObject *, Py_buffer *, const char**);
86
87static int vgetargskeywords(PyObject *, PyObject *,
88 const char *, char **, va_list *, int);
89static int vgetargskeywordsfast(PyObject *, PyObject *,
90 struct _PyArg_Parser *, va_list *, int);
91static int vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
92 PyObject *keywords, PyObject *kwnames,
93 struct _PyArg_Parser *parser,
94 va_list *p_va, int flags);
95static const char *skipitem(const char **, va_list *, int);
96
97int
98PyArg_Parse(PyObject *args, const char *format, ...)
99{
100 int retval;
101 va_list va;
102
103 va_start(va, format);
104 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
105 va_end(va);
106 return retval;
107}
108
109PyAPI_FUNC(int)
110_PyArg_Parse_SizeT(PyObject *args, const char *format, ...)
111{
112 int retval;
113 va_list va;
114
115 va_start(va, format);
116 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
117 va_end(va);
118 return retval;
119}
120
121
122int
123PyArg_ParseTuple(PyObject *args, const char *format, ...)
124{
125 int retval;
126 va_list va;
127
128 va_start(va, format);
129 retval = vgetargs1(args, format, &va, 0);
130 va_end(va);
131 return retval;
132}
133
134PyAPI_FUNC(int)
135_PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...)
136{
137 int retval;
138 va_list va;
139
140 va_start(va, format);
141 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
142 va_end(va);
143 return retval;
144}
145
146
147int
148_PyArg_ParseStack(PyObject *const *args, Py_ssize_t nargs, const char *format, ...)
149{
150 int retval;
151 va_list va;
152
153 va_start(va, format);
154 retval = vgetargs1_impl(NULL, args, nargs, format, &va, 0);
155 va_end(va);
156 return retval;
157}
158
159PyAPI_FUNC(int)
160_PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs, const char *format, ...)
161{
162 int retval;
163 va_list va;
164
165 va_start(va, format);
166 retval = vgetargs1_impl(NULL, args, nargs, format, &va, FLAG_SIZE_T);
167 va_end(va);
168 return retval;
169}
170
171
172int
173PyArg_VaParse(PyObject *args, const char *format, va_list va)
174{
175 va_list lva;
176 int retval;
177
178 va_copy(lva, va);
179
180 retval = vgetargs1(args, format, &lva, 0);
181 va_end(lva);
182 return retval;
183}
184
185PyAPI_FUNC(int)
186_PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
187{
188 va_list lva;
189 int retval;
190
191 va_copy(lva, va);
192
193 retval = vgetargs1(args, format, &lva, FLAG_SIZE_T);
194 va_end(lva);
195 return retval;
196}
197
198
199/* Handle cleanup of allocated memory in case of exception */
200
201static int
202cleanup_ptr(PyObject *self, void *ptr)
203{
204 if (ptr) {
205 PyMem_Free(ptr);
206 }
207 return 0;
208}
209
210static int
211cleanup_buffer(PyObject *self, void *ptr)
212{
213 Py_buffer *buf = (Py_buffer *)ptr;
214 if (buf) {
215 PyBuffer_Release(buf);
216 }
217 return 0;
218}
219
220static int
221addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
222{
223 int index;
224
225 index = freelist->first_available;
226 freelist->first_available += 1;
227
228 freelist->entries[index].item = ptr;
229 freelist->entries[index].destructor = destructor;
230
231 return 0;
232}
233
234static int
235cleanreturn(int retval, freelist_t *freelist)
236{
237 int index;
238
239 if (retval == 0) {
240 /* A failure occurred, therefore execute all of the cleanup
241 functions.
242 */
243 for (index = 0; index < freelist->first_available; ++index) {
244 freelist->entries[index].destructor(NULL,
245 freelist->entries[index].item);
246 }
247 }
248 if (freelist->entries_malloced)
249 PyMem_Free(freelist->entries);
250 return retval;
251}
252
253
254static int
255vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs, const char *format,
256 va_list *p_va, int flags)
257{
258 char msgbuf[256];
259 int levels[32];
260 const char *fname = NULL;
261 const char *message = NULL;
262 int min = -1;
263 int max = 0;
264 int level = 0;
265 int endfmt = 0;
266 const char *formatsave = format;
267 Py_ssize_t i;
268 const char *msg;
269 int compat = flags & FLAG_COMPAT;
270 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
271 freelist_t freelist;
272
273 assert(nargs == 0 || stack != NULL);
274
275 freelist.entries = static_entries;
276 freelist.first_available = 0;
277 freelist.entries_malloced = 0;
278
279 flags = flags & ~FLAG_COMPAT;
280
281 while (endfmt == 0) {
282 int c = *format++;
283 switch (c) {
284 case '(':
285 if (level == 0)
286 max++;
287 level++;
288 if (level >= 30)
289 Py_FatalError("too many tuple nesting levels "
290 "in argument format string");
291 break;
292 case ')':
293 if (level == 0)
294 Py_FatalError("excess ')' in getargs format");
295 else
296 level--;
297 break;
298 case '\0':
299 endfmt = 1;
300 break;
301 case ':':
302 fname = format;
303 endfmt = 1;
304 break;
305 case ';':
306 message = format;
307 endfmt = 1;
308 break;
309 case '|':
310 if (level == 0)
311 min = max;
312 break;
313 default:
314 if (level == 0) {
315 if (Py_ISALPHA(c))
316 if (c != 'e') /* skip encoded */
317 max++;
318 }
319 break;
320 }
321 }
322
323 if (level != 0)
324 Py_FatalError(/* '(' */ "missing ')' in getargs format");
325
326 if (min < 0)
327 min = max;
328
329 format = formatsave;
330
331 if (max > STATIC_FREELIST_ENTRIES) {
332 freelist.entries = PyMem_NEW(freelistentry_t, max);
333 if (freelist.entries == NULL) {
334 PyErr_NoMemory();
335 return 0;
336 }
337 freelist.entries_malloced = 1;
338 }
339
340 if (compat) {
341 if (max == 0) {
342 if (compat_args == NULL)
343 return 1;
344 PyErr_Format(PyExc_TypeError,
345 "%.200s%s takes no arguments",
346 fname==NULL ? "function" : fname,
347 fname==NULL ? "" : "()");
348 return cleanreturn(0, &freelist);
349 }
350 else if (min == 1 && max == 1) {
351 if (compat_args == NULL) {
352 PyErr_Format(PyExc_TypeError,
353 "%.200s%s takes at least one argument",
354 fname==NULL ? "function" : fname,
355 fname==NULL ? "" : "()");
356 return cleanreturn(0, &freelist);
357 }
358 msg = convertitem(compat_args, &format, p_va, flags, levels,
359 msgbuf, sizeof(msgbuf), &freelist);
360 if (msg == NULL)
361 return cleanreturn(1, &freelist);
362 seterror(levels[0], msg, levels+1, fname, message);
363 return cleanreturn(0, &freelist);
364 }
365 else {
366 PyErr_SetString(PyExc_SystemError,
367 "old style getargs format uses new features");
368 return cleanreturn(0, &freelist);
369 }
370 }
371
372 if (nargs < min || max < nargs) {
373 if (message == NULL)
374 PyErr_Format(PyExc_TypeError,
375 "%.150s%s takes %s %d argument%s (%zd given)",
376 fname==NULL ? "function" : fname,
377 fname==NULL ? "" : "()",
378 min==max ? "exactly"
379 : nargs < min ? "at least" : "at most",
380 nargs < min ? min : max,
381 (nargs < min ? min : max) == 1 ? "" : "s",
382 nargs);
383 else
384 PyErr_SetString(PyExc_TypeError, message);
385 return cleanreturn(0, &freelist);
386 }
387
388 for (i = 0; i < nargs; i++) {
389 if (*format == '|')
390 format++;
391 msg = convertitem(stack[i], &format, p_va,
392 flags, levels, msgbuf,
393 sizeof(msgbuf), &freelist);
394 if (msg) {
395 seterror(i+1, msg, levels, fname, message);
396 return cleanreturn(0, &freelist);
397 }
398 }
399
400 if (*format != '\0' && !Py_ISALPHA(*format) &&
401 *format != '(' &&
402 *format != '|' && *format != ':' && *format != ';') {
403 PyErr_Format(PyExc_SystemError,
404 "bad format string: %.200s", formatsave);
405 return cleanreturn(0, &freelist);
406 }
407
408 return cleanreturn(1, &freelist);
409}
410
411static int
412vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
413{
414 PyObject **stack;
415 Py_ssize_t nargs;
416
417 if (!(flags & FLAG_COMPAT)) {
418 assert(args != NULL);
419
420 if (!PyTuple_Check(args)) {
421 PyErr_SetString(PyExc_SystemError,
422 "new style getargs format but argument is not a tuple");
423 return 0;
424 }
425
426 stack = _PyTuple_ITEMS(args);
427 nargs = PyTuple_GET_SIZE(args);
428 }
429 else {
430 stack = NULL;
431 nargs = 0;
432 }
433
434 return vgetargs1_impl(args, stack, nargs, format, p_va, flags);
435}
436
437
438static void
439seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname,
440 const char *message)
441{
442 char buf[512];
443 int i;
444 char *p = buf;
445
446 if (PyErr_Occurred())
447 return;
448 else if (message == NULL) {
449 if (fname != NULL) {
450 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
451 p += strlen(p);
452 }
453 if (iarg != 0) {
454 PyOS_snprintf(p, sizeof(buf) - (p - buf),
455 "argument %zd", iarg);
456 i = 0;
457 p += strlen(p);
458 while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) {
459 PyOS_snprintf(p, sizeof(buf) - (p - buf),
460 ", item %d", levels[i]-1);
461 p += strlen(p);
462 i++;
463 }
464 }
465 else {
466 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
467 p += strlen(p);
468 }
469 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
470 message = buf;
471 }
472 if (msg[0] == '(') {
473 PyErr_SetString(PyExc_SystemError, message);
474 }
475 else {
476 PyErr_SetString(PyExc_TypeError, message);
477 }
478}
479
480
481/* Convert a tuple argument.
482 On entry, *p_format points to the character _after_ the opening '('.
483 On successful exit, *p_format points to the closing ')'.
484 If successful:
485 *p_format and *p_va are updated,
486 *levels and *msgbuf are untouched,
487 and NULL is returned.
488 If the argument is invalid:
489 *p_format is unchanged,
490 *p_va is undefined,
491 *levels is a 0-terminated list of item numbers,
492 *msgbuf contains an error message, whose format is:
493 "must be <typename1>, not <typename2>", where:
494 <typename1> is the name of the expected type, and
495 <typename2> is the name of the actual type,
496 and msgbuf is returned.
497*/
498
499static const char *
500converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
501 int *levels, char *msgbuf, size_t bufsize, int toplevel,
502 freelist_t *freelist)
503{
504 int level = 0;
505 int n = 0;
506 const char *format = *p_format;
507 int i;
508 Py_ssize_t len;
509
510 for (;;) {
511 int c = *format++;
512 if (c == '(') {
513 if (level == 0)
514 n++;
515 level++;
516 }
517 else if (c == ')') {
518 if (level == 0)
519 break;
520 level--;
521 }
522 else if (c == ':' || c == ';' || c == '\0')
523 break;
524 else if (level == 0 && Py_ISALPHA(c))
525 n++;
526 }
527
528 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
529 levels[0] = 0;
530 PyOS_snprintf(msgbuf, bufsize,
531 toplevel ? "expected %d arguments, not %.50s" :
532 "must be %d-item sequence, not %.50s",
533 n,
534 arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
535 return msgbuf;
536 }
537
538 len = PySequence_Size(arg);
539 if (len != n) {
540 levels[0] = 0;
541 if (toplevel) {
542 PyOS_snprintf(msgbuf, bufsize,
543 "expected %d argument%s, not %zd",
544 n,
545 n == 1 ? "" : "s",
546 len);
547 }
548 else {
549 PyOS_snprintf(msgbuf, bufsize,
550 "must be sequence of length %d, not %zd",
551 n, len);
552 }
553 return msgbuf;
554 }
555
556 format = *p_format;
557 for (i = 0; i < n; i++) {
558 const char *msg;
559 PyObject *item;
560 item = PySequence_GetItem(arg, i);
561 if (item == NULL) {
562 PyErr_Clear();
563 levels[0] = i+1;
564 levels[1] = 0;
565 strncpy(msgbuf, "is not retrievable", bufsize);
566 return msgbuf;
567 }
568 msg = convertitem(item, &format, p_va, flags, levels+1,
569 msgbuf, bufsize, freelist);
570 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
571 Py_XDECREF(item);
572 if (msg != NULL) {
573 levels[0] = i+1;
574 return msg;
575 }
576 }
577
578 *p_format = format;
579 return NULL;
580}
581
582
583/* Convert a single item. */
584
585static const char *
586convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
587 int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
588{
589 const char *msg;
590 const char *format = *p_format;
591
592 if (*format == '(' /* ')' */) {
593 format++;
594 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
595 bufsize, 0, freelist);
596 if (msg == NULL)
597 format++;
598 }
599 else {
600 msg = convertsimple(arg, &format, p_va, flags,
601 msgbuf, bufsize, freelist);
602 if (msg != NULL)
603 levels[0] = 0;
604 }
605 if (msg == NULL)
606 *p_format = format;
607 return msg;
608}
609
610
611
612/* Format an error message generated by convertsimple().
613 displayname must be UTF-8 encoded.
614*/
615
616void
617_PyArg_BadArgument(const char *fname, const char *displayname,
618 const char *expected, PyObject *arg)
619{
620 PyErr_Format(PyExc_TypeError,
621 "%.200s() %.200s must be %.50s, not %.50s",
622 fname, displayname, expected,
623 arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
624}
625
626static const char *
627converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
628{
629 assert(expected != NULL);
630 assert(arg != NULL);
631 if (expected[0] == '(') {
632 PyOS_snprintf(msgbuf, bufsize,
633 "%.100s", expected);
634 }
635 else {
636 PyOS_snprintf(msgbuf, bufsize,
637 "must be %.50s, not %.50s", expected,
638 arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
639 }
640 return msgbuf;
641}
642
643#define CONV_UNICODE "(unicode conversion error)"
644
645/* Convert a non-tuple argument. Return NULL if conversion went OK,
646 or a string with a message describing the failure. The message is
647 formatted as "must be <desired type>, not <actual type>".
648 When failing, an exception may or may not have been raised.
649 Don't call if a tuple is expected.
650
651 When you add new format codes, please don't forget poor skipitem() below.
652*/
653
654static const char *
655convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
656 char *msgbuf, size_t bufsize, freelist_t *freelist)
657{
658#define RETURN_ERR_OCCURRED return msgbuf
659 /* For # codes */
660#define REQUIRE_PY_SSIZE_T_CLEAN \
661 if (!(flags & FLAG_SIZE_T)) { \
662 PyErr_SetString(PyExc_SystemError, \
663 "PY_SSIZE_T_CLEAN macro must be defined for '#' formats"); \
664 RETURN_ERR_OCCURRED; \
665 }
666
667 const char *format = *p_format;
668 char c = *format++;
669 const char *sarg;
670
671 switch (c) {
672
673 case 'b': { /* unsigned byte -- very short int */
674 char *p = va_arg(*p_va, char *);
675 long ival = PyLong_AsLong(arg);
676 if (ival == -1 && PyErr_Occurred())
677 RETURN_ERR_OCCURRED;
678 else if (ival < 0) {
679 PyErr_SetString(PyExc_OverflowError,
680 "unsigned byte integer is less than minimum");
681 RETURN_ERR_OCCURRED;
682 }
683 else if (ival > UCHAR_MAX) {
684 PyErr_SetString(PyExc_OverflowError,
685 "unsigned byte integer is greater than maximum");
686 RETURN_ERR_OCCURRED;
687 }
688 else
689 *p = (unsigned char) ival;
690 break;
691 }
692
693 case 'B': {/* byte sized bitfield - both signed and unsigned
694 values allowed */
695 char *p = va_arg(*p_va, char *);
696 unsigned long ival = PyLong_AsUnsignedLongMask(arg);
697 if (ival == (unsigned long)-1 && PyErr_Occurred())
698 RETURN_ERR_OCCURRED;
699 else
700 *p = (unsigned char) ival;
701 break;
702 }
703
704 case 'h': {/* signed short int */
705 short *p = va_arg(*p_va, short *);
706 long ival = PyLong_AsLong(arg);
707 if (ival == -1 && PyErr_Occurred())
708 RETURN_ERR_OCCURRED;
709 else if (ival < SHRT_MIN) {
710 PyErr_SetString(PyExc_OverflowError,
711 "signed short integer is less than minimum");
712 RETURN_ERR_OCCURRED;
713 }
714 else if (ival > SHRT_MAX) {
715 PyErr_SetString(PyExc_OverflowError,
716 "signed short integer is greater than maximum");
717 RETURN_ERR_OCCURRED;
718 }
719 else
720 *p = (short) ival;
721 break;
722 }
723
724 case 'H': { /* short int sized bitfield, both signed and
725 unsigned allowed */
726 unsigned short *p = va_arg(*p_va, unsigned short *);
727 unsigned long ival = PyLong_AsUnsignedLongMask(arg);
728 if (ival == (unsigned long)-1 && PyErr_Occurred())
729 RETURN_ERR_OCCURRED;
730 else
731 *p = (unsigned short) ival;
732 break;
733 }
734
735 case 'i': {/* signed int */
736 int *p = va_arg(*p_va, int *);
737 long ival = PyLong_AsLong(arg);
738 if (ival == -1 && PyErr_Occurred())
739 RETURN_ERR_OCCURRED;
740 else if (ival > INT_MAX) {
741 PyErr_SetString(PyExc_OverflowError,
742 "signed integer is greater than maximum");
743 RETURN_ERR_OCCURRED;
744 }
745 else if (ival < INT_MIN) {
746 PyErr_SetString(PyExc_OverflowError,
747 "signed integer is less than minimum");
748 RETURN_ERR_OCCURRED;
749 }
750 else
751 *p = ival;
752 break;
753 }
754
755 case 'I': { /* int sized bitfield, both signed and
756 unsigned allowed */
757 unsigned int *p = va_arg(*p_va, unsigned int *);
758 unsigned long ival = PyLong_AsUnsignedLongMask(arg);
759 if (ival == (unsigned long)-1 && PyErr_Occurred())
760 RETURN_ERR_OCCURRED;
761 else
762 *p = (unsigned int) ival;
763 break;
764 }
765
766 case 'n': /* Py_ssize_t */
767 {
768 PyObject *iobj;
769 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
770 Py_ssize_t ival = -1;
771 iobj = _PyNumber_Index(arg);
772 if (iobj != NULL) {
773 ival = PyLong_AsSsize_t(iobj);
774 Py_DECREF(iobj);
775 }
776 if (ival == -1 && PyErr_Occurred())
777 RETURN_ERR_OCCURRED;
778 *p = ival;
779 break;
780 }
781 case 'l': {/* long int */
782 long *p = va_arg(*p_va, long *);
783 long ival = PyLong_AsLong(arg);
784 if (ival == -1 && PyErr_Occurred())
785 RETURN_ERR_OCCURRED;
786 else
787 *p = ival;
788 break;
789 }
790
791 case 'k': { /* long sized bitfield */
792 unsigned long *p = va_arg(*p_va, unsigned long *);
793 unsigned long ival;
794 if (PyLong_Check(arg))
795 ival = PyLong_AsUnsignedLongMask(arg);
796 else
797 return converterr("int", arg, msgbuf, bufsize);
798 *p = ival;
799 break;
800 }
801
802 case 'L': {/* long long */
803 long long *p = va_arg( *p_va, long long * );
804 long long ival = PyLong_AsLongLong(arg);
805 if (ival == (long long)-1 && PyErr_Occurred())
806 RETURN_ERR_OCCURRED;
807 else
808 *p = ival;
809 break;
810 }
811
812 case 'K': { /* long long sized bitfield */
813 unsigned long long *p = va_arg(*p_va, unsigned long long *);
814 unsigned long long ival;
815 if (PyLong_Check(arg))
816 ival = PyLong_AsUnsignedLongLongMask(arg);
817 else
818 return converterr("int", arg, msgbuf, bufsize);
819 *p = ival;
820 break;
821 }
822
823 case 'f': {/* float */
824 float *p = va_arg(*p_va, float *);
825 double dval = PyFloat_AsDouble(arg);
826 if (dval == -1.0 && PyErr_Occurred())
827 RETURN_ERR_OCCURRED;
828 else
829 *p = (float) dval;
830 break;
831 }
832
833 case 'd': {/* double */
834 double *p = va_arg(*p_va, double *);
835 double dval = PyFloat_AsDouble(arg);
836 if (dval == -1.0 && PyErr_Occurred())
837 RETURN_ERR_OCCURRED;
838 else
839 *p = dval;
840 break;
841 }
842
843 case 'D': {/* complex double */
844 Py_complex *p = va_arg(*p_va, Py_complex *);
845 Py_complex cval;
846 cval = PyComplex_AsCComplex(arg);
847 if (PyErr_Occurred())
848 RETURN_ERR_OCCURRED;
849 else
850 *p = cval;
851 break;
852 }
853
854 case 'c': {/* char */
855 char *p = va_arg(*p_va, char *);
856 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
857 *p = PyBytes_AS_STRING(arg)[0];
858 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
859 *p = PyByteArray_AS_STRING(arg)[0];
860 else
861 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
862 break;
863 }
864
865 case 'C': {/* unicode char */
866 int *p = va_arg(*p_va, int *);
867 int kind;
868 const void *data;
869
870 if (!PyUnicode_Check(arg))
871 return converterr("a unicode character", arg, msgbuf, bufsize);
872
873 if (PyUnicode_READY(arg))
874 RETURN_ERR_OCCURRED;
875
876 if (PyUnicode_GET_LENGTH(arg) != 1)
877 return converterr("a unicode character", arg, msgbuf, bufsize);
878
879 kind = PyUnicode_KIND(arg);
880 data = PyUnicode_DATA(arg);
881 *p = PyUnicode_READ(kind, data, 0);
882 break;
883 }
884
885 case 'p': {/* boolean *p*redicate */
886 int *p = va_arg(*p_va, int *);
887 int val = PyObject_IsTrue(arg);
888 if (val > 0)
889 *p = 1;
890 else if (val == 0)
891 *p = 0;
892 else
893 RETURN_ERR_OCCURRED;
894 break;
895 }
896
897 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
898 need to be cleaned up! */
899
900 case 'y': {/* any bytes-like object */
901 void **p = (void **)va_arg(*p_va, char **);
902 const char *buf;
903 Py_ssize_t count;
904 if (*format == '*') {
905 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
906 return converterr(buf, arg, msgbuf, bufsize);
907 format++;
908 if (addcleanup(p, freelist, cleanup_buffer)) {
909 return converterr(
910 "(cleanup problem)",
911 arg, msgbuf, bufsize);
912 }
913 break;
914 }
915 count = convertbuffer(arg, (const void **)p, &buf);
916 if (count < 0)
917 return converterr(buf, arg, msgbuf, bufsize);
918 if (*format == '#') {
919 REQUIRE_PY_SSIZE_T_CLEAN;
920 Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*);
921 *psize = count;
922 format++;
923 } else {
924 if (strlen(*p) != (size_t)count) {
925 PyErr_SetString(PyExc_ValueError, "embedded null byte");
926 RETURN_ERR_OCCURRED;
927 }
928 }
929 break;
930 }
931
932 case 's': /* text string or bytes-like object */
933 case 'z': /* text string, bytes-like object or None */
934 {
935 if (*format == '*') {
936 /* "s*" or "z*" */
937 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
938
939 if (c == 'z' && arg == Py_None)
940 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
941 else if (PyUnicode_Check(arg)) {
942 Py_ssize_t len;
943 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
944 if (sarg == NULL)
945 return converterr(CONV_UNICODE,
946 arg, msgbuf, bufsize);
947 PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0);
948 }
949 else { /* any bytes-like object */
950 const char *buf;
951 if (getbuffer(arg, p, &buf) < 0)
952 return converterr(buf, arg, msgbuf, bufsize);
953 }
954 if (addcleanup(p, freelist, cleanup_buffer)) {
955 return converterr(
956 "(cleanup problem)",
957 arg, msgbuf, bufsize);
958 }
959 format++;
960 } else if (*format == '#') { /* a string or read-only bytes-like object */
961 /* "s#" or "z#" */
962 const void **p = (const void **)va_arg(*p_va, const char **);
963 REQUIRE_PY_SSIZE_T_CLEAN;
964 Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*);
965
966 if (c == 'z' && arg == Py_None) {
967 *p = NULL;
968 *psize = 0;
969 }
970 else if (PyUnicode_Check(arg)) {
971 Py_ssize_t len;
972 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
973 if (sarg == NULL)
974 return converterr(CONV_UNICODE,
975 arg, msgbuf, bufsize);
976 *p = sarg;
977 *psize = len;
978 }
979 else { /* read-only bytes-like object */
980 /* XXX Really? */
981 const char *buf;
982 Py_ssize_t count = convertbuffer(arg, p, &buf);
983 if (count < 0)
984 return converterr(buf, arg, msgbuf, bufsize);
985 *psize = count;
986 }
987 format++;
988 } else {
989 /* "s" or "z" */
990 const char **p = va_arg(*p_va, const char **);
991 Py_ssize_t len;
992 sarg = NULL;
993
994 if (c == 'z' && arg == Py_None)
995 *p = NULL;
996 else if (PyUnicode_Check(arg)) {
997 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
998 if (sarg == NULL)
999 return converterr(CONV_UNICODE,
1000 arg, msgbuf, bufsize);
1001 if (strlen(sarg) != (size_t)len) {
1002 PyErr_SetString(PyExc_ValueError, "embedded null character");
1003 RETURN_ERR_OCCURRED;
1004 }
1005 *p = sarg;
1006 }
1007 else
1008 return converterr(c == 'z' ? "str or None" : "str",
1009 arg, msgbuf, bufsize);
1010 }
1011 break;
1012 }
1013
1014 case 'u': /* raw unicode buffer (Py_UNICODE *) */
1015 case 'Z': /* raw unicode buffer or None */
1016 {
1017 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1018 "getargs: The '%c' format is deprecated. Use 'U' instead.", c)) {
1019 return NULL;
1020 }
1021_Py_COMP_DIAG_PUSH
1022_Py_COMP_DIAG_IGNORE_DEPR_DECLS
1023 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1024
1025 if (*format == '#') {
1026 /* "u#" or "Z#" */
1027 REQUIRE_PY_SSIZE_T_CLEAN;
1028 Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*);
1029
1030 if (c == 'Z' && arg == Py_None) {
1031 *p = NULL;
1032 *psize = 0;
1033 }
1034 else if (PyUnicode_Check(arg)) {
1035 Py_ssize_t len;
1036 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
1037 if (*p == NULL)
1038 RETURN_ERR_OCCURRED;
1039 *psize = len;
1040 }
1041 else
1042 return converterr(c == 'Z' ? "str or None" : "str",
1043 arg, msgbuf, bufsize);
1044 format++;
1045 } else {
1046 /* "u" or "Z" */
1047 if (c == 'Z' && arg == Py_None)
1048 *p = NULL;
1049 else if (PyUnicode_Check(arg)) {
1050 Py_ssize_t len;
1051 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
1052 if (*p == NULL)
1053 RETURN_ERR_OCCURRED;
1054 if (wcslen(*p) != (size_t)len) {
1055 PyErr_SetString(PyExc_ValueError, "embedded null character");
1056 RETURN_ERR_OCCURRED;
1057 }
1058 } else
1059 return converterr(c == 'Z' ? "str or None" : "str",
1060 arg, msgbuf, bufsize);
1061 }
1062 break;
1063_Py_COMP_DIAG_POP
1064 }
1065
1066 case 'e': {/* encoded string */
1067 char **buffer;
1068 const char *encoding;
1069 PyObject *s;
1070 int recode_strings;
1071 Py_ssize_t size;
1072 const char *ptr;
1073
1074 /* Get 'e' parameter: the encoding name */
1075 encoding = (const char *)va_arg(*p_va, const char *);
1076 if (encoding == NULL)
1077 encoding = PyUnicode_GetDefaultEncoding();
1078
1079 /* Get output buffer parameter:
1080 's' (recode all objects via Unicode) or
1081 't' (only recode non-string objects)
1082 */
1083 if (*format == 's')
1084 recode_strings = 1;
1085 else if (*format == 't')
1086 recode_strings = 0;
1087 else
1088 return converterr(
1089 "(unknown parser marker combination)",
1090 arg, msgbuf, bufsize);
1091 buffer = (char **)va_arg(*p_va, char **);
1092 format++;
1093 if (buffer == NULL)
1094 return converterr("(buffer is NULL)",
1095 arg, msgbuf, bufsize);
1096
1097 /* Encode object */
1098 if (!recode_strings &&
1099 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1100 s = arg;
1101 Py_INCREF(s);
1102 if (PyBytes_Check(arg)) {
1103 size = PyBytes_GET_SIZE(s);
1104 ptr = PyBytes_AS_STRING(s);
1105 }
1106 else {
1107 size = PyByteArray_GET_SIZE(s);
1108 ptr = PyByteArray_AS_STRING(s);
1109 }
1110 }
1111 else if (PyUnicode_Check(arg)) {
1112 /* Encode object; use default error handling */
1113 s = PyUnicode_AsEncodedString(arg,
1114 encoding,
1115 NULL);
1116 if (s == NULL)
1117 return converterr("(encoding failed)",
1118 arg, msgbuf, bufsize);
1119 assert(PyBytes_Check(s));
1120 size = PyBytes_GET_SIZE(s);
1121 ptr = PyBytes_AS_STRING(s);
1122 if (ptr == NULL)
1123 ptr = "";
1124 }
1125 else {
1126 return converterr(
1127 recode_strings ? "str" : "str, bytes or bytearray",
1128 arg, msgbuf, bufsize);
1129 }
1130
1131 /* Write output; output is guaranteed to be 0-terminated */
1132 if (*format == '#') {
1133 /* Using buffer length parameter '#':
1134
1135 - if *buffer is NULL, a new buffer of the
1136 needed size is allocated and the data
1137 copied into it; *buffer is updated to point
1138 to the new buffer; the caller is
1139 responsible for PyMem_Free()ing it after
1140 usage
1141
1142 - if *buffer is not NULL, the data is
1143 copied to *buffer; *buffer_len has to be
1144 set to the size of the buffer on input;
1145 buffer overflow is signalled with an error;
1146 buffer has to provide enough room for the
1147 encoded string plus the trailing 0-byte
1148
1149 - in both cases, *buffer_len is updated to
1150 the size of the buffer /excluding/ the
1151 trailing 0-byte
1152
1153 */
1154 REQUIRE_PY_SSIZE_T_CLEAN;
1155 Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*);
1156
1157 format++;
1158 if (psize == NULL) {
1159 Py_DECREF(s);
1160 return converterr(
1161 "(buffer_len is NULL)",
1162 arg, msgbuf, bufsize);
1163 }
1164 if (*buffer == NULL) {
1165 *buffer = PyMem_NEW(char, size + 1);
1166 if (*buffer == NULL) {
1167 Py_DECREF(s);
1168 PyErr_NoMemory();
1169 RETURN_ERR_OCCURRED;
1170 }
1171 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1172 Py_DECREF(s);
1173 return converterr(
1174 "(cleanup problem)",
1175 arg, msgbuf, bufsize);
1176 }
1177 } else {
1178 if (size + 1 > *psize) {
1179 Py_DECREF(s);
1180 PyErr_Format(PyExc_ValueError,
1181 "encoded string too long "
1182 "(%zd, maximum length %zd)",
1183 (Py_ssize_t)size, (Py_ssize_t)(*psize - 1));
1184 RETURN_ERR_OCCURRED;
1185 }
1186 }
1187 memcpy(*buffer, ptr, size+1);
1188
1189 *psize = size;
1190 }
1191 else {
1192 /* Using a 0-terminated buffer:
1193
1194 - the encoded string has to be 0-terminated
1195 for this variant to work; if it is not, an
1196 error raised
1197
1198 - a new buffer of the needed size is
1199 allocated and the data copied into it;
1200 *buffer is updated to point to the new
1201 buffer; the caller is responsible for
1202 PyMem_Free()ing it after usage
1203
1204 */
1205 if ((Py_ssize_t)strlen(ptr) != size) {
1206 Py_DECREF(s);
1207 return converterr(
1208 "encoded string without null bytes",
1209 arg, msgbuf, bufsize);
1210 }
1211 *buffer = PyMem_NEW(char, size + 1);
1212 if (*buffer == NULL) {
1213 Py_DECREF(s);
1214 PyErr_NoMemory();
1215 RETURN_ERR_OCCURRED;
1216 }
1217 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1218 Py_DECREF(s);
1219 return converterr("(cleanup problem)",
1220 arg, msgbuf, bufsize);
1221 }
1222 memcpy(*buffer, ptr, size+1);
1223 }
1224 Py_DECREF(s);
1225 break;
1226 }
1227
1228 case 'S': { /* PyBytes object */
1229 PyObject **p = va_arg(*p_va, PyObject **);
1230 if (PyBytes_Check(arg))
1231 *p = arg;
1232 else
1233 return converterr("bytes", arg, msgbuf, bufsize);
1234 break;
1235 }
1236
1237 case 'Y': { /* PyByteArray object */
1238 PyObject **p = va_arg(*p_va, PyObject **);
1239 if (PyByteArray_Check(arg))
1240 *p = arg;
1241 else
1242 return converterr("bytearray", arg, msgbuf, bufsize);
1243 break;
1244 }
1245
1246 case 'U': { /* PyUnicode object */
1247 PyObject **p = va_arg(*p_va, PyObject **);
1248 if (PyUnicode_Check(arg)) {
1249 if (PyUnicode_READY(arg) == -1)
1250 RETURN_ERR_OCCURRED;
1251 *p = arg;
1252 }
1253 else
1254 return converterr("str", arg, msgbuf, bufsize);
1255 break;
1256 }
1257
1258 case 'O': { /* object */
1259 PyTypeObject *type;
1260 PyObject **p;
1261 if (*format == '!') {
1262 type = va_arg(*p_va, PyTypeObject*);
1263 p = va_arg(*p_va, PyObject **);
1264 format++;
1265 if (PyType_IsSubtype(Py_TYPE(arg), type))
1266 *p = arg;
1267 else
1268 return converterr(type->tp_name, arg, msgbuf, bufsize);
1269
1270 }
1271 else if (*format == '&') {
1272 typedef int (*converter)(PyObject *, void *);
1273 converter convert = va_arg(*p_va, converter);
1274 void *addr = va_arg(*p_va, void *);
1275 int res;
1276 format++;
1277 if (! (res = (*convert)(arg, addr)))
1278 return converterr("(unspecified)",
1279 arg, msgbuf, bufsize);
1280 if (res == Py_CLEANUP_SUPPORTED &&
1281 addcleanup(addr, freelist, convert) == -1)
1282 return converterr("(cleanup problem)",
1283 arg, msgbuf, bufsize);
1284 }
1285 else {
1286 p = va_arg(*p_va, PyObject **);
1287 *p = arg;
1288 }
1289 break;
1290 }
1291
1292
1293 case 'w': { /* "w*": memory buffer, read-write access */
1294 void **p = va_arg(*p_va, void **);
1295
1296 if (*format != '*')
1297 return converterr(
1298 "(invalid use of 'w' format character)",
1299 arg, msgbuf, bufsize);
1300 format++;
1301
1302 /* Caller is interested in Py_buffer, and the object
1303 supports it directly. */
1304 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1305 PyErr_Clear();
1306 return converterr("read-write bytes-like object",
1307 arg, msgbuf, bufsize);
1308 }
1309 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1310 PyBuffer_Release((Py_buffer*)p);
1311 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1312 }
1313 if (addcleanup(p, freelist, cleanup_buffer)) {
1314 return converterr(
1315 "(cleanup problem)",
1316 arg, msgbuf, bufsize);
1317 }
1318 break;
1319 }
1320
1321 default:
1322 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
1323
1324 }
1325
1326 *p_format = format;
1327 return NULL;
1328
1329#undef REQUIRE_PY_SSIZE_T_CLEAN
1330#undef RETURN_ERR_OCCURRED
1331}
1332
1333static Py_ssize_t
1334convertbuffer(PyObject *arg, const void **p, const char **errmsg)
1335{
1336 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
1337 Py_ssize_t count;
1338 Py_buffer view;
1339
1340 *errmsg = NULL;
1341 *p = NULL;
1342 if (pb != NULL && pb->bf_releasebuffer != NULL) {
1343 *errmsg = "read-only bytes-like object";
1344 return -1;
1345 }
1346
1347 if (getbuffer(arg, &view, errmsg) < 0)
1348 return -1;
1349 count = view.len;
1350 *p = view.buf;
1351 PyBuffer_Release(&view);
1352 return count;
1353}
1354
1355static int
1356getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
1357{
1358 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
1359 *errmsg = "bytes-like object";
1360 return -1;
1361 }
1362 if (!PyBuffer_IsContiguous(view, 'C')) {
1363 PyBuffer_Release(view);
1364 *errmsg = "contiguous buffer";
1365 return -1;
1366 }
1367 return 0;
1368}
1369
1370/* Support for keyword arguments donated by
1371 Geoff Philbrick <[email protected]> */
1372
1373/* Return false (0) for error, else true. */
1374int
1375PyArg_ParseTupleAndKeywords(PyObject *args,
1376 PyObject *keywords,
1377 const char *format,
1378 char **kwlist, ...)
1379{
1380 int retval;
1381 va_list va;
1382
1383 if ((args == NULL || !PyTuple_Check(args)) ||
1384 (keywords != NULL && !PyDict_Check(keywords)) ||
1385 format == NULL ||
1386 kwlist == NULL)
1387 {
1388 PyErr_BadInternalCall();
1389 return 0;
1390 }
1391
1392 va_start(va, kwlist);
1393 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1394 va_end(va);
1395 return retval;
1396}
1397
1398PyAPI_FUNC(int)
1399_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1400 PyObject *keywords,
1401 const char *format,
1402 char **kwlist, ...)
1403{
1404 int retval;
1405 va_list va;
1406
1407 if ((args == NULL || !PyTuple_Check(args)) ||
1408 (keywords != NULL && !PyDict_Check(keywords)) ||
1409 format == NULL ||
1410 kwlist == NULL)
1411 {
1412 PyErr_BadInternalCall();
1413 return 0;
1414 }
1415
1416 va_start(va, kwlist);
1417 retval = vgetargskeywords(args, keywords, format,
1418 kwlist, &va, FLAG_SIZE_T);
1419 va_end(va);
1420 return retval;
1421}
1422
1423
1424int
1425PyArg_VaParseTupleAndKeywords(PyObject *args,
1426 PyObject *keywords,
1427 const char *format,
1428 char **kwlist, va_list va)
1429{
1430 int retval;
1431 va_list lva;
1432
1433 if ((args == NULL || !PyTuple_Check(args)) ||
1434 (keywords != NULL && !PyDict_Check(keywords)) ||
1435 format == NULL ||
1436 kwlist == NULL)
1437 {
1438 PyErr_BadInternalCall();
1439 return 0;
1440 }
1441
1442 va_copy(lva, va);
1443
1444 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1445 va_end(lva);
1446 return retval;
1447}
1448
1449PyAPI_FUNC(int)
1450_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1451 PyObject *keywords,
1452 const char *format,
1453 char **kwlist, va_list va)
1454{
1455 int retval;
1456 va_list lva;
1457
1458 if ((args == NULL || !PyTuple_Check(args)) ||
1459 (keywords != NULL && !PyDict_Check(keywords)) ||
1460 format == NULL ||
1461 kwlist == NULL)
1462 {
1463 PyErr_BadInternalCall();
1464 return 0;
1465 }
1466
1467 va_copy(lva, va);
1468
1469 retval = vgetargskeywords(args, keywords, format,
1470 kwlist, &lva, FLAG_SIZE_T);
1471 va_end(lva);
1472 return retval;
1473}
1474
1475PyAPI_FUNC(int)
1476_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1477 struct _PyArg_Parser *parser, ...)
1478{
1479 int retval;
1480 va_list va;
1481
1482 va_start(va, parser);
1483 retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1484 va_end(va);
1485 return retval;
1486}
1487
1488PyAPI_FUNC(int)
1489_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1490 struct _PyArg_Parser *parser, ...)
1491{
1492 int retval;
1493 va_list va;
1494
1495 va_start(va, parser);
1496 retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
1497 va_end(va);
1498 return retval;
1499}
1500
1501PyAPI_FUNC(int)
1502_PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
1503 struct _PyArg_Parser *parser, ...)
1504{
1505 int retval;
1506 va_list va;
1507
1508 va_start(va, parser);
1509 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1510 va_end(va);
1511 return retval;
1512}
1513
1514PyAPI_FUNC(int)
1515_PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
1516 struct _PyArg_Parser *parser, ...)
1517{
1518 int retval;
1519 va_list va;
1520
1521 va_start(va, parser);
1522 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T);
1523 va_end(va);
1524 return retval;
1525}
1526
1527
1528PyAPI_FUNC(int)
1529_PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1530 struct _PyArg_Parser *parser, va_list va)
1531{
1532 int retval;
1533 va_list lva;
1534
1535 va_copy(lva, va);
1536
1537 retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
1538 va_end(lva);
1539 return retval;
1540}
1541
1542PyAPI_FUNC(int)
1543_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1544 struct _PyArg_Parser *parser, va_list va)
1545{
1546 int retval;
1547 va_list lva;
1548
1549 va_copy(lva, va);
1550
1551 retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
1552 va_end(lva);
1553 return retval;
1554}
1555
1556int
1557PyArg_ValidateKeywordArguments(PyObject *kwargs)
1558{
1559 if (!PyDict_Check(kwargs)) {
1560 PyErr_BadInternalCall();
1561 return 0;
1562 }
1563 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1564 PyErr_SetString(PyExc_TypeError,
1565 "keywords must be strings");
1566 return 0;
1567 }
1568 return 1;
1569}
1570
1571#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1572
1573static int
1574vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
1575 char **kwlist, va_list *p_va, int flags)
1576{
1577 char msgbuf[512];
1578 int levels[32];
1579 const char *fname, *msg, *custom_msg;
1580 int min = INT_MAX;
1581 int max = INT_MAX;
1582 int i, pos, len;
1583 int skip = 0;
1584 Py_ssize_t nargs, nkwargs;
1585 PyObject *current_arg;
1586 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
1587 freelist_t freelist;
1588
1589 freelist.entries = static_entries;
1590 freelist.first_available = 0;
1591 freelist.entries_malloced = 0;
1592
1593 assert(args != NULL && PyTuple_Check(args));
1594 assert(kwargs == NULL || PyDict_Check(kwargs));
1595 assert(format != NULL);
1596 assert(kwlist != NULL);
1597 assert(p_va != NULL);
1598
1599 /* grab the function name or custom error msg first (mutually exclusive) */
1600 fname = strchr(format, ':');
1601 if (fname) {
1602 fname++;
1603 custom_msg = NULL;
1604 }
1605 else {
1606 custom_msg = strchr(format,';');
1607 if (custom_msg)
1608 custom_msg++;
1609 }
1610
1611 /* scan kwlist and count the number of positional-only parameters */
1612 for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1613 }
1614 /* scan kwlist and get greatest possible nbr of args */
1615 for (len = pos; kwlist[len]; len++) {
1616 if (!*kwlist[len]) {
1617 PyErr_SetString(PyExc_SystemError,
1618 "Empty keyword parameter name");
1619 return cleanreturn(0, &freelist);
1620 }
1621 }
1622
1623 if (len > STATIC_FREELIST_ENTRIES) {
1624 freelist.entries = PyMem_NEW(freelistentry_t, len);
1625 if (freelist.entries == NULL) {
1626 PyErr_NoMemory();
1627 return 0;
1628 }
1629 freelist.entries_malloced = 1;
1630 }
1631
1632 nargs = PyTuple_GET_SIZE(args);
1633 nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
1634 if (nargs + nkwargs > len) {
1635 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
1636 messages in some special cases (see bpo-31229). */
1637 PyErr_Format(PyExc_TypeError,
1638 "%.200s%s takes at most %d %sargument%s (%zd given)",
1639 (fname == NULL) ? "function" : fname,
1640 (fname == NULL) ? "" : "()",
1641 len,
1642 (nargs == 0) ? "keyword " : "",
1643 (len == 1) ? "" : "s",
1644 nargs + nkwargs);
1645 return cleanreturn(0, &freelist);
1646 }
1647
1648 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1649 for (i = 0; i < len; i++) {
1650 if (*format == '|') {
1651 if (min != INT_MAX) {
1652 PyErr_SetString(PyExc_SystemError,
1653 "Invalid format string (| specified twice)");
1654 return cleanreturn(0, &freelist);
1655 }
1656
1657 min = i;
1658 format++;
1659
1660 if (max != INT_MAX) {
1661 PyErr_SetString(PyExc_SystemError,
1662 "Invalid format string ($ before |)");
1663 return cleanreturn(0, &freelist);
1664 }
1665 }
1666 if (*format == '$') {
1667 if (max != INT_MAX) {
1668 PyErr_SetString(PyExc_SystemError,
1669 "Invalid format string ($ specified twice)");
1670 return cleanreturn(0, &freelist);
1671 }
1672
1673 max = i;
1674 format++;
1675
1676 if (max < pos) {
1677 PyErr_SetString(PyExc_SystemError,
1678 "Empty parameter name after $");
1679 return cleanreturn(0, &freelist);
1680 }
1681 if (skip) {
1682 /* Now we know the minimal and the maximal numbers of
1683 * positional arguments and can raise an exception with
1684 * informative message (see below). */
1685 break;
1686 }
1687 if (max < nargs) {
1688 if (max == 0) {
1689 PyErr_Format(PyExc_TypeError,
1690 "%.200s%s takes no positional arguments",
1691 (fname == NULL) ? "function" : fname,
1692 (fname == NULL) ? "" : "()");
1693 }
1694 else {
1695 PyErr_Format(PyExc_TypeError,
1696 "%.200s%s takes %s %d positional argument%s"
1697 " (%zd given)",
1698 (fname == NULL) ? "function" : fname,
1699 (fname == NULL) ? "" : "()",
1700 (min != INT_MAX) ? "at most" : "exactly",
1701 max,
1702 max == 1 ? "" : "s",
1703 nargs);
1704 }
1705 return cleanreturn(0, &freelist);
1706 }
1707 }
1708 if (IS_END_OF_FORMAT(*format)) {
1709 PyErr_Format(PyExc_SystemError,
1710 "More keyword list entries (%d) than "
1711 "format specifiers (%d)", len, i);
1712 return cleanreturn(0, &freelist);
1713 }
1714 if (!skip) {
1715 if (i < nargs) {
1716 current_arg = PyTuple_GET_ITEM(args, i);
1717 }
1718 else if (nkwargs && i >= pos) {
1719 current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
1720 if (current_arg) {
1721 --nkwargs;
1722 }
1723 else if (PyErr_Occurred()) {
1724 return cleanreturn(0, &freelist);
1725 }
1726 }
1727 else {
1728 current_arg = NULL;
1729 }
1730
1731 if (current_arg) {
1732 msg = convertitem(current_arg, &format, p_va, flags,
1733 levels, msgbuf, sizeof(msgbuf), &freelist);
1734 if (msg) {
1735 seterror(i+1, msg, levels, fname, custom_msg);
1736 return cleanreturn(0, &freelist);
1737 }
1738 continue;
1739 }
1740
1741 if (i < min) {
1742 if (i < pos) {
1743 assert (min == INT_MAX);
1744 assert (max == INT_MAX);
1745 skip = 1;
1746 /* At that moment we still don't know the minimal and
1747 * the maximal numbers of positional arguments. Raising
1748 * an exception is deferred until we encounter | and $
1749 * or the end of the format. */
1750 }
1751 else {
1752 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
1753 "argument '%s' (pos %d)",
1754 (fname == NULL) ? "function" : fname,
1755 (fname == NULL) ? "" : "()",
1756 kwlist[i], i+1);
1757 return cleanreturn(0, &freelist);
1758 }
1759 }
1760 /* current code reports success when all required args
1761 * fulfilled and no keyword args left, with no further
1762 * validation. XXX Maybe skip this in debug build ?
1763 */
1764 if (!nkwargs && !skip) {
1765 return cleanreturn(1, &freelist);
1766 }
1767 }
1768
1769 /* We are into optional args, skip through to any remaining
1770 * keyword args */
1771 msg = skipitem(&format, p_va, flags);
1772 if (msg) {
1773 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1774 format);
1775 return cleanreturn(0, &freelist);
1776 }
1777 }
1778
1779 if (skip) {
1780 PyErr_Format(PyExc_TypeError,
1781 "%.200s%s takes %s %d positional argument%s"
1782 " (%zd given)",
1783 (fname == NULL) ? "function" : fname,
1784 (fname == NULL) ? "" : "()",
1785 (Py_MIN(pos, min) < i) ? "at least" : "exactly",
1786 Py_MIN(pos, min),
1787 Py_MIN(pos, min) == 1 ? "" : "s",
1788 nargs);
1789 return cleanreturn(0, &freelist);
1790 }
1791
1792 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1793 PyErr_Format(PyExc_SystemError,
1794 "more argument specifiers than keyword list entries "
1795 "(remaining format:'%s')", format);
1796 return cleanreturn(0, &freelist);
1797 }
1798
1799 if (nkwargs > 0) {
1800 PyObject *key;
1801 Py_ssize_t j;
1802 /* make sure there are no arguments given by name and position */
1803 for (i = pos; i < nargs; i++) {
1804 current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
1805 if (current_arg) {
1806 /* arg present in tuple and in dict */
1807 PyErr_Format(PyExc_TypeError,
1808 "argument for %.200s%s given by name ('%s') "
1809 "and position (%d)",
1810 (fname == NULL) ? "function" : fname,
1811 (fname == NULL) ? "" : "()",
1812 kwlist[i], i+1);
1813 return cleanreturn(0, &freelist);
1814 }
1815 else if (PyErr_Occurred()) {
1816 return cleanreturn(0, &freelist);
1817 }
1818 }
1819 /* make sure there are no extraneous keyword arguments */
1820 j = 0;
1821 while (PyDict_Next(kwargs, &j, &key, NULL)) {
1822 int match = 0;
1823 if (!PyUnicode_Check(key)) {
1824 PyErr_SetString(PyExc_TypeError,
1825 "keywords must be strings");
1826 return cleanreturn(0, &freelist);
1827 }
1828 for (i = pos; i < len; i++) {
1829 if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) {
1830 match = 1;
1831 break;
1832 }
1833 }
1834 if (!match) {
1835 PyErr_Format(PyExc_TypeError,
1836 "'%U' is an invalid keyword "
1837 "argument for %.200s%s",
1838 key,
1839 (fname == NULL) ? "this function" : fname,
1840 (fname == NULL) ? "" : "()");
1841 return cleanreturn(0, &freelist);
1842 }
1843 }
1844 }
1845
1846 return cleanreturn(1, &freelist);
1847}
1848
1849
1850/* List of static parsers. */
1851static struct _PyArg_Parser *static_arg_parsers = NULL;
1852
1853static int
1854parser_init(struct _PyArg_Parser *parser)
1855{
1856 const char * const *keywords;
1857 const char *format, *msg;
1858 int i, len, min, max, nkw;
1859 PyObject *kwtuple;
1860
1861 assert(parser->keywords != NULL);
1862 if (parser->kwtuple != NULL) {
1863 return 1;
1864 }
1865
1866 keywords = parser->keywords;
1867 /* scan keywords and count the number of positional-only parameters */
1868 for (i = 0; keywords[i] && !*keywords[i]; i++) {
1869 }
1870 parser->pos = i;
1871 /* scan keywords and get greatest possible nbr of args */
1872 for (; keywords[i]; i++) {
1873 if (!*keywords[i]) {
1874 PyErr_SetString(PyExc_SystemError,
1875 "Empty keyword parameter name");
1876 return 0;
1877 }
1878 }
1879 len = i;
1880
1881 format = parser->format;
1882 if (format) {
1883 /* grab the function name or custom error msg first (mutually exclusive) */
1884 parser->fname = strchr(parser->format, ':');
1885 if (parser->fname) {
1886 parser->fname++;
1887 parser->custom_msg = NULL;
1888 }
1889 else {
1890 parser->custom_msg = strchr(parser->format,';');
1891 if (parser->custom_msg)
1892 parser->custom_msg++;
1893 }
1894
1895 min = max = INT_MAX;
1896 for (i = 0; i < len; i++) {
1897 if (*format == '|') {
1898 if (min != INT_MAX) {
1899 PyErr_SetString(PyExc_SystemError,
1900 "Invalid format string (| specified twice)");
1901 return 0;
1902 }
1903 if (max != INT_MAX) {
1904 PyErr_SetString(PyExc_SystemError,
1905 "Invalid format string ($ before |)");
1906 return 0;
1907 }
1908 min = i;
1909 format++;
1910 }
1911 if (*format == '$') {
1912 if (max != INT_MAX) {
1913 PyErr_SetString(PyExc_SystemError,
1914 "Invalid format string ($ specified twice)");
1915 return 0;
1916 }
1917 if (i < parser->pos) {
1918 PyErr_SetString(PyExc_SystemError,
1919 "Empty parameter name after $");
1920 return 0;
1921 }
1922 max = i;
1923 format++;
1924 }
1925 if (IS_END_OF_FORMAT(*format)) {
1926 PyErr_Format(PyExc_SystemError,
1927 "More keyword list entries (%d) than "
1928 "format specifiers (%d)", len, i);
1929 return 0;
1930 }
1931
1932 msg = skipitem(&format, NULL, 0);
1933 if (msg) {
1934 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1935 format);
1936 return 0;
1937 }
1938 }
1939 parser->min = Py_MIN(min, len);
1940 parser->max = Py_MIN(max, len);
1941
1942 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1943 PyErr_Format(PyExc_SystemError,
1944 "more argument specifiers than keyword list entries "
1945 "(remaining format:'%s')", format);
1946 return 0;
1947 }
1948 }
1949
1950 nkw = len - parser->pos;
1951 kwtuple = PyTuple_New(nkw);
1952 if (kwtuple == NULL) {
1953 return 0;
1954 }
1955 keywords = parser->keywords + parser->pos;
1956 for (i = 0; i < nkw; i++) {
1957 PyObject *str = PyUnicode_FromString(keywords[i]);
1958 if (str == NULL) {
1959 Py_DECREF(kwtuple);
1960 return 0;
1961 }
1962 PyUnicode_InternInPlace(&str);
1963 PyTuple_SET_ITEM(kwtuple, i, str);
1964 }
1965 parser->kwtuple = kwtuple;
1966
1967 assert(parser->next == NULL);
1968 parser->next = static_arg_parsers;
1969 static_arg_parsers = parser;
1970 return 1;
1971}
1972
1973static void
1974parser_clear(struct _PyArg_Parser *parser)
1975{
1976 Py_CLEAR(parser->kwtuple);
1977}
1978
1979static PyObject*
1980find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
1981{
1982 Py_ssize_t i, nkwargs;
1983
1984 nkwargs = PyTuple_GET_SIZE(kwnames);
1985 for (i = 0; i < nkwargs; i++) {
1986 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
1987
1988 /* kwname == key will normally find a match in since keyword keys
1989 should be interned strings; if not retry below in a new loop. */
1990 if (kwname == key) {
1991 return kwstack[i];
1992 }
1993 }
1994
1995 for (i = 0; i < nkwargs; i++) {
1996 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
1997 assert(PyUnicode_Check(kwname));
1998 if (_PyUnicode_EQ(kwname, key)) {
1999 return kwstack[i];
2000 }
2001 }
2002 return NULL;
2003}
2004
2005static int
2006vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
2007 PyObject *kwargs, PyObject *kwnames,
2008 struct _PyArg_Parser *parser,
2009 va_list *p_va, int flags)
2010{
2011 PyObject *kwtuple;
2012 char msgbuf[512];
2013 int levels[32];
2014 const char *format;
2015 const char *msg;
2016 PyObject *keyword;
2017 int i, pos, len;
2018 Py_ssize_t nkwargs;
2019 PyObject *current_arg;
2020 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
2021 freelist_t freelist;
2022 PyObject *const *kwstack = NULL;
2023
2024 freelist.entries = static_entries;
2025 freelist.first_available = 0;
2026 freelist.entries_malloced = 0;
2027
2028 assert(kwargs == NULL || PyDict_Check(kwargs));
2029 assert(kwargs == NULL || kwnames == NULL);
2030 assert(p_va != NULL);
2031
2032 if (parser == NULL) {
2033 PyErr_BadInternalCall();
2034 return 0;
2035 }
2036
2037 if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2038 PyErr_BadInternalCall();
2039 return 0;
2040 }
2041
2042 if (!parser_init(parser)) {
2043 return 0;
2044 }
2045
2046 kwtuple = parser->kwtuple;
2047 pos = parser->pos;
2048 len = pos + (int)PyTuple_GET_SIZE(kwtuple);
2049
2050 if (len > STATIC_FREELIST_ENTRIES) {
2051 freelist.entries = PyMem_NEW(freelistentry_t, len);
2052 if (freelist.entries == NULL) {
2053 PyErr_NoMemory();
2054 return 0;
2055 }
2056 freelist.entries_malloced = 1;
2057 }
2058
2059 if (kwargs != NULL) {
2060 nkwargs = PyDict_GET_SIZE(kwargs);
2061 }
2062 else if (kwnames != NULL) {
2063 nkwargs = PyTuple_GET_SIZE(kwnames);
2064 kwstack = args + nargs;
2065 }
2066 else {
2067 nkwargs = 0;
2068 }
2069 if (nargs + nkwargs > len) {
2070 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2071 messages in some special cases (see bpo-31229). */
2072 PyErr_Format(PyExc_TypeError,
2073 "%.200s%s takes at most %d %sargument%s (%zd given)",
2074 (parser->fname == NULL) ? "function" : parser->fname,
2075 (parser->fname == NULL) ? "" : "()",
2076 len,
2077 (nargs == 0) ? "keyword " : "",
2078 (len == 1) ? "" : "s",
2079 nargs + nkwargs);
2080 return cleanreturn(0, &freelist);
2081 }
2082 if (parser->max < nargs) {
2083 if (parser->max == 0) {
2084 PyErr_Format(PyExc_TypeError,
2085 "%.200s%s takes no positional arguments",
2086 (parser->fname == NULL) ? "function" : parser->fname,
2087 (parser->fname == NULL) ? "" : "()");
2088 }
2089 else {
2090 PyErr_Format(PyExc_TypeError,
2091 "%.200s%s takes %s %d positional argument%s (%zd given)",
2092 (parser->fname == NULL) ? "function" : parser->fname,
2093 (parser->fname == NULL) ? "" : "()",
2094 (parser->min < parser->max) ? "at most" : "exactly",
2095 parser->max,
2096 parser->max == 1 ? "" : "s",
2097 nargs);
2098 }
2099 return cleanreturn(0, &freelist);
2100 }
2101
2102 format = parser->format;
2103 /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
2104 for (i = 0; i < len; i++) {
2105 if (*format == '|') {
2106 format++;
2107 }
2108 if (*format == '$') {
2109 format++;
2110 }
2111 assert(!IS_END_OF_FORMAT(*format));
2112
2113 if (i < nargs) {
2114 current_arg = args[i];
2115 }
2116 else if (nkwargs && i >= pos) {
2117 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2118 if (kwargs != NULL) {
2119 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2120 if (!current_arg && PyErr_Occurred()) {
2121 return cleanreturn(0, &freelist);
2122 }
2123 }
2124 else {
2125 current_arg = find_keyword(kwnames, kwstack, keyword);
2126 }
2127 if (current_arg) {
2128 --nkwargs;
2129 }
2130 }
2131 else {
2132 current_arg = NULL;
2133 }
2134
2135 if (current_arg) {
2136 msg = convertitem(current_arg, &format, p_va, flags,
2137 levels, msgbuf, sizeof(msgbuf), &freelist);
2138 if (msg) {
2139 seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2140 return cleanreturn(0, &freelist);
2141 }
2142 continue;
2143 }
2144
2145 if (i < parser->min) {
2146 /* Less arguments than required */
2147 if (i < pos) {
2148 Py_ssize_t min = Py_MIN(pos, parser->min);
2149 PyErr_Format(PyExc_TypeError,
2150 "%.200s%s takes %s %d positional argument%s"
2151 " (%zd given)",
2152 (parser->fname == NULL) ? "function" : parser->fname,
2153 (parser->fname == NULL) ? "" : "()",
2154 min < parser->max ? "at least" : "exactly",
2155 min,
2156 min == 1 ? "" : "s",
2157 nargs);
2158 }
2159 else {
2160 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2161 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
2162 "argument '%U' (pos %d)",
2163 (parser->fname == NULL) ? "function" : parser->fname,
2164 (parser->fname == NULL) ? "" : "()",
2165 keyword, i+1);
2166 }
2167 return cleanreturn(0, &freelist);
2168 }
2169 /* current code reports success when all required args
2170 * fulfilled and no keyword args left, with no further
2171 * validation. XXX Maybe skip this in debug build ?
2172 */
2173 if (!nkwargs) {
2174 return cleanreturn(1, &freelist);
2175 }
2176
2177 /* We are into optional args, skip through to any remaining
2178 * keyword args */
2179 msg = skipitem(&format, p_va, flags);
2180 assert(msg == NULL);
2181 }
2182
2183 assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2184
2185 if (nkwargs > 0) {
2186 Py_ssize_t j;
2187 /* make sure there are no arguments given by name and position */
2188 for (i = pos; i < nargs; i++) {
2189 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2190 if (kwargs != NULL) {
2191 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2192 if (!current_arg && PyErr_Occurred()) {
2193 return cleanreturn(0, &freelist);
2194 }
2195 }
2196 else {
2197 current_arg = find_keyword(kwnames, kwstack, keyword);
2198 }
2199 if (current_arg) {
2200 /* arg present in tuple and in dict */
2201 PyErr_Format(PyExc_TypeError,
2202 "argument for %.200s%s given by name ('%U') "
2203 "and position (%d)",
2204 (parser->fname == NULL) ? "function" : parser->fname,
2205 (parser->fname == NULL) ? "" : "()",
2206 keyword, i+1);
2207 return cleanreturn(0, &freelist);
2208 }
2209 }
2210 /* make sure there are no extraneous keyword arguments */
2211 j = 0;
2212 while (1) {
2213 int match;
2214 if (kwargs != NULL) {
2215 if (!PyDict_Next(kwargs, &j, &keyword, NULL))
2216 break;
2217 }
2218 else {
2219 if (j >= PyTuple_GET_SIZE(kwnames))
2220 break;
2221 keyword = PyTuple_GET_ITEM(kwnames, j);
2222 j++;
2223 }
2224
2225 match = PySequence_Contains(kwtuple, keyword);
2226 if (match <= 0) {
2227 if (!match) {
2228 PyErr_Format(PyExc_TypeError,
2229 "'%S' is an invalid keyword "
2230 "argument for %.200s%s",
2231 keyword,
2232 (parser->fname == NULL) ? "this function" : parser->fname,
2233 (parser->fname == NULL) ? "" : "()");
2234 }
2235 return cleanreturn(0, &freelist);
2236 }
2237 }
2238 }
2239
2240 return cleanreturn(1, &freelist);
2241}
2242
2243static int
2244vgetargskeywordsfast(PyObject *args, PyObject *keywords,
2245 struct _PyArg_Parser *parser, va_list *p_va, int flags)
2246{
2247 PyObject **stack;
2248 Py_ssize_t nargs;
2249
2250 if (args == NULL
2251 || !PyTuple_Check(args)
2252 || (keywords != NULL && !PyDict_Check(keywords)))
2253 {
2254 PyErr_BadInternalCall();
2255 return 0;
2256 }
2257
2258 stack = _PyTuple_ITEMS(args);
2259 nargs = PyTuple_GET_SIZE(args);
2260 return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
2261 parser, p_va, flags);
2262}
2263
2264
2265#undef _PyArg_UnpackKeywords
2266
2267PyObject * const *
2268_PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
2269 PyObject *kwargs, PyObject *kwnames,
2270 struct _PyArg_Parser *parser,
2271 int minpos, int maxpos, int minkw,
2272 PyObject **buf)
2273{
2274 PyObject *kwtuple;
2275 PyObject *keyword;
2276 int i, posonly, minposonly, maxargs;
2277 int reqlimit = minkw ? maxpos + minkw : minpos;
2278 Py_ssize_t nkwargs;
2279 PyObject *current_arg;
2280 PyObject * const *kwstack = NULL;
2281
2282 assert(kwargs == NULL || PyDict_Check(kwargs));
2283 assert(kwargs == NULL || kwnames == NULL);
2284
2285 if (parser == NULL) {
2286 PyErr_BadInternalCall();
2287 return NULL;
2288 }
2289
2290 if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2291 PyErr_BadInternalCall();
2292 return NULL;
2293 }
2294
2295 if (args == NULL && nargs == 0) {
2296 args = buf;
2297 }
2298
2299 if (!parser_init(parser)) {
2300 return NULL;
2301 }
2302
2303 kwtuple = parser->kwtuple;
2304 posonly = parser->pos;
2305 minposonly = Py_MIN(posonly, minpos);
2306 maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple);
2307
2308 if (kwargs != NULL) {
2309 nkwargs = PyDict_GET_SIZE(kwargs);
2310 }
2311 else if (kwnames != NULL) {
2312 nkwargs = PyTuple_GET_SIZE(kwnames);
2313 kwstack = args + nargs;
2314 }
2315 else {
2316 nkwargs = 0;
2317 }
2318 if (nkwargs == 0 && minkw == 0 && minpos <= nargs && nargs <= maxpos) {
2319 /* Fast path. */
2320 return args;
2321 }
2322 if (nargs + nkwargs > maxargs) {
2323 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2324 messages in some special cases (see bpo-31229). */
2325 PyErr_Format(PyExc_TypeError,
2326 "%.200s%s takes at most %d %sargument%s (%zd given)",
2327 (parser->fname == NULL) ? "function" : parser->fname,
2328 (parser->fname == NULL) ? "" : "()",
2329 maxargs,
2330 (nargs == 0) ? "keyword " : "",
2331 (maxargs == 1) ? "" : "s",
2332 nargs + nkwargs);
2333 return NULL;
2334 }
2335 if (nargs > maxpos) {
2336 if (maxpos == 0) {
2337 PyErr_Format(PyExc_TypeError,
2338 "%.200s%s takes no positional arguments",
2339 (parser->fname == NULL) ? "function" : parser->fname,
2340 (parser->fname == NULL) ? "" : "()");
2341 }
2342 else {
2343 PyErr_Format(PyExc_TypeError,
2344 "%.200s%s takes %s %d positional argument%s (%zd given)",
2345 (parser->fname == NULL) ? "function" : parser->fname,
2346 (parser->fname == NULL) ? "" : "()",
2347 (minpos < maxpos) ? "at most" : "exactly",
2348 maxpos,
2349 (maxpos == 1) ? "" : "s",
2350 nargs);
2351 }
2352 return NULL;
2353 }
2354 if (nargs < minposonly) {
2355 PyErr_Format(PyExc_TypeError,
2356 "%.200s%s takes %s %d positional argument%s"
2357 " (%zd given)",
2358 (parser->fname == NULL) ? "function" : parser->fname,
2359 (parser->fname == NULL) ? "" : "()",
2360 minposonly < maxpos ? "at least" : "exactly",
2361 minposonly,
2362 minposonly == 1 ? "" : "s",
2363 nargs);
2364 return NULL;
2365 }
2366
2367 /* copy tuple args */
2368 for (i = 0; i < nargs; i++) {
2369 buf[i] = args[i];
2370 }
2371
2372 /* copy keyword args using kwtuple to drive process */
2373 for (i = Py_MAX((int)nargs, posonly); i < maxargs; i++) {
2374 if (nkwargs) {
2375 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2376 if (kwargs != NULL) {
2377 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2378 if (!current_arg && PyErr_Occurred()) {
2379 return NULL;
2380 }
2381 }
2382 else {
2383 current_arg = find_keyword(kwnames, kwstack, keyword);
2384 }
2385 }
2386 else if (i >= reqlimit) {
2387 break;
2388 }
2389 else {
2390 current_arg = NULL;
2391 }
2392
2393 buf[i] = current_arg;
2394
2395 if (current_arg) {
2396 --nkwargs;
2397 }
2398 else if (i < minpos || (maxpos <= i && i < reqlimit)) {
2399 /* Less arguments than required */
2400 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2401 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
2402 "argument '%U' (pos %d)",
2403 (parser->fname == NULL) ? "function" : parser->fname,
2404 (parser->fname == NULL) ? "" : "()",
2405 keyword, i+1);
2406 return NULL;
2407 }
2408 }
2409
2410 if (nkwargs > 0) {
2411 Py_ssize_t j;
2412 /* make sure there are no arguments given by name and position */
2413 for (i = posonly; i < nargs; i++) {
2414 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2415 if (kwargs != NULL) {
2416 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2417 if (!current_arg && PyErr_Occurred()) {
2418 return NULL;
2419 }
2420 }
2421 else {
2422 current_arg = find_keyword(kwnames, kwstack, keyword);
2423 }
2424 if (current_arg) {
2425 /* arg present in tuple and in dict */
2426 PyErr_Format(PyExc_TypeError,
2427 "argument for %.200s%s given by name ('%U') "
2428 "and position (%d)",
2429 (parser->fname == NULL) ? "function" : parser->fname,
2430 (parser->fname == NULL) ? "" : "()",
2431 keyword, i+1);
2432 return NULL;
2433 }
2434 }
2435 /* make sure there are no extraneous keyword arguments */
2436 j = 0;
2437 while (1) {
2438 int match;
2439 if (kwargs != NULL) {
2440 if (!PyDict_Next(kwargs, &j, &keyword, NULL))
2441 break;
2442 }
2443 else {
2444 if (j >= PyTuple_GET_SIZE(kwnames))
2445 break;
2446 keyword = PyTuple_GET_ITEM(kwnames, j);
2447 j++;
2448 }
2449
2450 match = PySequence_Contains(kwtuple, keyword);
2451 if (match <= 0) {
2452 if (!match) {
2453 PyErr_Format(PyExc_TypeError,
2454 "'%S' is an invalid keyword "
2455 "argument for %.200s%s",
2456 keyword,
2457 (parser->fname == NULL) ? "this function" : parser->fname,
2458 (parser->fname == NULL) ? "" : "()");
2459 }
2460 return NULL;
2461 }
2462 }
2463 }
2464
2465 return buf;
2466}
2467
2468
2469static const char *
2470skipitem(const char **p_format, va_list *p_va, int flags)
2471{
2472 const char *format = *p_format;
2473 char c = *format++;
2474
2475 switch (c) {
2476
2477 /*
2478 * codes that take a single data pointer as an argument
2479 * (the type of the pointer is irrelevant)
2480 */
2481
2482 case 'b': /* byte -- very short int */
2483 case 'B': /* byte as bitfield */
2484 case 'h': /* short int */
2485 case 'H': /* short int as bitfield */
2486 case 'i': /* int */
2487 case 'I': /* int sized bitfield */
2488 case 'l': /* long int */
2489 case 'k': /* long int sized bitfield */
2490 case 'L': /* long long */
2491 case 'K': /* long long sized bitfield */
2492 case 'n': /* Py_ssize_t */
2493 case 'f': /* float */
2494 case 'd': /* double */
2495 case 'D': /* complex double */
2496 case 'c': /* char */
2497 case 'C': /* unicode char */
2498 case 'p': /* boolean predicate */
2499 case 'S': /* string object */
2500 case 'Y': /* string object */
2501 case 'U': /* unicode string object */
2502 {
2503 if (p_va != NULL) {
2504 (void) va_arg(*p_va, void *);
2505 }
2506 break;
2507 }
2508
2509 /* string codes */
2510
2511 case 'e': /* string with encoding */
2512 {
2513 if (p_va != NULL) {
2514 (void) va_arg(*p_va, const char *);
2515 }
2516 if (!(*format == 's' || *format == 't'))
2517 /* after 'e', only 's' and 't' is allowed */
2518 goto err;
2519 format++;
2520 }
2521 /* fall through */
2522
2523 case 's': /* string */
2524 case 'z': /* string or None */
2525 case 'y': /* bytes */
2526 case 'u': /* unicode string */
2527 case 'Z': /* unicode string or None */
2528 case 'w': /* buffer, read-write */
2529 {
2530 if (p_va != NULL) {
2531 (void) va_arg(*p_va, char **);
2532 }
2533 if (*format == '#') {
2534 if (p_va != NULL) {
2535 if (!(flags & FLAG_SIZE_T)) {
2536 PyErr_SetString(PyExc_SystemError,
2537 "PY_SSIZE_T_CLEAN macro must be defined for '#' formats");
2538 return NULL;
2539 }
2540 (void) va_arg(*p_va, Py_ssize_t *);
2541 }
2542 format++;
2543 } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w')
2544 && *format == '*')
2545 {
2546 format++;
2547 }
2548 break;
2549 }
2550
2551 case 'O': /* object */
2552 {
2553 if (*format == '!') {
2554 format++;
2555 if (p_va != NULL) {
2556 (void) va_arg(*p_va, PyTypeObject*);
2557 (void) va_arg(*p_va, PyObject **);
2558 }
2559 }
2560 else if (*format == '&') {
2561 typedef int (*converter)(PyObject *, void *);
2562 if (p_va != NULL) {
2563 (void) va_arg(*p_va, converter);
2564 (void) va_arg(*p_va, void *);
2565 }
2566 format++;
2567 }
2568 else {
2569 if (p_va != NULL) {
2570 (void) va_arg(*p_va, PyObject **);
2571 }
2572 }
2573 break;
2574 }
2575
2576 case '(': /* bypass tuple, not handled at all previously */
2577 {
2578 const char *msg;
2579 for (;;) {
2580 if (*format==')')
2581 break;
2582 if (IS_END_OF_FORMAT(*format))
2583 return "Unmatched left paren in format "
2584 "string";
2585 msg = skipitem(&format, p_va, flags);
2586 if (msg)
2587 return msg;
2588 }
2589 format++;
2590 break;
2591 }
2592
2593 case ')':
2594 return "Unmatched right paren in format string";
2595
2596 default:
2597err:
2598 return "impossible<bad format char>";
2599
2600 }
2601
2602 *p_format = format;
2603 return NULL;
2604}
2605
2606
2607#undef _PyArg_CheckPositional
2608
2609int
2610_PyArg_CheckPositional(const char *name, Py_ssize_t nargs,
2611 Py_ssize_t min, Py_ssize_t max)
2612{
2613 assert(min >= 0);
2614 assert(min <= max);
2615
2616 if (nargs < min) {
2617 if (name != NULL)
2618 PyErr_Format(
2619 PyExc_TypeError,
2620 "%.200s expected %s%zd argument%s, got %zd",
2621 name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
2622 else
2623 PyErr_Format(
2624 PyExc_TypeError,
2625 "unpacked tuple should have %s%zd element%s,"
2626 " but has %zd",
2627 (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
2628 return 0;
2629 }
2630
2631 if (nargs == 0) {
2632 return 1;
2633 }
2634
2635 if (nargs > max) {
2636 if (name != NULL)
2637 PyErr_Format(
2638 PyExc_TypeError,
2639 "%.200s expected %s%zd argument%s, got %zd",
2640 name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
2641 else
2642 PyErr_Format(
2643 PyExc_TypeError,
2644 "unpacked tuple should have %s%zd element%s,"
2645 " but has %zd",
2646 (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
2647 return 0;
2648 }
2649
2650 return 1;
2651}
2652
2653static int
2654unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name,
2655 Py_ssize_t min, Py_ssize_t max, va_list vargs)
2656{
2657 Py_ssize_t i;
2658 PyObject **o;
2659
2660 if (!_PyArg_CheckPositional(name, nargs, min, max)) {
2661 return 0;
2662 }
2663
2664 for (i = 0; i < nargs; i++) {
2665 o = va_arg(vargs, PyObject **);
2666 *o = args[i];
2667 }
2668 return 1;
2669}
2670
2671int
2672PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
2673{
2674 PyObject **stack;
2675 Py_ssize_t nargs;
2676 int retval;
2677 va_list vargs;
2678
2679 if (!PyTuple_Check(args)) {
2680 PyErr_SetString(PyExc_SystemError,
2681 "PyArg_UnpackTuple() argument list is not a tuple");
2682 return 0;
2683 }
2684 stack = _PyTuple_ITEMS(args);
2685 nargs = PyTuple_GET_SIZE(args);
2686
2687#ifdef HAVE_STDARG_PROTOTYPES
2688 va_start(vargs, max);
2689#else
2690 va_start(vargs);
2691#endif
2692 retval = unpack_stack(stack, nargs, name, min, max, vargs);
2693 va_end(vargs);
2694 return retval;
2695}
2696
2697int
2698_PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
2699 Py_ssize_t min, Py_ssize_t max, ...)
2700{
2701 int retval;
2702 va_list vargs;
2703
2704#ifdef HAVE_STDARG_PROTOTYPES
2705 va_start(vargs, max);
2706#else
2707 va_start(vargs);
2708#endif
2709 retval = unpack_stack(args, nargs, name, min, max, vargs);
2710 va_end(vargs);
2711 return retval;
2712}
2713
2714
2715#undef _PyArg_NoKeywords
2716#undef _PyArg_NoKwnames
2717#undef _PyArg_NoPositional
2718
2719/* For type constructors that don't take keyword args
2720 *
2721 * Sets a TypeError and returns 0 if the args/kwargs is
2722 * not empty, returns 1 otherwise
2723 */
2724int
2725_PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
2726{
2727 if (kwargs == NULL) {
2728 return 1;
2729 }
2730 if (!PyDict_CheckExact(kwargs)) {
2731 PyErr_BadInternalCall();
2732 return 0;
2733 }
2734 if (PyDict_GET_SIZE(kwargs) == 0) {
2735 return 1;
2736 }
2737
2738 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
2739 funcname);
2740 return 0;
2741}
2742
2743int
2744_PyArg_NoPositional(const char *funcname, PyObject *args)
2745{
2746 if (args == NULL)
2747 return 1;
2748 if (!PyTuple_CheckExact(args)) {
2749 PyErr_BadInternalCall();
2750 return 0;
2751 }
2752 if (PyTuple_GET_SIZE(args) == 0)
2753 return 1;
2754
2755 PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
2756 funcname);
2757 return 0;
2758}
2759
2760int
2761_PyArg_NoKwnames(const char *funcname, PyObject *kwnames)
2762{
2763 if (kwnames == NULL) {
2764 return 1;
2765 }
2766
2767 assert(PyTuple_CheckExact(kwnames));
2768
2769 if (PyTuple_GET_SIZE(kwnames) == 0) {
2770 return 1;
2771 }
2772
2773 PyErr_Format(PyExc_TypeError, "%s() takes no keyword arguments", funcname);
2774 return 0;
2775}
2776
2777void
2778_PyArg_Fini(void)
2779{
2780 struct _PyArg_Parser *tmp, *s = static_arg_parsers;
2781 while (s) {
2782 tmp = s->next;
2783 s->next = NULL;
2784 parser_clear(s);
2785 s = tmp;
2786 }
2787 static_arg_parsers = NULL;
2788}
2789
2790#ifdef __cplusplus
2791};
2792#endif
2793