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