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