Merged release21-maint changes.
[python/dscho.git] / Python / getargs.c
blobc9a5273c845c35ad40e00848910ec4f66829b4dc
2 /* New getargs implementation */
4 /* XXX There are several unchecked sprintf or strcat calls in this file.
5 XXX The only way these can become a danger is if some C code in the
6 XXX Python source (or in an extension) uses ridiculously long names
7 XXX or ridiculously deep nesting in format strings. */
9 #include "Python.h"
11 #include <ctype.h>
14 int PyArg_Parse(PyObject *, char *, ...);
15 int PyArg_ParseTuple(PyObject *, char *, ...);
16 int PyArg_VaParse(PyObject *, char *, va_list);
18 int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
19 char *, char **, ...);
21 /* Forward */
22 static int vgetargs1(PyObject *, char *, va_list *, int);
23 static void seterror(int, char *, int *, char *, char *);
24 static char *convertitem(PyObject *, char **, va_list *, int *, char *);
25 static char *converttuple(PyObject *, char **, va_list *,
26 int *, char *, int);
27 static char *convertsimple(PyObject *, char **, va_list *, char *);
28 static int convertbuffer(PyObject *, void **p, char **);
30 static int vgetargskeywords(PyObject *, PyObject *,
31 char *, char **, va_list *);
32 static char *skipitem(char **, va_list *);
34 int PyArg_Parse(PyObject *args, char *format, ...)
36 int retval;
37 va_list va;
39 va_start(va, format);
40 retval = vgetargs1(args, format, &va, 1);
41 va_end(va);
42 return retval;
46 int PyArg_ParseTuple(PyObject *args, char *format, ...)
48 int retval;
49 va_list va;
51 va_start(va, format);
52 retval = vgetargs1(args, format, &va, 0);
53 va_end(va);
54 return retval;
58 int
59 PyArg_VaParse(PyObject *args, char *format, va_list va)
61 va_list lva;
63 #ifdef VA_LIST_IS_ARRAY
64 memcpy(lva, va, sizeof(va_list));
65 #else
66 lva = va;
67 #endif
69 return vgetargs1(args, format, &lva, 0);
73 static int
74 vgetargs1(PyObject *args, char *format, va_list *p_va, int compat)
76 char msgbuf[256];
77 int levels[32];
78 char *fname = NULL;
79 char *message = NULL;
80 int min = -1;
81 int max = 0;
82 int level = 0;
83 int endfmt = 0;
84 char *formatsave = format;
85 int i, len;
86 char *msg;
88 assert(compat || (args != (PyObject*)NULL));
90 while (endfmt == 0) {
91 int c = *format++;
92 switch (c) {
93 case '(':
94 if (level == 0)
95 max++;
96 level++;
97 break;
98 case ')':
99 if (level == 0)
100 Py_FatalError("excess ')' in getargs format");
101 else
102 level--;
103 break;
104 case '\0':
105 endfmt = 1;
106 break;
107 case ':':
108 fname = format;
109 endfmt = 1;
110 break;
111 case ';':
112 message = format;
113 endfmt = 1;
114 break;
115 default:
116 if (level == 0) {
117 if (c == 'O')
118 max++;
119 else if (isalpha(c)) {
120 if (c != 'e') /* skip encoded */
121 max++;
122 } else if (c == '|')
123 min = max;
125 break;
129 if (level != 0)
130 Py_FatalError(/* '(' */ "missing ')' in getargs format");
132 if (min < 0)
133 min = max;
135 format = formatsave;
137 if (compat) {
138 if (max == 0) {
139 if (args == NULL)
140 return 1;
141 sprintf(msgbuf, "%s%s takes no arguments",
142 fname==NULL ? "function" : fname,
143 fname==NULL ? "" : "()");
144 PyErr_SetString(PyExc_TypeError, msgbuf);
145 return 0;
147 else if (min == 1 && max == 1) {
148 if (args == NULL) {
149 sprintf(msgbuf,
150 "%s%s takes at least one argument",
151 fname==NULL ? "function" : fname,
152 fname==NULL ? "" : "()");
153 PyErr_SetString(PyExc_TypeError, msgbuf);
154 return 0;
156 msg = convertitem(args, &format, p_va, levels, msgbuf);
157 if (msg == NULL)
158 return 1;
159 seterror(levels[0], msg, levels+1, fname, message);
160 return 0;
162 else {
163 PyErr_SetString(PyExc_SystemError,
164 "old style getargs format uses new features");
165 return 0;
169 if (!PyTuple_Check(args)) {
170 PyErr_SetString(PyExc_SystemError,
171 "new style getargs format but argument is not a tuple");
172 return 0;
175 len = PyTuple_GET_SIZE(args);
177 if (len < min || max < len) {
178 if (message == NULL) {
179 sprintf(msgbuf,
180 "%s%s takes %s %d argument%s (%d given)",
181 fname==NULL ? "function" : fname,
182 fname==NULL ? "" : "()",
183 min==max ? "exactly"
184 : len < min ? "at least" : "at most",
185 len < min ? min : max,
186 (len < min ? min : max) == 1 ? "" : "s",
187 len);
188 message = msgbuf;
190 PyErr_SetString(PyExc_TypeError, message);
191 return 0;
194 for (i = 0; i < len; i++) {
195 if (*format == '|')
196 format++;
197 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
198 levels, msgbuf);
199 if (msg) {
200 seterror(i+1, msg, levels, fname, message);
201 return 0;
205 if (*format != '\0' && !isalpha((int)(*format)) &&
206 *format != '(' &&
207 *format != '|' && *format != ':' && *format != ';') {
208 PyErr_Format(PyExc_SystemError,
209 "bad format string: %.200s", formatsave);
210 return 0;
213 return 1;
218 static void
219 seterror(int iarg, char *msg, int *levels, char *fname, char *message)
221 char buf[256];
222 int i;
223 char *p = buf;
225 if (PyErr_Occurred())
226 return;
227 else if (message == NULL) {
228 if (fname != NULL) {
229 sprintf(p, "%s() ", fname);
230 p += strlen(p);
232 if (iarg != 0) {
233 sprintf(p, "argument %d", iarg);
234 i = 0;
235 p += strlen(p);
236 while (levels[i] > 0) {
237 sprintf(p, ", item %d", levels[i]-1);
238 p += strlen(p);
239 i++;
242 else {
243 sprintf(p, "argument");
244 p += strlen(p);
246 sprintf(p, " %s", msg);
247 message = buf;
249 PyErr_SetString(PyExc_TypeError, message);
253 /* Convert a tuple argument.
254 On entry, *p_format points to the character _after_ the opening '('.
255 On successful exit, *p_format points to the closing ')'.
256 If successful:
257 *p_format and *p_va are updated,
258 *levels and *msgbuf are untouched,
259 and NULL is returned.
260 If the argument is invalid:
261 *p_format is unchanged,
262 *p_va is undefined,
263 *levels is a 0-terminated list of item numbers,
264 *msgbuf contains an error message, whose format is:
265 "must be <typename1>, not <typename2>", where:
266 <typename1> is the name of the expected type, and
267 <typename2> is the name of the actual type,
268 and msgbuf is returned.
271 static char *
272 converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels,
273 char *msgbuf, int toplevel)
275 int level = 0;
276 int n = 0;
277 char *format = *p_format;
278 int i;
280 for (;;) {
281 int c = *format++;
282 if (c == '(') {
283 if (level == 0)
284 n++;
285 level++;
287 else if (c == ')') {
288 if (level == 0)
289 break;
290 level--;
292 else if (c == ':' || c == ';' || c == '\0')
293 break;
294 else if (level == 0 && isalpha(c))
295 n++;
298 if (!PySequence_Check(arg) || PyString_Check(arg)) {
299 levels[0] = 0;
300 sprintf(msgbuf,
301 toplevel ? "expected %d arguments, not %s" :
302 "must be %d-item sequence, not %s",
303 n, arg == Py_None ? "None" : arg->ob_type->tp_name);
304 return msgbuf;
307 if ((i = PySequence_Size(arg)) != n) {
308 levels[0] = 0;
309 sprintf(msgbuf,
310 toplevel ? "expected %d arguments, not %d" :
311 "must be sequence of length %d, not %d",
312 n, i);
313 return msgbuf;
316 format = *p_format;
317 for (i = 0; i < n; i++) {
318 char *msg;
319 PyObject *item;
320 item = PySequence_GetItem(arg, i);
321 msg = convertitem(item, &format, p_va, levels+1, msgbuf);
322 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
323 Py_XDECREF(item);
324 if (msg != NULL) {
325 levels[0] = i+1;
326 return msg;
330 *p_format = format;
331 return NULL;
335 /* Convert a single item. */
337 static char *
338 convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels,
339 char *msgbuf)
341 char *msg;
342 char *format = *p_format;
344 if (*format == '(' /* ')' */) {
345 format++;
346 msg = converttuple(arg, &format, p_va, levels, msgbuf, 0);
347 if (msg == NULL)
348 format++;
350 else {
351 msg = convertsimple(arg, &format, p_va, msgbuf);
352 if (msg != NULL)
353 levels[0] = 0;
355 if (msg == NULL)
356 *p_format = format;
357 return msg;
362 #define UNICODE_DEFAULT_ENCODING(arg) \
363 _PyUnicode_AsDefaultEncodedString(arg, NULL)
365 /* Format an error message generated by convertsimple(). */
367 static char *
368 converterr(char *expected, PyObject *arg, char *msgbuf)
370 assert (expected != NULL);
371 sprintf(msgbuf, "must be %.50s, not %.50s", expected,
372 arg == Py_None ? "None" : arg->ob_type->tp_name);
373 return msgbuf;
376 #define CONV_UNICODE "(unicode conversion error)"
378 /* Convert a non-tuple argument. Return NULL if conversion went OK,
379 or a string with a message describing the failure. The message is
380 formatted as "must be <desired type>, not <actual type>".
381 When failing, an exception may or may not have been raised.
382 Don't call if a tuple is expected.
385 static char *
386 convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
388 char *format = *p_format;
389 char c = *format++;
391 switch (c) {
393 case 'b': { /* unsigned byte -- very short int */
394 char *p = va_arg(*p_va, char *);
395 long ival = PyInt_AsLong(arg);
396 if (ival == -1 && PyErr_Occurred())
397 return converterr("integer<b>", arg, msgbuf);
398 else if (ival < 0) {
399 PyErr_SetString(PyExc_OverflowError,
400 "unsigned byte integer is less than minimum");
401 return converterr("integer<b>", arg, msgbuf);
403 else if (ival > UCHAR_MAX) {
404 PyErr_SetString(PyExc_OverflowError,
405 "unsigned byte integer is greater than maximum");
406 return converterr("integer<b>", arg, msgbuf);
408 else
409 *p = (unsigned char) ival;
410 break;
413 case 'B': {/* byte sized bitfield - both signed and unsigned
414 values allowed */
415 char *p = va_arg(*p_va, char *);
416 long ival = PyInt_AsLong(arg);
417 if (ival == -1 && PyErr_Occurred())
418 return converterr("integer<b>", arg, msgbuf);
419 else if (ival < SCHAR_MIN) {
420 PyErr_SetString(PyExc_OverflowError,
421 "byte-sized integer bitfield is less than minimum");
422 return converterr("integer<B>", arg, msgbuf);
424 else if (ival > (int)UCHAR_MAX) {
425 PyErr_SetString(PyExc_OverflowError,
426 "byte-sized integer bitfield is greater than maximum");
427 return converterr("integer<B>", arg, msgbuf);
429 else
430 *p = (unsigned char) ival;
431 break;
434 case 'h': {/* signed short int */
435 short *p = va_arg(*p_va, short *);
436 long ival = PyInt_AsLong(arg);
437 if (ival == -1 && PyErr_Occurred())
438 return converterr("integer<h>", arg, msgbuf);
439 else if (ival < SHRT_MIN) {
440 PyErr_SetString(PyExc_OverflowError,
441 "signed short integer is less than minimum");
442 return converterr("integer<h>", arg, msgbuf);
444 else if (ival > SHRT_MAX) {
445 PyErr_SetString(PyExc_OverflowError,
446 "signed short integer is greater than maximum");
447 return converterr("integer<h>", arg, msgbuf);
449 else
450 *p = (short) ival;
451 break;
454 case 'H': { /* short int sized bitfield, both signed and
455 unsigned allowed */
456 unsigned short *p = va_arg(*p_va, unsigned short *);
457 long ival = PyInt_AsLong(arg);
458 if (ival == -1 && PyErr_Occurred())
459 return converterr("integer<H>", arg, msgbuf);
460 else if (ival < SHRT_MIN) {
461 PyErr_SetString(PyExc_OverflowError,
462 "short integer bitfield is less than minimum");
463 return converterr("integer<H>", arg, msgbuf);
465 else if (ival > USHRT_MAX) {
466 PyErr_SetString(PyExc_OverflowError,
467 "short integer bitfield is greater than maximum");
468 return converterr("integer<H>", arg, msgbuf);
470 else
471 *p = (unsigned short) ival;
472 break;
475 case 'i': {/* signed int */
476 int *p = va_arg(*p_va, int *);
477 long ival = PyInt_AsLong(arg);
478 if (ival == -1 && PyErr_Occurred())
479 return converterr("integer<i>", arg, msgbuf);
480 else if (ival > INT_MAX) {
481 PyErr_SetString(PyExc_OverflowError,
482 "signed integer is greater than maximum");
483 return converterr("integer<i>", arg, msgbuf);
485 else if (ival < INT_MIN) {
486 PyErr_SetString(PyExc_OverflowError,
487 "signed integer is less than minimum");
488 return converterr("integer<i>", arg, msgbuf);
490 else
491 *p = ival;
492 break;
495 case 'l': {/* long int */
496 long *p = va_arg(*p_va, long *);
497 long ival = PyInt_AsLong(arg);
498 if (ival == -1 && PyErr_Occurred())
499 return converterr("integer<l>", arg, msgbuf);
500 else
501 *p = ival;
502 break;
505 #ifdef HAVE_LONG_LONG
506 case 'L': {/* LONG_LONG */
507 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
508 LONG_LONG ival = PyLong_AsLongLong( arg );
509 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
510 return converterr("long<L>", arg, msgbuf);
511 } else {
512 *p = ival;
514 break;
516 #endif
518 case 'f': {/* float */
519 float *p = va_arg(*p_va, float *);
520 double dval = PyFloat_AsDouble(arg);
521 if (PyErr_Occurred())
522 return converterr("float<f>", arg, msgbuf);
523 else
524 *p = (float) dval;
525 break;
528 case 'd': {/* double */
529 double *p = va_arg(*p_va, double *);
530 double dval = PyFloat_AsDouble(arg);
531 if (PyErr_Occurred())
532 return converterr("float<d>", arg, msgbuf);
533 else
534 *p = dval;
535 break;
538 #ifndef WITHOUT_COMPLEX
539 case 'D': {/* complex double */
540 Py_complex *p = va_arg(*p_va, Py_complex *);
541 Py_complex cval;
542 cval = PyComplex_AsCComplex(arg);
543 if (PyErr_Occurred())
544 return converterr("complex<D>", arg, msgbuf);
545 else
546 *p = cval;
547 break;
549 #endif /* WITHOUT_COMPLEX */
551 case 'c': {/* char */
552 char *p = va_arg(*p_va, char *);
553 if (PyString_Check(arg) && PyString_Size(arg) == 1)
554 *p = PyString_AsString(arg)[0];
555 else
556 return converterr("char", arg, msgbuf);
557 break;
560 case 's': {/* string */
561 if (*format == '#') {
562 void **p = (void **)va_arg(*p_va, char **);
563 int *q = va_arg(*p_va, int *);
565 if (PyString_Check(arg)) {
566 *p = PyString_AS_STRING(arg);
567 *q = PyString_GET_SIZE(arg);
569 else if (PyUnicode_Check(arg)) {
570 arg = UNICODE_DEFAULT_ENCODING(arg);
571 if (arg == NULL)
572 return converterr(CONV_UNICODE,
573 arg, msgbuf);
574 *p = PyString_AS_STRING(arg);
575 *q = PyString_GET_SIZE(arg);
577 else { /* any buffer-like object */
578 char *buf;
579 int count = convertbuffer(arg, p, &buf);
580 if (count < 0)
581 return converterr(buf, arg, msgbuf);
582 *q = count;
584 format++;
585 } else {
586 char **p = va_arg(*p_va, char **);
588 if (PyString_Check(arg))
589 *p = PyString_AS_STRING(arg);
590 else if (PyUnicode_Check(arg)) {
591 arg = UNICODE_DEFAULT_ENCODING(arg);
592 if (arg == NULL)
593 return converterr(CONV_UNICODE,
594 arg, msgbuf);
595 *p = PyString_AS_STRING(arg);
597 else
598 return converterr("string", arg, msgbuf);
599 if ((int)strlen(*p) != PyString_Size(arg))
600 return converterr("string without null bytes",
601 arg, msgbuf);
603 break;
606 case 'z': {/* string, may be NULL (None) */
607 if (*format == '#') { /* any buffer-like object */
608 void **p = (void **)va_arg(*p_va, char **);
609 int *q = va_arg(*p_va, int *);
611 if (arg == Py_None) {
612 *p = 0;
613 *q = 0;
615 else if (PyString_Check(arg)) {
616 *p = PyString_AS_STRING(arg);
617 *q = PyString_GET_SIZE(arg);
619 else if (PyUnicode_Check(arg)) {
620 arg = UNICODE_DEFAULT_ENCODING(arg);
621 if (arg == NULL)
622 return converterr(CONV_UNICODE,
623 arg, msgbuf);
624 *p = PyString_AS_STRING(arg);
625 *q = PyString_GET_SIZE(arg);
627 else { /* any buffer-like object */
628 char *buf;
629 int count = convertbuffer(arg, p, &buf);
631 if (count < 0)
632 return converterr(buf, arg, msgbuf);
633 *q = count;
635 format++;
636 } else {
637 char **p = va_arg(*p_va, char **);
639 if (arg == Py_None)
640 *p = 0;
641 else if (PyString_Check(arg))
642 *p = PyString_AsString(arg);
643 else if (PyUnicode_Check(arg)) {
644 arg = UNICODE_DEFAULT_ENCODING(arg);
645 if (arg == NULL)
646 return converterr(CONV_UNICODE,
647 arg, msgbuf);
648 *p = PyString_AS_STRING(arg);
650 else
651 return converterr("string or None",
652 arg, msgbuf);
653 if (*format == '#') {
654 int *q = va_arg(*p_va, int *);
655 if (arg == Py_None)
656 *q = 0;
657 else
658 *q = PyString_Size(arg);
659 format++;
661 else if (*p != NULL &&
662 (int)strlen(*p) != PyString_Size(arg))
663 return converterr(
664 "string without null bytes or None",
665 arg, msgbuf);
667 break;
670 case 'e': {/* encoded string */
671 char **buffer;
672 const char *encoding;
673 PyObject *u, *s;
674 int size, recode_strings;
676 /* Get 'e' parameter: the encoding name */
677 encoding = (const char *)va_arg(*p_va, const char *);
678 if (encoding == NULL)
679 encoding = PyUnicode_GetDefaultEncoding();
681 /* Get output buffer parameter:
682 's' (recode all objects via Unicode) or
683 't' (only recode non-string objects)
685 if (*format == 's')
686 recode_strings = 1;
687 else if (*format == 't')
688 recode_strings = 0;
689 else
690 return converterr(
691 "(unknown parser marker combination)",
692 arg, msgbuf);
693 buffer = (char **)va_arg(*p_va, char **);
694 format++;
695 if (buffer == NULL)
696 return converterr("(buffer is NULL)",
697 arg, msgbuf);
699 /* Encode object */
700 if (!recode_strings && PyString_Check(arg)) {
701 s = arg;
702 Py_INCREF(s);
704 else {
705 /* Convert object to Unicode */
706 u = PyUnicode_FromObject(arg);
707 if (u == NULL)
708 return converterr(
709 "string or unicode or text buffer",
710 arg, msgbuf);
712 /* Encode object; use default error handling */
713 s = PyUnicode_AsEncodedString(u,
714 encoding,
715 NULL);
716 Py_DECREF(u);
717 if (s == NULL)
718 return converterr("(encoding failed)",
719 arg, msgbuf);
720 if (!PyString_Check(s)) {
721 Py_DECREF(s);
722 return converterr(
723 "(encoder failed to return a string)",
724 arg, msgbuf);
727 size = PyString_GET_SIZE(s);
729 /* Write output; output is guaranteed to be 0-terminated */
730 if (*format == '#') {
731 /* Using buffer length parameter '#':
733 - if *buffer is NULL, a new buffer of the
734 needed size is allocated and the data
735 copied into it; *buffer is updated to point
736 to the new buffer; the caller is
737 responsible for PyMem_Free()ing it after
738 usage
740 - if *buffer is not NULL, the data is
741 copied to *buffer; *buffer_len has to be
742 set to the size of the buffer on input;
743 buffer overflow is signalled with an error;
744 buffer has to provide enough room for the
745 encoded string plus the trailing 0-byte
747 - in both cases, *buffer_len is updated to
748 the size of the buffer /excluding/ the
749 trailing 0-byte
752 int *buffer_len = va_arg(*p_va, int *);
754 format++;
755 if (buffer_len == NULL)
756 return converterr(
757 "(buffer_len is NULL)",
758 arg, msgbuf);
759 if (*buffer == NULL) {
760 *buffer = PyMem_NEW(char, size + 1);
761 if (*buffer == NULL) {
762 Py_DECREF(s);
763 return converterr(
764 "(memory error)",
765 arg, msgbuf);
767 } else {
768 if (size + 1 > *buffer_len) {
769 Py_DECREF(s);
770 return converterr(
771 "(buffer overflow)",
772 arg, msgbuf);
775 memcpy(*buffer,
776 PyString_AS_STRING(s),
777 size + 1);
778 *buffer_len = size;
779 } else {
780 /* Using a 0-terminated buffer:
782 - the encoded string has to be 0-terminated
783 for this variant to work; if it is not, an
784 error raised
786 - a new buffer of the needed size is
787 allocated and the data copied into it;
788 *buffer is updated to point to the new
789 buffer; the caller is responsible for
790 PyMem_Free()ing it after usage
793 if ((int)strlen(PyString_AS_STRING(s)) != size)
794 return converterr(
795 "(encoded string without NULL bytes)",
796 arg, msgbuf);
797 *buffer = PyMem_NEW(char, size + 1);
798 if (*buffer == NULL) {
799 Py_DECREF(s);
800 return converterr("(memory error)",
801 arg, msgbuf);
803 memcpy(*buffer,
804 PyString_AS_STRING(s),
805 size + 1);
807 Py_DECREF(s);
808 break;
811 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
812 if (*format == '#') { /* any buffer-like object */
813 void **p = (void **)va_arg(*p_va, char **);
814 int *q = va_arg(*p_va, int *);
815 char *buf;
816 int count = convertbuffer(arg, p, &buf);
818 if (count < 0)
819 return converterr(buf, arg, msgbuf);
820 *q = count/(sizeof(Py_UNICODE));
821 format++;
822 } else {
823 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
825 if (PyUnicode_Check(arg))
826 *p = PyUnicode_AS_UNICODE(arg);
827 else
828 return converterr("unicode", arg, msgbuf);
830 break;
833 case 'S': { /* string object */
834 PyObject **p = va_arg(*p_va, PyObject **);
835 if (PyString_Check(arg))
836 *p = arg;
837 else
838 return converterr("string", arg, msgbuf);
839 break;
842 case 'U': { /* Unicode object */
843 PyObject **p = va_arg(*p_va, PyObject **);
844 if (PyUnicode_Check(arg))
845 *p = arg;
846 else
847 return converterr("unicode", arg, msgbuf);
848 break;
851 case 'O': { /* object */
852 PyTypeObject *type;
853 PyObject **p;
854 if (*format == '!') {
855 type = va_arg(*p_va, PyTypeObject*);
856 p = va_arg(*p_va, PyObject **);
857 format++;
858 if (arg->ob_type == type)
859 *p = arg;
860 else
861 return converterr(type->tp_name, arg, msgbuf);
864 else if (*format == '?') {
865 inquiry pred = va_arg(*p_va, inquiry);
866 p = va_arg(*p_va, PyObject **);
867 format++;
868 if ((*pred)(arg))
869 *p = arg;
870 else
871 return converterr("(unspecified)",
872 arg, msgbuf);
875 else if (*format == '&') {
876 typedef int (*converter)(PyObject *, void *);
877 converter convert = va_arg(*p_va, converter);
878 void *addr = va_arg(*p_va, void *);
879 format++;
880 if (! (*convert)(arg, addr))
881 return converterr("(unspecified)",
882 arg, msgbuf);
884 else {
885 p = va_arg(*p_va, PyObject **);
886 *p = arg;
888 break;
892 case 'w': { /* memory buffer, read-write access */
893 void **p = va_arg(*p_va, void **);
894 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
895 int count;
897 if (pb == NULL ||
898 pb->bf_getwritebuffer == NULL ||
899 pb->bf_getsegcount == NULL)
900 return converterr("read-write buffer", arg, msgbuf);
901 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
902 return converterr("single-segment read-write buffer",
903 arg, msgbuf);
904 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
905 return converterr("(unspecified)", arg, msgbuf);
906 if (*format == '#') {
907 int *q = va_arg(*p_va, int *);
909 *q = count;
910 format++;
912 break;
915 case 't': { /* 8-bit character buffer, read-only access */
916 const char **p = va_arg(*p_va, const char **);
917 char *buf;
918 int count;
920 if (*format++ != '#')
921 return converterr(
922 "invalid use of 't' format character",
923 arg, msgbuf);
924 if (!PyType_HasFeature(arg->ob_type,
925 Py_TPFLAGS_HAVE_GETCHARBUFFER))
926 return converterr(
927 "string or read-only character buffer",
928 arg, msgbuf);
930 count = convertbuffer(arg, (void **)p, &buf);
931 if (count < 0)
932 return converterr(buf, arg, msgbuf);
933 *va_arg(*p_va, int *) = count;
934 break;
937 default:
938 return converterr("impossible<bad format char>", arg, msgbuf);
942 *p_format = format;
943 return NULL;
946 int convertbuffer(PyObject *arg, void **p, char **errmsg)
948 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
949 int count;
950 if (pb == NULL ||
951 pb->bf_getreadbuffer == NULL ||
952 pb->bf_getsegcount == NULL) {
953 *errmsg = "string or read-only buffer";
954 return -1;
956 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
957 *errmsg = "string or single-segment read-only buffer";
958 return -1;
960 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
961 *errmsg = "(unspecified)";
963 return count;
966 /* Support for keyword arguments donated by
967 Geoff Philbrick <philbric@delphi.hks.com> */
969 int PyArg_ParseTupleAndKeywords(PyObject *args,
970 PyObject *keywords,
971 char *format,
972 char **kwlist, ...)
974 int retval;
975 va_list va;
977 va_start(va, kwlist);
978 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
979 va_end(va);
980 return retval;
984 static int
985 vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
986 char **kwlist, va_list *p_va)
988 char msgbuf[256];
989 int levels[32];
990 char *fname = NULL;
991 char *message = NULL;
992 int min = -1;
993 int max = 0;
994 char *formatsave = format;
995 int i, len, tplen, kwlen;
996 char *msg, *ks, **p;
997 int nkwds, pos, match, converted;
998 PyObject *key, *value;
1000 /* nested tuples cannot be parsed when using keyword arguments */
1002 for (;;) {
1003 int c = *format++;
1004 if (c == '(') {
1005 PyErr_SetString(PyExc_SystemError,
1006 "tuple found in format when using keyword arguments");
1007 return 0;
1009 else if (c == '\0')
1010 break;
1011 else if (c == ':') {
1012 fname = format;
1013 break;
1014 } else if (c == ';') {
1015 message = format;
1016 break;
1017 } else if (c == 'e')
1018 ; /* Pass */
1019 else if (isalpha(c))
1020 max++;
1021 else if (c == '|')
1022 min = max;
1025 if (min < 0)
1026 min = max;
1028 format = formatsave;
1030 if (!PyTuple_Check(args)) {
1031 PyErr_SetString(PyExc_SystemError,
1032 "new style getargs format but argument is not a tuple");
1033 return 0;
1036 tplen = PyTuple_GET_SIZE(args);
1038 /* do a cursory check of the keywords just to see how many we got */
1040 if (keywords) {
1041 if (!PyDict_Check(keywords)) {
1042 if (keywords == NULL)
1043 PyErr_SetString(PyExc_SystemError,
1044 "NULL received when keyword dictionary expected");
1045 else
1046 PyErr_Format(PyExc_SystemError,
1047 "%s received when keyword dictionary expected",
1048 keywords->ob_type->tp_name);
1049 return 0;
1051 kwlen = PyDict_Size(keywords);
1053 else {
1054 kwlen = 0;
1057 /* make sure there are no duplicate values for an argument;
1058 its not clear when to use the term "keyword argument vs.
1059 keyword parameter in messages */
1061 if (keywords) {
1062 for (i = 0; i < tplen; i++) {
1063 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1064 sprintf(msgbuf,
1065 "keyword parameter %s redefined",
1066 kwlist[i]);
1067 PyErr_SetString(PyExc_TypeError, msgbuf);
1068 return 0;
1072 PyErr_Clear(); /* I'm not which Py functions set the error string */
1074 /* required arguments missing from args can be supplied by keyword
1075 arguments */
1077 len = tplen;
1078 if (keywords && tplen < min) {
1079 for (i = tplen; i < min; i++) {
1080 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1081 len++;
1085 PyErr_Clear();
1087 /* make sure we got an acceptable number of arguments; the message
1088 is a little confusing with keywords since keyword arguments
1089 which are supplied, but don't match the required arguments
1090 are not included in the "%d given" part of the message */
1092 if (len < min || max < len) {
1093 if (message == NULL) {
1094 sprintf(msgbuf,
1095 "%s%s takes %s %d argument%s (%d given)",
1096 fname==NULL ? "function" : fname,
1097 fname==NULL ? "" : "()",
1098 min==max ? "exactly"
1099 : len < min ? "at least" : "at most",
1100 len < min ? min : max,
1101 (len < min ? min : max) == 1 ? "" : "s",
1102 len);
1103 message = msgbuf;
1105 PyErr_SetString(PyExc_TypeError, message);
1106 return 0;
1109 for (i = 0; i < tplen; i++) {
1110 if (*format == '|')
1111 format++;
1112 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
1113 levels, msgbuf);
1114 if (msg) {
1115 seterror(i+1, msg, levels, fname, message);
1116 return 0;
1120 /* handle no keyword parameters in call */
1122 if (!keywords) return 1;
1124 /* make sure the number of keywords in the keyword list matches the
1125 number of items in the format string */
1127 nkwds = 0;
1128 p = kwlist;
1129 for (;;) {
1130 if (!*(p++)) break;
1131 nkwds++;
1134 if (nkwds != max) {
1135 PyErr_SetString(PyExc_SystemError,
1136 "number of items in format string and keyword list do not match");
1137 return 0;
1140 /* convert the keyword arguments; this uses the format
1141 string where it was left after processing args */
1143 converted = 0;
1144 for (i = tplen; i < nkwds; i++) {
1145 PyObject *item;
1146 if (*format == '|')
1147 format++;
1148 item = PyMapping_GetItemString(keywords, kwlist[i]);
1149 if (item != NULL) {
1150 msg = convertitem(item, &format, p_va, levels, msgbuf);
1151 if (msg) {
1152 seterror(i+1, msg, levels, fname, message);
1153 return 0;
1155 converted++;
1156 Py_DECREF(item);
1158 else {
1159 PyErr_Clear();
1160 msg = skipitem(&format, p_va);
1161 if (msg) {
1162 seterror(i+1, msg, levels, fname, message);
1163 return 0;
1168 /* make sure there are no extraneous keyword arguments */
1170 pos = 0;
1171 if (converted < kwlen) {
1172 while (PyDict_Next(keywords, &pos, &key, &value)) {
1173 match = 0;
1174 ks = PyString_AsString(key);
1175 for (i = 0; i < nkwds; i++) {
1176 if (!strcmp(ks, kwlist[i])) {
1177 match = 1;
1178 break;
1181 if (!match) {
1182 sprintf(msgbuf,
1183 "%s is an invalid keyword argument for this function",
1184 ks);
1185 PyErr_SetString(PyExc_TypeError, msgbuf);
1186 return 0;
1191 return 1;
1195 static char *
1196 skipitem(char **p_format, va_list *p_va)
1198 char *format = *p_format;
1199 char c = *format++;
1201 switch (c) {
1203 case 'b': /* byte -- very short int */
1204 case 'B': /* byte as bitfield */
1206 (void) va_arg(*p_va, char *);
1207 break;
1210 case 'h': /* short int */
1212 (void) va_arg(*p_va, short *);
1213 break;
1216 case 'H': /* short int as bitfield */
1218 (void) va_arg(*p_va, unsigned short *);
1219 break;
1222 case 'i': /* int */
1224 (void) va_arg(*p_va, int *);
1225 break;
1228 case 'l': /* long int */
1230 (void) va_arg(*p_va, long *);
1231 break;
1234 #ifdef HAVE_LONG_LONG
1235 case 'L': /* LONG_LONG int */
1237 (void) va_arg(*p_va, LONG_LONG *);
1238 break;
1240 #endif
1242 case 'f': /* float */
1244 (void) va_arg(*p_va, float *);
1245 break;
1248 case 'd': /* double */
1250 (void) va_arg(*p_va, double *);
1251 break;
1254 #ifndef WITHOUT_COMPLEX
1255 case 'D': /* complex double */
1257 (void) va_arg(*p_va, Py_complex *);
1258 break;
1260 #endif /* WITHOUT_COMPLEX */
1262 case 'c': /* char */
1264 (void) va_arg(*p_va, char *);
1265 break;
1268 case 's': /* string */
1270 (void) va_arg(*p_va, char **);
1271 if (*format == '#') {
1272 (void) va_arg(*p_va, int *);
1273 format++;
1275 break;
1278 case 'z': /* string */
1280 (void) va_arg(*p_va, char **);
1281 if (*format == '#') {
1282 (void) va_arg(*p_va, int *);
1283 format++;
1285 break;
1288 case 'S': /* string object */
1290 (void) va_arg(*p_va, PyObject **);
1291 break;
1294 case 'O': /* object */
1296 if (*format == '!') {
1297 format++;
1298 (void) va_arg(*p_va, PyTypeObject*);
1299 (void) va_arg(*p_va, PyObject **);
1301 #if 0
1302 /* I don't know what this is for */
1303 else if (*format == '?') {
1304 inquiry pred = va_arg(*p_va, inquiry);
1305 format++;
1306 if ((*pred)(arg)) {
1307 (void) va_arg(*p_va, PyObject **);
1310 #endif
1311 else if (*format == '&') {
1312 typedef int (*converter)(PyObject *, void *);
1313 (void) va_arg(*p_va, converter);
1314 (void) va_arg(*p_va, void *);
1315 format++;
1317 else {
1318 (void) va_arg(*p_va, PyObject **);
1320 break;
1323 default:
1324 return "impossible<bad format char>";
1328 *p_format = format;
1329 return NULL;