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 char *convertsimple1(PyObject
*, char **, va_list *);
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
)
83 char *formatsave
= format
;
87 assert(compat
|| (args
!= (PyObject
*)NULL
));
91 if (c
== '(' /* ')' */) {
96 else if (/* '(' */ c
== ')') {
98 Py_FatalError(/* '(' */
99 "excess ')' in getargs format");
124 Py_FatalError(/* '(' */ "missing ')' in getargs format");
135 sprintf(msgbuf
, "%s%s takes no arguments",
136 fname
==NULL
? "function" : fname
,
137 fname
==NULL
? "" : "()");
138 PyErr_SetString(PyExc_TypeError
, msgbuf
);
141 else if (min
== 1 && max
== 1) {
144 "%s%s takes at least one argument",
145 fname
==NULL
? "function" : fname
,
146 fname
==NULL
? "" : "()");
147 PyErr_SetString(PyExc_TypeError
, msgbuf
);
150 msg
= convertitem(args
, &format
, p_va
, levels
, msgbuf
);
153 seterror(levels
[0], msg
, levels
+1, fname
, message
);
157 PyErr_SetString(PyExc_SystemError
,
158 "old style getargs format uses new features");
163 if (!PyTuple_Check(args
)) {
164 PyErr_SetString(PyExc_SystemError
,
165 "new style getargs format but argument is not a tuple");
169 len
= PyTuple_Size(args
);
171 if (len
< min
|| max
< len
) {
172 if (message
== NULL
) {
174 "%s%s takes %s %d argument%s (%d given)",
175 fname
==NULL
? "function" : fname
,
176 fname
==NULL
? "" : "()",
178 : len
< min
? "at least" : "at most",
179 len
< min
? min
: max
,
180 (len
< min
? min
: max
) == 1 ? "" : "s",
184 PyErr_SetString(PyExc_TypeError
, message
);
188 for (i
= 0; i
< len
; i
++) {
191 msg
= convertitem(PyTuple_GetItem(args
, i
), &format
, p_va
,
194 seterror(i
+1, msg
, levels
, fname
, message
);
199 if (*format
!= '\0' && !isalpha((int)(*format
)) &&
201 *format
!= '|' && *format
!= ':' && *format
!= ';') {
202 PyErr_Format(PyExc_SystemError
,
203 "bad format string: %.200s", formatsave
);
213 seterror(int iarg
, char *msg
, int *levels
, char *fname
, char *message
)
219 if (PyErr_Occurred())
221 else if (message
== NULL
) {
223 sprintf(p
, "%s() ", fname
);
227 sprintf(p
, "argument %d", iarg
);
230 while (levels
[i
] > 0) {
231 sprintf(p
, ", item %d", levels
[i
]-1);
237 sprintf(p
, "argument");
240 sprintf(p
, " %s", msg
);
243 PyErr_SetString(PyExc_TypeError
, message
);
247 /* Convert a tuple argument.
248 On entry, *p_format points to the character _after_ the opening '('.
249 On successful exit, *p_format points to the closing ')'.
251 *p_format and *p_va are updated,
252 *levels and *msgbuf are untouched,
253 and NULL is returned.
254 If the argument is invalid:
255 *p_format is unchanged,
257 *levels is a 0-terminated list of item numbers,
258 *msgbuf contains an error message, whose format is:
259 "must be <typename1>, not <typename2>", where:
260 <typename1> is the name of the expected type, and
261 <typename2> is the name of the actual type,
262 and msgbuf is returned.
266 converttuple(PyObject
*arg
, char **p_format
, va_list *p_va
, int *levels
,
267 char *msgbuf
, int toplevel
)
271 char *format
= *p_format
;
286 else if (c
== ':' || c
== ';' || c
== '\0')
288 else if (level
== 0 && isalpha(c
))
292 if (!PySequence_Check(arg
) || PyString_Check(arg
)) {
295 toplevel
? "expected %d arguments, not %s" :
296 "must be %d-item sequence, not %s",
297 n
, arg
== Py_None
? "None" : arg
->ob_type
->tp_name
);
301 if ((i
= PySequence_Size(arg
)) != n
) {
304 toplevel
? "expected %d arguments, not %d" :
305 "must be sequence of length %d, not %d",
311 for (i
= 0; i
< n
; i
++) {
314 item
= PySequence_GetItem(arg
, i
);
315 msg
= convertitem(item
, &format
, p_va
, levels
+1, msgbuf
);
316 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
329 /* Convert a single item. */
332 convertitem(PyObject
*arg
, char **p_format
, va_list *p_va
, int *levels
,
336 char *format
= *p_format
;
338 if (*format
== '(' /* ')' */) {
340 msg
= converttuple(arg
, &format
, p_va
, levels
, msgbuf
, 0);
345 msg
= convertsimple(arg
, &format
, p_va
, msgbuf
);
355 /* Convert a non-tuple argument. Adds to convertsimple1 functionality
356 by formatting messages as "must be <desired type>, not <actual type>". */
359 convertsimple(PyObject
*arg
, char **p_format
, va_list *p_va
, char *msgbuf
)
361 char *msg
= convertsimple1(arg
, p_format
, p_va
);
363 sprintf(msgbuf
, "must be %.50s, not %.50s", msg
,
364 arg
== Py_None
? "None" : arg
->ob_type
->tp_name
);
371 /* Internal API needed by convertsimple1(): */
373 PyObject
*_PyUnicode_AsDefaultEncodedString(PyObject
*unicode
,
376 /* Convert a non-tuple argument. Return NULL if conversion went OK,
377 or a string representing the expected type if the conversion failed.
378 When failing, an exception may or may not have been raised.
379 Don't call if a tuple is expected. */
382 convertsimple1(PyObject
*arg
, char **p_format
, va_list *p_va
)
384 char *format
= *p_format
;
389 case 'b': /* unsigned byte -- very short int */
391 char *p
= va_arg(*p_va
, char *);
392 long ival
= PyInt_AsLong(arg
);
393 if (ival
== -1 && PyErr_Occurred())
396 PyErr_SetString(PyExc_OverflowError
,
397 "unsigned byte integer is less than minimum");
400 else if (ival
> UCHAR_MAX
) {
401 PyErr_SetString(PyExc_OverflowError
,
402 "unsigned byte integer is greater than maximum");
406 *p
= (unsigned char) ival
;
410 case 'B': /* byte sized bitfield - both signed and unsigned values allowed */
412 char *p
= va_arg(*p_va
, char *);
413 long ival
= PyInt_AsLong(arg
);
414 if (ival
== -1 && PyErr_Occurred())
416 else if (ival
< SCHAR_MIN
) {
417 PyErr_SetString(PyExc_OverflowError
,
418 "byte-sized integer bitfield is less than minimum");
421 else if (ival
> (int)UCHAR_MAX
) {
422 PyErr_SetString(PyExc_OverflowError
,
423 "byte-sized integer bitfield is greater than maximum");
427 *p
= (unsigned char) ival
;
431 case 'h': /* signed short int */
433 short *p
= va_arg(*p_va
, short *);
434 long ival
= PyInt_AsLong(arg
);
435 if (ival
== -1 && PyErr_Occurred())
437 else if (ival
< SHRT_MIN
) {
438 PyErr_SetString(PyExc_OverflowError
,
439 "signed short integer is less than minimum");
442 else if (ival
> SHRT_MAX
) {
443 PyErr_SetString(PyExc_OverflowError
,
444 "signed short integer is greater than maximum");
452 case 'H': /* short int sized bitfield, both signed and unsigned allowed */
454 unsigned short *p
= va_arg(*p_va
, unsigned short *);
455 long ival
= PyInt_AsLong(arg
);
456 if (ival
== -1 && PyErr_Occurred())
458 else if (ival
< SHRT_MIN
) {
459 PyErr_SetString(PyExc_OverflowError
,
460 "short integer bitfield is less than minimum");
463 else if (ival
> USHRT_MAX
) {
464 PyErr_SetString(PyExc_OverflowError
,
465 "short integer bitfield is greater than maximum");
469 *p
= (unsigned short) ival
;
473 case 'i': /* signed int */
475 int *p
= va_arg(*p_va
, int *);
476 long ival
= PyInt_AsLong(arg
);
477 if (ival
== -1 && PyErr_Occurred())
479 else if (ival
> INT_MAX
) {
480 PyErr_SetString(PyExc_OverflowError
,
481 "signed integer is greater than maximum");
484 else if (ival
< INT_MIN
) {
485 PyErr_SetString(PyExc_OverflowError
,
486 "signed integer is less than minimum");
493 case 'l': /* long int */
495 long *p
= va_arg(*p_va
, long *);
496 long ival
= PyInt_AsLong(arg
);
497 if (ival
== -1 && PyErr_Occurred())
504 #ifdef HAVE_LONG_LONG
505 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() ) {
518 case 'f': /* float */
520 float *p
= va_arg(*p_va
, float *);
521 double dval
= PyFloat_AsDouble(arg
);
522 if (PyErr_Occurred())
529 case 'd': /* double */
531 double *p
= va_arg(*p_va
, double *);
532 double dval
= PyFloat_AsDouble(arg
);
533 if (PyErr_Occurred())
540 #ifndef WITHOUT_COMPLEX
541 case 'D': /* complex double */
543 Py_complex
*p
= va_arg(*p_va
, Py_complex
*);
545 cval
= PyComplex_AsCComplex(arg
);
546 if (PyErr_Occurred())
552 #endif /* WITHOUT_COMPLEX */
556 char *p
= va_arg(*p_va
, char *);
557 if (PyString_Check(arg
) && PyString_Size(arg
) == 1)
558 *p
= PyString_AsString(arg
)[0];
564 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
= _PyUnicode_AsDefaultEncodedString(
578 return "(unicode conversion error)";
579 *p
= PyString_AS_STRING(arg
);
580 *q
= PyString_GET_SIZE(arg
);
582 else { /* any buffer-like object */
583 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
586 pb
->bf_getreadbuffer
== NULL
||
587 pb
->bf_getsegcount
== NULL
)
588 return "string or read-only buffer";
589 if ( (*pb
->bf_getsegcount
)(arg
, NULL
) != 1 )
590 return "string or single-segment read-only buffer";
592 (*pb
->bf_getreadbuffer
)(arg
, 0, p
)) < 0 )
593 return "(unspecified)";
598 char **p
= va_arg(*p_va
, char **);
600 if (PyString_Check(arg
))
601 *p
= PyString_AS_STRING(arg
);
602 else if (PyUnicode_Check(arg
)) {
603 arg
= _PyUnicode_AsDefaultEncodedString(
606 return "(unicode conversion error)";
607 *p
= PyString_AS_STRING(arg
);
611 if ((int)strlen(*p
) != PyString_Size(arg
))
612 return "string without null bytes";
617 case 'z': /* string, may be NULL (None) */
619 if (*format
== '#') { /* any buffer-like object */
620 void **p
= (void **)va_arg(*p_va
, char **);
621 int *q
= va_arg(*p_va
, int *);
623 if (arg
== Py_None
) {
627 else if (PyString_Check(arg
)) {
628 *p
= PyString_AS_STRING(arg
);
629 *q
= PyString_GET_SIZE(arg
);
631 else if (PyUnicode_Check(arg
)) {
632 arg
= _PyUnicode_AsDefaultEncodedString(
635 return "(unicode conversion error)";
636 *p
= PyString_AS_STRING(arg
);
637 *q
= PyString_GET_SIZE(arg
);
639 else { /* any buffer-like object */
640 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
643 pb
->bf_getreadbuffer
== NULL
||
644 pb
->bf_getsegcount
== NULL
)
645 return "string or read-only buffer";
646 if ( (*pb
->bf_getsegcount
)(arg
, NULL
) != 1 )
647 return "string or single-segment read-only buffer";
649 (*pb
->bf_getreadbuffer
)(arg
, 0, p
)) < 0 )
650 return "(unspecified)";
655 char **p
= va_arg(*p_va
, char **);
659 else if (PyString_Check(arg
))
660 *p
= PyString_AsString(arg
);
661 else if (PyUnicode_Check(arg
)) {
662 arg
= _PyUnicode_AsDefaultEncodedString(
665 return "(unicode conversion error)";
666 *p
= PyString_AS_STRING(arg
);
669 return "string or None";
670 if (*format
== '#') {
671 int *q
= va_arg(*p_va
, int *);
675 *q
= PyString_Size(arg
);
678 else if (*p
!= NULL
&&
679 (int)strlen(*p
) != PyString_Size(arg
))
680 return "string without null bytes or None";
685 case 'e': /* encoded string */
688 const char *encoding
;
692 /* Get 'e' parameter: the encoding name */
693 encoding
= (const char *)va_arg(*p_va
, const char *);
694 if (encoding
== NULL
)
695 encoding
= PyUnicode_GetDefaultEncoding();
697 /* Get 's' parameter: the output buffer to use */
699 return "(unknown parser marker combination)";
700 buffer
= (char **)va_arg(*p_va
, char **);
703 return "(buffer is NULL)";
705 /* Convert object to Unicode */
706 u
= PyUnicode_FromObject(arg
);
708 return "string or unicode or text buffer";
710 /* Encode object; use default error handling */
711 s
= PyUnicode_AsEncodedString(u
,
716 return "(encoding failed)";
717 if (!PyString_Check(s
)) {
719 return "(encoder failed to return a string)";
721 size
= PyString_GET_SIZE(s
);
723 /* Write output; output is guaranteed to be
725 if (*format
== '#') {
726 /* Using buffer length parameter '#':
728 - if *buffer is NULL, a new buffer
729 of the needed size is allocated and
730 the data copied into it; *buffer is
731 updated to point to the new buffer;
732 the caller is responsible for
733 PyMem_Free()ing it after usage
735 - if *buffer is not NULL, the data
736 is copied to *buffer; *buffer_len
737 has to be set to the size of the
738 buffer on input; buffer overflow is
739 signalled with an error; buffer has
740 to provide enough room for the
741 encoded string plus the trailing
744 - in both cases, *buffer_len is
745 updated to the size of the buffer
746 /excluding/ the trailing 0-byte
749 int *buffer_len
= va_arg(*p_va
, int *);
752 if (buffer_len
== NULL
)
753 return "(buffer_len is NULL)";
754 if (*buffer
== NULL
) {
755 *buffer
= PyMem_NEW(char, size
+ 1);
756 if (*buffer
== NULL
) {
758 return "(memory error)";
761 if (size
+ 1 > *buffer_len
) {
763 return "(buffer overflow)";
767 PyString_AS_STRING(s
),
771 /* Using a 0-terminated buffer:
773 - the encoded string has to be
774 0-terminated for this variant to
775 work; if it is not, an error raised
777 - a new buffer of the needed size
778 is allocated and the data copied
779 into it; *buffer is updated to
780 point to the new buffer; the caller
781 is responsible for PyMem_Free()ing it
785 if ((int)strlen(PyString_AS_STRING(s
)) != size
)
786 return "(encoded string without NULL bytes)";
787 *buffer
= PyMem_NEW(char, size
+ 1);
788 if (*buffer
== NULL
) {
790 return "(memory error)";
793 PyString_AS_STRING(s
),
800 case 'u': /* raw unicode buffer (Py_UNICODE *) */
802 if (*format
== '#') { /* any buffer-like object */
803 void **p
= (void **)va_arg(*p_va
, char **);
804 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
805 int *q
= va_arg(*p_va
, int *);
809 pb
->bf_getreadbuffer
== NULL
||
810 pb
->bf_getsegcount
== NULL
)
811 return "unicode or read-only buffer";
812 if ( (*pb
->bf_getsegcount
)(arg
, NULL
) != 1 )
813 return "unicode or single-segment read-only buffer";
815 (*pb
->bf_getreadbuffer
)(arg
, 0, p
)) < 0 )
816 return "(unspecified)";
817 /* buffer interface returns bytes, we want
818 length in characters */
819 *q
= count
/(sizeof(Py_UNICODE
));
822 Py_UNICODE
**p
= va_arg(*p_va
, Py_UNICODE
**);
824 if (PyUnicode_Check(arg
))
825 *p
= PyUnicode_AS_UNICODE(arg
);
832 case 'S': /* string object */
834 PyObject
**p
= va_arg(*p_va
, PyObject
**);
835 if (PyString_Check(arg
))
842 case 'U': /* Unicode object */
844 PyObject
**p
= va_arg(*p_va
, PyObject
**);
845 if (PyUnicode_Check(arg
))
852 case 'O': /* object */
856 if (*format
== '!') {
857 type
= va_arg(*p_va
, PyTypeObject
*);
858 p
= va_arg(*p_va
, PyObject
**);
860 if (arg
->ob_type
== type
)
863 return type
->tp_name
;
866 else if (*format
== '?') {
867 inquiry pred
= va_arg(*p_va
, inquiry
);
868 p
= va_arg(*p_va
, PyObject
**);
873 return "(unspecified)";
876 else if (*format
== '&') {
877 typedef int (*converter
)(PyObject
*, void *);
878 converter convert
= va_arg(*p_va
, converter
);
879 void *addr
= va_arg(*p_va
, void *);
881 if (! (*convert
)(arg
, addr
))
882 return "(unspecified)";
885 p
= va_arg(*p_va
, PyObject
**);
892 case 'w': /* memory buffer, read-write access */
894 void **p
= va_arg(*p_va
, void **);
895 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
898 if ( pb
== NULL
|| pb
->bf_getwritebuffer
== NULL
||
899 pb
->bf_getsegcount
== NULL
)
900 return "read-write buffer";
901 if ( (*pb
->bf_getsegcount
)(arg
, NULL
) != 1 )
902 return "single-segment read-write buffer";
903 if ( (count
= pb
->bf_getwritebuffer(arg
, 0, p
)) < 0 )
904 return "(unspecified)";
905 if (*format
== '#') {
906 int *q
= va_arg(*p_va
, int *);
914 case 't': /* 8-bit character buffer, read-only access */
916 const char **p
= va_arg(*p_va
, const char **);
917 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
920 if ( *format
++ != '#' )
921 return "invalid use of 't' format character";
922 if ( !PyType_HasFeature(
924 Py_TPFLAGS_HAVE_GETCHARBUFFER
) ||
926 pb
->bf_getcharbuffer
== NULL
||
927 pb
->bf_getsegcount
== NULL
)
928 return "string or read-only character buffer";
929 if ( (*pb
->bf_getsegcount
)(arg
, NULL
) != 1 )
930 return "string or single-segment read-only buffer";
931 if ( (count
= pb
->bf_getcharbuffer(arg
, 0, p
)) < 0 )
932 return "(unspecified)";
934 *va_arg(*p_va
, int *) = count
;
941 return "impossible<bad format char>";
950 /* Support for keyword arguments donated by
951 Geoff Philbrick <philbric@delphi.hks.com> */
953 int PyArg_ParseTupleAndKeywords(PyObject
*args
,
961 va_start(va
, kwlist
);
962 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &va
);
969 vgetargskeywords(PyObject
*args
, PyObject
*keywords
, char *format
,
970 char **kwlist
, va_list *p_va
)
975 char *message
= NULL
;
978 char *formatsave
= format
;
979 int i
, len
, tplen
, kwlen
;
981 int nkwds
, pos
, match
, converted
;
982 PyObject
*key
, *value
;
984 /* nested tuples cannot be parsed when using keyword arguments */
989 PyErr_SetString(PyExc_SystemError
,
990 "tuple found in format when using keyword arguments");
1005 else if (isalpha(c
))
1014 format
= formatsave
;
1016 if (!PyTuple_Check(args
)) {
1017 PyErr_SetString(PyExc_SystemError
,
1018 "new style getargs format but argument is not a tuple");
1022 tplen
= PyTuple_Size(args
);
1024 /* do a cursory check of the keywords just to see how many we got */
1027 if (!PyDict_Check(keywords
)) {
1028 if (keywords
== NULL
)
1029 PyErr_SetString(PyExc_SystemError
,
1030 "NULL received when keyword dictionary expected");
1032 PyErr_Format(PyExc_SystemError
,
1033 "%s received when keyword dictionary expected",
1034 keywords
->ob_type
->tp_name
);
1037 kwlen
= PyDict_Size(keywords
);
1043 /* make sure there are no duplicate values for an argument;
1044 its not clear when to use the term "keyword argument vs.
1045 keyword parameter in messages */
1048 for (i
= 0; i
< tplen
; i
++) {
1049 if (PyMapping_HasKeyString(keywords
, kwlist
[i
])) {
1051 "keyword parameter %s redefined",
1053 PyErr_SetString(PyExc_TypeError
, msgbuf
);
1058 PyErr_Clear(); /* I'm not which Py functions set the error string */
1060 /* required arguments missing from args can be supplied by keyword
1064 if (keywords
&& tplen
< min
) {
1065 for (i
= tplen
; i
< min
; i
++) {
1066 if (PyMapping_HasKeyString(keywords
, kwlist
[i
])) {
1073 /* make sure we got an acceptable number of arguments; the message
1074 is a little confusing with keywords since keyword arguments
1075 which are supplied, but don't match the required arguments
1076 are not included in the "%d given" part of the message */
1078 if (len
< min
|| max
< len
) {
1079 if (message
== NULL
) {
1081 "%s%s takes %s %d argument%s (%d given)",
1082 fname
==NULL
? "function" : fname
,
1083 fname
==NULL
? "" : "()",
1084 min
==max
? "exactly"
1085 : len
< min
? "at least" : "at most",
1086 len
< min
? min
: max
,
1087 (len
< min
? min
: max
) == 1 ? "" : "s",
1091 PyErr_SetString(PyExc_TypeError
, message
);
1095 for (i
= 0; i
< tplen
; i
++) {
1098 msg
= convertitem(PyTuple_GetItem(args
, i
), &format
, p_va
,
1101 seterror(i
+1, msg
, levels
, fname
, message
);
1106 /* handle no keyword parameters in call */
1108 if (!keywords
) return 1;
1110 /* make sure the number of keywords in the keyword list matches the
1111 number of items in the format string */
1121 PyErr_SetString(PyExc_SystemError
,
1122 "number of items in format string and keyword list do not match");
1126 /* convert the keyword arguments; this uses the format
1127 string where it was left after processing args */
1130 for (i
= tplen
; i
< nkwds
; i
++) {
1134 item
= PyMapping_GetItemString(keywords
, kwlist
[i
]);
1136 msg
= convertitem(item
, &format
, p_va
, levels
, msgbuf
);
1138 seterror(i
+1, msg
, levels
, fname
, message
);
1146 msg
= skipitem(&format
, p_va
);
1148 seterror(i
+1, msg
, levels
, fname
, message
);
1154 /* make sure there are no extraneous keyword arguments */
1157 if (converted
< kwlen
) {
1158 while (PyDict_Next(keywords
, &pos
, &key
, &value
)) {
1160 ks
= PyString_AsString(key
);
1161 for (i
= 0; i
< nkwds
; i
++) {
1162 if (!strcmp(ks
, kwlist
[i
])) {
1169 "%s is an invalid keyword argument for this function",
1171 PyErr_SetString(PyExc_TypeError
, msgbuf
);
1182 skipitem(char **p_format
, va_list *p_va
)
1184 char *format
= *p_format
;
1189 case 'b': /* byte -- very short int */
1190 case 'B': /* byte as bitfield */
1192 (void) va_arg(*p_va
, char *);
1196 case 'h': /* short int */
1198 (void) va_arg(*p_va
, short *);
1202 case 'H': /* short int as bitfield */
1204 (void) va_arg(*p_va
, unsigned short *);
1210 (void) va_arg(*p_va
, int *);
1214 case 'l': /* long int */
1216 (void) va_arg(*p_va
, long *);
1220 #ifdef HAVE_LONG_LONG
1221 case 'L': /* LONG_LONG int */
1223 (void) va_arg(*p_va
, LONG_LONG
*);
1228 case 'f': /* float */
1230 (void) va_arg(*p_va
, float *);
1234 case 'd': /* double */
1236 (void) va_arg(*p_va
, double *);
1240 #ifndef WITHOUT_COMPLEX
1241 case 'D': /* complex double */
1243 (void) va_arg(*p_va
, Py_complex
*);
1246 #endif /* WITHOUT_COMPLEX */
1248 case 'c': /* char */
1250 (void) va_arg(*p_va
, char *);
1254 case 's': /* string */
1256 (void) va_arg(*p_va
, char **);
1257 if (*format
== '#') {
1258 (void) va_arg(*p_va
, int *);
1264 case 'z': /* string */
1266 (void) va_arg(*p_va
, char **);
1267 if (*format
== '#') {
1268 (void) va_arg(*p_va
, int *);
1274 case 'S': /* string object */
1276 (void) va_arg(*p_va
, PyObject
**);
1280 case 'O': /* object */
1282 if (*format
== '!') {
1284 (void) va_arg(*p_va
, PyTypeObject
*);
1285 (void) va_arg(*p_va
, PyObject
**);
1288 /* I don't know what this is for */
1289 else if (*format
== '?') {
1290 inquiry pred
= va_arg(*p_va
, inquiry
);
1293 (void) va_arg(*p_va
, PyObject
**);
1297 else if (*format
== '&') {
1298 typedef int (*converter
)(PyObject
*, void *);
1299 (void) va_arg(*p_va
, converter
);
1300 (void) va_arg(*p_va
, void *);
1304 (void) va_arg(*p_va
, PyObject
**);
1310 return "impossible<bad format char>";