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. */
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 **, ...);
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 *,
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
, ...)
40 retval
= vgetargs1(args
, format
, &va
, 1);
46 int PyArg_ParseTuple(PyObject
*args
, char *format
, ...)
52 retval
= vgetargs1(args
, format
, &va
, 0);
59 PyArg_VaParse(PyObject
*args
, char *format
, va_list va
)
63 #ifdef VA_LIST_IS_ARRAY
64 memcpy(lva
, va
, sizeof(va_list));
69 return vgetargs1(args
, format
, &lva
, 0);
74 vgetargs1(PyObject
*args
, char *format
, va_list *p_va
, int compat
)
84 char *formatsave
= format
;
88 assert(compat
|| (args
!= (PyObject
*)NULL
));
100 Py_FatalError("excess ')' in getargs format");
119 else if (isalpha(c
)) {
120 if (c
!= 'e') /* skip encoded */
130 Py_FatalError(/* '(' */ "missing ')' in getargs format");
141 sprintf(msgbuf
, "%s%s takes no arguments",
142 fname
==NULL
? "function" : fname
,
143 fname
==NULL
? "" : "()");
144 PyErr_SetString(PyExc_TypeError
, msgbuf
);
147 else if (min
== 1 && max
== 1) {
150 "%s%s takes at least one argument",
151 fname
==NULL
? "function" : fname
,
152 fname
==NULL
? "" : "()");
153 PyErr_SetString(PyExc_TypeError
, msgbuf
);
156 msg
= convertitem(args
, &format
, p_va
, levels
, msgbuf
);
159 seterror(levels
[0], msg
, levels
+1, fname
, message
);
163 PyErr_SetString(PyExc_SystemError
,
164 "old style getargs format uses new features");
169 if (!PyTuple_Check(args
)) {
170 PyErr_SetString(PyExc_SystemError
,
171 "new style getargs format but argument is not a tuple");
175 len
= PyTuple_GET_SIZE(args
);
177 if (len
< min
|| max
< len
) {
178 if (message
== NULL
) {
180 "%s%s takes %s %d argument%s (%d given)",
181 fname
==NULL
? "function" : fname
,
182 fname
==NULL
? "" : "()",
184 : len
< min
? "at least" : "at most",
185 len
< min
? min
: max
,
186 (len
< min
? min
: max
) == 1 ? "" : "s",
190 PyErr_SetString(PyExc_TypeError
, message
);
194 for (i
= 0; i
< len
; i
++) {
197 msg
= convertitem(PyTuple_GET_ITEM(args
, i
), &format
, p_va
,
200 seterror(i
+1, msg
, levels
, fname
, message
);
205 if (*format
!= '\0' && !isalpha((int)(*format
)) &&
207 *format
!= '|' && *format
!= ':' && *format
!= ';') {
208 PyErr_Format(PyExc_SystemError
,
209 "bad format string: %.200s", formatsave
);
219 seterror(int iarg
, char *msg
, int *levels
, char *fname
, char *message
)
225 if (PyErr_Occurred())
227 else if (message
== NULL
) {
229 sprintf(p
, "%s() ", fname
);
233 sprintf(p
, "argument %d", iarg
);
236 while (levels
[i
] > 0) {
237 sprintf(p
, ", item %d", levels
[i
]-1);
243 sprintf(p
, "argument");
246 sprintf(p
, " %s", msg
);
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 ')'.
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,
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.
272 converttuple(PyObject
*arg
, char **p_format
, va_list *p_va
, int *levels
,
273 char *msgbuf
, int toplevel
)
277 char *format
= *p_format
;
292 else if (c
== ':' || c
== ';' || c
== '\0')
294 else if (level
== 0 && isalpha(c
))
298 if (!PySequence_Check(arg
) || PyString_Check(arg
)) {
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
);
307 if ((i
= PySequence_Size(arg
)) != n
) {
310 toplevel
? "expected %d arguments, not %d" :
311 "must be sequence of length %d, not %d",
317 for (i
= 0; i
< n
; i
++) {
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 */
335 /* Convert a single item. */
338 convertitem(PyObject
*arg
, char **p_format
, va_list *p_va
, int *levels
,
342 char *format
= *p_format
;
344 if (*format
== '(' /* ')' */) {
346 msg
= converttuple(arg
, &format
, p_va
, levels
, msgbuf
, 0);
351 msg
= convertsimple(arg
, &format
, p_va
, msgbuf
);
362 #define UNICODE_DEFAULT_ENCODING(arg) \
363 _PyUnicode_AsDefaultEncodedString(arg, NULL)
365 /* Format an error message generated by convertsimple(). */
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
);
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.
386 convertsimple(PyObject
*arg
, char **p_format
, va_list *p_va
, char *msgbuf
)
388 char *format
= *p_format
;
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
);
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
);
409 *p
= (unsigned char) ival
;
413 case 'B': {/* byte sized bitfield - both signed and unsigned
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
);
430 *p
= (unsigned char) ival
;
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
);
454 case 'H': { /* short int sized bitfield, both signed and
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
);
471 *p
= (unsigned short) ival
;
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
);
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
);
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
);
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
);
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
);
538 #ifndef WITHOUT_COMPLEX
539 case 'D': {/* complex double */
540 Py_complex
*p
= va_arg(*p_va
, Py_complex
*);
542 cval
= PyComplex_AsCComplex(arg
);
543 if (PyErr_Occurred())
544 return converterr("complex<D>", arg
, msgbuf
);
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];
556 return converterr("char", arg
, msgbuf
);
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
);
572 return converterr(CONV_UNICODE
,
574 *p
= PyString_AS_STRING(arg
);
575 *q
= PyString_GET_SIZE(arg
);
577 else { /* any buffer-like object */
579 int count
= convertbuffer(arg
, p
, &buf
);
581 return converterr(buf
, arg
, msgbuf
);
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
);
593 return converterr(CONV_UNICODE
,
595 *p
= PyString_AS_STRING(arg
);
598 return converterr("string", arg
, msgbuf
);
599 if ((int)strlen(*p
) != PyString_Size(arg
))
600 return converterr("string without null bytes",
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
) {
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
);
622 return converterr(CONV_UNICODE
,
624 *p
= PyString_AS_STRING(arg
);
625 *q
= PyString_GET_SIZE(arg
);
627 else { /* any buffer-like object */
629 int count
= convertbuffer(arg
, p
, &buf
);
632 return converterr(buf
, arg
, msgbuf
);
637 char **p
= va_arg(*p_va
, char **);
641 else if (PyString_Check(arg
))
642 *p
= PyString_AsString(arg
);
643 else if (PyUnicode_Check(arg
)) {
644 arg
= UNICODE_DEFAULT_ENCODING(arg
);
646 return converterr(CONV_UNICODE
,
648 *p
= PyString_AS_STRING(arg
);
651 return converterr("string or None",
653 if (*format
== '#') {
654 int *q
= va_arg(*p_va
, int *);
658 *q
= PyString_Size(arg
);
661 else if (*p
!= NULL
&&
662 (int)strlen(*p
) != PyString_Size(arg
))
664 "string without null bytes or None",
670 case 'e': {/* encoded string */
672 const char *encoding
;
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)
687 else if (*format
== 't')
691 "(unknown parser marker combination)",
693 buffer
= (char **)va_arg(*p_va
, char **);
696 return converterr("(buffer is NULL)",
700 if (!recode_strings
&& PyString_Check(arg
)) {
705 /* Convert object to Unicode */
706 u
= PyUnicode_FromObject(arg
);
709 "string or unicode or text buffer",
712 /* Encode object; use default error handling */
713 s
= PyUnicode_AsEncodedString(u
,
718 return converterr("(encoding failed)",
720 if (!PyString_Check(s
)) {
723 "(encoder failed to return a string)",
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
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
752 int *buffer_len
= va_arg(*p_va
, int *);
755 if (buffer_len
== NULL
)
757 "(buffer_len is NULL)",
759 if (*buffer
== NULL
) {
760 *buffer
= PyMem_NEW(char, size
+ 1);
761 if (*buffer
== NULL
) {
768 if (size
+ 1 > *buffer_len
) {
776 PyString_AS_STRING(s
),
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
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
)
795 "(encoded string without NULL bytes)",
797 *buffer
= PyMem_NEW(char, size
+ 1);
798 if (*buffer
== NULL
) {
800 return converterr("(memory error)",
804 PyString_AS_STRING(s
),
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 *);
816 int count
= convertbuffer(arg
, p
, &buf
);
819 return converterr(buf
, arg
, msgbuf
);
820 *q
= count
/(sizeof(Py_UNICODE
));
823 Py_UNICODE
**p
= va_arg(*p_va
, Py_UNICODE
**);
825 if (PyUnicode_Check(arg
))
826 *p
= PyUnicode_AS_UNICODE(arg
);
828 return converterr("unicode", arg
, msgbuf
);
833 case 'S': { /* string object */
834 PyObject
**p
= va_arg(*p_va
, PyObject
**);
835 if (PyString_Check(arg
))
838 return converterr("string", arg
, msgbuf
);
842 case 'U': { /* Unicode object */
843 PyObject
**p
= va_arg(*p_va
, PyObject
**);
844 if (PyUnicode_Check(arg
))
847 return converterr("unicode", arg
, msgbuf
);
851 case 'O': { /* object */
854 if (*format
== '!') {
855 type
= va_arg(*p_va
, PyTypeObject
*);
856 p
= va_arg(*p_va
, PyObject
**);
858 if (arg
->ob_type
== type
)
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
**);
871 return converterr("(unspecified)",
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 *);
880 if (! (*convert
)(arg
, addr
))
881 return converterr("(unspecified)",
885 p
= va_arg(*p_va
, PyObject
**);
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
;
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",
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 *);
915 case 't': { /* 8-bit character buffer, read-only access */
916 const char **p
= va_arg(*p_va
, const char **);
920 if (*format
++ != '#')
922 "invalid use of 't' format character",
924 if (!PyType_HasFeature(arg
->ob_type
,
925 Py_TPFLAGS_HAVE_GETCHARBUFFER
))
927 "string or read-only character buffer",
930 count
= convertbuffer(arg
, (void **)p
, &buf
);
932 return converterr(buf
, arg
, msgbuf
);
933 *va_arg(*p_va
, int *) = count
;
938 return converterr("impossible<bad format char>", arg
, msgbuf
);
946 int convertbuffer(PyObject
*arg
, void **p
, char **errmsg
)
948 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
951 pb
->bf_getreadbuffer
== NULL
||
952 pb
->bf_getsegcount
== NULL
) {
953 *errmsg
= "string or read-only buffer";
956 if ((*pb
->bf_getsegcount
)(arg
, NULL
) != 1) {
957 *errmsg
= "string or single-segment read-only buffer";
960 if ((count
= (*pb
->bf_getreadbuffer
)(arg
, 0, p
)) < 0) {
961 *errmsg
= "(unspecified)";
966 /* Support for keyword arguments donated by
967 Geoff Philbrick <philbric@delphi.hks.com> */
969 int PyArg_ParseTupleAndKeywords(PyObject
*args
,
977 va_start(va
, kwlist
);
978 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &va
);
985 vgetargskeywords(PyObject
*args
, PyObject
*keywords
, char *format
,
986 char **kwlist
, va_list *p_va
)
991 char *message
= NULL
;
994 char *formatsave
= format
;
995 int i
, len
, tplen
, kwlen
;
997 int nkwds
, pos
, match
, converted
;
998 PyObject
*key
, *value
;
1000 /* nested tuples cannot be parsed when using keyword arguments */
1005 PyErr_SetString(PyExc_SystemError
,
1006 "tuple found in format when using keyword arguments");
1011 else if (c
== ':') {
1014 } else if (c
== ';') {
1017 } else if (c
== 'e')
1019 else if (isalpha(c
))
1028 format
= formatsave
;
1030 if (!PyTuple_Check(args
)) {
1031 PyErr_SetString(PyExc_SystemError
,
1032 "new style getargs format but argument is not a tuple");
1036 tplen
= PyTuple_GET_SIZE(args
);
1038 /* do a cursory check of the keywords just to see how many we got */
1041 if (!PyDict_Check(keywords
)) {
1042 if (keywords
== NULL
)
1043 PyErr_SetString(PyExc_SystemError
,
1044 "NULL received when keyword dictionary expected");
1046 PyErr_Format(PyExc_SystemError
,
1047 "%s received when keyword dictionary expected",
1048 keywords
->ob_type
->tp_name
);
1051 kwlen
= PyDict_Size(keywords
);
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 */
1062 for (i
= 0; i
< tplen
; i
++) {
1063 if (PyMapping_HasKeyString(keywords
, kwlist
[i
])) {
1065 "keyword parameter %s redefined",
1067 PyErr_SetString(PyExc_TypeError
, msgbuf
);
1072 PyErr_Clear(); /* I'm not which Py functions set the error string */
1074 /* required arguments missing from args can be supplied by keyword
1078 if (keywords
&& tplen
< min
) {
1079 for (i
= tplen
; i
< min
; i
++) {
1080 if (PyMapping_HasKeyString(keywords
, kwlist
[i
])) {
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
) {
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",
1105 PyErr_SetString(PyExc_TypeError
, message
);
1109 for (i
= 0; i
< tplen
; i
++) {
1112 msg
= convertitem(PyTuple_GET_ITEM(args
, i
), &format
, p_va
,
1115 seterror(i
+1, msg
, levels
, fname
, message
);
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 */
1135 PyErr_SetString(PyExc_SystemError
,
1136 "number of items in format string and keyword list do not match");
1140 /* convert the keyword arguments; this uses the format
1141 string where it was left after processing args */
1144 for (i
= tplen
; i
< nkwds
; i
++) {
1148 item
= PyMapping_GetItemString(keywords
, kwlist
[i
]);
1150 msg
= convertitem(item
, &format
, p_va
, levels
, msgbuf
);
1152 seterror(i
+1, msg
, levels
, fname
, message
);
1160 msg
= skipitem(&format
, p_va
);
1162 seterror(i
+1, msg
, levels
, fname
, message
);
1168 /* make sure there are no extraneous keyword arguments */
1171 if (converted
< kwlen
) {
1172 while (PyDict_Next(keywords
, &pos
, &key
, &value
)) {
1174 ks
= PyString_AsString(key
);
1175 for (i
= 0; i
< nkwds
; i
++) {
1176 if (!strcmp(ks
, kwlist
[i
])) {
1183 "%s is an invalid keyword argument for this function",
1185 PyErr_SetString(PyExc_TypeError
, msgbuf
);
1196 skipitem(char **p_format
, va_list *p_va
)
1198 char *format
= *p_format
;
1203 case 'b': /* byte -- very short int */
1204 case 'B': /* byte as bitfield */
1206 (void) va_arg(*p_va
, char *);
1210 case 'h': /* short int */
1212 (void) va_arg(*p_va
, short *);
1216 case 'H': /* short int as bitfield */
1218 (void) va_arg(*p_va
, unsigned short *);
1224 (void) va_arg(*p_va
, int *);
1228 case 'l': /* long int */
1230 (void) va_arg(*p_va
, long *);
1234 #ifdef HAVE_LONG_LONG
1235 case 'L': /* LONG_LONG int */
1237 (void) va_arg(*p_va
, LONG_LONG
*);
1242 case 'f': /* float */
1244 (void) va_arg(*p_va
, float *);
1248 case 'd': /* double */
1250 (void) va_arg(*p_va
, double *);
1254 #ifndef WITHOUT_COMPLEX
1255 case 'D': /* complex double */
1257 (void) va_arg(*p_va
, Py_complex
*);
1260 #endif /* WITHOUT_COMPLEX */
1262 case 'c': /* char */
1264 (void) va_arg(*p_va
, char *);
1268 case 's': /* string */
1270 (void) va_arg(*p_va
, char **);
1271 if (*format
== '#') {
1272 (void) va_arg(*p_va
, int *);
1278 case 'z': /* string */
1280 (void) va_arg(*p_va
, char **);
1281 if (*format
== '#') {
1282 (void) va_arg(*p_va
, int *);
1288 case 'S': /* string object */
1290 (void) va_arg(*p_va
, PyObject
**);
1294 case 'O': /* object */
1296 if (*format
== '!') {
1298 (void) va_arg(*p_va
, PyTypeObject
*);
1299 (void) va_arg(*p_va
, PyObject
**);
1302 /* I don't know what this is for */
1303 else if (*format
== '?') {
1304 inquiry pred
= va_arg(*p_va
, inquiry
);
1307 (void) va_arg(*p_va
, PyObject
**);
1311 else if (*format
== '&') {
1312 typedef int (*converter
)(PyObject
*, void *);
1313 (void) va_arg(*p_va
, converter
);
1314 (void) va_arg(*p_va
, void *);
1318 (void) va_arg(*p_va
, PyObject
**);
1324 return "impossible<bad format char>";