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 **);
47 static int getbuffer(PyObject
*, Py_buffer
*, char**);
49 static int vgetargskeywords(PyObject
*, PyObject
*,
50 const char *, char **, va_list *, int);
51 static char *skipitem(const char **, va_list *, int);
54 PyArg_Parse(PyObject
*args
, const char *format
, ...)
60 retval
= vgetargs1(args
, format
, &va
, FLAG_COMPAT
);
66 _PyArg_Parse_SizeT(PyObject
*args
, char *format
, ...)
72 retval
= vgetargs1(args
, format
, &va
, FLAG_COMPAT
|FLAG_SIZE_T
);
79 PyArg_ParseTuple(PyObject
*args
, const char *format
, ...)
85 retval
= vgetargs1(args
, format
, &va
, 0);
91 _PyArg_ParseTuple_SizeT(PyObject
*args
, char *format
, ...)
97 retval
= vgetargs1(args
, format
, &va
, FLAG_SIZE_T
);
104 PyArg_VaParse(PyObject
*args
, const char *format
, va_list va
)
108 #ifdef VA_LIST_IS_ARRAY
109 memcpy(lva
, va
, sizeof(va_list));
118 return vgetargs1(args
, format
, &lva
, 0);
122 _PyArg_VaParse_SizeT(PyObject
*args
, char *format
, va_list va
)
126 #ifdef VA_LIST_IS_ARRAY
127 memcpy(lva
, va
, sizeof(va_list));
136 return vgetargs1(args
, format
, &lva
, FLAG_SIZE_T
);
140 /* Handle cleanup of allocated memory in case of exception */
143 cleanup_ptr(void *ptr
)
149 cleanup_buffer(void *ptr
)
151 PyBuffer_Release((Py_buffer
*) ptr
);
155 addcleanup(void *ptr
, PyObject
**freelist
, void (*destr
)(void *))
159 *freelist
= PyList_New(0);
165 cobj
= PyCObject_FromVoidPtr(ptr
, destr
);
170 if (PyList_Append(*freelist
, cobj
)) {
179 cleanreturn(int retval
, PyObject
*freelist
)
181 if (freelist
&& retval
!= 0) {
182 /* We were successful, reset the destructors so that they
184 Py_ssize_t len
= PyList_GET_SIZE(freelist
), i
;
185 for (i
= 0; i
< len
; i
++)
186 ((PyCObject
*) PyList_GET_ITEM(freelist
, i
))
189 Py_XDECREF(freelist
);
195 vgetargs1(PyObject
*args
, const char *format
, va_list *p_va
, int flags
)
199 const char *fname
= NULL
;
200 const char *message
= NULL
;
205 const char *formatsave
= format
;
208 PyObject
*freelist
= NULL
;
209 int compat
= flags
& FLAG_COMPAT
;
211 assert(compat
|| (args
!= (PyObject
*)NULL
));
212 flags
= flags
& ~FLAG_COMPAT
;
214 while (endfmt
== 0) {
222 Py_FatalError("too many tuple nesting levels "
223 "in argument format string");
227 Py_FatalError("excess ')' in getargs format");
246 else if (isalpha(Py_CHARMASK(c
))) {
247 if (c
!= 'e') /* skip encoded */
257 Py_FatalError(/* '(' */ "missing ')' in getargs format");
268 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
269 "%.200s%s takes no arguments",
270 fname
==NULL
? "function" : fname
,
271 fname
==NULL
? "" : "()");
272 PyErr_SetString(PyExc_TypeError
, msgbuf
);
275 else if (min
== 1 && max
== 1) {
277 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
278 "%.200s%s takes at least one argument",
279 fname
==NULL
? "function" : fname
,
280 fname
==NULL
? "" : "()");
281 PyErr_SetString(PyExc_TypeError
, msgbuf
);
284 msg
= convertitem(args
, &format
, p_va
, flags
, levels
,
285 msgbuf
, sizeof(msgbuf
), &freelist
);
287 return cleanreturn(1, freelist
);
288 seterror(levels
[0], msg
, levels
+1, fname
, message
);
289 return cleanreturn(0, freelist
);
292 PyErr_SetString(PyExc_SystemError
,
293 "old style getargs format uses new features");
298 if (!PyTuple_Check(args
)) {
299 PyErr_SetString(PyExc_SystemError
,
300 "new style getargs format but argument is not a tuple");
304 len
= PyTuple_GET_SIZE(args
);
306 if (len
< min
|| max
< len
) {
307 if (message
== NULL
) {
308 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
309 "%.150s%s takes %s %d argument%s "
311 fname
==NULL
? "function" : fname
,
312 fname
==NULL
? "" : "()",
314 : len
< min
? "at least" : "at most",
315 len
< min
? min
: max
,
316 (len
< min
? min
: max
) == 1 ? "" : "s",
317 Py_SAFE_DOWNCAST(len
, Py_ssize_t
, long));
320 PyErr_SetString(PyExc_TypeError
, message
);
324 for (i
= 0; i
< len
; i
++) {
327 msg
= convertitem(PyTuple_GET_ITEM(args
, i
), &format
, p_va
,
328 flags
, levels
, msgbuf
,
329 sizeof(msgbuf
), &freelist
);
331 seterror(i
+1, msg
, levels
, fname
, msg
);
332 return cleanreturn(0, freelist
);
336 if (*format
!= '\0' && !isalpha(Py_CHARMASK(*format
)) &&
338 *format
!= '|' && *format
!= ':' && *format
!= ';') {
339 PyErr_Format(PyExc_SystemError
,
340 "bad format string: %.200s", formatsave
);
341 return cleanreturn(0, freelist
);
344 return cleanreturn(1, freelist
);
350 seterror(int iarg
, const char *msg
, int *levels
, const char *fname
,
357 if (PyErr_Occurred())
359 else if (message
== NULL
) {
361 PyOS_snprintf(p
, sizeof(buf
), "%.200s() ", fname
);
365 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
),
366 "argument %d", iarg
);
369 while (levels
[i
] > 0 && i
< 32 && (int)(p
-buf
) < 220) {
370 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
),
371 ", item %d", levels
[i
]-1);
377 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
), "argument");
380 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
), " %.256s", msg
);
383 PyErr_SetString(PyExc_TypeError
, message
);
387 /* Convert a tuple argument.
388 On entry, *p_format points to the character _after_ the opening '('.
389 On successful exit, *p_format points to the closing ')'.
391 *p_format and *p_va are updated,
392 *levels and *msgbuf are untouched,
393 and NULL is returned.
394 If the argument is invalid:
395 *p_format is unchanged,
397 *levels is a 0-terminated list of item numbers,
398 *msgbuf contains an error message, whose format is:
399 "must be <typename1>, not <typename2>", where:
400 <typename1> is the name of the expected type, and
401 <typename2> is the name of the actual type,
402 and msgbuf is returned.
406 converttuple(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
407 int *levels
, char *msgbuf
, size_t bufsize
, int toplevel
,
412 const char *format
= *p_format
;
427 else if (c
== ':' || c
== ';' || c
== '\0')
429 else if (level
== 0 && isalpha(Py_CHARMASK(c
)))
433 if (!PySequence_Check(arg
) || PyString_Check(arg
)) {
435 PyOS_snprintf(msgbuf
, bufsize
,
436 toplevel
? "expected %d arguments, not %.50s" :
437 "must be %d-item sequence, not %.50s",
439 arg
== Py_None
? "None" : arg
->ob_type
->tp_name
);
443 if ((i
= PySequence_Size(arg
)) != n
) {
445 PyOS_snprintf(msgbuf
, bufsize
,
446 toplevel
? "expected %d arguments, not %d" :
447 "must be sequence of length %d, not %d",
453 for (i
= 0; i
< n
; i
++) {
456 item
= PySequence_GetItem(arg
, i
);
461 strncpy(msgbuf
, "is not retrievable", bufsize
);
464 msg
= convertitem(item
, &format
, p_va
, flags
, levels
+1,
465 msgbuf
, bufsize
, freelist
);
466 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
479 /* Convert a single item. */
482 convertitem(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
483 int *levels
, char *msgbuf
, size_t bufsize
, PyObject
**freelist
)
486 const char *format
= *p_format
;
488 if (*format
== '(' /* ')' */) {
490 msg
= converttuple(arg
, &format
, p_va
, flags
, levels
, msgbuf
,
491 bufsize
, 0, freelist
);
496 msg
= convertsimple(arg
, &format
, p_va
, flags
,
497 msgbuf
, bufsize
, freelist
);
508 #define UNICODE_DEFAULT_ENCODING(arg) \
509 _PyUnicode_AsDefaultEncodedString(arg, NULL)
511 /* Format an error message generated by convertsimple(). */
514 converterr(const char *expected
, PyObject
*arg
, char *msgbuf
, size_t bufsize
)
516 assert(expected
!= NULL
);
518 PyOS_snprintf(msgbuf
, bufsize
,
519 "must be %.50s, not %.50s", expected
,
520 arg
== Py_None
? "None" : arg
->ob_type
->tp_name
);
524 #define CONV_UNICODE "(unicode conversion error)"
526 /* explicitly check for float arguments when integers are expected. For now
527 * signal a warning. Returns true if an exception was raised. */
529 float_argument_warning(PyObject
*arg
)
531 if (PyFloat_Check(arg
) &&
532 PyErr_Warn(PyExc_DeprecationWarning
,
533 "integer argument expected, got float" ))
539 /* explicitly check for float arguments when integers are expected. Raises
540 TypeError and returns true for float arguments. */
542 float_argument_error(PyObject
*arg
)
544 if (PyFloat_Check(arg
)) {
545 PyErr_SetString(PyExc_TypeError
,
546 "integer argument expected, got float");
553 /* Convert a non-tuple argument. Return NULL if conversion went OK,
554 or a string with a message describing the failure. The message is
555 formatted as "must be <desired type>, not <actual type>".
556 When failing, an exception may or may not have been raised.
557 Don't call if a tuple is expected.
559 When you add new format codes, please don't forget poor skipitem() below.
563 convertsimple(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
564 char *msgbuf
, size_t bufsize
, PyObject
**freelist
)
567 #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
568 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
569 else q=va_arg(*p_va, int*);
570 #define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
571 #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
573 const char *format
= *p_format
;
575 #ifdef Py_USING_UNICODE
581 case 'b': { /* unsigned byte -- very short int */
582 char *p
= va_arg(*p_va
, char *);
584 if (float_argument_error(arg
))
585 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
586 ival
= PyInt_AsLong(arg
);
587 if (ival
== -1 && PyErr_Occurred())
588 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
590 PyErr_SetString(PyExc_OverflowError
,
591 "unsigned byte integer is less than minimum");
592 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
594 else if (ival
> UCHAR_MAX
) {
595 PyErr_SetString(PyExc_OverflowError
,
596 "unsigned byte integer is greater than maximum");
597 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
600 *p
= (unsigned char) ival
;
604 case 'B': {/* byte sized bitfield - both signed and unsigned
606 char *p
= va_arg(*p_va
, char *);
608 if (float_argument_error(arg
))
609 return converterr("integer<B>", arg
, msgbuf
, bufsize
);
610 ival
= PyInt_AsUnsignedLongMask(arg
);
611 if (ival
== -1 && PyErr_Occurred())
612 return converterr("integer<B>", arg
, msgbuf
, bufsize
);
614 *p
= (unsigned char) ival
;
618 case 'h': {/* signed short int */
619 short *p
= va_arg(*p_va
, short *);
621 if (float_argument_error(arg
))
622 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
623 ival
= PyInt_AsLong(arg
);
624 if (ival
== -1 && PyErr_Occurred())
625 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
626 else if (ival
< SHRT_MIN
) {
627 PyErr_SetString(PyExc_OverflowError
,
628 "signed short integer is less than minimum");
629 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
631 else if (ival
> SHRT_MAX
) {
632 PyErr_SetString(PyExc_OverflowError
,
633 "signed short integer is greater than maximum");
634 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
641 case 'H': { /* short int sized bitfield, both signed and
643 unsigned short *p
= va_arg(*p_va
, unsigned short *);
645 if (float_argument_error(arg
))
646 return converterr("integer<H>", arg
, msgbuf
, bufsize
);
647 ival
= PyInt_AsUnsignedLongMask(arg
);
648 if (ival
== -1 && PyErr_Occurred())
649 return converterr("integer<H>", arg
, msgbuf
, bufsize
);
651 *p
= (unsigned short) ival
;
655 case 'i': {/* signed int */
656 int *p
= va_arg(*p_va
, int *);
658 if (float_argument_error(arg
))
659 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
660 ival
= PyInt_AsLong(arg
);
661 if (ival
== -1 && PyErr_Occurred())
662 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
663 else if (ival
> INT_MAX
) {
664 PyErr_SetString(PyExc_OverflowError
,
665 "signed integer is greater than maximum");
666 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
668 else if (ival
< INT_MIN
) {
669 PyErr_SetString(PyExc_OverflowError
,
670 "signed integer is less than minimum");
671 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
678 case 'I': { /* int sized bitfield, both signed and
680 unsigned int *p
= va_arg(*p_va
, unsigned int *);
682 if (float_argument_error(arg
))
683 return converterr("integer<I>", arg
, msgbuf
, bufsize
);
684 ival
= (unsigned int)PyInt_AsUnsignedLongMask(arg
);
685 if (ival
== (unsigned int)-1 && PyErr_Occurred())
686 return converterr("integer<I>", arg
, msgbuf
, bufsize
);
692 case 'n': /* Py_ssize_t */
693 #if SIZEOF_SIZE_T != SIZEOF_LONG
695 Py_ssize_t
*p
= va_arg(*p_va
, Py_ssize_t
*);
697 if (float_argument_error(arg
))
698 return converterr("integer<n>", arg
, msgbuf
, bufsize
);
699 ival
= PyInt_AsSsize_t(arg
);
700 if (ival
== -1 && PyErr_Occurred())
701 return converterr("integer<n>", arg
, msgbuf
, bufsize
);
706 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
707 case 'l': {/* long int */
708 long *p
= va_arg(*p_va
, long *);
710 if (float_argument_error(arg
))
711 return converterr("integer<l>", arg
, msgbuf
, bufsize
);
712 ival
= PyInt_AsLong(arg
);
713 if (ival
== -1 && PyErr_Occurred())
714 return converterr("integer<l>", arg
, msgbuf
, bufsize
);
720 case 'k': { /* long sized bitfield */
721 unsigned long *p
= va_arg(*p_va
, unsigned long *);
723 if (PyInt_Check(arg
))
724 ival
= PyInt_AsUnsignedLongMask(arg
);
725 else if (PyLong_Check(arg
))
726 ival
= PyLong_AsUnsignedLongMask(arg
);
728 return converterr("integer<k>", arg
, msgbuf
, bufsize
);
733 #ifdef HAVE_LONG_LONG
734 case 'L': {/* PY_LONG_LONG */
735 PY_LONG_LONG
*p
= va_arg( *p_va
, PY_LONG_LONG
* );
737 if (float_argument_warning(arg
))
738 return converterr("long<L>", arg
, msgbuf
, bufsize
);
739 ival
= PyLong_AsLongLong(arg
);
740 if (ival
== (PY_LONG_LONG
)-1 && PyErr_Occurred() ) {
741 return converterr("long<L>", arg
, msgbuf
, bufsize
);
748 case 'K': { /* long long sized bitfield */
749 unsigned PY_LONG_LONG
*p
= va_arg(*p_va
, unsigned PY_LONG_LONG
*);
750 unsigned PY_LONG_LONG ival
;
751 if (PyInt_Check(arg
))
752 ival
= PyInt_AsUnsignedLongMask(arg
);
753 else if (PyLong_Check(arg
))
754 ival
= PyLong_AsUnsignedLongLongMask(arg
);
756 return converterr("integer<K>", arg
, msgbuf
, bufsize
);
762 case 'f': {/* float */
763 float *p
= va_arg(*p_va
, float *);
764 double dval
= PyFloat_AsDouble(arg
);
765 if (PyErr_Occurred())
766 return converterr("float<f>", arg
, msgbuf
, bufsize
);
772 case 'd': {/* double */
773 double *p
= va_arg(*p_va
, double *);
774 double dval
= PyFloat_AsDouble(arg
);
775 if (PyErr_Occurred())
776 return converterr("float<d>", arg
, msgbuf
, bufsize
);
782 #ifndef WITHOUT_COMPLEX
783 case 'D': {/* complex double */
784 Py_complex
*p
= va_arg(*p_va
, Py_complex
*);
786 cval
= PyComplex_AsCComplex(arg
);
787 if (PyErr_Occurred())
788 return converterr("complex<D>", arg
, msgbuf
, bufsize
);
793 #endif /* WITHOUT_COMPLEX */
795 case 'c': {/* char */
796 char *p
= va_arg(*p_va
, char *);
797 if (PyString_Check(arg
) && PyString_Size(arg
) == 1)
798 *p
= PyString_AS_STRING(arg
)[0];
800 return converterr("char", arg
, msgbuf
, bufsize
);
804 case 's': {/* string */
805 if (*format
== '*') {
806 Py_buffer
*p
= (Py_buffer
*)va_arg(*p_va
, Py_buffer
*);
808 if (PyString_Check(arg
)) {
809 PyBuffer_FillInfo(p
, arg
,
810 PyString_AS_STRING(arg
), PyString_GET_SIZE(arg
),
813 #ifdef Py_USING_UNICODE
814 else if (PyUnicode_Check(arg
)) {
815 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
817 return converterr(CONV_UNICODE
,
818 arg
, msgbuf
, bufsize
);
819 PyBuffer_FillInfo(p
, arg
,
820 PyString_AS_STRING(uarg
), PyString_GET_SIZE(uarg
),
824 else { /* any buffer-like object */
826 if (getbuffer(arg
, p
, &buf
) < 0)
827 return converterr(buf
, arg
, msgbuf
, bufsize
);
829 if (addcleanup(p
, freelist
, cleanup_buffer
)) {
832 arg
, msgbuf
, bufsize
);
835 } else if (*format
== '#') {
836 void **p
= (void **)va_arg(*p_va
, char **);
839 if (PyString_Check(arg
)) {
840 *p
= PyString_AS_STRING(arg
);
841 STORE_SIZE(PyString_GET_SIZE(arg
));
843 #ifdef Py_USING_UNICODE
844 else if (PyUnicode_Check(arg
)) {
845 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
847 return converterr(CONV_UNICODE
,
848 arg
, msgbuf
, bufsize
);
849 *p
= PyString_AS_STRING(uarg
);
850 STORE_SIZE(PyString_GET_SIZE(uarg
));
853 else { /* any buffer-like object */
855 Py_ssize_t count
= convertbuffer(arg
, p
, &buf
);
857 return converterr(buf
, arg
, msgbuf
, bufsize
);
862 char **p
= va_arg(*p_va
, char **);
864 if (PyString_Check(arg
))
865 *p
= PyString_AS_STRING(arg
);
866 #ifdef Py_USING_UNICODE
867 else if (PyUnicode_Check(arg
)) {
868 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
870 return converterr(CONV_UNICODE
,
871 arg
, msgbuf
, bufsize
);
872 *p
= PyString_AS_STRING(uarg
);
876 return converterr("string", arg
, msgbuf
, bufsize
);
877 if ((Py_ssize_t
)strlen(*p
) != PyString_Size(arg
))
878 return converterr("string without null bytes",
879 arg
, msgbuf
, bufsize
);
884 case 'z': {/* string, may be NULL (None) */
885 if (*format
== '*') {
886 Py_buffer
*p
= (Py_buffer
*)va_arg(*p_va
, Py_buffer
*);
889 PyBuffer_FillInfo(p
, NULL
, NULL
, 0, 1, 0);
890 else if (PyString_Check(arg
)) {
891 PyBuffer_FillInfo(p
, arg
,
892 PyString_AS_STRING(arg
), PyString_GET_SIZE(arg
),
895 #ifdef Py_USING_UNICODE
896 else if (PyUnicode_Check(arg
)) {
897 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
899 return converterr(CONV_UNICODE
,
900 arg
, msgbuf
, bufsize
);
901 PyBuffer_FillInfo(p
, arg
,
902 PyString_AS_STRING(uarg
), PyString_GET_SIZE(uarg
),
906 else { /* any buffer-like object */
908 if (getbuffer(arg
, p
, &buf
) < 0)
909 return converterr(buf
, arg
, msgbuf
, bufsize
);
911 if (addcleanup(p
, freelist
, cleanup_buffer
)) {
914 arg
, msgbuf
, bufsize
);
917 } else if (*format
== '#') { /* any buffer-like object */
918 void **p
= (void **)va_arg(*p_va
, char **);
921 if (arg
== Py_None
) {
925 else if (PyString_Check(arg
)) {
926 *p
= PyString_AS_STRING(arg
);
927 STORE_SIZE(PyString_GET_SIZE(arg
));
929 #ifdef Py_USING_UNICODE
930 else if (PyUnicode_Check(arg
)) {
931 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
933 return converterr(CONV_UNICODE
,
934 arg
, msgbuf
, bufsize
);
935 *p
= PyString_AS_STRING(uarg
);
936 STORE_SIZE(PyString_GET_SIZE(uarg
));
939 else { /* any buffer-like object */
941 Py_ssize_t count
= convertbuffer(arg
, p
, &buf
);
943 return converterr(buf
, arg
, msgbuf
, bufsize
);
948 char **p
= va_arg(*p_va
, char **);
952 else if (PyString_Check(arg
))
953 *p
= PyString_AS_STRING(arg
);
954 #ifdef Py_USING_UNICODE
955 else if (PyUnicode_Check(arg
)) {
956 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
958 return converterr(CONV_UNICODE
,
959 arg
, msgbuf
, bufsize
);
960 *p
= PyString_AS_STRING(uarg
);
964 return converterr("string or None",
965 arg
, msgbuf
, bufsize
);
966 if (*format
== '#') {
968 assert(0); /* XXX redundant with if-case */
972 *q
= PyString_Size(arg
);
975 else if (*p
!= NULL
&&
976 (Py_ssize_t
)strlen(*p
) != PyString_Size(arg
))
978 "string without null bytes or None",
979 arg
, msgbuf
, bufsize
);
984 case 'e': {/* encoded string */
986 const char *encoding
;
991 /* Get 'e' parameter: the encoding name */
992 encoding
= (const char *)va_arg(*p_va
, const char *);
993 #ifdef Py_USING_UNICODE
994 if (encoding
== NULL
)
995 encoding
= PyUnicode_GetDefaultEncoding();
998 /* Get output buffer parameter:
999 's' (recode all objects via Unicode) or
1000 't' (only recode non-string objects)
1004 else if (*format
== 't')
1008 "(unknown parser marker combination)",
1009 arg
, msgbuf
, bufsize
);
1010 buffer
= (char **)va_arg(*p_va
, char **);
1013 return converterr("(buffer is NULL)",
1014 arg
, msgbuf
, bufsize
);
1017 if (!recode_strings
&& PyString_Check(arg
)) {
1022 #ifdef Py_USING_UNICODE
1025 /* Convert object to Unicode */
1026 u
= PyUnicode_FromObject(arg
);
1029 "string or unicode or text buffer",
1030 arg
, msgbuf
, bufsize
);
1032 /* Encode object; use default error handling */
1033 s
= PyUnicode_AsEncodedString(u
,
1038 return converterr("(encoding failed)",
1039 arg
, msgbuf
, bufsize
);
1040 if (!PyString_Check(s
)) {
1043 "(encoder failed to return a string)",
1044 arg
, msgbuf
, bufsize
);
1047 return converterr("string<e>", arg
, msgbuf
, bufsize
);
1050 size
= PyString_GET_SIZE(s
);
1052 /* Write output; output is guaranteed to be 0-terminated */
1053 if (*format
== '#') {
1054 /* Using buffer length parameter '#':
1056 - if *buffer is NULL, a new buffer of the
1057 needed size is allocated and the data
1058 copied into it; *buffer is updated to point
1059 to the new buffer; the caller is
1060 responsible for PyMem_Free()ing it after
1063 - if *buffer is not NULL, the data is
1064 copied to *buffer; *buffer_len has to be
1065 set to the size of the buffer on input;
1066 buffer overflow is signalled with an error;
1067 buffer has to provide enough room for the
1068 encoded string plus the trailing 0-byte
1070 - in both cases, *buffer_len is updated to
1071 the size of the buffer /excluding/ the
1078 if (q
== NULL
&& q2
== NULL
) {
1081 "(buffer_len is NULL)",
1082 arg
, msgbuf
, bufsize
);
1084 if (*buffer
== NULL
) {
1085 *buffer
= PyMem_NEW(char, size
+ 1);
1086 if (*buffer
== NULL
) {
1090 arg
, msgbuf
, bufsize
);
1092 if (addcleanup(*buffer
, freelist
, cleanup_ptr
)) {
1095 "(cleanup problem)",
1096 arg
, msgbuf
, bufsize
);
1099 if (size
+ 1 > BUFFER_LEN
) {
1102 "(buffer overflow)",
1103 arg
, msgbuf
, bufsize
);
1107 PyString_AS_STRING(s
),
1111 /* Using a 0-terminated buffer:
1113 - the encoded string has to be 0-terminated
1114 for this variant to work; if it is not, an
1117 - a new buffer of the needed size is
1118 allocated and the data copied into it;
1119 *buffer is updated to point to the new
1120 buffer; the caller is responsible for
1121 PyMem_Free()ing it after usage
1124 if ((Py_ssize_t
)strlen(PyString_AS_STRING(s
))
1128 "encoded string without NULL bytes",
1129 arg
, msgbuf
, bufsize
);
1131 *buffer
= PyMem_NEW(char, size
+ 1);
1132 if (*buffer
== NULL
) {
1134 return converterr("(memory error)",
1135 arg
, msgbuf
, bufsize
);
1137 if (addcleanup(*buffer
, freelist
, cleanup_ptr
)) {
1139 return converterr("(cleanup problem)",
1140 arg
, msgbuf
, bufsize
);
1143 PyString_AS_STRING(s
),
1150 #ifdef Py_USING_UNICODE
1151 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1152 if (*format
== '#') { /* any buffer-like object */
1153 void **p
= (void **)va_arg(*p_va
, char **);
1155 if (PyUnicode_Check(arg
)) {
1156 *p
= PyUnicode_AS_UNICODE(arg
);
1157 STORE_SIZE(PyUnicode_GET_SIZE(arg
));
1160 return converterr("cannot convert raw buffers",
1161 arg
, msgbuf
, bufsize
);
1165 Py_UNICODE
**p
= va_arg(*p_va
, Py_UNICODE
**);
1166 if (PyUnicode_Check(arg
))
1167 *p
= PyUnicode_AS_UNICODE(arg
);
1169 return converterr("unicode", arg
, msgbuf
, bufsize
);
1175 case 'S': { /* string object */
1176 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1177 if (PyString_Check(arg
))
1180 return converterr("string", arg
, msgbuf
, bufsize
);
1184 #ifdef Py_USING_UNICODE
1185 case 'U': { /* Unicode object */
1186 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1187 if (PyUnicode_Check(arg
))
1190 return converterr("unicode", arg
, msgbuf
, bufsize
);
1195 case 'O': { /* object */
1198 if (*format
== '!') {
1199 type
= va_arg(*p_va
, PyTypeObject
*);
1200 p
= va_arg(*p_va
, PyObject
**);
1202 if (PyType_IsSubtype(arg
->ob_type
, type
))
1205 return converterr(type
->tp_name
, arg
, msgbuf
, bufsize
);
1208 else if (*format
== '?') {
1209 inquiry pred
= va_arg(*p_va
, inquiry
);
1210 p
= va_arg(*p_va
, PyObject
**);
1215 return converterr("(unspecified)",
1216 arg
, msgbuf
, bufsize
);
1219 else if (*format
== '&') {
1220 typedef int (*converter
)(PyObject
*, void *);
1221 converter convert
= va_arg(*p_va
, converter
);
1222 void *addr
= va_arg(*p_va
, void *);
1224 if (! (*convert
)(arg
, addr
))
1225 return converterr("(unspecified)",
1226 arg
, msgbuf
, bufsize
);
1229 p
= va_arg(*p_va
, PyObject
**);
1236 case 'w': { /* memory buffer, read-write access */
1237 void **p
= va_arg(*p_va
, void **);
1239 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1242 if (pb
&& pb
->bf_releasebuffer
&& *format
!= '*')
1243 /* Buffer must be released, yet caller does not use
1244 the Py_buffer protocol. */
1245 return converterr("pinned buffer", arg
, msgbuf
, bufsize
);
1247 if (pb
&& pb
->bf_getbuffer
&& *format
== '*') {
1248 /* Caller is interested in Py_buffer, and the object
1249 supports it directly. */
1251 if (pb
->bf_getbuffer(arg
, (Py_buffer
*)p
, PyBUF_WRITABLE
) < 0) {
1253 return converterr("read-write buffer", arg
, msgbuf
, bufsize
);
1255 if (addcleanup(p
, freelist
, cleanup_buffer
)) {
1257 "(cleanup problem)",
1258 arg
, msgbuf
, bufsize
);
1260 if (!PyBuffer_IsContiguous((Py_buffer
*)p
, 'C'))
1261 return converterr("contiguous buffer", arg
, msgbuf
, bufsize
);
1266 pb
->bf_getwritebuffer
== NULL
||
1267 pb
->bf_getsegcount
== NULL
)
1268 return converterr("read-write buffer", arg
, msgbuf
, bufsize
);
1269 if ((*pb
->bf_getsegcount
)(arg
, NULL
) != 1)
1270 return converterr("single-segment read-write buffer",
1271 arg
, msgbuf
, bufsize
);
1272 if ((count
= pb
->bf_getwritebuffer(arg
, 0, &res
)) < 0)
1273 return converterr("(unspecified)", arg
, msgbuf
, bufsize
);
1274 if (*format
== '*') {
1275 PyBuffer_FillInfo((Py_buffer
*)p
, arg
, res
, count
, 1, 0);
1280 if (*format
== '#') {
1289 case 't': { /* 8-bit character buffer, read-only access */
1290 char **p
= va_arg(*p_va
, char **);
1291 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1294 if (*format
++ != '#')
1296 "invalid use of 't' format character",
1297 arg
, msgbuf
, bufsize
);
1298 if (!PyType_HasFeature(arg
->ob_type
,
1299 Py_TPFLAGS_HAVE_GETCHARBUFFER
) ||
1300 pb
== NULL
|| pb
->bf_getcharbuffer
== NULL
||
1301 pb
->bf_getsegcount
== NULL
)
1303 "string or read-only character buffer",
1304 arg
, msgbuf
, bufsize
);
1306 if (pb
->bf_getsegcount(arg
, NULL
) != 1)
1308 "string or single-segment read-only buffer",
1309 arg
, msgbuf
, bufsize
);
1311 if (pb
->bf_releasebuffer
)
1313 "string or pinned buffer",
1314 arg
, msgbuf
, bufsize
);
1316 count
= pb
->bf_getcharbuffer(arg
, 0, p
);
1318 return converterr("(unspecified)", arg
, msgbuf
, bufsize
);
1327 return converterr("impossible<bad format char>", arg
, msgbuf
, bufsize
);
1336 convertbuffer(PyObject
*arg
, void **p
, char **errmsg
)
1338 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1341 pb
->bf_getreadbuffer
== NULL
||
1342 pb
->bf_getsegcount
== NULL
||
1343 pb
->bf_releasebuffer
!= NULL
) {
1344 *errmsg
= "string or read-only buffer";
1347 if ((*pb
->bf_getsegcount
)(arg
, NULL
) != 1) {
1348 *errmsg
= "string or single-segment read-only buffer";
1351 if ((count
= (*pb
->bf_getreadbuffer
)(arg
, 0, p
)) < 0) {
1352 *errmsg
= "(unspecified)";
1358 getbuffer(PyObject
*arg
, Py_buffer
*view
, char **errmsg
)
1362 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1364 *errmsg
= "string or buffer";
1367 if (pb
->bf_getbuffer
) {
1368 if (pb
->bf_getbuffer(arg
, view
, 0) < 0) {
1369 *errmsg
= "convertible to a buffer";
1372 if (!PyBuffer_IsContiguous(view
, 'C')) {
1373 *errmsg
= "contiguous buffer";
1379 count
= convertbuffer(arg
, &buf
, errmsg
);
1381 *errmsg
= "convertible to a buffer";
1384 PyBuffer_FillInfo(view
, NULL
, buf
, count
, 1, 0);
1388 /* Support for keyword arguments donated by
1389 Geoff Philbrick <philbric@delphi.hks.com> */
1391 /* Return false (0) for error, else true. */
1393 PyArg_ParseTupleAndKeywords(PyObject
*args
,
1401 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1402 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1406 PyErr_BadInternalCall();
1410 va_start(va
, kwlist
);
1411 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &va
, 0);
1417 _PyArg_ParseTupleAndKeywords_SizeT(PyObject
*args
,
1425 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1426 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1430 PyErr_BadInternalCall();
1434 va_start(va
, kwlist
);
1435 retval
= vgetargskeywords(args
, keywords
, format
,
1436 kwlist
, &va
, FLAG_SIZE_T
);
1443 PyArg_VaParseTupleAndKeywords(PyObject
*args
,
1446 char **kwlist
, va_list va
)
1451 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1452 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1456 PyErr_BadInternalCall();
1460 #ifdef VA_LIST_IS_ARRAY
1461 memcpy(lva
, va
, sizeof(va_list));
1470 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &lva
, 0);
1475 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject
*args
,
1478 char **kwlist
, va_list va
)
1483 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1484 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1488 PyErr_BadInternalCall();
1492 #ifdef VA_LIST_IS_ARRAY
1493 memcpy(lva
, va
, sizeof(va_list));
1502 retval
= vgetargskeywords(args
, keywords
, format
,
1503 kwlist
, &lva
, FLAG_SIZE_T
);
1507 #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1510 vgetargskeywords(PyObject
*args
, PyObject
*keywords
, const char *format
,
1511 char **kwlist
, va_list *p_va
, int flags
)
1515 const char *fname
, *msg
, *custom_msg
, *keyword
;
1517 int i
, len
, nargs
, nkeywords
;
1518 PyObject
*freelist
= NULL
, *current_arg
;
1520 assert(args
!= NULL
&& PyTuple_Check(args
));
1521 assert(keywords
== NULL
|| PyDict_Check(keywords
));
1522 assert(format
!= NULL
);
1523 assert(kwlist
!= NULL
);
1524 assert(p_va
!= NULL
);
1526 /* grab the function name or custom error msg first (mutually exclusive) */
1527 fname
= strchr(format
, ':');
1533 custom_msg
= strchr(format
,';');
1538 /* scan kwlist and get greatest possible nbr of args */
1539 for (len
=0; kwlist
[len
]; len
++)
1542 nargs
= PyTuple_GET_SIZE(args
);
1543 nkeywords
= (keywords
== NULL
) ? 0 : PyDict_Size(keywords
);
1544 if (nargs
+ nkeywords
> len
) {
1545 PyErr_Format(PyExc_TypeError
, "%s%s takes at most %d "
1546 "argument%s (%d given)",
1547 (fname
== NULL
) ? "function" : fname
,
1548 (fname
== NULL
) ? "" : "()",
1550 (len
== 1) ? "" : "s",
1555 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1556 for (i
= 0; i
< len
; i
++) {
1557 keyword
= kwlist
[i
];
1558 if (*format
== '|') {
1562 if (IS_END_OF_FORMAT(*format
)) {
1563 PyErr_Format(PyExc_RuntimeError
,
1564 "More keyword list entries (%d) than "
1565 "format specifiers (%d)", len
, i
);
1566 return cleanreturn(0, freelist
);
1570 current_arg
= PyDict_GetItemString(keywords
, keyword
);
1575 /* arg present in tuple and in dict */
1576 PyErr_Format(PyExc_TypeError
,
1577 "Argument given by name ('%s') "
1578 "and position (%d)",
1580 return cleanreturn(0, freelist
);
1583 else if (nkeywords
&& PyErr_Occurred())
1584 return cleanreturn(0, freelist
);
1586 current_arg
= PyTuple_GET_ITEM(args
, i
);
1589 msg
= convertitem(current_arg
, &format
, p_va
, flags
,
1590 levels
, msgbuf
, sizeof(msgbuf
), &freelist
);
1592 seterror(i
+1, msg
, levels
, fname
, custom_msg
);
1593 return cleanreturn(0, freelist
);
1599 PyErr_Format(PyExc_TypeError
, "Required argument "
1600 "'%s' (pos %d) not found",
1602 return cleanreturn(0, freelist
);
1604 /* current code reports success when all required args
1605 * fulfilled and no keyword args left, with no further
1606 * validation. XXX Maybe skip this in debug build ?
1609 return cleanreturn(1, freelist
);
1611 /* We are into optional args, skip thru to any remaining
1613 msg
= skipitem(&format
, p_va
, flags
);
1615 PyErr_Format(PyExc_RuntimeError
, "%s: '%s'", msg
,
1617 return cleanreturn(0, freelist
);
1621 if (!IS_END_OF_FORMAT(*format
) && *format
!= '|') {
1622 PyErr_Format(PyExc_RuntimeError
,
1623 "more argument specifiers than keyword list entries "
1624 "(remaining format:'%s')", format
);
1625 return cleanreturn(0, freelist
);
1628 /* make sure there are no extraneous keyword arguments */
1629 if (nkeywords
> 0) {
1630 PyObject
*key
, *value
;
1632 while (PyDict_Next(keywords
, &pos
, &key
, &value
)) {
1635 if (!PyString_Check(key
)) {
1636 PyErr_SetString(PyExc_TypeError
,
1637 "keywords must be strings");
1638 return cleanreturn(0, freelist
);
1640 ks
= PyString_AsString(key
);
1641 for (i
= 0; i
< len
; i
++) {
1642 if (!strcmp(ks
, kwlist
[i
])) {
1648 PyErr_Format(PyExc_TypeError
,
1649 "'%s' is an invalid keyword "
1650 "argument for this function",
1652 return cleanreturn(0, freelist
);
1657 return cleanreturn(1, freelist
);
1662 skipitem(const char **p_format
, va_list *p_va
, int flags
)
1664 const char *format
= *p_format
;
1670 * The individual types (second arg of va_arg) are irrelevant */
1672 case 'b': /* byte -- very short int */
1673 case 'B': /* byte as bitfield */
1674 case 'h': /* short int */
1675 case 'H': /* short int as bitfield */
1677 case 'I': /* int sized bitfield */
1678 case 'l': /* long int */
1679 case 'k': /* long int sized bitfield */
1680 #ifdef HAVE_LONG_LONG
1681 case 'L': /* PY_LONG_LONG */
1682 case 'K': /* PY_LONG_LONG sized bitfield */
1684 case 'f': /* float */
1685 case 'd': /* double */
1686 #ifndef WITHOUT_COMPLEX
1687 case 'D': /* complex double */
1689 case 'c': /* char */
1691 (void) va_arg(*p_va
, void *);
1695 case 'n': /* Py_ssize_t */
1697 (void) va_arg(*p_va
, Py_ssize_t
*);
1703 case 'e': /* string with encoding */
1705 (void) va_arg(*p_va
, const char *);
1706 if (!(*format
== 's' || *format
== 't'))
1707 /* after 'e', only 's' and 't' is allowed */
1710 /* explicit fallthrough to string cases */
1713 case 's': /* string */
1714 case 'z': /* string or None */
1715 #ifdef Py_USING_UNICODE
1716 case 'u': /* unicode string */
1718 case 't': /* buffer, read-only */
1719 case 'w': /* buffer, read-write */
1721 (void) va_arg(*p_va
, char **);
1722 if (*format
== '#') {
1723 if (flags
& FLAG_SIZE_T
)
1724 (void) va_arg(*p_va
, Py_ssize_t
*);
1726 (void) va_arg(*p_va
, int *);
1728 } else if ((c
== 's' || c
== 'z') && *format
== '*') {
1736 case 'S': /* string object */
1737 #ifdef Py_USING_UNICODE
1738 case 'U': /* unicode string object */
1741 (void) va_arg(*p_va
, PyObject
**);
1745 case 'O': /* object */
1747 if (*format
== '!') {
1749 (void) va_arg(*p_va
, PyTypeObject
*);
1750 (void) va_arg(*p_va
, PyObject
**);
1753 /* I don't know what this is for */
1754 else if (*format
== '?') {
1755 inquiry pred
= va_arg(*p_va
, inquiry
);
1758 (void) va_arg(*p_va
, PyObject
**);
1762 else if (*format
== '&') {
1763 typedef int (*converter
)(PyObject
*, void *);
1764 (void) va_arg(*p_va
, converter
);
1765 (void) va_arg(*p_va
, void *);
1769 (void) va_arg(*p_va
, PyObject
**);
1774 case '(': /* bypass tuple, not handled at all previously */
1780 if (IS_END_OF_FORMAT(*format
))
1781 return "Unmatched left paren in format "
1783 msg
= skipitem(&format
, p_va
, flags
);
1792 return "Unmatched right paren in format string";
1796 return "impossible<bad format char>";
1806 PyArg_UnpackTuple(PyObject
*args
, const char *name
, Py_ssize_t min
, Py_ssize_t max
, ...)
1812 #ifdef HAVE_STDARG_PROTOTYPES
1813 va_start(vargs
, max
);
1820 if (!PyTuple_Check(args
)) {
1821 PyErr_SetString(PyExc_SystemError
,
1822 "PyArg_UnpackTuple() argument list is not a tuple");
1825 l
= PyTuple_GET_SIZE(args
);
1830 "%s expected %s%zd arguments, got %zd",
1831 name
, (min
== max
? "" : "at least "), min
, l
);
1835 "unpacked tuple should have %s%zd elements,"
1837 (min
== max
? "" : "at least "), min
, l
);
1845 "%s expected %s%zd arguments, got %zd",
1846 name
, (min
== max
? "" : "at most "), max
, l
);
1850 "unpacked tuple should have %s%zd elements,"
1852 (min
== max
? "" : "at most "), max
, l
);
1856 for (i
= 0; i
< l
; i
++) {
1857 o
= va_arg(vargs
, PyObject
**);
1858 *o
= PyTuple_GET_ITEM(args
, i
);
1865 /* For type constructors that don't take keyword args
1867 * Sets a TypeError and returns 0 if the kwds dict is
1868 * not empty, returns 1 otherwise
1871 _PyArg_NoKeywords(const char *funcname
, PyObject
*kw
)
1875 if (!PyDict_CheckExact(kw
)) {
1876 PyErr_BadInternalCall();
1879 if (PyDict_Size(kw
) == 0)
1882 PyErr_Format(PyExc_TypeError
, "%s does not take keyword arguments",