Issue #7632: Fix a serious wrong output bug for string -> float conversion.
[python.git] / Python / getargs.c
blob287b5d9be8a637935e5dd49f9a9db574c070c3a2
2 /* New getargs implementation */
4 #include "Python.h"
6 #include <ctype.h>
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 int PyArg_Parse(PyObject *, const char *, ...);
13 int PyArg_ParseTuple(PyObject *, const char *, ...);
14 int PyArg_VaParse(PyObject *, const char *, va_list);
16 int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
17 const char *, char **, ...);
18 int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
19 const char *, char **, va_list);
21 #ifdef HAVE_DECLSPEC_DLL
22 /* Export functions */
23 PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, char *, ...);
24 PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, char *, ...);
25 PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
26 const char *, char **, ...);
27 PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
28 PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, char *, va_list);
29 PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
30 const char *, char **, va_list);
31 #endif
33 #define FLAG_COMPAT 1
34 #define FLAG_SIZE_T 2
37 /* Forward */
38 static int vgetargs1(PyObject *, const char *, va_list *, int);
39 static void seterror(int, const char *, int *, const char *, const char *);
40 static char *convertitem(PyObject *, const char **, va_list *, int, int *,
41 char *, size_t, PyObject **);
42 static char *converttuple(PyObject *, const char **, va_list *, int,
43 int *, char *, size_t, int, PyObject **);
44 static char *convertsimple(PyObject *, const char **, va_list *, int, char *,
45 size_t, PyObject **);
46 static Py_ssize_t convertbuffer(PyObject *, void **p, char **);
47 static int getbuffer(PyObject *, Py_buffer *, char**);
49 static int vgetargskeywords(PyObject *, PyObject *,
50 const char *, char **, va_list *, int);
51 static char *skipitem(const char **, va_list *, int);
53 int
54 PyArg_Parse(PyObject *args, const char *format, ...)
56 int retval;
57 va_list va;
59 va_start(va, format);
60 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
61 va_end(va);
62 return retval;
65 int
66 _PyArg_Parse_SizeT(PyObject *args, char *format, ...)
68 int retval;
69 va_list va;
71 va_start(va, format);
72 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
73 va_end(va);
74 return retval;
78 int
79 PyArg_ParseTuple(PyObject *args, const char *format, ...)
81 int retval;
82 va_list va;
84 va_start(va, format);
85 retval = vgetargs1(args, format, &va, 0);
86 va_end(va);
87 return retval;
90 int
91 _PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
93 int retval;
94 va_list va;
96 va_start(va, format);
97 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
98 va_end(va);
99 return retval;
104 PyArg_VaParse(PyObject *args, const char *format, va_list va)
106 va_list lva;
108 #ifdef VA_LIST_IS_ARRAY
109 memcpy(lva, va, sizeof(va_list));
110 #else
111 #ifdef __va_copy
112 __va_copy(lva, va);
113 #else
114 lva = va;
115 #endif
116 #endif
118 return vgetargs1(args, format, &lva, 0);
122 _PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
124 va_list lva;
126 #ifdef VA_LIST_IS_ARRAY
127 memcpy(lva, va, sizeof(va_list));
128 #else
129 #ifdef __va_copy
130 __va_copy(lva, va);
131 #else
132 lva = va;
133 #endif
134 #endif
136 return vgetargs1(args, format, &lva, FLAG_SIZE_T);
140 /* Handle cleanup of allocated memory in case of exception */
142 static void
143 cleanup_ptr(void *ptr)
145 PyMem_FREE(ptr);
148 static void
149 cleanup_buffer(void *ptr)
151 PyBuffer_Release((Py_buffer *) ptr);
154 static int
155 addcleanup(void *ptr, PyObject **freelist, void (*destr)(void *))
157 PyObject *cobj;
158 if (!*freelist) {
159 *freelist = PyList_New(0);
160 if (!*freelist) {
161 destr(ptr);
162 return -1;
165 cobj = PyCObject_FromVoidPtr(ptr, destr);
166 if (!cobj) {
167 destr(ptr);
168 return -1;
170 if (PyList_Append(*freelist, cobj)) {
171 Py_DECREF(cobj);
172 return -1;
174 Py_DECREF(cobj);
175 return 0;
178 static int
179 cleanreturn(int retval, PyObject *freelist)
181 if (freelist && retval != 0) {
182 /* We were successful, reset the destructors so that they
183 don't get called. */
184 Py_ssize_t len = PyList_GET_SIZE(freelist), i;
185 for (i = 0; i < len; i++)
186 ((PyCObject *) PyList_GET_ITEM(freelist, i))
187 ->destructor = NULL;
189 Py_XDECREF(freelist);
190 return retval;
194 static int
195 vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
197 char msgbuf[256];
198 int levels[32];
199 const char *fname = NULL;
200 const char *message = NULL;
201 int min = -1;
202 int max = 0;
203 int level = 0;
204 int endfmt = 0;
205 const char *formatsave = format;
206 Py_ssize_t i, len;
207 char *msg;
208 PyObject *freelist = NULL;
209 int compat = flags & FLAG_COMPAT;
211 assert(compat || (args != (PyObject*)NULL));
212 flags = flags & ~FLAG_COMPAT;
214 while (endfmt == 0) {
215 int c = *format++;
216 switch (c) {
217 case '(':
218 if (level == 0)
219 max++;
220 level++;
221 if (level >= 30)
222 Py_FatalError("too many tuple nesting levels "
223 "in argument format string");
224 break;
225 case ')':
226 if (level == 0)
227 Py_FatalError("excess ')' in getargs format");
228 else
229 level--;
230 break;
231 case '\0':
232 endfmt = 1;
233 break;
234 case ':':
235 fname = format;
236 endfmt = 1;
237 break;
238 case ';':
239 message = format;
240 endfmt = 1;
241 break;
242 default:
243 if (level == 0) {
244 if (c == 'O')
245 max++;
246 else if (isalpha(Py_CHARMASK(c))) {
247 if (c != 'e') /* skip encoded */
248 max++;
249 } else if (c == '|')
250 min = max;
252 break;
256 if (level != 0)
257 Py_FatalError(/* '(' */ "missing ')' in getargs format");
259 if (min < 0)
260 min = max;
262 format = formatsave;
264 if (compat) {
265 if (max == 0) {
266 if (args == NULL)
267 return 1;
268 PyOS_snprintf(msgbuf, sizeof(msgbuf),
269 "%.200s%s takes no arguments",
270 fname==NULL ? "function" : fname,
271 fname==NULL ? "" : "()");
272 PyErr_SetString(PyExc_TypeError, msgbuf);
273 return 0;
275 else if (min == 1 && max == 1) {
276 if (args == NULL) {
277 PyOS_snprintf(msgbuf, sizeof(msgbuf),
278 "%.200s%s takes at least one argument",
279 fname==NULL ? "function" : fname,
280 fname==NULL ? "" : "()");
281 PyErr_SetString(PyExc_TypeError, msgbuf);
282 return 0;
284 msg = convertitem(args, &format, p_va, flags, levels,
285 msgbuf, sizeof(msgbuf), &freelist);
286 if (msg == NULL)
287 return cleanreturn(1, freelist);
288 seterror(levels[0], msg, levels+1, fname, message);
289 return cleanreturn(0, freelist);
291 else {
292 PyErr_SetString(PyExc_SystemError,
293 "old style getargs format uses new features");
294 return 0;
298 if (!PyTuple_Check(args)) {
299 PyErr_SetString(PyExc_SystemError,
300 "new style getargs format but argument is not a tuple");
301 return 0;
304 len = PyTuple_GET_SIZE(args);
306 if (len < min || max < len) {
307 if (message == NULL) {
308 PyOS_snprintf(msgbuf, sizeof(msgbuf),
309 "%.150s%s takes %s %d argument%s "
310 "(%ld given)",
311 fname==NULL ? "function" : fname,
312 fname==NULL ? "" : "()",
313 min==max ? "exactly"
314 : len < min ? "at least" : "at most",
315 len < min ? min : max,
316 (len < min ? min : max) == 1 ? "" : "s",
317 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
318 message = msgbuf;
320 PyErr_SetString(PyExc_TypeError, message);
321 return 0;
324 for (i = 0; i < len; i++) {
325 if (*format == '|')
326 format++;
327 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
328 flags, levels, msgbuf,
329 sizeof(msgbuf), &freelist);
330 if (msg) {
331 seterror(i+1, msg, levels, fname, msg);
332 return cleanreturn(0, freelist);
336 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
337 *format != '(' &&
338 *format != '|' && *format != ':' && *format != ';') {
339 PyErr_Format(PyExc_SystemError,
340 "bad format string: %.200s", formatsave);
341 return cleanreturn(0, freelist);
344 return cleanreturn(1, freelist);
349 static void
350 seterror(int iarg, const char *msg, int *levels, const char *fname,
351 const char *message)
353 char buf[512];
354 int i;
355 char *p = buf;
357 if (PyErr_Occurred())
358 return;
359 else if (message == NULL) {
360 if (fname != NULL) {
361 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
362 p += strlen(p);
364 if (iarg != 0) {
365 PyOS_snprintf(p, sizeof(buf) - (p - buf),
366 "argument %d", iarg);
367 i = 0;
368 p += strlen(p);
369 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
370 PyOS_snprintf(p, sizeof(buf) - (p - buf),
371 ", item %d", levels[i]-1);
372 p += strlen(p);
373 i++;
376 else {
377 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
378 p += strlen(p);
380 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
381 message = buf;
383 PyErr_SetString(PyExc_TypeError, message);
387 /* Convert a tuple argument.
388 On entry, *p_format points to the character _after_ the opening '('.
389 On successful exit, *p_format points to the closing ')'.
390 If successful:
391 *p_format and *p_va are updated,
392 *levels and *msgbuf are untouched,
393 and NULL is returned.
394 If the argument is invalid:
395 *p_format is unchanged,
396 *p_va is undefined,
397 *levels is a 0-terminated list of item numbers,
398 *msgbuf contains an error message, whose format is:
399 "must be <typename1>, not <typename2>", where:
400 <typename1> is the name of the expected type, and
401 <typename2> is the name of the actual type,
402 and msgbuf is returned.
405 static char *
406 converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
407 int *levels, char *msgbuf, size_t bufsize, int toplevel,
408 PyObject **freelist)
410 int level = 0;
411 int n = 0;
412 const char *format = *p_format;
413 int i;
415 for (;;) {
416 int c = *format++;
417 if (c == '(') {
418 if (level == 0)
419 n++;
420 level++;
422 else if (c == ')') {
423 if (level == 0)
424 break;
425 level--;
427 else if (c == ':' || c == ';' || c == '\0')
428 break;
429 else if (level == 0 && isalpha(Py_CHARMASK(c)))
430 n++;
433 if (!PySequence_Check(arg) || PyString_Check(arg)) {
434 levels[0] = 0;
435 PyOS_snprintf(msgbuf, bufsize,
436 toplevel ? "expected %d arguments, not %.50s" :
437 "must be %d-item sequence, not %.50s",
439 arg == Py_None ? "None" : arg->ob_type->tp_name);
440 return msgbuf;
443 if ((i = PySequence_Size(arg)) != n) {
444 levels[0] = 0;
445 PyOS_snprintf(msgbuf, bufsize,
446 toplevel ? "expected %d arguments, not %d" :
447 "must be sequence of length %d, not %d",
448 n, i);
449 return msgbuf;
452 format = *p_format;
453 for (i = 0; i < n; i++) {
454 char *msg;
455 PyObject *item;
456 item = PySequence_GetItem(arg, i);
457 if (item == NULL) {
458 PyErr_Clear();
459 levels[0] = i+1;
460 levels[1] = 0;
461 strncpy(msgbuf, "is not retrievable", bufsize);
462 return msgbuf;
464 msg = convertitem(item, &format, p_va, flags, levels+1,
465 msgbuf, bufsize, freelist);
466 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
467 Py_XDECREF(item);
468 if (msg != NULL) {
469 levels[0] = i+1;
470 return msg;
474 *p_format = format;
475 return NULL;
479 /* Convert a single item. */
481 static char *
482 convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
483 int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
485 char *msg;
486 const char *format = *p_format;
488 if (*format == '(' /* ')' */) {
489 format++;
490 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
491 bufsize, 0, freelist);
492 if (msg == NULL)
493 format++;
495 else {
496 msg = convertsimple(arg, &format, p_va, flags,
497 msgbuf, bufsize, freelist);
498 if (msg != NULL)
499 levels[0] = 0;
501 if (msg == NULL)
502 *p_format = format;
503 return msg;
508 #define UNICODE_DEFAULT_ENCODING(arg) \
509 _PyUnicode_AsDefaultEncodedString(arg, NULL)
511 /* Format an error message generated by convertsimple(). */
513 static char *
514 converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
516 assert(expected != NULL);
517 assert(arg != NULL);
518 PyOS_snprintf(msgbuf, bufsize,
519 "must be %.50s, not %.50s", expected,
520 arg == Py_None ? "None" : arg->ob_type->tp_name);
521 return msgbuf;
524 #define CONV_UNICODE "(unicode conversion error)"
526 /* explicitly check for float arguments when integers are expected. For now
527 * signal a warning. Returns true if an exception was raised. */
528 static int
529 float_argument_warning(PyObject *arg)
531 if (PyFloat_Check(arg) &&
532 PyErr_Warn(PyExc_DeprecationWarning,
533 "integer argument expected, got float" ))
534 return 1;
535 else
536 return 0;
539 /* explicitly check for float arguments when integers are expected. Raises
540 TypeError and returns true for float arguments. */
541 static int
542 float_argument_error(PyObject *arg)
544 if (PyFloat_Check(arg)) {
545 PyErr_SetString(PyExc_TypeError,
546 "integer argument expected, got float");
547 return 1;
549 else
550 return 0;
553 /* Convert a non-tuple argument. Return NULL if conversion went OK,
554 or a string with a message describing the failure. The message is
555 formatted as "must be <desired type>, not <actual type>".
556 When failing, an exception may or may not have been raised.
557 Don't call if a tuple is expected.
559 When you add new format codes, please don't forget poor skipitem() below.
562 static char *
563 convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
564 char *msgbuf, size_t bufsize, PyObject **freelist)
566 /* For # codes */
567 #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
568 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
569 else q=va_arg(*p_va, int*);
570 #define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
571 #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
573 const char *format = *p_format;
574 char c = *format++;
575 #ifdef Py_USING_UNICODE
576 PyObject *uarg;
577 #endif
579 switch (c) {
581 case 'b': { /* unsigned byte -- very short int */
582 char *p = va_arg(*p_va, char *);
583 long ival;
584 if (float_argument_error(arg))
585 return converterr("integer<b>", arg, msgbuf, bufsize);
586 ival = PyInt_AsLong(arg);
587 if (ival == -1 && PyErr_Occurred())
588 return converterr("integer<b>", arg, msgbuf, bufsize);
589 else if (ival < 0) {
590 PyErr_SetString(PyExc_OverflowError,
591 "unsigned byte integer is less than minimum");
592 return converterr("integer<b>", arg, msgbuf, bufsize);
594 else if (ival > UCHAR_MAX) {
595 PyErr_SetString(PyExc_OverflowError,
596 "unsigned byte integer is greater than maximum");
597 return converterr("integer<b>", arg, msgbuf, bufsize);
599 else
600 *p = (unsigned char) ival;
601 break;
604 case 'B': {/* byte sized bitfield - both signed and unsigned
605 values allowed */
606 char *p = va_arg(*p_va, char *);
607 long ival;
608 if (float_argument_error(arg))
609 return converterr("integer<B>", arg, msgbuf, bufsize);
610 ival = PyInt_AsUnsignedLongMask(arg);
611 if (ival == -1 && PyErr_Occurred())
612 return converterr("integer<B>", arg, msgbuf, bufsize);
613 else
614 *p = (unsigned char) ival;
615 break;
618 case 'h': {/* signed short int */
619 short *p = va_arg(*p_va, short *);
620 long ival;
621 if (float_argument_error(arg))
622 return converterr("integer<h>", arg, msgbuf, bufsize);
623 ival = PyInt_AsLong(arg);
624 if (ival == -1 && PyErr_Occurred())
625 return converterr("integer<h>", arg, msgbuf, bufsize);
626 else if (ival < SHRT_MIN) {
627 PyErr_SetString(PyExc_OverflowError,
628 "signed short integer is less than minimum");
629 return converterr("integer<h>", arg, msgbuf, bufsize);
631 else if (ival > SHRT_MAX) {
632 PyErr_SetString(PyExc_OverflowError,
633 "signed short integer is greater than maximum");
634 return converterr("integer<h>", arg, msgbuf, bufsize);
636 else
637 *p = (short) ival;
638 break;
641 case 'H': { /* short int sized bitfield, both signed and
642 unsigned allowed */
643 unsigned short *p = va_arg(*p_va, unsigned short *);
644 long ival;
645 if (float_argument_error(arg))
646 return converterr("integer<H>", arg, msgbuf, bufsize);
647 ival = PyInt_AsUnsignedLongMask(arg);
648 if (ival == -1 && PyErr_Occurred())
649 return converterr("integer<H>", arg, msgbuf, bufsize);
650 else
651 *p = (unsigned short) ival;
652 break;
655 case 'i': {/* signed int */
656 int *p = va_arg(*p_va, int *);
657 long ival;
658 if (float_argument_error(arg))
659 return converterr("integer<i>", arg, msgbuf, bufsize);
660 ival = PyInt_AsLong(arg);
661 if (ival == -1 && PyErr_Occurred())
662 return converterr("integer<i>", arg, msgbuf, bufsize);
663 else if (ival > INT_MAX) {
664 PyErr_SetString(PyExc_OverflowError,
665 "signed integer is greater than maximum");
666 return converterr("integer<i>", arg, msgbuf, bufsize);
668 else if (ival < INT_MIN) {
669 PyErr_SetString(PyExc_OverflowError,
670 "signed integer is less than minimum");
671 return converterr("integer<i>", arg, msgbuf, bufsize);
673 else
674 *p = ival;
675 break;
678 case 'I': { /* int sized bitfield, both signed and
679 unsigned allowed */
680 unsigned int *p = va_arg(*p_va, unsigned int *);
681 unsigned int ival;
682 if (float_argument_error(arg))
683 return converterr("integer<I>", arg, msgbuf, bufsize);
684 ival = (unsigned int)PyInt_AsUnsignedLongMask(arg);
685 if (ival == (unsigned int)-1 && PyErr_Occurred())
686 return converterr("integer<I>", arg, msgbuf, bufsize);
687 else
688 *p = ival;
689 break;
692 case 'n': /* Py_ssize_t */
693 #if SIZEOF_SIZE_T != SIZEOF_LONG
695 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
696 Py_ssize_t ival;
697 if (float_argument_error(arg))
698 return converterr("integer<n>", arg, msgbuf, bufsize);
699 ival = PyInt_AsSsize_t(arg);
700 if (ival == -1 && PyErr_Occurred())
701 return converterr("integer<n>", arg, msgbuf, bufsize);
702 *p = ival;
703 break;
705 #endif
706 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
707 case 'l': {/* long int */
708 long *p = va_arg(*p_va, long *);
709 long ival;
710 if (float_argument_error(arg))
711 return converterr("integer<l>", arg, msgbuf, bufsize);
712 ival = PyInt_AsLong(arg);
713 if (ival == -1 && PyErr_Occurred())
714 return converterr("integer<l>", arg, msgbuf, bufsize);
715 else
716 *p = ival;
717 break;
720 case 'k': { /* long sized bitfield */
721 unsigned long *p = va_arg(*p_va, unsigned long *);
722 unsigned long ival;
723 if (PyInt_Check(arg))
724 ival = PyInt_AsUnsignedLongMask(arg);
725 else if (PyLong_Check(arg))
726 ival = PyLong_AsUnsignedLongMask(arg);
727 else
728 return converterr("integer<k>", arg, msgbuf, bufsize);
729 *p = ival;
730 break;
733 #ifdef HAVE_LONG_LONG
734 case 'L': {/* PY_LONG_LONG */
735 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
736 PY_LONG_LONG ival;
737 if (float_argument_warning(arg))
738 return converterr("long<L>", arg, msgbuf, bufsize);
739 ival = PyLong_AsLongLong(arg);
740 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
741 return converterr("long<L>", arg, msgbuf, bufsize);
742 } else {
743 *p = ival;
745 break;
748 case 'K': { /* long long sized bitfield */
749 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
750 unsigned PY_LONG_LONG ival;
751 if (PyInt_Check(arg))
752 ival = PyInt_AsUnsignedLongMask(arg);
753 else if (PyLong_Check(arg))
754 ival = PyLong_AsUnsignedLongLongMask(arg);
755 else
756 return converterr("integer<K>", arg, msgbuf, bufsize);
757 *p = ival;
758 break;
760 #endif
762 case 'f': {/* float */
763 float *p = va_arg(*p_va, float *);
764 double dval = PyFloat_AsDouble(arg);
765 if (PyErr_Occurred())
766 return converterr("float<f>", arg, msgbuf, bufsize);
767 else
768 *p = (float) dval;
769 break;
772 case 'd': {/* double */
773 double *p = va_arg(*p_va, double *);
774 double dval = PyFloat_AsDouble(arg);
775 if (PyErr_Occurred())
776 return converterr("float<d>", arg, msgbuf, bufsize);
777 else
778 *p = dval;
779 break;
782 #ifndef WITHOUT_COMPLEX
783 case 'D': {/* complex double */
784 Py_complex *p = va_arg(*p_va, Py_complex *);
785 Py_complex cval;
786 cval = PyComplex_AsCComplex(arg);
787 if (PyErr_Occurred())
788 return converterr("complex<D>", arg, msgbuf, bufsize);
789 else
790 *p = cval;
791 break;
793 #endif /* WITHOUT_COMPLEX */
795 case 'c': {/* char */
796 char *p = va_arg(*p_va, char *);
797 if (PyString_Check(arg) && PyString_Size(arg) == 1)
798 *p = PyString_AS_STRING(arg)[0];
799 else
800 return converterr("char", arg, msgbuf, bufsize);
801 break;
804 case 's': {/* string */
805 if (*format == '*') {
806 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
808 if (PyString_Check(arg)) {
809 PyBuffer_FillInfo(p, arg,
810 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
811 1, 0);
813 #ifdef Py_USING_UNICODE
814 else if (PyUnicode_Check(arg)) {
815 uarg = UNICODE_DEFAULT_ENCODING(arg);
816 if (uarg == NULL)
817 return converterr(CONV_UNICODE,
818 arg, msgbuf, bufsize);
819 PyBuffer_FillInfo(p, arg,
820 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
821 1, 0);
823 #endif
824 else { /* any buffer-like object */
825 char *buf;
826 if (getbuffer(arg, p, &buf) < 0)
827 return converterr(buf, arg, msgbuf, bufsize);
829 if (addcleanup(p, freelist, cleanup_buffer)) {
830 return converterr(
831 "(cleanup problem)",
832 arg, msgbuf, bufsize);
834 format++;
835 } else if (*format == '#') {
836 void **p = (void **)va_arg(*p_va, char **);
837 FETCH_SIZE;
839 if (PyString_Check(arg)) {
840 *p = PyString_AS_STRING(arg);
841 STORE_SIZE(PyString_GET_SIZE(arg));
843 #ifdef Py_USING_UNICODE
844 else if (PyUnicode_Check(arg)) {
845 uarg = UNICODE_DEFAULT_ENCODING(arg);
846 if (uarg == NULL)
847 return converterr(CONV_UNICODE,
848 arg, msgbuf, bufsize);
849 *p = PyString_AS_STRING(uarg);
850 STORE_SIZE(PyString_GET_SIZE(uarg));
852 #endif
853 else { /* any buffer-like object */
854 char *buf;
855 Py_ssize_t count = convertbuffer(arg, p, &buf);
856 if (count < 0)
857 return converterr(buf, arg, msgbuf, bufsize);
858 STORE_SIZE(count);
860 format++;
861 } else {
862 char **p = va_arg(*p_va, char **);
864 if (PyString_Check(arg))
865 *p = PyString_AS_STRING(arg);
866 #ifdef Py_USING_UNICODE
867 else if (PyUnicode_Check(arg)) {
868 uarg = UNICODE_DEFAULT_ENCODING(arg);
869 if (uarg == NULL)
870 return converterr(CONV_UNICODE,
871 arg, msgbuf, bufsize);
872 *p = PyString_AS_STRING(uarg);
874 #endif
875 else
876 return converterr("string", arg, msgbuf, bufsize);
877 if ((Py_ssize_t)strlen(*p) != PyString_Size(arg))
878 return converterr("string without null bytes",
879 arg, msgbuf, bufsize);
881 break;
884 case 'z': {/* string, may be NULL (None) */
885 if (*format == '*') {
886 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
888 if (arg == Py_None)
889 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
890 else if (PyString_Check(arg)) {
891 PyBuffer_FillInfo(p, arg,
892 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
893 1, 0);
895 #ifdef Py_USING_UNICODE
896 else if (PyUnicode_Check(arg)) {
897 uarg = UNICODE_DEFAULT_ENCODING(arg);
898 if (uarg == NULL)
899 return converterr(CONV_UNICODE,
900 arg, msgbuf, bufsize);
901 PyBuffer_FillInfo(p, arg,
902 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
903 1, 0);
905 #endif
906 else { /* any buffer-like object */
907 char *buf;
908 if (getbuffer(arg, p, &buf) < 0)
909 return converterr(buf, arg, msgbuf, bufsize);
911 if (addcleanup(p, freelist, cleanup_buffer)) {
912 return converterr(
913 "(cleanup problem)",
914 arg, msgbuf, bufsize);
916 format++;
917 } else if (*format == '#') { /* any buffer-like object */
918 void **p = (void **)va_arg(*p_va, char **);
919 FETCH_SIZE;
921 if (arg == Py_None) {
922 *p = 0;
923 STORE_SIZE(0);
925 else if (PyString_Check(arg)) {
926 *p = PyString_AS_STRING(arg);
927 STORE_SIZE(PyString_GET_SIZE(arg));
929 #ifdef Py_USING_UNICODE
930 else if (PyUnicode_Check(arg)) {
931 uarg = UNICODE_DEFAULT_ENCODING(arg);
932 if (uarg == NULL)
933 return converterr(CONV_UNICODE,
934 arg, msgbuf, bufsize);
935 *p = PyString_AS_STRING(uarg);
936 STORE_SIZE(PyString_GET_SIZE(uarg));
938 #endif
939 else { /* any buffer-like object */
940 char *buf;
941 Py_ssize_t count = convertbuffer(arg, p, &buf);
942 if (count < 0)
943 return converterr(buf, arg, msgbuf, bufsize);
944 STORE_SIZE(count);
946 format++;
947 } else {
948 char **p = va_arg(*p_va, char **);
950 if (arg == Py_None)
951 *p = 0;
952 else if (PyString_Check(arg))
953 *p = PyString_AS_STRING(arg);
954 #ifdef Py_USING_UNICODE
955 else if (PyUnicode_Check(arg)) {
956 uarg = UNICODE_DEFAULT_ENCODING(arg);
957 if (uarg == NULL)
958 return converterr(CONV_UNICODE,
959 arg, msgbuf, bufsize);
960 *p = PyString_AS_STRING(uarg);
962 #endif
963 else
964 return converterr("string or None",
965 arg, msgbuf, bufsize);
966 if (*format == '#') {
967 FETCH_SIZE;
968 assert(0); /* XXX redundant with if-case */
969 if (arg == Py_None)
970 *q = 0;
971 else
972 *q = PyString_Size(arg);
973 format++;
975 else if (*p != NULL &&
976 (Py_ssize_t)strlen(*p) != PyString_Size(arg))
977 return converterr(
978 "string without null bytes or None",
979 arg, msgbuf, bufsize);
981 break;
984 case 'e': {/* encoded string */
985 char **buffer;
986 const char *encoding;
987 PyObject *s;
988 Py_ssize_t size;
989 int recode_strings;
991 /* Get 'e' parameter: the encoding name */
992 encoding = (const char *)va_arg(*p_va, const char *);
993 #ifdef Py_USING_UNICODE
994 if (encoding == NULL)
995 encoding = PyUnicode_GetDefaultEncoding();
996 #endif
998 /* Get output buffer parameter:
999 's' (recode all objects via Unicode) or
1000 't' (only recode non-string objects)
1002 if (*format == 's')
1003 recode_strings = 1;
1004 else if (*format == 't')
1005 recode_strings = 0;
1006 else
1007 return converterr(
1008 "(unknown parser marker combination)",
1009 arg, msgbuf, bufsize);
1010 buffer = (char **)va_arg(*p_va, char **);
1011 format++;
1012 if (buffer == NULL)
1013 return converterr("(buffer is NULL)",
1014 arg, msgbuf, bufsize);
1016 /* Encode object */
1017 if (!recode_strings && PyString_Check(arg)) {
1018 s = arg;
1019 Py_INCREF(s);
1021 else {
1022 #ifdef Py_USING_UNICODE
1023 PyObject *u;
1025 /* Convert object to Unicode */
1026 u = PyUnicode_FromObject(arg);
1027 if (u == NULL)
1028 return converterr(
1029 "string or unicode or text buffer",
1030 arg, msgbuf, bufsize);
1032 /* Encode object; use default error handling */
1033 s = PyUnicode_AsEncodedString(u,
1034 encoding,
1035 NULL);
1036 Py_DECREF(u);
1037 if (s == NULL)
1038 return converterr("(encoding failed)",
1039 arg, msgbuf, bufsize);
1040 if (!PyString_Check(s)) {
1041 Py_DECREF(s);
1042 return converterr(
1043 "(encoder failed to return a string)",
1044 arg, msgbuf, bufsize);
1046 #else
1047 return converterr("string<e>", arg, msgbuf, bufsize);
1048 #endif
1050 size = PyString_GET_SIZE(s);
1052 /* Write output; output is guaranteed to be 0-terminated */
1053 if (*format == '#') {
1054 /* Using buffer length parameter '#':
1056 - if *buffer is NULL, a new buffer of the
1057 needed size is allocated and the data
1058 copied into it; *buffer is updated to point
1059 to the new buffer; the caller is
1060 responsible for PyMem_Free()ing it after
1061 usage
1063 - if *buffer is not NULL, the data is
1064 copied to *buffer; *buffer_len has to be
1065 set to the size of the buffer on input;
1066 buffer overflow is signalled with an error;
1067 buffer has to provide enough room for the
1068 encoded string plus the trailing 0-byte
1070 - in both cases, *buffer_len is updated to
1071 the size of the buffer /excluding/ the
1072 trailing 0-byte
1075 FETCH_SIZE;
1077 format++;
1078 if (q == NULL && q2 == NULL) {
1079 Py_DECREF(s);
1080 return converterr(
1081 "(buffer_len is NULL)",
1082 arg, msgbuf, bufsize);
1084 if (*buffer == NULL) {
1085 *buffer = PyMem_NEW(char, size + 1);
1086 if (*buffer == NULL) {
1087 Py_DECREF(s);
1088 return converterr(
1089 "(memory error)",
1090 arg, msgbuf, bufsize);
1092 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1093 Py_DECREF(s);
1094 return converterr(
1095 "(cleanup problem)",
1096 arg, msgbuf, bufsize);
1098 } else {
1099 if (size + 1 > BUFFER_LEN) {
1100 Py_DECREF(s);
1101 return converterr(
1102 "(buffer overflow)",
1103 arg, msgbuf, bufsize);
1106 memcpy(*buffer,
1107 PyString_AS_STRING(s),
1108 size + 1);
1109 STORE_SIZE(size);
1110 } else {
1111 /* Using a 0-terminated buffer:
1113 - the encoded string has to be 0-terminated
1114 for this variant to work; if it is not, an
1115 error raised
1117 - a new buffer of the needed size is
1118 allocated and the data copied into it;
1119 *buffer is updated to point to the new
1120 buffer; the caller is responsible for
1121 PyMem_Free()ing it after usage
1124 if ((Py_ssize_t)strlen(PyString_AS_STRING(s))
1125 != size) {
1126 Py_DECREF(s);
1127 return converterr(
1128 "encoded string without NULL bytes",
1129 arg, msgbuf, bufsize);
1131 *buffer = PyMem_NEW(char, size + 1);
1132 if (*buffer == NULL) {
1133 Py_DECREF(s);
1134 return converterr("(memory error)",
1135 arg, msgbuf, bufsize);
1137 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1138 Py_DECREF(s);
1139 return converterr("(cleanup problem)",
1140 arg, msgbuf, bufsize);
1142 memcpy(*buffer,
1143 PyString_AS_STRING(s),
1144 size + 1);
1146 Py_DECREF(s);
1147 break;
1150 #ifdef Py_USING_UNICODE
1151 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1152 if (*format == '#') { /* any buffer-like object */
1153 void **p = (void **)va_arg(*p_va, char **);
1154 FETCH_SIZE;
1155 if (PyUnicode_Check(arg)) {
1156 *p = PyUnicode_AS_UNICODE(arg);
1157 STORE_SIZE(PyUnicode_GET_SIZE(arg));
1159 else {
1160 return converterr("cannot convert raw buffers",
1161 arg, msgbuf, bufsize);
1163 format++;
1164 } else {
1165 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1166 if (PyUnicode_Check(arg))
1167 *p = PyUnicode_AS_UNICODE(arg);
1168 else
1169 return converterr("unicode", arg, msgbuf, bufsize);
1171 break;
1173 #endif
1175 case 'S': { /* string object */
1176 PyObject **p = va_arg(*p_va, PyObject **);
1177 if (PyString_Check(arg))
1178 *p = arg;
1179 else
1180 return converterr("string", arg, msgbuf, bufsize);
1181 break;
1184 #ifdef Py_USING_UNICODE
1185 case 'U': { /* Unicode object */
1186 PyObject **p = va_arg(*p_va, PyObject **);
1187 if (PyUnicode_Check(arg))
1188 *p = arg;
1189 else
1190 return converterr("unicode", arg, msgbuf, bufsize);
1191 break;
1193 #endif
1195 case 'O': { /* object */
1196 PyTypeObject *type;
1197 PyObject **p;
1198 if (*format == '!') {
1199 type = va_arg(*p_va, PyTypeObject*);
1200 p = va_arg(*p_va, PyObject **);
1201 format++;
1202 if (PyType_IsSubtype(arg->ob_type, type))
1203 *p = arg;
1204 else
1205 return converterr(type->tp_name, arg, msgbuf, bufsize);
1208 else if (*format == '?') {
1209 inquiry pred = va_arg(*p_va, inquiry);
1210 p = va_arg(*p_va, PyObject **);
1211 format++;
1212 if ((*pred)(arg))
1213 *p = arg;
1214 else
1215 return converterr("(unspecified)",
1216 arg, msgbuf, bufsize);
1219 else if (*format == '&') {
1220 typedef int (*converter)(PyObject *, void *);
1221 converter convert = va_arg(*p_va, converter);
1222 void *addr = va_arg(*p_va, void *);
1223 format++;
1224 if (! (*convert)(arg, addr))
1225 return converterr("(unspecified)",
1226 arg, msgbuf, bufsize);
1228 else {
1229 p = va_arg(*p_va, PyObject **);
1230 *p = arg;
1232 break;
1236 case 'w': { /* memory buffer, read-write access */
1237 void **p = va_arg(*p_va, void **);
1238 void *res;
1239 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1240 Py_ssize_t count;
1242 if (pb && pb->bf_releasebuffer && *format != '*')
1243 /* Buffer must be released, yet caller does not use
1244 the Py_buffer protocol. */
1245 return converterr("pinned buffer", arg, msgbuf, bufsize);
1247 if (pb && pb->bf_getbuffer && *format == '*') {
1248 /* Caller is interested in Py_buffer, and the object
1249 supports it directly. */
1250 format++;
1251 if (pb->bf_getbuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1252 PyErr_Clear();
1253 return converterr("read-write buffer", arg, msgbuf, bufsize);
1255 if (addcleanup(p, freelist, cleanup_buffer)) {
1256 return converterr(
1257 "(cleanup problem)",
1258 arg, msgbuf, bufsize);
1260 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1261 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1262 break;
1265 if (pb == NULL ||
1266 pb->bf_getwritebuffer == NULL ||
1267 pb->bf_getsegcount == NULL)
1268 return converterr("read-write buffer", arg, msgbuf, bufsize);
1269 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1270 return converterr("single-segment read-write buffer",
1271 arg, msgbuf, bufsize);
1272 if ((count = pb->bf_getwritebuffer(arg, 0, &res)) < 0)
1273 return converterr("(unspecified)", arg, msgbuf, bufsize);
1274 if (*format == '*') {
1275 PyBuffer_FillInfo((Py_buffer*)p, arg, res, count, 1, 0);
1276 format++;
1278 else {
1279 *p = res;
1280 if (*format == '#') {
1281 FETCH_SIZE;
1282 STORE_SIZE(count);
1283 format++;
1286 break;
1289 case 't': { /* 8-bit character buffer, read-only access */
1290 char **p = va_arg(*p_va, char **);
1291 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1292 Py_ssize_t count;
1294 if (*format++ != '#')
1295 return converterr(
1296 "invalid use of 't' format character",
1297 arg, msgbuf, bufsize);
1298 if (!PyType_HasFeature(arg->ob_type,
1299 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
1300 pb == NULL || pb->bf_getcharbuffer == NULL ||
1301 pb->bf_getsegcount == NULL)
1302 return converterr(
1303 "string or read-only character buffer",
1304 arg, msgbuf, bufsize);
1306 if (pb->bf_getsegcount(arg, NULL) != 1)
1307 return converterr(
1308 "string or single-segment read-only buffer",
1309 arg, msgbuf, bufsize);
1311 if (pb->bf_releasebuffer)
1312 return converterr(
1313 "string or pinned buffer",
1314 arg, msgbuf, bufsize);
1316 count = pb->bf_getcharbuffer(arg, 0, p);
1317 if (count < 0)
1318 return converterr("(unspecified)", arg, msgbuf, bufsize);
1320 FETCH_SIZE;
1321 STORE_SIZE(count);
1323 break;
1326 default:
1327 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
1331 *p_format = format;
1332 return NULL;
1335 static Py_ssize_t
1336 convertbuffer(PyObject *arg, void **p, char **errmsg)
1338 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1339 Py_ssize_t count;
1340 if (pb == NULL ||
1341 pb->bf_getreadbuffer == NULL ||
1342 pb->bf_getsegcount == NULL ||
1343 pb->bf_releasebuffer != NULL) {
1344 *errmsg = "string or read-only buffer";
1345 return -1;
1347 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
1348 *errmsg = "string or single-segment read-only buffer";
1349 return -1;
1351 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
1352 *errmsg = "(unspecified)";
1354 return count;
1357 static int
1358 getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
1360 void *buf;
1361 Py_ssize_t count;
1362 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1363 if (pb == NULL) {
1364 *errmsg = "string or buffer";
1365 return -1;
1367 if (pb->bf_getbuffer) {
1368 if (pb->bf_getbuffer(arg, view, 0) < 0) {
1369 *errmsg = "convertible to a buffer";
1370 return -1;
1372 if (!PyBuffer_IsContiguous(view, 'C')) {
1373 *errmsg = "contiguous buffer";
1374 return -1;
1376 return 0;
1379 count = convertbuffer(arg, &buf, errmsg);
1380 if (count < 0) {
1381 *errmsg = "convertible to a buffer";
1382 return count;
1384 PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
1385 return 0;
1388 /* Support for keyword arguments donated by
1389 Geoff Philbrick <philbric@delphi.hks.com> */
1391 /* Return false (0) for error, else true. */
1393 PyArg_ParseTupleAndKeywords(PyObject *args,
1394 PyObject *keywords,
1395 const char *format,
1396 char **kwlist, ...)
1398 int retval;
1399 va_list va;
1401 if ((args == NULL || !PyTuple_Check(args)) ||
1402 (keywords != NULL && !PyDict_Check(keywords)) ||
1403 format == NULL ||
1404 kwlist == NULL)
1406 PyErr_BadInternalCall();
1407 return 0;
1410 va_start(va, kwlist);
1411 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1412 va_end(va);
1413 return retval;
1417 _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1418 PyObject *keywords,
1419 const char *format,
1420 char **kwlist, ...)
1422 int retval;
1423 va_list va;
1425 if ((args == NULL || !PyTuple_Check(args)) ||
1426 (keywords != NULL && !PyDict_Check(keywords)) ||
1427 format == NULL ||
1428 kwlist == NULL)
1430 PyErr_BadInternalCall();
1431 return 0;
1434 va_start(va, kwlist);
1435 retval = vgetargskeywords(args, keywords, format,
1436 kwlist, &va, FLAG_SIZE_T);
1437 va_end(va);
1438 return retval;
1443 PyArg_VaParseTupleAndKeywords(PyObject *args,
1444 PyObject *keywords,
1445 const char *format,
1446 char **kwlist, va_list va)
1448 int retval;
1449 va_list lva;
1451 if ((args == NULL || !PyTuple_Check(args)) ||
1452 (keywords != NULL && !PyDict_Check(keywords)) ||
1453 format == NULL ||
1454 kwlist == NULL)
1456 PyErr_BadInternalCall();
1457 return 0;
1460 #ifdef VA_LIST_IS_ARRAY
1461 memcpy(lva, va, sizeof(va_list));
1462 #else
1463 #ifdef __va_copy
1464 __va_copy(lva, va);
1465 #else
1466 lva = va;
1467 #endif
1468 #endif
1470 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1471 return retval;
1475 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1476 PyObject *keywords,
1477 const char *format,
1478 char **kwlist, va_list va)
1480 int retval;
1481 va_list lva;
1483 if ((args == NULL || !PyTuple_Check(args)) ||
1484 (keywords != NULL && !PyDict_Check(keywords)) ||
1485 format == NULL ||
1486 kwlist == NULL)
1488 PyErr_BadInternalCall();
1489 return 0;
1492 #ifdef VA_LIST_IS_ARRAY
1493 memcpy(lva, va, sizeof(va_list));
1494 #else
1495 #ifdef __va_copy
1496 __va_copy(lva, va);
1497 #else
1498 lva = va;
1499 #endif
1500 #endif
1502 retval = vgetargskeywords(args, keywords, format,
1503 kwlist, &lva, FLAG_SIZE_T);
1504 return retval;
1507 #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1509 static int
1510 vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
1511 char **kwlist, va_list *p_va, int flags)
1513 char msgbuf[512];
1514 int levels[32];
1515 const char *fname, *msg, *custom_msg, *keyword;
1516 int min = INT_MAX;
1517 int i, len, nargs, nkeywords;
1518 PyObject *freelist = NULL, *current_arg;
1520 assert(args != NULL && PyTuple_Check(args));
1521 assert(keywords == NULL || PyDict_Check(keywords));
1522 assert(format != NULL);
1523 assert(kwlist != NULL);
1524 assert(p_va != NULL);
1526 /* grab the function name or custom error msg first (mutually exclusive) */
1527 fname = strchr(format, ':');
1528 if (fname) {
1529 fname++;
1530 custom_msg = NULL;
1532 else {
1533 custom_msg = strchr(format,';');
1534 if (custom_msg)
1535 custom_msg++;
1538 /* scan kwlist and get greatest possible nbr of args */
1539 for (len=0; kwlist[len]; len++)
1540 continue;
1542 nargs = PyTuple_GET_SIZE(args);
1543 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1544 if (nargs + nkeywords > len) {
1545 PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
1546 "argument%s (%d given)",
1547 (fname == NULL) ? "function" : fname,
1548 (fname == NULL) ? "" : "()",
1549 len,
1550 (len == 1) ? "" : "s",
1551 nargs + nkeywords);
1552 return 0;
1555 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1556 for (i = 0; i < len; i++) {
1557 keyword = kwlist[i];
1558 if (*format == '|') {
1559 min = i;
1560 format++;
1562 if (IS_END_OF_FORMAT(*format)) {
1563 PyErr_Format(PyExc_RuntimeError,
1564 "More keyword list entries (%d) than "
1565 "format specifiers (%d)", len, i);
1566 return cleanreturn(0, freelist);
1568 current_arg = NULL;
1569 if (nkeywords) {
1570 current_arg = PyDict_GetItemString(keywords, keyword);
1572 if (current_arg) {
1573 --nkeywords;
1574 if (i < nargs) {
1575 /* arg present in tuple and in dict */
1576 PyErr_Format(PyExc_TypeError,
1577 "Argument given by name ('%s') "
1578 "and position (%d)",
1579 keyword, i+1);
1580 return cleanreturn(0, freelist);
1583 else if (nkeywords && PyErr_Occurred())
1584 return cleanreturn(0, freelist);
1585 else if (i < nargs)
1586 current_arg = PyTuple_GET_ITEM(args, i);
1588 if (current_arg) {
1589 msg = convertitem(current_arg, &format, p_va, flags,
1590 levels, msgbuf, sizeof(msgbuf), &freelist);
1591 if (msg) {
1592 seterror(i+1, msg, levels, fname, custom_msg);
1593 return cleanreturn(0, freelist);
1595 continue;
1598 if (i < min) {
1599 PyErr_Format(PyExc_TypeError, "Required argument "
1600 "'%s' (pos %d) not found",
1601 keyword, i+1);
1602 return cleanreturn(0, freelist);
1604 /* current code reports success when all required args
1605 * fulfilled and no keyword args left, with no further
1606 * validation. XXX Maybe skip this in debug build ?
1608 if (!nkeywords)
1609 return cleanreturn(1, freelist);
1611 /* We are into optional args, skip thru to any remaining
1612 * keyword args */
1613 msg = skipitem(&format, p_va, flags);
1614 if (msg) {
1615 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1616 format);
1617 return cleanreturn(0, freelist);
1621 if (!IS_END_OF_FORMAT(*format) && *format != '|') {
1622 PyErr_Format(PyExc_RuntimeError,
1623 "more argument specifiers than keyword list entries "
1624 "(remaining format:'%s')", format);
1625 return cleanreturn(0, freelist);
1628 /* make sure there are no extraneous keyword arguments */
1629 if (nkeywords > 0) {
1630 PyObject *key, *value;
1631 Py_ssize_t pos = 0;
1632 while (PyDict_Next(keywords, &pos, &key, &value)) {
1633 int match = 0;
1634 char *ks;
1635 if (!PyString_Check(key)) {
1636 PyErr_SetString(PyExc_TypeError,
1637 "keywords must be strings");
1638 return cleanreturn(0, freelist);
1640 ks = PyString_AsString(key);
1641 for (i = 0; i < len; i++) {
1642 if (!strcmp(ks, kwlist[i])) {
1643 match = 1;
1644 break;
1647 if (!match) {
1648 PyErr_Format(PyExc_TypeError,
1649 "'%s' is an invalid keyword "
1650 "argument for this function",
1651 ks);
1652 return cleanreturn(0, freelist);
1657 return cleanreturn(1, freelist);
1661 static char *
1662 skipitem(const char **p_format, va_list *p_va, int flags)
1664 const char *format = *p_format;
1665 char c = *format++;
1667 switch (c) {
1669 /* simple codes
1670 * The individual types (second arg of va_arg) are irrelevant */
1672 case 'b': /* byte -- very short int */
1673 case 'B': /* byte as bitfield */
1674 case 'h': /* short int */
1675 case 'H': /* short int as bitfield */
1676 case 'i': /* int */
1677 case 'I': /* int sized bitfield */
1678 case 'l': /* long int */
1679 case 'k': /* long int sized bitfield */
1680 #ifdef HAVE_LONG_LONG
1681 case 'L': /* PY_LONG_LONG */
1682 case 'K': /* PY_LONG_LONG sized bitfield */
1683 #endif
1684 case 'f': /* float */
1685 case 'd': /* double */
1686 #ifndef WITHOUT_COMPLEX
1687 case 'D': /* complex double */
1688 #endif
1689 case 'c': /* char */
1691 (void) va_arg(*p_va, void *);
1692 break;
1695 case 'n': /* Py_ssize_t */
1697 (void) va_arg(*p_va, Py_ssize_t *);
1698 break;
1701 /* string codes */
1703 case 'e': /* string with encoding */
1705 (void) va_arg(*p_va, const char *);
1706 if (!(*format == 's' || *format == 't'))
1707 /* after 'e', only 's' and 't' is allowed */
1708 goto err;
1709 format++;
1710 /* explicit fallthrough to string cases */
1713 case 's': /* string */
1714 case 'z': /* string or None */
1715 #ifdef Py_USING_UNICODE
1716 case 'u': /* unicode string */
1717 #endif
1718 case 't': /* buffer, read-only */
1719 case 'w': /* buffer, read-write */
1721 (void) va_arg(*p_va, char **);
1722 if (*format == '#') {
1723 if (flags & FLAG_SIZE_T)
1724 (void) va_arg(*p_va, Py_ssize_t *);
1725 else
1726 (void) va_arg(*p_va, int *);
1727 format++;
1728 } else if ((c == 's' || c == 'z') && *format == '*') {
1729 format++;
1731 break;
1734 /* object codes */
1736 case 'S': /* string object */
1737 #ifdef Py_USING_UNICODE
1738 case 'U': /* unicode string object */
1739 #endif
1741 (void) va_arg(*p_va, PyObject **);
1742 break;
1745 case 'O': /* object */
1747 if (*format == '!') {
1748 format++;
1749 (void) va_arg(*p_va, PyTypeObject*);
1750 (void) va_arg(*p_va, PyObject **);
1752 #if 0
1753 /* I don't know what this is for */
1754 else if (*format == '?') {
1755 inquiry pred = va_arg(*p_va, inquiry);
1756 format++;
1757 if ((*pred)(arg)) {
1758 (void) va_arg(*p_va, PyObject **);
1761 #endif
1762 else if (*format == '&') {
1763 typedef int (*converter)(PyObject *, void *);
1764 (void) va_arg(*p_va, converter);
1765 (void) va_arg(*p_va, void *);
1766 format++;
1768 else {
1769 (void) va_arg(*p_va, PyObject **);
1771 break;
1774 case '(': /* bypass tuple, not handled at all previously */
1776 char *msg;
1777 for (;;) {
1778 if (*format==')')
1779 break;
1780 if (IS_END_OF_FORMAT(*format))
1781 return "Unmatched left paren in format "
1782 "string";
1783 msg = skipitem(&format, p_va, flags);
1784 if (msg)
1785 return msg;
1787 format++;
1788 break;
1791 case ')':
1792 return "Unmatched right paren in format string";
1794 default:
1795 err:
1796 return "impossible<bad format char>";
1800 *p_format = format;
1801 return NULL;
1806 PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
1808 Py_ssize_t i, l;
1809 PyObject **o;
1810 va_list vargs;
1812 #ifdef HAVE_STDARG_PROTOTYPES
1813 va_start(vargs, max);
1814 #else
1815 va_start(vargs);
1816 #endif
1818 assert(min >= 0);
1819 assert(min <= max);
1820 if (!PyTuple_Check(args)) {
1821 PyErr_SetString(PyExc_SystemError,
1822 "PyArg_UnpackTuple() argument list is not a tuple");
1823 return 0;
1825 l = PyTuple_GET_SIZE(args);
1826 if (l < min) {
1827 if (name != NULL)
1828 PyErr_Format(
1829 PyExc_TypeError,
1830 "%s expected %s%zd arguments, got %zd",
1831 name, (min == max ? "" : "at least "), min, l);
1832 else
1833 PyErr_Format(
1834 PyExc_TypeError,
1835 "unpacked tuple should have %s%zd elements,"
1836 " but has %zd",
1837 (min == max ? "" : "at least "), min, l);
1838 va_end(vargs);
1839 return 0;
1841 if (l > max) {
1842 if (name != NULL)
1843 PyErr_Format(
1844 PyExc_TypeError,
1845 "%s expected %s%zd arguments, got %zd",
1846 name, (min == max ? "" : "at most "), max, l);
1847 else
1848 PyErr_Format(
1849 PyExc_TypeError,
1850 "unpacked tuple should have %s%zd elements,"
1851 " but has %zd",
1852 (min == max ? "" : "at most "), max, l);
1853 va_end(vargs);
1854 return 0;
1856 for (i = 0; i < l; i++) {
1857 o = va_arg(vargs, PyObject **);
1858 *o = PyTuple_GET_ITEM(args, i);
1860 va_end(vargs);
1861 return 1;
1865 /* For type constructors that don't take keyword args
1867 * Sets a TypeError and returns 0 if the kwds dict is
1868 * not empty, returns 1 otherwise
1871 _PyArg_NoKeywords(const char *funcname, PyObject *kw)
1873 if (kw == NULL)
1874 return 1;
1875 if (!PyDict_CheckExact(kw)) {
1876 PyErr_BadInternalCall();
1877 return 0;
1879 if (PyDict_Size(kw) == 0)
1880 return 1;
1882 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1883 funcname);
1884 return 0;
1886 #ifdef __cplusplus
1888 #endif