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