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) {
212 Py_FatalError("excess ')' in getargs format");
231 else if (isalpha(Py_CHARMASK(c
))) {
232 if (c
!= 'e') /* skip encoded */
242 Py_FatalError(/* '(' */ "missing ')' in getargs format");
253 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
254 "%.200s%s takes no arguments",
255 fname
==NULL
? "function" : fname
,
256 fname
==NULL
? "" : "()");
257 PyErr_SetString(PyExc_TypeError
, msgbuf
);
260 else if (min
== 1 && max
== 1) {
262 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
263 "%.200s%s takes at least one argument",
264 fname
==NULL
? "function" : fname
,
265 fname
==NULL
? "" : "()");
266 PyErr_SetString(PyExc_TypeError
, msgbuf
);
269 msg
= convertitem(args
, &format
, p_va
, flags
, levels
,
270 msgbuf
, sizeof(msgbuf
), &freelist
);
272 return cleanreturn(1, freelist
);
273 seterror(levels
[0], msg
, levels
+1, fname
, message
);
274 return cleanreturn(0, freelist
);
277 PyErr_SetString(PyExc_SystemError
,
278 "old style getargs format uses new features");
283 if (!PyTuple_Check(args
)) {
284 PyErr_SetString(PyExc_SystemError
,
285 "new style getargs format but argument is not a tuple");
289 len
= PyTuple_GET_SIZE(args
);
291 if (len
< min
|| max
< len
) {
292 if (message
== NULL
) {
293 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
294 "%.150s%s takes %s %d argument%s "
296 fname
==NULL
? "function" : fname
,
297 fname
==NULL
? "" : "()",
299 : len
< min
? "at least" : "at most",
300 len
< min
? min
: max
,
301 (len
< min
? min
: max
) == 1 ? "" : "s",
302 Py_SAFE_DOWNCAST(len
, Py_ssize_t
, long));
305 PyErr_SetString(PyExc_TypeError
, message
);
309 for (i
= 0; i
< len
; i
++) {
312 msg
= convertitem(PyTuple_GET_ITEM(args
, i
), &format
, p_va
,
313 flags
, levels
, msgbuf
,
314 sizeof(msgbuf
), &freelist
);
316 seterror(i
+1, msg
, levels
, fname
, message
);
317 return cleanreturn(0, freelist
);
321 if (*format
!= '\0' && !isalpha(Py_CHARMASK(*format
)) &&
323 *format
!= '|' && *format
!= ':' && *format
!= ';') {
324 PyErr_Format(PyExc_SystemError
,
325 "bad format string: %.200s", formatsave
);
326 return cleanreturn(0, freelist
);
329 return cleanreturn(1, freelist
);
335 seterror(int iarg
, const char *msg
, int *levels
, const char *fname
,
342 if (PyErr_Occurred())
344 else if (message
== NULL
) {
346 PyOS_snprintf(p
, sizeof(buf
), "%.200s() ", fname
);
350 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
),
351 "argument %d", iarg
);
354 while (levels
[i
] > 0 && (int)(p
-buf
) < 220) {
355 PyOS_snprintf(p
, sizeof(buf
) - (buf
- p
),
356 ", item %d", levels
[i
]-1);
362 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
), "argument");
365 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
), " %.256s", msg
);
368 PyErr_SetString(PyExc_TypeError
, message
);
372 /* Convert a tuple argument.
373 On entry, *p_format points to the character _after_ the opening '('.
374 On successful exit, *p_format points to the closing ')'.
376 *p_format and *p_va are updated,
377 *levels and *msgbuf are untouched,
378 and NULL is returned.
379 If the argument is invalid:
380 *p_format is unchanged,
382 *levels is a 0-terminated list of item numbers,
383 *msgbuf contains an error message, whose format is:
384 "must be <typename1>, not <typename2>", where:
385 <typename1> is the name of the expected type, and
386 <typename2> is the name of the actual type,
387 and msgbuf is returned.
391 converttuple(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
392 int *levels
, char *msgbuf
, size_t bufsize
, int toplevel
,
397 const char *format
= *p_format
;
412 else if (c
== ':' || c
== ';' || c
== '\0')
414 else if (level
== 0 && isalpha(Py_CHARMASK(c
)))
418 if (!PySequence_Check(arg
) || PyString_Check(arg
)) {
420 PyOS_snprintf(msgbuf
, bufsize
,
421 toplevel
? "expected %d arguments, not %.50s" :
422 "must be %d-item sequence, not %.50s",
424 arg
== Py_None
? "None" : arg
->ob_type
->tp_name
);
428 if ((i
= PySequence_Size(arg
)) != n
) {
430 PyOS_snprintf(msgbuf
, bufsize
,
431 toplevel
? "expected %d arguments, not %d" :
432 "must be sequence of length %d, not %d",
438 for (i
= 0; i
< n
; i
++) {
441 item
= PySequence_GetItem(arg
, i
);
442 msg
= convertitem(item
, &format
, p_va
, flags
, levels
+1,
443 msgbuf
, bufsize
, freelist
);
444 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
457 /* Convert a single item. */
460 convertitem(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
461 int *levels
, char *msgbuf
, size_t bufsize
, PyObject
**freelist
)
464 const char *format
= *p_format
;
466 if (*format
== '(' /* ')' */) {
468 msg
= converttuple(arg
, &format
, p_va
, flags
, levels
, msgbuf
,
469 bufsize
, 0, freelist
);
474 msg
= convertsimple(arg
, &format
, p_va
, flags
,
475 msgbuf
, bufsize
, freelist
);
486 #define UNICODE_DEFAULT_ENCODING(arg) \
487 _PyUnicode_AsDefaultEncodedString(arg, NULL)
489 /* Format an error message generated by convertsimple(). */
492 converterr(const char *expected
, PyObject
*arg
, char *msgbuf
, size_t bufsize
)
494 assert(expected
!= NULL
);
496 PyOS_snprintf(msgbuf
, bufsize
,
497 "must be %.50s, not %.50s", expected
,
498 arg
== Py_None
? "None" : arg
->ob_type
->tp_name
);
502 #define CONV_UNICODE "(unicode conversion error)"
504 /* explicitly check for float arguments when integers are expected. For now
505 * signal a warning. Returns true if an exception was raised. */
507 float_argument_error(PyObject
*arg
)
509 if (PyFloat_Check(arg
) &&
510 PyErr_Warn(PyExc_DeprecationWarning
,
511 "integer argument expected, got float" ))
517 /* Convert a non-tuple argument. Return NULL if conversion went OK,
518 or a string with a message describing the failure. The message is
519 formatted as "must be <desired type>, not <actual type>".
520 When failing, an exception may or may not have been raised.
521 Don't call if a tuple is expected.
523 When you add new format codes, please don't forget poor skipitem() below.
527 convertsimple(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
528 char *msgbuf
, size_t bufsize
, PyObject
**freelist
)
531 #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
532 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
533 else q=va_arg(*p_va, int*);
534 #define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
535 #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
537 const char *format
= *p_format
;
539 #ifdef Py_USING_UNICODE
545 case 'b': { /* unsigned byte -- very short int */
546 char *p
= va_arg(*p_va
, char *);
548 if (float_argument_error(arg
))
549 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
550 ival
= PyInt_AsLong(arg
);
551 if (ival
== -1 && PyErr_Occurred())
552 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
554 PyErr_SetString(PyExc_OverflowError
,
555 "unsigned byte integer is less than minimum");
556 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
558 else if (ival
> UCHAR_MAX
) {
559 PyErr_SetString(PyExc_OverflowError
,
560 "unsigned byte integer is greater than maximum");
561 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
564 *p
= (unsigned char) ival
;
568 case 'B': {/* byte sized bitfield - both signed and unsigned
570 char *p
= va_arg(*p_va
, char *);
572 if (float_argument_error(arg
))
573 return converterr("integer<B>", arg
, msgbuf
, bufsize
);
574 ival
= PyInt_AsUnsignedLongMask(arg
);
575 if (ival
== -1 && PyErr_Occurred())
576 return converterr("integer<B>", arg
, msgbuf
, bufsize
);
578 *p
= (unsigned char) ival
;
582 case 'h': {/* signed short int */
583 short *p
= va_arg(*p_va
, short *);
585 if (float_argument_error(arg
))
586 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
587 ival
= PyInt_AsLong(arg
);
588 if (ival
== -1 && PyErr_Occurred())
589 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
590 else if (ival
< SHRT_MIN
) {
591 PyErr_SetString(PyExc_OverflowError
,
592 "signed short integer is less than minimum");
593 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
595 else if (ival
> SHRT_MAX
) {
596 PyErr_SetString(PyExc_OverflowError
,
597 "signed short integer is greater than maximum");
598 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
605 case 'H': { /* short int sized bitfield, both signed and
607 unsigned short *p
= va_arg(*p_va
, unsigned short *);
609 if (float_argument_error(arg
))
610 return converterr("integer<H>", arg
, msgbuf
, bufsize
);
611 ival
= PyInt_AsUnsignedLongMask(arg
);
612 if (ival
== -1 && PyErr_Occurred())
613 return converterr("integer<H>", arg
, msgbuf
, bufsize
);
615 *p
= (unsigned short) ival
;
619 case 'i': {/* signed int */
620 int *p
= va_arg(*p_va
, int *);
622 if (float_argument_error(arg
))
623 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
624 ival
= PyInt_AsLong(arg
);
625 if (ival
== -1 && PyErr_Occurred())
626 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
627 else if (ival
> INT_MAX
) {
628 PyErr_SetString(PyExc_OverflowError
,
629 "signed integer is greater than maximum");
630 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
632 else if (ival
< INT_MIN
) {
633 PyErr_SetString(PyExc_OverflowError
,
634 "signed integer is less than minimum");
635 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
642 case 'I': { /* int sized bitfield, both signed and
644 unsigned int *p
= va_arg(*p_va
, unsigned int *);
646 if (float_argument_error(arg
))
647 return converterr("integer<I>", arg
, msgbuf
, bufsize
);
648 ival
= (unsigned int)PyInt_AsUnsignedLongMask(arg
);
649 if (ival
== (unsigned int)-1 && PyErr_Occurred())
650 return converterr("integer<I>", arg
, msgbuf
, bufsize
);
656 case 'n': /* Py_ssize_t */
657 #if SIZEOF_SIZE_T != SIZEOF_LONG
659 Py_ssize_t
*p
= va_arg(*p_va
, Py_ssize_t
*);
661 if (float_argument_error(arg
))
662 return converterr("integer<n>", arg
, msgbuf
, bufsize
);
663 ival
= PyInt_AsSsize_t(arg
);
664 if (ival
== -1 && PyErr_Occurred())
665 return converterr("integer<n>", arg
, msgbuf
, bufsize
);
670 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
671 case 'l': {/* long int */
672 long *p
= va_arg(*p_va
, long *);
674 if (float_argument_error(arg
))
675 return converterr("integer<l>", arg
, msgbuf
, bufsize
);
676 ival
= PyInt_AsLong(arg
);
677 if (ival
== -1 && PyErr_Occurred())
678 return converterr("integer<l>", arg
, msgbuf
, bufsize
);
684 case 'k': { /* long sized bitfield */
685 unsigned long *p
= va_arg(*p_va
, unsigned long *);
687 if (PyInt_Check(arg
))
688 ival
= PyInt_AsUnsignedLongMask(arg
);
689 else if (PyLong_Check(arg
))
690 ival
= PyLong_AsUnsignedLongMask(arg
);
692 return converterr("integer<k>", arg
, msgbuf
, bufsize
);
697 #ifdef HAVE_LONG_LONG
698 case 'L': {/* PY_LONG_LONG */
699 PY_LONG_LONG
*p
= va_arg( *p_va
, PY_LONG_LONG
* );
700 PY_LONG_LONG ival
= PyLong_AsLongLong( arg
);
701 if( ival
== (PY_LONG_LONG
)-1 && PyErr_Occurred() ) {
702 return converterr("long<L>", arg
, msgbuf
, bufsize
);
709 case 'K': { /* long long sized bitfield */
710 unsigned PY_LONG_LONG
*p
= va_arg(*p_va
, unsigned PY_LONG_LONG
*);
711 unsigned PY_LONG_LONG ival
;
712 if (PyInt_Check(arg
))
713 ival
= PyInt_AsUnsignedLongMask(arg
);
714 else if (PyLong_Check(arg
))
715 ival
= PyLong_AsUnsignedLongLongMask(arg
);
717 return converterr("integer<K>", arg
, msgbuf
, bufsize
);
723 case 'f': {/* float */
724 float *p
= va_arg(*p_va
, float *);
725 double dval
= PyFloat_AsDouble(arg
);
726 if (PyErr_Occurred())
727 return converterr("float<f>", arg
, msgbuf
, bufsize
);
733 case 'd': {/* double */
734 double *p
= va_arg(*p_va
, double *);
735 double dval
= PyFloat_AsDouble(arg
);
736 if (PyErr_Occurred())
737 return converterr("float<d>", arg
, msgbuf
, bufsize
);
743 #ifndef WITHOUT_COMPLEX
744 case 'D': {/* complex double */
745 Py_complex
*p
= va_arg(*p_va
, Py_complex
*);
747 cval
= PyComplex_AsCComplex(arg
);
748 if (PyErr_Occurred())
749 return converterr("complex<D>", arg
, msgbuf
, bufsize
);
754 #endif /* WITHOUT_COMPLEX */
756 case 'c': {/* char */
757 char *p
= va_arg(*p_va
, char *);
758 if (PyString_Check(arg
) && PyString_Size(arg
) == 1)
759 *p
= PyString_AS_STRING(arg
)[0];
761 return converterr("char", arg
, msgbuf
, bufsize
);
765 case 's': {/* string */
766 if (*format
== '#') {
767 void **p
= (void **)va_arg(*p_va
, char **);
770 if (PyString_Check(arg
)) {
771 *p
= PyString_AS_STRING(arg
);
772 STORE_SIZE(PyString_GET_SIZE(arg
));
774 #ifdef Py_USING_UNICODE
775 else if (PyUnicode_Check(arg
)) {
776 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
778 return converterr(CONV_UNICODE
,
779 arg
, msgbuf
, bufsize
);
780 *p
= PyString_AS_STRING(uarg
);
781 STORE_SIZE(PyString_GET_SIZE(uarg
));
784 else { /* any buffer-like object */
786 Py_ssize_t count
= convertbuffer(arg
, p
, &buf
);
788 return converterr(buf
, arg
, msgbuf
, bufsize
);
793 char **p
= va_arg(*p_va
, char **);
795 if (PyString_Check(arg
))
796 *p
= PyString_AS_STRING(arg
);
797 #ifdef Py_USING_UNICODE
798 else if (PyUnicode_Check(arg
)) {
799 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
801 return converterr(CONV_UNICODE
,
802 arg
, msgbuf
, bufsize
);
803 *p
= PyString_AS_STRING(uarg
);
807 return converterr("string", arg
, msgbuf
, bufsize
);
808 if ((int)strlen(*p
) != PyString_Size(arg
))
809 return converterr("string without null bytes",
810 arg
, msgbuf
, bufsize
);
815 case 'z': {/* string, may be NULL (None) */
816 if (*format
== '#') { /* any buffer-like object */
817 void **p
= (void **)va_arg(*p_va
, char **);
820 if (arg
== Py_None
) {
824 else if (PyString_Check(arg
)) {
825 *p
= PyString_AS_STRING(arg
);
826 STORE_SIZE(PyString_GET_SIZE(arg
));
828 #ifdef Py_USING_UNICODE
829 else if (PyUnicode_Check(arg
)) {
830 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
832 return converterr(CONV_UNICODE
,
833 arg
, msgbuf
, bufsize
);
834 *p
= PyString_AS_STRING(uarg
);
835 STORE_SIZE(PyString_GET_SIZE(uarg
));
838 else { /* any buffer-like object */
840 Py_ssize_t count
= convertbuffer(arg
, p
, &buf
);
842 return converterr(buf
, arg
, msgbuf
, bufsize
);
847 char **p
= va_arg(*p_va
, char **);
851 else if (PyString_Check(arg
))
852 *p
= PyString_AS_STRING(arg
);
853 #ifdef Py_USING_UNICODE
854 else if (PyUnicode_Check(arg
)) {
855 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
857 return converterr(CONV_UNICODE
,
858 arg
, msgbuf
, bufsize
);
859 *p
= PyString_AS_STRING(uarg
);
863 return converterr("string or None",
864 arg
, msgbuf
, bufsize
);
865 if (*format
== '#') {
867 assert(0); /* XXX redundant with if-case */
871 *q
= PyString_Size(arg
);
874 else if (*p
!= NULL
&&
875 (int)strlen(*p
) != PyString_Size(arg
))
877 "string without null bytes or None",
878 arg
, msgbuf
, bufsize
);
883 case 'e': {/* encoded string */
885 const char *encoding
;
887 int size
, recode_strings
;
889 /* Get 'e' parameter: the encoding name */
890 encoding
= (const char *)va_arg(*p_va
, const char *);
891 #ifdef Py_USING_UNICODE
892 if (encoding
== NULL
)
893 encoding
= PyUnicode_GetDefaultEncoding();
896 /* Get output buffer parameter:
897 's' (recode all objects via Unicode) or
898 't' (only recode non-string objects)
902 else if (*format
== 't')
906 "(unknown parser marker combination)",
907 arg
, msgbuf
, bufsize
);
908 buffer
= (char **)va_arg(*p_va
, char **);
911 return converterr("(buffer is NULL)",
912 arg
, msgbuf
, bufsize
);
915 if (!recode_strings
&& PyString_Check(arg
)) {
920 #ifdef Py_USING_UNICODE
923 /* Convert object to Unicode */
924 u
= PyUnicode_FromObject(arg
);
927 "string or unicode or text buffer",
928 arg
, msgbuf
, bufsize
);
930 /* Encode object; use default error handling */
931 s
= PyUnicode_AsEncodedString(u
,
936 return converterr("(encoding failed)",
937 arg
, msgbuf
, bufsize
);
938 if (!PyString_Check(s
)) {
941 "(encoder failed to return a string)",
942 arg
, msgbuf
, bufsize
);
945 return converterr("string<e>", arg
, msgbuf
, bufsize
);
948 size
= PyString_GET_SIZE(s
);
950 /* Write output; output is guaranteed to be 0-terminated */
951 if (*format
== '#') {
952 /* Using buffer length parameter '#':
954 - if *buffer is NULL, a new buffer of the
955 needed size is allocated and the data
956 copied into it; *buffer is updated to point
957 to the new buffer; the caller is
958 responsible for PyMem_Free()ing it after
961 - if *buffer is not NULL, the data is
962 copied to *buffer; *buffer_len has to be
963 set to the size of the buffer on input;
964 buffer overflow is signalled with an error;
965 buffer has to provide enough room for the
966 encoded string plus the trailing 0-byte
968 - in both cases, *buffer_len is updated to
969 the size of the buffer /excluding/ the
976 if (q
== NULL
&& q2
== NULL
) {
979 "(buffer_len is NULL)",
980 arg
, msgbuf
, bufsize
);
982 if (*buffer
== NULL
) {
983 *buffer
= PyMem_NEW(char, size
+ 1);
984 if (*buffer
== NULL
) {
988 arg
, msgbuf
, bufsize
);
990 if(addcleanup(*buffer
, freelist
)) {
994 arg
, msgbuf
, bufsize
);
997 if (size
+ 1 > BUFFER_LEN
) {
1000 "(buffer overflow)",
1001 arg
, msgbuf
, bufsize
);
1005 PyString_AS_STRING(s
),
1009 /* Using a 0-terminated buffer:
1011 - the encoded string has to be 0-terminated
1012 for this variant to work; if it is not, an
1015 - a new buffer of the needed size is
1016 allocated and the data copied into it;
1017 *buffer is updated to point to the new
1018 buffer; the caller is responsible for
1019 PyMem_Free()ing it after usage
1022 if ((int)strlen(PyString_AS_STRING(s
)) != size
) {
1025 "(encoded string without NULL bytes)",
1026 arg
, msgbuf
, bufsize
);
1028 *buffer
= PyMem_NEW(char, size
+ 1);
1029 if (*buffer
== NULL
) {
1031 return converterr("(memory error)",
1032 arg
, msgbuf
, bufsize
);
1034 if(addcleanup(*buffer
, freelist
)) {
1036 return converterr("(cleanup problem)",
1037 arg
, msgbuf
, bufsize
);
1040 PyString_AS_STRING(s
),
1047 #ifdef Py_USING_UNICODE
1048 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1049 if (*format
== '#') { /* any buffer-like object */
1050 void **p
= (void **)va_arg(*p_va
, char **);
1052 if (PyUnicode_Check(arg
)) {
1053 *p
= PyUnicode_AS_UNICODE(arg
);
1054 STORE_SIZE(PyUnicode_GET_SIZE(arg
));
1057 return converterr("cannot convert raw buffers",
1058 arg
, msgbuf
, bufsize
);
1062 Py_UNICODE
**p
= va_arg(*p_va
, Py_UNICODE
**);
1063 if (PyUnicode_Check(arg
))
1064 *p
= PyUnicode_AS_UNICODE(arg
);
1066 return converterr("unicode", arg
, msgbuf
, bufsize
);
1072 case 'S': { /* string object */
1073 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1074 if (PyString_Check(arg
))
1077 return converterr("string", arg
, msgbuf
, bufsize
);
1081 #ifdef Py_USING_UNICODE
1082 case 'U': { /* Unicode object */
1083 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1084 if (PyUnicode_Check(arg
))
1087 return converterr("unicode", arg
, msgbuf
, bufsize
);
1092 case 'O': { /* object */
1095 if (*format
== '!') {
1096 type
= va_arg(*p_va
, PyTypeObject
*);
1097 p
= va_arg(*p_va
, PyObject
**);
1099 if (PyType_IsSubtype(arg
->ob_type
, type
))
1102 return converterr(type
->tp_name
, arg
, msgbuf
, bufsize
);
1105 else if (*format
== '?') {
1106 inquiry pred
= va_arg(*p_va
, inquiry
);
1107 p
= va_arg(*p_va
, PyObject
**);
1112 return converterr("(unspecified)",
1113 arg
, msgbuf
, bufsize
);
1116 else if (*format
== '&') {
1117 typedef int (*converter
)(PyObject
*, void *);
1118 converter convert
= va_arg(*p_va
, converter
);
1119 void *addr
= va_arg(*p_va
, void *);
1121 if (! (*convert
)(arg
, addr
))
1122 return converterr("(unspecified)",
1123 arg
, msgbuf
, bufsize
);
1126 p
= va_arg(*p_va
, PyObject
**);
1133 case 'w': { /* memory buffer, read-write access */
1134 void **p
= va_arg(*p_va
, void **);
1135 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1139 pb
->bf_getwritebuffer
== NULL
||
1140 pb
->bf_getsegcount
== NULL
)
1141 return converterr("read-write buffer", arg
, msgbuf
, bufsize
);
1142 if ((*pb
->bf_getsegcount
)(arg
, NULL
) != 1)
1143 return converterr("single-segment read-write buffer",
1144 arg
, msgbuf
, bufsize
);
1145 if ((count
= pb
->bf_getwritebuffer(arg
, 0, p
)) < 0)
1146 return converterr("(unspecified)", arg
, msgbuf
, bufsize
);
1147 if (*format
== '#') {
1155 case 't': { /* 8-bit character buffer, read-only access */
1156 char **p
= va_arg(*p_va
, char **);
1157 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1160 if (*format
++ != '#')
1162 "invalid use of 't' format character",
1163 arg
, msgbuf
, bufsize
);
1164 if (!PyType_HasFeature(arg
->ob_type
,
1165 Py_TPFLAGS_HAVE_GETCHARBUFFER
) ||
1166 pb
== NULL
|| pb
->bf_getcharbuffer
== NULL
||
1167 pb
->bf_getsegcount
== NULL
)
1169 "string or read-only character buffer",
1170 arg
, msgbuf
, bufsize
);
1172 if (pb
->bf_getsegcount(arg
, NULL
) != 1)
1174 "string or single-segment read-only buffer",
1175 arg
, msgbuf
, bufsize
);
1177 count
= pb
->bf_getcharbuffer(arg
, 0, p
);
1179 return converterr("(unspecified)", arg
, msgbuf
, bufsize
);
1188 return converterr("impossible<bad format char>", arg
, msgbuf
, bufsize
);
1197 convertbuffer(PyObject
*arg
, void **p
, char **errmsg
)
1199 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1202 pb
->bf_getreadbuffer
== NULL
||
1203 pb
->bf_getsegcount
== NULL
) {
1204 *errmsg
= "string or read-only buffer";
1207 if ((*pb
->bf_getsegcount
)(arg
, NULL
) != 1) {
1208 *errmsg
= "string or single-segment read-only buffer";
1211 if ((count
= (*pb
->bf_getreadbuffer
)(arg
, 0, p
)) < 0) {
1212 *errmsg
= "(unspecified)";
1217 /* Support for keyword arguments donated by
1218 Geoff Philbrick <philbric@delphi.hks.com> */
1220 /* Return false (0) for error, else true. */
1222 PyArg_ParseTupleAndKeywords(PyObject
*args
,
1230 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1231 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1235 PyErr_BadInternalCall();
1239 va_start(va
, kwlist
);
1240 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &va
, 0);
1246 _PyArg_ParseTupleAndKeywords_SizeT(PyObject
*args
,
1254 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1255 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1259 PyErr_BadInternalCall();
1263 va_start(va
, kwlist
);
1264 retval
= vgetargskeywords(args
, keywords
, format
,
1265 kwlist
, &va
, FLAG_SIZE_T
);
1272 PyArg_VaParseTupleAndKeywords(PyObject
*args
,
1275 char **kwlist
, va_list va
)
1280 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1281 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1285 PyErr_BadInternalCall();
1289 #ifdef VA_LIST_IS_ARRAY
1290 memcpy(lva
, va
, sizeof(va_list));
1299 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &lva
, 0);
1304 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject
*args
,
1307 char **kwlist
, va_list va
)
1312 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1313 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1317 PyErr_BadInternalCall();
1321 #ifdef VA_LIST_IS_ARRAY
1322 memcpy(lva
, va
, sizeof(va_list));
1331 retval
= vgetargskeywords(args
, keywords
, format
,
1332 kwlist
, &lva
, FLAG_SIZE_T
);
1338 vgetargskeywords(PyObject
*args
, PyObject
*keywords
, const char *format
,
1339 char **kwlist
, va_list *p_va
, int flags
)
1343 const char *fname
, *message
;
1345 const char *formatsave
;
1346 int i
, len
, nargs
, nkeywords
;
1349 PyObject
*freelist
= NULL
;
1351 assert(args
!= NULL
&& PyTuple_Check(args
));
1352 assert(keywords
== NULL
|| PyDict_Check(keywords
));
1353 assert(format
!= NULL
);
1354 assert(kwlist
!= NULL
);
1355 assert(p_va
!= NULL
);
1357 /* Search the format:
1358 message <- error msg, if any (else NULL).
1359 fname <- routine name, if any (else NULL).
1360 min <- # of required arguments, or -1 if all are required.
1361 max <- most arguments (required + optional).
1362 Check that kwlist has a non-NULL entry for each arg.
1363 Raise error if a tuple arg spec is found.
1365 fname
= message
= NULL
;
1366 formatsave
= format
;
1370 while ((i
= *format
++) != '\0') {
1371 if (isalpha(Py_CHARMASK(i
)) && i
!= 'e') {
1374 PyErr_SetString(PyExc_RuntimeError
,
1375 "more argument specifiers than "
1376 "keyword list entries");
1383 else if (i
== ':') {
1387 else if (i
== ';') {
1391 else if (i
== '(') {
1392 PyErr_SetString(PyExc_RuntimeError
,
1393 "tuple found in format when using keyword "
1398 format
= formatsave
;
1400 PyErr_SetString(PyExc_RuntimeError
,
1401 "more keyword list entries than "
1402 "argument specifiers");
1406 /* All arguments are required. */
1410 nargs
= PyTuple_GET_SIZE(args
);
1411 nkeywords
= keywords
== NULL
? 0 : PyDict_Size(keywords
);
1413 /* make sure there are no duplicate values for an argument;
1414 its not clear when to use the term "keyword argument vs.
1415 keyword parameter in messages */
1416 if (nkeywords
> 0) {
1417 for (i
= 0; i
< nargs
; i
++) {
1418 const char *thiskw
= kwlist
[i
];
1421 if (PyDict_GetItemString(keywords
, thiskw
)) {
1422 PyErr_Format(PyExc_TypeError
,
1423 "keyword parameter '%s' was given "
1424 "by position and by name",
1428 else if (PyErr_Occurred())
1433 /* required arguments missing from args can be supplied by keyword
1434 arguments; set len to the number of positional arguments, and,
1435 if that's less than the minimum required, add in the number of
1436 required arguments that are supplied by keywords */
1438 if (nkeywords
> 0 && nargs
< min
) {
1439 for (i
= nargs
; i
< min
; i
++) {
1440 if (PyDict_GetItemString(keywords
, kwlist
[i
]))
1442 else if (PyErr_Occurred())
1447 /* make sure we got an acceptable number of arguments; the message
1448 is a little confusing with keywords since keyword arguments
1449 which are supplied, but don't match the required arguments
1450 are not included in the "%d given" part of the message
1451 XXX and this isn't a bug!? */
1452 if (len
< min
|| max
< len
) {
1453 if (message
== NULL
) {
1454 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
1455 "%.200s%s takes %s %d argument%s "
1457 fname
==NULL
? "function" : fname
,
1458 fname
==NULL
? "" : "()",
1459 min
==max
? "exactly"
1460 : len
< min
? "at least" : "at most",
1461 len
< min
? min
: max
,
1462 (len
< min
? min
: max
) == 1 ? "" : "s",
1466 PyErr_SetString(PyExc_TypeError
, message
);
1470 /* convert the positional arguments */
1471 for (i
= 0; i
< nargs
; i
++) {
1474 msg
= convertitem(PyTuple_GET_ITEM(args
, i
), &format
, p_va
,
1475 flags
, levels
, msgbuf
, sizeof(msgbuf
),
1478 seterror(i
+1, msg
, levels
, fname
, message
);
1479 return cleanreturn(0, freelist
);
1483 /* handle no keyword parameters in call */
1485 return cleanreturn(1, freelist
);
1487 /* convert the keyword arguments; this uses the format
1488 string where it was left after processing args */
1489 for (i
= nargs
; i
< max
; i
++) {
1493 item
= PyDict_GetItemString(keywords
, kwlist
[i
]);
1496 msg
= convertitem(item
, &format
, p_va
, flags
, levels
,
1497 msgbuf
, sizeof(msgbuf
), &freelist
);
1500 seterror(i
+1, msg
, levels
, fname
, message
);
1501 return cleanreturn(0, freelist
);
1507 else if (PyErr_Occurred())
1508 return cleanreturn(0, freelist
);
1510 msg
= skipitem(&format
, p_va
, flags
);
1512 seterror(i
+1, msg
, levels
, fname
, message
);
1513 return cleanreturn(0, freelist
);
1518 /* make sure there are no extraneous keyword arguments */
1519 if (nkeywords
> 0) {
1520 PyObject
*key
, *value
;
1522 while (PyDict_Next(keywords
, &pos
, &key
, &value
)) {
1525 if (!PyString_Check(key
)) {
1526 PyErr_SetString(PyExc_TypeError
,
1527 "keywords must be strings");
1528 return cleanreturn(0, freelist
);
1530 ks
= PyString_AsString(key
);
1531 for (i
= 0; i
< max
; i
++) {
1532 if (!strcmp(ks
, kwlist
[i
])) {
1538 PyErr_Format(PyExc_TypeError
,
1539 "'%s' is an invalid keyword "
1540 "argument for this function",
1542 return cleanreturn(0, freelist
);
1547 return cleanreturn(1, freelist
);
1552 skipitem(const char **p_format
, va_list *p_va
, int flags
)
1554 const char *format
= *p_format
;
1560 * The individual types (second arg of va_arg) are irrelevant */
1562 case 'b': /* byte -- very short int */
1563 case 'B': /* byte as bitfield */
1564 case 'h': /* short int */
1565 case 'H': /* short int as bitfield */
1567 case 'I': /* int sized bitfield */
1568 case 'l': /* long int */
1569 case 'k': /* long int sized bitfield */
1570 #ifdef HAVE_LONG_LONG
1571 case 'L': /* PY_LONG_LONG */
1572 case 'K': /* PY_LONG_LONG sized bitfield */
1574 case 'f': /* float */
1575 case 'd': /* double */
1576 #ifndef WITHOUT_COMPLEX
1577 case 'D': /* complex double */
1579 case 'c': /* char */
1581 (void) va_arg(*p_va
, void *);
1585 case 'n': /* Py_ssize_t */
1587 (void) va_arg(*p_va
, Py_ssize_t
*);
1593 case 'e': /* string with encoding */
1595 (void) va_arg(*p_va
, const char *);
1596 if (!(*format
== 's' || *format
== 't'))
1597 /* after 'e', only 's' and 't' is allowed */
1600 /* explicit fallthrough to string cases */
1603 case 's': /* string */
1604 case 'z': /* string or None */
1605 #ifdef Py_USING_UNICODE
1606 case 'u': /* unicode string */
1608 case 't': /* buffer, read-only */
1609 case 'w': /* buffer, read-write */
1611 (void) va_arg(*p_va
, char **);
1612 if (*format
== '#') {
1613 if (flags
& FLAG_SIZE_T
)
1614 (void) va_arg(*p_va
, Py_ssize_t
*);
1616 (void) va_arg(*p_va
, int *);
1624 case 'S': /* string object */
1625 #ifdef Py_USING_UNICODE
1626 case 'U': /* unicode string object */
1629 (void) va_arg(*p_va
, PyObject
**);
1633 case 'O': /* object */
1635 if (*format
== '!') {
1637 (void) va_arg(*p_va
, PyTypeObject
*);
1638 (void) va_arg(*p_va
, PyObject
**);
1641 /* I don't know what this is for */
1642 else if (*format
== '?') {
1643 inquiry pred
= va_arg(*p_va
, inquiry
);
1646 (void) va_arg(*p_va
, PyObject
**);
1650 else if (*format
== '&') {
1651 typedef int (*converter
)(PyObject
*, void *);
1652 (void) va_arg(*p_va
, converter
);
1653 (void) va_arg(*p_va
, void *);
1657 (void) va_arg(*p_va
, PyObject
**);
1664 return "impossible<bad format char>";
1668 /* The "(...)" format code for tuples is not handled here because
1669 * it is not allowed with keyword args. */
1677 PyArg_UnpackTuple(PyObject
*args
, const char *name
, Py_ssize_t min
, Py_ssize_t max
, ...)
1683 #ifdef HAVE_STDARG_PROTOTYPES
1684 va_start(vargs
, max
);
1691 if (!PyTuple_Check(args
)) {
1692 PyErr_SetString(PyExc_SystemError
,
1693 "PyArg_UnpackTuple() argument list is not a tuple");
1696 l
= PyTuple_GET_SIZE(args
);
1701 "%s expected %s%zd arguments, got %zd",
1702 name
, (min
== max
? "" : "at least "), min
, l
);
1706 "unpacked tuple should have %s%zd elements,"
1708 (min
== max
? "" : "at least "), min
, l
);
1716 "%s expected %s%zd arguments, got %zd",
1717 name
, (min
== max
? "" : "at most "), max
, l
);
1721 "unpacked tuple should have %s%zd elements,"
1723 (min
== max
? "" : "at most "), max
, l
);
1727 for (i
= 0; i
< l
; i
++) {
1728 o
= va_arg(vargs
, PyObject
**);
1729 *o
= PyTuple_GET_ITEM(args
, i
);
1736 /* For type constructors that don't take keyword args
1738 * Sets a TypeError and returns 0 if the kwds dict is
1739 * not emtpy, returns 1 otherwise
1742 _PyArg_NoKeywords(const char *funcname
, PyObject
*kw
)
1746 if (!PyDict_CheckExact(kw
)) {
1747 PyErr_BadInternalCall();
1750 if (PyDict_Size(kw
) == 0)
1753 PyErr_Format(PyExc_TypeError
, "%s does not take keyword arguments",