Use ldexp(q, exp) instead of q*2.**exp in true division test, to avoid bogus failures...
[python.git] / Python / getargs.c
blob1fd17d3ddc1130b08bca3b8418239366df48cf57
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_error(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 /* Convert a non-tuple argument. Return NULL if conversion went OK,
540 or a string with a message describing the failure. The message is
541 formatted as "must be <desired type>, not <actual type>".
542 When failing, an exception may or may not have been raised.
543 Don't call if a tuple is expected.
545 When you add new format codes, please don't forget poor skipitem() below.
548 static char *
549 convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
550 char *msgbuf, size_t bufsize, PyObject **freelist)
552 /* For # codes */
553 #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
554 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
555 else q=va_arg(*p_va, int*);
556 #define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
557 #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
559 const char *format = *p_format;
560 char c = *format++;
561 #ifdef Py_USING_UNICODE
562 PyObject *uarg;
563 #endif
565 switch (c) {
567 case 'b': { /* unsigned byte -- very short int */
568 char *p = va_arg(*p_va, char *);
569 long ival;
570 if (float_argument_error(arg))
571 return converterr("integer<b>", arg, msgbuf, bufsize);
572 ival = PyInt_AsLong(arg);
573 if (ival == -1 && PyErr_Occurred())
574 return converterr("integer<b>", arg, msgbuf, bufsize);
575 else if (ival < 0) {
576 PyErr_SetString(PyExc_OverflowError,
577 "unsigned byte integer is less than minimum");
578 return converterr("integer<b>", arg, msgbuf, bufsize);
580 else if (ival > UCHAR_MAX) {
581 PyErr_SetString(PyExc_OverflowError,
582 "unsigned byte integer is greater than maximum");
583 return converterr("integer<b>", arg, msgbuf, bufsize);
585 else
586 *p = (unsigned char) ival;
587 break;
590 case 'B': {/* byte sized bitfield - both signed and unsigned
591 values allowed */
592 char *p = va_arg(*p_va, char *);
593 long ival;
594 if (float_argument_error(arg))
595 return converterr("integer<B>", arg, msgbuf, bufsize);
596 ival = PyInt_AsUnsignedLongMask(arg);
597 if (ival == -1 && PyErr_Occurred())
598 return converterr("integer<B>", arg, msgbuf, bufsize);
599 else
600 *p = (unsigned char) ival;
601 break;
604 case 'h': {/* signed short int */
605 short *p = va_arg(*p_va, short *);
606 long ival;
607 if (float_argument_error(arg))
608 return converterr("integer<h>", arg, msgbuf, bufsize);
609 ival = PyInt_AsLong(arg);
610 if (ival == -1 && PyErr_Occurred())
611 return converterr("integer<h>", arg, msgbuf, bufsize);
612 else if (ival < SHRT_MIN) {
613 PyErr_SetString(PyExc_OverflowError,
614 "signed short integer is less than minimum");
615 return converterr("integer<h>", arg, msgbuf, bufsize);
617 else if (ival > SHRT_MAX) {
618 PyErr_SetString(PyExc_OverflowError,
619 "signed short integer is greater than maximum");
620 return converterr("integer<h>", arg, msgbuf, bufsize);
622 else
623 *p = (short) ival;
624 break;
627 case 'H': { /* short int sized bitfield, both signed and
628 unsigned allowed */
629 unsigned short *p = va_arg(*p_va, unsigned short *);
630 long ival;
631 if (float_argument_error(arg))
632 return converterr("integer<H>", arg, msgbuf, bufsize);
633 ival = PyInt_AsUnsignedLongMask(arg);
634 if (ival == -1 && PyErr_Occurred())
635 return converterr("integer<H>", arg, msgbuf, bufsize);
636 else
637 *p = (unsigned short) ival;
638 break;
641 case 'i': {/* signed int */
642 int *p = va_arg(*p_va, int *);
643 long ival;
644 if (float_argument_error(arg))
645 return converterr("integer<i>", arg, msgbuf, bufsize);
646 ival = PyInt_AsLong(arg);
647 if (ival == -1 && PyErr_Occurred())
648 return converterr("integer<i>", arg, msgbuf, bufsize);
649 else if (ival > INT_MAX) {
650 PyErr_SetString(PyExc_OverflowError,
651 "signed integer is greater than maximum");
652 return converterr("integer<i>", arg, msgbuf, bufsize);
654 else if (ival < INT_MIN) {
655 PyErr_SetString(PyExc_OverflowError,
656 "signed integer is less than minimum");
657 return converterr("integer<i>", arg, msgbuf, bufsize);
659 else
660 *p = ival;
661 break;
664 case 'I': { /* int sized bitfield, both signed and
665 unsigned allowed */
666 unsigned int *p = va_arg(*p_va, unsigned int *);
667 unsigned int ival;
668 if (float_argument_error(arg))
669 return converterr("integer<I>", arg, msgbuf, bufsize);
670 ival = (unsigned int)PyInt_AsUnsignedLongMask(arg);
671 if (ival == (unsigned int)-1 && PyErr_Occurred())
672 return converterr("integer<I>", arg, msgbuf, bufsize);
673 else
674 *p = ival;
675 break;
678 case 'n': /* Py_ssize_t */
679 #if SIZEOF_SIZE_T != SIZEOF_LONG
681 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
682 Py_ssize_t ival;
683 if (float_argument_error(arg))
684 return converterr("integer<n>", arg, msgbuf, bufsize);
685 ival = PyInt_AsSsize_t(arg);
686 if (ival == -1 && PyErr_Occurred())
687 return converterr("integer<n>", arg, msgbuf, bufsize);
688 *p = ival;
689 break;
691 #endif
692 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
693 case 'l': {/* long int */
694 long *p = va_arg(*p_va, long *);
695 long ival;
696 if (float_argument_error(arg))
697 return converterr("integer<l>", arg, msgbuf, bufsize);
698 ival = PyInt_AsLong(arg);
699 if (ival == -1 && PyErr_Occurred())
700 return converterr("integer<l>", arg, msgbuf, bufsize);
701 else
702 *p = ival;
703 break;
706 case 'k': { /* long sized bitfield */
707 unsigned long *p = va_arg(*p_va, unsigned long *);
708 unsigned long ival;
709 if (PyInt_Check(arg))
710 ival = PyInt_AsUnsignedLongMask(arg);
711 else if (PyLong_Check(arg))
712 ival = PyLong_AsUnsignedLongMask(arg);
713 else
714 return converterr("integer<k>", arg, msgbuf, bufsize);
715 *p = ival;
716 break;
719 #ifdef HAVE_LONG_LONG
720 case 'L': {/* PY_LONG_LONG */
721 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
722 PY_LONG_LONG ival = PyLong_AsLongLong( arg );
723 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
724 return converterr("long<L>", arg, msgbuf, bufsize);
725 } else {
726 *p = ival;
728 break;
731 case 'K': { /* long long sized bitfield */
732 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
733 unsigned PY_LONG_LONG ival;
734 if (PyInt_Check(arg))
735 ival = PyInt_AsUnsignedLongMask(arg);
736 else if (PyLong_Check(arg))
737 ival = PyLong_AsUnsignedLongLongMask(arg);
738 else
739 return converterr("integer<K>", arg, msgbuf, bufsize);
740 *p = ival;
741 break;
743 #endif
745 case 'f': {/* float */
746 float *p = va_arg(*p_va, float *);
747 double dval = PyFloat_AsDouble(arg);
748 if (PyErr_Occurred())
749 return converterr("float<f>", arg, msgbuf, bufsize);
750 else
751 *p = (float) dval;
752 break;
755 case 'd': {/* double */
756 double *p = va_arg(*p_va, double *);
757 double dval = PyFloat_AsDouble(arg);
758 if (PyErr_Occurred())
759 return converterr("float<d>", arg, msgbuf, bufsize);
760 else
761 *p = dval;
762 break;
765 #ifndef WITHOUT_COMPLEX
766 case 'D': {/* complex double */
767 Py_complex *p = va_arg(*p_va, Py_complex *);
768 Py_complex cval;
769 cval = PyComplex_AsCComplex(arg);
770 if (PyErr_Occurred())
771 return converterr("complex<D>", arg, msgbuf, bufsize);
772 else
773 *p = cval;
774 break;
776 #endif /* WITHOUT_COMPLEX */
778 case 'c': {/* char */
779 char *p = va_arg(*p_va, char *);
780 if (PyString_Check(arg) && PyString_Size(arg) == 1)
781 *p = PyString_AS_STRING(arg)[0];
782 else
783 return converterr("char", arg, msgbuf, bufsize);
784 break;
787 case 's': {/* string */
788 if (*format == '*') {
789 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
791 if (PyString_Check(arg)) {
792 PyBuffer_FillInfo(p, arg,
793 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
794 1, 0);
796 #ifdef Py_USING_UNICODE
797 else if (PyUnicode_Check(arg)) {
798 uarg = UNICODE_DEFAULT_ENCODING(arg);
799 if (uarg == NULL)
800 return converterr(CONV_UNICODE,
801 arg, msgbuf, bufsize);
802 PyBuffer_FillInfo(p, arg,
803 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
804 1, 0);
806 #endif
807 else { /* any buffer-like object */
808 char *buf;
809 if (getbuffer(arg, p, &buf) < 0)
810 return converterr(buf, arg, msgbuf, bufsize);
812 if (addcleanup(p, freelist, cleanup_buffer)) {
813 return converterr(
814 "(cleanup problem)",
815 arg, msgbuf, bufsize);
817 format++;
818 } else if (*format == '#') {
819 void **p = (void **)va_arg(*p_va, char **);
820 FETCH_SIZE;
822 if (PyString_Check(arg)) {
823 *p = PyString_AS_STRING(arg);
824 STORE_SIZE(PyString_GET_SIZE(arg));
826 #ifdef Py_USING_UNICODE
827 else if (PyUnicode_Check(arg)) {
828 uarg = UNICODE_DEFAULT_ENCODING(arg);
829 if (uarg == NULL)
830 return converterr(CONV_UNICODE,
831 arg, msgbuf, bufsize);
832 *p = PyString_AS_STRING(uarg);
833 STORE_SIZE(PyString_GET_SIZE(uarg));
835 #endif
836 else { /* any buffer-like object */
837 char *buf;
838 Py_ssize_t count = convertbuffer(arg, p, &buf);
839 if (count < 0)
840 return converterr(buf, arg, msgbuf, bufsize);
841 STORE_SIZE(count);
843 format++;
844 } else {
845 char **p = va_arg(*p_va, char **);
847 if (PyString_Check(arg))
848 *p = PyString_AS_STRING(arg);
849 #ifdef Py_USING_UNICODE
850 else if (PyUnicode_Check(arg)) {
851 uarg = UNICODE_DEFAULT_ENCODING(arg);
852 if (uarg == NULL)
853 return converterr(CONV_UNICODE,
854 arg, msgbuf, bufsize);
855 *p = PyString_AS_STRING(uarg);
857 #endif
858 else
859 return converterr("string", arg, msgbuf, bufsize);
860 if ((Py_ssize_t)strlen(*p) != PyString_Size(arg))
861 return converterr("string without null bytes",
862 arg, msgbuf, bufsize);
864 break;
867 case 'z': {/* string, may be NULL (None) */
868 if (*format == '*') {
869 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
871 if (arg == Py_None)
872 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
873 else if (PyString_Check(arg)) {
874 PyBuffer_FillInfo(p, arg,
875 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
876 1, 0);
878 #ifdef Py_USING_UNICODE
879 else if (PyUnicode_Check(arg)) {
880 uarg = UNICODE_DEFAULT_ENCODING(arg);
881 if (uarg == NULL)
882 return converterr(CONV_UNICODE,
883 arg, msgbuf, bufsize);
884 PyBuffer_FillInfo(p, arg,
885 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
886 1, 0);
888 #endif
889 else { /* any buffer-like object */
890 char *buf;
891 if (getbuffer(arg, p, &buf) < 0)
892 return converterr(buf, arg, msgbuf, bufsize);
894 if (addcleanup(p, freelist, cleanup_buffer)) {
895 return converterr(
896 "(cleanup problem)",
897 arg, msgbuf, bufsize);
899 format++;
900 } else if (*format == '#') { /* any buffer-like object */
901 void **p = (void **)va_arg(*p_va, char **);
902 FETCH_SIZE;
904 if (arg == Py_None) {
905 *p = 0;
906 STORE_SIZE(0);
908 else if (PyString_Check(arg)) {
909 *p = PyString_AS_STRING(arg);
910 STORE_SIZE(PyString_GET_SIZE(arg));
912 #ifdef Py_USING_UNICODE
913 else if (PyUnicode_Check(arg)) {
914 uarg = UNICODE_DEFAULT_ENCODING(arg);
915 if (uarg == NULL)
916 return converterr(CONV_UNICODE,
917 arg, msgbuf, bufsize);
918 *p = PyString_AS_STRING(uarg);
919 STORE_SIZE(PyString_GET_SIZE(uarg));
921 #endif
922 else { /* any buffer-like object */
923 char *buf;
924 Py_ssize_t count = convertbuffer(arg, p, &buf);
925 if (count < 0)
926 return converterr(buf, arg, msgbuf, bufsize);
927 STORE_SIZE(count);
929 format++;
930 } else {
931 char **p = va_arg(*p_va, char **);
933 if (arg == Py_None)
934 *p = 0;
935 else if (PyString_Check(arg))
936 *p = PyString_AS_STRING(arg);
937 #ifdef Py_USING_UNICODE
938 else if (PyUnicode_Check(arg)) {
939 uarg = UNICODE_DEFAULT_ENCODING(arg);
940 if (uarg == NULL)
941 return converterr(CONV_UNICODE,
942 arg, msgbuf, bufsize);
943 *p = PyString_AS_STRING(uarg);
945 #endif
946 else
947 return converterr("string or None",
948 arg, msgbuf, bufsize);
949 if (*format == '#') {
950 FETCH_SIZE;
951 assert(0); /* XXX redundant with if-case */
952 if (arg == Py_None)
953 *q = 0;
954 else
955 *q = PyString_Size(arg);
956 format++;
958 else if (*p != NULL &&
959 (Py_ssize_t)strlen(*p) != PyString_Size(arg))
960 return converterr(
961 "string without null bytes or None",
962 arg, msgbuf, bufsize);
964 break;
967 case 'e': {/* encoded string */
968 char **buffer;
969 const char *encoding;
970 PyObject *s;
971 Py_ssize_t size;
972 int recode_strings;
974 /* Get 'e' parameter: the encoding name */
975 encoding = (const char *)va_arg(*p_va, const char *);
976 #ifdef Py_USING_UNICODE
977 if (encoding == NULL)
978 encoding = PyUnicode_GetDefaultEncoding();
979 #endif
981 /* Get output buffer parameter:
982 's' (recode all objects via Unicode) or
983 't' (only recode non-string objects)
985 if (*format == 's')
986 recode_strings = 1;
987 else if (*format == 't')
988 recode_strings = 0;
989 else
990 return converterr(
991 "(unknown parser marker combination)",
992 arg, msgbuf, bufsize);
993 buffer = (char **)va_arg(*p_va, char **);
994 format++;
995 if (buffer == NULL)
996 return converterr("(buffer is NULL)",
997 arg, msgbuf, bufsize);
999 /* Encode object */
1000 if (!recode_strings && PyString_Check(arg)) {
1001 s = arg;
1002 Py_INCREF(s);
1004 else {
1005 #ifdef Py_USING_UNICODE
1006 PyObject *u;
1008 /* Convert object to Unicode */
1009 u = PyUnicode_FromObject(arg);
1010 if (u == NULL)
1011 return converterr(
1012 "string or unicode or text buffer",
1013 arg, msgbuf, bufsize);
1015 /* Encode object; use default error handling */
1016 s = PyUnicode_AsEncodedString(u,
1017 encoding,
1018 NULL);
1019 Py_DECREF(u);
1020 if (s == NULL)
1021 return converterr("(encoding failed)",
1022 arg, msgbuf, bufsize);
1023 if (!PyString_Check(s)) {
1024 Py_DECREF(s);
1025 return converterr(
1026 "(encoder failed to return a string)",
1027 arg, msgbuf, bufsize);
1029 #else
1030 return converterr("string<e>", arg, msgbuf, bufsize);
1031 #endif
1033 size = PyString_GET_SIZE(s);
1035 /* Write output; output is guaranteed to be 0-terminated */
1036 if (*format == '#') {
1037 /* Using buffer length parameter '#':
1039 - if *buffer is NULL, a new buffer of the
1040 needed size is allocated and the data
1041 copied into it; *buffer is updated to point
1042 to the new buffer; the caller is
1043 responsible for PyMem_Free()ing it after
1044 usage
1046 - if *buffer is not NULL, the data is
1047 copied to *buffer; *buffer_len has to be
1048 set to the size of the buffer on input;
1049 buffer overflow is signalled with an error;
1050 buffer has to provide enough room for the
1051 encoded string plus the trailing 0-byte
1053 - in both cases, *buffer_len is updated to
1054 the size of the buffer /excluding/ the
1055 trailing 0-byte
1058 FETCH_SIZE;
1060 format++;
1061 if (q == NULL && q2 == NULL) {
1062 Py_DECREF(s);
1063 return converterr(
1064 "(buffer_len is NULL)",
1065 arg, msgbuf, bufsize);
1067 if (*buffer == NULL) {
1068 *buffer = PyMem_NEW(char, size + 1);
1069 if (*buffer == NULL) {
1070 Py_DECREF(s);
1071 return converterr(
1072 "(memory error)",
1073 arg, msgbuf, bufsize);
1075 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1076 Py_DECREF(s);
1077 return converterr(
1078 "(cleanup problem)",
1079 arg, msgbuf, bufsize);
1081 } else {
1082 if (size + 1 > BUFFER_LEN) {
1083 Py_DECREF(s);
1084 return converterr(
1085 "(buffer overflow)",
1086 arg, msgbuf, bufsize);
1089 memcpy(*buffer,
1090 PyString_AS_STRING(s),
1091 size + 1);
1092 STORE_SIZE(size);
1093 } else {
1094 /* Using a 0-terminated buffer:
1096 - the encoded string has to be 0-terminated
1097 for this variant to work; if it is not, an
1098 error raised
1100 - a new buffer of the needed size is
1101 allocated and the data copied into it;
1102 *buffer is updated to point to the new
1103 buffer; the caller is responsible for
1104 PyMem_Free()ing it after usage
1107 if ((Py_ssize_t)strlen(PyString_AS_STRING(s))
1108 != size) {
1109 Py_DECREF(s);
1110 return converterr(
1111 "encoded string without NULL bytes",
1112 arg, msgbuf, bufsize);
1114 *buffer = PyMem_NEW(char, size + 1);
1115 if (*buffer == NULL) {
1116 Py_DECREF(s);
1117 return converterr("(memory error)",
1118 arg, msgbuf, bufsize);
1120 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1121 Py_DECREF(s);
1122 return converterr("(cleanup problem)",
1123 arg, msgbuf, bufsize);
1125 memcpy(*buffer,
1126 PyString_AS_STRING(s),
1127 size + 1);
1129 Py_DECREF(s);
1130 break;
1133 #ifdef Py_USING_UNICODE
1134 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1135 if (*format == '#') { /* any buffer-like object */
1136 void **p = (void **)va_arg(*p_va, char **);
1137 FETCH_SIZE;
1138 if (PyUnicode_Check(arg)) {
1139 *p = PyUnicode_AS_UNICODE(arg);
1140 STORE_SIZE(PyUnicode_GET_SIZE(arg));
1142 else {
1143 return converterr("cannot convert raw buffers",
1144 arg, msgbuf, bufsize);
1146 format++;
1147 } else {
1148 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1149 if (PyUnicode_Check(arg))
1150 *p = PyUnicode_AS_UNICODE(arg);
1151 else
1152 return converterr("unicode", arg, msgbuf, bufsize);
1154 break;
1156 #endif
1158 case 'S': { /* string object */
1159 PyObject **p = va_arg(*p_va, PyObject **);
1160 if (PyString_Check(arg))
1161 *p = arg;
1162 else
1163 return converterr("string", arg, msgbuf, bufsize);
1164 break;
1167 #ifdef Py_USING_UNICODE
1168 case 'U': { /* Unicode object */
1169 PyObject **p = va_arg(*p_va, PyObject **);
1170 if (PyUnicode_Check(arg))
1171 *p = arg;
1172 else
1173 return converterr("unicode", arg, msgbuf, bufsize);
1174 break;
1176 #endif
1178 case 'O': { /* object */
1179 PyTypeObject *type;
1180 PyObject **p;
1181 if (*format == '!') {
1182 type = va_arg(*p_va, PyTypeObject*);
1183 p = va_arg(*p_va, PyObject **);
1184 format++;
1185 if (PyType_IsSubtype(arg->ob_type, type))
1186 *p = arg;
1187 else
1188 return converterr(type->tp_name, arg, msgbuf, bufsize);
1191 else if (*format == '?') {
1192 inquiry pred = va_arg(*p_va, inquiry);
1193 p = va_arg(*p_va, PyObject **);
1194 format++;
1195 if ((*pred)(arg))
1196 *p = arg;
1197 else
1198 return converterr("(unspecified)",
1199 arg, msgbuf, bufsize);
1202 else if (*format == '&') {
1203 typedef int (*converter)(PyObject *, void *);
1204 converter convert = va_arg(*p_va, converter);
1205 void *addr = va_arg(*p_va, void *);
1206 format++;
1207 if (! (*convert)(arg, addr))
1208 return converterr("(unspecified)",
1209 arg, msgbuf, bufsize);
1211 else {
1212 p = va_arg(*p_va, PyObject **);
1213 *p = arg;
1215 break;
1219 case 'w': { /* memory buffer, read-write access */
1220 void **p = va_arg(*p_va, void **);
1221 void *res;
1222 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1223 Py_ssize_t count;
1225 if (pb && pb->bf_releasebuffer && *format != '*')
1226 /* Buffer must be released, yet caller does not use
1227 the Py_buffer protocol. */
1228 return converterr("pinned buffer", arg, msgbuf, bufsize);
1230 if (pb && pb->bf_getbuffer && *format == '*') {
1231 /* Caller is interested in Py_buffer, and the object
1232 supports it directly. */
1233 format++;
1234 if (pb->bf_getbuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1235 PyErr_Clear();
1236 return converterr("read-write buffer", arg, msgbuf, bufsize);
1238 if (addcleanup(p, freelist, cleanup_buffer)) {
1239 return converterr(
1240 "(cleanup problem)",
1241 arg, msgbuf, bufsize);
1243 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1244 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1245 break;
1248 if (pb == NULL ||
1249 pb->bf_getwritebuffer == NULL ||
1250 pb->bf_getsegcount == NULL)
1251 return converterr("read-write buffer", arg, msgbuf, bufsize);
1252 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1253 return converterr("single-segment read-write buffer",
1254 arg, msgbuf, bufsize);
1255 if ((count = pb->bf_getwritebuffer(arg, 0, &res)) < 0)
1256 return converterr("(unspecified)", arg, msgbuf, bufsize);
1257 if (*format == '*') {
1258 PyBuffer_FillInfo((Py_buffer*)p, arg, res, count, 1, 0);
1259 format++;
1261 else {
1262 *p = res;
1263 if (*format == '#') {
1264 FETCH_SIZE;
1265 STORE_SIZE(count);
1266 format++;
1269 break;
1272 case 't': { /* 8-bit character buffer, read-only access */
1273 char **p = va_arg(*p_va, char **);
1274 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1275 Py_ssize_t count;
1277 if (*format++ != '#')
1278 return converterr(
1279 "invalid use of 't' format character",
1280 arg, msgbuf, bufsize);
1281 if (!PyType_HasFeature(arg->ob_type,
1282 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
1283 pb == NULL || pb->bf_getcharbuffer == NULL ||
1284 pb->bf_getsegcount == NULL)
1285 return converterr(
1286 "string or read-only character buffer",
1287 arg, msgbuf, bufsize);
1289 if (pb->bf_getsegcount(arg, NULL) != 1)
1290 return converterr(
1291 "string or single-segment read-only buffer",
1292 arg, msgbuf, bufsize);
1294 if (pb->bf_releasebuffer)
1295 return converterr(
1296 "string or pinned buffer",
1297 arg, msgbuf, bufsize);
1299 count = pb->bf_getcharbuffer(arg, 0, p);
1300 if (count < 0)
1301 return converterr("(unspecified)", arg, msgbuf, bufsize);
1303 FETCH_SIZE;
1304 STORE_SIZE(count);
1306 break;
1309 default:
1310 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
1314 *p_format = format;
1315 return NULL;
1318 static Py_ssize_t
1319 convertbuffer(PyObject *arg, void **p, char **errmsg)
1321 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1322 Py_ssize_t count;
1323 if (pb == NULL ||
1324 pb->bf_getreadbuffer == NULL ||
1325 pb->bf_getsegcount == NULL ||
1326 pb->bf_releasebuffer != NULL) {
1327 *errmsg = "string or read-only buffer";
1328 return -1;
1330 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
1331 *errmsg = "string or single-segment read-only buffer";
1332 return -1;
1334 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
1335 *errmsg = "(unspecified)";
1337 return count;
1340 static int
1341 getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
1343 void *buf;
1344 Py_ssize_t count;
1345 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1346 if (pb == NULL) {
1347 *errmsg = "string or buffer";
1348 return -1;
1350 if (pb->bf_getbuffer) {
1351 if (pb->bf_getbuffer(arg, view, 0) < 0) {
1352 *errmsg = "convertible to a buffer";
1353 return -1;
1355 if (!PyBuffer_IsContiguous(view, 'C')) {
1356 *errmsg = "contiguous buffer";
1357 return -1;
1359 return 0;
1362 count = convertbuffer(arg, &buf, errmsg);
1363 if (count < 0) {
1364 *errmsg = "convertible to a buffer";
1365 return count;
1367 PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
1368 return 0;
1371 /* Support for keyword arguments donated by
1372 Geoff Philbrick <philbric@delphi.hks.com> */
1374 /* Return false (0) for error, else true. */
1376 PyArg_ParseTupleAndKeywords(PyObject *args,
1377 PyObject *keywords,
1378 const char *format,
1379 char **kwlist, ...)
1381 int retval;
1382 va_list va;
1384 if ((args == NULL || !PyTuple_Check(args)) ||
1385 (keywords != NULL && !PyDict_Check(keywords)) ||
1386 format == NULL ||
1387 kwlist == NULL)
1389 PyErr_BadInternalCall();
1390 return 0;
1393 va_start(va, kwlist);
1394 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1395 va_end(va);
1396 return retval;
1400 _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1401 PyObject *keywords,
1402 const char *format,
1403 char **kwlist, ...)
1405 int retval;
1406 va_list va;
1408 if ((args == NULL || !PyTuple_Check(args)) ||
1409 (keywords != NULL && !PyDict_Check(keywords)) ||
1410 format == NULL ||
1411 kwlist == NULL)
1413 PyErr_BadInternalCall();
1414 return 0;
1417 va_start(va, kwlist);
1418 retval = vgetargskeywords(args, keywords, format,
1419 kwlist, &va, FLAG_SIZE_T);
1420 va_end(va);
1421 return retval;
1426 PyArg_VaParseTupleAndKeywords(PyObject *args,
1427 PyObject *keywords,
1428 const char *format,
1429 char **kwlist, va_list va)
1431 int retval;
1432 va_list lva;
1434 if ((args == NULL || !PyTuple_Check(args)) ||
1435 (keywords != NULL && !PyDict_Check(keywords)) ||
1436 format == NULL ||
1437 kwlist == NULL)
1439 PyErr_BadInternalCall();
1440 return 0;
1443 #ifdef VA_LIST_IS_ARRAY
1444 memcpy(lva, va, sizeof(va_list));
1445 #else
1446 #ifdef __va_copy
1447 __va_copy(lva, va);
1448 #else
1449 lva = va;
1450 #endif
1451 #endif
1453 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1454 return retval;
1458 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1459 PyObject *keywords,
1460 const char *format,
1461 char **kwlist, va_list va)
1463 int retval;
1464 va_list lva;
1466 if ((args == NULL || !PyTuple_Check(args)) ||
1467 (keywords != NULL && !PyDict_Check(keywords)) ||
1468 format == NULL ||
1469 kwlist == NULL)
1471 PyErr_BadInternalCall();
1472 return 0;
1475 #ifdef VA_LIST_IS_ARRAY
1476 memcpy(lva, va, sizeof(va_list));
1477 #else
1478 #ifdef __va_copy
1479 __va_copy(lva, va);
1480 #else
1481 lva = va;
1482 #endif
1483 #endif
1485 retval = vgetargskeywords(args, keywords, format,
1486 kwlist, &lva, FLAG_SIZE_T);
1487 return retval;
1490 #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1492 static int
1493 vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
1494 char **kwlist, va_list *p_va, int flags)
1496 char msgbuf[512];
1497 int levels[32];
1498 const char *fname, *msg, *custom_msg, *keyword;
1499 int min = INT_MAX;
1500 int i, len, nargs, nkeywords;
1501 PyObject *freelist = NULL, *current_arg;
1503 assert(args != NULL && PyTuple_Check(args));
1504 assert(keywords == NULL || PyDict_Check(keywords));
1505 assert(format != NULL);
1506 assert(kwlist != NULL);
1507 assert(p_va != NULL);
1509 /* grab the function name or custom error msg first (mutually exclusive) */
1510 fname = strchr(format, ':');
1511 if (fname) {
1512 fname++;
1513 custom_msg = NULL;
1515 else {
1516 custom_msg = strchr(format,';');
1517 if (custom_msg)
1518 custom_msg++;
1521 /* scan kwlist and get greatest possible nbr of args */
1522 for (len=0; kwlist[len]; len++)
1523 continue;
1525 nargs = PyTuple_GET_SIZE(args);
1526 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1527 if (nargs + nkeywords > len) {
1528 PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
1529 "argument%s (%d given)",
1530 (fname == NULL) ? "function" : fname,
1531 (fname == NULL) ? "" : "()",
1532 len,
1533 (len == 1) ? "" : "s",
1534 nargs + nkeywords);
1535 return 0;
1538 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1539 for (i = 0; i < len; i++) {
1540 keyword = kwlist[i];
1541 if (*format == '|') {
1542 min = i;
1543 format++;
1545 if (IS_END_OF_FORMAT(*format)) {
1546 PyErr_Format(PyExc_RuntimeError,
1547 "More keyword list entries (%d) than "
1548 "format specifiers (%d)", len, i);
1549 return cleanreturn(0, freelist);
1551 current_arg = NULL;
1552 if (nkeywords) {
1553 current_arg = PyDict_GetItemString(keywords, keyword);
1555 if (current_arg) {
1556 --nkeywords;
1557 if (i < nargs) {
1558 /* arg present in tuple and in dict */
1559 PyErr_Format(PyExc_TypeError,
1560 "Argument given by name ('%s') "
1561 "and position (%d)",
1562 keyword, i+1);
1563 return cleanreturn(0, freelist);
1566 else if (nkeywords && PyErr_Occurred())
1567 return cleanreturn(0, freelist);
1568 else if (i < nargs)
1569 current_arg = PyTuple_GET_ITEM(args, i);
1571 if (current_arg) {
1572 msg = convertitem(current_arg, &format, p_va, flags,
1573 levels, msgbuf, sizeof(msgbuf), &freelist);
1574 if (msg) {
1575 seterror(i+1, msg, levels, fname, custom_msg);
1576 return cleanreturn(0, freelist);
1578 continue;
1581 if (i < min) {
1582 PyErr_Format(PyExc_TypeError, "Required argument "
1583 "'%s' (pos %d) not found",
1584 keyword, i+1);
1585 return cleanreturn(0, freelist);
1587 /* current code reports success when all required args
1588 * fulfilled and no keyword args left, with no further
1589 * validation. XXX Maybe skip this in debug build ?
1591 if (!nkeywords)
1592 return cleanreturn(1, freelist);
1594 /* We are into optional args, skip thru to any remaining
1595 * keyword args */
1596 msg = skipitem(&format, p_va, flags);
1597 if (msg) {
1598 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1599 format);
1600 return cleanreturn(0, freelist);
1604 if (!IS_END_OF_FORMAT(*format) && *format != '|') {
1605 PyErr_Format(PyExc_RuntimeError,
1606 "more argument specifiers than keyword list entries "
1607 "(remaining format:'%s')", format);
1608 return cleanreturn(0, freelist);
1611 /* make sure there are no extraneous keyword arguments */
1612 if (nkeywords > 0) {
1613 PyObject *key, *value;
1614 Py_ssize_t pos = 0;
1615 while (PyDict_Next(keywords, &pos, &key, &value)) {
1616 int match = 0;
1617 char *ks;
1618 if (!PyString_Check(key)) {
1619 PyErr_SetString(PyExc_TypeError,
1620 "keywords must be strings");
1621 return cleanreturn(0, freelist);
1623 ks = PyString_AsString(key);
1624 for (i = 0; i < len; i++) {
1625 if (!strcmp(ks, kwlist[i])) {
1626 match = 1;
1627 break;
1630 if (!match) {
1631 PyErr_Format(PyExc_TypeError,
1632 "'%s' is an invalid keyword "
1633 "argument for this function",
1634 ks);
1635 return cleanreturn(0, freelist);
1640 return cleanreturn(1, freelist);
1644 static char *
1645 skipitem(const char **p_format, va_list *p_va, int flags)
1647 const char *format = *p_format;
1648 char c = *format++;
1650 switch (c) {
1652 /* simple codes
1653 * The individual types (second arg of va_arg) are irrelevant */
1655 case 'b': /* byte -- very short int */
1656 case 'B': /* byte as bitfield */
1657 case 'h': /* short int */
1658 case 'H': /* short int as bitfield */
1659 case 'i': /* int */
1660 case 'I': /* int sized bitfield */
1661 case 'l': /* long int */
1662 case 'k': /* long int sized bitfield */
1663 #ifdef HAVE_LONG_LONG
1664 case 'L': /* PY_LONG_LONG */
1665 case 'K': /* PY_LONG_LONG sized bitfield */
1666 #endif
1667 case 'f': /* float */
1668 case 'd': /* double */
1669 #ifndef WITHOUT_COMPLEX
1670 case 'D': /* complex double */
1671 #endif
1672 case 'c': /* char */
1674 (void) va_arg(*p_va, void *);
1675 break;
1678 case 'n': /* Py_ssize_t */
1680 (void) va_arg(*p_va, Py_ssize_t *);
1681 break;
1684 /* string codes */
1686 case 'e': /* string with encoding */
1688 (void) va_arg(*p_va, const char *);
1689 if (!(*format == 's' || *format == 't'))
1690 /* after 'e', only 's' and 't' is allowed */
1691 goto err;
1692 format++;
1693 /* explicit fallthrough to string cases */
1696 case 's': /* string */
1697 case 'z': /* string or None */
1698 #ifdef Py_USING_UNICODE
1699 case 'u': /* unicode string */
1700 #endif
1701 case 't': /* buffer, read-only */
1702 case 'w': /* buffer, read-write */
1704 (void) va_arg(*p_va, char **);
1705 if (*format == '#') {
1706 if (flags & FLAG_SIZE_T)
1707 (void) va_arg(*p_va, Py_ssize_t *);
1708 else
1709 (void) va_arg(*p_va, int *);
1710 format++;
1711 } else if ((c == 's' || c == 'z') && *format == '*') {
1712 format++;
1714 break;
1717 /* object codes */
1719 case 'S': /* string object */
1720 #ifdef Py_USING_UNICODE
1721 case 'U': /* unicode string object */
1722 #endif
1724 (void) va_arg(*p_va, PyObject **);
1725 break;
1728 case 'O': /* object */
1730 if (*format == '!') {
1731 format++;
1732 (void) va_arg(*p_va, PyTypeObject*);
1733 (void) va_arg(*p_va, PyObject **);
1735 #if 0
1736 /* I don't know what this is for */
1737 else if (*format == '?') {
1738 inquiry pred = va_arg(*p_va, inquiry);
1739 format++;
1740 if ((*pred)(arg)) {
1741 (void) va_arg(*p_va, PyObject **);
1744 #endif
1745 else if (*format == '&') {
1746 typedef int (*converter)(PyObject *, void *);
1747 (void) va_arg(*p_va, converter);
1748 (void) va_arg(*p_va, void *);
1749 format++;
1751 else {
1752 (void) va_arg(*p_va, PyObject **);
1754 break;
1757 case '(': /* bypass tuple, not handled at all previously */
1759 char *msg;
1760 for (;;) {
1761 if (*format==')')
1762 break;
1763 if (IS_END_OF_FORMAT(*format))
1764 return "Unmatched left paren in format "
1765 "string";
1766 msg = skipitem(&format, p_va, flags);
1767 if (msg)
1768 return msg;
1770 format++;
1771 break;
1774 case ')':
1775 return "Unmatched right paren in format string";
1777 default:
1778 err:
1779 return "impossible<bad format char>";
1783 *p_format = format;
1784 return NULL;
1789 PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
1791 Py_ssize_t i, l;
1792 PyObject **o;
1793 va_list vargs;
1795 #ifdef HAVE_STDARG_PROTOTYPES
1796 va_start(vargs, max);
1797 #else
1798 va_start(vargs);
1799 #endif
1801 assert(min >= 0);
1802 assert(min <= max);
1803 if (!PyTuple_Check(args)) {
1804 PyErr_SetString(PyExc_SystemError,
1805 "PyArg_UnpackTuple() argument list is not a tuple");
1806 return 0;
1808 l = PyTuple_GET_SIZE(args);
1809 if (l < min) {
1810 if (name != NULL)
1811 PyErr_Format(
1812 PyExc_TypeError,
1813 "%s expected %s%zd arguments, got %zd",
1814 name, (min == max ? "" : "at least "), min, l);
1815 else
1816 PyErr_Format(
1817 PyExc_TypeError,
1818 "unpacked tuple should have %s%zd elements,"
1819 " but has %zd",
1820 (min == max ? "" : "at least "), min, l);
1821 va_end(vargs);
1822 return 0;
1824 if (l > max) {
1825 if (name != NULL)
1826 PyErr_Format(
1827 PyExc_TypeError,
1828 "%s expected %s%zd arguments, got %zd",
1829 name, (min == max ? "" : "at most "), max, l);
1830 else
1831 PyErr_Format(
1832 PyExc_TypeError,
1833 "unpacked tuple should have %s%zd elements,"
1834 " but has %zd",
1835 (min == max ? "" : "at most "), max, l);
1836 va_end(vargs);
1837 return 0;
1839 for (i = 0; i < l; i++) {
1840 o = va_arg(vargs, PyObject **);
1841 *o = PyTuple_GET_ITEM(args, i);
1843 va_end(vargs);
1844 return 1;
1848 /* For type constructors that don't take keyword args
1850 * Sets a TypeError and returns 0 if the kwds dict is
1851 * not empty, returns 1 otherwise
1854 _PyArg_NoKeywords(const char *funcname, PyObject *kw)
1856 if (kw == NULL)
1857 return 1;
1858 if (!PyDict_CheckExact(kw)) {
1859 PyErr_BadInternalCall();
1860 return 0;
1862 if (PyDict_Size(kw) == 0)
1863 return 1;
1865 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1866 funcname);
1867 return 0;
1869 #ifdef __cplusplus
1871 #endif