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 /* Internal API needed by convertsimple() and a helper macro. */
364 PyObject
*_PyUnicode_AsDefaultEncodedString(PyObject
*unicode
,
367 #define UNICODE_DEFAULT_ENCODING(arg) \
368 _PyUnicode_AsDefaultEncodedString(arg, NULL)
370 /* Format an error message generated by convertsimple(). */
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
);
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.
391 convertsimple(PyObject
*arg
, char **p_format
, va_list *p_va
, char *msgbuf
)
393 char *format
= *p_format
;
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
);
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
);
414 *p
= (unsigned char) ival
;
418 case 'B': {/* byte sized bitfield - both signed and unsigned
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
);
435 *p
= (unsigned char) ival
;
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
);
459 case 'H': { /* short int sized bitfield, both signed and
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
);
476 *p
= (unsigned short) ival
;
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
);
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
);
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
);
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
);
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
);
543 #ifndef WITHOUT_COMPLEX
544 case 'D': {/* complex double */
545 Py_complex
*p
= va_arg(*p_va
, Py_complex
*);
547 cval
= PyComplex_AsCComplex(arg
);
548 if (PyErr_Occurred())
549 return converterr("complex<D>", arg
, msgbuf
);
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];
561 return converterr("char", arg
, msgbuf
);
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
);
577 return converterr(CONV_UNICODE
,
579 *p
= PyString_AS_STRING(arg
);
580 *q
= PyString_GET_SIZE(arg
);
582 else { /* any buffer-like object */
584 int count
= convertbuffer(arg
, p
, &buf
);
586 return converterr(buf
, arg
, msgbuf
);
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
);
598 return converterr(CONV_UNICODE
,
600 *p
= PyString_AS_STRING(arg
);
603 return converterr("string", arg
, msgbuf
);
604 if ((int)strlen(*p
) != PyString_Size(arg
))
605 return converterr("string without null bytes",
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
) {
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
);
627 return converterr(CONV_UNICODE
,
629 *p
= PyString_AS_STRING(arg
);
630 *q
= PyString_GET_SIZE(arg
);
632 else { /* any buffer-like object */
634 int count
= convertbuffer(arg
, p
, &buf
);
637 return converterr(buf
, arg
, msgbuf
);
642 char **p
= va_arg(*p_va
, char **);
646 else if (PyString_Check(arg
))
647 *p
= PyString_AsString(arg
);
648 else if (PyUnicode_Check(arg
)) {
649 arg
= UNICODE_DEFAULT_ENCODING(arg
);
651 return converterr(CONV_UNICODE
,
653 *p
= PyString_AS_STRING(arg
);
656 return converterr("string or None",
658 if (*format
== '#') {
659 int *q
= va_arg(*p_va
, int *);
663 *q
= PyString_Size(arg
);
666 else if (*p
!= NULL
&&
667 (int)strlen(*p
) != PyString_Size(arg
))
669 "string without null bytes or None",
675 case 'e': {/* encoded string */
677 const char *encoding
;
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)
692 else if (*format
== 't')
696 "(unknown parser marker combination)",
698 buffer
= (char **)va_arg(*p_va
, char **);
701 return converterr("(buffer is NULL)",
705 if (!recode_strings
&& PyString_Check(arg
)) {
710 /* Convert object to Unicode */
711 u
= PyUnicode_FromObject(arg
);
714 "string or unicode or text buffer",
717 /* Encode object; use default error handling */
718 s
= PyUnicode_AsEncodedString(u
,
723 return converterr("(encoding failed)",
725 if (!PyString_Check(s
)) {
728 "(encoder failed to return a string)",
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
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
757 int *buffer_len
= va_arg(*p_va
, int *);
760 if (buffer_len
== NULL
)
762 "(buffer_len is NULL)",
764 if (*buffer
== NULL
) {
765 *buffer
= PyMem_NEW(char, size
+ 1);
766 if (*buffer
== NULL
) {
773 if (size
+ 1 > *buffer_len
) {
781 PyString_AS_STRING(s
),
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
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
)
800 "(encoded string without NULL bytes)",
802 *buffer
= PyMem_NEW(char, size
+ 1);
803 if (*buffer
== NULL
) {
805 return converterr("(memory error)",
809 PyString_AS_STRING(s
),
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 *);
821 int count
= convertbuffer(arg
, p
, &buf
);
824 return converterr(buf
, arg
, msgbuf
);
825 *q
= count
/(sizeof(Py_UNICODE
));
828 Py_UNICODE
**p
= va_arg(*p_va
, Py_UNICODE
**);
830 if (PyUnicode_Check(arg
))
831 *p
= PyUnicode_AS_UNICODE(arg
);
833 return converterr("unicode", arg
, msgbuf
);
838 case 'S': { /* string object */
839 PyObject
**p
= va_arg(*p_va
, PyObject
**);
840 if (PyString_Check(arg
))
843 return converterr("string", arg
, msgbuf
);
847 case 'U': { /* Unicode object */
848 PyObject
**p
= va_arg(*p_va
, PyObject
**);
849 if (PyUnicode_Check(arg
))
852 return converterr("unicode", arg
, msgbuf
);
856 case 'O': { /* object */
859 if (*format
== '!') {
860 type
= va_arg(*p_va
, PyTypeObject
*);
861 p
= va_arg(*p_va
, PyObject
**);
863 if (arg
->ob_type
== type
)
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
**);
876 return converterr("(unspecified)",
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 *);
885 if (! (*convert
)(arg
, addr
))
886 return converterr("(unspecified)",
890 p
= va_arg(*p_va
, PyObject
**);
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
;
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",
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 *);
920 case 't': { /* 8-bit character buffer, read-only access */
921 const char **p
= va_arg(*p_va
, const char **);
925 if (*format
++ != '#')
927 "invalid use of 't' format character",
929 if (!PyType_HasFeature(arg
->ob_type
,
930 Py_TPFLAGS_HAVE_GETCHARBUFFER
))
932 "string or read-only character buffer",
935 count
= convertbuffer(arg
, (void **)p
, &buf
);
937 return converterr(buf
, arg
, msgbuf
);
938 *va_arg(*p_va
, int *) = count
;
943 return converterr("impossible<bad format char>", arg
, msgbuf
);
951 int convertbuffer(PyObject
*arg
, void **p
, char **errmsg
)
953 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
956 pb
->bf_getreadbuffer
== NULL
||
957 pb
->bf_getsegcount
== NULL
) {
958 *errmsg
= "string or read-only buffer";
961 if ((*pb
->bf_getsegcount
)(arg
, NULL
) != 1) {
962 *errmsg
= "string or single-segment read-only buffer";
965 if ((count
= (*pb
->bf_getreadbuffer
)(arg
, 0, p
)) < 0) {
966 *errmsg
= "(unspecified)";
971 /* Support for keyword arguments donated by
972 Geoff Philbrick <philbric@delphi.hks.com> */
974 int PyArg_ParseTupleAndKeywords(PyObject
*args
,
982 va_start(va
, kwlist
);
983 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &va
);
990 vgetargskeywords(PyObject
*args
, PyObject
*keywords
, char *format
,
991 char **kwlist
, va_list *p_va
)
996 char *message
= NULL
;
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 */
1010 PyErr_SetString(PyExc_SystemError
,
1011 "tuple found in format when using keyword arguments");
1016 else if (c
== ':') {
1019 } else if (c
== ';') {
1022 } else if (c
== 'e')
1024 else if (isalpha(c
))
1033 format
= formatsave
;
1035 if (!PyTuple_Check(args
)) {
1036 PyErr_SetString(PyExc_SystemError
,
1037 "new style getargs format but argument is not a tuple");
1041 tplen
= PyTuple_GET_SIZE(args
);
1043 /* do a cursory check of the keywords just to see how many we got */
1046 if (!PyDict_Check(keywords
)) {
1047 if (keywords
== NULL
)
1048 PyErr_SetString(PyExc_SystemError
,
1049 "NULL received when keyword dictionary expected");
1051 PyErr_Format(PyExc_SystemError
,
1052 "%s received when keyword dictionary expected",
1053 keywords
->ob_type
->tp_name
);
1056 kwlen
= PyDict_Size(keywords
);
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 */
1067 for (i
= 0; i
< tplen
; i
++) {
1068 if (PyMapping_HasKeyString(keywords
, kwlist
[i
])) {
1070 "keyword parameter %s redefined",
1072 PyErr_SetString(PyExc_TypeError
, msgbuf
);
1077 PyErr_Clear(); /* I'm not which Py functions set the error string */
1079 /* required arguments missing from args can be supplied by keyword
1083 if (keywords
&& tplen
< min
) {
1084 for (i
= tplen
; i
< min
; i
++) {
1085 if (PyMapping_HasKeyString(keywords
, kwlist
[i
])) {
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
) {
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",
1110 PyErr_SetString(PyExc_TypeError
, message
);
1114 for (i
= 0; i
< tplen
; i
++) {
1117 msg
= convertitem(PyTuple_GET_ITEM(args
, i
), &format
, p_va
,
1120 seterror(i
+1, msg
, levels
, fname
, message
);
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 */
1140 PyErr_SetString(PyExc_SystemError
,
1141 "number of items in format string and keyword list do not match");
1145 /* convert the keyword arguments; this uses the format
1146 string where it was left after processing args */
1149 for (i
= tplen
; i
< nkwds
; i
++) {
1153 item
= PyMapping_GetItemString(keywords
, kwlist
[i
]);
1155 msg
= convertitem(item
, &format
, p_va
, levels
, msgbuf
);
1157 seterror(i
+1, msg
, levels
, fname
, message
);
1165 msg
= skipitem(&format
, p_va
);
1167 seterror(i
+1, msg
, levels
, fname
, message
);
1173 /* make sure there are no extraneous keyword arguments */
1176 if (converted
< kwlen
) {
1177 while (PyDict_Next(keywords
, &pos
, &key
, &value
)) {
1179 ks
= PyString_AsString(key
);
1180 for (i
= 0; i
< nkwds
; i
++) {
1181 if (!strcmp(ks
, kwlist
[i
])) {
1188 "%s is an invalid keyword argument for this function",
1190 PyErr_SetString(PyExc_TypeError
, msgbuf
);
1201 skipitem(char **p_format
, va_list *p_va
)
1203 char *format
= *p_format
;
1208 case 'b': /* byte -- very short int */
1209 case 'B': /* byte as bitfield */
1211 (void) va_arg(*p_va
, char *);
1215 case 'h': /* short int */
1217 (void) va_arg(*p_va
, short *);
1221 case 'H': /* short int as bitfield */
1223 (void) va_arg(*p_va
, unsigned short *);
1229 (void) va_arg(*p_va
, int *);
1233 case 'l': /* long int */
1235 (void) va_arg(*p_va
, long *);
1239 #ifdef HAVE_LONG_LONG
1240 case 'L': /* LONG_LONG int */
1242 (void) va_arg(*p_va
, LONG_LONG
*);
1247 case 'f': /* float */
1249 (void) va_arg(*p_va
, float *);
1253 case 'd': /* double */
1255 (void) va_arg(*p_va
, double *);
1259 #ifndef WITHOUT_COMPLEX
1260 case 'D': /* complex double */
1262 (void) va_arg(*p_va
, Py_complex
*);
1265 #endif /* WITHOUT_COMPLEX */
1267 case 'c': /* char */
1269 (void) va_arg(*p_va
, char *);
1273 case 's': /* string */
1275 (void) va_arg(*p_va
, char **);
1276 if (*format
== '#') {
1277 (void) va_arg(*p_va
, int *);
1283 case 'z': /* string */
1285 (void) va_arg(*p_va
, char **);
1286 if (*format
== '#') {
1287 (void) va_arg(*p_va
, int *);
1293 case 'S': /* string object */
1295 (void) va_arg(*p_va
, PyObject
**);
1299 case 'O': /* object */
1301 if (*format
== '!') {
1303 (void) va_arg(*p_va
, PyTypeObject
*);
1304 (void) va_arg(*p_va
, PyObject
**);
1307 /* I don't know what this is for */
1308 else if (*format
== '?') {
1309 inquiry pred
= va_arg(*p_va
, inquiry
);
1312 (void) va_arg(*p_va
, PyObject
**);
1316 else if (*format
== '&') {
1317 typedef int (*converter
)(PyObject
*, void *);
1318 (void) va_arg(*p_va
, converter
);
1319 (void) va_arg(*p_va
, void *);
1323 (void) va_arg(*p_va
, PyObject
**);
1329 return "impossible<bad format char>";