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 */
142 #define GETARGS_CAPSULE_NAME_CLEANUP_PTR "getargs.cleanup_ptr"
143 #define GETARGS_CAPSULE_NAME_CLEANUP_BUFFER "getargs.cleanup_buffer"
144 #define GETARGS_CAPSULE_NAME_CLEANUP_CONVERT "getargs.cleanup_convert"
147 cleanup_ptr(PyObject
*self
)
149 void *ptr
= PyCapsule_GetPointer(self
, GETARGS_CAPSULE_NAME_CLEANUP_PTR
);
156 cleanup_buffer(PyObject
*self
)
158 Py_buffer
*ptr
= (Py_buffer
*)PyCapsule_GetPointer(self
, GETARGS_CAPSULE_NAME_CLEANUP_BUFFER
);
160 PyBuffer_Release(ptr
);
165 addcleanup(void *ptr
, PyObject
**freelist
, PyCapsule_Destructor destr
)
171 *freelist
= PyList_New(0);
178 if (destr
== cleanup_ptr
) {
179 name
= GETARGS_CAPSULE_NAME_CLEANUP_PTR
;
180 } else if (destr
== cleanup_buffer
) {
181 name
= GETARGS_CAPSULE_NAME_CLEANUP_BUFFER
;
185 cobj
= PyCapsule_New(ptr
, name
, destr
);
190 if (PyList_Append(*freelist
, cobj
)) {
199 cleanup_convert(PyObject
*self
)
201 typedef int (*destr_t
)(PyObject
*, void *);
202 destr_t destr
= (destr_t
)PyCapsule_GetContext(self
);
203 void *ptr
= PyCapsule_GetPointer(self
,
204 GETARGS_CAPSULE_NAME_CLEANUP_CONVERT
);
210 addcleanup_convert(void *ptr
, PyObject
**freelist
, int (*destr
)(PyObject
*,void*))
214 *freelist
= PyList_New(0);
220 cobj
= PyCapsule_New(ptr
, GETARGS_CAPSULE_NAME_CLEANUP_CONVERT
,
226 if (PyCapsule_SetContext(cobj
, destr
) == -1) {
227 /* This really should not happen. */
228 Py_FatalError("capsule refused setting of context.");
230 if (PyList_Append(*freelist
, cobj
)) {
231 Py_DECREF(cobj
); /* This will also call destr. */
239 cleanreturn(int retval
, PyObject
*freelist
)
241 if (freelist
&& retval
!= 0) {
242 /* We were successful, reset the destructors so that they
244 Py_ssize_t len
= PyList_GET_SIZE(freelist
), i
;
245 for (i
= 0; i
< len
; i
++)
246 PyCapsule_SetDestructor(PyList_GET_ITEM(freelist
, i
), NULL
);
248 Py_XDECREF(freelist
);
254 vgetargs1(PyObject
*args
, const char *format
, va_list *p_va
, int flags
)
258 const char *fname
= NULL
;
259 const char *message
= NULL
;
264 const char *formatsave
= format
;
267 PyObject
*freelist
= NULL
;
268 int compat
= flags
& FLAG_COMPAT
;
270 assert(compat
|| (args
!= (PyObject
*)NULL
));
271 flags
= flags
& ~FLAG_COMPAT
;
273 while (endfmt
== 0) {
281 Py_FatalError("too many tuple nesting levels "
282 "in argument format string");
286 Py_FatalError("excess ')' in getargs format");
305 else if (isalpha(Py_CHARMASK(c
))) {
306 if (c
!= 'e') /* skip encoded */
316 Py_FatalError(/* '(' */ "missing ')' in getargs format");
327 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
328 "%.200s%s takes no arguments",
329 fname
==NULL
? "function" : fname
,
330 fname
==NULL
? "" : "()");
331 PyErr_SetString(PyExc_TypeError
, msgbuf
);
334 else if (min
== 1 && max
== 1) {
336 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
337 "%.200s%s takes at least one argument",
338 fname
==NULL
? "function" : fname
,
339 fname
==NULL
? "" : "()");
340 PyErr_SetString(PyExc_TypeError
, msgbuf
);
343 msg
= convertitem(args
, &format
, p_va
, flags
, levels
,
344 msgbuf
, sizeof(msgbuf
), &freelist
);
346 return cleanreturn(1, freelist
);
347 seterror(levels
[0], msg
, levels
+1, fname
, message
);
348 return cleanreturn(0, freelist
);
351 PyErr_SetString(PyExc_SystemError
,
352 "old style getargs format uses new features");
357 if (!PyTuple_Check(args
)) {
358 PyErr_SetString(PyExc_SystemError
,
359 "new style getargs format but argument is not a tuple");
363 len
= PyTuple_GET_SIZE(args
);
365 if (len
< min
|| max
< len
) {
366 if (message
== NULL
) {
367 PyOS_snprintf(msgbuf
, sizeof(msgbuf
),
368 "%.150s%s takes %s %d argument%s "
370 fname
==NULL
? "function" : fname
,
371 fname
==NULL
? "" : "()",
373 : len
< min
? "at least" : "at most",
374 len
< min
? min
: max
,
375 (len
< min
? min
: max
) == 1 ? "" : "s",
376 Py_SAFE_DOWNCAST(len
, Py_ssize_t
, long));
379 PyErr_SetString(PyExc_TypeError
, message
);
383 for (i
= 0; i
< len
; i
++) {
386 msg
= convertitem(PyTuple_GET_ITEM(args
, i
), &format
, p_va
,
387 flags
, levels
, msgbuf
,
388 sizeof(msgbuf
), &freelist
);
390 seterror(i
+1, msg
, levels
, fname
, message
);
391 return cleanreturn(0, freelist
);
395 if (*format
!= '\0' && !isalpha(Py_CHARMASK(*format
)) &&
397 *format
!= '|' && *format
!= ':' && *format
!= ';') {
398 PyErr_Format(PyExc_SystemError
,
399 "bad format string: %.200s", formatsave
);
400 return cleanreturn(0, freelist
);
403 return cleanreturn(1, freelist
);
409 seterror(int iarg
, const char *msg
, int *levels
, const char *fname
,
416 if (PyErr_Occurred())
418 else if (message
== NULL
) {
420 PyOS_snprintf(p
, sizeof(buf
), "%.200s() ", fname
);
424 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
),
425 "argument %d", iarg
);
428 while (levels
[i
] > 0 && i
< 32 && (int)(p
-buf
) < 220) {
429 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
),
430 ", item %d", levels
[i
]-1);
436 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
), "argument");
439 PyOS_snprintf(p
, sizeof(buf
) - (p
- buf
), " %.256s", msg
);
442 PyErr_SetString(PyExc_TypeError
, message
);
446 /* Convert a tuple argument.
447 On entry, *p_format points to the character _after_ the opening '('.
448 On successful exit, *p_format points to the closing ')'.
450 *p_format and *p_va are updated,
451 *levels and *msgbuf are untouched,
452 and NULL is returned.
453 If the argument is invalid:
454 *p_format is unchanged,
456 *levels is a 0-terminated list of item numbers,
457 *msgbuf contains an error message, whose format is:
458 "must be <typename1>, not <typename2>", where:
459 <typename1> is the name of the expected type, and
460 <typename2> is the name of the actual type,
461 and msgbuf is returned.
465 converttuple(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
466 int *levels
, char *msgbuf
, size_t bufsize
, int toplevel
,
471 const char *format
= *p_format
;
486 else if (c
== ':' || c
== ';' || c
== '\0')
488 else if (level
== 0 && isalpha(Py_CHARMASK(c
)))
492 if (!PySequence_Check(arg
) || PyBytes_Check(arg
)) {
494 PyOS_snprintf(msgbuf
, bufsize
,
495 toplevel
? "expected %d arguments, not %.50s" :
496 "must be %d-item sequence, not %.50s",
498 arg
== Py_None
? "None" : arg
->ob_type
->tp_name
);
502 if ((i
= PySequence_Size(arg
)) != n
) {
504 PyOS_snprintf(msgbuf
, bufsize
,
505 toplevel
? "expected %d arguments, not %d" :
506 "must be sequence of length %d, not %d",
512 for (i
= 0; i
< n
; i
++) {
515 item
= PySequence_GetItem(arg
, i
);
520 strncpy(msgbuf
, "is not retrievable", bufsize
);
523 msg
= convertitem(item
, &format
, p_va
, flags
, levels
+1,
524 msgbuf
, bufsize
, freelist
);
525 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
538 /* Convert a single item. */
541 convertitem(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
542 int *levels
, char *msgbuf
, size_t bufsize
, PyObject
**freelist
)
545 const char *format
= *p_format
;
547 if (*format
== '(' /* ')' */) {
549 msg
= converttuple(arg
, &format
, p_va
, flags
, levels
, msgbuf
,
550 bufsize
, 0, freelist
);
555 msg
= convertsimple(arg
, &format
, p_va
, flags
,
556 msgbuf
, bufsize
, freelist
);
567 #define UNICODE_DEFAULT_ENCODING(arg) \
568 _PyUnicode_AsDefaultEncodedString(arg, NULL)
570 /* Format an error message generated by convertsimple(). */
573 converterr(const char *expected
, PyObject
*arg
, char *msgbuf
, size_t bufsize
)
575 assert(expected
!= NULL
);
577 PyOS_snprintf(msgbuf
, bufsize
,
578 "must be %.50s, not %.50s", expected
,
579 arg
== Py_None
? "None" : arg
->ob_type
->tp_name
);
583 #define CONV_UNICODE "(unicode conversion error)"
585 /* Explicitly check for float arguments when integers are expected.
586 Return 1 for error, 0 if ok. */
588 float_argument_error(PyObject
*arg
)
590 if (PyFloat_Check(arg
)) {
591 PyErr_SetString(PyExc_TypeError
,
592 "integer argument expected, got float" );
599 /* Convert a non-tuple argument. Return NULL if conversion went OK,
600 or a string with a message describing the failure. The message is
601 formatted as "must be <desired type>, not <actual type>".
602 When failing, an exception may or may not have been raised.
603 Don't call if a tuple is expected.
605 When you add new format codes, please don't forget poor skipitem() below.
609 convertsimple(PyObject
*arg
, const char **p_format
, va_list *p_va
, int flags
,
610 char *msgbuf
, size_t bufsize
, PyObject
**freelist
)
613 #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
614 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
615 else q=va_arg(*p_va, int*);
616 #define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
617 #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
619 const char *format
= *p_format
;
625 case 'b': { /* unsigned byte -- very short int */
626 char *p
= va_arg(*p_va
, char *);
628 if (float_argument_error(arg
))
629 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
630 ival
= PyLong_AsLong(arg
);
631 if (ival
== -1 && PyErr_Occurred())
632 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
634 PyErr_SetString(PyExc_OverflowError
,
635 "unsigned byte integer is less than minimum");
636 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
638 else if (ival
> UCHAR_MAX
) {
639 PyErr_SetString(PyExc_OverflowError
,
640 "unsigned byte integer is greater than maximum");
641 return converterr("integer<b>", arg
, msgbuf
, bufsize
);
644 *p
= (unsigned char) ival
;
648 case 'B': {/* byte sized bitfield - both signed and unsigned
650 char *p
= va_arg(*p_va
, char *);
652 if (float_argument_error(arg
))
653 return converterr("integer<B>", arg
, msgbuf
, bufsize
);
654 ival
= PyLong_AsUnsignedLongMask(arg
);
655 if (ival
== -1 && PyErr_Occurred())
656 return converterr("integer<B>", arg
, msgbuf
, bufsize
);
658 *p
= (unsigned char) ival
;
662 case 'h': {/* signed short int */
663 short *p
= va_arg(*p_va
, short *);
665 if (float_argument_error(arg
))
666 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
667 ival
= PyLong_AsLong(arg
);
668 if (ival
== -1 && PyErr_Occurred())
669 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
670 else if (ival
< SHRT_MIN
) {
671 PyErr_SetString(PyExc_OverflowError
,
672 "signed short integer is less than minimum");
673 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
675 else if (ival
> SHRT_MAX
) {
676 PyErr_SetString(PyExc_OverflowError
,
677 "signed short integer is greater than maximum");
678 return converterr("integer<h>", arg
, msgbuf
, bufsize
);
685 case 'H': { /* short int sized bitfield, both signed and
687 unsigned short *p
= va_arg(*p_va
, unsigned short *);
689 if (float_argument_error(arg
))
690 return converterr("integer<H>", arg
, msgbuf
, bufsize
);
691 ival
= PyLong_AsUnsignedLongMask(arg
);
692 if (ival
== -1 && PyErr_Occurred())
693 return converterr("integer<H>", arg
, msgbuf
, bufsize
);
695 *p
= (unsigned short) ival
;
699 case 'i': {/* signed int */
700 int *p
= va_arg(*p_va
, int *);
702 if (float_argument_error(arg
))
703 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
704 ival
= PyLong_AsLong(arg
);
705 if (ival
== -1 && PyErr_Occurred())
706 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
707 else if (ival
> INT_MAX
) {
708 PyErr_SetString(PyExc_OverflowError
,
709 "signed integer is greater than maximum");
710 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
712 else if (ival
< INT_MIN
) {
713 PyErr_SetString(PyExc_OverflowError
,
714 "signed integer is less than minimum");
715 return converterr("integer<i>", arg
, msgbuf
, bufsize
);
722 case 'I': { /* int sized bitfield, both signed and
724 unsigned int *p
= va_arg(*p_va
, unsigned int *);
726 if (float_argument_error(arg
))
727 return converterr("integer<I>", arg
, msgbuf
, bufsize
);
728 ival
= (unsigned int)PyLong_AsUnsignedLongMask(arg
);
729 if (ival
== (unsigned int)-1 && PyErr_Occurred())
730 return converterr("integer<I>", arg
, msgbuf
, bufsize
);
736 case 'n': /* Py_ssize_t */
739 Py_ssize_t
*p
= va_arg(*p_va
, Py_ssize_t
*);
740 Py_ssize_t ival
= -1;
741 if (float_argument_error(arg
))
742 return converterr("integer<n>", arg
, msgbuf
, bufsize
);
743 iobj
= PyNumber_Index(arg
);
745 ival
= PyLong_AsSsize_t(iobj
);
748 if (ival
== -1 && PyErr_Occurred())
749 return converterr("integer<n>", arg
, msgbuf
, bufsize
);
753 case 'l': {/* long int */
754 long *p
= va_arg(*p_va
, long *);
756 if (float_argument_error(arg
))
757 return converterr("integer<l>", arg
, msgbuf
, bufsize
);
758 ival
= PyLong_AsLong(arg
);
759 if (ival
== -1 && PyErr_Occurred())
760 return converterr("integer<l>", arg
, msgbuf
, bufsize
);
766 case 'k': { /* long sized bitfield */
767 unsigned long *p
= va_arg(*p_va
, unsigned long *);
769 if (PyLong_Check(arg
))
770 ival
= PyLong_AsUnsignedLongMask(arg
);
772 return converterr("integer<k>", arg
, msgbuf
, bufsize
);
777 #ifdef HAVE_LONG_LONG
778 case 'L': {/* PY_LONG_LONG */
779 PY_LONG_LONG
*p
= va_arg( *p_va
, PY_LONG_LONG
* );
780 PY_LONG_LONG ival
= PyLong_AsLongLong( arg
);
781 if (ival
== (PY_LONG_LONG
)-1 && PyErr_Occurred() ) {
782 return converterr("long<L>", arg
, msgbuf
, bufsize
);
789 case 'K': { /* long long sized bitfield */
790 unsigned PY_LONG_LONG
*p
= va_arg(*p_va
, unsigned PY_LONG_LONG
*);
791 unsigned PY_LONG_LONG ival
;
792 if (PyLong_Check(arg
))
793 ival
= PyLong_AsUnsignedLongLongMask(arg
);
795 return converterr("integer<K>", arg
, msgbuf
, bufsize
);
801 case 'f': {/* float */
802 float *p
= va_arg(*p_va
, float *);
803 double dval
= PyFloat_AsDouble(arg
);
804 if (PyErr_Occurred())
805 return converterr("float<f>", arg
, msgbuf
, bufsize
);
811 case 'd': {/* double */
812 double *p
= va_arg(*p_va
, double *);
813 double dval
= PyFloat_AsDouble(arg
);
814 if (PyErr_Occurred())
815 return converterr("float<d>", arg
, msgbuf
, bufsize
);
821 #ifndef WITHOUT_COMPLEX
822 case 'D': {/* complex double */
823 Py_complex
*p
= va_arg(*p_va
, Py_complex
*);
825 cval
= PyComplex_AsCComplex(arg
);
826 if (PyErr_Occurred())
827 return converterr("complex<D>", arg
, msgbuf
, bufsize
);
832 #endif /* WITHOUT_COMPLEX */
834 case 'c': {/* char */
835 char *p
= va_arg(*p_va
, char *);
836 if (PyBytes_Check(arg
) && PyBytes_Size(arg
) == 1)
837 *p
= PyBytes_AS_STRING(arg
)[0];
839 return converterr("a byte string of length 1", arg
, msgbuf
, bufsize
);
843 case 'C': {/* unicode char */
844 int *p
= va_arg(*p_va
, int *);
845 if (PyUnicode_Check(arg
) &&
846 PyUnicode_GET_SIZE(arg
) == 1)
847 *p
= PyUnicode_AS_UNICODE(arg
)[0];
849 return converterr("a unicode character", arg
, msgbuf
, bufsize
);
853 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w', 't' codes all
854 need to be cleaned up! */
856 case 's': {/* text string */
857 if (*format
== '*') {
858 Py_buffer
*p
= (Py_buffer
*)va_arg(*p_va
, Py_buffer
*);
860 if (PyUnicode_Check(arg
)) {
861 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
863 return converterr(CONV_UNICODE
,
864 arg
, msgbuf
, bufsize
);
865 PyBuffer_FillInfo(p
, arg
,
866 PyBytes_AS_STRING(uarg
), PyBytes_GET_SIZE(uarg
),
869 else { /* any buffer-like object */
871 if (getbuffer(arg
, p
, &buf
) < 0)
872 return converterr(buf
, arg
, msgbuf
, bufsize
);
874 if (addcleanup(p
, freelist
, cleanup_buffer
)) {
877 arg
, msgbuf
, bufsize
);
880 } else if (*format
== '#') {
881 void **p
= (void **)va_arg(*p_va
, char **);
884 if (PyUnicode_Check(arg
)) {
885 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
887 return converterr(CONV_UNICODE
,
888 arg
, msgbuf
, bufsize
);
889 *p
= PyBytes_AS_STRING(uarg
);
890 STORE_SIZE(PyBytes_GET_SIZE(uarg
));
892 else { /* any buffer-like object */
895 Py_ssize_t count
= convertbuffer(arg
, p
, &buf
);
897 return converterr(buf
, arg
, msgbuf
, bufsize
);
902 char **p
= va_arg(*p_va
, char **);
904 if (PyUnicode_Check(arg
)) {
905 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
907 return converterr(CONV_UNICODE
,
908 arg
, msgbuf
, bufsize
);
909 *p
= PyBytes_AS_STRING(uarg
);
912 return converterr("string", arg
, msgbuf
, bufsize
);
913 if ((Py_ssize_t
) strlen(*p
) != PyBytes_GET_SIZE(uarg
))
914 return converterr("string without null bytes",
915 arg
, msgbuf
, bufsize
);
920 case 'y': {/* any buffer-like object, but not PyUnicode */
921 void **p
= (void **)va_arg(*p_va
, char **);
924 if (*format
== '*') {
925 if (getbuffer(arg
, (Py_buffer
*)p
, &buf
) < 0)
926 return converterr(buf
, arg
, msgbuf
, bufsize
);
928 if (addcleanup(p
, freelist
, cleanup_buffer
)) {
931 arg
, msgbuf
, bufsize
);
935 count
= convertbuffer(arg
, p
, &buf
);
937 return converterr(buf
, arg
, msgbuf
, bufsize
);
938 else if (*format
== '#') {
946 case 'z': {/* like 's' or 's#', but None is okay, stored as NULL */
947 if (*format
== '*') {
948 Py_buffer
*p
= (Py_buffer
*)va_arg(*p_va
, Py_buffer
*);
951 PyBuffer_FillInfo(p
, NULL
, NULL
, 0, 1, 0);
952 else if (PyUnicode_Check(arg
)) {
953 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
955 return converterr(CONV_UNICODE
,
956 arg
, msgbuf
, bufsize
);
957 PyBuffer_FillInfo(p
, arg
,
958 PyBytes_AS_STRING(uarg
), PyBytes_GET_SIZE(uarg
),
961 else { /* any buffer-like object */
963 if (getbuffer(arg
, p
, &buf
) < 0)
964 return converterr(buf
, arg
, msgbuf
, bufsize
);
966 if (addcleanup(p
, freelist
, cleanup_buffer
)) {
969 arg
, msgbuf
, bufsize
);
972 } else if (*format
== '#') { /* any buffer-like object */
973 void **p
= (void **)va_arg(*p_va
, char **);
976 if (arg
== Py_None
) {
980 else if (PyUnicode_Check(arg
)) {
981 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
983 return converterr(CONV_UNICODE
,
984 arg
, msgbuf
, bufsize
);
985 *p
= PyBytes_AS_STRING(uarg
);
986 STORE_SIZE(PyBytes_GET_SIZE(uarg
));
988 else { /* any buffer-like object */
991 Py_ssize_t count
= convertbuffer(arg
, p
, &buf
);
993 return converterr(buf
, arg
, msgbuf
, bufsize
);
998 char **p
= va_arg(*p_va
, char **);
1003 else if (PyBytes_Check(arg
)) {
1004 /* Enable null byte check below */
1006 *p
= PyBytes_AS_STRING(arg
);
1008 else if (PyUnicode_Check(arg
)) {
1009 uarg
= UNICODE_DEFAULT_ENCODING(arg
);
1011 return converterr(CONV_UNICODE
,
1012 arg
, msgbuf
, bufsize
);
1013 *p
= PyBytes_AS_STRING(uarg
);
1016 return converterr("string or None",
1017 arg
, msgbuf
, bufsize
);
1018 if (*format
== '#') {
1020 assert(0); /* XXX redundant with if-case */
1021 if (arg
== Py_None
) {
1025 STORE_SIZE(PyBytes_Size(arg
));
1029 else if (*p
!= NULL
&& uarg
!= NULL
&&
1030 (Py_ssize_t
) strlen(*p
) != PyBytes_GET_SIZE(uarg
))
1032 "string without null bytes or None",
1033 arg
, msgbuf
, bufsize
);
1038 case 'Z': {/* unicode, may be NULL (None) */
1039 if (*format
== '#') { /* any buffer-like object */
1040 Py_UNICODE
**p
= va_arg(*p_va
, Py_UNICODE
**);
1043 if (arg
== Py_None
) {
1047 else if (PyUnicode_Check(arg
)) {
1048 *p
= PyUnicode_AS_UNICODE(arg
);
1049 STORE_SIZE(PyUnicode_GET_SIZE(arg
));
1053 Py_UNICODE
**p
= va_arg(*p_va
, Py_UNICODE
**);
1057 else if (PyUnicode_Check(arg
))
1058 *p
= PyUnicode_AS_UNICODE(arg
);
1060 return converterr("string or None",
1061 arg
, msgbuf
, bufsize
);
1066 case 'e': {/* encoded string */
1068 const char *encoding
;
1074 /* Get 'e' parameter: the encoding name */
1075 encoding
= (const char *)va_arg(*p_va
, const char *);
1076 if (encoding
== NULL
)
1077 encoding
= PyUnicode_GetDefaultEncoding();
1079 /* Get output buffer parameter:
1080 's' (recode all objects via Unicode) or
1081 't' (only recode non-string objects)
1085 else if (*format
== 't')
1089 "(unknown parser marker combination)",
1090 arg
, msgbuf
, bufsize
);
1091 buffer
= (char **)va_arg(*p_va
, char **);
1094 return converterr("(buffer is NULL)",
1095 arg
, msgbuf
, bufsize
);
1098 if (!recode_strings
&&
1099 (PyBytes_Check(arg
) || PyByteArray_Check(arg
))) {
1102 if (PyObject_AsCharBuffer(s
, &ptr
, &size
) < 0)
1103 return converterr("(AsCharBuffer failed)",
1104 arg
, msgbuf
, bufsize
);
1109 /* Convert object to Unicode */
1110 u
= PyUnicode_FromObject(arg
);
1113 "string or unicode or text buffer",
1114 arg
, msgbuf
, bufsize
);
1116 /* Encode object; use default error handling */
1117 s
= PyUnicode_AsEncodedString(u
,
1122 return converterr("(encoding failed)",
1123 arg
, msgbuf
, bufsize
);
1124 if (!PyBytes_Check(s
)) {
1127 "(encoder failed to return bytes)",
1128 arg
, msgbuf
, bufsize
);
1130 size
= PyBytes_GET_SIZE(s
);
1131 ptr
= PyBytes_AS_STRING(s
);
1136 /* Write output; output is guaranteed to be 0-terminated */
1137 if (*format
== '#') {
1138 /* Using buffer length parameter '#':
1140 - if *buffer is NULL, a new buffer of the
1141 needed size is allocated and the data
1142 copied into it; *buffer is updated to point
1143 to the new buffer; the caller is
1144 responsible for PyMem_Free()ing it after
1147 - if *buffer is not NULL, the data is
1148 copied to *buffer; *buffer_len has to be
1149 set to the size of the buffer on input;
1150 buffer overflow is signalled with an error;
1151 buffer has to provide enough room for the
1152 encoded string plus the trailing 0-byte
1154 - in both cases, *buffer_len is updated to
1155 the size of the buffer /excluding/ the
1162 if (q
== NULL
&& q2
== NULL
) {
1165 "(buffer_len is NULL)",
1166 arg
, msgbuf
, bufsize
);
1168 if (*buffer
== NULL
) {
1169 *buffer
= PyMem_NEW(char, size
+ 1);
1170 if (*buffer
== NULL
) {
1174 arg
, msgbuf
, bufsize
);
1176 if (addcleanup(*buffer
, freelist
, cleanup_ptr
)) {
1179 "(cleanup problem)",
1180 arg
, msgbuf
, bufsize
);
1183 if (size
+ 1 > BUFFER_LEN
) {
1186 "(buffer overflow)",
1187 arg
, msgbuf
, bufsize
);
1190 memcpy(*buffer
, ptr
, size
+1);
1193 /* Using a 0-terminated buffer:
1195 - the encoded string has to be 0-terminated
1196 for this variant to work; if it is not, an
1199 - a new buffer of the needed size is
1200 allocated and the data copied into it;
1201 *buffer is updated to point to the new
1202 buffer; the caller is responsible for
1203 PyMem_Free()ing it after usage
1206 if ((Py_ssize_t
)strlen(ptr
) != size
) {
1209 "encoded string without NULL bytes",
1210 arg
, msgbuf
, bufsize
);
1212 *buffer
= PyMem_NEW(char, size
+ 1);
1213 if (*buffer
== NULL
) {
1215 return converterr("(memory error)",
1216 arg
, msgbuf
, bufsize
);
1218 if (addcleanup(*buffer
, freelist
, cleanup_ptr
)) {
1220 return converterr("(cleanup problem)",
1221 arg
, msgbuf
, bufsize
);
1223 memcpy(*buffer
, ptr
, size
+1);
1229 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1230 Py_UNICODE
**p
= va_arg(*p_va
, Py_UNICODE
**);
1231 if (!PyUnicode_Check(arg
))
1232 return converterr("str", arg
, msgbuf
, bufsize
);
1233 *p
= PyUnicode_AS_UNICODE(arg
);
1234 if (*format
== '#') { /* store pointer and size */
1236 STORE_SIZE(PyUnicode_GET_SIZE(arg
));
1242 case 'S': { /* PyBytes object */
1243 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1244 if (PyBytes_Check(arg
))
1247 return converterr("bytes", arg
, msgbuf
, bufsize
);
1251 case 'Y': { /* PyByteArray object */
1252 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1253 if (PyByteArray_Check(arg
))
1256 return converterr("buffer", arg
, msgbuf
, bufsize
);
1260 case 'U': { /* PyUnicode object */
1261 PyObject
**p
= va_arg(*p_va
, PyObject
**);
1262 if (PyUnicode_Check(arg
))
1265 return converterr("str", arg
, msgbuf
, bufsize
);
1269 case 'O': { /* object */
1272 if (*format
== '!') {
1273 type
= va_arg(*p_va
, PyTypeObject
*);
1274 p
= va_arg(*p_va
, PyObject
**);
1276 if (PyType_IsSubtype(arg
->ob_type
, type
))
1279 return converterr(type
->tp_name
, arg
, msgbuf
, bufsize
);
1282 else if (*format
== '?') {
1283 inquiry pred
= va_arg(*p_va
, inquiry
);
1284 p
= va_arg(*p_va
, PyObject
**);
1289 return converterr("(unspecified)",
1290 arg
, msgbuf
, bufsize
);
1293 else if (*format
== '&') {
1294 typedef int (*converter
)(PyObject
*, void *);
1295 converter convert
= va_arg(*p_va
, converter
);
1296 void *addr
= va_arg(*p_va
, void *);
1299 if (! (res
= (*convert
)(arg
, addr
)))
1300 return converterr("(unspecified)",
1301 arg
, msgbuf
, bufsize
);
1302 if (res
== Py_CLEANUP_SUPPORTED
&&
1303 addcleanup_convert(addr
, freelist
, convert
) == -1)
1304 return converterr("(cleanup problem)",
1305 arg
, msgbuf
, bufsize
);
1308 p
= va_arg(*p_va
, PyObject
**);
1315 case 'w': { /* memory buffer, read-write access */
1316 void **p
= va_arg(*p_va
, void **);
1317 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1322 if (pb
&& pb
->bf_releasebuffer
&& *format
!= '*')
1323 /* Buffer must be released, yet caller does not use
1324 the Py_buffer protocol. */
1325 return converterr("pinned buffer", arg
, msgbuf
, bufsize
);
1328 if (pb
&& pb
->bf_getbuffer
&& *format
== '*') {
1329 /* Caller is interested in Py_buffer, and the object
1330 supports it directly. */
1332 if (PyObject_GetBuffer(arg
, (Py_buffer
*)p
, PyBUF_WRITABLE
) < 0) {
1334 return converterr("read-write buffer", arg
, msgbuf
, bufsize
);
1336 if (addcleanup(p
, freelist
, cleanup_buffer
)) {
1338 "(cleanup problem)",
1339 arg
, msgbuf
, bufsize
);
1341 if (!PyBuffer_IsContiguous((Py_buffer
*)p
, 'C'))
1342 return converterr("contiguous buffer", arg
, msgbuf
, bufsize
);
1346 /* Here we have processed w*, only w and w# remain. */
1348 pb
->bf_getbuffer
== NULL
||
1349 ((temp
= PyObject_GetBuffer(arg
, &view
,
1350 PyBUF_SIMPLE
)) != 0) ||
1351 view
.readonly
== 1) {
1353 PyBuffer_Release(&view
);
1355 return converterr("single-segment read-write buffer",
1356 arg
, msgbuf
, bufsize
);
1359 if ((count
= view
.len
) < 0)
1360 return converterr("(unspecified)", arg
, msgbuf
, bufsize
);
1362 if (*format
== '#') {
1370 /*TEO: This can be eliminated --- here only for backward
1372 case 't': { /* 8-bit character buffer, read-only access */
1373 char **p
= va_arg(*p_va
, char **);
1374 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1378 if (*format
++ != '#')
1380 "invalid use of 't' format character",
1381 arg
, msgbuf
, bufsize
);
1382 if (pb
== NULL
|| pb
->bf_getbuffer
== NULL
)
1384 "bytes or read-only character buffer",
1385 arg
, msgbuf
, bufsize
);
1387 if (PyObject_GetBuffer(arg
, &view
, PyBUF_SIMPLE
) != 0)
1388 return converterr("string or single-segment read-only buffer",
1389 arg
, msgbuf
, bufsize
);
1393 if (pb
->bf_releasebuffer
)
1395 "string or pinned buffer",
1396 arg
, msgbuf
, bufsize
);
1398 PyBuffer_Release(&view
);
1401 return converterr("(unspecified)", arg
, msgbuf
, bufsize
);
1410 return converterr("impossible<bad format char>", arg
, msgbuf
, bufsize
);
1419 convertbuffer(PyObject
*arg
, void **p
, char **errmsg
)
1421 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1428 pb
->bf_getbuffer
== NULL
||
1429 pb
->bf_releasebuffer
!= NULL
) {
1430 *errmsg
= "bytes or read-only buffer";
1434 if (PyObject_GetBuffer(arg
, &view
, PyBUF_SIMPLE
) != 0) {
1435 *errmsg
= "bytes or single-segment read-only buffer";
1440 PyBuffer_Release(&view
);
1444 /* XXX for 3.x, getbuffer and convertbuffer can probably
1447 getbuffer(PyObject
*arg
, Py_buffer
*view
, char **errmsg
)
1451 PyBufferProcs
*pb
= arg
->ob_type
->tp_as_buffer
;
1453 *errmsg
= "bytes or buffer";
1456 if (pb
->bf_getbuffer
) {
1457 if (PyObject_GetBuffer(arg
, view
, 0) < 0) {
1458 *errmsg
= "convertible to a buffer";
1461 if (!PyBuffer_IsContiguous(view
, 'C')) {
1462 *errmsg
= "contiguous buffer";
1468 count
= convertbuffer(arg
, &buf
, errmsg
);
1470 *errmsg
= "convertible to a buffer";
1473 PyBuffer_FillInfo(view
, NULL
, buf
, count
, 1, 0);
1477 /* Support for keyword arguments donated by
1478 Geoff Philbrick <philbric@delphi.hks.com> */
1480 /* Return false (0) for error, else true. */
1482 PyArg_ParseTupleAndKeywords(PyObject
*args
,
1490 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1491 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1495 PyErr_BadInternalCall();
1499 va_start(va
, kwlist
);
1500 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &va
, 0);
1506 _PyArg_ParseTupleAndKeywords_SizeT(PyObject
*args
,
1514 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1515 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1519 PyErr_BadInternalCall();
1523 va_start(va
, kwlist
);
1524 retval
= vgetargskeywords(args
, keywords
, format
,
1525 kwlist
, &va
, FLAG_SIZE_T
);
1532 PyArg_VaParseTupleAndKeywords(PyObject
*args
,
1535 char **kwlist
, va_list va
)
1540 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1541 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1545 PyErr_BadInternalCall();
1549 #ifdef VA_LIST_IS_ARRAY
1550 memcpy(lva
, va
, sizeof(va_list));
1559 retval
= vgetargskeywords(args
, keywords
, format
, kwlist
, &lva
, 0);
1564 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject
*args
,
1567 char **kwlist
, va_list va
)
1572 if ((args
== NULL
|| !PyTuple_Check(args
)) ||
1573 (keywords
!= NULL
&& !PyDict_Check(keywords
)) ||
1577 PyErr_BadInternalCall();
1581 #ifdef VA_LIST_IS_ARRAY
1582 memcpy(lva
, va
, sizeof(va_list));
1591 retval
= vgetargskeywords(args
, keywords
, format
,
1592 kwlist
, &lva
, FLAG_SIZE_T
);
1596 #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1599 vgetargskeywords(PyObject
*args
, PyObject
*keywords
, const char *format
,
1600 char **kwlist
, va_list *p_va
, int flags
)
1604 const char *fname
, *msg
, *custom_msg
, *keyword
;
1606 int i
, len
, nargs
, nkeywords
;
1607 PyObject
*freelist
= NULL
, *current_arg
;
1609 assert(args
!= NULL
&& PyTuple_Check(args
));
1610 assert(keywords
== NULL
|| PyDict_Check(keywords
));
1611 assert(format
!= NULL
);
1612 assert(kwlist
!= NULL
);
1613 assert(p_va
!= NULL
);
1615 /* grab the function name or custom error msg first (mutually exclusive) */
1616 fname
= strchr(format
, ':');
1622 custom_msg
= strchr(format
,';');
1627 /* scan kwlist and get greatest possible nbr of args */
1628 for (len
=0; kwlist
[len
]; len
++)
1631 nargs
= PyTuple_GET_SIZE(args
);
1632 nkeywords
= (keywords
== NULL
) ? 0 : PyDict_Size(keywords
);
1633 if (nargs
+ nkeywords
> len
) {
1634 PyErr_Format(PyExc_TypeError
, "%s%s takes at most %d "
1635 "argument%s (%d given)",
1636 (fname
== NULL
) ? "function" : fname
,
1637 (fname
== NULL
) ? "" : "()",
1639 (len
== 1) ? "" : "s",
1644 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1645 for (i
= 0; i
< len
; i
++) {
1646 keyword
= kwlist
[i
];
1647 if (*format
== '|') {
1651 if (IS_END_OF_FORMAT(*format
)) {
1652 PyErr_Format(PyExc_RuntimeError
,
1653 "More keyword list entries (%d) than "
1654 "format specifiers (%d)", len
, i
);
1655 return cleanreturn(0, freelist
);
1659 current_arg
= PyDict_GetItemString(keywords
, keyword
);
1664 /* arg present in tuple and in dict */
1665 PyErr_Format(PyExc_TypeError
,
1666 "Argument given by name ('%s') "
1667 "and position (%d)",
1669 return cleanreturn(0, freelist
);
1672 else if (nkeywords
&& PyErr_Occurred())
1673 return cleanreturn(0, freelist
);
1675 current_arg
= PyTuple_GET_ITEM(args
, i
);
1678 msg
= convertitem(current_arg
, &format
, p_va
, flags
,
1679 levels
, msgbuf
, sizeof(msgbuf
), &freelist
);
1681 seterror(i
+1, msg
, levels
, fname
, custom_msg
);
1682 return cleanreturn(0, freelist
);
1688 PyErr_Format(PyExc_TypeError
, "Required argument "
1689 "'%s' (pos %d) not found",
1691 return cleanreturn(0, freelist
);
1693 /* current code reports success when all required args
1694 * fulfilled and no keyword args left, with no further
1695 * validation. XXX Maybe skip this in debug build ?
1698 return cleanreturn(1, freelist
);
1700 /* We are into optional args, skip thru to any remaining
1702 msg
= skipitem(&format
, p_va
, flags
);
1704 PyErr_Format(PyExc_RuntimeError
, "%s: '%s'", msg
,
1706 return cleanreturn(0, freelist
);
1710 if (!IS_END_OF_FORMAT(*format
) && *format
!= '|') {
1711 PyErr_Format(PyExc_RuntimeError
,
1712 "more argument specifiers than keyword list entries "
1713 "(remaining format:'%s')", format
);
1714 return cleanreturn(0, freelist
);
1717 /* make sure there are no extraneous keyword arguments */
1718 if (nkeywords
> 0) {
1719 PyObject
*key
, *value
;
1721 while (PyDict_Next(keywords
, &pos
, &key
, &value
)) {
1724 if (!PyUnicode_Check(key
)) {
1725 PyErr_SetString(PyExc_TypeError
,
1726 "keywords must be strings");
1727 return cleanreturn(0, freelist
);
1729 ks
= _PyUnicode_AsString(key
);
1730 for (i
= 0; i
< len
; i
++) {
1731 if (!strcmp(ks
, kwlist
[i
])) {
1737 PyErr_Format(PyExc_TypeError
,
1738 "'%s' is an invalid keyword "
1739 "argument for this function",
1741 return cleanreturn(0, freelist
);
1746 return cleanreturn(1, freelist
);
1751 skipitem(const char **p_format
, va_list *p_va
, int flags
)
1753 const char *format
= *p_format
;
1759 * The individual types (second arg of va_arg) are irrelevant */
1761 case 'b': /* byte -- very short int */
1762 case 'B': /* byte as bitfield */
1763 case 'h': /* short int */
1764 case 'H': /* short int as bitfield */
1766 case 'I': /* int sized bitfield */
1767 case 'l': /* long int */
1768 case 'k': /* long int sized bitfield */
1769 #ifdef HAVE_LONG_LONG
1770 case 'L': /* PY_LONG_LONG */
1771 case 'K': /* PY_LONG_LONG sized bitfield */
1773 case 'f': /* float */
1774 case 'd': /* double */
1775 #ifndef WITHOUT_COMPLEX
1776 case 'D': /* complex double */
1778 case 'c': /* char */
1780 (void) va_arg(*p_va
, void *);
1784 case 'n': /* Py_ssize_t */
1786 (void) va_arg(*p_va
, Py_ssize_t
*);
1792 case 'e': /* string with encoding */
1794 (void) va_arg(*p_va
, const char *);
1795 if (!(*format
== 's' || *format
== 't'))
1796 /* after 'e', only 's' and 't' is allowed */
1799 /* explicit fallthrough to string cases */
1802 case 's': /* string */
1803 case 'z': /* string or None */
1804 case 'y': /* bytes */
1805 case 'u': /* unicode string */
1806 case 't': /* buffer, read-only */
1807 case 'w': /* buffer, read-write */
1809 (void) va_arg(*p_va
, char **);
1810 if (*format
== '#') {
1811 if (flags
& FLAG_SIZE_T
)
1812 (void) va_arg(*p_va
, Py_ssize_t
*);
1814 (void) va_arg(*p_va
, int *);
1816 } else if ((c
== 's' || c
== 'z' || c
== 'y') && *format
== '*') {
1824 case 'S': /* string object */
1825 case 'Y': /* string object */
1826 case 'U': /* unicode string object */
1828 (void) va_arg(*p_va
, PyObject
**);
1832 case 'O': /* object */
1834 if (*format
== '!') {
1836 (void) va_arg(*p_va
, PyTypeObject
*);
1837 (void) va_arg(*p_va
, PyObject
**);
1840 /* I don't know what this is for */
1841 else if (*format
== '?') {
1842 inquiry pred
= va_arg(*p_va
, inquiry
);
1845 (void) va_arg(*p_va
, PyObject
**);
1849 else if (*format
== '&') {
1850 typedef int (*converter
)(PyObject
*, void *);
1851 (void) va_arg(*p_va
, converter
);
1852 (void) va_arg(*p_va
, void *);
1856 (void) va_arg(*p_va
, PyObject
**);
1861 case '(': /* bypass tuple, not handled at all previously */
1867 if (IS_END_OF_FORMAT(*format
))
1868 return "Unmatched left paren in format "
1870 msg
= skipitem(&format
, p_va
, flags
);
1879 return "Unmatched right paren in format string";
1883 return "impossible<bad format char>";
1893 PyArg_UnpackTuple(PyObject
*args
, const char *name
, Py_ssize_t min
, Py_ssize_t max
, ...)
1899 #ifdef HAVE_STDARG_PROTOTYPES
1900 va_start(vargs
, max
);
1907 if (!PyTuple_Check(args
)) {
1908 PyErr_SetString(PyExc_SystemError
,
1909 "PyArg_UnpackTuple() argument list is not a tuple");
1912 l
= PyTuple_GET_SIZE(args
);
1917 "%s expected %s%zd arguments, got %zd",
1918 name
, (min
== max
? "" : "at least "), min
, l
);
1922 "unpacked tuple should have %s%zd elements,"
1924 (min
== max
? "" : "at least "), min
, l
);
1932 "%s expected %s%zd arguments, got %zd",
1933 name
, (min
== max
? "" : "at most "), max
, l
);
1937 "unpacked tuple should have %s%zd elements,"
1939 (min
== max
? "" : "at most "), max
, l
);
1943 for (i
= 0; i
< l
; i
++) {
1944 o
= va_arg(vargs
, PyObject
**);
1945 *o
= PyTuple_GET_ITEM(args
, i
);
1952 /* For type constructors that don't take keyword args
1954 * Sets a TypeError and returns 0 if the kwds dict is
1955 * not empty, returns 1 otherwise
1958 _PyArg_NoKeywords(const char *funcname
, PyObject
*kw
)
1962 if (!PyDict_CheckExact(kw
)) {
1963 PyErr_BadInternalCall();
1966 if (PyDict_Size(kw
) == 0)
1969 PyErr_Format(PyExc_TypeError
, "%s does not take keyword arguments",