py-cvs-rel2_1 (Rev 1.2) merge
[python/dscho.git] / Python / getargs.c
blob8c00b0ee1a10f2a4b38fe574810891559f855254
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 /* Internal API needed by convertsimple() and a helper macro. */
363 extern
364 PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode,
365 const char *errors);
367 #define UNICODE_DEFAULT_ENCODING(arg) \
368 _PyUnicode_AsDefaultEncodedString(arg, NULL)
370 /* Format an error message generated by convertsimple(). */
372 static char *
373 converterr(char *expected, PyObject *arg, char *msgbuf)
375 assert (expected != NULL);
376 sprintf(msgbuf, "must be %.50s, not %.50s", expected,
377 arg == Py_None ? "None" : arg->ob_type->tp_name);
378 return msgbuf;
381 #define CONV_UNICODE "(unicode conversion error)"
383 /* Convert a non-tuple argument. Return NULL if conversion went OK,
384 or a string with a message describing the failure. The message is
385 formatted as "must be <desired type>, not <actual type>".
386 When failing, an exception may or may not have been raised.
387 Don't call if a tuple is expected.
390 static char *
391 convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf)
393 char *format = *p_format;
394 char c = *format++;
396 switch (c) {
398 case 'b': { /* unsigned byte -- very short int */
399 char *p = va_arg(*p_va, char *);
400 long ival = PyInt_AsLong(arg);
401 if (ival == -1 && PyErr_Occurred())
402 return converterr("integer<b>", arg, msgbuf);
403 else if (ival < 0) {
404 PyErr_SetString(PyExc_OverflowError,
405 "unsigned byte integer is less than minimum");
406 return converterr("integer<b>", arg, msgbuf);
408 else if (ival > UCHAR_MAX) {
409 PyErr_SetString(PyExc_OverflowError,
410 "unsigned byte integer is greater than maximum");
411 return converterr("integer<b>", arg, msgbuf);
413 else
414 *p = (unsigned char) ival;
415 break;
418 case 'B': {/* byte sized bitfield - both signed and unsigned
419 values allowed */
420 char *p = va_arg(*p_va, char *);
421 long ival = PyInt_AsLong(arg);
422 if (ival == -1 && PyErr_Occurred())
423 return converterr("integer<b>", arg, msgbuf);
424 else if (ival < SCHAR_MIN) {
425 PyErr_SetString(PyExc_OverflowError,
426 "byte-sized integer bitfield is less than minimum");
427 return converterr("integer<B>", arg, msgbuf);
429 else if (ival > (int)UCHAR_MAX) {
430 PyErr_SetString(PyExc_OverflowError,
431 "byte-sized integer bitfield is greater than maximum");
432 return converterr("integer<B>", arg, msgbuf);
434 else
435 *p = (unsigned char) ival;
436 break;
439 case 'h': {/* signed short int */
440 short *p = va_arg(*p_va, short *);
441 long ival = PyInt_AsLong(arg);
442 if (ival == -1 && PyErr_Occurred())
443 return converterr("integer<h>", arg, msgbuf);
444 else if (ival < SHRT_MIN) {
445 PyErr_SetString(PyExc_OverflowError,
446 "signed short integer is less than minimum");
447 return converterr("integer<h>", arg, msgbuf);
449 else if (ival > SHRT_MAX) {
450 PyErr_SetString(PyExc_OverflowError,
451 "signed short integer is greater than maximum");
452 return converterr("integer<h>", arg, msgbuf);
454 else
455 *p = (short) ival;
456 break;
459 case 'H': { /* short int sized bitfield, both signed and
460 unsigned allowed */
461 unsigned short *p = va_arg(*p_va, unsigned short *);
462 long ival = PyInt_AsLong(arg);
463 if (ival == -1 && PyErr_Occurred())
464 return converterr("integer<H>", arg, msgbuf);
465 else if (ival < SHRT_MIN) {
466 PyErr_SetString(PyExc_OverflowError,
467 "short integer bitfield is less than minimum");
468 return converterr("integer<H>", arg, msgbuf);
470 else if (ival > USHRT_MAX) {
471 PyErr_SetString(PyExc_OverflowError,
472 "short integer bitfield is greater than maximum");
473 return converterr("integer<H>", arg, msgbuf);
475 else
476 *p = (unsigned short) ival;
477 break;
480 case 'i': {/* signed int */
481 int *p = va_arg(*p_va, int *);
482 long ival = PyInt_AsLong(arg);
483 if (ival == -1 && PyErr_Occurred())
484 return converterr("integer<i>", arg, msgbuf);
485 else if (ival > INT_MAX) {
486 PyErr_SetString(PyExc_OverflowError,
487 "signed integer is greater than maximum");
488 return converterr("integer<i>", arg, msgbuf);
490 else if (ival < INT_MIN) {
491 PyErr_SetString(PyExc_OverflowError,
492 "signed integer is less than minimum");
493 return converterr("integer<i>", arg, msgbuf);
495 else
496 *p = ival;
497 break;
500 case 'l': {/* long int */
501 long *p = va_arg(*p_va, long *);
502 long ival = PyInt_AsLong(arg);
503 if (ival == -1 && PyErr_Occurred())
504 return converterr("integer<l>", arg, msgbuf);
505 else
506 *p = ival;
507 break;
510 #ifdef HAVE_LONG_LONG
511 case 'L': {/* LONG_LONG */
512 LONG_LONG *p = va_arg( *p_va, LONG_LONG * );
513 LONG_LONG ival = PyLong_AsLongLong( arg );
514 if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) {
515 return converterr("long<L>", arg, msgbuf);
516 } else {
517 *p = ival;
519 break;
521 #endif
523 case 'f': {/* float */
524 float *p = va_arg(*p_va, float *);
525 double dval = PyFloat_AsDouble(arg);
526 if (PyErr_Occurred())
527 return converterr("float<f>", arg, msgbuf);
528 else
529 *p = (float) dval;
530 break;
533 case 'd': {/* double */
534 double *p = va_arg(*p_va, double *);
535 double dval = PyFloat_AsDouble(arg);
536 if (PyErr_Occurred())
537 return converterr("float<d>", arg, msgbuf);
538 else
539 *p = dval;
540 break;
543 #ifndef WITHOUT_COMPLEX
544 case 'D': {/* complex double */
545 Py_complex *p = va_arg(*p_va, Py_complex *);
546 Py_complex cval;
547 cval = PyComplex_AsCComplex(arg);
548 if (PyErr_Occurred())
549 return converterr("complex<D>", arg, msgbuf);
550 else
551 *p = cval;
552 break;
554 #endif /* WITHOUT_COMPLEX */
556 case 'c': {/* char */
557 char *p = va_arg(*p_va, char *);
558 if (PyString_Check(arg) && PyString_Size(arg) == 1)
559 *p = PyString_AsString(arg)[0];
560 else
561 return converterr("char", arg, msgbuf);
562 break;
565 case 's': {/* string */
566 if (*format == '#') {
567 void **p = (void **)va_arg(*p_va, char **);
568 int *q = va_arg(*p_va, int *);
570 if (PyString_Check(arg)) {
571 *p = PyString_AS_STRING(arg);
572 *q = PyString_GET_SIZE(arg);
574 else if (PyUnicode_Check(arg)) {
575 arg = UNICODE_DEFAULT_ENCODING(arg);
576 if (arg == NULL)
577 return converterr(CONV_UNICODE,
578 arg, msgbuf);
579 *p = PyString_AS_STRING(arg);
580 *q = PyString_GET_SIZE(arg);
582 else { /* any buffer-like object */
583 char *buf;
584 int count = convertbuffer(arg, p, &buf);
585 if (count < 0)
586 return converterr(buf, arg, msgbuf);
587 *q = count;
589 format++;
590 } else {
591 char **p = va_arg(*p_va, char **);
593 if (PyString_Check(arg))
594 *p = PyString_AS_STRING(arg);
595 else if (PyUnicode_Check(arg)) {
596 arg = UNICODE_DEFAULT_ENCODING(arg);
597 if (arg == NULL)
598 return converterr(CONV_UNICODE,
599 arg, msgbuf);
600 *p = PyString_AS_STRING(arg);
602 else
603 return converterr("string", arg, msgbuf);
604 if ((int)strlen(*p) != PyString_Size(arg))
605 return converterr("string without null bytes",
606 arg, msgbuf);
608 break;
611 case 'z': {/* string, may be NULL (None) */
612 if (*format == '#') { /* any buffer-like object */
613 void **p = (void **)va_arg(*p_va, char **);
614 int *q = va_arg(*p_va, int *);
616 if (arg == Py_None) {
617 *p = 0;
618 *q = 0;
620 else if (PyString_Check(arg)) {
621 *p = PyString_AS_STRING(arg);
622 *q = PyString_GET_SIZE(arg);
624 else if (PyUnicode_Check(arg)) {
625 arg = UNICODE_DEFAULT_ENCODING(arg);
626 if (arg == NULL)
627 return converterr(CONV_UNICODE,
628 arg, msgbuf);
629 *p = PyString_AS_STRING(arg);
630 *q = PyString_GET_SIZE(arg);
632 else { /* any buffer-like object */
633 char *buf;
634 int count = convertbuffer(arg, p, &buf);
636 if (count < 0)
637 return converterr(buf, arg, msgbuf);
638 *q = count;
640 format++;
641 } else {
642 char **p = va_arg(*p_va, char **);
644 if (arg == Py_None)
645 *p = 0;
646 else if (PyString_Check(arg))
647 *p = PyString_AsString(arg);
648 else if (PyUnicode_Check(arg)) {
649 arg = UNICODE_DEFAULT_ENCODING(arg);
650 if (arg == NULL)
651 return converterr(CONV_UNICODE,
652 arg, msgbuf);
653 *p = PyString_AS_STRING(arg);
655 else
656 return converterr("string or None",
657 arg, msgbuf);
658 if (*format == '#') {
659 int *q = va_arg(*p_va, int *);
660 if (arg == Py_None)
661 *q = 0;
662 else
663 *q = PyString_Size(arg);
664 format++;
666 else if (*p != NULL &&
667 (int)strlen(*p) != PyString_Size(arg))
668 return converterr(
669 "string without null bytes or None",
670 arg, msgbuf);
672 break;
675 case 'e': {/* encoded string */
676 char **buffer;
677 const char *encoding;
678 PyObject *u, *s;
679 int size, recode_strings;
681 /* Get 'e' parameter: the encoding name */
682 encoding = (const char *)va_arg(*p_va, const char *);
683 if (encoding == NULL)
684 encoding = PyUnicode_GetDefaultEncoding();
686 /* Get output buffer parameter:
687 's' (recode all objects via Unicode) or
688 't' (only recode non-string objects)
690 if (*format == 's')
691 recode_strings = 1;
692 else if (*format == 't')
693 recode_strings = 0;
694 else
695 return converterr(
696 "(unknown parser marker combination)",
697 arg, msgbuf);
698 buffer = (char **)va_arg(*p_va, char **);
699 format++;
700 if (buffer == NULL)
701 return converterr("(buffer is NULL)",
702 arg, msgbuf);
704 /* Encode object */
705 if (!recode_strings && PyString_Check(arg)) {
706 s = arg;
707 Py_INCREF(s);
709 else {
710 /* Convert object to Unicode */
711 u = PyUnicode_FromObject(arg);
712 if (u == NULL)
713 return converterr(
714 "string or unicode or text buffer",
715 arg, msgbuf);
717 /* Encode object; use default error handling */
718 s = PyUnicode_AsEncodedString(u,
719 encoding,
720 NULL);
721 Py_DECREF(u);
722 if (s == NULL)
723 return converterr("(encoding failed)",
724 arg, msgbuf);
725 if (!PyString_Check(s)) {
726 Py_DECREF(s);
727 return converterr(
728 "(encoder failed to return a string)",
729 arg, msgbuf);
732 size = PyString_GET_SIZE(s);
734 /* Write output; output is guaranteed to be 0-terminated */
735 if (*format == '#') {
736 /* Using buffer length parameter '#':
738 - if *buffer is NULL, a new buffer of the
739 needed size is allocated and the data
740 copied into it; *buffer is updated to point
741 to the new buffer; the caller is
742 responsible for PyMem_Free()ing it after
743 usage
745 - if *buffer is not NULL, the data is
746 copied to *buffer; *buffer_len has to be
747 set to the size of the buffer on input;
748 buffer overflow is signalled with an error;
749 buffer has to provide enough room for the
750 encoded string plus the trailing 0-byte
752 - in both cases, *buffer_len is updated to
753 the size of the buffer /excluding/ the
754 trailing 0-byte
757 int *buffer_len = va_arg(*p_va, int *);
759 format++;
760 if (buffer_len == NULL)
761 return converterr(
762 "(buffer_len is NULL)",
763 arg, msgbuf);
764 if (*buffer == NULL) {
765 *buffer = PyMem_NEW(char, size + 1);
766 if (*buffer == NULL) {
767 Py_DECREF(s);
768 return converterr(
769 "(memory error)",
770 arg, msgbuf);
772 } else {
773 if (size + 1 > *buffer_len) {
774 Py_DECREF(s);
775 return converterr(
776 "(buffer overflow)",
777 arg, msgbuf);
780 memcpy(*buffer,
781 PyString_AS_STRING(s),
782 size + 1);
783 *buffer_len = size;
784 } else {
785 /* Using a 0-terminated buffer:
787 - the encoded string has to be 0-terminated
788 for this variant to work; if it is not, an
789 error raised
791 - a new buffer of the needed size is
792 allocated and the data copied into it;
793 *buffer is updated to point to the new
794 buffer; the caller is responsible for
795 PyMem_Free()ing it after usage
798 if ((int)strlen(PyString_AS_STRING(s)) != size)
799 return converterr(
800 "(encoded string without NULL bytes)",
801 arg, msgbuf);
802 *buffer = PyMem_NEW(char, size + 1);
803 if (*buffer == NULL) {
804 Py_DECREF(s);
805 return converterr("(memory error)",
806 arg, msgbuf);
808 memcpy(*buffer,
809 PyString_AS_STRING(s),
810 size + 1);
812 Py_DECREF(s);
813 break;
816 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
817 if (*format == '#') { /* any buffer-like object */
818 void **p = (void **)va_arg(*p_va, char **);
819 int *q = va_arg(*p_va, int *);
820 char *buf;
821 int count = convertbuffer(arg, p, &buf);
823 if (count < 0)
824 return converterr(buf, arg, msgbuf);
825 *q = count/(sizeof(Py_UNICODE));
826 format++;
827 } else {
828 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
830 if (PyUnicode_Check(arg))
831 *p = PyUnicode_AS_UNICODE(arg);
832 else
833 return converterr("unicode", arg, msgbuf);
835 break;
838 case 'S': { /* string object */
839 PyObject **p = va_arg(*p_va, PyObject **);
840 if (PyString_Check(arg))
841 *p = arg;
842 else
843 return converterr("string", arg, msgbuf);
844 break;
847 case 'U': { /* Unicode object */
848 PyObject **p = va_arg(*p_va, PyObject **);
849 if (PyUnicode_Check(arg))
850 *p = arg;
851 else
852 return converterr("unicode", arg, msgbuf);
853 break;
856 case 'O': { /* object */
857 PyTypeObject *type;
858 PyObject **p;
859 if (*format == '!') {
860 type = va_arg(*p_va, PyTypeObject*);
861 p = va_arg(*p_va, PyObject **);
862 format++;
863 if (arg->ob_type == type)
864 *p = arg;
865 else
866 return converterr(type->tp_name, arg, msgbuf);
869 else if (*format == '?') {
870 inquiry pred = va_arg(*p_va, inquiry);
871 p = va_arg(*p_va, PyObject **);
872 format++;
873 if ((*pred)(arg))
874 *p = arg;
875 else
876 return converterr("(unspecified)",
877 arg, msgbuf);
880 else if (*format == '&') {
881 typedef int (*converter)(PyObject *, void *);
882 converter convert = va_arg(*p_va, converter);
883 void *addr = va_arg(*p_va, void *);
884 format++;
885 if (! (*convert)(arg, addr))
886 return converterr("(unspecified)",
887 arg, msgbuf);
889 else {
890 p = va_arg(*p_va, PyObject **);
891 *p = arg;
893 break;
897 case 'w': { /* memory buffer, read-write access */
898 void **p = va_arg(*p_va, void **);
899 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
900 int count;
902 if (pb == NULL ||
903 pb->bf_getwritebuffer == NULL ||
904 pb->bf_getsegcount == NULL)
905 return converterr("read-write buffer", arg, msgbuf);
906 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
907 return converterr("single-segment read-write buffer",
908 arg, msgbuf);
909 if ((count = pb->bf_getwritebuffer(arg, 0, p)) < 0)
910 return converterr("(unspecified)", arg, msgbuf);
911 if (*format == '#') {
912 int *q = va_arg(*p_va, int *);
914 *q = count;
915 format++;
917 break;
920 case 't': { /* 8-bit character buffer, read-only access */
921 const char **p = va_arg(*p_va, const char **);
922 char *buf;
923 int count;
925 if (*format++ != '#')
926 return converterr(
927 "invalid use of 't' format character",
928 arg, msgbuf);
929 if (!PyType_HasFeature(arg->ob_type,
930 Py_TPFLAGS_HAVE_GETCHARBUFFER))
931 return converterr(
932 "string or read-only character buffer",
933 arg, msgbuf);
935 count = convertbuffer(arg, (void **)p, &buf);
936 if (count < 0)
937 return converterr(buf, arg, msgbuf);
938 *va_arg(*p_va, int *) = count;
939 break;
942 default:
943 return converterr("impossible<bad format char>", arg, msgbuf);
947 *p_format = format;
948 return NULL;
951 int convertbuffer(PyObject *arg, void **p, char **errmsg)
953 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
954 int count;
955 if (pb == NULL ||
956 pb->bf_getreadbuffer == NULL ||
957 pb->bf_getsegcount == NULL) {
958 *errmsg = "string or read-only buffer";
959 return -1;
961 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
962 *errmsg = "string or single-segment read-only buffer";
963 return -1;
965 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
966 *errmsg = "(unspecified)";
968 return count;
971 /* Support for keyword arguments donated by
972 Geoff Philbrick <philbric@delphi.hks.com> */
974 int PyArg_ParseTupleAndKeywords(PyObject *args,
975 PyObject *keywords,
976 char *format,
977 char **kwlist, ...)
979 int retval;
980 va_list va;
982 va_start(va, kwlist);
983 retval = vgetargskeywords(args, keywords, format, kwlist, &va);
984 va_end(va);
985 return retval;
989 static int
990 vgetargskeywords(PyObject *args, PyObject *keywords, char *format,
991 char **kwlist, va_list *p_va)
993 char msgbuf[256];
994 int levels[32];
995 char *fname = NULL;
996 char *message = NULL;
997 int min = -1;
998 int max = 0;
999 char *formatsave = format;
1000 int i, len, tplen, kwlen;
1001 char *msg, *ks, **p;
1002 int nkwds, pos, match, converted;
1003 PyObject *key, *value;
1005 /* nested tuples cannot be parsed when using keyword arguments */
1007 for (;;) {
1008 int c = *format++;
1009 if (c == '(') {
1010 PyErr_SetString(PyExc_SystemError,
1011 "tuple found in format when using keyword arguments");
1012 return 0;
1014 else if (c == '\0')
1015 break;
1016 else if (c == ':') {
1017 fname = format;
1018 break;
1019 } else if (c == ';') {
1020 message = format;
1021 break;
1022 } else if (c == 'e')
1023 ; /* Pass */
1024 else if (isalpha(c))
1025 max++;
1026 else if (c == '|')
1027 min = max;
1030 if (min < 0)
1031 min = max;
1033 format = formatsave;
1035 if (!PyTuple_Check(args)) {
1036 PyErr_SetString(PyExc_SystemError,
1037 "new style getargs format but argument is not a tuple");
1038 return 0;
1041 tplen = PyTuple_GET_SIZE(args);
1043 /* do a cursory check of the keywords just to see how many we got */
1045 if (keywords) {
1046 if (!PyDict_Check(keywords)) {
1047 if (keywords == NULL)
1048 PyErr_SetString(PyExc_SystemError,
1049 "NULL received when keyword dictionary expected");
1050 else
1051 PyErr_Format(PyExc_SystemError,
1052 "%s received when keyword dictionary expected",
1053 keywords->ob_type->tp_name);
1054 return 0;
1056 kwlen = PyDict_Size(keywords);
1058 else {
1059 kwlen = 0;
1062 /* make sure there are no duplicate values for an argument;
1063 its not clear when to use the term "keyword argument vs.
1064 keyword parameter in messages */
1066 if (keywords) {
1067 for (i = 0; i < tplen; i++) {
1068 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1069 sprintf(msgbuf,
1070 "keyword parameter %s redefined",
1071 kwlist[i]);
1072 PyErr_SetString(PyExc_TypeError, msgbuf);
1073 return 0;
1077 PyErr_Clear(); /* I'm not which Py functions set the error string */
1079 /* required arguments missing from args can be supplied by keyword
1080 arguments */
1082 len = tplen;
1083 if (keywords && tplen < min) {
1084 for (i = tplen; i < min; i++) {
1085 if (PyMapping_HasKeyString(keywords, kwlist[i])) {
1086 len++;
1090 PyErr_Clear();
1092 /* make sure we got an acceptable number of arguments; the message
1093 is a little confusing with keywords since keyword arguments
1094 which are supplied, but don't match the required arguments
1095 are not included in the "%d given" part of the message */
1097 if (len < min || max < len) {
1098 if (message == NULL) {
1099 sprintf(msgbuf,
1100 "%s%s takes %s %d argument%s (%d given)",
1101 fname==NULL ? "function" : fname,
1102 fname==NULL ? "" : "()",
1103 min==max ? "exactly"
1104 : len < min ? "at least" : "at most",
1105 len < min ? min : max,
1106 (len < min ? min : max) == 1 ? "" : "s",
1107 len);
1108 message = msgbuf;
1110 PyErr_SetString(PyExc_TypeError, message);
1111 return 0;
1114 for (i = 0; i < tplen; i++) {
1115 if (*format == '|')
1116 format++;
1117 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
1118 levels, msgbuf);
1119 if (msg) {
1120 seterror(i+1, msg, levels, fname, message);
1121 return 0;
1125 /* handle no keyword parameters in call */
1127 if (!keywords) return 1;
1129 /* make sure the number of keywords in the keyword list matches the
1130 number of items in the format string */
1132 nkwds = 0;
1133 p = kwlist;
1134 for (;;) {
1135 if (!*(p++)) break;
1136 nkwds++;
1139 if (nkwds != max) {
1140 PyErr_SetString(PyExc_SystemError,
1141 "number of items in format string and keyword list do not match");
1142 return 0;
1145 /* convert the keyword arguments; this uses the format
1146 string where it was left after processing args */
1148 converted = 0;
1149 for (i = tplen; i < nkwds; i++) {
1150 PyObject *item;
1151 if (*format == '|')
1152 format++;
1153 item = PyMapping_GetItemString(keywords, kwlist[i]);
1154 if (item != NULL) {
1155 msg = convertitem(item, &format, p_va, levels, msgbuf);
1156 if (msg) {
1157 seterror(i+1, msg, levels, fname, message);
1158 return 0;
1160 converted++;
1161 Py_DECREF(item);
1163 else {
1164 PyErr_Clear();
1165 msg = skipitem(&format, p_va);
1166 if (msg) {
1167 seterror(i+1, msg, levels, fname, message);
1168 return 0;
1173 /* make sure there are no extraneous keyword arguments */
1175 pos = 0;
1176 if (converted < kwlen) {
1177 while (PyDict_Next(keywords, &pos, &key, &value)) {
1178 match = 0;
1179 ks = PyString_AsString(key);
1180 for (i = 0; i < nkwds; i++) {
1181 if (!strcmp(ks, kwlist[i])) {
1182 match = 1;
1183 break;
1186 if (!match) {
1187 sprintf(msgbuf,
1188 "%s is an invalid keyword argument for this function",
1189 ks);
1190 PyErr_SetString(PyExc_TypeError, msgbuf);
1191 return 0;
1196 return 1;
1200 static char *
1201 skipitem(char **p_format, va_list *p_va)
1203 char *format = *p_format;
1204 char c = *format++;
1206 switch (c) {
1208 case 'b': /* byte -- very short int */
1209 case 'B': /* byte as bitfield */
1211 (void) va_arg(*p_va, char *);
1212 break;
1215 case 'h': /* short int */
1217 (void) va_arg(*p_va, short *);
1218 break;
1221 case 'H': /* short int as bitfield */
1223 (void) va_arg(*p_va, unsigned short *);
1224 break;
1227 case 'i': /* int */
1229 (void) va_arg(*p_va, int *);
1230 break;
1233 case 'l': /* long int */
1235 (void) va_arg(*p_va, long *);
1236 break;
1239 #ifdef HAVE_LONG_LONG
1240 case 'L': /* LONG_LONG int */
1242 (void) va_arg(*p_va, LONG_LONG *);
1243 break;
1245 #endif
1247 case 'f': /* float */
1249 (void) va_arg(*p_va, float *);
1250 break;
1253 case 'd': /* double */
1255 (void) va_arg(*p_va, double *);
1256 break;
1259 #ifndef WITHOUT_COMPLEX
1260 case 'D': /* complex double */
1262 (void) va_arg(*p_va, Py_complex *);
1263 break;
1265 #endif /* WITHOUT_COMPLEX */
1267 case 'c': /* char */
1269 (void) va_arg(*p_va, char *);
1270 break;
1273 case 's': /* string */
1275 (void) va_arg(*p_va, char **);
1276 if (*format == '#') {
1277 (void) va_arg(*p_va, int *);
1278 format++;
1280 break;
1283 case 'z': /* string */
1285 (void) va_arg(*p_va, char **);
1286 if (*format == '#') {
1287 (void) va_arg(*p_va, int *);
1288 format++;
1290 break;
1293 case 'S': /* string object */
1295 (void) va_arg(*p_va, PyObject **);
1296 break;
1299 case 'O': /* object */
1301 if (*format == '!') {
1302 format++;
1303 (void) va_arg(*p_va, PyTypeObject*);
1304 (void) va_arg(*p_va, PyObject **);
1306 #if 0
1307 /* I don't know what this is for */
1308 else if (*format == '?') {
1309 inquiry pred = va_arg(*p_va, inquiry);
1310 format++;
1311 if ((*pred)(arg)) {
1312 (void) va_arg(*p_va, PyObject **);
1315 #endif
1316 else if (*format == '&') {
1317 typedef int (*converter)(PyObject *, void *);
1318 (void) va_arg(*p_va, converter);
1319 (void) va_arg(*p_va, void *);
1320 format++;
1322 else {
1323 (void) va_arg(*p_va, PyObject **);
1325 break;
1328 default:
1329 return "impossible<bad format char>";
1333 *p_format = format;
1334 return NULL;