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
, message
);
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_error(PyObject
*arg
)
531 if (PyFloat_Check(arg
) &&
532 PyErr_Warn(PyExc_DeprecationWarning
,
533 "integer argument expected, got float" ))
539 /* Convert a non-tuple argument. Return NULL if conversion went OK,
540 or a string with a message describing the failure. The message is
541 formatted as "must be <desired type>, not <actual type>".
542 When failing, an exception may or may not have been raised.
543 Don't call if a tuple is expected.
545 When you add new format codes, please don't forget poor skipitem() below.
549 convertsimple(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
550 char *msgbuf
, size_t bufsize
, PyObject
**freelist
)
553 #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
554 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
555 else q=va_arg(*p_va, int*);
556 #define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
557 #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
559 const char *format
= *p_format
;
561 #ifdef Py_USING_UNICODE
567 case 'b': { /* unsigned byte -- very short int */
568 char *p
= va_arg(*p_va
, char *);
570 if (float_argument_error(arg
))
571 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
572 ival
= PyInt_AsLong(arg
);
573 if (ival
== -1 && PyErr_Occurred())
574 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
576 PyErr_SetString(PyExc_OverflowError
,
577 "unsigned byte integer is less than minimum");
578 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
580 else if (ival
> UCHAR_MAX
) {
581 PyErr_SetString(PyExc_OverflowError
,
582 "unsigned byte integer is greater than maximum");
583 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
586 *p
= (unsigned char) ival
;
590 case 'B': {/* byte sized bitfield - both signed and unsigned
592 char *p
= va_arg(*p_va
, char *);
594 if (float_argument_error(arg
))
595 return converterr("integer<B>", arg
, msgbuf
, bufsize
);
596 ival
= PyInt_AsUnsignedLongMask(arg
);
597 if (ival
== -1 && PyErr_Occurred())
598 return converterr("integer<B>", arg
, msgbuf
, bufsize
);
600 *p
= (unsigned char) ival
;
604 case 'h': {/* signed short int */
605 short *p
= va_arg(*p_va
, short *);
607 if (float_argument_error(arg
))
608 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
609 ival
= PyInt_AsLong(arg
);
610 if (ival
== -1 && PyErr_Occurred())
611 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
612 else if (ival
< SHRT_MIN
) {
613 PyErr_SetString(PyExc_OverflowError
,
614 "signed short integer is less than minimum");
615 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
617 else if (ival
> SHRT_MAX
) {
618 PyErr_SetString(PyExc_OverflowError
,
619 "signed short integer is greater than maximum");
620 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
627 case 'H': { /* short int sized bitfield, both signed and
629 unsigned short *p
= va_arg(*p_va
, unsigned short *);
631 if (float_argument_error(arg
))
632 return converterr("integer<H>", arg
, msgbuf
, bufsize
);
633 ival
= PyInt_AsUnsignedLongMask(arg
);
634 if (ival
== -1 && PyErr_Occurred())
635 return converterr("integer<H>", arg
, msgbuf
, bufsize
);
637 *p
= (unsigned short) ival
;
641 case 'i': {/* signed int */
642 int *p
= va_arg(*p_va
, int *);
644 if (float_argument_error(arg
))
645 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
646 ival
= PyInt_AsLong(arg
);
647 if (ival
== -1 && PyErr_Occurred())
648 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
649 else if (ival
> INT_MAX
) {
650 PyErr_SetString(PyExc_OverflowError
,
651 "signed integer is greater than maximum");
652 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
654 else if (ival
< INT_MIN
) {
655 PyErr_SetString(PyExc_OverflowError
,
656 "signed integer is less than minimum");
657 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
664 case 'I': { /* int sized bitfield, both signed and
666 unsigned int *p
= va_arg(*p_va
, unsigned int *);
668 if (float_argument_error(arg
))
669 return converterr("integer<I>", arg
, msgbuf
, bufsize
);
670 ival
= (unsigned int)PyInt_AsUnsignedLongMask(arg
);
671 if (ival
== (unsigned int)-1 && PyErr_Occurred())
672 return converterr("integer<I>", arg
, msgbuf
, bufsize
);
678 case 'n': /* Py_ssize_t */
679 #if SIZEOF_SIZE_T != SIZEOF_LONG
681 Py_ssize_t
*p
= va_arg(*p_va
, Py_ssize_t
*);
683 if (float_argument_error(arg
))
684 return converterr("integer<n>", arg
, msgbuf
, bufsize
);
685 ival
= PyInt_AsSsize_t(arg
);
686 if (ival
== -1 && PyErr_Occurred())
687 return converterr("integer<n>", arg
, msgbuf
, bufsize
);
692 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
693 case 'l': {/* long int */
694 long *p
= va_arg(*p_va
, long *);
696 if (float_argument_error(arg
))
697 return converterr("integer<l>", arg
, msgbuf
, bufsize
);
698 ival
= PyInt_AsLong(arg
);
699 if (ival
== -1 && PyErr_Occurred())
700 return converterr("integer<l>", arg
, msgbuf
, bufsize
);
706 case 'k': { /* long sized bitfield */
707 unsigned long *p
= va_arg(*p_va
, unsigned long *);
709 if (PyInt_Check(arg
))
710 ival
= PyInt_AsUnsignedLongMask(arg
);
711 else if (PyLong_Check(arg
))
712 ival
= PyLong_AsUnsignedLongMask(arg
);
714 return converterr("integer<k>", arg
, msgbuf
, bufsize
);
719 #ifdef HAVE_LONG_LONG
720 case 'L': {/* PY_LONG_LONG */
721 PY_LONG_LONG
*p
= va_arg( *p_va
, PY_LONG_LONG
* );
722 PY_LONG_LONG ival
= PyLong_AsLongLong( arg
);
723 if (ival
== (PY_LONG_LONG
)-1 && PyErr_Occurred() ) {
724 return converterr("long<L>", arg
, msgbuf
, bufsize
);
731 case 'K': { /* long long sized bitfield */
732 unsigned PY_LONG_LONG
*p
= va_arg(*p_va
, unsigned PY_LONG_LONG
*);
733 unsigned PY_LONG_LONG ival
;
734 if (PyInt_Check(arg
))
735 ival
= PyInt_AsUnsignedLongMask(arg
);
736 else if (PyLong_Check(arg
))
737 ival
= PyLong_AsUnsignedLongLongMask(arg
);
739 return converterr("integer<K>", arg
, msgbuf
, bufsize
);
745 case 'f': {/* float */
746 float *p
= va_arg(*p_va
, float *);
747 double dval
= PyFloat_AsDouble(arg
);
748 if (PyErr_Occurred())
749 return converterr("float<f>", arg
, msgbuf
, bufsize
);
755 case 'd': {/* double */
756 double *p
= va_arg(*p_va
, double *);
757 double dval
= PyFloat_AsDouble(arg
);
758 if (PyErr_Occurred())
759 return converterr("float<d>", arg
, msgbuf
, bufsize
);
765 #ifndef WITHOUT_COMPLEX
766 case 'D': {/* complex double */
767 Py_complex
*p
= va_arg(*p_va
, Py_complex
*);
769 cval
= PyComplex_AsCComplex(arg
);
770 if (PyErr_Occurred())
771 return converterr("complex<D>", arg
, msgbuf
, bufsize
);
776 #endif /* WITHOUT_COMPLEX */
778 case 'c': {/* char */
779 char *p
= va_arg(*p_va
, char *);
780 if (PyString_Check(arg
) && PyString_Size(arg
) == 1)
781 *p
= PyString_AS_STRING(arg
)[0];
783 return converterr("char", arg
, msgbuf
, bufsize
);
787 case 's': {/* string */
788 if (*format
== '*') {
789 Py_buffer
*p
= (Py_buffer
*)va_arg(*p_va
, Py_buffer
*);
791 if (PyString_Check(arg
)) {
792 PyBuffer_FillInfo(p
, arg
,
793 PyString_AS_STRING(arg
), PyString_GET_SIZE(arg
),
796 #ifdef Py_USING_UNICODE
797 else if (PyUnicode_Check(arg
)) {
798 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
800 return converterr(CONV_UNICODE
,
801 arg
, msgbuf
, bufsize
);
802 PyBuffer_FillInfo(p
, arg
,
803 PyString_AS_STRING(uarg
), PyString_GET_SIZE(uarg
),
807 else { /* any buffer-like object */
809 if (getbuffer(arg
, p
, &buf
) < 0)
810 return converterr(buf
, arg
, msgbuf
, bufsize
);
812 if (addcleanup(p
, freelist
, cleanup_buffer
)) {
815 arg
, msgbuf
, bufsize
);
818 } else if (*format
== '#') {
819 void **p
= (void **)va_arg(*p_va
, char **);
822 if (PyString_Check(arg
)) {
823 *p
= PyString_AS_STRING(arg
);
824 STORE_SIZE(PyString_GET_SIZE(arg
));
826 #ifdef Py_USING_UNICODE
827 else if (PyUnicode_Check(arg
)) {
828 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
830 return converterr(CONV_UNICODE
,
831 arg
, msgbuf
, bufsize
);
832 *p
= PyString_AS_STRING(uarg
);
833 STORE_SIZE(PyString_GET_SIZE(uarg
));
836 else { /* any buffer-like object */
838 Py_ssize_t count
= convertbuffer(arg
, p
, &buf
);
840 return converterr(buf
, arg
, msgbuf
, bufsize
);
845 char **p
= va_arg(*p_va
, char **);
847 if (PyString_Check(arg
))
848 *p
= PyString_AS_STRING(arg
);
849 #ifdef Py_USING_UNICODE
850 else if (PyUnicode_Check(arg
)) {
851 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
853 return converterr(CONV_UNICODE
,
854 arg
, msgbuf
, bufsize
);
855 *p
= PyString_AS_STRING(uarg
);
859 return converterr("string", arg
, msgbuf
, bufsize
);
860 if ((Py_ssize_t
)strlen(*p
) != PyString_Size(arg
))
861 return converterr("string without null bytes",
862 arg
, msgbuf
, bufsize
);
867 case 'z': {/* string, may be NULL (None) */
868 if (*format
== '*') {
869 Py_buffer
*p
= (Py_buffer
*)va_arg(*p_va
, Py_buffer
*);
872 PyBuffer_FillInfo(p
, NULL
, NULL
, 0, 1, 0);
873 else if (PyString_Check(arg
)) {
874 PyBuffer_FillInfo(p
, arg
,
875 PyString_AS_STRING(arg
), PyString_GET_SIZE(arg
),
878 #ifdef Py_USING_UNICODE
879 else if (PyUnicode_Check(arg
)) {
880 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
882 return converterr(CONV_UNICODE
,
883 arg
, msgbuf
, bufsize
);
884 PyBuffer_FillInfo(p
, arg
,
885 PyString_AS_STRING(uarg
), PyString_GET_SIZE(uarg
),
889 else { /* any buffer-like object */
891 if (getbuffer(arg
, p
, &buf
) < 0)
892 return converterr(buf
, arg
, msgbuf
, bufsize
);
894 if (addcleanup(p
, freelist
, cleanup_buffer
)) {
897 arg
, msgbuf
, bufsize
);
900 } else if (*format
== '#') { /* any buffer-like object */
901 void **p
= (void **)va_arg(*p_va
, char **);
904 if (arg
== Py_None
) {
908 else if (PyString_Check(arg
)) {
909 *p
= PyString_AS_STRING(arg
);
910 STORE_SIZE(PyString_GET_SIZE(arg
));
912 #ifdef Py_USING_UNICODE
913 else if (PyUnicode_Check(arg
)) {
914 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
916 return converterr(CONV_UNICODE
,
917 arg
, msgbuf
, bufsize
);
918 *p
= PyString_AS_STRING(uarg
);
919 STORE_SIZE(PyString_GET_SIZE(uarg
));
922 else { /* any buffer-like object */
924 Py_ssize_t count
= convertbuffer(arg
, p
, &buf
);
926 return converterr(buf
, arg
, msgbuf
, bufsize
);
931 char **p
= va_arg(*p_va
, char **);
935 else if (PyString_Check(arg
))
936 *p
= PyString_AS_STRING(arg
);
937 #ifdef Py_USING_UNICODE
938 else if (PyUnicode_Check(arg
)) {
939 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
941 return converterr(CONV_UNICODE
,
942 arg
, msgbuf
, bufsize
);
943 *p
= PyString_AS_STRING(uarg
);
947 return converterr("string or None",
948 arg
, msgbuf
, bufsize
);
949 if (*format
== '#') {
951 assert(0); /* XXX redundant with if-case */
955 *q
= PyString_Size(arg
);
958 else if (*p
!= NULL
&&
959 (Py_ssize_t
)strlen(*p
) != PyString_Size(arg
))
961 "string without null bytes or None",
962 arg
, msgbuf
, bufsize
);
967 case 'e': {/* encoded string */
969 const char *encoding
;
974 /* Get 'e' parameter: the encoding name */
975 encoding
= (const char *)va_arg(*p_va
, const char *);
976 #ifdef Py_USING_UNICODE
977 if (encoding
== NULL
)
978 encoding
= PyUnicode_GetDefaultEncoding();
981 /* Get output buffer parameter:
982 's' (recode all objects via Unicode) or
983 't' (only recode non-string objects)
987 else if (*format
== 't')
991 "(unknown parser marker combination)",
992 arg
, msgbuf
, bufsize
);
993 buffer
= (char **)va_arg(*p_va
, char **);
996 return converterr("(buffer is NULL)",
997 arg
, msgbuf
, bufsize
);
1000 if (!recode_strings
&& PyString_Check(arg
)) {
1005 #ifdef Py_USING_UNICODE
1008 /* Convert object to Unicode */
1009 u
= PyUnicode_FromObject(arg
);
1012 "string or unicode or text buffer",
1013 arg
, msgbuf
, bufsize
);
1015 /* Encode object; use default error handling */
1016 s
= PyUnicode_AsEncodedString(u
,
1021 return converterr("(encoding failed)",
1022 arg
, msgbuf
, bufsize
);
1023 if (!PyString_Check(s
)) {
1026 "(encoder failed to return a string)",
1027 arg
, msgbuf
, bufsize
);
1030 return converterr("string<e>", arg
, msgbuf
, bufsize
);
1033 size
= PyString_GET_SIZE(s
);
1035 /* Write output; output is guaranteed to be 0-terminated */
1036 if (*format
== '#') {
1037 /* Using buffer length parameter '#':
1039 - if *buffer is NULL, a new buffer of the
1040 needed size is allocated and the data
1041 copied into it; *buffer is updated to point
1042 to the new buffer; the caller is
1043 responsible for PyMem_Free()ing it after
1046 - if *buffer is not NULL, the data is
1047 copied to *buffer; *buffer_len has to be
1048 set to the size of the buffer on input;
1049 buffer overflow is signalled with an error;
1050 buffer has to provide enough room for the
1051 encoded string plus the trailing 0-byte
1053 - in both cases, *buffer_len is updated to
1054 the size of the buffer /excluding/ the
1061 if (q
== NULL
&& q2
== NULL
) {
1064 "(buffer_len is NULL)",
1065 arg
, msgbuf
, bufsize
);
1067 if (*buffer
== NULL
) {
1068 *buffer
= PyMem_NEW(char, size
+ 1);
1069 if (*buffer
== NULL
) {
1073 arg
, msgbuf
, bufsize
);
1075 if (addcleanup(*buffer
, freelist
, cleanup_ptr
)) {
1078 "(cleanup problem)",
1079 arg
, msgbuf
, bufsize
);
1082 if (size
+ 1 > BUFFER_LEN
) {
1085 "(buffer overflow)",
1086 arg
, msgbuf
, bufsize
);
1090 PyString_AS_STRING(s
),
1094 /* Using a 0-terminated buffer:
1096 - the encoded string has to be 0-terminated
1097 for this variant to work; if it is not, an
1100 - a new buffer of the needed size is
1101 allocated and the data copied into it;
1102 *buffer is updated to point to the new
1103 buffer; the caller is responsible for
1104 PyMem_Free()ing it after usage
1107 if ((Py_ssize_t
)strlen(PyString_AS_STRING(s
))
1111 "(encoded string without NULL bytes)",
1112 arg
, msgbuf
, bufsize
);
1114 *buffer
= PyMem_NEW(char, size
+ 1);
1115 if (*buffer
== NULL
) {
1117 return converterr("(memory error)",
1118 arg
, msgbuf
, bufsize
);
1120 if (addcleanup(*buffer
, freelist
, cleanup_ptr
)) {
1122 return converterr("(cleanup problem)",
1123 arg
, msgbuf
, bufsize
);
1126 PyString_AS_STRING(s
),
1133 #ifdef Py_USING_UNICODE
1134 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1135 if (*format
== '#') { /* any buffer-like object */
1136 void **p
= (void **)va_arg(*p_va
, char **);
1138 if (PyUnicode_Check(arg
)) {
1139 *p
= PyUnicode_AS_UNICODE(arg
);
1140 STORE_SIZE(PyUnicode_GET_SIZE(arg
));
1143 return converterr("cannot convert raw buffers",
1144 arg
, msgbuf
, bufsize
);
1148 Py_UNICODE
**p
= va_arg(*p_va
, Py_UNICODE
**);
1149 if (PyUnicode_Check(arg
))
1150 *p
= PyUnicode_AS_UNICODE(arg
);
1152 return converterr("unicode", arg
, msgbuf
, bufsize
);
1158 case 'S': { /* string object */
1159 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1160 if (PyString_Check(arg
))
1163 return converterr("string", arg
, msgbuf
, bufsize
);
1167 #ifdef Py_USING_UNICODE
1168 case 'U': { /* Unicode object */
1169 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1170 if (PyUnicode_Check(arg
))
1173 return converterr("unicode", arg
, msgbuf
, bufsize
);
1178 case 'O': { /* object */
1181 if (*format
== '!') {
1182 type
= va_arg(*p_va
, PyTypeObject
*);
1183 p
= va_arg(*p_va
, PyObject
**);
1185 if (PyType_IsSubtype(arg
->ob_type
, type
))
1188 return converterr(type
->tp_name
, arg
, msgbuf
, bufsize
);
1191 else if (*format
== '?') {
1192 inquiry pred
= va_arg(*p_va
, inquiry
);
1193 p
= va_arg(*p_va
, PyObject
**);
1198 return converterr("(unspecified)",
1199 arg
, msgbuf
, bufsize
);
1202 else if (*format
== '&') {
1203 typedef int (*converter
)(PyObject
*, void *);
1204 converter convert
= va_arg(*p_va
, converter
);
1205 void *addr
= va_arg(*p_va
, void *);
1207 if (! (*convert
)(arg
, addr
))
1208 return converterr("(unspecified)",
1209 arg
, msgbuf
, bufsize
);
1212 p
= va_arg(*p_va
, PyObject
**);
1219 case 'w': { /* memory buffer, read-write access */
1220 void **p
= va_arg(*p_va
, void **);
1222 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1225 if (pb
&& pb
->bf_releasebuffer
&& *format
!= '*')
1226 /* Buffer must be released, yet caller does not use
1227 the Py_buffer protocol. */
1228 return converterr("pinned buffer", arg
, msgbuf
, bufsize
);
1230 if (pb
&& pb
->bf_getbuffer
&& *format
== '*') {
1231 /* Caller is interested in Py_buffer, and the object
1232 supports it directly. */
1234 if (pb
->bf_getbuffer(arg
, (Py_buffer
*)p
, PyBUF_WRITABLE
) < 0) {
1236 return converterr("read-write buffer", arg
, msgbuf
, bufsize
);
1238 if (addcleanup(p
, freelist
, cleanup_buffer
)) {
1240 "(cleanup problem)",
1241 arg
, msgbuf
, bufsize
);
1243 if (!PyBuffer_IsContiguous((Py_buffer
*)p
, 'C'))
1244 return converterr("contiguous buffer", arg
, msgbuf
, bufsize
);
1249 pb
->bf_getwritebuffer
== NULL
||
1250 pb
->bf_getsegcount
== NULL
)
1251 return converterr("read-write buffer", arg
, msgbuf
, bufsize
);
1252 if ((*pb
->bf_getsegcount
)(arg
, NULL
) != 1)
1253 return converterr("single-segment read-write buffer",
1254 arg
, msgbuf
, bufsize
);
1255 if ((count
= pb
->bf_getwritebuffer(arg
, 0, &res
)) < 0)
1256 return converterr("(unspecified)", arg
, msgbuf
, bufsize
);
1257 if (*format
== '*') {
1258 PyBuffer_FillInfo((Py_buffer
*)p
, arg
, res
, count
, 1, 0);
1263 if (*format
== '#') {
1272 case 't': { /* 8-bit character buffer, read-only access */
1273 char **p
= va_arg(*p_va
, char **);
1274 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1277 if (*format
++ != '#')
1279 "invalid use of 't' format character",
1280 arg
, msgbuf
, bufsize
);
1281 if (!PyType_HasFeature(arg
->ob_type
,
1282 Py_TPFLAGS_HAVE_GETCHARBUFFER
) ||
1283 pb
== NULL
|| pb
->bf_getcharbuffer
== NULL
||
1284 pb
->bf_getsegcount
== NULL
)
1286 "string or read-only character buffer",
1287 arg
, msgbuf
, bufsize
);
1289 if (pb
->bf_getsegcount(arg
, NULL
) != 1)
1291 "string or single-segment read-only buffer",
1292 arg
, msgbuf
, bufsize
);
1294 if (pb
->bf_releasebuffer
)
1296 "string or pinned buffer",
1297 arg
, msgbuf
, bufsize
);
1299 count
= pb
->bf_getcharbuffer(arg
, 0, p
);
1301 return converterr("(unspecified)", arg
, msgbuf
, bufsize
);
1310 return converterr("impossible<bad format char>", arg
, msgbuf
, bufsize
);
1319 convertbuffer(PyObject
*arg
, void **p
, char **errmsg
)
1321 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1324 pb
->bf_getreadbuffer
== NULL
||
1325 pb
->bf_getsegcount
== NULL
||
1326 pb
->bf_releasebuffer
!= NULL
) {
1327 *errmsg
= "string or read-only buffer";
1330 if ((*pb
->bf_getsegcount
)(arg
, NULL
) != 1) {
1331 *errmsg
= "string or single-segment read-only buffer";
1334 if ((count
= (*pb
->bf_getreadbuffer
)(arg
, 0, p
)) < 0) {
1335 *errmsg
= "(unspecified)";
1341 getbuffer(PyObject
*arg
, Py_buffer
*view
, char **errmsg
)
1345 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1347 *errmsg
= "string or buffer";
1350 if (pb
->bf_getbuffer
) {
1351 if (pb
->bf_getbuffer(arg
, view
, 0) < 0) {
1352 *errmsg
= "convertible to a buffer";
1355 if (!PyBuffer_IsContiguous(view
, 'C')) {
1356 *errmsg
= "contiguous buffer";
1362 count
= convertbuffer(arg
, &buf
, errmsg
);
1364 *errmsg
= "convertible to a buffer";
1367 PyBuffer_FillInfo(view
, NULL
, buf
, count
, 1, 0);
1371 /* Support for keyword arguments donated by
1372 Geoff Philbrick <philbric@delphi.hks.com> */
1374 /* Return false (0) for error, else true. */
1376 PyArg_ParseTupleAndKeywords(PyObject
*args
,
1384 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1385 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1389 PyErr_BadInternalCall();
1393 va_start(va
, kwlist
);
1394 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &va
, 0);
1400 _PyArg_ParseTupleAndKeywords_SizeT(PyObject
*args
,
1408 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1409 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1413 PyErr_BadInternalCall();
1417 va_start(va
, kwlist
);
1418 retval
= vgetargskeywords(args
, keywords
, format
,
1419 kwlist
, &va
, FLAG_SIZE_T
);
1426 PyArg_VaParseTupleAndKeywords(PyObject
*args
,
1429 char **kwlist
, va_list va
)
1434 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1435 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1439 PyErr_BadInternalCall();
1443 #ifdef VA_LIST_IS_ARRAY
1444 memcpy(lva
, va
, sizeof(va_list));
1453 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &lva
, 0);
1458 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject
*args
,
1461 char **kwlist
, va_list va
)
1466 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1467 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1471 PyErr_BadInternalCall();
1475 #ifdef VA_LIST_IS_ARRAY
1476 memcpy(lva
, va
, sizeof(va_list));
1485 retval
= vgetargskeywords(args
, keywords
, format
,
1486 kwlist
, &lva
, FLAG_SIZE_T
);
1490 #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1493 vgetargskeywords(PyObject
*args
, PyObject
*keywords
, const char *format
,
1494 char **kwlist
, va_list *p_va
, int flags
)
1498 const char *fname
, *msg
, *custom_msg
, *keyword
;
1500 int i
, len
, nargs
, nkeywords
;
1501 PyObject
*freelist
= NULL
, *current_arg
;
1503 assert(args
!= NULL
&& PyTuple_Check(args
));
1504 assert(keywords
== NULL
|| PyDict_Check(keywords
));
1505 assert(format
!= NULL
);
1506 assert(kwlist
!= NULL
);
1507 assert(p_va
!= NULL
);
1509 /* grab the function name or custom error msg first (mutually exclusive) */
1510 fname
= strchr(format
, ':');
1516 custom_msg
= strchr(format
,';');
1521 /* scan kwlist and get greatest possible nbr of args */
1522 for (len
=0; kwlist
[len
]; len
++)
1525 nargs
= PyTuple_GET_SIZE(args
);
1526 nkeywords
= (keywords
== NULL
) ? 0 : PyDict_Size(keywords
);
1527 if (nargs
+ nkeywords
> len
) {
1528 PyErr_Format(PyExc_TypeError
, "%s%s takes at most %d "
1529 "argument%s (%d given)",
1530 (fname
== NULL
) ? "function" : fname
,
1531 (fname
== NULL
) ? "" : "()",
1533 (len
== 1) ? "" : "s",
1538 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1539 for (i
= 0; i
< len
; i
++) {
1540 keyword
= kwlist
[i
];
1541 if (*format
== '|') {
1545 if (IS_END_OF_FORMAT(*format
)) {
1546 PyErr_Format(PyExc_RuntimeError
,
1547 "More keyword list entries (%d) than "
1548 "format specifiers (%d)", len
, i
);
1549 return cleanreturn(0, freelist
);
1553 current_arg
= PyDict_GetItemString(keywords
, keyword
);
1558 /* arg present in tuple and in dict */
1559 PyErr_Format(PyExc_TypeError
,
1560 "Argument given by name ('%s') "
1561 "and position (%d)",
1563 return cleanreturn(0, freelist
);
1566 else if (nkeywords
&& PyErr_Occurred())
1567 return cleanreturn(0, freelist
);
1569 current_arg
= PyTuple_GET_ITEM(args
, i
);
1572 msg
= convertitem(current_arg
, &format
, p_va
, flags
,
1573 levels
, msgbuf
, sizeof(msgbuf
), &freelist
);
1575 seterror(i
+1, msg
, levels
, fname
, custom_msg
);
1576 return cleanreturn(0, freelist
);
1582 PyErr_Format(PyExc_TypeError
, "Required argument "
1583 "'%s' (pos %d) not found",
1585 return cleanreturn(0, freelist
);
1587 /* current code reports success when all required args
1588 * fulfilled and no keyword args left, with no further
1589 * validation. XXX Maybe skip this in debug build ?
1592 return cleanreturn(1, freelist
);
1594 /* We are into optional args, skip thru to any remaining
1596 msg
= skipitem(&format
, p_va
, flags
);
1598 PyErr_Format(PyExc_RuntimeError
, "%s: '%s'", msg
,
1600 return cleanreturn(0, freelist
);
1604 if (!IS_END_OF_FORMAT(*format
)) {
1605 PyErr_Format(PyExc_RuntimeError
,
1606 "more argument specifiers than keyword list entries "
1607 "(remaining format:'%s')", format
);
1608 return cleanreturn(0, freelist
);
1611 /* make sure there are no extraneous keyword arguments */
1612 if (nkeywords
> 0) {
1613 PyObject
*key
, *value
;
1615 while (PyDict_Next(keywords
, &pos
, &key
, &value
)) {
1618 if (!PyString_Check(key
)) {
1619 PyErr_SetString(PyExc_TypeError
,
1620 "keywords must be strings");
1621 return cleanreturn(0, freelist
);
1623 ks
= PyString_AsString(key
);
1624 for (i
= 0; i
< len
; i
++) {
1625 if (!strcmp(ks
, kwlist
[i
])) {
1631 PyErr_Format(PyExc_TypeError
,
1632 "'%s' is an invalid keyword "
1633 "argument for this function",
1635 return cleanreturn(0, freelist
);
1640 return cleanreturn(1, freelist
);
1645 skipitem(const char **p_format
, va_list *p_va
, int flags
)
1647 const char *format
= *p_format
;
1653 * The individual types (second arg of va_arg) are irrelevant */
1655 case 'b': /* byte -- very short int */
1656 case 'B': /* byte as bitfield */
1657 case 'h': /* short int */
1658 case 'H': /* short int as bitfield */
1660 case 'I': /* int sized bitfield */
1661 case 'l': /* long int */
1662 case 'k': /* long int sized bitfield */
1663 #ifdef HAVE_LONG_LONG
1664 case 'L': /* PY_LONG_LONG */
1665 case 'K': /* PY_LONG_LONG sized bitfield */
1667 case 'f': /* float */
1668 case 'd': /* double */
1669 #ifndef WITHOUT_COMPLEX
1670 case 'D': /* complex double */
1672 case 'c': /* char */
1674 (void) va_arg(*p_va
, void *);
1678 case 'n': /* Py_ssize_t */
1680 (void) va_arg(*p_va
, Py_ssize_t
*);
1686 case 'e': /* string with encoding */
1688 (void) va_arg(*p_va
, const char *);
1689 if (!(*format
== 's' || *format
== 't'))
1690 /* after 'e', only 's' and 't' is allowed */
1693 /* explicit fallthrough to string cases */
1696 case 's': /* string */
1697 case 'z': /* string or None */
1698 #ifdef Py_USING_UNICODE
1699 case 'u': /* unicode string */
1701 case 't': /* buffer, read-only */
1702 case 'w': /* buffer, read-write */
1704 (void) va_arg(*p_va
, char **);
1705 if (*format
== '#') {
1706 if (flags
& FLAG_SIZE_T
)
1707 (void) va_arg(*p_va
, Py_ssize_t
*);
1709 (void) va_arg(*p_va
, int *);
1711 } else if ((c
== 's' || c
== 'z') && *format
== '*') {
1719 case 'S': /* string object */
1720 #ifdef Py_USING_UNICODE
1721 case 'U': /* unicode string object */
1724 (void) va_arg(*p_va
, PyObject
**);
1728 case 'O': /* object */
1730 if (*format
== '!') {
1732 (void) va_arg(*p_va
, PyTypeObject
*);
1733 (void) va_arg(*p_va
, PyObject
**);
1736 /* I don't know what this is for */
1737 else if (*format
== '?') {
1738 inquiry pred
= va_arg(*p_va
, inquiry
);
1741 (void) va_arg(*p_va
, PyObject
**);
1745 else if (*format
== '&') {
1746 typedef int (*converter
)(PyObject
*, void *);
1747 (void) va_arg(*p_va
, converter
);
1748 (void) va_arg(*p_va
, void *);
1752 (void) va_arg(*p_va
, PyObject
**);
1757 case '(': /* bypass tuple, not handled at all previously */
1763 if (IS_END_OF_FORMAT(*format
))
1764 return "Unmatched left paren in format "
1766 msg
= skipitem(&format
, p_va
, flags
);
1775 return "Unmatched right paren in format string";
1779 return "impossible<bad format char>";
1789 PyArg_UnpackTuple(PyObject
*args
, const char *name
, Py_ssize_t min
, Py_ssize_t max
, ...)
1795 #ifdef HAVE_STDARG_PROTOTYPES
1796 va_start(vargs
, max
);
1803 if (!PyTuple_Check(args
)) {
1804 PyErr_SetString(PyExc_SystemError
,
1805 "PyArg_UnpackTuple() argument list is not a tuple");
1808 l
= PyTuple_GET_SIZE(args
);
1813 "%s expected %s%zd arguments, got %zd",
1814 name
, (min
== max
? "" : "at least "), min
, l
);
1818 "unpacked tuple should have %s%zd elements,"
1820 (min
== max
? "" : "at least "), min
, l
);
1828 "%s expected %s%zd arguments, got %zd",
1829 name
, (min
== max
? "" : "at most "), max
, l
);
1833 "unpacked tuple should have %s%zd elements,"
1835 (min
== max
? "" : "at most "), max
, l
);
1839 for (i
= 0; i
< l
; i
++) {
1840 o
= va_arg(vargs
, PyObject
**);
1841 *o
= PyTuple_GET_ITEM(args
, i
);
1848 /* For type constructors that don't take keyword args
1850 * Sets a TypeError and returns 0 if the kwds dict is
1851 * not empty, returns 1 otherwise
1854 _PyArg_NoKeywords(const char *funcname
, PyObject
*kw
)
1858 if (!PyDict_CheckExact(kw
)) {
1859 PyErr_BadInternalCall();
1862 if (PyDict_Size(kw
) == 0)
1865 PyErr_Format(PyExc_TypeError
, "%s does not take keyword arguments",