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 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 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 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 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(Py_CHARMASK(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(Py_CHARMASK(*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(Py_CHARMASK(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" : arg->ob_type->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" : arg->ob_type->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" : arg->ob_type->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         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         Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1074 
1075         if (*format == '#') {
1076             /* "u#" or "Z#" */
1077             FETCH_SIZE;
1078 
1079             if (c == 'Z' && arg == Py_None) {
1080                 *p = NULL;
1081                 STORE_SIZE(0);
1082             }
1083             else if (PyUnicode_Check(arg)) {
1084                 Py_ssize_t len;
1085                 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
1086                 if (*p == NULL)
1087                     RETURN_ERR_OCCURRED;
1088                 STORE_SIZE(len);
1089             }
1090             else
1091                 return converterr(c == 'Z' ? "str or None" : "str",
1092                                   arg, msgbuf, bufsize);
1093             format++;
1094         } else {
1095             /* "u" or "Z" */
1096             if (c == 'Z' && arg == Py_None)
1097                 *p = NULL;
1098             else if (PyUnicode_Check(arg)) {
1099                 Py_ssize_t len;
1100                 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
1101                 if (*p == NULL)
1102                     RETURN_ERR_OCCURRED;
1103                 if (wcslen(*p) != (size_t)len) {
1104                     PyErr_SetString(PyExc_ValueError, "embedded null character");
1105                     RETURN_ERR_OCCURRED;
1106                 }
1107             } else
1108                 return converterr(c == 'Z' ? "str or None" : "str",
1109                                   arg, msgbuf, bufsize);
1110         }
1111         break;
1112     }
1113 
1114     case 'e': {/* encoded string */
1115         char **buffer;
1116         const char *encoding;
1117         PyObject *s;
1118         int recode_strings;
1119         Py_ssize_t size;
1120         const char *ptr;
1121 
1122         /* Get 'e' parameter: the encoding name */
1123         encoding = (const char *)va_arg(*p_va, const char *);
1124         if (encoding == NULL)
1125             encoding = PyUnicode_GetDefaultEncoding();
1126 
1127         /* Get output buffer parameter:
1128            's' (recode all objects via Unicode) or
1129            't' (only recode non-string objects)
1130         */
1131         if (*format == 's')
1132             recode_strings = 1;
1133         else if (*format == 't')
1134             recode_strings = 0;
1135         else
1136             return converterr(
1137                 "(unknown parser marker combination)",
1138                 arg, msgbuf, bufsize);
1139         buffer = (char **)va_arg(*p_va, char **);
1140         format++;
1141         if (buffer == NULL)
1142             return converterr("(buffer is NULL)",
1143                               arg, msgbuf, bufsize);
1144 
1145         /* Encode object */
1146         if (!recode_strings &&
1147             (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1148             s = arg;
1149             Py_INCREF(s);
1150             if (PyBytes_Check(arg)) {
1151                 size = PyBytes_GET_SIZE(s);
1152                 ptr = PyBytes_AS_STRING(s);
1153             }
1154             else {
1155                 size = PyByteArray_GET_SIZE(s);
1156                 ptr = PyByteArray_AS_STRING(s);
1157             }
1158         }
1159         else if (PyUnicode_Check(arg)) {
1160             /* Encode object; use default error handling */
1161             s = PyUnicode_AsEncodedString(arg,
1162                                           encoding,
1163                                           NULL);
1164             if (s == NULL)
1165                 return converterr("(encoding failed)",
1166                                   arg, msgbuf, bufsize);
1167             assert(PyBytes_Check(s));
1168             size = PyBytes_GET_SIZE(s);
1169             ptr = PyBytes_AS_STRING(s);
1170             if (ptr == NULL)
1171                 ptr = "";
1172         }
1173         else {
1174             return converterr(
1175                 recode_strings ? "str" : "str, bytes or bytearray",
1176                 arg, msgbuf, bufsize);
1177         }
1178 
1179         /* Write output; output is guaranteed to be 0-terminated */
1180         if (*format == '#') {
1181             /* Using buffer length parameter '#':
1182 
1183                - if *buffer is NULL, a new buffer of the
1184                needed size is allocated and the data
1185                copied into it; *buffer is updated to point
1186                to the new buffer; the caller is
1187                responsible for PyMem_Free()ing it after
1188                usage
1189 
1190                - if *buffer is not NULL, the data is
1191                copied to *buffer; *buffer_len has to be
1192                set to the size of the buffer on input;
1193                buffer overflow is signalled with an error;
1194                buffer has to provide enough room for the
1195                encoded string plus the trailing 0-byte
1196 
1197                - in both cases, *buffer_len is updated to
1198                the size of the buffer /excluding/ the
1199                trailing 0-byte
1200 
1201             */
1202             int *q = NULL; Py_ssize_t *q2 = NULL;
1203             if (flags & FLAG_SIZE_T) {
1204                 q2 = va_arg(*p_va, Py_ssize_t*);
1205             }
1206             else {
1207                 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1208                             "PY_SSIZE_T_CLEAN will be required for '#' formats", 1))
1209                 {
1210                     Py_DECREF(s);
1211                     return NULL;
1212                 }
1213                 q = va_arg(*p_va, int*);
1214             }
1215 
1216             format++;
1217             if (q == NULL && q2 == NULL) {
1218                 Py_DECREF(s);
1219                 return converterr(
1220                     "(buffer_len is NULL)",
1221                     arg, msgbuf, bufsize);
1222             }
1223             if (*buffer == NULL) {
1224                 *buffer = PyMem_NEW(char, size + 1);
1225                 if (*buffer == NULL) {
1226                     Py_DECREF(s);
1227                     PyErr_NoMemory();
1228                     RETURN_ERR_OCCURRED;
1229                 }
1230                 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1231                     Py_DECREF(s);
1232                     return converterr(
1233                         "(cleanup problem)",
1234                         arg, msgbuf, bufsize);
1235                 }
1236             } else {
1237                 if (size + 1 > BUFFER_LEN) {
1238                     Py_DECREF(s);
1239                     PyErr_Format(PyExc_ValueError,
1240                                  "encoded string too long "
1241                                  "(%zd, maximum length %zd)",
1242                                  (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1243                     RETURN_ERR_OCCURRED;
1244                 }
1245             }
1246             memcpy(*buffer, ptr, size+1);
1247 
1248             if (flags & FLAG_SIZE_T) {
1249                 *q2 = size;
1250             }
1251             else {
1252                 if (INT_MAX < size) {
1253                     Py_DECREF(s);
1254                     PyErr_SetString(PyExc_OverflowError,
1255                                     "size does not fit in an int");
1256                     return converterr("", arg, msgbuf, bufsize);
1257                 }
1258                 *q = (int)size;
1259             }
1260         } else {
1261             /* Using a 0-terminated buffer:
1262 
1263                - the encoded string has to be 0-terminated
1264                for this variant to work; if it is not, an
1265                error raised
1266 
1267                - a new buffer of the needed size is
1268                allocated and the data copied into it;
1269                *buffer is updated to point to the new
1270                buffer; the caller is responsible for
1271                PyMem_Free()ing it after usage
1272 
1273             */
1274             if ((Py_ssize_t)strlen(ptr) != size) {
1275                 Py_DECREF(s);
1276                 return converterr(
1277                     "encoded string without null bytes",
1278                     arg, msgbuf, bufsize);
1279             }
1280             *buffer = PyMem_NEW(char, size + 1);
1281             if (*buffer == NULL) {
1282                 Py_DECREF(s);
1283                 PyErr_NoMemory();
1284                 RETURN_ERR_OCCURRED;
1285             }
1286             if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1287                 Py_DECREF(s);
1288                 return converterr("(cleanup problem)",
1289                                 arg, msgbuf, bufsize);
1290             }
1291             memcpy(*buffer, ptr, size+1);
1292         }
1293         Py_DECREF(s);
1294         break;
1295     }
1296 
1297     case 'S': { /* PyBytes object */
1298         PyObject **p = va_arg(*p_va, PyObject **);
1299         if (PyBytes_Check(arg))
1300             *p = arg;
1301         else
1302             return converterr("bytes", arg, msgbuf, bufsize);
1303         break;
1304     }
1305 
1306     case 'Y': { /* PyByteArray object */
1307         PyObject **p = va_arg(*p_va, PyObject **);
1308         if (PyByteArray_Check(arg))
1309             *p = arg;
1310         else
1311             return converterr("bytearray", arg, msgbuf, bufsize);
1312         break;
1313     }
1314 
1315     case 'U': { /* PyUnicode object */
1316         PyObject **p = va_arg(*p_va, PyObject **);
1317         if (PyUnicode_Check(arg)) {
1318             if (PyUnicode_READY(arg) == -1)
1319                 RETURN_ERR_OCCURRED;
1320             *p = arg;
1321         }
1322         else
1323             return converterr("str", arg, msgbuf, bufsize);
1324         break;
1325     }
1326 
1327     case 'O': { /* object */
1328         PyTypeObject *type;
1329         PyObject **p;
1330         if (*format == '!') {
1331             type = va_arg(*p_va, PyTypeObject*);
1332             p = va_arg(*p_va, PyObject **);
1333             format++;
1334             if (PyType_IsSubtype(arg->ob_type, type))
1335                 *p = arg;
1336             else
1337                 return converterr(type->tp_name, arg, msgbuf, bufsize);
1338 
1339         }
1340         else if (*format == '&') {
1341             typedef int (*converter)(PyObject *, void *);
1342             converter convert = va_arg(*p_va, converter);
1343             void *addr = va_arg(*p_va, void *);
1344             int res;
1345             format++;
1346             if (! (res = (*convert)(arg, addr)))
1347                 return converterr("(unspecified)",
1348                                   arg, msgbuf, bufsize);
1349             if (res == Py_CLEANUP_SUPPORTED &&
1350                 addcleanup(addr, freelist, convert) == -1)
1351                 return converterr("(cleanup problem)",
1352                                 arg, msgbuf, bufsize);
1353         }
1354         else {
1355             p = va_arg(*p_va, PyObject **);
1356             *p = arg;
1357         }
1358         break;
1359     }
1360 
1361 
1362     case 'w': { /* "w*": memory buffer, read-write access */
1363         void **p = va_arg(*p_va, void **);
1364 
1365         if (*format != '*')
1366             return converterr(
1367                 "(invalid use of 'w' format character)",
1368                 arg, msgbuf, bufsize);
1369         format++;
1370 
1371         /* Caller is interested in Py_buffer, and the object
1372            supports it directly. */
1373         if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1374             PyErr_Clear();
1375             return converterr("read-write bytes-like object",
1376                               arg, msgbuf, bufsize);
1377         }
1378         if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1379             PyBuffer_Release((Py_buffer*)p);
1380             return converterr("contiguous buffer", arg, msgbuf, bufsize);
1381         }
1382         if (addcleanup(p, freelist, cleanup_buffer)) {
1383             return converterr(
1384                 "(cleanup problem)",
1385                 arg, msgbuf, bufsize);
1386         }
1387         break;
1388     }
1389 
1390     default:
1391         return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
1392 
1393     }
1394 
1395     *p_format = format;
1396     return NULL;
1397 
1398 #undef FETCH_SIZE
1399 #undef STORE_SIZE
1400 #undef BUFFER_LEN
1401 #undef RETURN_ERR_OCCURRED
1402 }
1403 
1404 static Py_ssize_t
convertbuffer(PyObject * arg,const void ** p,const char ** errmsg)1405 convertbuffer(PyObject *arg, const void **p, const char **errmsg)
1406 {
1407     PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
1408     Py_ssize_t count;
1409     Py_buffer view;
1410 
1411     *errmsg = NULL;
1412     *p = NULL;
1413     if (pb != NULL && pb->bf_releasebuffer != NULL) {
1414         *errmsg = "read-only bytes-like object";
1415         return -1;
1416     }
1417 
1418     if (getbuffer(arg, &view, errmsg) < 0)
1419         return -1;
1420     count = view.len;
1421     *p = view.buf;
1422     PyBuffer_Release(&view);
1423     return count;
1424 }
1425 
1426 static int
getbuffer(PyObject * arg,Py_buffer * view,const char ** errmsg)1427 getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
1428 {
1429     if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
1430         *errmsg = "bytes-like object";
1431         return -1;
1432     }
1433     if (!PyBuffer_IsContiguous(view, 'C')) {
1434         PyBuffer_Release(view);
1435         *errmsg = "contiguous buffer";
1436         return -1;
1437     }
1438     return 0;
1439 }
1440 
1441 /* Support for keyword arguments donated by
1442    Geoff Philbrick <philbric@delphi.hks.com> */
1443 
1444 /* Return false (0) for error, else true. */
1445 int
PyArg_ParseTupleAndKeywords(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,...)1446 PyArg_ParseTupleAndKeywords(PyObject *args,
1447                             PyObject *keywords,
1448                             const char *format,
1449                             char **kwlist, ...)
1450 {
1451     int retval;
1452     va_list va;
1453 
1454     if ((args == NULL || !PyTuple_Check(args)) ||
1455         (keywords != NULL && !PyDict_Check(keywords)) ||
1456         format == NULL ||
1457         kwlist == NULL)
1458     {
1459         PyErr_BadInternalCall();
1460         return 0;
1461     }
1462 
1463     va_start(va, kwlist);
1464     retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1465     va_end(va);
1466     return retval;
1467 }
1468 
1469 int
_PyArg_ParseTupleAndKeywords_SizeT(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,...)1470 _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1471                                   PyObject *keywords,
1472                                   const char *format,
1473                                   char **kwlist, ...)
1474 {
1475     int retval;
1476     va_list va;
1477 
1478     if ((args == NULL || !PyTuple_Check(args)) ||
1479         (keywords != NULL && !PyDict_Check(keywords)) ||
1480         format == NULL ||
1481         kwlist == NULL)
1482     {
1483         PyErr_BadInternalCall();
1484         return 0;
1485     }
1486 
1487     va_start(va, kwlist);
1488     retval = vgetargskeywords(args, keywords, format,
1489                               kwlist, &va, FLAG_SIZE_T);
1490     va_end(va);
1491     return retval;
1492 }
1493 
1494 
1495 int
PyArg_VaParseTupleAndKeywords(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,va_list va)1496 PyArg_VaParseTupleAndKeywords(PyObject *args,
1497                               PyObject *keywords,
1498                               const char *format,
1499                               char **kwlist, va_list va)
1500 {
1501     int retval;
1502     va_list lva;
1503 
1504     if ((args == NULL || !PyTuple_Check(args)) ||
1505         (keywords != NULL && !PyDict_Check(keywords)) ||
1506         format == NULL ||
1507         kwlist == NULL)
1508     {
1509         PyErr_BadInternalCall();
1510         return 0;
1511     }
1512 
1513     va_copy(lva, va);
1514 
1515     retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1516     va_end(lva);
1517     return retval;
1518 }
1519 
1520 int
_PyArg_VaParseTupleAndKeywords_SizeT(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,va_list va)1521 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1522                                     PyObject *keywords,
1523                                     const char *format,
1524                                     char **kwlist, va_list va)
1525 {
1526     int retval;
1527     va_list lva;
1528 
1529     if ((args == NULL || !PyTuple_Check(args)) ||
1530         (keywords != NULL && !PyDict_Check(keywords)) ||
1531         format == NULL ||
1532         kwlist == NULL)
1533     {
1534         PyErr_BadInternalCall();
1535         return 0;
1536     }
1537 
1538     va_copy(lva, va);
1539 
1540     retval = vgetargskeywords(args, keywords, format,
1541                               kwlist, &lva, FLAG_SIZE_T);
1542     va_end(lva);
1543     return retval;
1544 }
1545 
1546 int
_PyArg_ParseTupleAndKeywordsFast(PyObject * args,PyObject * keywords,struct _PyArg_Parser * parser,...)1547 _PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1548                             struct _PyArg_Parser *parser, ...)
1549 {
1550     int retval;
1551     va_list va;
1552 
1553     va_start(va, parser);
1554     retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1555     va_end(va);
1556     return retval;
1557 }
1558 
1559 int
_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject * args,PyObject * keywords,struct _PyArg_Parser * parser,...)1560 _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1561                             struct _PyArg_Parser *parser, ...)
1562 {
1563     int retval;
1564     va_list va;
1565 
1566     va_start(va, parser);
1567     retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
1568     va_end(va);
1569     return retval;
1570 }
1571 
1572 int
_PyArg_ParseStackAndKeywords(PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames,struct _PyArg_Parser * parser,...)1573 _PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
1574                   struct _PyArg_Parser *parser, ...)
1575 {
1576     int retval;
1577     va_list va;
1578 
1579     va_start(va, parser);
1580     retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1581     va_end(va);
1582     return retval;
1583 }
1584 
1585 int
_PyArg_ParseStackAndKeywords_SizeT(PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames,struct _PyArg_Parser * parser,...)1586 _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
1587                         struct _PyArg_Parser *parser, ...)
1588 {
1589     int retval;
1590     va_list va;
1591 
1592     va_start(va, parser);
1593     retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T);
1594     va_end(va);
1595     return retval;
1596 }
1597 
1598 
1599 int
_PyArg_VaParseTupleAndKeywordsFast(PyObject * args,PyObject * keywords,struct _PyArg_Parser * parser,va_list va)1600 _PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1601                             struct _PyArg_Parser *parser, va_list va)
1602 {
1603     int retval;
1604     va_list lva;
1605 
1606     va_copy(lva, va);
1607 
1608     retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
1609     va_end(lva);
1610     return retval;
1611 }
1612 
1613 int
_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject * args,PyObject * keywords,struct _PyArg_Parser * parser,va_list va)1614 _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1615                             struct _PyArg_Parser *parser, va_list va)
1616 {
1617     int retval;
1618     va_list lva;
1619 
1620     va_copy(lva, va);
1621 
1622     retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
1623     va_end(lva);
1624     return retval;
1625 }
1626 
1627 int
PyArg_ValidateKeywordArguments(PyObject * kwargs)1628 PyArg_ValidateKeywordArguments(PyObject *kwargs)
1629 {
1630     if (!PyDict_Check(kwargs)) {
1631         PyErr_BadInternalCall();
1632         return 0;
1633     }
1634     if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1635         PyErr_SetString(PyExc_TypeError,
1636                         "keywords must be strings");
1637         return 0;
1638     }
1639     return 1;
1640 }
1641 
1642 #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1643 
1644 static int
vgetargskeywords(PyObject * args,PyObject * kwargs,const char * format,char ** kwlist,va_list * p_va,int flags)1645 vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
1646                  char **kwlist, va_list *p_va, int flags)
1647 {
1648     char msgbuf[512];
1649     int levels[32];
1650     const char *fname, *msg, *custom_msg;
1651     int min = INT_MAX;
1652     int max = INT_MAX;
1653     int i, pos, len;
1654     int skip = 0;
1655     Py_ssize_t nargs, nkwargs;
1656     PyObject *current_arg;
1657     freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
1658     freelist_t freelist;
1659 
1660     freelist.entries = static_entries;
1661     freelist.first_available = 0;
1662     freelist.entries_malloced = 0;
1663 
1664     assert(args != NULL && PyTuple_Check(args));
1665     assert(kwargs == NULL || PyDict_Check(kwargs));
1666     assert(format != NULL);
1667     assert(kwlist != NULL);
1668     assert(p_va != NULL);
1669 
1670     /* grab the function name or custom error msg first (mutually exclusive) */
1671     fname = strchr(format, ':');
1672     if (fname) {
1673         fname++;
1674         custom_msg = NULL;
1675     }
1676     else {
1677         custom_msg = strchr(format,';');
1678         if (custom_msg)
1679             custom_msg++;
1680     }
1681 
1682     /* scan kwlist and count the number of positional-only parameters */
1683     for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1684     }
1685     /* scan kwlist and get greatest possible nbr of args */
1686     for (len = pos; kwlist[len]; len++) {
1687         if (!*kwlist[len]) {
1688             PyErr_SetString(PyExc_SystemError,
1689                             "Empty keyword parameter name");
1690             return cleanreturn(0, &freelist);
1691         }
1692     }
1693 
1694     if (len > STATIC_FREELIST_ENTRIES) {
1695         freelist.entries = PyMem_NEW(freelistentry_t, len);
1696         if (freelist.entries == NULL) {
1697             PyErr_NoMemory();
1698             return 0;
1699         }
1700         freelist.entries_malloced = 1;
1701     }
1702 
1703     nargs = PyTuple_GET_SIZE(args);
1704     nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
1705     if (nargs + nkwargs > len) {
1706         /* Adding "keyword" (when nargs == 0) prevents producing wrong error
1707            messages in some special cases (see bpo-31229). */
1708         PyErr_Format(PyExc_TypeError,
1709                      "%.200s%s takes at most %d %sargument%s (%zd given)",
1710                      (fname == NULL) ? "function" : fname,
1711                      (fname == NULL) ? "" : "()",
1712                      len,
1713                      (nargs == 0) ? "keyword " : "",
1714                      (len == 1) ? "" : "s",
1715                      nargs + nkwargs);
1716         return cleanreturn(0, &freelist);
1717     }
1718 
1719     /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1720     for (i = 0; i < len; i++) {
1721         if (*format == '|') {
1722             if (min != INT_MAX) {
1723                 PyErr_SetString(PyExc_SystemError,
1724                                 "Invalid format string (| specified twice)");
1725                 return cleanreturn(0, &freelist);
1726             }
1727 
1728             min = i;
1729             format++;
1730 
1731             if (max != INT_MAX) {
1732                 PyErr_SetString(PyExc_SystemError,
1733                                 "Invalid format string ($ before |)");
1734                 return cleanreturn(0, &freelist);
1735             }
1736         }
1737         if (*format == '$') {
1738             if (max != INT_MAX) {
1739                 PyErr_SetString(PyExc_SystemError,
1740                                 "Invalid format string ($ specified twice)");
1741                 return cleanreturn(0, &freelist);
1742             }
1743 
1744             max = i;
1745             format++;
1746 
1747             if (max < pos) {
1748                 PyErr_SetString(PyExc_SystemError,
1749                                 "Empty parameter name after $");
1750                 return cleanreturn(0, &freelist);
1751             }
1752             if (skip) {
1753                 /* Now we know the minimal and the maximal numbers of
1754                  * positional arguments and can raise an exception with
1755                  * informative message (see below). */
1756                 break;
1757             }
1758             if (max < nargs) {
1759                 if (max == 0) {
1760                     PyErr_Format(PyExc_TypeError,
1761                                  "%.200s%s takes no positional arguments",
1762                                  (fname == NULL) ? "function" : fname,
1763                                  (fname == NULL) ? "" : "()");
1764                 }
1765                 else {
1766                     PyErr_Format(PyExc_TypeError,
1767                                  "%.200s%s takes %s %d positional argument%s"
1768                                  " (%zd given)",
1769                                  (fname == NULL) ? "function" : fname,
1770                                  (fname == NULL) ? "" : "()",
1771                                  (min != INT_MAX) ? "at most" : "exactly",
1772                                  max,
1773                                  max == 1 ? "" : "s",
1774                                  nargs);
1775                 }
1776                 return cleanreturn(0, &freelist);
1777             }
1778         }
1779         if (IS_END_OF_FORMAT(*format)) {
1780             PyErr_Format(PyExc_SystemError,
1781                          "More keyword list entries (%d) than "
1782                          "format specifiers (%d)", len, i);
1783             return cleanreturn(0, &freelist);
1784         }
1785         if (!skip) {
1786             if (i < nargs) {
1787                 current_arg = PyTuple_GET_ITEM(args, i);
1788             }
1789             else if (nkwargs && i >= pos) {
1790                 current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
1791                 if (current_arg) {
1792                     --nkwargs;
1793                 }
1794                 else if (PyErr_Occurred()) {
1795                     return cleanreturn(0, &freelist);
1796                 }
1797             }
1798             else {
1799                 current_arg = NULL;
1800             }
1801 
1802             if (current_arg) {
1803                 msg = convertitem(current_arg, &format, p_va, flags,
1804                     levels, msgbuf, sizeof(msgbuf), &freelist);
1805                 if (msg) {
1806                     seterror(i+1, msg, levels, fname, custom_msg);
1807                     return cleanreturn(0, &freelist);
1808                 }
1809                 continue;
1810             }
1811 
1812             if (i < min) {
1813                 if (i < pos) {
1814                     assert (min == INT_MAX);
1815                     assert (max == INT_MAX);
1816                     skip = 1;
1817                     /* At that moment we still don't know the minimal and
1818                      * the maximal numbers of positional arguments.  Raising
1819                      * an exception is deferred until we encounter | and $
1820                      * or the end of the format. */
1821                 }
1822                 else {
1823                     PyErr_Format(PyExc_TypeError,  "%.200s%s missing required "
1824                                  "argument '%s' (pos %d)",
1825                                  (fname == NULL) ? "function" : fname,
1826                                  (fname == NULL) ? "" : "()",
1827                                  kwlist[i], i+1);
1828                     return cleanreturn(0, &freelist);
1829                 }
1830             }
1831             /* current code reports success when all required args
1832              * fulfilled and no keyword args left, with no further
1833              * validation. XXX Maybe skip this in debug build ?
1834              */
1835             if (!nkwargs && !skip) {
1836                 return cleanreturn(1, &freelist);
1837             }
1838         }
1839 
1840         /* We are into optional args, skip through to any remaining
1841          * keyword args */
1842         msg = skipitem(&format, p_va, flags);
1843         if (msg) {
1844             PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1845                          format);
1846             return cleanreturn(0, &freelist);
1847         }
1848     }
1849 
1850     if (skip) {
1851         PyErr_Format(PyExc_TypeError,
1852                      "%.200s%s takes %s %d positional argument%s"
1853                      " (%zd given)",
1854                      (fname == NULL) ? "function" : fname,
1855                      (fname == NULL) ? "" : "()",
1856                      (Py_MIN(pos, min) < i) ? "at least" : "exactly",
1857                      Py_MIN(pos, min),
1858                      Py_MIN(pos, min) == 1 ? "" : "s",
1859                      nargs);
1860         return cleanreturn(0, &freelist);
1861     }
1862 
1863     if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1864         PyErr_Format(PyExc_SystemError,
1865             "more argument specifiers than keyword list entries "
1866             "(remaining format:'%s')", format);
1867         return cleanreturn(0, &freelist);
1868     }
1869 
1870     if (nkwargs > 0) {
1871         PyObject *key;
1872         Py_ssize_t j;
1873         /* make sure there are no arguments given by name and position */
1874         for (i = pos; i < nargs; i++) {
1875             current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
1876             if (current_arg) {
1877                 /* arg present in tuple and in dict */
1878                 PyErr_Format(PyExc_TypeError,
1879                              "argument for %.200s%s given by name ('%s') "
1880                              "and position (%d)",
1881                              (fname == NULL) ? "function" : fname,
1882                              (fname == NULL) ? "" : "()",
1883                              kwlist[i], i+1);
1884                 return cleanreturn(0, &freelist);
1885             }
1886             else if (PyErr_Occurred()) {
1887                 return cleanreturn(0, &freelist);
1888             }
1889         }
1890         /* make sure there are no extraneous keyword arguments */
1891         j = 0;
1892         while (PyDict_Next(kwargs, &j, &key, NULL)) {
1893             int match = 0;
1894             if (!PyUnicode_Check(key)) {
1895                 PyErr_SetString(PyExc_TypeError,
1896                                 "keywords must be strings");
1897                 return cleanreturn(0, &freelist);
1898             }
1899             for (i = pos; i < len; i++) {
1900                 if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) {
1901                     match = 1;
1902                     break;
1903                 }
1904             }
1905             if (!match) {
1906                 PyErr_Format(PyExc_TypeError,
1907                              "'%U' is an invalid keyword "
1908                              "argument for %.200s%s",
1909                              key,
1910                              (fname == NULL) ? "this function" : fname,
1911                              (fname == NULL) ? "" : "()");
1912                 return cleanreturn(0, &freelist);
1913             }
1914         }
1915     }
1916 
1917     return cleanreturn(1, &freelist);
1918 }
1919 
1920 
1921 /* List of static parsers. */
1922 static struct _PyArg_Parser *static_arg_parsers = NULL;
1923 
1924 static int
parser_init(struct _PyArg_Parser * parser)1925 parser_init(struct _PyArg_Parser *parser)
1926 {
1927     const char * const *keywords;
1928     const char *format, *msg;
1929     int i, len, min, max, nkw;
1930     PyObject *kwtuple;
1931 
1932     assert(parser->keywords != NULL);
1933     if (parser->kwtuple != NULL) {
1934         return 1;
1935     }
1936 
1937     keywords = parser->keywords;
1938     /* scan keywords and count the number of positional-only parameters */
1939     for (i = 0; keywords[i] && !*keywords[i]; i++) {
1940     }
1941     parser->pos = i;
1942     /* scan keywords and get greatest possible nbr of args */
1943     for (; keywords[i]; i++) {
1944         if (!*keywords[i]) {
1945             PyErr_SetString(PyExc_SystemError,
1946                             "Empty keyword parameter name");
1947             return 0;
1948         }
1949     }
1950     len = i;
1951 
1952     format = parser->format;
1953     if (format) {
1954         /* grab the function name or custom error msg first (mutually exclusive) */
1955         parser->fname = strchr(parser->format, ':');
1956         if (parser->fname) {
1957             parser->fname++;
1958             parser->custom_msg = NULL;
1959         }
1960         else {
1961             parser->custom_msg = strchr(parser->format,';');
1962             if (parser->custom_msg)
1963                 parser->custom_msg++;
1964         }
1965 
1966         min = max = INT_MAX;
1967         for (i = 0; i < len; i++) {
1968             if (*format == '|') {
1969                 if (min != INT_MAX) {
1970                     PyErr_SetString(PyExc_SystemError,
1971                                     "Invalid format string (| specified twice)");
1972                     return 0;
1973                 }
1974                 if (max != INT_MAX) {
1975                     PyErr_SetString(PyExc_SystemError,
1976                                     "Invalid format string ($ before |)");
1977                     return 0;
1978                 }
1979                 min = i;
1980                 format++;
1981             }
1982             if (*format == '$') {
1983                 if (max != INT_MAX) {
1984                     PyErr_SetString(PyExc_SystemError,
1985                                     "Invalid format string ($ specified twice)");
1986                     return 0;
1987                 }
1988                 if (i < parser->pos) {
1989                     PyErr_SetString(PyExc_SystemError,
1990                                     "Empty parameter name after $");
1991                     return 0;
1992                 }
1993                 max = i;
1994                 format++;
1995             }
1996             if (IS_END_OF_FORMAT(*format)) {
1997                 PyErr_Format(PyExc_SystemError,
1998                             "More keyword list entries (%d) than "
1999                             "format specifiers (%d)", len, i);
2000                 return 0;
2001             }
2002 
2003             msg = skipitem(&format, NULL, 0);
2004             if (msg) {
2005                 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
2006                             format);
2007                 return 0;
2008             }
2009         }
2010         parser->min = Py_MIN(min, len);
2011         parser->max = Py_MIN(max, len);
2012 
2013         if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
2014             PyErr_Format(PyExc_SystemError,
2015                 "more argument specifiers than keyword list entries "
2016                 "(remaining format:'%s')", format);
2017             return 0;
2018         }
2019     }
2020 
2021     nkw = len - parser->pos;
2022     kwtuple = PyTuple_New(nkw);
2023     if (kwtuple == NULL) {
2024         return 0;
2025     }
2026     keywords = parser->keywords + parser->pos;
2027     for (i = 0; i < nkw; i++) {
2028         PyObject *str = PyUnicode_FromString(keywords[i]);
2029         if (str == NULL) {
2030             Py_DECREF(kwtuple);
2031             return 0;
2032         }
2033         PyUnicode_InternInPlace(&str);
2034         PyTuple_SET_ITEM(kwtuple, i, str);
2035     }
2036     parser->kwtuple = kwtuple;
2037 
2038     assert(parser->next == NULL);
2039     parser->next = static_arg_parsers;
2040     static_arg_parsers = parser;
2041     return 1;
2042 }
2043 
2044 static void
parser_clear(struct _PyArg_Parser * parser)2045 parser_clear(struct _PyArg_Parser *parser)
2046 {
2047     Py_CLEAR(parser->kwtuple);
2048 }
2049 
2050 static PyObject*
find_keyword(PyObject * kwnames,PyObject * const * kwstack,PyObject * key)2051 find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
2052 {
2053     Py_ssize_t i, nkwargs;
2054 
2055     nkwargs = PyTuple_GET_SIZE(kwnames);
2056     for (i=0; i < nkwargs; i++) {
2057         PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
2058 
2059         /* ptr==ptr should match in most cases since keyword keys
2060            should be interned strings */
2061         if (kwname == key) {
2062             return kwstack[i];
2063         }
2064         if (!PyUnicode_Check(kwname)) {
2065             /* ignore non-string keyword keys:
2066                an error will be raised below */
2067             continue;
2068         }
2069         if (_PyUnicode_EQ(kwname, key)) {
2070             return kwstack[i];
2071         }
2072     }
2073     return NULL;
2074 }
2075 
2076 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)2077 vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
2078                           PyObject *kwargs, PyObject *kwnames,
2079                           struct _PyArg_Parser *parser,
2080                           va_list *p_va, int flags)
2081 {
2082     PyObject *kwtuple;
2083     char msgbuf[512];
2084     int levels[32];
2085     const char *format;
2086     const char *msg;
2087     PyObject *keyword;
2088     int i, pos, len;
2089     Py_ssize_t nkwargs;
2090     PyObject *current_arg;
2091     freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
2092     freelist_t freelist;
2093     PyObject *const *kwstack = NULL;
2094 
2095     freelist.entries = static_entries;
2096     freelist.first_available = 0;
2097     freelist.entries_malloced = 0;
2098 
2099     assert(kwargs == NULL || PyDict_Check(kwargs));
2100     assert(kwargs == NULL || kwnames == NULL);
2101     assert(p_va != NULL);
2102 
2103     if (parser == NULL) {
2104         PyErr_BadInternalCall();
2105         return 0;
2106     }
2107 
2108     if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2109         PyErr_BadInternalCall();
2110         return 0;
2111     }
2112 
2113     if (!parser_init(parser)) {
2114         return 0;
2115     }
2116 
2117     kwtuple = parser->kwtuple;
2118     pos = parser->pos;
2119     len = pos + (int)PyTuple_GET_SIZE(kwtuple);
2120 
2121     if (len > STATIC_FREELIST_ENTRIES) {
2122         freelist.entries = PyMem_NEW(freelistentry_t, len);
2123         if (freelist.entries == NULL) {
2124             PyErr_NoMemory();
2125             return 0;
2126         }
2127         freelist.entries_malloced = 1;
2128     }
2129 
2130     if (kwargs != NULL) {
2131         nkwargs = PyDict_GET_SIZE(kwargs);
2132     }
2133     else if (kwnames != NULL) {
2134         nkwargs = PyTuple_GET_SIZE(kwnames);
2135         kwstack = args + nargs;
2136     }
2137     else {
2138         nkwargs = 0;
2139     }
2140     if (nargs + nkwargs > len) {
2141         /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2142            messages in some special cases (see bpo-31229). */
2143         PyErr_Format(PyExc_TypeError,
2144                      "%.200s%s takes at most %d %sargument%s (%zd given)",
2145                      (parser->fname == NULL) ? "function" : parser->fname,
2146                      (parser->fname == NULL) ? "" : "()",
2147                      len,
2148                      (nargs == 0) ? "keyword " : "",
2149                      (len == 1) ? "" : "s",
2150                      nargs + nkwargs);
2151         return cleanreturn(0, &freelist);
2152     }
2153     if (parser->max < nargs) {
2154         if (parser->max == 0) {
2155             PyErr_Format(PyExc_TypeError,
2156                          "%.200s%s takes no positional arguments",
2157                          (parser->fname == NULL) ? "function" : parser->fname,
2158                          (parser->fname == NULL) ? "" : "()");
2159         }
2160         else {
2161             PyErr_Format(PyExc_TypeError,
2162                          "%.200s%s takes %s %d positional argument%s (%zd given)",
2163                          (parser->fname == NULL) ? "function" : parser->fname,
2164                          (parser->fname == NULL) ? "" : "()",
2165                          (parser->min < parser->max) ? "at most" : "exactly",
2166                          parser->max,
2167                          parser->max == 1 ? "" : "s",
2168                          nargs);
2169         }
2170         return cleanreturn(0, &freelist);
2171     }
2172 
2173     format = parser->format;
2174     /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
2175     for (i = 0; i < len; i++) {
2176         if (*format == '|') {
2177             format++;
2178         }
2179         if (*format == '$') {
2180             format++;
2181         }
2182         assert(!IS_END_OF_FORMAT(*format));
2183 
2184         if (i < nargs) {
2185             current_arg = args[i];
2186         }
2187         else if (nkwargs && i >= pos) {
2188             keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2189             if (kwargs != NULL) {
2190                 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2191                 if (!current_arg && PyErr_Occurred()) {
2192                     return cleanreturn(0, &freelist);
2193                 }
2194             }
2195             else {
2196                 current_arg = find_keyword(kwnames, kwstack, keyword);
2197             }
2198             if (current_arg) {
2199                 --nkwargs;
2200             }
2201         }
2202         else {
2203             current_arg = NULL;
2204         }
2205 
2206         if (current_arg) {
2207             msg = convertitem(current_arg, &format, p_va, flags,
2208                 levels, msgbuf, sizeof(msgbuf), &freelist);
2209             if (msg) {
2210                 seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2211                 return cleanreturn(0, &freelist);
2212             }
2213             continue;
2214         }
2215 
2216         if (i < parser->min) {
2217             /* Less arguments than required */
2218             if (i < pos) {
2219                 Py_ssize_t min = Py_MIN(pos, parser->min);
2220                 PyErr_Format(PyExc_TypeError,
2221                              "%.200s%s takes %s %d positional argument%s"
2222                              " (%zd given)",
2223                              (parser->fname == NULL) ? "function" : parser->fname,
2224                              (parser->fname == NULL) ? "" : "()",
2225                              min < parser->max ? "at least" : "exactly",
2226                              min,
2227                              min == 1 ? "" : "s",
2228                              nargs);
2229             }
2230             else {
2231                 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2232                 PyErr_Format(PyExc_TypeError,  "%.200s%s missing required "
2233                              "argument '%U' (pos %d)",
2234                              (parser->fname == NULL) ? "function" : parser->fname,
2235                              (parser->fname == NULL) ? "" : "()",
2236                              keyword, i+1);
2237             }
2238             return cleanreturn(0, &freelist);
2239         }
2240         /* current code reports success when all required args
2241          * fulfilled and no keyword args left, with no further
2242          * validation. XXX Maybe skip this in debug build ?
2243          */
2244         if (!nkwargs) {
2245             return cleanreturn(1, &freelist);
2246         }
2247 
2248         /* We are into optional args, skip through to any remaining
2249          * keyword args */
2250         msg = skipitem(&format, p_va, flags);
2251         assert(msg == NULL);
2252     }
2253 
2254     assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2255 
2256     if (nkwargs > 0) {
2257         Py_ssize_t j;
2258         /* make sure there are no arguments given by name and position */
2259         for (i = pos; i < nargs; i++) {
2260             keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2261             if (kwargs != NULL) {
2262                 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2263                 if (!current_arg && PyErr_Occurred()) {
2264                     return cleanreturn(0, &freelist);
2265                 }
2266             }
2267             else {
2268                 current_arg = find_keyword(kwnames, kwstack, keyword);
2269             }
2270             if (current_arg) {
2271                 /* arg present in tuple and in dict */
2272                 PyErr_Format(PyExc_TypeError,
2273                              "argument for %.200s%s given by name ('%U') "
2274                              "and position (%d)",
2275                              (parser->fname == NULL) ? "function" : parser->fname,
2276                              (parser->fname == NULL) ? "" : "()",
2277                              keyword, i+1);
2278                 return cleanreturn(0, &freelist);
2279             }
2280         }
2281         /* make sure there are no extraneous keyword arguments */
2282         j = 0;
2283         while (1) {
2284             int match;
2285             if (kwargs != NULL) {
2286                 if (!PyDict_Next(kwargs, &j, &keyword, NULL))
2287                     break;
2288             }
2289             else {
2290                 if (j >= PyTuple_GET_SIZE(kwnames))
2291                     break;
2292                 keyword = PyTuple_GET_ITEM(kwnames, j);
2293                 j++;
2294             }
2295 
2296             if (!PyUnicode_Check(keyword)) {
2297                 PyErr_SetString(PyExc_TypeError,
2298                                 "keywords must be strings");
2299                 return cleanreturn(0, &freelist);
2300             }
2301             match = PySequence_Contains(kwtuple, keyword);
2302             if (match <= 0) {
2303                 if (!match) {
2304                     PyErr_Format(PyExc_TypeError,
2305                                  "'%U' is an invalid keyword "
2306                                  "argument for %.200s%s",
2307                                  keyword,
2308                                  (parser->fname == NULL) ? "this function" : parser->fname,
2309                                  (parser->fname == NULL) ? "" : "()");
2310                 }
2311                 return cleanreturn(0, &freelist);
2312             }
2313         }
2314     }
2315 
2316     return cleanreturn(1, &freelist);
2317 }
2318 
2319 static int
vgetargskeywordsfast(PyObject * args,PyObject * keywords,struct _PyArg_Parser * parser,va_list * p_va,int flags)2320 vgetargskeywordsfast(PyObject *args, PyObject *keywords,
2321                      struct _PyArg_Parser *parser, va_list *p_va, int flags)
2322 {
2323     PyObject **stack;
2324     Py_ssize_t nargs;
2325 
2326     if (args == NULL
2327         || !PyTuple_Check(args)
2328         || (keywords != NULL && !PyDict_Check(keywords)))
2329     {
2330         PyErr_BadInternalCall();
2331         return 0;
2332     }
2333 
2334     stack = _PyTuple_ITEMS(args);
2335     nargs = PyTuple_GET_SIZE(args);
2336     return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
2337                                      parser, p_va, flags);
2338 }
2339 
2340 
2341 #undef _PyArg_UnpackKeywords
2342 
2343 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)2344 _PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
2345                       PyObject *kwargs, PyObject *kwnames,
2346                       struct _PyArg_Parser *parser,
2347                       int minpos, int maxpos, int minkw,
2348                       PyObject **buf)
2349 {
2350     PyObject *kwtuple;
2351     PyObject *keyword;
2352     int i, posonly, minposonly, maxargs;
2353     int reqlimit = minkw ? maxpos + minkw : minpos;
2354     Py_ssize_t nkwargs;
2355     PyObject *current_arg;
2356     PyObject * const *kwstack = NULL;
2357 
2358     assert(kwargs == NULL || PyDict_Check(kwargs));
2359     assert(kwargs == NULL || kwnames == NULL);
2360 
2361     if (parser == NULL) {
2362         PyErr_BadInternalCall();
2363         return NULL;
2364     }
2365 
2366     if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2367         PyErr_BadInternalCall();
2368         return NULL;
2369     }
2370 
2371     if (args == NULL && nargs == 0) {
2372         args = buf;
2373     }
2374 
2375     if (!parser_init(parser)) {
2376         return NULL;
2377     }
2378 
2379     kwtuple = parser->kwtuple;
2380     posonly = parser->pos;
2381     minposonly = Py_MIN(posonly, minpos);
2382     maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple);
2383 
2384     if (kwargs != NULL) {
2385         nkwargs = PyDict_GET_SIZE(kwargs);
2386     }
2387     else if (kwnames != NULL) {
2388         nkwargs = PyTuple_GET_SIZE(kwnames);
2389         kwstack = args + nargs;
2390     }
2391     else {
2392         nkwargs = 0;
2393     }
2394     if (nkwargs == 0 && minkw == 0 && minpos <= nargs && nargs <= maxpos) {
2395         /* Fast path. */
2396         return args;
2397     }
2398     if (nargs + nkwargs > maxargs) {
2399         /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2400            messages in some special cases (see bpo-31229). */
2401         PyErr_Format(PyExc_TypeError,
2402                      "%.200s%s takes at most %d %sargument%s (%zd given)",
2403                      (parser->fname == NULL) ? "function" : parser->fname,
2404                      (parser->fname == NULL) ? "" : "()",
2405                      maxargs,
2406                      (nargs == 0) ? "keyword " : "",
2407                      (maxargs == 1) ? "" : "s",
2408                      nargs + nkwargs);
2409         return NULL;
2410     }
2411     if (nargs > maxpos) {
2412         if (maxpos == 0) {
2413             PyErr_Format(PyExc_TypeError,
2414                          "%.200s%s takes no positional arguments",
2415                          (parser->fname == NULL) ? "function" : parser->fname,
2416                          (parser->fname == NULL) ? "" : "()");
2417         }
2418         else {
2419             PyErr_Format(PyExc_TypeError,
2420                          "%.200s%s takes %s %d positional argument%s (%zd given)",
2421                          (parser->fname == NULL) ? "function" : parser->fname,
2422                          (parser->fname == NULL) ? "" : "()",
2423                          (minpos < maxpos) ? "at most" : "exactly",
2424                          maxpos,
2425                          (maxpos == 1) ? "" : "s",
2426                          nargs);
2427         }
2428         return NULL;
2429     }
2430     if (nargs < minposonly) {
2431         PyErr_Format(PyExc_TypeError,
2432                      "%.200s%s takes %s %d positional argument%s"
2433                      " (%zd given)",
2434                      (parser->fname == NULL) ? "function" : parser->fname,
2435                      (parser->fname == NULL) ? "" : "()",
2436                      minposonly < maxpos ? "at least" : "exactly",
2437                      minposonly,
2438                      minposonly == 1 ? "" : "s",
2439                      nargs);
2440         return NULL;
2441     }
2442 
2443     /* copy tuple args */
2444     for (i = 0; i < nargs; i++) {
2445         buf[i] = args[i];
2446     }
2447 
2448     /* copy keyword args using kwtuple to drive process */
2449     for (i = Py_MAX((int)nargs, posonly); i < maxargs; i++) {
2450         if (nkwargs) {
2451             keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2452             if (kwargs != NULL) {
2453                 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2454                 if (!current_arg && PyErr_Occurred()) {
2455                     return NULL;
2456                 }
2457             }
2458             else {
2459                 current_arg = find_keyword(kwnames, kwstack, keyword);
2460             }
2461         }
2462         else if (i >= reqlimit) {
2463             break;
2464         }
2465         else {
2466             current_arg = NULL;
2467         }
2468 
2469         buf[i] = current_arg;
2470 
2471         if (current_arg) {
2472             --nkwargs;
2473         }
2474         else if (i < minpos || (maxpos <= i && i < reqlimit)) {
2475             /* Less arguments than required */
2476             keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2477             PyErr_Format(PyExc_TypeError,  "%.200s%s missing required "
2478                          "argument '%U' (pos %d)",
2479                          (parser->fname == NULL) ? "function" : parser->fname,
2480                          (parser->fname == NULL) ? "" : "()",
2481                          keyword, i+1);
2482             return NULL;
2483         }
2484     }
2485 
2486     if (nkwargs > 0) {
2487         Py_ssize_t j;
2488         /* make sure there are no arguments given by name and position */
2489         for (i = posonly; i < nargs; i++) {
2490             keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2491             if (kwargs != NULL) {
2492                 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2493                 if (!current_arg && PyErr_Occurred()) {
2494                     return NULL;
2495                 }
2496             }
2497             else {
2498                 current_arg = find_keyword(kwnames, kwstack, keyword);
2499             }
2500             if (current_arg) {
2501                 /* arg present in tuple and in dict */
2502                 PyErr_Format(PyExc_TypeError,
2503                              "argument for %.200s%s given by name ('%U') "
2504                              "and position (%d)",
2505                              (parser->fname == NULL) ? "function" : parser->fname,
2506                              (parser->fname == NULL) ? "" : "()",
2507                              keyword, i+1);
2508                 return NULL;
2509             }
2510         }
2511         /* make sure there are no extraneous keyword arguments */
2512         j = 0;
2513         while (1) {
2514             int match;
2515             if (kwargs != NULL) {
2516                 if (!PyDict_Next(kwargs, &j, &keyword, NULL))
2517                     break;
2518             }
2519             else {
2520                 if (j >= PyTuple_GET_SIZE(kwnames))
2521                     break;
2522                 keyword = PyTuple_GET_ITEM(kwnames, j);
2523                 j++;
2524             }
2525 
2526             if (!PyUnicode_Check(keyword)) {
2527                 PyErr_SetString(PyExc_TypeError,
2528                                 "keywords must be strings");
2529                 return NULL;
2530             }
2531             match = PySequence_Contains(kwtuple, keyword);
2532             if (match <= 0) {
2533                 if (!match) {
2534                     PyErr_Format(PyExc_TypeError,
2535                                  "'%U' is an invalid keyword "
2536                                  "argument for %.200s%s",
2537                                  keyword,
2538                                  (parser->fname == NULL) ? "this function" : parser->fname,
2539                                  (parser->fname == NULL) ? "" : "()");
2540                 }
2541                 return NULL;
2542             }
2543         }
2544     }
2545 
2546     return buf;
2547 }
2548 
2549 
2550 static const char *
skipitem(const char ** p_format,va_list * p_va,int flags)2551 skipitem(const char **p_format, va_list *p_va, int flags)
2552 {
2553     const char *format = *p_format;
2554     char c = *format++;
2555 
2556     switch (c) {
2557 
2558     /*
2559      * codes that take a single data pointer as an argument
2560      * (the type of the pointer is irrelevant)
2561      */
2562 
2563     case 'b': /* byte -- very short int */
2564     case 'B': /* byte as bitfield */
2565     case 'h': /* short int */
2566     case 'H': /* short int as bitfield */
2567     case 'i': /* int */
2568     case 'I': /* int sized bitfield */
2569     case 'l': /* long int */
2570     case 'k': /* long int sized bitfield */
2571     case 'L': /* long long */
2572     case 'K': /* long long sized bitfield */
2573     case 'n': /* Py_ssize_t */
2574     case 'f': /* float */
2575     case 'd': /* double */
2576     case 'D': /* complex double */
2577     case 'c': /* char */
2578     case 'C': /* unicode char */
2579     case 'p': /* boolean predicate */
2580     case 'S': /* string object */
2581     case 'Y': /* string object */
2582     case 'U': /* unicode string object */
2583         {
2584             if (p_va != NULL) {
2585                 (void) va_arg(*p_va, void *);
2586             }
2587             break;
2588         }
2589 
2590     /* string codes */
2591 
2592     case 'e': /* string with encoding */
2593         {
2594             if (p_va != NULL) {
2595                 (void) va_arg(*p_va, const char *);
2596             }
2597             if (!(*format == 's' || *format == 't'))
2598                 /* after 'e', only 's' and 't' is allowed */
2599                 goto err;
2600             format++;
2601         }
2602         /* fall through */
2603 
2604     case 's': /* string */
2605     case 'z': /* string or None */
2606     case 'y': /* bytes */
2607     case 'u': /* unicode string */
2608     case 'Z': /* unicode string or None */
2609     case 'w': /* buffer, read-write */
2610         {
2611             if (p_va != NULL) {
2612                 (void) va_arg(*p_va, char **);
2613             }
2614             if (*format == '#') {
2615                 if (p_va != NULL) {
2616                     if (flags & FLAG_SIZE_T)
2617                         (void) va_arg(*p_va, Py_ssize_t *);
2618                     else {
2619                         if (PyErr_WarnEx(PyExc_DeprecationWarning,
2620                                     "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
2621                             return NULL;
2622                         }
2623                         (void) va_arg(*p_va, int *);
2624                     }
2625                 }
2626                 format++;
2627             } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w')
2628                        && *format == '*')
2629             {
2630                 format++;
2631             }
2632             break;
2633         }
2634 
2635     case 'O': /* object */
2636         {
2637             if (*format == '!') {
2638                 format++;
2639                 if (p_va != NULL) {
2640                     (void) va_arg(*p_va, PyTypeObject*);
2641                     (void) va_arg(*p_va, PyObject **);
2642                 }
2643             }
2644             else if (*format == '&') {
2645                 typedef int (*converter)(PyObject *, void *);
2646                 if (p_va != NULL) {
2647                     (void) va_arg(*p_va, converter);
2648                     (void) va_arg(*p_va, void *);
2649                 }
2650                 format++;
2651             }
2652             else {
2653                 if (p_va != NULL) {
2654                     (void) va_arg(*p_va, PyObject **);
2655                 }
2656             }
2657             break;
2658         }
2659 
2660     case '(':           /* bypass tuple, not handled at all previously */
2661         {
2662             const char *msg;
2663             for (;;) {
2664                 if (*format==')')
2665                     break;
2666                 if (IS_END_OF_FORMAT(*format))
2667                     return "Unmatched left paren in format "
2668                            "string";
2669                 msg = skipitem(&format, p_va, flags);
2670                 if (msg)
2671                     return msg;
2672             }
2673             format++;
2674             break;
2675         }
2676 
2677     case ')':
2678         return "Unmatched right paren in format string";
2679 
2680     default:
2681 err:
2682         return "impossible<bad format char>";
2683 
2684     }
2685 
2686     *p_format = format;
2687     return NULL;
2688 }
2689 
2690 
2691 #undef _PyArg_CheckPositional
2692 
2693 int
_PyArg_CheckPositional(const char * name,Py_ssize_t nargs,Py_ssize_t min,Py_ssize_t max)2694 _PyArg_CheckPositional(const char *name, Py_ssize_t nargs,
2695                        Py_ssize_t min, Py_ssize_t max)
2696 {
2697     assert(min >= 0);
2698     assert(min <= max);
2699 
2700     if (nargs < min) {
2701         if (name != NULL)
2702             PyErr_Format(
2703                 PyExc_TypeError,
2704                 "%.200s expected %s%zd argument%s, got %zd",
2705                 name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
2706         else
2707             PyErr_Format(
2708                 PyExc_TypeError,
2709                 "unpacked tuple should have %s%zd element%s,"
2710                 " but has %zd",
2711                 (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
2712         return 0;
2713     }
2714 
2715     if (nargs == 0) {
2716         return 1;
2717     }
2718 
2719     if (nargs > max) {
2720         if (name != NULL)
2721             PyErr_Format(
2722                 PyExc_TypeError,
2723                 "%.200s expected %s%zd argument%s, got %zd",
2724                 name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
2725         else
2726             PyErr_Format(
2727                 PyExc_TypeError,
2728                 "unpacked tuple should have %s%zd element%s,"
2729                 " but has %zd",
2730                 (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
2731         return 0;
2732     }
2733 
2734     return 1;
2735 }
2736 
2737 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)2738 unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name,
2739              Py_ssize_t min, Py_ssize_t max, va_list vargs)
2740 {
2741     Py_ssize_t i;
2742     PyObject **o;
2743 
2744     if (!_PyArg_CheckPositional(name, nargs, min, max)) {
2745         return 0;
2746     }
2747 
2748     for (i = 0; i < nargs; i++) {
2749         o = va_arg(vargs, PyObject **);
2750         *o = args[i];
2751     }
2752     return 1;
2753 }
2754 
2755 int
PyArg_UnpackTuple(PyObject * args,const char * name,Py_ssize_t min,Py_ssize_t max,...)2756 PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
2757 {
2758     PyObject **stack;
2759     Py_ssize_t nargs;
2760     int retval;
2761     va_list vargs;
2762 
2763     if (!PyTuple_Check(args)) {
2764         PyErr_SetString(PyExc_SystemError,
2765             "PyArg_UnpackTuple() argument list is not a tuple");
2766         return 0;
2767     }
2768     stack = _PyTuple_ITEMS(args);
2769     nargs = PyTuple_GET_SIZE(args);
2770 
2771 #ifdef HAVE_STDARG_PROTOTYPES
2772     va_start(vargs, max);
2773 #else
2774     va_start(vargs);
2775 #endif
2776     retval = unpack_stack(stack, nargs, name, min, max, vargs);
2777     va_end(vargs);
2778     return retval;
2779 }
2780 
2781 int
_PyArg_UnpackStack(PyObject * const * args,Py_ssize_t nargs,const char * name,Py_ssize_t min,Py_ssize_t max,...)2782 _PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
2783                    Py_ssize_t min, Py_ssize_t max, ...)
2784 {
2785     int retval;
2786     va_list vargs;
2787 
2788 #ifdef HAVE_STDARG_PROTOTYPES
2789     va_start(vargs, max);
2790 #else
2791     va_start(vargs);
2792 #endif
2793     retval = unpack_stack(args, nargs, name, min, max, vargs);
2794     va_end(vargs);
2795     return retval;
2796 }
2797 
2798 
2799 #undef _PyArg_NoKeywords
2800 #undef _PyArg_NoPositional
2801 
2802 /* For type constructors that don't take keyword args
2803  *
2804  * Sets a TypeError and returns 0 if the args/kwargs is
2805  * not empty, returns 1 otherwise
2806  */
2807 int
_PyArg_NoKeywords(const char * funcname,PyObject * kwargs)2808 _PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
2809 {
2810     if (kwargs == NULL) {
2811         return 1;
2812     }
2813     if (!PyDict_CheckExact(kwargs)) {
2814         PyErr_BadInternalCall();
2815         return 0;
2816     }
2817     if (PyDict_GET_SIZE(kwargs) == 0) {
2818         return 1;
2819     }
2820 
2821     PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
2822                     funcname);
2823     return 0;
2824 }
2825 
2826 
2827 int
_PyArg_NoPositional(const char * funcname,PyObject * args)2828 _PyArg_NoPositional(const char *funcname, PyObject *args)
2829 {
2830     if (args == NULL)
2831         return 1;
2832     if (!PyTuple_CheckExact(args)) {
2833         PyErr_BadInternalCall();
2834         return 0;
2835     }
2836     if (PyTuple_GET_SIZE(args) == 0)
2837         return 1;
2838 
2839     PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
2840                     funcname);
2841     return 0;
2842 }
2843 
2844 void
_PyArg_Fini(void)2845 _PyArg_Fini(void)
2846 {
2847     struct _PyArg_Parser *tmp, *s = static_arg_parsers;
2848     while (s) {
2849         tmp = s->next;
2850         s->next = NULL;
2851         parser_clear(s);
2852         s = tmp;
2853     }
2854     static_arg_parsers = NULL;
2855 }
2856 
2857 #ifdef __cplusplus
2858 };
2859 #endif
2860