2 /* New getargs implementation */
12 int PyArg_Parse(PyObject
*, const char *, ...);
13 int PyArg_ParseTuple(PyObject
*, const char *, ...);
14 int PyArg_VaParse(PyObject
*, const char *, va_list);
16 int PyArg_ParseTupleAndKeywords(PyObject
*, PyObject
*,
17 const char *, char **, ...);
18 int PyArg_VaParseTupleAndKeywords(PyObject
*, PyObject
*,
19 const char *, char **, va_list);
21 #ifdef HAVE_DECLSPEC_DLL
22 /* Export functions */
23 PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject
*, char *, ...);
24 PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject
*, char *, ...);
25 PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject
*, PyObject
*,
26 const char *, char **, ...);
27 PyAPI_FUNC(PyObject
*) _Py_BuildValue_SizeT(const char *, ...);
28 PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject
*, char *, va_list);
29 PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject
*, PyObject
*,
30 const char *, char **, va_list);
38 static int vgetargs1(PyObject
*, const char *, va_list *, int);
39 static void seterror(int, const char *, int *, const char *, const char *);
40 static char *convertitem(PyObject
*, const char **, va_list *, int, int *,
41 char *, size_t, PyObject
**);
42 static char *converttuple(PyObject
*, const char **, va_list *, int,
43 int *, char *, size_t, int, PyObject
**);
44 static char *convertsimple(PyObject
*, const char **, va_list *, int, char *,
46 static Py_ssize_t
convertbuffer(PyObject
*, void **p
, char **);
48 static int vgetargskeywords(PyObject
*, PyObject
*,
49 const char *, char **, va_list *, int);
50 static char *skipitem(const char **, va_list *, int);
53 PyArg_Parse(PyObject
*args
, const char *format
, ...)
59 retval
= vgetargs1(args
, format
, &va
, FLAG_COMPAT
);
65 _PyArg_Parse_SizeT(PyObject
*args
, char *format
, ...)
71 retval
= vgetargs1(args
, format
, &va
, FLAG_COMPAT
|FLAG_SIZE_T
);
78 PyArg_ParseTuple(PyObject
*args
, const char *format
, ...)
84 retval
= vgetargs1(args
, format
, &va
, 0);
90 _PyArg_ParseTuple_SizeT(PyObject
*args
, char *format
, ...)
96 retval
= vgetargs1(args
, format
, &va
, FLAG_SIZE_T
);
103 PyArg_VaParse(PyObject
*args
, const char *format
, va_list va
)
107 #ifdef VA_LIST_IS_ARRAY
108 memcpy(lva
, va
, sizeof(va_list));
117 return vgetargs1(args
, format
, &lva
, 0);
121 _PyArg_VaParse_SizeT(PyObject
*args
, char *format
, va_list va
)
125 #ifdef VA_LIST_IS_ARRAY
126 memcpy(lva
, va
, sizeof(va_list));
135 return vgetargs1(args
, format
, &lva
, FLAG_SIZE_T
);
139 /* Handle cleanup of allocated memory in case of exception */
142 addcleanup(void *ptr
, PyObject
**freelist
)
146 *freelist
= PyList_New(0);
152 cobj
= PyCObject_FromVoidPtr(ptr
, NULL
);
157 if(PyList_Append(*freelist
, cobj
)) {
167 cleanreturn(int retval
, PyObject
*freelist
)
171 Py_ssize_t len
= PyList_GET_SIZE(freelist
), i
;
172 for (i
= 0; i
< len
; i
++)
173 PyMem_FREE(PyCObject_AsVoidPtr(
174 PyList_GET_ITEM(freelist
, i
)));
183 vgetargs1(PyObject
*args
, const char *format
, va_list *p_va
, int flags
)
187 const char *fname
= NULL
;
188 const char *message
= NULL
;
193 const char *formatsave
= format
;
196 PyObject
*freelist
= NULL
;
197 int compat
= flags
& FLAG_COMPAT
;
199 assert(compat
|| (args
!= (PyObject
*)NULL
));
200 flags
= flags
& ~FLAG_COMPAT
;
202 while (endfmt
== 0) {
210 Py_FatalError("too many tuple nesting levels "
211 "in argument format string");
215 Py_FatalError("excess ')' in getargs format");
234 else if (isalpha(Py_CHARMASK(c
))) {
235 if (c
!= 'e') /* skip encoded */
245 Py_FatalError(/* '(' */ "missing ')' in getargs format");
256 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
257 "%.200s%s takes no arguments",
258 fname
==NULL
? "function" : fname
,
259 fname
==NULL
? "" : "()");
260 PyErr_SetString(PyExc_TypeError
, msgbuf
);
263 else if (min
== 1 && max
== 1) {
265 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
266 "%.200s%s takes at least one argument",
267 fname
==NULL
? "function" : fname
,
268 fname
==NULL
? "" : "()");
269 PyErr_SetString(PyExc_TypeError
, msgbuf
);
272 msg
= convertitem(args
, &format
, p_va
, flags
, levels
,
273 msgbuf
, sizeof(msgbuf
), &freelist
);
275 return cleanreturn(1, freelist
);
276 seterror(levels
[0], msg
, levels
+1, fname
, message
);
277 return cleanreturn(0, freelist
);
280 PyErr_SetString(PyExc_SystemError
,
281 "old style getargs format uses new features");
286 if (!PyTuple_Check(args
)) {
287 PyErr_SetString(PyExc_SystemError
,
288 "new style getargs format but argument is not a tuple");
292 len
= PyTuple_GET_SIZE(args
);
294 if (len
< min
|| max
< len
) {
295 if (message
== NULL
) {
296 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
297 "%.150s%s takes %s %d argument%s "
299 fname
==NULL
? "function" : fname
,
300 fname
==NULL
? "" : "()",
302 : len
< min
? "at least" : "at most",
303 len
< min
? min
: max
,
304 (len
< min
? min
: max
) == 1 ? "" : "s",
305 Py_SAFE_DOWNCAST(len
, Py_ssize_t
, long));
308 PyErr_SetString(PyExc_TypeError
, message
);
312 for (i
= 0; i
< len
; i
++) {
315 msg
= convertitem(PyTuple_GET_ITEM(args
, i
), &format
, p_va
,
316 flags
, levels
, msgbuf
,
317 sizeof(msgbuf
), &freelist
);
319 seterror(i
+1, msg
, levels
, fname
, message
);
320 return cleanreturn(0, freelist
);
324 if (*format
!= '\0' && !isalpha(Py_CHARMASK(*format
)) &&
326 *format
!= '|' && *format
!= ':' && *format
!= ';') {
327 PyErr_Format(PyExc_SystemError
,
328 "bad format string: %.200s", formatsave
);
329 return cleanreturn(0, freelist
);
332 return cleanreturn(1, freelist
);
338 seterror(int iarg
, const char *msg
, int *levels
, const char *fname
,
345 if (PyErr_Occurred())
347 else if (message
== NULL
) {
349 PyOS_snprintf(p
, sizeof(buf
), "%.200s() ", fname
);
353 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
),
354 "argument %d", iarg
);
357 while (levels
[i
] > 0 && i
< 32 && (int)(p
-buf
) < 220) {
358 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
),
359 ", item %d", levels
[i
]-1);
365 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
), "argument");
368 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
), " %.256s", msg
);
371 PyErr_SetString(PyExc_TypeError
, message
);
375 /* Convert a tuple argument.
376 On entry, *p_format points to the character _after_ the opening '('.
377 On successful exit, *p_format points to the closing ')'.
379 *p_format and *p_va are updated,
380 *levels and *msgbuf are untouched,
381 and NULL is returned.
382 If the argument is invalid:
383 *p_format is unchanged,
385 *levels is a 0-terminated list of item numbers,
386 *msgbuf contains an error message, whose format is:
387 "must be <typename1>, not <typename2>", where:
388 <typename1> is the name of the expected type, and
389 <typename2> is the name of the actual type,
390 and msgbuf is returned.
394 converttuple(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
395 int *levels
, char *msgbuf
, size_t bufsize
, int toplevel
,
400 const char *format
= *p_format
;
415 else if (c
== ':' || c
== ';' || c
== '\0')
417 else if (level
== 0 && isalpha(Py_CHARMASK(c
)))
421 if (!PySequence_Check(arg
) || PyString_Check(arg
)) {
423 PyOS_snprintf(msgbuf
, bufsize
,
424 toplevel
? "expected %d arguments, not %.50s" :
425 "must be %d-item sequence, not %.50s",
427 arg
== Py_None
? "None" : arg
->ob_type
->tp_name
);
431 if ((i
= PySequence_Size(arg
)) != n
) {
433 PyOS_snprintf(msgbuf
, bufsize
,
434 toplevel
? "expected %d arguments, not %d" :
435 "must be sequence of length %d, not %d",
441 for (i
= 0; i
< n
; i
++) {
444 item
= PySequence_GetItem(arg
, i
);
449 strncpy(msgbuf
, "is not retrievable", bufsize
);
452 msg
= convertitem(item
, &format
, p_va
, flags
, levels
+1,
453 msgbuf
, bufsize
, freelist
);
454 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
467 /* Convert a single item. */
470 convertitem(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
471 int *levels
, char *msgbuf
, size_t bufsize
, PyObject
**freelist
)
474 const char *format
= *p_format
;
476 if (*format
== '(' /* ')' */) {
478 msg
= converttuple(arg
, &format
, p_va
, flags
, levels
, msgbuf
,
479 bufsize
, 0, freelist
);
484 msg
= convertsimple(arg
, &format
, p_va
, flags
,
485 msgbuf
, bufsize
, freelist
);
496 #define UNICODE_DEFAULT_ENCODING(arg) \
497 _PyUnicode_AsDefaultEncodedString(arg, NULL)
499 /* Format an error message generated by convertsimple(). */
502 converterr(const char *expected
, PyObject
*arg
, char *msgbuf
, size_t bufsize
)
504 assert(expected
!= NULL
);
506 PyOS_snprintf(msgbuf
, bufsize
,
507 "must be %.50s, not %.50s", expected
,
508 arg
== Py_None
? "None" : arg
->ob_type
->tp_name
);
512 #define CONV_UNICODE "(unicode conversion error)"
514 /* explicitly check for float arguments when integers are expected. For now
515 * signal a warning. Returns true if an exception was raised. */
517 float_argument_error(PyObject
*arg
)
519 if (PyFloat_Check(arg
) &&
520 PyErr_Warn(PyExc_DeprecationWarning
,
521 "integer argument expected, got float" ))
527 /* Convert a non-tuple argument. Return NULL if conversion went OK,
528 or a string with a message describing the failure. The message is
529 formatted as "must be <desired type>, not <actual type>".
530 When failing, an exception may or may not have been raised.
531 Don't call if a tuple is expected.
533 When you add new format codes, please don't forget poor skipitem() below.
537 convertsimple(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
538 char *msgbuf
, size_t bufsize
, PyObject
**freelist
)
541 #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
542 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
543 else q=va_arg(*p_va, int*);
544 #define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
545 #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
547 const char *format
= *p_format
;
549 #ifdef Py_USING_UNICODE
555 case 'b': { /* unsigned byte -- very short int */
556 char *p
= va_arg(*p_va
, char *);
558 if (float_argument_error(arg
))
559 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
560 ival
= PyInt_AsLong(arg
);
561 if (ival
== -1 && PyErr_Occurred())
562 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
564 PyErr_SetString(PyExc_OverflowError
,
565 "unsigned byte integer is less than minimum");
566 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
568 else if (ival
> UCHAR_MAX
) {
569 PyErr_SetString(PyExc_OverflowError
,
570 "unsigned byte integer is greater than maximum");
571 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
574 *p
= (unsigned char) ival
;
578 case 'B': {/* byte sized bitfield - both signed and unsigned
580 char *p
= va_arg(*p_va
, char *);
582 if (float_argument_error(arg
))
583 return converterr("integer<B>", arg
, msgbuf
, bufsize
);
584 ival
= PyInt_AsUnsignedLongMask(arg
);
585 if (ival
== -1 && PyErr_Occurred())
586 return converterr("integer<B>", arg
, msgbuf
, bufsize
);
588 *p
= (unsigned char) ival
;
592 case 'h': {/* signed short int */
593 short *p
= va_arg(*p_va
, short *);
595 if (float_argument_error(arg
))
596 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
597 ival
= PyInt_AsLong(arg
);
598 if (ival
== -1 && PyErr_Occurred())
599 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
600 else if (ival
< SHRT_MIN
) {
601 PyErr_SetString(PyExc_OverflowError
,
602 "signed short integer is less than minimum");
603 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
605 else if (ival
> SHRT_MAX
) {
606 PyErr_SetString(PyExc_OverflowError
,
607 "signed short integer is greater than maximum");
608 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
615 case 'H': { /* short int sized bitfield, both signed and
617 unsigned short *p
= va_arg(*p_va
, unsigned short *);
619 if (float_argument_error(arg
))
620 return converterr("integer<H>", arg
, msgbuf
, bufsize
);
621 ival
= PyInt_AsUnsignedLongMask(arg
);
622 if (ival
== -1 && PyErr_Occurred())
623 return converterr("integer<H>", arg
, msgbuf
, bufsize
);
625 *p
= (unsigned short) ival
;
629 case 'i': {/* signed int */
630 int *p
= va_arg(*p_va
, int *);
632 if (float_argument_error(arg
))
633 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
634 ival
= PyInt_AsLong(arg
);
635 if (ival
== -1 && PyErr_Occurred())
636 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
637 else if (ival
> INT_MAX
) {
638 PyErr_SetString(PyExc_OverflowError
,
639 "signed integer is greater than maximum");
640 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
642 else if (ival
< INT_MIN
) {
643 PyErr_SetString(PyExc_OverflowError
,
644 "signed integer is less than minimum");
645 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
652 case 'I': { /* int sized bitfield, both signed and
654 unsigned int *p
= va_arg(*p_va
, unsigned int *);
656 if (float_argument_error(arg
))
657 return converterr("integer<I>", arg
, msgbuf
, bufsize
);
658 ival
= (unsigned int)PyInt_AsUnsignedLongMask(arg
);
659 if (ival
== (unsigned int)-1 && PyErr_Occurred())
660 return converterr("integer<I>", arg
, msgbuf
, bufsize
);
666 case 'n': /* Py_ssize_t */
667 #if SIZEOF_SIZE_T != SIZEOF_LONG
669 Py_ssize_t
*p
= va_arg(*p_va
, Py_ssize_t
*);
671 if (float_argument_error(arg
))
672 return converterr("integer<n>", arg
, msgbuf
, bufsize
);
673 ival
= PyInt_AsSsize_t(arg
);
674 if (ival
== -1 && PyErr_Occurred())
675 return converterr("integer<n>", arg
, msgbuf
, bufsize
);
680 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
681 case 'l': {/* long int */
682 long *p
= va_arg(*p_va
, long *);
684 if (float_argument_error(arg
))
685 return converterr("integer<l>", arg
, msgbuf
, bufsize
);
686 ival
= PyInt_AsLong(arg
);
687 if (ival
== -1 && PyErr_Occurred())
688 return converterr("integer<l>", arg
, msgbuf
, bufsize
);
694 case 'k': { /* long sized bitfield */
695 unsigned long *p
= va_arg(*p_va
, unsigned long *);
697 if (PyInt_Check(arg
))
698 ival
= PyInt_AsUnsignedLongMask(arg
);
699 else if (PyLong_Check(arg
))
700 ival
= PyLong_AsUnsignedLongMask(arg
);
702 return converterr("integer<k>", arg
, msgbuf
, bufsize
);
707 #ifdef HAVE_LONG_LONG
708 case 'L': {/* PY_LONG_LONG */
709 PY_LONG_LONG
*p
= va_arg( *p_va
, PY_LONG_LONG
* );
710 PY_LONG_LONG ival
= PyLong_AsLongLong( arg
);
711 if( ival
== (PY_LONG_LONG
)-1 && PyErr_Occurred() ) {
712 return converterr("long<L>", arg
, msgbuf
, bufsize
);
719 case 'K': { /* long long sized bitfield */
720 unsigned PY_LONG_LONG
*p
= va_arg(*p_va
, unsigned PY_LONG_LONG
*);
721 unsigned PY_LONG_LONG ival
;
722 if (PyInt_Check(arg
))
723 ival
= PyInt_AsUnsignedLongMask(arg
);
724 else if (PyLong_Check(arg
))
725 ival
= PyLong_AsUnsignedLongLongMask(arg
);
727 return converterr("integer<K>", arg
, msgbuf
, bufsize
);
733 case 'f': {/* float */
734 float *p
= va_arg(*p_va
, float *);
735 double dval
= PyFloat_AsDouble(arg
);
736 if (PyErr_Occurred())
737 return converterr("float<f>", arg
, msgbuf
, bufsize
);
743 case 'd': {/* double */
744 double *p
= va_arg(*p_va
, double *);
745 double dval
= PyFloat_AsDouble(arg
);
746 if (PyErr_Occurred())
747 return converterr("float<d>", arg
, msgbuf
, bufsize
);
753 #ifndef WITHOUT_COMPLEX
754 case 'D': {/* complex double */
755 Py_complex
*p
= va_arg(*p_va
, Py_complex
*);
757 cval
= PyComplex_AsCComplex(arg
);
758 if (PyErr_Occurred())
759 return converterr("complex<D>", arg
, msgbuf
, bufsize
);
764 #endif /* WITHOUT_COMPLEX */
766 case 'c': {/* char */
767 char *p
= va_arg(*p_va
, char *);
768 if (PyString_Check(arg
) && PyString_Size(arg
) == 1)
769 *p
= PyString_AS_STRING(arg
)[0];
771 return converterr("char", arg
, msgbuf
, bufsize
);
775 case 's': {/* string */
776 if (*format
== '#') {
777 void **p
= (void **)va_arg(*p_va
, char **);
780 if (PyString_Check(arg
)) {
781 *p
= PyString_AS_STRING(arg
);
782 STORE_SIZE(PyString_GET_SIZE(arg
));
784 #ifdef Py_USING_UNICODE
785 else if (PyUnicode_Check(arg
)) {
786 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
788 return converterr(CONV_UNICODE
,
789 arg
, msgbuf
, bufsize
);
790 *p
= PyString_AS_STRING(uarg
);
791 STORE_SIZE(PyString_GET_SIZE(uarg
));
794 else { /* any buffer-like object */
796 Py_ssize_t count
= convertbuffer(arg
, p
, &buf
);
798 return converterr(buf
, arg
, msgbuf
, bufsize
);
803 char **p
= va_arg(*p_va
, char **);
805 if (PyString_Check(arg
))
806 *p
= PyString_AS_STRING(arg
);
807 #ifdef Py_USING_UNICODE
808 else if (PyUnicode_Check(arg
)) {
809 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
811 return converterr(CONV_UNICODE
,
812 arg
, msgbuf
, bufsize
);
813 *p
= PyString_AS_STRING(uarg
);
817 return converterr("string", arg
, msgbuf
, bufsize
);
818 if ((Py_ssize_t
)strlen(*p
) != PyString_Size(arg
))
819 return converterr("string without null bytes",
820 arg
, msgbuf
, bufsize
);
825 case 'z': {/* string, may be NULL (None) */
826 if (*format
== '#') { /* any buffer-like object */
827 void **p
= (void **)va_arg(*p_va
, char **);
830 if (arg
== Py_None
) {
834 else if (PyString_Check(arg
)) {
835 *p
= PyString_AS_STRING(arg
);
836 STORE_SIZE(PyString_GET_SIZE(arg
));
838 #ifdef Py_USING_UNICODE
839 else if (PyUnicode_Check(arg
)) {
840 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
842 return converterr(CONV_UNICODE
,
843 arg
, msgbuf
, bufsize
);
844 *p
= PyString_AS_STRING(uarg
);
845 STORE_SIZE(PyString_GET_SIZE(uarg
));
848 else { /* any buffer-like object */
850 Py_ssize_t count
= convertbuffer(arg
, p
, &buf
);
852 return converterr(buf
, arg
, msgbuf
, bufsize
);
857 char **p
= va_arg(*p_va
, char **);
861 else if (PyString_Check(arg
))
862 *p
= PyString_AS_STRING(arg
);
863 #ifdef Py_USING_UNICODE
864 else if (PyUnicode_Check(arg
)) {
865 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
867 return converterr(CONV_UNICODE
,
868 arg
, msgbuf
, bufsize
);
869 *p
= PyString_AS_STRING(uarg
);
873 return converterr("string or None",
874 arg
, msgbuf
, bufsize
);
875 if (*format
== '#') {
877 assert(0); /* XXX redundant with if-case */
881 *q
= PyString_Size(arg
);
884 else if (*p
!= NULL
&&
885 (Py_ssize_t
)strlen(*p
) != PyString_Size(arg
))
887 "string without null bytes or None",
888 arg
, msgbuf
, bufsize
);
893 case 'e': {/* encoded string */
895 const char *encoding
;
897 int size
, recode_strings
;
899 /* Get 'e' parameter: the encoding name */
900 encoding
= (const char *)va_arg(*p_va
, const char *);
901 #ifdef Py_USING_UNICODE
902 if (encoding
== NULL
)
903 encoding
= PyUnicode_GetDefaultEncoding();
906 /* Get output buffer parameter:
907 's' (recode all objects via Unicode) or
908 't' (only recode non-string objects)
912 else if (*format
== 't')
916 "(unknown parser marker combination)",
917 arg
, msgbuf
, bufsize
);
918 buffer
= (char **)va_arg(*p_va
, char **);
921 return converterr("(buffer is NULL)",
922 arg
, msgbuf
, bufsize
);
925 if (!recode_strings
&& PyString_Check(arg
)) {
930 #ifdef Py_USING_UNICODE
933 /* Convert object to Unicode */
934 u
= PyUnicode_FromObject(arg
);
937 "string or unicode or text buffer",
938 arg
, msgbuf
, bufsize
);
940 /* Encode object; use default error handling */
941 s
= PyUnicode_AsEncodedString(u
,
946 return converterr("(encoding failed)",
947 arg
, msgbuf
, bufsize
);
948 if (!PyString_Check(s
)) {
951 "(encoder failed to return a string)",
952 arg
, msgbuf
, bufsize
);
955 return converterr("string<e>", arg
, msgbuf
, bufsize
);
958 size
= PyString_GET_SIZE(s
);
960 /* Write output; output is guaranteed to be 0-terminated */
961 if (*format
== '#') {
962 /* Using buffer length parameter '#':
964 - if *buffer is NULL, a new buffer of the
965 needed size is allocated and the data
966 copied into it; *buffer is updated to point
967 to the new buffer; the caller is
968 responsible for PyMem_Free()ing it after
971 - if *buffer is not NULL, the data is
972 copied to *buffer; *buffer_len has to be
973 set to the size of the buffer on input;
974 buffer overflow is signalled with an error;
975 buffer has to provide enough room for the
976 encoded string plus the trailing 0-byte
978 - in both cases, *buffer_len is updated to
979 the size of the buffer /excluding/ the
986 if (q
== NULL
&& q2
== NULL
) {
989 "(buffer_len is NULL)",
990 arg
, msgbuf
, bufsize
);
992 if (*buffer
== NULL
) {
993 *buffer
= PyMem_NEW(char, size
+ 1);
994 if (*buffer
== NULL
) {
998 arg
, msgbuf
, bufsize
);
1000 if(addcleanup(*buffer
, freelist
)) {
1003 "(cleanup problem)",
1004 arg
, msgbuf
, bufsize
);
1007 if (size
+ 1 > BUFFER_LEN
) {
1010 "(buffer overflow)",
1011 arg
, msgbuf
, bufsize
);
1015 PyString_AS_STRING(s
),
1019 /* Using a 0-terminated buffer:
1021 - the encoded string has to be 0-terminated
1022 for this variant to work; if it is not, an
1025 - a new buffer of the needed size is
1026 allocated and the data copied into it;
1027 *buffer is updated to point to the new
1028 buffer; the caller is responsible for
1029 PyMem_Free()ing it after usage
1032 if ((Py_ssize_t
)strlen(PyString_AS_STRING(s
))
1036 "(encoded string without NULL bytes)",
1037 arg
, msgbuf
, bufsize
);
1039 *buffer
= PyMem_NEW(char, size
+ 1);
1040 if (*buffer
== NULL
) {
1042 return converterr("(memory error)",
1043 arg
, msgbuf
, bufsize
);
1045 if(addcleanup(*buffer
, freelist
)) {
1047 return converterr("(cleanup problem)",
1048 arg
, msgbuf
, bufsize
);
1051 PyString_AS_STRING(s
),
1058 #ifdef Py_USING_UNICODE
1059 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1060 if (*format
== '#') { /* any buffer-like object */
1061 void **p
= (void **)va_arg(*p_va
, char **);
1063 if (PyUnicode_Check(arg
)) {
1064 *p
= PyUnicode_AS_UNICODE(arg
);
1065 STORE_SIZE(PyUnicode_GET_SIZE(arg
));
1068 return converterr("cannot convert raw buffers",
1069 arg
, msgbuf
, bufsize
);
1073 Py_UNICODE
**p
= va_arg(*p_va
, Py_UNICODE
**);
1074 if (PyUnicode_Check(arg
))
1075 *p
= PyUnicode_AS_UNICODE(arg
);
1077 return converterr("unicode", arg
, msgbuf
, bufsize
);
1083 case 'S': { /* string object */
1084 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1085 if (PyString_Check(arg
))
1088 return converterr("string", arg
, msgbuf
, bufsize
);
1092 #ifdef Py_USING_UNICODE
1093 case 'U': { /* Unicode object */
1094 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1095 if (PyUnicode_Check(arg
))
1098 return converterr("unicode", arg
, msgbuf
, bufsize
);
1103 case 'O': { /* object */
1106 if (*format
== '!') {
1107 type
= va_arg(*p_va
, PyTypeObject
*);
1108 p
= va_arg(*p_va
, PyObject
**);
1110 if (PyType_IsSubtype(arg
->ob_type
, type
))
1113 return converterr(type
->tp_name
, arg
, msgbuf
, bufsize
);
1116 else if (*format
== '?') {
1117 inquiry pred
= va_arg(*p_va
, inquiry
);
1118 p
= va_arg(*p_va
, PyObject
**);
1123 return converterr("(unspecified)",
1124 arg
, msgbuf
, bufsize
);
1127 else if (*format
== '&') {
1128 typedef int (*converter
)(PyObject
*, void *);
1129 converter convert
= va_arg(*p_va
, converter
);
1130 void *addr
= va_arg(*p_va
, void *);
1132 if (! (*convert
)(arg
, addr
))
1133 return converterr("(unspecified)",
1134 arg
, msgbuf
, bufsize
);
1137 p
= va_arg(*p_va
, PyObject
**);
1144 case 'w': { /* memory buffer, read-write access */
1145 void **p
= va_arg(*p_va
, void **);
1146 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1150 pb
->bf_getwritebuffer
== NULL
||
1151 pb
->bf_getsegcount
== NULL
)
1152 return converterr("read-write buffer", arg
, msgbuf
, bufsize
);
1153 if ((*pb
->bf_getsegcount
)(arg
, NULL
) != 1)
1154 return converterr("single-segment read-write buffer",
1155 arg
, msgbuf
, bufsize
);
1156 if ((count
= pb
->bf_getwritebuffer(arg
, 0, p
)) < 0)
1157 return converterr("(unspecified)", arg
, msgbuf
, bufsize
);
1158 if (*format
== '#') {
1166 case 't': { /* 8-bit character buffer, read-only access */
1167 char **p
= va_arg(*p_va
, char **);
1168 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1171 if (*format
++ != '#')
1173 "invalid use of 't' format character",
1174 arg
, msgbuf
, bufsize
);
1175 if (!PyType_HasFeature(arg
->ob_type
,
1176 Py_TPFLAGS_HAVE_GETCHARBUFFER
) ||
1177 pb
== NULL
|| pb
->bf_getcharbuffer
== NULL
||
1178 pb
->bf_getsegcount
== NULL
)
1180 "string or read-only character buffer",
1181 arg
, msgbuf
, bufsize
);
1183 if (pb
->bf_getsegcount(arg
, NULL
) != 1)
1185 "string or single-segment read-only buffer",
1186 arg
, msgbuf
, bufsize
);
1188 count
= pb
->bf_getcharbuffer(arg
, 0, p
);
1190 return converterr("(unspecified)", arg
, msgbuf
, bufsize
);
1199 return converterr("impossible<bad format char>", arg
, msgbuf
, bufsize
);
1208 convertbuffer(PyObject
*arg
, void **p
, char **errmsg
)
1210 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1213 pb
->bf_getreadbuffer
== NULL
||
1214 pb
->bf_getsegcount
== NULL
) {
1215 *errmsg
= "string or read-only buffer";
1218 if ((*pb
->bf_getsegcount
)(arg
, NULL
) != 1) {
1219 *errmsg
= "string or single-segment read-only buffer";
1222 if ((count
= (*pb
->bf_getreadbuffer
)(arg
, 0, p
)) < 0) {
1223 *errmsg
= "(unspecified)";
1228 /* Support for keyword arguments donated by
1229 Geoff Philbrick <philbric@delphi.hks.com> */
1231 /* Return false (0) for error, else true. */
1233 PyArg_ParseTupleAndKeywords(PyObject
*args
,
1241 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1242 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1246 PyErr_BadInternalCall();
1250 va_start(va
, kwlist
);
1251 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &va
, 0);
1257 _PyArg_ParseTupleAndKeywords_SizeT(PyObject
*args
,
1265 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1266 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1270 PyErr_BadInternalCall();
1274 va_start(va
, kwlist
);
1275 retval
= vgetargskeywords(args
, keywords
, format
,
1276 kwlist
, &va
, FLAG_SIZE_T
);
1283 PyArg_VaParseTupleAndKeywords(PyObject
*args
,
1286 char **kwlist
, va_list va
)
1291 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1292 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1296 PyErr_BadInternalCall();
1300 #ifdef VA_LIST_IS_ARRAY
1301 memcpy(lva
, va
, sizeof(va_list));
1310 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &lva
, 0);
1315 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject
*args
,
1318 char **kwlist
, va_list va
)
1323 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1324 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1328 PyErr_BadInternalCall();
1332 #ifdef VA_LIST_IS_ARRAY
1333 memcpy(lva
, va
, sizeof(va_list));
1342 retval
= vgetargskeywords(args
, keywords
, format
,
1343 kwlist
, &lva
, FLAG_SIZE_T
);
1349 vgetargskeywords(PyObject
*args
, PyObject
*keywords
, const char *format
,
1350 char **kwlist
, va_list *p_va
, int flags
)
1354 const char *fname
, *message
;
1356 const char *formatsave
;
1357 int i
, len
, nargs
, nkeywords
;
1360 PyObject
*freelist
= NULL
;
1362 assert(args
!= NULL
&& PyTuple_Check(args
));
1363 assert(keywords
== NULL
|| PyDict_Check(keywords
));
1364 assert(format
!= NULL
);
1365 assert(kwlist
!= NULL
);
1366 assert(p_va
!= NULL
);
1368 /* Search the format:
1369 message <- error msg, if any (else NULL).
1370 fname <- routine name, if any (else NULL).
1371 min <- # of required arguments, or -1 if all are required.
1372 max <- most arguments (required + optional).
1373 Check that kwlist has a non-NULL entry for each arg.
1374 Raise error if a tuple arg spec is found.
1376 fname
= message
= NULL
;
1377 formatsave
= format
;
1381 while ((i
= *format
++) != '\0') {
1382 if (isalpha(Py_CHARMASK(i
)) && i
!= 'e') {
1385 PyErr_SetString(PyExc_RuntimeError
,
1386 "more argument specifiers than "
1387 "keyword list entries");
1394 else if (i
== ':') {
1398 else if (i
== ';') {
1402 else if (i
== '(') {
1403 PyErr_SetString(PyExc_RuntimeError
,
1404 "tuple found in format when using keyword "
1409 format
= formatsave
;
1411 PyErr_SetString(PyExc_RuntimeError
,
1412 "more keyword list entries than "
1413 "argument specifiers");
1417 /* All arguments are required. */
1421 nargs
= PyTuple_GET_SIZE(args
);
1422 nkeywords
= keywords
== NULL
? 0 : PyDict_Size(keywords
);
1424 /* make sure there are no duplicate values for an argument;
1425 its not clear when to use the term "keyword argument vs.
1426 keyword parameter in messages */
1427 if (nkeywords
> 0) {
1428 for (i
= 0; i
< nargs
; i
++) {
1429 const char *thiskw
= kwlist
[i
];
1432 if (PyDict_GetItemString(keywords
, thiskw
)) {
1433 PyErr_Format(PyExc_TypeError
,
1434 "keyword parameter '%s' was given "
1435 "by position and by name",
1439 else if (PyErr_Occurred())
1444 /* required arguments missing from args can be supplied by keyword
1445 arguments; set len to the number of positional arguments, and,
1446 if that's less than the minimum required, add in the number of
1447 required arguments that are supplied by keywords */
1449 if (nkeywords
> 0 && nargs
< min
) {
1450 for (i
= nargs
; i
< min
; i
++) {
1451 if (PyDict_GetItemString(keywords
, kwlist
[i
]))
1453 else if (PyErr_Occurred())
1458 /* make sure we got an acceptable number of arguments; the message
1459 is a little confusing with keywords since keyword arguments
1460 which are supplied, but don't match the required arguments
1461 are not included in the "%d given" part of the message
1462 XXX and this isn't a bug!? */
1463 if (len
< min
|| max
< len
) {
1464 if (message
== NULL
) {
1465 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
1466 "%.200s%s takes %s %d argument%s "
1468 fname
==NULL
? "function" : fname
,
1469 fname
==NULL
? "" : "()",
1470 min
==max
? "exactly"
1471 : len
< min
? "at least" : "at most",
1472 len
< min
? min
: max
,
1473 (len
< min
? min
: max
) == 1 ? "" : "s",
1477 PyErr_SetString(PyExc_TypeError
, message
);
1481 /* convert the positional arguments */
1482 for (i
= 0; i
< nargs
; i
++) {
1485 msg
= convertitem(PyTuple_GET_ITEM(args
, i
), &format
, p_va
,
1486 flags
, levels
, msgbuf
, sizeof(msgbuf
),
1489 seterror(i
+1, msg
, levels
, fname
, message
);
1490 return cleanreturn(0, freelist
);
1494 /* handle no keyword parameters in call */
1496 return cleanreturn(1, freelist
);
1498 /* convert the keyword arguments; this uses the format
1499 string where it was left after processing args */
1500 for (i
= nargs
; i
< max
; i
++) {
1504 item
= PyDict_GetItemString(keywords
, kwlist
[i
]);
1507 msg
= convertitem(item
, &format
, p_va
, flags
, levels
,
1508 msgbuf
, sizeof(msgbuf
), &freelist
);
1511 seterror(i
+1, msg
, levels
, fname
, message
);
1512 return cleanreturn(0, freelist
);
1518 else if (PyErr_Occurred())
1519 return cleanreturn(0, freelist
);
1521 msg
= skipitem(&format
, p_va
, flags
);
1524 seterror(i
+1, msg
, levels
, fname
, message
);
1525 return cleanreturn(0, freelist
);
1530 /* make sure there are no extraneous keyword arguments */
1531 if (nkeywords
> 0) {
1532 PyObject
*key
, *value
;
1534 while (PyDict_Next(keywords
, &pos
, &key
, &value
)) {
1537 if (!PyString_Check(key
)) {
1538 PyErr_SetString(PyExc_TypeError
,
1539 "keywords must be strings");
1540 return cleanreturn(0, freelist
);
1542 ks
= PyString_AsString(key
);
1543 for (i
= 0; i
< max
; i
++) {
1544 if (!strcmp(ks
, kwlist
[i
])) {
1550 PyErr_Format(PyExc_TypeError
,
1551 "'%s' is an invalid keyword "
1552 "argument for this function",
1554 return cleanreturn(0, freelist
);
1559 return cleanreturn(1, freelist
);
1564 skipitem(const char **p_format
, va_list *p_va
, int flags
)
1566 const char *format
= *p_format
;
1572 * The individual types (second arg of va_arg) are irrelevant */
1574 case 'b': /* byte -- very short int */
1575 case 'B': /* byte as bitfield */
1576 case 'h': /* short int */
1577 case 'H': /* short int as bitfield */
1579 case 'I': /* int sized bitfield */
1580 case 'l': /* long int */
1581 case 'k': /* long int sized bitfield */
1582 #ifdef HAVE_LONG_LONG
1583 case 'L': /* PY_LONG_LONG */
1584 case 'K': /* PY_LONG_LONG sized bitfield */
1586 case 'f': /* float */
1587 case 'd': /* double */
1588 #ifndef WITHOUT_COMPLEX
1589 case 'D': /* complex double */
1591 case 'c': /* char */
1593 (void) va_arg(*p_va
, void *);
1597 case 'n': /* Py_ssize_t */
1599 (void) va_arg(*p_va
, Py_ssize_t
*);
1605 case 'e': /* string with encoding */
1607 (void) va_arg(*p_va
, const char *);
1608 if (!(*format
== 's' || *format
== 't'))
1609 /* after 'e', only 's' and 't' is allowed */
1612 /* explicit fallthrough to string cases */
1615 case 's': /* string */
1616 case 'z': /* string or None */
1617 #ifdef Py_USING_UNICODE
1618 case 'u': /* unicode string */
1620 case 't': /* buffer, read-only */
1621 case 'w': /* buffer, read-write */
1623 (void) va_arg(*p_va
, char **);
1624 if (*format
== '#') {
1625 if (flags
& FLAG_SIZE_T
)
1626 (void) va_arg(*p_va
, Py_ssize_t
*);
1628 (void) va_arg(*p_va
, int *);
1636 case 'S': /* string object */
1637 #ifdef Py_USING_UNICODE
1638 case 'U': /* unicode string object */
1641 (void) va_arg(*p_va
, PyObject
**);
1645 case 'O': /* object */
1647 if (*format
== '!') {
1649 (void) va_arg(*p_va
, PyTypeObject
*);
1650 (void) va_arg(*p_va
, PyObject
**);
1653 /* I don't know what this is for */
1654 else if (*format
== '?') {
1655 inquiry pred
= va_arg(*p_va
, inquiry
);
1658 (void) va_arg(*p_va
, PyObject
**);
1662 else if (*format
== '&') {
1663 typedef int (*converter
)(PyObject
*, void *);
1664 (void) va_arg(*p_va
, converter
);
1665 (void) va_arg(*p_va
, void *);
1669 (void) va_arg(*p_va
, PyObject
**);
1676 return "impossible<bad format char>";
1680 /* The "(...)" format code for tuples is not handled here because
1681 * it is not allowed with keyword args. */
1689 PyArg_UnpackTuple(PyObject
*args
, const char *name
, Py_ssize_t min
, Py_ssize_t max
, ...)
1695 #ifdef HAVE_STDARG_PROTOTYPES
1696 va_start(vargs
, max
);
1703 if (!PyTuple_Check(args
)) {
1704 PyErr_SetString(PyExc_SystemError
,
1705 "PyArg_UnpackTuple() argument list is not a tuple");
1708 l
= PyTuple_GET_SIZE(args
);
1713 "%s expected %s%zd arguments, got %zd",
1714 name
, (min
== max
? "" : "at least "), min
, l
);
1718 "unpacked tuple should have %s%zd elements,"
1720 (min
== max
? "" : "at least "), min
, l
);
1728 "%s expected %s%zd arguments, got %zd",
1729 name
, (min
== max
? "" : "at most "), max
, l
);
1733 "unpacked tuple should have %s%zd elements,"
1735 (min
== max
? "" : "at most "), max
, l
);
1739 for (i
= 0; i
< l
; i
++) {
1740 o
= va_arg(vargs
, PyObject
**);
1741 *o
= PyTuple_GET_ITEM(args
, i
);
1748 /* For type constructors that don't take keyword args
1750 * Sets a TypeError and returns 0 if the kwds dict is
1751 * not emtpy, returns 1 otherwise
1754 _PyArg_NoKeywords(const char *funcname
, PyObject
*kw
)
1758 if (!PyDict_CheckExact(kw
)) {
1759 PyErr_BadInternalCall();
1762 if (PyDict_Size(kw
) == 0)
1765 PyErr_Format(PyExc_TypeError
, "%s does not take keyword arguments",