update peek documentation to implementation
[python/dscho.git] / Python / getargs.c
bloba5dc3602b2b7ce487363f4c5aa407ce834b9610e
2 /* New getargs implementation */
4 #include "Python.h"
6 #include <ctype.h>
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
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);
31 #endif
33 #define FLAG_COMPAT 1
34 #define FLAG_SIZE_T 2
37 /* Forward */
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 *,
45 size_t, PyObject **);
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);
53 int
54 PyArg_Parse(PyObject *args, const char *format, ...)
56 int retval;
57 va_list va;
59 va_start(va, format);
60 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
61 va_end(va);
62 return retval;
65 int
66 _PyArg_Parse_SizeT(PyObject *args, char *format, ...)
68 int retval;
69 va_list va;
71 va_start(va, format);
72 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
73 va_end(va);
74 return retval;
78 int
79 PyArg_ParseTuple(PyObject *args, const char *format, ...)
81 int retval;
82 va_list va;
84 va_start(va, format);
85 retval = vgetargs1(args, format, &va, 0);
86 va_end(va);
87 return retval;
90 int
91 _PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
93 int retval;
94 va_list va;
96 va_start(va, format);
97 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
98 va_end(va);
99 return retval;
104 PyArg_VaParse(PyObject *args, const char *format, va_list va)
106 va_list lva;
108 #ifdef VA_LIST_IS_ARRAY
109 memcpy(lva, va, sizeof(va_list));
110 #else
111 #ifdef __va_copy
112 __va_copy(lva, va);
113 #else
114 lva = va;
115 #endif
116 #endif
118 return vgetargs1(args, format, &lva, 0);
122 _PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
124 va_list lva;
126 #ifdef VA_LIST_IS_ARRAY
127 memcpy(lva, va, sizeof(va_list));
128 #else
129 #ifdef __va_copy
130 __va_copy(lva, va);
131 #else
132 lva = va;
133 #endif
134 #endif
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"
146 static void
147 cleanup_ptr(PyObject *self)
149 void *ptr = PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_PTR);
150 if (ptr) {
151 PyMem_FREE(ptr);
155 static void
156 cleanup_buffer(PyObject *self)
158 Py_buffer *ptr = (Py_buffer *)PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_BUFFER);
159 if (ptr) {
160 PyBuffer_Release(ptr);
164 static int
165 addcleanup(void *ptr, PyObject **freelist, PyCapsule_Destructor destr)
167 PyObject *cobj;
168 const char *name;
170 if (!*freelist) {
171 *freelist = PyList_New(0);
172 if (!*freelist) {
173 destr(ptr);
174 return -1;
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;
182 } else {
183 return -1;
185 cobj = PyCapsule_New(ptr, name, destr);
186 if (!cobj) {
187 destr(ptr);
188 return -1;
190 if (PyList_Append(*freelist, cobj)) {
191 Py_DECREF(cobj);
192 return -1;
194 Py_DECREF(cobj);
195 return 0;
198 static void
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);
205 if (ptr && destr)
206 destr(NULL, ptr);
209 static int
210 addcleanup_convert(void *ptr, PyObject **freelist, int (*destr)(PyObject*,void*))
212 PyObject *cobj;
213 if (!*freelist) {
214 *freelist = PyList_New(0);
215 if (!*freelist) {
216 destr(NULL, ptr);
217 return -1;
220 cobj = PyCapsule_New(ptr, GETARGS_CAPSULE_NAME_CLEANUP_CONVERT,
221 cleanup_convert);
222 if (!cobj) {
223 destr(NULL, ptr);
224 return -1;
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. */
232 return -1;
234 Py_DECREF(cobj);
235 return 0;
238 static int
239 cleanreturn(int retval, PyObject *freelist)
241 if (freelist && retval != 0) {
242 /* We were successful, reset the destructors so that they
243 don't get called. */
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);
249 return retval;
253 static int
254 vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
256 char msgbuf[256];
257 int levels[32];
258 const char *fname = NULL;
259 const char *message = NULL;
260 int min = -1;
261 int max = 0;
262 int level = 0;
263 int endfmt = 0;
264 const char *formatsave = format;
265 Py_ssize_t i, len;
266 char *msg;
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) {
274 int c = *format++;
275 switch (c) {
276 case '(':
277 if (level == 0)
278 max++;
279 level++;
280 if (level >= 30)
281 Py_FatalError("too many tuple nesting levels "
282 "in argument format string");
283 break;
284 case ')':
285 if (level == 0)
286 Py_FatalError("excess ')' in getargs format");
287 else
288 level--;
289 break;
290 case '\0':
291 endfmt = 1;
292 break;
293 case ':':
294 fname = format;
295 endfmt = 1;
296 break;
297 case ';':
298 message = format;
299 endfmt = 1;
300 break;
301 default:
302 if (level == 0) {
303 if (c == 'O')
304 max++;
305 else if (isalpha(Py_CHARMASK(c))) {
306 if (c != 'e') /* skip encoded */
307 max++;
308 } else if (c == '|')
309 min = max;
311 break;
315 if (level != 0)
316 Py_FatalError(/* '(' */ "missing ')' in getargs format");
318 if (min < 0)
319 min = max;
321 format = formatsave;
323 if (compat) {
324 if (max == 0) {
325 if (args == NULL)
326 return 1;
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);
332 return 0;
334 else if (min == 1 && max == 1) {
335 if (args == NULL) {
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);
341 return 0;
343 msg = convertitem(args, &format, p_va, flags, levels,
344 msgbuf, sizeof(msgbuf), &freelist);
345 if (msg == NULL)
346 return cleanreturn(1, freelist);
347 seterror(levels[0], msg, levels+1, fname, message);
348 return cleanreturn(0, freelist);
350 else {
351 PyErr_SetString(PyExc_SystemError,
352 "old style getargs format uses new features");
353 return 0;
357 if (!PyTuple_Check(args)) {
358 PyErr_SetString(PyExc_SystemError,
359 "new style getargs format but argument is not a tuple");
360 return 0;
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 "
369 "(%ld given)",
370 fname==NULL ? "function" : fname,
371 fname==NULL ? "" : "()",
372 min==max ? "exactly"
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));
377 message = msgbuf;
379 PyErr_SetString(PyExc_TypeError, message);
380 return 0;
383 for (i = 0; i < len; i++) {
384 if (*format == '|')
385 format++;
386 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
387 flags, levels, msgbuf,
388 sizeof(msgbuf), &freelist);
389 if (msg) {
390 seterror(i+1, msg, levels, fname, message);
391 return cleanreturn(0, freelist);
395 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
396 *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);
408 static void
409 seterror(int iarg, const char *msg, int *levels, const char *fname,
410 const char *message)
412 char buf[512];
413 int i;
414 char *p = buf;
416 if (PyErr_Occurred())
417 return;
418 else if (message == NULL) {
419 if (fname != NULL) {
420 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
421 p += strlen(p);
423 if (iarg != 0) {
424 PyOS_snprintf(p, sizeof(buf) - (p - buf),
425 "argument %d", iarg);
426 i = 0;
427 p += strlen(p);
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);
431 p += strlen(p);
432 i++;
435 else {
436 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
437 p += strlen(p);
439 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
440 message = buf;
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 ')'.
449 If successful:
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,
455 *p_va is undefined,
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.
464 static char *
465 converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
466 int *levels, char *msgbuf, size_t bufsize, int toplevel,
467 PyObject **freelist)
469 int level = 0;
470 int n = 0;
471 const char *format = *p_format;
472 int i;
474 for (;;) {
475 int c = *format++;
476 if (c == '(') {
477 if (level == 0)
478 n++;
479 level++;
481 else if (c == ')') {
482 if (level == 0)
483 break;
484 level--;
486 else if (c == ':' || c == ';' || c == '\0')
487 break;
488 else if (level == 0 && isalpha(Py_CHARMASK(c)))
489 n++;
492 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
493 levels[0] = 0;
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);
499 return msgbuf;
502 if ((i = PySequence_Size(arg)) != n) {
503 levels[0] = 0;
504 PyOS_snprintf(msgbuf, bufsize,
505 toplevel ? "expected %d arguments, not %d" :
506 "must be sequence of length %d, not %d",
507 n, i);
508 return msgbuf;
511 format = *p_format;
512 for (i = 0; i < n; i++) {
513 char *msg;
514 PyObject *item;
515 item = PySequence_GetItem(arg, i);
516 if (item == NULL) {
517 PyErr_Clear();
518 levels[0] = i+1;
519 levels[1] = 0;
520 strncpy(msgbuf, "is not retrievable", bufsize);
521 return msgbuf;
523 msg = convertitem(item, &format, p_va, flags, levels+1,
524 msgbuf, bufsize, freelist);
525 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
526 Py_XDECREF(item);
527 if (msg != NULL) {
528 levels[0] = i+1;
529 return msg;
533 *p_format = format;
534 return NULL;
538 /* Convert a single item. */
540 static char *
541 convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
542 int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
544 char *msg;
545 const char *format = *p_format;
547 if (*format == '(' /* ')' */) {
548 format++;
549 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
550 bufsize, 0, freelist);
551 if (msg == NULL)
552 format++;
554 else {
555 msg = convertsimple(arg, &format, p_va, flags,
556 msgbuf, bufsize, freelist);
557 if (msg != NULL)
558 levels[0] = 0;
560 if (msg == NULL)
561 *p_format = format;
562 return msg;
567 #define UNICODE_DEFAULT_ENCODING(arg) \
568 _PyUnicode_AsDefaultEncodedString(arg, NULL)
570 /* Format an error message generated by convertsimple(). */
572 static char *
573 converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
575 assert(expected != NULL);
576 assert(arg != NULL);
577 PyOS_snprintf(msgbuf, bufsize,
578 "must be %.50s, not %.50s", expected,
579 arg == Py_None ? "None" : arg->ob_type->tp_name);
580 return msgbuf;
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. */
587 static int
588 float_argument_error(PyObject *arg)
590 if (PyFloat_Check(arg)) {
591 PyErr_SetString(PyExc_TypeError,
592 "integer argument expected, got float" );
593 return 1;
595 else
596 return 0;
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.
608 static char *
609 convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
610 char *msgbuf, size_t bufsize, PyObject **freelist)
612 /* For # codes */
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;
620 char c = *format++;
621 PyObject *uarg;
623 switch (c) {
625 case 'b': { /* unsigned byte -- very short int */
626 char *p = va_arg(*p_va, char *);
627 long ival;
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);
633 else if (ival < 0) {
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);
643 else
644 *p = (unsigned char) ival;
645 break;
648 case 'B': {/* byte sized bitfield - both signed and unsigned
649 values allowed */
650 char *p = va_arg(*p_va, char *);
651 long ival;
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);
657 else
658 *p = (unsigned char) ival;
659 break;
662 case 'h': {/* signed short int */
663 short *p = va_arg(*p_va, short *);
664 long ival;
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);
680 else
681 *p = (short) ival;
682 break;
685 case 'H': { /* short int sized bitfield, both signed and
686 unsigned allowed */
687 unsigned short *p = va_arg(*p_va, unsigned short *);
688 long ival;
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);
694 else
695 *p = (unsigned short) ival;
696 break;
699 case 'i': {/* signed int */
700 int *p = va_arg(*p_va, int *);
701 long ival;
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);
717 else
718 *p = ival;
719 break;
722 case 'I': { /* int sized bitfield, both signed and
723 unsigned allowed */
724 unsigned int *p = va_arg(*p_va, unsigned int *);
725 unsigned int ival;
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);
731 else
732 *p = ival;
733 break;
736 case 'n': /* Py_ssize_t */
738 PyObject *iobj;
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);
744 if (iobj != NULL) {
745 ival = PyLong_AsSsize_t(iobj);
746 Py_DECREF(iobj);
748 if (ival == -1 && PyErr_Occurred())
749 return converterr("integer<n>", arg, msgbuf, bufsize);
750 *p = ival;
751 break;
753 case 'l': {/* long int */
754 long *p = va_arg(*p_va, long *);
755 long ival;
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);
761 else
762 *p = ival;
763 break;
766 case 'k': { /* long sized bitfield */
767 unsigned long *p = va_arg(*p_va, unsigned long *);
768 unsigned long ival;
769 if (PyLong_Check(arg))
770 ival = PyLong_AsUnsignedLongMask(arg);
771 else
772 return converterr("integer<k>", arg, msgbuf, bufsize);
773 *p = ival;
774 break;
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);
783 } else {
784 *p = ival;
786 break;
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);
794 else
795 return converterr("integer<K>", arg, msgbuf, bufsize);
796 *p = ival;
797 break;
799 #endif
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);
806 else
807 *p = (float) dval;
808 break;
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);
816 else
817 *p = dval;
818 break;
821 #ifndef WITHOUT_COMPLEX
822 case 'D': {/* complex double */
823 Py_complex *p = va_arg(*p_va, Py_complex *);
824 Py_complex cval;
825 cval = PyComplex_AsCComplex(arg);
826 if (PyErr_Occurred())
827 return converterr("complex<D>", arg, msgbuf, bufsize);
828 else
829 *p = cval;
830 break;
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];
838 else
839 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
840 break;
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];
848 else
849 return converterr("a unicode character", arg, msgbuf, bufsize);
850 break;
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);
862 if (uarg == NULL)
863 return converterr(CONV_UNICODE,
864 arg, msgbuf, bufsize);
865 PyBuffer_FillInfo(p, arg,
866 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
867 1, 0);
869 else { /* any buffer-like object */
870 char *buf;
871 if (getbuffer(arg, p, &buf) < 0)
872 return converterr(buf, arg, msgbuf, bufsize);
874 if (addcleanup(p, freelist, cleanup_buffer)) {
875 return converterr(
876 "(cleanup problem)",
877 arg, msgbuf, bufsize);
879 format++;
880 } else if (*format == '#') {
881 void **p = (void **)va_arg(*p_va, char **);
882 FETCH_SIZE;
884 if (PyUnicode_Check(arg)) {
885 uarg = UNICODE_DEFAULT_ENCODING(arg);
886 if (uarg == NULL)
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 */
893 /* XXX Really? */
894 char *buf;
895 Py_ssize_t count = convertbuffer(arg, p, &buf);
896 if (count < 0)
897 return converterr(buf, arg, msgbuf, bufsize);
898 STORE_SIZE(count);
900 format++;
901 } else {
902 char **p = va_arg(*p_va, char **);
904 if (PyUnicode_Check(arg)) {
905 uarg = UNICODE_DEFAULT_ENCODING(arg);
906 if (uarg == NULL)
907 return converterr(CONV_UNICODE,
908 arg, msgbuf, bufsize);
909 *p = PyBytes_AS_STRING(uarg);
911 else
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);
917 break;
920 case 'y': {/* any buffer-like object, but not PyUnicode */
921 void **p = (void **)va_arg(*p_va, char **);
922 char *buf;
923 Py_ssize_t count;
924 if (*format == '*') {
925 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
926 return converterr(buf, arg, msgbuf, bufsize);
927 format++;
928 if (addcleanup(p, freelist, cleanup_buffer)) {
929 return converterr(
930 "(cleanup problem)",
931 arg, msgbuf, bufsize);
933 break;
935 count = convertbuffer(arg, p, &buf);
936 if (count < 0)
937 return converterr(buf, arg, msgbuf, bufsize);
938 else if (*format == '#') {
939 FETCH_SIZE;
940 STORE_SIZE(count);
941 format++;
943 break;
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 *);
950 if (arg == Py_None)
951 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
952 else if (PyUnicode_Check(arg)) {
953 uarg = UNICODE_DEFAULT_ENCODING(arg);
954 if (uarg == NULL)
955 return converterr(CONV_UNICODE,
956 arg, msgbuf, bufsize);
957 PyBuffer_FillInfo(p, arg,
958 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
959 1, 0);
961 else { /* any buffer-like object */
962 char *buf;
963 if (getbuffer(arg, p, &buf) < 0)
964 return converterr(buf, arg, msgbuf, bufsize);
966 if (addcleanup(p, freelist, cleanup_buffer)) {
967 return converterr(
968 "(cleanup problem)",
969 arg, msgbuf, bufsize);
971 format++;
972 } else if (*format == '#') { /* any buffer-like object */
973 void **p = (void **)va_arg(*p_va, char **);
974 FETCH_SIZE;
976 if (arg == Py_None) {
977 *p = 0;
978 STORE_SIZE(0);
980 else if (PyUnicode_Check(arg)) {
981 uarg = UNICODE_DEFAULT_ENCODING(arg);
982 if (uarg == NULL)
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 */
989 /* XXX Really? */
990 char *buf;
991 Py_ssize_t count = convertbuffer(arg, p, &buf);
992 if (count < 0)
993 return converterr(buf, arg, msgbuf, bufsize);
994 STORE_SIZE(count);
996 format++;
997 } else {
998 char **p = va_arg(*p_va, char **);
999 uarg = NULL;
1001 if (arg == Py_None)
1002 *p = 0;
1003 else if (PyBytes_Check(arg)) {
1004 /* Enable null byte check below */
1005 uarg = arg;
1006 *p = PyBytes_AS_STRING(arg);
1008 else if (PyUnicode_Check(arg)) {
1009 uarg = UNICODE_DEFAULT_ENCODING(arg);
1010 if (uarg == NULL)
1011 return converterr(CONV_UNICODE,
1012 arg, msgbuf, bufsize);
1013 *p = PyBytes_AS_STRING(uarg);
1015 else
1016 return converterr("string or None",
1017 arg, msgbuf, bufsize);
1018 if (*format == '#') {
1019 FETCH_SIZE;
1020 assert(0); /* XXX redundant with if-case */
1021 if (arg == Py_None) {
1022 STORE_SIZE(0);
1024 else {
1025 STORE_SIZE(PyBytes_Size(arg));
1027 format++;
1029 else if (*p != NULL && uarg != NULL &&
1030 (Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
1031 return converterr(
1032 "string without null bytes or None",
1033 arg, msgbuf, bufsize);
1035 break;
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 **);
1041 FETCH_SIZE;
1043 if (arg == Py_None) {
1044 *p = 0;
1045 STORE_SIZE(0);
1047 else if (PyUnicode_Check(arg)) {
1048 *p = PyUnicode_AS_UNICODE(arg);
1049 STORE_SIZE(PyUnicode_GET_SIZE(arg));
1051 format++;
1052 } else {
1053 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1055 if (arg == Py_None)
1056 *p = 0;
1057 else if (PyUnicode_Check(arg))
1058 *p = PyUnicode_AS_UNICODE(arg);
1059 else
1060 return converterr("string or None",
1061 arg, msgbuf, bufsize);
1063 break;
1066 case 'e': {/* encoded string */
1067 char **buffer;
1068 const char *encoding;
1069 PyObject *s;
1070 int recode_strings;
1071 Py_ssize_t size;
1072 const char *ptr;
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)
1083 if (*format == 's')
1084 recode_strings = 1;
1085 else if (*format == 't')
1086 recode_strings = 0;
1087 else
1088 return converterr(
1089 "(unknown parser marker combination)",
1090 arg, msgbuf, bufsize);
1091 buffer = (char **)va_arg(*p_va, char **);
1092 format++;
1093 if (buffer == NULL)
1094 return converterr("(buffer is NULL)",
1095 arg, msgbuf, bufsize);
1097 /* Encode object */
1098 if (!recode_strings &&
1099 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1100 s = arg;
1101 Py_INCREF(s);
1102 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1103 return converterr("(AsCharBuffer failed)",
1104 arg, msgbuf, bufsize);
1106 else {
1107 PyObject *u;
1109 /* Convert object to Unicode */
1110 u = PyUnicode_FromObject(arg);
1111 if (u == NULL)
1112 return converterr(
1113 "string or unicode or text buffer",
1114 arg, msgbuf, bufsize);
1116 /* Encode object; use default error handling */
1117 s = PyUnicode_AsEncodedString(u,
1118 encoding,
1119 NULL);
1120 Py_DECREF(u);
1121 if (s == NULL)
1122 return converterr("(encoding failed)",
1123 arg, msgbuf, bufsize);
1124 if (!PyBytes_Check(s)) {
1125 Py_DECREF(s);
1126 return converterr(
1127 "(encoder failed to return bytes)",
1128 arg, msgbuf, bufsize);
1130 size = PyBytes_GET_SIZE(s);
1131 ptr = PyBytes_AS_STRING(s);
1132 if (ptr == NULL)
1133 ptr = "";
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
1145 usage
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
1156 trailing 0-byte
1159 FETCH_SIZE;
1161 format++;
1162 if (q == NULL && q2 == NULL) {
1163 Py_DECREF(s);
1164 return converterr(
1165 "(buffer_len is NULL)",
1166 arg, msgbuf, bufsize);
1168 if (*buffer == NULL) {
1169 *buffer = PyMem_NEW(char, size + 1);
1170 if (*buffer == NULL) {
1171 Py_DECREF(s);
1172 return converterr(
1173 "(memory error)",
1174 arg, msgbuf, bufsize);
1176 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1177 Py_DECREF(s);
1178 return converterr(
1179 "(cleanup problem)",
1180 arg, msgbuf, bufsize);
1182 } else {
1183 if (size + 1 > BUFFER_LEN) {
1184 Py_DECREF(s);
1185 return converterr(
1186 "(buffer overflow)",
1187 arg, msgbuf, bufsize);
1190 memcpy(*buffer, ptr, size+1);
1191 STORE_SIZE(size);
1192 } else {
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
1197 error raised
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) {
1207 Py_DECREF(s);
1208 return converterr(
1209 "encoded string without NULL bytes",
1210 arg, msgbuf, bufsize);
1212 *buffer = PyMem_NEW(char, size + 1);
1213 if (*buffer == NULL) {
1214 Py_DECREF(s);
1215 return converterr("(memory error)",
1216 arg, msgbuf, bufsize);
1218 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1219 Py_DECREF(s);
1220 return converterr("(cleanup problem)",
1221 arg, msgbuf, bufsize);
1223 memcpy(*buffer, ptr, size+1);
1225 Py_DECREF(s);
1226 break;
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 */
1235 FETCH_SIZE;
1236 STORE_SIZE(PyUnicode_GET_SIZE(arg));
1237 format++;
1239 break;
1242 case 'S': { /* PyBytes object */
1243 PyObject **p = va_arg(*p_va, PyObject **);
1244 if (PyBytes_Check(arg))
1245 *p = arg;
1246 else
1247 return converterr("bytes", arg, msgbuf, bufsize);
1248 break;
1251 case 'Y': { /* PyByteArray object */
1252 PyObject **p = va_arg(*p_va, PyObject **);
1253 if (PyByteArray_Check(arg))
1254 *p = arg;
1255 else
1256 return converterr("buffer", arg, msgbuf, bufsize);
1257 break;
1260 case 'U': { /* PyUnicode object */
1261 PyObject **p = va_arg(*p_va, PyObject **);
1262 if (PyUnicode_Check(arg))
1263 *p = arg;
1264 else
1265 return converterr("str", arg, msgbuf, bufsize);
1266 break;
1269 case 'O': { /* object */
1270 PyTypeObject *type;
1271 PyObject **p;
1272 if (*format == '!') {
1273 type = va_arg(*p_va, PyTypeObject*);
1274 p = va_arg(*p_va, PyObject **);
1275 format++;
1276 if (PyType_IsSubtype(arg->ob_type, type))
1277 *p = arg;
1278 else
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 **);
1285 format++;
1286 if ((*pred)(arg))
1287 *p = arg;
1288 else
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 *);
1297 int res;
1298 format++;
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);
1307 else {
1308 p = va_arg(*p_va, PyObject **);
1309 *p = arg;
1311 break;
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;
1318 Py_ssize_t count;
1319 int temp=-1;
1320 Py_buffer view;
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. */
1331 format++;
1332 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1333 PyErr_Clear();
1334 return converterr("read-write buffer", arg, msgbuf, bufsize);
1336 if (addcleanup(p, freelist, cleanup_buffer)) {
1337 return converterr(
1338 "(cleanup problem)",
1339 arg, msgbuf, bufsize);
1341 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1342 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1343 break;
1346 /* Here we have processed w*, only w and w# remain. */
1347 if (pb == NULL ||
1348 pb->bf_getbuffer == NULL ||
1349 ((temp = PyObject_GetBuffer(arg, &view,
1350 PyBUF_SIMPLE)) != 0) ||
1351 view.readonly == 1) {
1352 if (temp==0) {
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);
1361 *p = view.buf;
1362 if (*format == '#') {
1363 FETCH_SIZE;
1364 STORE_SIZE(count);
1365 format++;
1367 break;
1370 /*TEO: This can be eliminated --- here only for backward
1371 compatibility */
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;
1375 Py_ssize_t count;
1376 Py_buffer view;
1378 if (*format++ != '#')
1379 return converterr(
1380 "invalid use of 't' format character",
1381 arg, msgbuf, bufsize);
1382 if (pb == NULL || pb->bf_getbuffer == NULL)
1383 return converterr(
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);
1391 count = view.len;
1392 *p = view.buf;
1393 if (pb->bf_releasebuffer)
1394 return converterr(
1395 "string or pinned buffer",
1396 arg, msgbuf, bufsize);
1398 PyBuffer_Release(&view);
1400 if (count < 0)
1401 return converterr("(unspecified)", arg, msgbuf, bufsize);
1403 FETCH_SIZE;
1404 STORE_SIZE(count);
1406 break;
1409 default:
1410 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
1414 *p_format = format;
1415 return NULL;
1418 static Py_ssize_t
1419 convertbuffer(PyObject *arg, void **p, char **errmsg)
1421 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1422 Py_ssize_t count;
1423 Py_buffer view;
1425 *errmsg = NULL;
1426 *p = NULL;
1427 if (pb == NULL ||
1428 pb->bf_getbuffer == NULL ||
1429 pb->bf_releasebuffer != NULL) {
1430 *errmsg = "bytes or read-only buffer";
1431 return -1;
1434 if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) != 0) {
1435 *errmsg = "bytes or single-segment read-only buffer";
1436 return -1;
1438 count = view.len;
1439 *p = view.buf;
1440 PyBuffer_Release(&view);
1441 return count;
1444 /* XXX for 3.x, getbuffer and convertbuffer can probably
1445 be merged again. */
1446 static int
1447 getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
1449 void *buf;
1450 Py_ssize_t count;
1451 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1452 if (pb == NULL) {
1453 *errmsg = "bytes or buffer";
1454 return -1;
1456 if (pb->bf_getbuffer) {
1457 if (PyObject_GetBuffer(arg, view, 0) < 0) {
1458 *errmsg = "convertible to a buffer";
1459 return -1;
1461 if (!PyBuffer_IsContiguous(view, 'C')) {
1462 *errmsg = "contiguous buffer";
1463 return -1;
1465 return 0;
1468 count = convertbuffer(arg, &buf, errmsg);
1469 if (count < 0) {
1470 *errmsg = "convertible to a buffer";
1471 return count;
1473 PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
1474 return 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,
1483 PyObject *keywords,
1484 const char *format,
1485 char **kwlist, ...)
1487 int retval;
1488 va_list va;
1490 if ((args == NULL || !PyTuple_Check(args)) ||
1491 (keywords != NULL && !PyDict_Check(keywords)) ||
1492 format == NULL ||
1493 kwlist == NULL)
1495 PyErr_BadInternalCall();
1496 return 0;
1499 va_start(va, kwlist);
1500 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1501 va_end(va);
1502 return retval;
1506 _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1507 PyObject *keywords,
1508 const char *format,
1509 char **kwlist, ...)
1511 int retval;
1512 va_list va;
1514 if ((args == NULL || !PyTuple_Check(args)) ||
1515 (keywords != NULL && !PyDict_Check(keywords)) ||
1516 format == NULL ||
1517 kwlist == NULL)
1519 PyErr_BadInternalCall();
1520 return 0;
1523 va_start(va, kwlist);
1524 retval = vgetargskeywords(args, keywords, format,
1525 kwlist, &va, FLAG_SIZE_T);
1526 va_end(va);
1527 return retval;
1532 PyArg_VaParseTupleAndKeywords(PyObject *args,
1533 PyObject *keywords,
1534 const char *format,
1535 char **kwlist, va_list va)
1537 int retval;
1538 va_list lva;
1540 if ((args == NULL || !PyTuple_Check(args)) ||
1541 (keywords != NULL && !PyDict_Check(keywords)) ||
1542 format == NULL ||
1543 kwlist == NULL)
1545 PyErr_BadInternalCall();
1546 return 0;
1549 #ifdef VA_LIST_IS_ARRAY
1550 memcpy(lva, va, sizeof(va_list));
1551 #else
1552 #ifdef __va_copy
1553 __va_copy(lva, va);
1554 #else
1555 lva = va;
1556 #endif
1557 #endif
1559 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1560 return retval;
1564 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1565 PyObject *keywords,
1566 const char *format,
1567 char **kwlist, va_list va)
1569 int retval;
1570 va_list lva;
1572 if ((args == NULL || !PyTuple_Check(args)) ||
1573 (keywords != NULL && !PyDict_Check(keywords)) ||
1574 format == NULL ||
1575 kwlist == NULL)
1577 PyErr_BadInternalCall();
1578 return 0;
1581 #ifdef VA_LIST_IS_ARRAY
1582 memcpy(lva, va, sizeof(va_list));
1583 #else
1584 #ifdef __va_copy
1585 __va_copy(lva, va);
1586 #else
1587 lva = va;
1588 #endif
1589 #endif
1591 retval = vgetargskeywords(args, keywords, format,
1592 kwlist, &lva, FLAG_SIZE_T);
1593 return retval;
1596 #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1598 static int
1599 vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
1600 char **kwlist, va_list *p_va, int flags)
1602 char msgbuf[512];
1603 int levels[32];
1604 const char *fname, *msg, *custom_msg, *keyword;
1605 int min = INT_MAX;
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, ':');
1617 if (fname) {
1618 fname++;
1619 custom_msg = NULL;
1621 else {
1622 custom_msg = strchr(format,';');
1623 if (custom_msg)
1624 custom_msg++;
1627 /* scan kwlist and get greatest possible nbr of args */
1628 for (len=0; kwlist[len]; len++)
1629 continue;
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) ? "" : "()",
1638 len,
1639 (len == 1) ? "" : "s",
1640 nargs + nkeywords);
1641 return 0;
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 == '|') {
1648 min = i;
1649 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);
1657 current_arg = NULL;
1658 if (nkeywords) {
1659 current_arg = PyDict_GetItemString(keywords, keyword);
1661 if (current_arg) {
1662 --nkeywords;
1663 if (i < nargs) {
1664 /* arg present in tuple and in dict */
1665 PyErr_Format(PyExc_TypeError,
1666 "Argument given by name ('%s') "
1667 "and position (%d)",
1668 keyword, i+1);
1669 return cleanreturn(0, freelist);
1672 else if (nkeywords && PyErr_Occurred())
1673 return cleanreturn(0, freelist);
1674 else if (i < nargs)
1675 current_arg = PyTuple_GET_ITEM(args, i);
1677 if (current_arg) {
1678 msg = convertitem(current_arg, &format, p_va, flags,
1679 levels, msgbuf, sizeof(msgbuf), &freelist);
1680 if (msg) {
1681 seterror(i+1, msg, levels, fname, custom_msg);
1682 return cleanreturn(0, freelist);
1684 continue;
1687 if (i < min) {
1688 PyErr_Format(PyExc_TypeError, "Required argument "
1689 "'%s' (pos %d) not found",
1690 keyword, i+1);
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 ?
1697 if (!nkeywords)
1698 return cleanreturn(1, freelist);
1700 /* We are into optional args, skip thru to any remaining
1701 * keyword args */
1702 msg = skipitem(&format, p_va, flags);
1703 if (msg) {
1704 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1705 format);
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;
1720 Py_ssize_t pos = 0;
1721 while (PyDict_Next(keywords, &pos, &key, &value)) {
1722 int match = 0;
1723 char *ks;
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])) {
1732 match = 1;
1733 break;
1736 if (!match) {
1737 PyErr_Format(PyExc_TypeError,
1738 "'%s' is an invalid keyword "
1739 "argument for this function",
1740 ks);
1741 return cleanreturn(0, freelist);
1746 return cleanreturn(1, freelist);
1750 static char *
1751 skipitem(const char **p_format, va_list *p_va, int flags)
1753 const char *format = *p_format;
1754 char c = *format++;
1756 switch (c) {
1758 /* simple codes
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 */
1765 case 'i': /* int */
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 */
1772 #endif
1773 case 'f': /* float */
1774 case 'd': /* double */
1775 #ifndef WITHOUT_COMPLEX
1776 case 'D': /* complex double */
1777 #endif
1778 case 'c': /* char */
1780 (void) va_arg(*p_va, void *);
1781 break;
1784 case 'n': /* Py_ssize_t */
1786 (void) va_arg(*p_va, Py_ssize_t *);
1787 break;
1790 /* string codes */
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 */
1797 goto err;
1798 format++;
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 *);
1813 else
1814 (void) va_arg(*p_va, int *);
1815 format++;
1816 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
1817 format++;
1819 break;
1822 /* object codes */
1824 case 'S': /* string object */
1825 case 'Y': /* string object */
1826 case 'U': /* unicode string object */
1828 (void) va_arg(*p_va, PyObject **);
1829 break;
1832 case 'O': /* object */
1834 if (*format == '!') {
1835 format++;
1836 (void) va_arg(*p_va, PyTypeObject*);
1837 (void) va_arg(*p_va, PyObject **);
1839 #if 0
1840 /* I don't know what this is for */
1841 else if (*format == '?') {
1842 inquiry pred = va_arg(*p_va, inquiry);
1843 format++;
1844 if ((*pred)(arg)) {
1845 (void) va_arg(*p_va, PyObject **);
1848 #endif
1849 else if (*format == '&') {
1850 typedef int (*converter)(PyObject *, void *);
1851 (void) va_arg(*p_va, converter);
1852 (void) va_arg(*p_va, void *);
1853 format++;
1855 else {
1856 (void) va_arg(*p_va, PyObject **);
1858 break;
1861 case '(': /* bypass tuple, not handled at all previously */
1863 char *msg;
1864 for (;;) {
1865 if (*format==')')
1866 break;
1867 if (IS_END_OF_FORMAT(*format))
1868 return "Unmatched left paren in format "
1869 "string";
1870 msg = skipitem(&format, p_va, flags);
1871 if (msg)
1872 return msg;
1874 format++;
1875 break;
1878 case ')':
1879 return "Unmatched right paren in format string";
1881 default:
1882 err:
1883 return "impossible<bad format char>";
1887 *p_format = format;
1888 return NULL;
1893 PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
1895 Py_ssize_t i, l;
1896 PyObject **o;
1897 va_list vargs;
1899 #ifdef HAVE_STDARG_PROTOTYPES
1900 va_start(vargs, max);
1901 #else
1902 va_start(vargs);
1903 #endif
1905 assert(min >= 0);
1906 assert(min <= max);
1907 if (!PyTuple_Check(args)) {
1908 PyErr_SetString(PyExc_SystemError,
1909 "PyArg_UnpackTuple() argument list is not a tuple");
1910 return 0;
1912 l = PyTuple_GET_SIZE(args);
1913 if (l < min) {
1914 if (name != NULL)
1915 PyErr_Format(
1916 PyExc_TypeError,
1917 "%s expected %s%zd arguments, got %zd",
1918 name, (min == max ? "" : "at least "), min, l);
1919 else
1920 PyErr_Format(
1921 PyExc_TypeError,
1922 "unpacked tuple should have %s%zd elements,"
1923 " but has %zd",
1924 (min == max ? "" : "at least "), min, l);
1925 va_end(vargs);
1926 return 0;
1928 if (l > max) {
1929 if (name != NULL)
1930 PyErr_Format(
1931 PyExc_TypeError,
1932 "%s expected %s%zd arguments, got %zd",
1933 name, (min == max ? "" : "at most "), max, l);
1934 else
1935 PyErr_Format(
1936 PyExc_TypeError,
1937 "unpacked tuple should have %s%zd elements,"
1938 " but has %zd",
1939 (min == max ? "" : "at most "), max, l);
1940 va_end(vargs);
1941 return 0;
1943 for (i = 0; i < l; i++) {
1944 o = va_arg(vargs, PyObject **);
1945 *o = PyTuple_GET_ITEM(args, i);
1947 va_end(vargs);
1948 return 1;
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)
1960 if (kw == NULL)
1961 return 1;
1962 if (!PyDict_CheckExact(kw)) {
1963 PyErr_BadInternalCall();
1964 return 0;
1966 if (PyDict_Size(kw) == 0)
1967 return 1;
1969 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1970 funcname);
1971 return 0;
1973 #ifdef __cplusplus
1975 #endif