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 (%ld 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 Py_SAFE_DOWNCAST(nargs, Py_ssize_t, long));
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 STORE_SIZE(size);
1213 } else {
1214 /* Using a 0-terminated buffer:
1215
1216 - the encoded string has to be 0-terminated
1217 for this variant to work; if it is not, an
1218 error raised
1219
1220 - a new buffer of the needed size is
1221 allocated and the data copied into it;
1222 *buffer is updated to point to the new
1223 buffer; the caller is responsible for
1224 PyMem_Free()ing it after usage
1225
1226 */
1227 if ((Py_ssize_t)strlen(ptr) != size) {
1228 Py_DECREF(s);
1229 return converterr(
1230 "encoded string without null bytes",
1231 arg, msgbuf, bufsize);
1232 }
1233 *buffer = PyMem_NEW(char, size + 1);
1234 if (*buffer == NULL) {
1235 Py_DECREF(s);
1236 PyErr_NoMemory();
1237 RETURN_ERR_OCCURRED;
1238 }
1239 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1240 Py_DECREF(s);
1241 return converterr("(cleanup problem)",
1242 arg, msgbuf, bufsize);
1243 }
1244 memcpy(*buffer, ptr, size+1);
1245 }
1246 Py_DECREF(s);
1247 break;
1248 }
1249
1250 case 'S': { /* PyBytes object */
1251 PyObject **p = va_arg(*p_va, PyObject **);
1252 if (PyBytes_Check(arg))
1253 *p = arg;
1254 else
1255 return converterr("bytes", arg, msgbuf, bufsize);
1256 break;
1257 }
1258
1259 case 'Y': { /* PyByteArray object */
1260 PyObject **p = va_arg(*p_va, PyObject **);
1261 if (PyByteArray_Check(arg))
1262 *p = arg;
1263 else
1264 return converterr("bytearray", arg, msgbuf, bufsize);
1265 break;
1266 }
1267
1268 case 'U': { /* PyUnicode object */
1269 PyObject **p = va_arg(*p_va, PyObject **);
1270 if (PyUnicode_Check(arg)) {
1271 if (PyUnicode_READY(arg) == -1)
1272 RETURN_ERR_OCCURRED;
1273 *p = arg;
1274 }
1275 else
1276 return converterr("str", arg, msgbuf, bufsize);
1277 break;
1278 }
1279
1280 case 'O': { /* object */
1281 PyTypeObject *type;
1282 PyObject **p;
1283 if (*format == '!') {
1284 type = va_arg(*p_va, PyTypeObject*);
1285 p = va_arg(*p_va, PyObject **);
1286 format++;
1287 if (PyType_IsSubtype(arg->ob_type, type))
1288 *p = arg;
1289 else
1290 return converterr(type->tp_name, arg, msgbuf, bufsize);
1291
1292 }
1293 else if (*format == '&') {
1294 typedef int (*converter)(PyObject *, void *);
1295 converter convert = va_arg(*p_va, converter);
1296 void *addr = va_arg(*p_va, void *);
1297 int res;
1298 format++;
1299 if (! (res = (*convert)(arg, addr)))
1300 return converterr("(unspecified)",
1301 arg, msgbuf, bufsize);
1302 if (res == Py_CLEANUP_SUPPORTED &&
1303 addcleanup(addr, freelist, convert) == -1)
1304 return converterr("(cleanup problem)",
1305 arg, msgbuf, bufsize);
1306 }
1307 else {
1308 p = va_arg(*p_va, PyObject **);
1309 *p = arg;
1310 }
1311 break;
1312 }
1313
1314
1315 case 'w': { /* "w*": memory buffer, read-write access */
1316 void **p = va_arg(*p_va, void **);
1317
1318 if (*format != '*')
1319 return converterr(
1320 "(invalid use of 'w' format character)",
1321 arg, msgbuf, bufsize);
1322 format++;
1323
1324 /* Caller is interested in Py_buffer, and the object
1325 supports it directly. */
1326 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1327 PyErr_Clear();
1328 return converterr("read-write bytes-like object",
1329 arg, msgbuf, bufsize);
1330 }
1331 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1332 PyBuffer_Release((Py_buffer*)p);
1333 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1334 }
1335 if (addcleanup(p, freelist, cleanup_buffer)) {
1336 return converterr(
1337 "(cleanup problem)",
1338 arg, msgbuf, bufsize);
1339 }
1340 break;
1341 }
1342
1343 default:
1344 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
1345
1346 }
1347
1348 *p_format = format;
1349 return NULL;
1350
1351 #undef FETCH_SIZE
1352 #undef STORE_SIZE
1353 #undef BUFFER_LEN
1354 #undef RETURN_ERR_OCCURRED
1355 }
1356
1357 static Py_ssize_t
convertbuffer(PyObject * arg,const void ** p,const char ** errmsg)1358 convertbuffer(PyObject *arg, const void **p, const char **errmsg)
1359 {
1360 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
1361 Py_ssize_t count;
1362 Py_buffer view;
1363
1364 *errmsg = NULL;
1365 *p = NULL;
1366 if (pb != NULL && pb->bf_releasebuffer != NULL) {
1367 *errmsg = "read-only bytes-like object";
1368 return -1;
1369 }
1370
1371 if (getbuffer(arg, &view, errmsg) < 0)
1372 return -1;
1373 count = view.len;
1374 *p = view.buf;
1375 PyBuffer_Release(&view);
1376 return count;
1377 }
1378
1379 static int
getbuffer(PyObject * arg,Py_buffer * view,const char ** errmsg)1380 getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
1381 {
1382 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
1383 *errmsg = "bytes-like object";
1384 return -1;
1385 }
1386 if (!PyBuffer_IsContiguous(view, 'C')) {
1387 PyBuffer_Release(view);
1388 *errmsg = "contiguous buffer";
1389 return -1;
1390 }
1391 return 0;
1392 }
1393
1394 /* Support for keyword arguments donated by
1395 Geoff Philbrick <philbric@delphi.hks.com> */
1396
1397 /* Return false (0) for error, else true. */
1398 int
PyArg_ParseTupleAndKeywords(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,...)1399 PyArg_ParseTupleAndKeywords(PyObject *args,
1400 PyObject *keywords,
1401 const char *format,
1402 char **kwlist, ...)
1403 {
1404 int retval;
1405 va_list va;
1406
1407 if ((args == NULL || !PyTuple_Check(args)) ||
1408 (keywords != NULL && !PyDict_Check(keywords)) ||
1409 format == NULL ||
1410 kwlist == NULL)
1411 {
1412 PyErr_BadInternalCall();
1413 return 0;
1414 }
1415
1416 va_start(va, kwlist);
1417 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1418 va_end(va);
1419 return retval;
1420 }
1421
1422 int
_PyArg_ParseTupleAndKeywords_SizeT(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,...)1423 _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1424 PyObject *keywords,
1425 const char *format,
1426 char **kwlist, ...)
1427 {
1428 int retval;
1429 va_list va;
1430
1431 if ((args == NULL || !PyTuple_Check(args)) ||
1432 (keywords != NULL && !PyDict_Check(keywords)) ||
1433 format == NULL ||
1434 kwlist == NULL)
1435 {
1436 PyErr_BadInternalCall();
1437 return 0;
1438 }
1439
1440 va_start(va, kwlist);
1441 retval = vgetargskeywords(args, keywords, format,
1442 kwlist, &va, FLAG_SIZE_T);
1443 va_end(va);
1444 return retval;
1445 }
1446
1447
1448 int
PyArg_VaParseTupleAndKeywords(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,va_list va)1449 PyArg_VaParseTupleAndKeywords(PyObject *args,
1450 PyObject *keywords,
1451 const char *format,
1452 char **kwlist, va_list va)
1453 {
1454 int retval;
1455 va_list lva;
1456
1457 if ((args == NULL || !PyTuple_Check(args)) ||
1458 (keywords != NULL && !PyDict_Check(keywords)) ||
1459 format == NULL ||
1460 kwlist == NULL)
1461 {
1462 PyErr_BadInternalCall();
1463 return 0;
1464 }
1465
1466 va_copy(lva, va);
1467
1468 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1469 va_end(lva);
1470 return retval;
1471 }
1472
1473 int
_PyArg_VaParseTupleAndKeywords_SizeT(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,va_list va)1474 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1475 PyObject *keywords,
1476 const char *format,
1477 char **kwlist, va_list va)
1478 {
1479 int retval;
1480 va_list lva;
1481
1482 if ((args == NULL || !PyTuple_Check(args)) ||
1483 (keywords != NULL && !PyDict_Check(keywords)) ||
1484 format == NULL ||
1485 kwlist == NULL)
1486 {
1487 PyErr_BadInternalCall();
1488 return 0;
1489 }
1490
1491 va_copy(lva, va);
1492
1493 retval = vgetargskeywords(args, keywords, format,
1494 kwlist, &lva, FLAG_SIZE_T);
1495 va_end(lva);
1496 return retval;
1497 }
1498
1499 int
_PyArg_ParseTupleAndKeywordsFast(PyObject * args,PyObject * keywords,struct _PyArg_Parser * parser,...)1500 _PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1501 struct _PyArg_Parser *parser, ...)
1502 {
1503 int retval;
1504 va_list va;
1505
1506 va_start(va, parser);
1507 retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1508 va_end(va);
1509 return retval;
1510 }
1511
1512 int
_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject * args,PyObject * keywords,struct _PyArg_Parser * parser,...)1513 _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1514 struct _PyArg_Parser *parser, ...)
1515 {
1516 int retval;
1517 va_list va;
1518
1519 va_start(va, parser);
1520 retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
1521 va_end(va);
1522 return retval;
1523 }
1524
1525 int
_PyArg_ParseStackAndKeywords(PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames,struct _PyArg_Parser * parser,...)1526 _PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
1527 struct _PyArg_Parser *parser, ...)
1528 {
1529 int retval;
1530 va_list va;
1531
1532 va_start(va, parser);
1533 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1534 va_end(va);
1535 return retval;
1536 }
1537
1538 int
_PyArg_ParseStackAndKeywords_SizeT(PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames,struct _PyArg_Parser * parser,...)1539 _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
1540 struct _PyArg_Parser *parser, ...)
1541 {
1542 int retval;
1543 va_list va;
1544
1545 va_start(va, parser);
1546 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T);
1547 va_end(va);
1548 return retval;
1549 }
1550
1551
1552 int
_PyArg_VaParseTupleAndKeywordsFast(PyObject * args,PyObject * keywords,struct _PyArg_Parser * parser,va_list va)1553 _PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1554 struct _PyArg_Parser *parser, va_list va)
1555 {
1556 int retval;
1557 va_list lva;
1558
1559 va_copy(lva, va);
1560
1561 retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
1562 va_end(lva);
1563 return retval;
1564 }
1565
1566 int
_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject * args,PyObject * keywords,struct _PyArg_Parser * parser,va_list va)1567 _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1568 struct _PyArg_Parser *parser, va_list va)
1569 {
1570 int retval;
1571 va_list lva;
1572
1573 va_copy(lva, va);
1574
1575 retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
1576 va_end(lva);
1577 return retval;
1578 }
1579
1580 int
PyArg_ValidateKeywordArguments(PyObject * kwargs)1581 PyArg_ValidateKeywordArguments(PyObject *kwargs)
1582 {
1583 if (!PyDict_Check(kwargs)) {
1584 PyErr_BadInternalCall();
1585 return 0;
1586 }
1587 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1588 PyErr_SetString(PyExc_TypeError,
1589 "keywords must be strings");
1590 return 0;
1591 }
1592 return 1;
1593 }
1594
1595 #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1596
1597 static int
vgetargskeywords(PyObject * args,PyObject * kwargs,const char * format,char ** kwlist,va_list * p_va,int flags)1598 vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
1599 char **kwlist, va_list *p_va, int flags)
1600 {
1601 char msgbuf[512];
1602 int levels[32];
1603 const char *fname, *msg, *custom_msg;
1604 int min = INT_MAX;
1605 int max = INT_MAX;
1606 int i, pos, len;
1607 int skip = 0;
1608 Py_ssize_t nargs, nkwargs;
1609 PyObject *current_arg;
1610 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
1611 freelist_t freelist;
1612
1613 freelist.entries = static_entries;
1614 freelist.first_available = 0;
1615 freelist.entries_malloced = 0;
1616
1617 assert(args != NULL && PyTuple_Check(args));
1618 assert(kwargs == NULL || PyDict_Check(kwargs));
1619 assert(format != NULL);
1620 assert(kwlist != NULL);
1621 assert(p_va != NULL);
1622
1623 /* grab the function name or custom error msg first (mutually exclusive) */
1624 fname = strchr(format, ':');
1625 if (fname) {
1626 fname++;
1627 custom_msg = NULL;
1628 }
1629 else {
1630 custom_msg = strchr(format,';');
1631 if (custom_msg)
1632 custom_msg++;
1633 }
1634
1635 /* scan kwlist and count the number of positional-only parameters */
1636 for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1637 }
1638 /* scan kwlist and get greatest possible nbr of args */
1639 for (len = pos; kwlist[len]; len++) {
1640 if (!*kwlist[len]) {
1641 PyErr_SetString(PyExc_SystemError,
1642 "Empty keyword parameter name");
1643 return cleanreturn(0, &freelist);
1644 }
1645 }
1646
1647 if (len > STATIC_FREELIST_ENTRIES) {
1648 freelist.entries = PyMem_NEW(freelistentry_t, len);
1649 if (freelist.entries == NULL) {
1650 PyErr_NoMemory();
1651 return 0;
1652 }
1653 freelist.entries_malloced = 1;
1654 }
1655
1656 nargs = PyTuple_GET_SIZE(args);
1657 nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
1658 if (nargs + nkwargs > len) {
1659 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
1660 messages in some special cases (see bpo-31229). */
1661 PyErr_Format(PyExc_TypeError,
1662 "%.200s%s takes at most %d %sargument%s (%zd given)",
1663 (fname == NULL) ? "function" : fname,
1664 (fname == NULL) ? "" : "()",
1665 len,
1666 (nargs == 0) ? "keyword " : "",
1667 (len == 1) ? "" : "s",
1668 nargs + nkwargs);
1669 return cleanreturn(0, &freelist);
1670 }
1671
1672 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1673 for (i = 0; i < len; i++) {
1674 if (*format == '|') {
1675 if (min != INT_MAX) {
1676 PyErr_SetString(PyExc_SystemError,
1677 "Invalid format string (| specified twice)");
1678 return cleanreturn(0, &freelist);
1679 }
1680
1681 min = i;
1682 format++;
1683
1684 if (max != INT_MAX) {
1685 PyErr_SetString(PyExc_SystemError,
1686 "Invalid format string ($ before |)");
1687 return cleanreturn(0, &freelist);
1688 }
1689 }
1690 if (*format == '$') {
1691 if (max != INT_MAX) {
1692 PyErr_SetString(PyExc_SystemError,
1693 "Invalid format string ($ specified twice)");
1694 return cleanreturn(0, &freelist);
1695 }
1696
1697 max = i;
1698 format++;
1699
1700 if (max < pos) {
1701 PyErr_SetString(PyExc_SystemError,
1702 "Empty parameter name after $");
1703 return cleanreturn(0, &freelist);
1704 }
1705 if (skip) {
1706 /* Now we know the minimal and the maximal numbers of
1707 * positional arguments and can raise an exception with
1708 * informative message (see below). */
1709 break;
1710 }
1711 if (max < nargs) {
1712 if (max == 0) {
1713 PyErr_Format(PyExc_TypeError,
1714 "%.200s%s takes no positional arguments",
1715 (fname == NULL) ? "function" : fname,
1716 (fname == NULL) ? "" : "()");
1717 }
1718 else {
1719 PyErr_Format(PyExc_TypeError,
1720 "%.200s%s takes %s %d positional arguments"
1721 " (%d given)",
1722 (fname == NULL) ? "function" : fname,
1723 (fname == NULL) ? "" : "()",
1724 (min != INT_MAX) ? "at most" : "exactly",
1725 max, nargs);
1726 }
1727 return cleanreturn(0, &freelist);
1728 }
1729 }
1730 if (IS_END_OF_FORMAT(*format)) {
1731 PyErr_Format(PyExc_SystemError,
1732 "More keyword list entries (%d) than "
1733 "format specifiers (%d)", len, i);
1734 return cleanreturn(0, &freelist);
1735 }
1736 if (!skip) {
1737 if (i < nargs) {
1738 current_arg = PyTuple_GET_ITEM(args, i);
1739 }
1740 else if (nkwargs && i >= pos) {
1741 current_arg = PyDict_GetItemString(kwargs, kwlist[i]);
1742 if (current_arg)
1743 --nkwargs;
1744 }
1745 else {
1746 current_arg = NULL;
1747 }
1748
1749 if (current_arg) {
1750 msg = convertitem(current_arg, &format, p_va, flags,
1751 levels, msgbuf, sizeof(msgbuf), &freelist);
1752 if (msg) {
1753 seterror(i+1, msg, levels, fname, custom_msg);
1754 return cleanreturn(0, &freelist);
1755 }
1756 continue;
1757 }
1758
1759 if (i < min) {
1760 if (i < pos) {
1761 assert (min == INT_MAX);
1762 assert (max == INT_MAX);
1763 skip = 1;
1764 /* At that moment we still don't know the minimal and
1765 * the maximal numbers of positional arguments. Raising
1766 * an exception is deferred until we encounter | and $
1767 * or the end of the format. */
1768 }
1769 else {
1770 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
1771 "argument '%s' (pos %d)",
1772 (fname == NULL) ? "function" : fname,
1773 (fname == NULL) ? "" : "()",
1774 kwlist[i], i+1);
1775 return cleanreturn(0, &freelist);
1776 }
1777 }
1778 /* current code reports success when all required args
1779 * fulfilled and no keyword args left, with no further
1780 * validation. XXX Maybe skip this in debug build ?
1781 */
1782 if (!nkwargs && !skip) {
1783 return cleanreturn(1, &freelist);
1784 }
1785 }
1786
1787 /* We are into optional args, skip through to any remaining
1788 * keyword args */
1789 msg = skipitem(&format, p_va, flags);
1790 if (msg) {
1791 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1792 format);
1793 return cleanreturn(0, &freelist);
1794 }
1795 }
1796
1797 if (skip) {
1798 PyErr_Format(PyExc_TypeError,
1799 "%.200s%s takes %s %d positional arguments"
1800 " (%d given)",
1801 (fname == NULL) ? "function" : fname,
1802 (fname == NULL) ? "" : "()",
1803 (Py_MIN(pos, min) < i) ? "at least" : "exactly",
1804 Py_MIN(pos, min), nargs);
1805 return cleanreturn(0, &freelist);
1806 }
1807
1808 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1809 PyErr_Format(PyExc_SystemError,
1810 "more argument specifiers than keyword list entries "
1811 "(remaining format:'%s')", format);
1812 return cleanreturn(0, &freelist);
1813 }
1814
1815 if (nkwargs > 0) {
1816 PyObject *key;
1817 Py_ssize_t j;
1818 /* make sure there are no arguments given by name and position */
1819 for (i = pos; i < nargs; i++) {
1820 current_arg = PyDict_GetItemString(kwargs, kwlist[i]);
1821 if (current_arg) {
1822 /* arg present in tuple and in dict */
1823 PyErr_Format(PyExc_TypeError,
1824 "argument for %.200s%s given by name ('%s') "
1825 "and position (%d)",
1826 (fname == NULL) ? "function" : fname,
1827 (fname == NULL) ? "" : "()",
1828 kwlist[i], i+1);
1829 return cleanreturn(0, &freelist);
1830 }
1831 }
1832 /* make sure there are no extraneous keyword arguments */
1833 j = 0;
1834 while (PyDict_Next(kwargs, &j, &key, NULL)) {
1835 int match = 0;
1836 if (!PyUnicode_Check(key)) {
1837 PyErr_SetString(PyExc_TypeError,
1838 "keywords must be strings");
1839 return cleanreturn(0, &freelist);
1840 }
1841 for (i = pos; i < len; i++) {
1842 if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) {
1843 match = 1;
1844 break;
1845 }
1846 }
1847 if (!match) {
1848 PyErr_Format(PyExc_TypeError,
1849 "'%U' is an invalid keyword "
1850 "argument for %.200s%s",
1851 key,
1852 (fname == NULL) ? "this function" : fname,
1853 (fname == NULL) ? "" : "()");
1854 return cleanreturn(0, &freelist);
1855 }
1856 }
1857 }
1858
1859 return cleanreturn(1, &freelist);
1860 }
1861
1862
1863 /* List of static parsers. */
1864 static struct _PyArg_Parser *static_arg_parsers = NULL;
1865
1866 static int
parser_init(struct _PyArg_Parser * parser)1867 parser_init(struct _PyArg_Parser *parser)
1868 {
1869 const char * const *keywords;
1870 const char *format, *msg;
1871 int i, len, min, max, nkw;
1872 PyObject *kwtuple;
1873
1874 assert(parser->format != NULL);
1875 assert(parser->keywords != NULL);
1876 if (parser->kwtuple != NULL) {
1877 return 1;
1878 }
1879
1880 /* grab the function name or custom error msg first (mutually exclusive) */
1881 parser->fname = strchr(parser->format, ':');
1882 if (parser->fname) {
1883 parser->fname++;
1884 parser->custom_msg = NULL;
1885 }
1886 else {
1887 parser->custom_msg = strchr(parser->format,';');
1888 if (parser->custom_msg)
1889 parser->custom_msg++;
1890 }
1891
1892 keywords = parser->keywords;
1893 /* scan keywords and count the number of positional-only parameters */
1894 for (i = 0; keywords[i] && !*keywords[i]; i++) {
1895 }
1896 parser->pos = i;
1897 /* scan keywords and get greatest possible nbr of args */
1898 for (; keywords[i]; i++) {
1899 if (!*keywords[i]) {
1900 PyErr_SetString(PyExc_SystemError,
1901 "Empty keyword parameter name");
1902 return 0;
1903 }
1904 }
1905 len = i;
1906
1907 min = max = INT_MAX;
1908 format = parser->format;
1909 for (i = 0; i < len; i++) {
1910 if (*format == '|') {
1911 if (min != INT_MAX) {
1912 PyErr_SetString(PyExc_SystemError,
1913 "Invalid format string (| specified twice)");
1914 return 0;
1915 }
1916 if (max != INT_MAX) {
1917 PyErr_SetString(PyExc_SystemError,
1918 "Invalid format string ($ before |)");
1919 return 0;
1920 }
1921 min = i;
1922 format++;
1923 }
1924 if (*format == '$') {
1925 if (max != INT_MAX) {
1926 PyErr_SetString(PyExc_SystemError,
1927 "Invalid format string ($ specified twice)");
1928 return 0;
1929 }
1930 if (i < parser->pos) {
1931 PyErr_SetString(PyExc_SystemError,
1932 "Empty parameter name after $");
1933 return 0;
1934 }
1935 max = i;
1936 format++;
1937 }
1938 if (IS_END_OF_FORMAT(*format)) {
1939 PyErr_Format(PyExc_SystemError,
1940 "More keyword list entries (%d) than "
1941 "format specifiers (%d)", len, i);
1942 return 0;
1943 }
1944
1945 msg = skipitem(&format, NULL, 0);
1946 if (msg) {
1947 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1948 format);
1949 return 0;
1950 }
1951 }
1952 parser->min = Py_MIN(min, len);
1953 parser->max = Py_MIN(max, len);
1954
1955 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1956 PyErr_Format(PyExc_SystemError,
1957 "more argument specifiers than keyword list entries "
1958 "(remaining format:'%s')", format);
1959 return 0;
1960 }
1961
1962 nkw = len - parser->pos;
1963 kwtuple = PyTuple_New(nkw);
1964 if (kwtuple == NULL) {
1965 return 0;
1966 }
1967 keywords = parser->keywords + parser->pos;
1968 for (i = 0; i < nkw; i++) {
1969 PyObject *str = PyUnicode_FromString(keywords[i]);
1970 if (str == NULL) {
1971 Py_DECREF(kwtuple);
1972 return 0;
1973 }
1974 PyUnicode_InternInPlace(&str);
1975 PyTuple_SET_ITEM(kwtuple, i, str);
1976 }
1977 parser->kwtuple = kwtuple;
1978
1979 assert(parser->next == NULL);
1980 parser->next = static_arg_parsers;
1981 static_arg_parsers = parser;
1982 return 1;
1983 }
1984
1985 static void
parser_clear(struct _PyArg_Parser * parser)1986 parser_clear(struct _PyArg_Parser *parser)
1987 {
1988 Py_CLEAR(parser->kwtuple);
1989 }
1990
1991 static PyObject*
find_keyword(PyObject * kwargs,PyObject * kwnames,PyObject * const * kwstack,PyObject * key)1992 find_keyword(PyObject *kwargs, PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
1993 {
1994 Py_ssize_t i, nkwargs;
1995
1996 if (kwargs != NULL) {
1997 return PyDict_GetItem(kwargs, key);
1998 }
1999 nkwargs = PyTuple_GET_SIZE(kwnames);
2000 for (i=0; i < nkwargs; i++) {
2001 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
2002
2003 /* ptr==ptr should match in most cases since keyword keys
2004 should be interned strings */
2005 if (kwname == key) {
2006 return kwstack[i];
2007 }
2008 if (!PyUnicode_Check(kwname)) {
2009 /* ignore non-string keyword keys:
2010 an error will be raised below */
2011 continue;
2012 }
2013 if (_PyUnicode_EQ(kwname, key)) {
2014 return kwstack[i];
2015 }
2016 }
2017 return NULL;
2018 }
2019
2020 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)2021 vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
2022 PyObject *kwargs, PyObject *kwnames,
2023 struct _PyArg_Parser *parser,
2024 va_list *p_va, int flags)
2025 {
2026 PyObject *kwtuple;
2027 char msgbuf[512];
2028 int levels[32];
2029 const char *format;
2030 const char *msg;
2031 PyObject *keyword;
2032 int i, pos, len;
2033 Py_ssize_t nkwargs;
2034 PyObject *current_arg;
2035 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
2036 freelist_t freelist;
2037 PyObject *const *kwstack = NULL;
2038
2039 freelist.entries = static_entries;
2040 freelist.first_available = 0;
2041 freelist.entries_malloced = 0;
2042
2043 assert(kwargs == NULL || PyDict_Check(kwargs));
2044 assert(kwargs == NULL || kwnames == NULL);
2045 assert(p_va != NULL);
2046
2047 if (parser == NULL) {
2048 PyErr_BadInternalCall();
2049 return 0;
2050 }
2051
2052 if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2053 PyErr_BadInternalCall();
2054 return 0;
2055 }
2056
2057 if (!parser_init(parser)) {
2058 return 0;
2059 }
2060
2061 kwtuple = parser->kwtuple;
2062 pos = parser->pos;
2063 len = pos + (int)PyTuple_GET_SIZE(kwtuple);
2064
2065 if (len > STATIC_FREELIST_ENTRIES) {
2066 freelist.entries = PyMem_NEW(freelistentry_t, len);
2067 if (freelist.entries == NULL) {
2068 PyErr_NoMemory();
2069 return 0;
2070 }
2071 freelist.entries_malloced = 1;
2072 }
2073
2074 if (kwargs != NULL) {
2075 nkwargs = PyDict_GET_SIZE(kwargs);
2076 }
2077 else if (kwnames != NULL) {
2078 nkwargs = PyTuple_GET_SIZE(kwnames);
2079 kwstack = args + nargs;
2080 }
2081 else {
2082 nkwargs = 0;
2083 }
2084 if (nargs + nkwargs > len) {
2085 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2086 messages in some special cases (see bpo-31229). */
2087 PyErr_Format(PyExc_TypeError,
2088 "%.200s%s takes at most %d %sargument%s (%zd given)",
2089 (parser->fname == NULL) ? "function" : parser->fname,
2090 (parser->fname == NULL) ? "" : "()",
2091 len,
2092 (nargs == 0) ? "keyword " : "",
2093 (len == 1) ? "" : "s",
2094 nargs + nkwargs);
2095 return cleanreturn(0, &freelist);
2096 }
2097 if (parser->max < nargs) {
2098 if (parser->max == 0) {
2099 PyErr_Format(PyExc_TypeError,
2100 "%.200s%s takes no positional arguments",
2101 (parser->fname == NULL) ? "function" : parser->fname,
2102 (parser->fname == NULL) ? "" : "()");
2103 }
2104 else {
2105 PyErr_Format(PyExc_TypeError,
2106 "%.200s%s takes %s %d positional arguments (%d given)",
2107 (parser->fname == NULL) ? "function" : parser->fname,
2108 (parser->fname == NULL) ? "" : "()",
2109 (parser->min != INT_MAX) ? "at most" : "exactly",
2110 parser->max, nargs);
2111 }
2112 return cleanreturn(0, &freelist);
2113 }
2114
2115 format = parser->format;
2116 /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
2117 for (i = 0; i < len; i++) {
2118 if (*format == '|') {
2119 format++;
2120 }
2121 if (*format == '$') {
2122 format++;
2123 }
2124 assert(!IS_END_OF_FORMAT(*format));
2125
2126 if (i < nargs) {
2127 current_arg = args[i];
2128 }
2129 else if (nkwargs && i >= pos) {
2130 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2131 current_arg = find_keyword(kwargs, kwnames, kwstack, keyword);
2132 if (current_arg)
2133 --nkwargs;
2134 }
2135 else {
2136 current_arg = NULL;
2137 }
2138
2139 if (current_arg) {
2140 msg = convertitem(current_arg, &format, p_va, flags,
2141 levels, msgbuf, sizeof(msgbuf), &freelist);
2142 if (msg) {
2143 seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2144 return cleanreturn(0, &freelist);
2145 }
2146 continue;
2147 }
2148
2149 if (i < parser->min) {
2150 /* Less arguments than required */
2151 if (i < pos) {
2152 Py_ssize_t min = Py_MIN(pos, parser->min);
2153 PyErr_Format(PyExc_TypeError,
2154 "%.200s%s takes %s %d positional arguments"
2155 " (%d given)",
2156 (parser->fname == NULL) ? "function" : parser->fname,
2157 (parser->fname == NULL) ? "" : "()",
2158 min < parser->max ? "at least" : "exactly",
2159 min, nargs);
2160 }
2161 else {
2162 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2163 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
2164 "argument '%U' (pos %d)",
2165 (parser->fname == NULL) ? "function" : parser->fname,
2166 (parser->fname == NULL) ? "" : "()",
2167 keyword, i+1);
2168 }
2169 return cleanreturn(0, &freelist);
2170 }
2171 /* current code reports success when all required args
2172 * fulfilled and no keyword args left, with no further
2173 * validation. XXX Maybe skip this in debug build ?
2174 */
2175 if (!nkwargs) {
2176 return cleanreturn(1, &freelist);
2177 }
2178
2179 /* We are into optional args, skip through to any remaining
2180 * keyword args */
2181 msg = skipitem(&format, p_va, flags);
2182 assert(msg == NULL);
2183 }
2184
2185 assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2186
2187 if (nkwargs > 0) {
2188 Py_ssize_t j;
2189 /* make sure there are no arguments given by name and position */
2190 for (i = pos; i < nargs; i++) {
2191 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2192 current_arg = find_keyword(kwargs, kwnames, kwstack, keyword);
2193 if (current_arg) {
2194 /* arg present in tuple and in dict */
2195 PyErr_Format(PyExc_TypeError,
2196 "argument for %.200s%s given by name ('%U') "
2197 "and position (%d)",
2198 (parser->fname == NULL) ? "function" : parser->fname,
2199 (parser->fname == NULL) ? "" : "()",
2200 keyword, i+1);
2201 return cleanreturn(0, &freelist);
2202 }
2203 }
2204 /* make sure there are no extraneous keyword arguments */
2205 j = 0;
2206 while (1) {
2207 int match;
2208 if (kwargs != NULL) {
2209 if (!PyDict_Next(kwargs, &j, &keyword, NULL))
2210 break;
2211 }
2212 else {
2213 if (j >= PyTuple_GET_SIZE(kwnames))
2214 break;
2215 keyword = PyTuple_GET_ITEM(kwnames, j);
2216 j++;
2217 }
2218
2219 if (!PyUnicode_Check(keyword)) {
2220 PyErr_SetString(PyExc_TypeError,
2221 "keywords must be strings");
2222 return cleanreturn(0, &freelist);
2223 }
2224 match = PySequence_Contains(kwtuple, keyword);
2225 if (match <= 0) {
2226 if (!match) {
2227 PyErr_Format(PyExc_TypeError,
2228 "'%U' is an invalid keyword "
2229 "argument for %.200s%s",
2230 keyword,
2231 (parser->fname == NULL) ? "this function" : parser->fname,
2232 (parser->fname == NULL) ? "" : "()");
2233 }
2234 return cleanreturn(0, &freelist);
2235 }
2236 }
2237 }
2238
2239 return cleanreturn(1, &freelist);
2240 }
2241
2242 static int
vgetargskeywordsfast(PyObject * args,PyObject * keywords,struct _PyArg_Parser * parser,va_list * p_va,int flags)2243 vgetargskeywordsfast(PyObject *args, PyObject *keywords,
2244 struct _PyArg_Parser *parser, va_list *p_va, int flags)
2245 {
2246 PyObject **stack;
2247 Py_ssize_t nargs;
2248
2249 if (args == NULL
2250 || !PyTuple_Check(args)
2251 || (keywords != NULL && !PyDict_Check(keywords)))
2252 {
2253 PyErr_BadInternalCall();
2254 return 0;
2255 }
2256
2257 stack = &PyTuple_GET_ITEM(args, 0);
2258 nargs = PyTuple_GET_SIZE(args);
2259 return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
2260 parser, p_va, flags);
2261 }
2262
2263
2264 static const char *
skipitem(const char ** p_format,va_list * p_va,int flags)2265 skipitem(const char **p_format, va_list *p_va, int flags)
2266 {
2267 const char *format = *p_format;
2268 char c = *format++;
2269
2270 switch (c) {
2271
2272 /*
2273 * codes that take a single data pointer as an argument
2274 * (the type of the pointer is irrelevant)
2275 */
2276
2277 case 'b': /* byte -- very short int */
2278 case 'B': /* byte as bitfield */
2279 case 'h': /* short int */
2280 case 'H': /* short int as bitfield */
2281 case 'i': /* int */
2282 case 'I': /* int sized bitfield */
2283 case 'l': /* long int */
2284 case 'k': /* long int sized bitfield */
2285 case 'L': /* long long */
2286 case 'K': /* long long sized bitfield */
2287 case 'n': /* Py_ssize_t */
2288 case 'f': /* float */
2289 case 'd': /* double */
2290 case 'D': /* complex double */
2291 case 'c': /* char */
2292 case 'C': /* unicode char */
2293 case 'p': /* boolean predicate */
2294 case 'S': /* string object */
2295 case 'Y': /* string object */
2296 case 'U': /* unicode string object */
2297 {
2298 if (p_va != NULL) {
2299 (void) va_arg(*p_va, void *);
2300 }
2301 break;
2302 }
2303
2304 /* string codes */
2305
2306 case 'e': /* string with encoding */
2307 {
2308 if (p_va != NULL) {
2309 (void) va_arg(*p_va, const char *);
2310 }
2311 if (!(*format == 's' || *format == 't'))
2312 /* after 'e', only 's' and 't' is allowed */
2313 goto err;
2314 format++;
2315 }
2316 /* fall through */
2317
2318 case 's': /* string */
2319 case 'z': /* string or None */
2320 case 'y': /* bytes */
2321 case 'u': /* unicode string */
2322 case 'Z': /* unicode string or None */
2323 case 'w': /* buffer, read-write */
2324 {
2325 if (p_va != NULL) {
2326 (void) va_arg(*p_va, char **);
2327 }
2328 if (*format == '#') {
2329 if (p_va != NULL) {
2330 if (flags & FLAG_SIZE_T)
2331 (void) va_arg(*p_va, Py_ssize_t *);
2332 else
2333 (void) va_arg(*p_va, int *);
2334 }
2335 format++;
2336 } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w')
2337 && *format == '*')
2338 {
2339 format++;
2340 }
2341 break;
2342 }
2343
2344 case 'O': /* object */
2345 {
2346 if (*format == '!') {
2347 format++;
2348 if (p_va != NULL) {
2349 (void) va_arg(*p_va, PyTypeObject*);
2350 (void) va_arg(*p_va, PyObject **);
2351 }
2352 }
2353 else if (*format == '&') {
2354 typedef int (*converter)(PyObject *, void *);
2355 if (p_va != NULL) {
2356 (void) va_arg(*p_va, converter);
2357 (void) va_arg(*p_va, void *);
2358 }
2359 format++;
2360 }
2361 else {
2362 if (p_va != NULL) {
2363 (void) va_arg(*p_va, PyObject **);
2364 }
2365 }
2366 break;
2367 }
2368
2369 case '(': /* bypass tuple, not handled at all previously */
2370 {
2371 const char *msg;
2372 for (;;) {
2373 if (*format==')')
2374 break;
2375 if (IS_END_OF_FORMAT(*format))
2376 return "Unmatched left paren in format "
2377 "string";
2378 msg = skipitem(&format, p_va, flags);
2379 if (msg)
2380 return msg;
2381 }
2382 format++;
2383 break;
2384 }
2385
2386 case ')':
2387 return "Unmatched right paren in format string";
2388
2389 default:
2390 err:
2391 return "impossible<bad format char>";
2392
2393 }
2394
2395 *p_format = format;
2396 return NULL;
2397 }
2398
2399
2400 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)2401 unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name,
2402 Py_ssize_t min, Py_ssize_t max, va_list vargs)
2403 {
2404 Py_ssize_t i;
2405 PyObject **o;
2406
2407 assert(min >= 0);
2408 assert(min <= max);
2409
2410 if (nargs < min) {
2411 if (name != NULL)
2412 PyErr_Format(
2413 PyExc_TypeError,
2414 "%.200s expected %s%zd arguments, got %zd",
2415 name, (min == max ? "" : "at least "), min, nargs);
2416 else
2417 PyErr_Format(
2418 PyExc_TypeError,
2419 "unpacked tuple should have %s%zd elements,"
2420 " but has %zd",
2421 (min == max ? "" : "at least "), min, nargs);
2422 return 0;
2423 }
2424
2425 if (nargs == 0) {
2426 return 1;
2427 }
2428
2429 if (nargs > max) {
2430 if (name != NULL)
2431 PyErr_Format(
2432 PyExc_TypeError,
2433 "%.200s expected %s%zd arguments, got %zd",
2434 name, (min == max ? "" : "at most "), max, nargs);
2435 else
2436 PyErr_Format(
2437 PyExc_TypeError,
2438 "unpacked tuple should have %s%zd elements,"
2439 " but has %zd",
2440 (min == max ? "" : "at most "), max, nargs);
2441 return 0;
2442 }
2443
2444 for (i = 0; i < nargs; i++) {
2445 o = va_arg(vargs, PyObject **);
2446 *o = args[i];
2447 }
2448 return 1;
2449 }
2450
2451 int
PyArg_UnpackTuple(PyObject * args,const char * name,Py_ssize_t min,Py_ssize_t max,...)2452 PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
2453 {
2454 PyObject **stack;
2455 Py_ssize_t nargs;
2456 int retval;
2457 va_list vargs;
2458
2459 if (!PyTuple_Check(args)) {
2460 PyErr_SetString(PyExc_SystemError,
2461 "PyArg_UnpackTuple() argument list is not a tuple");
2462 return 0;
2463 }
2464 stack = &PyTuple_GET_ITEM(args, 0);
2465 nargs = PyTuple_GET_SIZE(args);
2466
2467 #ifdef HAVE_STDARG_PROTOTYPES
2468 va_start(vargs, max);
2469 #else
2470 va_start(vargs);
2471 #endif
2472 retval = unpack_stack(stack, nargs, name, min, max, vargs);
2473 va_end(vargs);
2474 return retval;
2475 }
2476
2477 int
_PyArg_UnpackStack(PyObject * const * args,Py_ssize_t nargs,const char * name,Py_ssize_t min,Py_ssize_t max,...)2478 _PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
2479 Py_ssize_t min, Py_ssize_t max, ...)
2480 {
2481 int retval;
2482 va_list vargs;
2483
2484 #ifdef HAVE_STDARG_PROTOTYPES
2485 va_start(vargs, max);
2486 #else
2487 va_start(vargs);
2488 #endif
2489 retval = unpack_stack(args, nargs, name, min, max, vargs);
2490 va_end(vargs);
2491 return retval;
2492 }
2493
2494
2495 #undef _PyArg_NoKeywords
2496 #undef _PyArg_NoPositional
2497
2498 /* For type constructors that don't take keyword args
2499 *
2500 * Sets a TypeError and returns 0 if the args/kwargs is
2501 * not empty, returns 1 otherwise
2502 */
2503 int
_PyArg_NoKeywords(const char * funcname,PyObject * kwargs)2504 _PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
2505 {
2506 if (kwargs == NULL) {
2507 return 1;
2508 }
2509 if (!PyDict_CheckExact(kwargs)) {
2510 PyErr_BadInternalCall();
2511 return 0;
2512 }
2513 if (PyDict_GET_SIZE(kwargs) == 0) {
2514 return 1;
2515 }
2516
2517 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
2518 funcname);
2519 return 0;
2520 }
2521
2522
2523 int
_PyArg_NoPositional(const char * funcname,PyObject * args)2524 _PyArg_NoPositional(const char *funcname, PyObject *args)
2525 {
2526 if (args == NULL)
2527 return 1;
2528 if (!PyTuple_CheckExact(args)) {
2529 PyErr_BadInternalCall();
2530 return 0;
2531 }
2532 if (PyTuple_GET_SIZE(args) == 0)
2533 return 1;
2534
2535 PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
2536 funcname);
2537 return 0;
2538 }
2539
2540 void
_PyArg_Fini(void)2541 _PyArg_Fini(void)
2542 {
2543 struct _PyArg_Parser *tmp, *s = static_arg_parsers;
2544 while (s) {
2545 tmp = s->next;
2546 s->next = NULL;
2547 parser_clear(s);
2548 s = tmp;
2549 }
2550 static_arg_parsers = NULL;
2551 }
2552
2553 #ifdef __cplusplus
2554 };
2555 #endif
2556