1
2 /* New getargs implementation */
3
4 #include "Python.h"
5
6 #include <ctype.h>
7
8
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);
15
16 int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
17 const char *, char **, ...);
18 int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
19 const char *, char **, va_list);
20
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
32
33 #define FLAG_COMPAT 1
34 #define FLAG_SIZE_T 2
35
36
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**);
48
49 static int vgetargskeywords(PyObject *, PyObject *,
50 const char *, char **, va_list *, int);
51 static char *skipitem(const char **, va_list *, int);
52
53 int
PyArg_Parse(PyObject * args,const char * format,...)54 PyArg_Parse(PyObject *args, const char *format, ...)
55 {
56 int retval;
57 va_list va;
58
59 va_start(va, format);
60 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
61 va_end(va);
62 return retval;
63 }
64
65 int
_PyArg_Parse_SizeT(PyObject * args,char * format,...)66 _PyArg_Parse_SizeT(PyObject *args, char *format, ...)
67 {
68 int retval;
69 va_list va;
70
71 va_start(va, format);
72 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
73 va_end(va);
74 return retval;
75 }
76
77
78 int
PyArg_ParseTuple(PyObject * args,const char * format,...)79 PyArg_ParseTuple(PyObject *args, const char *format, ...)
80 {
81 int retval;
82 va_list va;
83
84 va_start(va, format);
85 retval = vgetargs1(args, format, &va, 0);
86 va_end(va);
87 return retval;
88 }
89
90 int
_PyArg_ParseTuple_SizeT(PyObject * args,char * format,...)91 _PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
92 {
93 int retval;
94 va_list va;
95
96 va_start(va, format);
97 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
98 va_end(va);
99 return retval;
100 }
101
102
103 int
PyArg_VaParse(PyObject * args,const char * format,va_list va)104 PyArg_VaParse(PyObject *args, const char *format, va_list va)
105 {
106 va_list lva;
107
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
117
118 return vgetargs1(args, format, &lva, 0);
119 }
120
121 int
_PyArg_VaParse_SizeT(PyObject * args,char * format,va_list va)122 _PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
123 {
124 va_list lva;
125
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
135
136 return vgetargs1(args, format, &lva, FLAG_SIZE_T);
137 }
138
139
140 /* Handle cleanup of allocated memory in case of exception */
141
142 #define GETARGS_CAPSULE_NAME_CLEANUP_PTR "getargs.cleanup_ptr"
143 #define GETARGS_CAPSULE_NAME_CLEANUP_BUFFER "getargs.cleanup_buffer"
144
145 static void
cleanup_ptr(PyObject * self)146 cleanup_ptr(PyObject *self)
147 {
148 void *ptr = PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_PTR);
149 if (ptr) {
150 PyMem_FREE(ptr);
151 }
152 }
153
154 static void
cleanup_buffer(PyObject * self)155 cleanup_buffer(PyObject *self)
156 {
157 Py_buffer *ptr = (Py_buffer *)PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_BUFFER);
158 if (ptr) {
159 PyBuffer_Release(ptr);
160 }
161 }
162
163 static int
addcleanup(void * ptr,PyObject ** freelist,PyCapsule_Destructor destr)164 addcleanup(void *ptr, PyObject **freelist, PyCapsule_Destructor destr)
165 {
166 PyObject *cobj;
167 const char *name;
168
169 if (!*freelist) {
170 *freelist = PyList_New(0);
171 if (!*freelist) {
172 destr(ptr);
173 return -1;
174 }
175 }
176
177 if (destr == cleanup_ptr) {
178 name = GETARGS_CAPSULE_NAME_CLEANUP_PTR;
179 } else if (destr == cleanup_buffer) {
180 name = GETARGS_CAPSULE_NAME_CLEANUP_BUFFER;
181 } else {
182 return -1;
183 }
184 cobj = PyCapsule_New(ptr, name, destr);
185 if (!cobj) {
186 destr(ptr);
187 return -1;
188 }
189 if (PyList_Append(*freelist, cobj)) {
190 Py_DECREF(cobj);
191 return -1;
192 }
193 Py_DECREF(cobj);
194 return 0;
195 }
196
197 static int
cleanreturn(int retval,PyObject * freelist)198 cleanreturn(int retval, PyObject *freelist)
199 {
200 if (freelist && retval != 0) {
201 /* We were successful, reset the destructors so that they
202 don't get called. */
203 Py_ssize_t len = PyList_GET_SIZE(freelist), i;
204 for (i = 0; i < len; i++)
205 PyCapsule_SetDestructor(PyList_GET_ITEM(freelist, i), NULL);
206 }
207 Py_XDECREF(freelist);
208 return retval;
209 }
210
211
212 static int
vgetargs1(PyObject * args,const char * format,va_list * p_va,int flags)213 vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
214 {
215 char msgbuf[256];
216 int levels[32];
217 const char *fname = NULL;
218 const char *message = NULL;
219 int min = -1;
220 int max = 0;
221 int level = 0;
222 int endfmt = 0;
223 const char *formatsave = format;
224 Py_ssize_t i, len;
225 char *msg;
226 PyObject *freelist = NULL;
227 int compat = flags & FLAG_COMPAT;
228
229 assert(compat || (args != (PyObject*)NULL));
230 flags = flags & ~FLAG_COMPAT;
231
232 while (endfmt == 0) {
233 int c = *format++;
234 switch (c) {
235 case '(':
236 if (level == 0)
237 max++;
238 level++;
239 if (level >= 30)
240 Py_FatalError("too many tuple nesting levels "
241 "in argument format string");
242 break;
243 case ')':
244 if (level == 0)
245 Py_FatalError("excess ')' in getargs format");
246 else
247 level--;
248 break;
249 case '\0':
250 endfmt = 1;
251 break;
252 case ':':
253 fname = format;
254 endfmt = 1;
255 break;
256 case ';':
257 message = format;
258 endfmt = 1;
259 break;
260 default:
261 if (level == 0) {
262 if (c == 'O')
263 max++;
264 else if (isalpha(Py_CHARMASK(c))) {
265 if (c != 'e') /* skip encoded */
266 max++;
267 } else if (c == '|')
268 min = max;
269 }
270 break;
271 }
272 }
273
274 if (level != 0)
275 Py_FatalError(/* '(' */ "missing ')' in getargs format");
276
277 if (min < 0)
278 min = max;
279
280 format = formatsave;
281
282 if (compat) {
283 if (max == 0) {
284 if (args == NULL)
285 return 1;
286 PyOS_snprintf(msgbuf, sizeof(msgbuf),
287 "%.200s%s takes no arguments",
288 fname==NULL ? "function" : fname,
289 fname==NULL ? "" : "()");
290 PyErr_SetString(PyExc_TypeError, msgbuf);
291 return 0;
292 }
293 else if (min == 1 && max == 1) {
294 if (args == NULL) {
295 PyOS_snprintf(msgbuf, sizeof(msgbuf),
296 "%.200s%s takes at least one argument",
297 fname==NULL ? "function" : fname,
298 fname==NULL ? "" : "()");
299 PyErr_SetString(PyExc_TypeError, msgbuf);
300 return 0;
301 }
302 msg = convertitem(args, &format, p_va, flags, levels,
303 msgbuf, sizeof(msgbuf), &freelist);
304 if (msg == NULL)
305 return cleanreturn(1, freelist);
306 seterror(levels[0], msg, levels+1, fname, message);
307 return cleanreturn(0, freelist);
308 }
309 else {
310 PyErr_SetString(PyExc_SystemError,
311 "old style getargs format uses new features");
312 return 0;
313 }
314 }
315
316 if (!PyTuple_Check(args)) {
317 PyErr_SetString(PyExc_SystemError,
318 "new style getargs format but argument is not a tuple");
319 return 0;
320 }
321
322 len = PyTuple_GET_SIZE(args);
323
324 if (len < min || max < len) {
325 if (message == NULL) {
326 PyOS_snprintf(msgbuf, sizeof(msgbuf),
327 "%.150s%s takes %s %d argument%s "
328 "(%ld given)",
329 fname==NULL ? "function" : fname,
330 fname==NULL ? "" : "()",
331 min==max ? "exactly"
332 : len < min ? "at least" : "at most",
333 len < min ? min : max,
334 (len < min ? min : max) == 1 ? "" : "s",
335 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
336 message = msgbuf;
337 }
338 PyErr_SetString(PyExc_TypeError, message);
339 return 0;
340 }
341
342 for (i = 0; i < len; i++) {
343 if (*format == '|')
344 format++;
345 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
346 flags, levels, msgbuf,
347 sizeof(msgbuf), &freelist);
348 if (msg) {
349 seterror(i+1, msg, levels, fname, message);
350 return cleanreturn(0, freelist);
351 }
352 }
353
354 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
355 *format != '(' &&
356 *format != '|' && *format != ':' && *format != ';') {
357 PyErr_Format(PyExc_SystemError,
358 "bad format string: %.200s", formatsave);
359 return cleanreturn(0, freelist);
360 }
361
362 return cleanreturn(1, freelist);
363 }
364
365
366
367 static void
seterror(int iarg,const char * msg,int * levels,const char * fname,const char * message)368 seterror(int iarg, const char *msg, int *levels, const char *fname,
369 const char *message)
370 {
371 char buf[512];
372 int i;
373 char *p = buf;
374
375 if (PyErr_Occurred())
376 return;
377 else if (message == NULL) {
378 if (fname != NULL) {
379 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
380 p += strlen(p);
381 }
382 if (iarg != 0) {
383 PyOS_snprintf(p, sizeof(buf) - (p - buf),
384 "argument %d", iarg);
385 i = 0;
386 p += strlen(p);
387 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
388 PyOS_snprintf(p, sizeof(buf) - (p - buf),
389 ", item %d", levels[i]-1);
390 p += strlen(p);
391 i++;
392 }
393 }
394 else {
395 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
396 p += strlen(p);
397 }
398 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
399 message = buf;
400 }
401 PyErr_SetString(PyExc_TypeError, message);
402 }
403
404
405 /* Convert a tuple argument.
406 On entry, *p_format points to the character _after_ the opening '('.
407 On successful exit, *p_format points to the closing ')'.
408 If successful:
409 *p_format and *p_va are updated,
410 *levels and *msgbuf are untouched,
411 and NULL is returned.
412 If the argument is invalid:
413 *p_format is unchanged,
414 *p_va is undefined,
415 *levels is a 0-terminated list of item numbers,
416 *msgbuf contains an error message, whose format is:
417 "must be <typename1>, not <typename2>", where:
418 <typename1> is the name of the expected type, and
419 <typename2> is the name of the actual type,
420 and msgbuf is returned.
421 */
422
423 static char *
converttuple(PyObject * arg,const char ** p_format,va_list * p_va,int flags,int * levels,char * msgbuf,size_t bufsize,int toplevel,PyObject ** freelist)424 converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
425 int *levels, char *msgbuf, size_t bufsize, int toplevel,
426 PyObject **freelist)
427 {
428 int level = 0;
429 int n = 0;
430 const char *format = *p_format;
431 int i;
432
433 for (;;) {
434 int c = *format++;
435 if (c == '(') {
436 if (level == 0)
437 n++;
438 level++;
439 }
440 else if (c == ')') {
441 if (level == 0)
442 break;
443 level--;
444 }
445 else if (c == ':' || c == ';' || c == '\0')
446 break;
447 else if (level == 0 && isalpha(Py_CHARMASK(c)))
448 n++;
449 }
450
451 if (!PySequence_Check(arg) || PyString_Check(arg)) {
452 levels[0] = 0;
453 PyOS_snprintf(msgbuf, bufsize,
454 toplevel ? "expected %d arguments, not %.50s" :
455 "must be %d-item sequence, not %.50s",
456 n,
457 arg == Py_None ? "None" : arg->ob_type->tp_name);
458 return msgbuf;
459 }
460
461 if ((i = PySequence_Size(arg)) != n) {
462 levels[0] = 0;
463 PyOS_snprintf(msgbuf, bufsize,
464 toplevel ? "expected %d arguments, not %d" :
465 "must be sequence of length %d, not %d",
466 n, i);
467 return msgbuf;
468 }
469
470 format = *p_format;
471 for (i = 0; i < n; i++) {
472 char *msg;
473 PyObject *item;
474 item = PySequence_GetItem(arg, i);
475 if (item == NULL) {
476 PyErr_Clear();
477 levels[0] = i+1;
478 levels[1] = 0;
479 strncpy(msgbuf, "is not retrievable", bufsize);
480 return msgbuf;
481 }
482 msg = convertitem(item, &format, p_va, flags, levels+1,
483 msgbuf, bufsize, freelist);
484 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
485 Py_XDECREF(item);
486 if (msg != NULL) {
487 levels[0] = i+1;
488 return msg;
489 }
490 }
491
492 *p_format = format;
493 return NULL;
494 }
495
496
497 /* Convert a single item. */
498
499 static char *
convertitem(PyObject * arg,const char ** p_format,va_list * p_va,int flags,int * levels,char * msgbuf,size_t bufsize,PyObject ** freelist)500 convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
501 int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
502 {
503 char *msg;
504 const char *format = *p_format;
505
506 if (*format == '(' /* ')' */) {
507 format++;
508 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
509 bufsize, 0, freelist);
510 if (msg == NULL)
511 format++;
512 }
513 else {
514 msg = convertsimple(arg, &format, p_va, flags,
515 msgbuf, bufsize, freelist);
516 if (msg != NULL)
517 levels[0] = 0;
518 }
519 if (msg == NULL)
520 *p_format = format;
521 return msg;
522 }
523
524
525
526 #define UNICODE_DEFAULT_ENCODING(arg) \
527 _PyUnicode_AsDefaultEncodedString(arg, NULL)
528
529 /* Format an error message generated by convertsimple(). */
530
531 static char *
converterr(const char * expected,PyObject * arg,char * msgbuf,size_t bufsize)532 converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
533 {
534 assert(expected != NULL);
535 assert(arg != NULL);
536 if (expected[0] == '(') {
537 PyOS_snprintf(msgbuf, bufsize,
538 "%.100s", expected);
539 strncpy(msgbuf, expected, bufsize);
540 msgbuf[bufsize-1] = '\0';
541 }
542 else {
543 PyOS_snprintf(msgbuf, bufsize,
544 "must be %.50s, not %.50s", expected,
545 arg == Py_None ? "None" : arg->ob_type->tp_name);
546 }
547 return msgbuf;
548 }
549
550 #define CONV_UNICODE "(unicode conversion error)"
551
552 /* explicitly check for float arguments when integers are expected. For now
553 * signal a warning. Returns true if an exception was raised. */
554 static int
float_argument_warning(PyObject * arg)555 float_argument_warning(PyObject *arg)
556 {
557 if (PyFloat_Check(arg) &&
558 PyErr_Warn(PyExc_DeprecationWarning,
559 "integer argument expected, got float" ))
560 return 1;
561 else
562 return 0;
563 }
564
565 /* explicitly check for float arguments when integers are expected. Raises
566 TypeError and returns true for float arguments. */
567 static int
float_argument_error(PyObject * arg)568 float_argument_error(PyObject *arg)
569 {
570 if (PyFloat_Check(arg)) {
571 PyErr_SetString(PyExc_TypeError,
572 "integer argument expected, got float");
573 return 1;
574 }
575 else
576 return 0;
577 }
578
579 #ifdef Py_USING_UNICODE
580 static size_t
_ustrlen(Py_UNICODE * u)581 _ustrlen(Py_UNICODE *u)
582 {
583 size_t i = 0;
584 Py_UNICODE *v = u;
585 while (*v != 0) { i++; v++; }
586 return i;
587 }
588 #endif
589
590 /* Convert a non-tuple argument. Return NULL if conversion went OK,
591 or a string with a message describing the failure. The message is
592 formatted as "must be <desired type>, not <actual type>".
593 When failing, an exception may or may not have been raised.
594 Don't call if a tuple is expected.
595
596 When you add new format codes, please don't forget poor skipitem() below.
597 */
598
599 static char *
convertsimple(PyObject * arg,const char ** p_format,va_list * p_va,int flags,char * msgbuf,size_t bufsize,PyObject ** freelist)600 convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
601 char *msgbuf, size_t bufsize, PyObject **freelist)
602 {
603 /* For # codes */
604 #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
605 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
606 else q=va_arg(*p_va, int*);
607 #define STORE_SIZE(s) \
608 if (flags & FLAG_SIZE_T) \
609 *q2=s; \
610 else { \
611 if (INT_MAX < s) { \
612 PyErr_SetString(PyExc_OverflowError, \
613 "size does not fit in an int"); \
614 return converterr("", arg, msgbuf, bufsize); \
615 } \
616 *q=s; \
617 }
618 #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
619
620 const char *format = *p_format;
621 char c = *format++;
622 #ifdef Py_USING_UNICODE
623 PyObject *uarg;
624 #endif
625
626 switch (c) {
627
628 case 'b': { /* unsigned byte -- very short int */
629 char *p = va_arg(*p_va, char *);
630 long ival;
631 if (float_argument_error(arg))
632 return converterr("integer<b>", arg, msgbuf, bufsize);
633 ival = PyInt_AsLong(arg);
634 if (ival == -1 && PyErr_Occurred())
635 return converterr("integer<b>", arg, msgbuf, bufsize);
636 else if (ival < 0) {
637 PyErr_SetString(PyExc_OverflowError,
638 "unsigned byte integer is less than minimum");
639 return converterr("integer<b>", arg, msgbuf, bufsize);
640 }
641 else if (ival > UCHAR_MAX) {
642 PyErr_SetString(PyExc_OverflowError,
643 "unsigned byte integer is greater than maximum");
644 return converterr("integer<b>", arg, msgbuf, bufsize);
645 }
646 else
647 *p = (unsigned char) ival;
648 break;
649 }
650
651 case 'B': {/* byte sized bitfield - both signed and unsigned
652 values allowed */
653 char *p = va_arg(*p_va, char *);
654 long ival;
655 if (float_argument_error(arg))
656 return converterr("integer<B>", arg, msgbuf, bufsize);
657 ival = PyInt_AsUnsignedLongMask(arg);
658 if (ival == -1 && PyErr_Occurred())
659 return converterr("integer<B>", arg, msgbuf, bufsize);
660 else
661 *p = (unsigned char) ival;
662 break;
663 }
664
665 case 'h': {/* signed short int */
666 short *p = va_arg(*p_va, short *);
667 long ival;
668 if (float_argument_error(arg))
669 return converterr("integer<h>", arg, msgbuf, bufsize);
670 ival = PyInt_AsLong(arg);
671 if (ival == -1 && PyErr_Occurred())
672 return converterr("integer<h>", arg, msgbuf, bufsize);
673 else if (ival < SHRT_MIN) {
674 PyErr_SetString(PyExc_OverflowError,
675 "signed short integer is less than minimum");
676 return converterr("integer<h>", arg, msgbuf, bufsize);
677 }
678 else if (ival > SHRT_MAX) {
679 PyErr_SetString(PyExc_OverflowError,
680 "signed short integer is greater than maximum");
681 return converterr("integer<h>", arg, msgbuf, bufsize);
682 }
683 else
684 *p = (short) ival;
685 break;
686 }
687
688 case 'H': { /* short int sized bitfield, both signed and
689 unsigned allowed */
690 unsigned short *p = va_arg(*p_va, unsigned short *);
691 long ival;
692 if (float_argument_error(arg))
693 return converterr("integer<H>", arg, msgbuf, bufsize);
694 ival = PyInt_AsUnsignedLongMask(arg);
695 if (ival == -1 && PyErr_Occurred())
696 return converterr("integer<H>", arg, msgbuf, bufsize);
697 else
698 *p = (unsigned short) ival;
699 break;
700 }
701
702 case 'i': {/* signed int */
703 int *p = va_arg(*p_va, int *);
704 long ival;
705 if (float_argument_error(arg))
706 return converterr("integer<i>", arg, msgbuf, bufsize);
707 ival = PyInt_AsLong(arg);
708 if (ival == -1 && PyErr_Occurred())
709 return converterr("integer<i>", arg, msgbuf, bufsize);
710 else if (ival > INT_MAX) {
711 PyErr_SetString(PyExc_OverflowError,
712 "signed integer is greater than maximum");
713 return converterr("integer<i>", arg, msgbuf, bufsize);
714 }
715 else if (ival < INT_MIN) {
716 PyErr_SetString(PyExc_OverflowError,
717 "signed integer is less than minimum");
718 return converterr("integer<i>", arg, msgbuf, bufsize);
719 }
720 else
721 *p = ival;
722 break;
723 }
724
725 case 'I': { /* int sized bitfield, both signed and
726 unsigned allowed */
727 unsigned int *p = va_arg(*p_va, unsigned int *);
728 unsigned int ival;
729 if (float_argument_error(arg))
730 return converterr("integer<I>", arg, msgbuf, bufsize);
731 ival = (unsigned int)PyInt_AsUnsignedLongMask(arg);
732 if (ival == (unsigned int)-1 && PyErr_Occurred())
733 return converterr("integer<I>", arg, msgbuf, bufsize);
734 else
735 *p = ival;
736 break;
737 }
738
739 case 'n': /* Py_ssize_t */
740 #if SIZEOF_SIZE_T != SIZEOF_LONG
741 {
742 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
743 Py_ssize_t ival;
744 if (float_argument_error(arg))
745 return converterr("integer<n>", arg, msgbuf, bufsize);
746 ival = PyInt_AsSsize_t(arg);
747 if (ival == -1 && PyErr_Occurred())
748 return converterr("integer<n>", arg, msgbuf, bufsize);
749 *p = ival;
750 break;
751 }
752 #endif
753 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
754 case 'l': {/* long int */
755 long *p = va_arg(*p_va, long *);
756 long ival;
757 if (float_argument_error(arg))
758 return converterr("integer<l>", arg, msgbuf, bufsize);
759 ival = PyInt_AsLong(arg);
760 if (ival == -1 && PyErr_Occurred())
761 return converterr("integer<l>", arg, msgbuf, bufsize);
762 else
763 *p = ival;
764 break;
765 }
766
767 case 'k': { /* long sized bitfield */
768 unsigned long *p = va_arg(*p_va, unsigned long *);
769 unsigned long ival;
770 if (PyInt_Check(arg))
771 ival = PyInt_AsUnsignedLongMask(arg);
772 else if (PyLong_Check(arg))
773 ival = PyLong_AsUnsignedLongMask(arg);
774 else
775 return converterr("an integer", arg, msgbuf, bufsize);
776 *p = ival;
777 break;
778 }
779
780 #ifdef HAVE_LONG_LONG
781 case 'L': {/* PY_LONG_LONG */
782 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
783 PY_LONG_LONG ival;
784 if (float_argument_warning(arg))
785 return converterr("long<L>", arg, msgbuf, bufsize);
786 ival = PyLong_AsLongLong(arg);
787 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
788 return converterr("long<L>", arg, msgbuf, bufsize);
789 } else {
790 *p = ival;
791 }
792 break;
793 }
794
795 case 'K': { /* long long sized bitfield */
796 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
797 unsigned PY_LONG_LONG ival;
798 if (PyInt_Check(arg))
799 ival = PyInt_AsUnsignedLongMask(arg);
800 else if (PyLong_Check(arg))
801 ival = PyLong_AsUnsignedLongLongMask(arg);
802 else
803 return converterr("an integer", arg, msgbuf, bufsize);
804 *p = ival;
805 break;
806 }
807 #endif
808
809 case 'f': {/* float */
810 float *p = va_arg(*p_va, float *);
811 double dval = PyFloat_AsDouble(arg);
812 if (PyErr_Occurred())
813 return converterr("float<f>", arg, msgbuf, bufsize);
814 else
815 *p = (float) dval;
816 break;
817 }
818
819 case 'd': {/* double */
820 double *p = va_arg(*p_va, double *);
821 double dval = PyFloat_AsDouble(arg);
822 if (PyErr_Occurred())
823 return converterr("float<d>", arg, msgbuf, bufsize);
824 else
825 *p = dval;
826 break;
827 }
828
829 #ifndef WITHOUT_COMPLEX
830 case 'D': {/* complex double */
831 Py_complex *p = va_arg(*p_va, Py_complex *);
832 Py_complex cval;
833 cval = PyComplex_AsCComplex(arg);
834 if (PyErr_Occurred())
835 return converterr("complex<D>", arg, msgbuf, bufsize);
836 else
837 *p = cval;
838 break;
839 }
840 #endif /* WITHOUT_COMPLEX */
841
842 case 'c': {/* char */
843 char *p = va_arg(*p_va, char *);
844 if (PyString_Check(arg) && PyString_Size(arg) == 1)
845 *p = PyString_AS_STRING(arg)[0];
846 else
847 return converterr("char", arg, msgbuf, bufsize);
848 break;
849 }
850
851 case 's': {/* string */
852 if (*format == '*') {
853 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
854
855 if (PyString_Check(arg)) {
856 PyBuffer_FillInfo(p, arg,
857 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
858 1, 0);
859 }
860 #ifdef Py_USING_UNICODE
861 else if (PyUnicode_Check(arg)) {
862 uarg = UNICODE_DEFAULT_ENCODING(arg);
863 if (uarg == NULL)
864 return converterr(CONV_UNICODE,
865 arg, msgbuf, bufsize);
866 PyBuffer_FillInfo(p, arg,
867 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
868 1, 0);
869 }
870 #endif
871 else { /* any buffer-like object */
872 char *buf;
873 if (getbuffer(arg, p, &buf) < 0)
874 return converterr(buf, arg, msgbuf, bufsize);
875 }
876 if (addcleanup(p, freelist, cleanup_buffer)) {
877 return converterr(
878 "(cleanup problem)",
879 arg, msgbuf, bufsize);
880 }
881 format++;
882 } else if (*format == '#') {
883 void **p = (void **)va_arg(*p_va, char **);
884 FETCH_SIZE;
885
886 if (PyString_Check(arg)) {
887 *p = PyString_AS_STRING(arg);
888 STORE_SIZE(PyString_GET_SIZE(arg));
889 }
890 #ifdef Py_USING_UNICODE
891 else if (PyUnicode_Check(arg)) {
892 uarg = UNICODE_DEFAULT_ENCODING(arg);
893 if (uarg == NULL)
894 return converterr(CONV_UNICODE,
895 arg, msgbuf, bufsize);
896 *p = PyString_AS_STRING(uarg);
897 STORE_SIZE(PyString_GET_SIZE(uarg));
898 }
899 #endif
900 else { /* any buffer-like object */
901 char *buf;
902 Py_ssize_t count = convertbuffer(arg, p, &buf);
903 if (count < 0)
904 return converterr(buf, arg, msgbuf, bufsize);
905 STORE_SIZE(count);
906 }
907 format++;
908 } else {
909 char **p = va_arg(*p_va, char **);
910
911 if (PyString_Check(arg))
912 *p = PyString_AS_STRING(arg);
913 #ifdef Py_USING_UNICODE
914 else if (PyUnicode_Check(arg)) {
915 uarg = UNICODE_DEFAULT_ENCODING(arg);
916 if (uarg == NULL)
917 return converterr(CONV_UNICODE,
918 arg, msgbuf, bufsize);
919 *p = PyString_AS_STRING(uarg);
920 }
921 #endif
922 else
923 return converterr("string", arg, msgbuf, bufsize);
924 if ((Py_ssize_t)strlen(*p) != PyString_Size(arg))
925 return converterr("string without null bytes",
926 arg, msgbuf, bufsize);
927 }
928 break;
929 }
930
931 case 'z': {/* string, may be NULL (None) */
932 if (*format == '*') {
933 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
934
935 if (arg == Py_None)
936 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
937 else if (PyString_Check(arg)) {
938 PyBuffer_FillInfo(p, arg,
939 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
940 1, 0);
941 }
942 #ifdef Py_USING_UNICODE
943 else if (PyUnicode_Check(arg)) {
944 uarg = UNICODE_DEFAULT_ENCODING(arg);
945 if (uarg == NULL)
946 return converterr(CONV_UNICODE,
947 arg, msgbuf, bufsize);
948 PyBuffer_FillInfo(p, arg,
949 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
950 1, 0);
951 }
952 #endif
953 else { /* any buffer-like object */
954 char *buf;
955 if (getbuffer(arg, p, &buf) < 0)
956 return converterr(buf, arg, msgbuf, bufsize);
957 }
958 if (addcleanup(p, freelist, cleanup_buffer)) {
959 return converterr(
960 "(cleanup problem)",
961 arg, msgbuf, bufsize);
962 }
963 format++;
964 } else if (*format == '#') { /* any buffer-like object */
965 void **p = (void **)va_arg(*p_va, char **);
966 FETCH_SIZE;
967
968 if (arg == Py_None) {
969 *p = 0;
970 STORE_SIZE(0);
971 }
972 else if (PyString_Check(arg)) {
973 *p = PyString_AS_STRING(arg);
974 STORE_SIZE(PyString_GET_SIZE(arg));
975 }
976 #ifdef Py_USING_UNICODE
977 else if (PyUnicode_Check(arg)) {
978 uarg = UNICODE_DEFAULT_ENCODING(arg);
979 if (uarg == NULL)
980 return converterr(CONV_UNICODE,
981 arg, msgbuf, bufsize);
982 *p = PyString_AS_STRING(uarg);
983 STORE_SIZE(PyString_GET_SIZE(uarg));
984 }
985 #endif
986 else { /* any buffer-like object */
987 char *buf;
988 Py_ssize_t count = convertbuffer(arg, p, &buf);
989 if (count < 0)
990 return converterr(buf, arg, msgbuf, bufsize);
991 STORE_SIZE(count);
992 }
993 format++;
994 } else {
995 char **p = va_arg(*p_va, char **);
996
997 if (arg == Py_None)
998 *p = 0;
999 else if (PyString_Check(arg))
1000 *p = PyString_AS_STRING(arg);
1001 #ifdef Py_USING_UNICODE
1002 else if (PyUnicode_Check(arg)) {
1003 uarg = UNICODE_DEFAULT_ENCODING(arg);
1004 if (uarg == NULL)
1005 return converterr(CONV_UNICODE,
1006 arg, msgbuf, bufsize);
1007 *p = PyString_AS_STRING(uarg);
1008 }
1009 #endif
1010 else
1011 return converterr("string or None",
1012 arg, msgbuf, bufsize);
1013 if (*format == '#') {
1014 FETCH_SIZE;
1015 assert(0); /* XXX redundant with if-case */
1016 if (arg == Py_None) {
1017 STORE_SIZE(0);
1018 } else {
1019 STORE_SIZE(PyString_Size(arg));
1020 }
1021 format++;
1022 }
1023 else if (*p != NULL &&
1024 (Py_ssize_t)strlen(*p) != PyString_Size(arg))
1025 return converterr(
1026 "string without null bytes or None",
1027 arg, msgbuf, bufsize);
1028 }
1029 break;
1030 }
1031
1032 case 'e': {/* encoded string */
1033 char **buffer;
1034 const char *encoding;
1035 PyObject *s;
1036 Py_ssize_t size;
1037 int recode_strings;
1038
1039 /* Get 'e' parameter: the encoding name */
1040 encoding = (const char *)va_arg(*p_va, const char *);
1041 #ifdef Py_USING_UNICODE
1042 if (encoding == NULL)
1043 encoding = PyUnicode_GetDefaultEncoding();
1044 #endif
1045
1046 /* Get output buffer parameter:
1047 's' (recode all objects via Unicode) or
1048 't' (only recode non-string objects)
1049 */
1050 if (*format == 's')
1051 recode_strings = 1;
1052 else if (*format == 't')
1053 recode_strings = 0;
1054 else
1055 return converterr(
1056 "(unknown parser marker combination)",
1057 arg, msgbuf, bufsize);
1058 buffer = (char **)va_arg(*p_va, char **);
1059 format++;
1060 if (buffer == NULL)
1061 return converterr("(buffer is NULL)",
1062 arg, msgbuf, bufsize);
1063
1064 /* Encode object */
1065 if (!recode_strings && PyString_Check(arg)) {
1066 s = arg;
1067 Py_INCREF(s);
1068 }
1069 else {
1070 #ifdef Py_USING_UNICODE
1071 PyObject *u;
1072
1073 /* Convert object to Unicode */
1074 u = PyUnicode_FromObject(arg);
1075 if (u == NULL)
1076 return converterr(
1077 "string or unicode or text buffer",
1078 arg, msgbuf, bufsize);
1079
1080 /* Encode object; use default error handling */
1081 s = PyUnicode_AsEncodedString(u,
1082 encoding,
1083 NULL);
1084 Py_DECREF(u);
1085 if (s == NULL)
1086 return converterr("(encoding failed)",
1087 arg, msgbuf, bufsize);
1088 if (!PyString_Check(s)) {
1089 Py_DECREF(s);
1090 return converterr(
1091 "(encoder failed to return a string)",
1092 arg, msgbuf, bufsize);
1093 }
1094 #else
1095 return converterr("string<e>", arg, msgbuf, bufsize);
1096 #endif
1097 }
1098 size = PyString_GET_SIZE(s);
1099
1100 /* Write output; output is guaranteed to be 0-terminated */
1101 if (*format == '#') {
1102 /* Using buffer length parameter '#':
1103
1104 - if *buffer is NULL, a new buffer of the
1105 needed size is allocated and the data
1106 copied into it; *buffer is updated to point
1107 to the new buffer; the caller is
1108 responsible for PyMem_Free()ing it after
1109 usage
1110
1111 - if *buffer is not NULL, the data is
1112 copied to *buffer; *buffer_len has to be
1113 set to the size of the buffer on input;
1114 buffer overflow is signalled with an error;
1115 buffer has to provide enough room for the
1116 encoded string plus the trailing 0-byte
1117
1118 - in both cases, *buffer_len is updated to
1119 the size of the buffer /excluding/ the
1120 trailing 0-byte
1121
1122 */
1123 FETCH_SIZE;
1124
1125 format++;
1126 if (q == NULL && q2 == NULL) {
1127 Py_DECREF(s);
1128 return converterr(
1129 "(buffer_len is NULL)",
1130 arg, msgbuf, bufsize);
1131 }
1132 if (*buffer == NULL) {
1133 *buffer = PyMem_NEW(char, size + 1);
1134 if (*buffer == NULL) {
1135 Py_DECREF(s);
1136 return converterr(
1137 "(memory error)",
1138 arg, msgbuf, bufsize);
1139 }
1140 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1141 Py_DECREF(s);
1142 return converterr(
1143 "(cleanup problem)",
1144 arg, msgbuf, bufsize);
1145 }
1146 } else {
1147 if (size + 1 > BUFFER_LEN) {
1148 Py_DECREF(s);
1149 PyErr_Format(PyExc_TypeError,
1150 "encoded string too long "
1151 "(%zd, maximum length %zd)",
1152 (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1153 return "";
1154 }
1155 }
1156 memcpy(*buffer,
1157 PyString_AS_STRING(s),
1158 size + 1);
1159 STORE_SIZE(size);
1160 } else {
1161 /* Using a 0-terminated buffer:
1162
1163 - the encoded string has to be 0-terminated
1164 for this variant to work; if it is not, an
1165 error raised
1166
1167 - a new buffer of the needed size is
1168 allocated and the data copied into it;
1169 *buffer is updated to point to the new
1170 buffer; the caller is responsible for
1171 PyMem_Free()ing it after usage
1172
1173 */
1174 if ((Py_ssize_t)strlen(PyString_AS_STRING(s))
1175 != size) {
1176 Py_DECREF(s);
1177 return converterr(
1178 "encoded string without null bytes",
1179 arg, msgbuf, bufsize);
1180 }
1181 *buffer = PyMem_NEW(char, size + 1);
1182 if (*buffer == NULL) {
1183 Py_DECREF(s);
1184 return converterr("(memory error)",
1185 arg, msgbuf, bufsize);
1186 }
1187 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1188 Py_DECREF(s);
1189 return converterr("(cleanup problem)",
1190 arg, msgbuf, bufsize);
1191 }
1192 memcpy(*buffer,
1193 PyString_AS_STRING(s),
1194 size + 1);
1195 }
1196 Py_DECREF(s);
1197 break;
1198 }
1199
1200 #ifdef Py_USING_UNICODE
1201 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1202 if (*format == '#') { /* any buffer-like object */
1203 void **p = (void **)va_arg(*p_va, char **);
1204 FETCH_SIZE;
1205 if (PyUnicode_Check(arg)) {
1206 *p = PyUnicode_AS_UNICODE(arg);
1207 STORE_SIZE(PyUnicode_GET_SIZE(arg));
1208 }
1209 else {
1210 return converterr("cannot convert raw buffers",
1211 arg, msgbuf, bufsize);
1212 }
1213 format++;
1214 } else {
1215 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1216 if (PyUnicode_Check(arg)) {
1217 *p = PyUnicode_AS_UNICODE(arg);
1218 if (_ustrlen(*p) != (size_t)PyUnicode_GET_SIZE(arg)) {
1219 return converterr(
1220 "unicode without null characters",
1221 arg, msgbuf, bufsize);
1222 }
1223 }
1224 else
1225 return converterr("unicode", arg, msgbuf, bufsize);
1226 }
1227 break;
1228 }
1229 #endif
1230
1231 case 'S': { /* string object */
1232 PyObject **p = va_arg(*p_va, PyObject **);
1233 if (PyString_Check(arg))
1234 *p = arg;
1235 else
1236 return converterr("string", arg, msgbuf, bufsize);
1237 break;
1238 }
1239
1240 #ifdef Py_USING_UNICODE
1241 case 'U': { /* Unicode object */
1242 PyObject **p = va_arg(*p_va, PyObject **);
1243 if (PyUnicode_Check(arg))
1244 *p = arg;
1245 else
1246 return converterr("unicode", arg, msgbuf, bufsize);
1247 break;
1248 }
1249 #endif
1250
1251 case 'O': { /* object */
1252 PyTypeObject *type;
1253 PyObject **p;
1254 if (*format == '!') {
1255 type = va_arg(*p_va, PyTypeObject*);
1256 p = va_arg(*p_va, PyObject **);
1257 format++;
1258 if (PyType_IsSubtype(arg->ob_type, type))
1259 *p = arg;
1260 else
1261 return converterr(type->tp_name, arg, msgbuf, bufsize);
1262
1263 }
1264 else if (*format == '?') {
1265 inquiry pred = va_arg(*p_va, inquiry);
1266 p = va_arg(*p_va, PyObject **);
1267 format++;
1268 if ((*pred)(arg))
1269 *p = arg;
1270 else
1271 return converterr("(unspecified)",
1272 arg, msgbuf, bufsize);
1273
1274 }
1275 else if (*format == '&') {
1276 typedef int (*converter)(PyObject *, void *);
1277 converter convert = va_arg(*p_va, converter);
1278 void *addr = va_arg(*p_va, void *);
1279 format++;
1280 if (! (*convert)(arg, addr))
1281 return converterr("(unspecified)",
1282 arg, msgbuf, bufsize);
1283 }
1284 else {
1285 p = va_arg(*p_va, PyObject **);
1286 *p = arg;
1287 }
1288 break;
1289 }
1290
1291 case 'w': { /* memory buffer, read-write access */
1292 void **p = va_arg(*p_va, void **);
1293 void *res;
1294 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1295 Py_ssize_t count;
1296
1297 if (pb && pb->bf_releasebuffer && *format != '*')
1298 /* Buffer must be released, yet caller does not use
1299 the Py_buffer protocol. */
1300 return converterr("pinned buffer", arg, msgbuf, bufsize);
1301
1302 if (pb && pb->bf_getbuffer && *format == '*') {
1303 /* Caller is interested in Py_buffer, and the object
1304 supports it directly. */
1305 format++;
1306 if (pb->bf_getbuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1307 PyErr_Clear();
1308 return converterr("read-write buffer", arg, msgbuf, bufsize);
1309 }
1310 if (addcleanup(p, freelist, cleanup_buffer)) {
1311 return converterr(
1312 "(cleanup problem)",
1313 arg, msgbuf, bufsize);
1314 }
1315 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1316 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1317 break;
1318 }
1319
1320 if (pb == NULL ||
1321 pb->bf_getwritebuffer == NULL ||
1322 pb->bf_getsegcount == NULL)
1323 return converterr("read-write buffer", arg, msgbuf, bufsize);
1324 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1325 return converterr("single-segment read-write buffer",
1326 arg, msgbuf, bufsize);
1327 if ((count = pb->bf_getwritebuffer(arg, 0, &res)) < 0)
1328 return converterr("(unspecified)", arg, msgbuf, bufsize);
1329 if (*format == '*') {
1330 PyBuffer_FillInfo((Py_buffer*)p, arg, res, count, 1, 0);
1331 format++;
1332 }
1333 else {
1334 *p = res;
1335 if (*format == '#') {
1336 FETCH_SIZE;
1337 STORE_SIZE(count);
1338 format++;
1339 }
1340 }
1341 break;
1342 }
1343
1344 case 't': { /* 8-bit character buffer, read-only access */
1345 char **p = va_arg(*p_va, char **);
1346 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1347 Py_ssize_t count;
1348
1349 if (*format++ != '#')
1350 return converterr(
1351 "invalid use of 't' format character",
1352 arg, msgbuf, bufsize);
1353 if (!PyType_HasFeature(arg->ob_type,
1354 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
1355 pb == NULL || pb->bf_getcharbuffer == NULL ||
1356 pb->bf_getsegcount == NULL)
1357 return converterr(
1358 "string or read-only character buffer",
1359 arg, msgbuf, bufsize);
1360
1361 if (pb->bf_getsegcount(arg, NULL) != 1)
1362 return converterr(
1363 "string or single-segment read-only buffer",
1364 arg, msgbuf, bufsize);
1365
1366 if (pb->bf_releasebuffer)
1367 return converterr(
1368 "string or pinned buffer",
1369 arg, msgbuf, bufsize);
1370
1371 count = pb->bf_getcharbuffer(arg, 0, p);
1372 if (count < 0)
1373 return converterr("(unspecified)", arg, msgbuf, bufsize);
1374 {
1375 FETCH_SIZE;
1376 STORE_SIZE(count);
1377 }
1378 break;
1379 }
1380
1381 default:
1382 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
1383
1384 }
1385
1386 *p_format = format;
1387 return NULL;
1388 }
1389
1390 static Py_ssize_t
convertbuffer(PyObject * arg,void ** p,char ** errmsg)1391 convertbuffer(PyObject *arg, void **p, char **errmsg)
1392 {
1393 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1394 Py_ssize_t count;
1395 if (pb == NULL ||
1396 pb->bf_getreadbuffer == NULL ||
1397 pb->bf_getsegcount == NULL ||
1398 pb->bf_releasebuffer != NULL) {
1399 *errmsg = "string or read-only buffer";
1400 return -1;
1401 }
1402 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
1403 *errmsg = "string or single-segment read-only buffer";
1404 return -1;
1405 }
1406 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
1407 *errmsg = "(unspecified)";
1408 }
1409 return count;
1410 }
1411
1412 static int
getbuffer(PyObject * arg,Py_buffer * view,char ** errmsg)1413 getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
1414 {
1415 void *buf;
1416 Py_ssize_t count;
1417 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1418 if (pb == NULL) {
1419 *errmsg = "string or buffer";
1420 return -1;
1421 }
1422 if (pb->bf_getbuffer) {
1423 if (pb->bf_getbuffer(arg, view, 0) < 0) {
1424 *errmsg = "convertible to a buffer";
1425 return -1;
1426 }
1427 if (!PyBuffer_IsContiguous(view, 'C')) {
1428 *errmsg = "contiguous buffer";
1429 return -1;
1430 }
1431 return 0;
1432 }
1433
1434 count = convertbuffer(arg, &buf, errmsg);
1435 if (count < 0) {
1436 *errmsg = "convertible to a buffer";
1437 return count;
1438 }
1439 PyBuffer_FillInfo(view, arg, buf, count, 1, 0);
1440 return 0;
1441 }
1442
1443 /* Support for keyword arguments donated by
1444 Geoff Philbrick <philbric@delphi.hks.com> */
1445
1446 /* Return false (0) for error, else true. */
1447 int
PyArg_ParseTupleAndKeywords(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,...)1448 PyArg_ParseTupleAndKeywords(PyObject *args,
1449 PyObject *keywords,
1450 const char *format,
1451 char **kwlist, ...)
1452 {
1453 int retval;
1454 va_list va;
1455
1456 if ((args == NULL || !PyTuple_Check(args)) ||
1457 (keywords != NULL && !PyDict_Check(keywords)) ||
1458 format == NULL ||
1459 kwlist == NULL)
1460 {
1461 PyErr_BadInternalCall();
1462 return 0;
1463 }
1464
1465 va_start(va, kwlist);
1466 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1467 va_end(va);
1468 return retval;
1469 }
1470
1471 int
_PyArg_ParseTupleAndKeywords_SizeT(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,...)1472 _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1473 PyObject *keywords,
1474 const char *format,
1475 char **kwlist, ...)
1476 {
1477 int retval;
1478 va_list va;
1479
1480 if ((args == NULL || !PyTuple_Check(args)) ||
1481 (keywords != NULL && !PyDict_Check(keywords)) ||
1482 format == NULL ||
1483 kwlist == NULL)
1484 {
1485 PyErr_BadInternalCall();
1486 return 0;
1487 }
1488
1489 va_start(va, kwlist);
1490 retval = vgetargskeywords(args, keywords, format,
1491 kwlist, &va, FLAG_SIZE_T);
1492 va_end(va);
1493 return retval;
1494 }
1495
1496
1497 int
PyArg_VaParseTupleAndKeywords(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,va_list va)1498 PyArg_VaParseTupleAndKeywords(PyObject *args,
1499 PyObject *keywords,
1500 const char *format,
1501 char **kwlist, va_list va)
1502 {
1503 int retval;
1504 va_list lva;
1505
1506 if ((args == NULL || !PyTuple_Check(args)) ||
1507 (keywords != NULL && !PyDict_Check(keywords)) ||
1508 format == NULL ||
1509 kwlist == NULL)
1510 {
1511 PyErr_BadInternalCall();
1512 return 0;
1513 }
1514
1515 #ifdef VA_LIST_IS_ARRAY
1516 memcpy(lva, va, sizeof(va_list));
1517 #else
1518 #ifdef __va_copy
1519 __va_copy(lva, va);
1520 #else
1521 lva = va;
1522 #endif
1523 #endif
1524
1525 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1526 return retval;
1527 }
1528
1529 int
_PyArg_VaParseTupleAndKeywords_SizeT(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,va_list va)1530 _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1531 PyObject *keywords,
1532 const char *format,
1533 char **kwlist, va_list va)
1534 {
1535 int retval;
1536 va_list lva;
1537
1538 if ((args == NULL || !PyTuple_Check(args)) ||
1539 (keywords != NULL && !PyDict_Check(keywords)) ||
1540 format == NULL ||
1541 kwlist == NULL)
1542 {
1543 PyErr_BadInternalCall();
1544 return 0;
1545 }
1546
1547 #ifdef VA_LIST_IS_ARRAY
1548 memcpy(lva, va, sizeof(va_list));
1549 #else
1550 #ifdef __va_copy
1551 __va_copy(lva, va);
1552 #else
1553 lva = va;
1554 #endif
1555 #endif
1556
1557 retval = vgetargskeywords(args, keywords, format,
1558 kwlist, &lva, FLAG_SIZE_T);
1559 return retval;
1560 }
1561
1562 #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1563
1564 static int
vgetargskeywords(PyObject * args,PyObject * keywords,const char * format,char ** kwlist,va_list * p_va,int flags)1565 vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
1566 char **kwlist, va_list *p_va, int flags)
1567 {
1568 char msgbuf[512];
1569 int levels[32];
1570 const char *fname, *msg, *custom_msg, *keyword;
1571 int min = INT_MAX;
1572 int i, len, nargs, nkeywords;
1573 PyObject *freelist = NULL, *current_arg;
1574
1575 assert(args != NULL && PyTuple_Check(args));
1576 assert(keywords == NULL || PyDict_Check(keywords));
1577 assert(format != NULL);
1578 assert(kwlist != NULL);
1579 assert(p_va != NULL);
1580
1581 /* grab the function name or custom error msg first (mutually exclusive) */
1582 fname = strchr(format, ':');
1583 if (fname) {
1584 fname++;
1585 custom_msg = NULL;
1586 }
1587 else {
1588 custom_msg = strchr(format,';');
1589 if (custom_msg)
1590 custom_msg++;
1591 }
1592
1593 /* scan kwlist and get greatest possible nbr of args */
1594 for (len=0; kwlist[len]; len++)
1595 continue;
1596
1597 nargs = PyTuple_GET_SIZE(args);
1598 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1599 if (nargs + nkeywords > len) {
1600 PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
1601 "argument%s (%d given)",
1602 (fname == NULL) ? "function" : fname,
1603 (fname == NULL) ? "" : "()",
1604 len,
1605 (len == 1) ? "" : "s",
1606 nargs + nkeywords);
1607 return 0;
1608 }
1609
1610 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1611 for (i = 0; i < len; i++) {
1612 keyword = kwlist[i];
1613 if (*format == '|') {
1614 min = i;
1615 format++;
1616 }
1617 if (IS_END_OF_FORMAT(*format)) {
1618 PyErr_Format(PyExc_RuntimeError,
1619 "More keyword list entries (%d) than "
1620 "format specifiers (%d)", len, i);
1621 return cleanreturn(0, freelist);
1622 }
1623 current_arg = NULL;
1624 if (nkeywords) {
1625 current_arg = PyDict_GetItemString(keywords, keyword);
1626 }
1627 if (current_arg) {
1628 --nkeywords;
1629 if (i < nargs) {
1630 /* arg present in tuple and in dict */
1631 PyErr_Format(PyExc_TypeError,
1632 "Argument given by name ('%s') "
1633 "and position (%d)",
1634 keyword, i+1);
1635 return cleanreturn(0, freelist);
1636 }
1637 }
1638 else if (nkeywords && PyErr_Occurred())
1639 return cleanreturn(0, freelist);
1640 else if (i < nargs)
1641 current_arg = PyTuple_GET_ITEM(args, i);
1642
1643 if (current_arg) {
1644 msg = convertitem(current_arg, &format, p_va, flags,
1645 levels, msgbuf, sizeof(msgbuf), &freelist);
1646 if (msg) {
1647 seterror(i+1, msg, levels, fname, custom_msg);
1648 return cleanreturn(0, freelist);
1649 }
1650 continue;
1651 }
1652
1653 if (i < min) {
1654 PyErr_Format(PyExc_TypeError, "Required argument "
1655 "'%s' (pos %d) not found",
1656 keyword, i+1);
1657 return cleanreturn(0, freelist);
1658 }
1659 /* current code reports success when all required args
1660 * fulfilled and no keyword args left, with no further
1661 * validation. XXX Maybe skip this in debug build ?
1662 */
1663 if (!nkeywords)
1664 return cleanreturn(1, freelist);
1665
1666 /* We are into optional args, skip thru to any remaining
1667 * keyword args */
1668 msg = skipitem(&format, p_va, flags);
1669 if (msg) {
1670 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1671 format);
1672 return cleanreturn(0, freelist);
1673 }
1674 }
1675
1676 if (!IS_END_OF_FORMAT(*format) && *format != '|') {
1677 PyErr_Format(PyExc_RuntimeError,
1678 "more argument specifiers than keyword list entries "
1679 "(remaining format:'%s')", format);
1680 return cleanreturn(0, freelist);
1681 }
1682
1683 /* make sure there are no extraneous keyword arguments */
1684 if (nkeywords > 0) {
1685 PyObject *key, *value;
1686 Py_ssize_t pos = 0;
1687 while (PyDict_Next(keywords, &pos, &key, &value)) {
1688 int match = 0;
1689 char *ks;
1690 if (!PyString_Check(key)) {
1691 PyErr_SetString(PyExc_TypeError,
1692 "keywords must be strings");
1693 return cleanreturn(0, freelist);
1694 }
1695 ks = PyString_AsString(key);
1696 for (i = 0; i < len; i++) {
1697 if (!strcmp(ks, kwlist[i])) {
1698 match = 1;
1699 break;
1700 }
1701 }
1702 if (!match) {
1703 PyErr_Format(PyExc_TypeError,
1704 "'%s' is an invalid keyword "
1705 "argument for this function",
1706 ks);
1707 return cleanreturn(0, freelist);
1708 }
1709 }
1710 }
1711
1712 return cleanreturn(1, freelist);
1713 }
1714
1715
1716 static char *
skipitem(const char ** p_format,va_list * p_va,int flags)1717 skipitem(const char **p_format, va_list *p_va, int flags)
1718 {
1719 const char *format = *p_format;
1720 char c = *format++;
1721
1722 switch (c) {
1723
1724 /* simple codes
1725 * The individual types (second arg of va_arg) are irrelevant */
1726
1727 case 'b': /* byte -- very short int */
1728 case 'B': /* byte as bitfield */
1729 case 'h': /* short int */
1730 case 'H': /* short int as bitfield */
1731 case 'i': /* int */
1732 case 'I': /* int sized bitfield */
1733 case 'l': /* long int */
1734 case 'k': /* long int sized bitfield */
1735 #ifdef HAVE_LONG_LONG
1736 case 'L': /* PY_LONG_LONG */
1737 case 'K': /* PY_LONG_LONG sized bitfield */
1738 #endif
1739 case 'f': /* float */
1740 case 'd': /* double */
1741 #ifndef WITHOUT_COMPLEX
1742 case 'D': /* complex double */
1743 #endif
1744 case 'c': /* char */
1745 {
1746 (void) va_arg(*p_va, void *);
1747 break;
1748 }
1749
1750 case 'n': /* Py_ssize_t */
1751 {
1752 (void) va_arg(*p_va, Py_ssize_t *);
1753 break;
1754 }
1755
1756 /* string codes */
1757
1758 case 'e': /* string with encoding */
1759 {
1760 (void) va_arg(*p_va, const char *);
1761 if (!(*format == 's' || *format == 't'))
1762 /* after 'e', only 's' and 't' is allowed */
1763 goto err;
1764 format++;
1765 /* explicit fallthrough to string cases */
1766 }
1767
1768 case 's': /* string */
1769 case 'z': /* string or None */
1770 #ifdef Py_USING_UNICODE
1771 case 'u': /* unicode string */
1772 #endif
1773 case 't': /* buffer, read-only */
1774 case 'w': /* buffer, read-write */
1775 {
1776 (void) va_arg(*p_va, char **);
1777 if (*format == '#') {
1778 if (flags & FLAG_SIZE_T)
1779 (void) va_arg(*p_va, Py_ssize_t *);
1780 else
1781 (void) va_arg(*p_va, int *);
1782 format++;
1783 } else if ((c == 's' || c == 'z') && *format == '*') {
1784 format++;
1785 }
1786 break;
1787 }
1788
1789 /* object codes */
1790
1791 case 'S': /* string object */
1792 #ifdef Py_USING_UNICODE
1793 case 'U': /* unicode string object */
1794 #endif
1795 {
1796 (void) va_arg(*p_va, PyObject **);
1797 break;
1798 }
1799
1800 case 'O': /* object */
1801 {
1802 if (*format == '!') {
1803 format++;
1804 (void) va_arg(*p_va, PyTypeObject*);
1805 (void) va_arg(*p_va, PyObject **);
1806 }
1807 else if (*format == '&') {
1808 typedef int (*converter)(PyObject *, void *);
1809 (void) va_arg(*p_va, converter);
1810 (void) va_arg(*p_va, void *);
1811 format++;
1812 }
1813 else {
1814 (void) va_arg(*p_va, PyObject **);
1815 }
1816 break;
1817 }
1818
1819 case '(': /* bypass tuple, not handled at all previously */
1820 {
1821 char *msg;
1822 for (;;) {
1823 if (*format==')')
1824 break;
1825 if (IS_END_OF_FORMAT(*format))
1826 return "Unmatched left paren in format "
1827 "string";
1828 msg = skipitem(&format, p_va, flags);
1829 if (msg)
1830 return msg;
1831 }
1832 format++;
1833 break;
1834 }
1835
1836 case ')':
1837 return "Unmatched right paren in format string";
1838
1839 default:
1840 err:
1841 return "impossible<bad format char>";
1842
1843 }
1844
1845 *p_format = format;
1846 return NULL;
1847 }
1848
1849
1850 int
PyArg_UnpackTuple(PyObject * args,const char * name,Py_ssize_t min,Py_ssize_t max,...)1851 PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
1852 {
1853 Py_ssize_t i, l;
1854 PyObject **o;
1855 va_list vargs;
1856
1857 #ifdef HAVE_STDARG_PROTOTYPES
1858 va_start(vargs, max);
1859 #else
1860 va_start(vargs);
1861 #endif
1862
1863 assert(min >= 0);
1864 assert(min <= max);
1865 if (!PyTuple_Check(args)) {
1866 va_end(vargs);
1867 PyErr_SetString(PyExc_SystemError,
1868 "PyArg_UnpackTuple() argument list is not a tuple");
1869 return 0;
1870 }
1871 l = PyTuple_GET_SIZE(args);
1872 if (l < min) {
1873 if (name != NULL)
1874 PyErr_Format(
1875 PyExc_TypeError,
1876 "%s expected %s%zd arguments, got %zd",
1877 name, (min == max ? "" : "at least "), min, l);
1878 else
1879 PyErr_Format(
1880 PyExc_TypeError,
1881 "unpacked tuple should have %s%zd elements,"
1882 " but has %zd",
1883 (min == max ? "" : "at least "), min, l);
1884 va_end(vargs);
1885 return 0;
1886 }
1887 if (l > max) {
1888 if (name != NULL)
1889 PyErr_Format(
1890 PyExc_TypeError,
1891 "%s expected %s%zd arguments, got %zd",
1892 name, (min == max ? "" : "at most "), max, l);
1893 else
1894 PyErr_Format(
1895 PyExc_TypeError,
1896 "unpacked tuple should have %s%zd elements,"
1897 " but has %zd",
1898 (min == max ? "" : "at most "), max, l);
1899 va_end(vargs);
1900 return 0;
1901 }
1902 for (i = 0; i < l; i++) {
1903 o = va_arg(vargs, PyObject **);
1904 *o = PyTuple_GET_ITEM(args, i);
1905 }
1906 va_end(vargs);
1907 return 1;
1908 }
1909
1910
1911 /* For type constructors that don't take keyword args
1912 *
1913 * Sets a TypeError and returns 0 if the kwds dict is
1914 * not empty, returns 1 otherwise
1915 */
1916 int
_PyArg_NoKeywords(const char * funcname,PyObject * kw)1917 _PyArg_NoKeywords(const char *funcname, PyObject *kw)
1918 {
1919 if (kw == NULL)
1920 return 1;
1921 if (!PyDict_CheckExact(kw)) {
1922 PyErr_BadInternalCall();
1923 return 0;
1924 }
1925 if (PyDict_Size(kw) == 0)
1926 return 1;
1927
1928 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1929 funcname);
1930 return 0;
1931 }
1932 #ifdef __cplusplus
1933 };
1934 #endif
1935