Line data Source code
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
54 0 : PyArg_Parse(PyObject *args, const char *format, ...)
55 : {
56 : int retval;
57 : va_list va;
58 :
59 0 : va_start(va, format);
60 0 : retval = vgetargs1(args, format, &va, FLAG_COMPAT);
61 0 : va_end(va);
62 0 : return retval;
63 : }
64 :
65 : int
66 0 : _PyArg_Parse_SizeT(PyObject *args, char *format, ...)
67 : {
68 : int retval;
69 : va_list va;
70 :
71 0 : va_start(va, format);
72 0 : retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
73 0 : va_end(va);
74 0 : return retval;
75 : }
76 :
77 :
78 : int
79 4537 : PyArg_ParseTuple(PyObject *args, const char *format, ...)
80 : {
81 : int retval;
82 : va_list va;
83 :
84 4537 : va_start(va, format);
85 4537 : retval = vgetargs1(args, format, &va, 0);
86 4537 : va_end(va);
87 4537 : return retval;
88 : }
89 :
90 : int
91 5655 : _PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
92 : {
93 : int retval;
94 : va_list va;
95 :
96 5655 : va_start(va, format);
97 5655 : retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
98 5655 : va_end(va);
99 5655 : return retval;
100 : }
101 :
102 :
103 : int
104 0 : PyArg_VaParse(PyObject *args, const char *format, va_list va)
105 : {
106 : va_list lva;
107 :
108 : #ifdef VA_LIST_IS_ARRAY
109 0 : 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 0 : return vgetargs1(args, format, &lva, 0);
119 : }
120 :
121 : int
122 0 : _PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
123 : {
124 : va_list lva;
125 :
126 : #ifdef VA_LIST_IS_ARRAY
127 0 : 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 0 : 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
146 0 : cleanup_ptr(PyObject *self)
147 : {
148 0 : void *ptr = PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_PTR);
149 0 : if (ptr) {
150 0 : PyMem_FREE(ptr);
151 : }
152 0 : }
153 :
154 : static void
155 0 : cleanup_buffer(PyObject *self)
156 : {
157 0 : Py_buffer *ptr = (Py_buffer *)PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_BUFFER);
158 0 : if (ptr) {
159 0 : PyBuffer_Release(ptr);
160 : }
161 0 : }
162 :
163 : static int
164 84 : addcleanup(void *ptr, PyObject **freelist, PyCapsule_Destructor destr)
165 : {
166 : PyObject *cobj;
167 : const char *name;
168 :
169 84 : if (!*freelist) {
170 84 : *freelist = PyList_New(0);
171 84 : if (!*freelist) {
172 0 : destr(ptr);
173 0 : return -1;
174 : }
175 : }
176 :
177 84 : if (destr == cleanup_ptr) {
178 81 : name = GETARGS_CAPSULE_NAME_CLEANUP_PTR;
179 3 : } else if (destr == cleanup_buffer) {
180 3 : name = GETARGS_CAPSULE_NAME_CLEANUP_BUFFER;
181 : } else {
182 0 : return -1;
183 : }
184 84 : cobj = PyCapsule_New(ptr, name, destr);
185 84 : if (!cobj) {
186 0 : destr(ptr);
187 0 : return -1;
188 : }
189 84 : if (PyList_Append(*freelist, cobj)) {
190 0 : Py_DECREF(cobj);
191 0 : return -1;
192 : }
193 84 : Py_DECREF(cobj);
194 84 : return 0;
195 : }
196 :
197 : static int
198 24574 : cleanreturn(int retval, PyObject *freelist)
199 : {
200 24574 : if (freelist && retval != 0) {
201 : /* We were successful, reset the destructors so that they
202 : don't get called. */
203 84 : Py_ssize_t len = PyList_GET_SIZE(freelist), i;
204 168 : for (i = 0; i < len; i++)
205 84 : PyCapsule_SetDestructor(PyList_GET_ITEM(freelist, i), NULL);
206 : }
207 24574 : Py_XDECREF(freelist);
208 24574 : return retval;
209 : }
210 :
211 :
212 : static int
213 10192 : vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
214 : {
215 : char msgbuf[256];
216 : int levels[32];
217 10192 : const char *fname = NULL;
218 10192 : const char *message = NULL;
219 10192 : int min = -1;
220 10192 : int max = 0;
221 10192 : int level = 0;
222 10192 : int endfmt = 0;
223 10192 : const char *formatsave = format;
224 : Py_ssize_t i, len;
225 : char *msg;
226 10192 : PyObject *freelist = NULL;
227 10192 : int compat = flags & FLAG_COMPAT;
228 :
229 : assert(compat || (args != (PyObject*)NULL));
230 10192 : flags = flags & ~FLAG_COMPAT;
231 :
232 61815 : while (endfmt == 0) {
233 41431 : int c = *format++;
234 41431 : switch (c) {
235 : case '(':
236 0 : if (level == 0)
237 0 : max++;
238 0 : level++;
239 0 : if (level >= 30)
240 0 : Py_FatalError("too many tuple nesting levels "
241 : "in argument format string");
242 0 : break;
243 : case ')':
244 0 : if (level == 0)
245 0 : Py_FatalError("excess ')' in getargs format");
246 : else
247 0 : level--;
248 0 : break;
249 : case '\0':
250 1258 : endfmt = 1;
251 1258 : break;
252 : case ':':
253 7685 : fname = format;
254 7685 : endfmt = 1;
255 7685 : break;
256 : case ';':
257 1249 : message = format;
258 1249 : endfmt = 1;
259 1249 : break;
260 : default:
261 31239 : if (level == 0) {
262 31239 : if (c == 'O')
263 14616 : max++;
264 16623 : else if (isalpha(Py_CHARMASK(c))) {
265 9876 : if (c != 'e') /* skip encoded */
266 9804 : max++;
267 6747 : } else if (c == '|')
268 5799 : min = max;
269 : }
270 31239 : break;
271 : }
272 : }
273 :
274 10192 : if (level != 0)
275 0 : Py_FatalError(/* '(' */ "missing ')' in getargs format");
276 :
277 10192 : if (min < 0)
278 4393 : min = max;
279 :
280 10192 : format = formatsave;
281 :
282 10192 : if (compat) {
283 0 : if (max == 0) {
284 0 : if (args == NULL)
285 0 : return 1;
286 0 : PyOS_snprintf(msgbuf, sizeof(msgbuf),
287 : "%.200s%s takes no arguments",
288 : fname==NULL ? "function" : fname,
289 : fname==NULL ? "" : "()");
290 0 : PyErr_SetString(PyExc_TypeError, msgbuf);
291 0 : return 0;
292 : }
293 0 : else if (min == 1 && max == 1) {
294 0 : if (args == NULL) {
295 0 : PyOS_snprintf(msgbuf, sizeof(msgbuf),
296 : "%.200s%s takes at least one argument",
297 : fname==NULL ? "function" : fname,
298 : fname==NULL ? "" : "()");
299 0 : PyErr_SetString(PyExc_TypeError, msgbuf);
300 0 : return 0;
301 : }
302 0 : msg = convertitem(args, &format, p_va, flags, levels,
303 : msgbuf, sizeof(msgbuf), &freelist);
304 0 : if (msg == NULL)
305 0 : return cleanreturn(1, freelist);
306 0 : seterror(levels[0], msg, levels+1, fname, message);
307 0 : return cleanreturn(0, freelist);
308 : }
309 : else {
310 0 : PyErr_SetString(PyExc_SystemError,
311 : "old style getargs format uses new features");
312 0 : return 0;
313 : }
314 : }
315 :
316 10192 : if (!PyTuple_Check(args)) {
317 0 : PyErr_SetString(PyExc_SystemError,
318 : "new style getargs format but argument is not a tuple");
319 0 : return 0;
320 : }
321 :
322 10192 : len = PyTuple_GET_SIZE(args);
323 :
324 10192 : if (len < min || max < len) {
325 0 : if (message == NULL) {
326 0 : 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 0 : : len < min ? "at least" : "at most",
333 0 : len < min ? min : max,
334 0 : (len < min ? min : max) == 1 ? "" : "s",
335 : Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
336 0 : message = msgbuf;
337 : }
338 0 : PyErr_SetString(PyExc_TypeError, message);
339 0 : return 0;
340 : }
341 :
342 28253 : for (i = 0; i < len; i++) {
343 18061 : if (*format == '|')
344 3643 : format++;
345 18061 : msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
346 : flags, levels, msgbuf,
347 : sizeof(msgbuf), &freelist);
348 18061 : if (msg) {
349 0 : seterror(i+1, msg, levels, fname, message);
350 0 : return cleanreturn(0, freelist);
351 : }
352 : }
353 :
354 16657 : if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
355 12930 : *format != '(' &&
356 10774 : *format != '|' && *format != ':' && *format != ';') {
357 0 : PyErr_Format(PyExc_SystemError,
358 : "bad format string: %.200s", formatsave);
359 0 : return cleanreturn(0, freelist);
360 : }
361 :
362 10192 : return cleanreturn(1, freelist);
363 : }
364 :
365 :
366 :
367 : static void
368 0 : seterror(int iarg, const char *msg, int *levels, const char *fname,
369 : const char *message)
370 : {
371 : char buf[512];
372 : int i;
373 0 : char *p = buf;
374 :
375 0 : if (PyErr_Occurred())
376 0 : return;
377 0 : else if (message == NULL) {
378 0 : if (fname != NULL) {
379 0 : PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
380 0 : p += strlen(p);
381 : }
382 0 : if (iarg != 0) {
383 0 : PyOS_snprintf(p, sizeof(buf) - (p - buf),
384 : "argument %d", iarg);
385 0 : i = 0;
386 0 : p += strlen(p);
387 0 : while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
388 0 : PyOS_snprintf(p, sizeof(buf) - (p - buf),
389 0 : ", item %d", levels[i]-1);
390 0 : p += strlen(p);
391 0 : i++;
392 : }
393 : }
394 : else {
395 0 : PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
396 0 : p += strlen(p);
397 : }
398 0 : PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
399 0 : message = buf;
400 : }
401 0 : 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 *
424 0 : 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 0 : int level = 0;
429 0 : int n = 0;
430 0 : const char *format = *p_format;
431 : int i;
432 :
433 : for (;;) {
434 0 : int c = *format++;
435 0 : if (c == '(') {
436 0 : if (level == 0)
437 0 : n++;
438 0 : level++;
439 : }
440 0 : else if (c == ')') {
441 0 : if (level == 0)
442 0 : break;
443 0 : level--;
444 : }
445 0 : else if (c == ':' || c == ';' || c == '\0')
446 : break;
447 0 : else if (level == 0 && isalpha(Py_CHARMASK(c)))
448 0 : n++;
449 0 : }
450 :
451 0 : if (!PySequence_Check(arg) || PyString_Check(arg)) {
452 0 : levels[0] = 0;
453 0 : PyOS_snprintf(msgbuf, bufsize,
454 : toplevel ? "expected %d arguments, not %.50s" :
455 : "must be %d-item sequence, not %.50s",
456 : n,
457 0 : arg == Py_None ? "None" : arg->ob_type->tp_name);
458 0 : return msgbuf;
459 : }
460 :
461 0 : if ((i = PySequence_Size(arg)) != n) {
462 0 : levels[0] = 0;
463 0 : PyOS_snprintf(msgbuf, bufsize,
464 : toplevel ? "expected %d arguments, not %d" :
465 : "must be sequence of length %d, not %d",
466 : n, i);
467 0 : return msgbuf;
468 : }
469 :
470 0 : format = *p_format;
471 0 : for (i = 0; i < n; i++) {
472 : char *msg;
473 : PyObject *item;
474 0 : item = PySequence_GetItem(arg, i);
475 0 : if (item == NULL) {
476 0 : PyErr_Clear();
477 0 : levels[0] = i+1;
478 0 : levels[1] = 0;
479 0 : strncpy(msgbuf, "is not retrievable", bufsize);
480 0 : return msgbuf;
481 : }
482 0 : msg = convertitem(item, &format, p_va, flags, levels+1,
483 : msgbuf, bufsize, freelist);
484 : /* PySequence_GetItem calls tp->sq_item, which INCREFs */
485 0 : Py_XDECREF(item);
486 0 : if (msg != NULL) {
487 0 : levels[0] = i+1;
488 0 : return msg;
489 : }
490 : }
491 :
492 0 : *p_format = format;
493 0 : return NULL;
494 : }
495 :
496 :
497 : /* Convert a single item. */
498 :
499 : static char *
500 43228 : 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 43228 : const char *format = *p_format;
505 :
506 43228 : if (*format == '(' /* ')' */) {
507 0 : format++;
508 0 : msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
509 : bufsize, 0, freelist);
510 0 : if (msg == NULL)
511 0 : format++;
512 : }
513 : else {
514 43228 : msg = convertsimple(arg, &format, p_va, flags,
515 : msgbuf, bufsize, freelist);
516 43228 : if (msg != NULL)
517 0 : levels[0] = 0;
518 : }
519 43228 : if (msg == NULL)
520 43228 : *p_format = format;
521 43228 : 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 *
532 0 : converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
533 : {
534 : assert(expected != NULL);
535 : assert(arg != NULL);
536 0 : if (expected[0] == '(') {
537 0 : PyOS_snprintf(msgbuf, bufsize,
538 : "%.100s", expected);
539 0 : strncpy(msgbuf, expected, bufsize);
540 0 : msgbuf[bufsize-1] = '\0';
541 : }
542 : else {
543 0 : PyOS_snprintf(msgbuf, bufsize,
544 : "must be %.50s, not %.50s", expected,
545 0 : arg == Py_None ? "None" : arg->ob_type->tp_name);
546 : }
547 0 : 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
555 0 : float_argument_warning(PyObject *arg)
556 : {
557 0 : if (PyFloat_Check(arg) &&
558 0 : PyErr_Warn(PyExc_DeprecationWarning,
559 : "integer argument expected, got float" ))
560 0 : return 1;
561 : else
562 0 : 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
568 12852 : float_argument_error(PyObject *arg)
569 : {
570 12852 : if (PyFloat_Check(arg)) {
571 0 : PyErr_SetString(PyExc_TypeError,
572 : "integer argument expected, got float");
573 0 : return 1;
574 : }
575 : else
576 12852 : return 0;
577 : }
578 :
579 : #ifdef Py_USING_UNICODE
580 : static size_t
581 0 : _ustrlen(Py_UNICODE *u)
582 : {
583 0 : size_t i = 0;
584 0 : Py_UNICODE *v = u;
585 0 : while (*v != 0) { i++; v++; }
586 0 : 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 *
600 43228 : 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 43228 : const char *format = *p_format;
621 43228 : char c = *format++;
622 : #ifdef Py_USING_UNICODE
623 : PyObject *uarg;
624 : #endif
625 :
626 43228 : switch (c) {
627 :
628 : case 'b': { /* unsigned byte -- very short int */
629 0 : char *p = va_arg(*p_va, char *);
630 : long ival;
631 0 : if (float_argument_error(arg))
632 0 : return converterr("integer<b>", arg, msgbuf, bufsize);
633 0 : ival = PyInt_AsLong(arg);
634 0 : if (ival == -1 && PyErr_Occurred())
635 0 : return converterr("integer<b>", arg, msgbuf, bufsize);
636 0 : else if (ival < 0) {
637 0 : PyErr_SetString(PyExc_OverflowError,
638 : "unsigned byte integer is less than minimum");
639 0 : return converterr("integer<b>", arg, msgbuf, bufsize);
640 : }
641 0 : else if (ival > UCHAR_MAX) {
642 0 : PyErr_SetString(PyExc_OverflowError,
643 : "unsigned byte integer is greater than maximum");
644 0 : return converterr("integer<b>", arg, msgbuf, bufsize);
645 : }
646 : else
647 0 : *p = (unsigned char) ival;
648 0 : break;
649 : }
650 :
651 : case 'B': {/* byte sized bitfield - both signed and unsigned
652 : values allowed */
653 0 : char *p = va_arg(*p_va, char *);
654 : long ival;
655 0 : if (float_argument_error(arg))
656 0 : return converterr("integer<B>", arg, msgbuf, bufsize);
657 0 : ival = PyInt_AsUnsignedLongMask(arg);
658 0 : if (ival == -1 && PyErr_Occurred())
659 0 : return converterr("integer<B>", arg, msgbuf, bufsize);
660 : else
661 0 : *p = (unsigned char) ival;
662 0 : break;
663 : }
664 :
665 : case 'h': {/* signed short int */
666 0 : short *p = va_arg(*p_va, short *);
667 : long ival;
668 0 : if (float_argument_error(arg))
669 0 : return converterr("integer<h>", arg, msgbuf, bufsize);
670 0 : ival = PyInt_AsLong(arg);
671 0 : if (ival == -1 && PyErr_Occurred())
672 0 : return converterr("integer<h>", arg, msgbuf, bufsize);
673 0 : else if (ival < SHRT_MIN) {
674 0 : PyErr_SetString(PyExc_OverflowError,
675 : "signed short integer is less than minimum");
676 0 : return converterr("integer<h>", arg, msgbuf, bufsize);
677 : }
678 0 : else if (ival > SHRT_MAX) {
679 0 : PyErr_SetString(PyExc_OverflowError,
680 : "signed short integer is greater than maximum");
681 0 : return converterr("integer<h>", arg, msgbuf, bufsize);
682 : }
683 : else
684 0 : *p = (short) ival;
685 0 : break;
686 : }
687 :
688 : case 'H': { /* short int sized bitfield, both signed and
689 : unsigned allowed */
690 0 : unsigned short *p = va_arg(*p_va, unsigned short *);
691 : long ival;
692 0 : if (float_argument_error(arg))
693 0 : return converterr("integer<H>", arg, msgbuf, bufsize);
694 0 : ival = PyInt_AsUnsignedLongMask(arg);
695 0 : if (ival == -1 && PyErr_Occurred())
696 0 : return converterr("integer<H>", arg, msgbuf, bufsize);
697 : else
698 0 : *p = (unsigned short) ival;
699 0 : break;
700 : }
701 :
702 : case 'i': {/* signed int */
703 4773 : int *p = va_arg(*p_va, int *);
704 : long ival;
705 4773 : if (float_argument_error(arg))
706 0 : return converterr("integer<i>", arg, msgbuf, bufsize);
707 4773 : ival = PyInt_AsLong(arg);
708 4773 : if (ival == -1 && PyErr_Occurred())
709 0 : return converterr("integer<i>", arg, msgbuf, bufsize);
710 4773 : else if (ival > INT_MAX) {
711 0 : PyErr_SetString(PyExc_OverflowError,
712 : "signed integer is greater than maximum");
713 0 : return converterr("integer<i>", arg, msgbuf, bufsize);
714 : }
715 4773 : else if (ival < INT_MIN) {
716 0 : PyErr_SetString(PyExc_OverflowError,
717 : "signed integer is less than minimum");
718 0 : return converterr("integer<i>", arg, msgbuf, bufsize);
719 : }
720 : else
721 4773 : *p = ival;
722 4773 : break;
723 : }
724 :
725 : case 'I': { /* int sized bitfield, both signed and
726 : unsigned allowed */
727 0 : unsigned int *p = va_arg(*p_va, unsigned int *);
728 : unsigned int ival;
729 0 : if (float_argument_error(arg))
730 0 : return converterr("integer<I>", arg, msgbuf, bufsize);
731 0 : ival = (unsigned int)PyInt_AsUnsignedLongMask(arg);
732 0 : if (ival == (unsigned int)-1 && PyErr_Occurred())
733 0 : return converterr("integer<I>", arg, msgbuf, bufsize);
734 : else
735 0 : *p = ival;
736 0 : 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 8079 : long *p = va_arg(*p_va, long *);
756 : long ival;
757 8079 : if (float_argument_error(arg))
758 0 : return converterr("integer<l>", arg, msgbuf, bufsize);
759 8079 : ival = PyInt_AsLong(arg);
760 8079 : if (ival == -1 && PyErr_Occurred())
761 0 : return converterr("integer<l>", arg, msgbuf, bufsize);
762 : else
763 8079 : *p = ival;
764 8079 : break;
765 : }
766 :
767 : case 'k': { /* long sized bitfield */
768 0 : unsigned long *p = va_arg(*p_va, unsigned long *);
769 : unsigned long ival;
770 0 : if (PyInt_Check(arg))
771 0 : ival = PyInt_AsUnsignedLongMask(arg);
772 0 : else if (PyLong_Check(arg))
773 0 : ival = PyLong_AsUnsignedLongMask(arg);
774 : else
775 0 : return converterr("an integer", arg, msgbuf, bufsize);
776 0 : *p = ival;
777 0 : break;
778 : }
779 :
780 : #ifdef HAVE_LONG_LONG
781 : case 'L': {/* PY_LONG_LONG */
782 0 : PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
783 : PY_LONG_LONG ival;
784 0 : if (float_argument_warning(arg))
785 0 : return converterr("long<L>", arg, msgbuf, bufsize);
786 0 : ival = PyLong_AsLongLong(arg);
787 0 : if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
788 0 : return converterr("long<L>", arg, msgbuf, bufsize);
789 : } else {
790 0 : *p = ival;
791 : }
792 0 : break;
793 : }
794 :
795 : case 'K': { /* long long sized bitfield */
796 0 : unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
797 : unsigned PY_LONG_LONG ival;
798 0 : if (PyInt_Check(arg))
799 0 : ival = PyInt_AsUnsignedLongMask(arg);
800 0 : else if (PyLong_Check(arg))
801 0 : ival = PyLong_AsUnsignedLongLongMask(arg);
802 : else
803 0 : return converterr("an integer", arg, msgbuf, bufsize);
804 0 : *p = ival;
805 0 : break;
806 : }
807 : #endif
808 :
809 : case 'f': {/* float */
810 0 : float *p = va_arg(*p_va, float *);
811 0 : double dval = PyFloat_AsDouble(arg);
812 0 : if (PyErr_Occurred())
813 0 : return converterr("float<f>", arg, msgbuf, bufsize);
814 : else
815 0 : *p = (float) dval;
816 0 : break;
817 : }
818 :
819 : case 'd': {/* double */
820 0 : double *p = va_arg(*p_va, double *);
821 0 : double dval = PyFloat_AsDouble(arg);
822 0 : if (PyErr_Occurred())
823 0 : return converterr("float<d>", arg, msgbuf, bufsize);
824 : else
825 0 : *p = dval;
826 0 : break;
827 : }
828 :
829 : #ifndef WITHOUT_COMPLEX
830 : case 'D': {/* complex double */
831 0 : Py_complex *p = va_arg(*p_va, Py_complex *);
832 : Py_complex cval;
833 0 : cval = PyComplex_AsCComplex(arg);
834 0 : if (PyErr_Occurred())
835 0 : return converterr("complex<D>", arg, msgbuf, bufsize);
836 : else
837 0 : *p = cval;
838 0 : break;
839 : }
840 : #endif /* WITHOUT_COMPLEX */
841 :
842 : case 'c': {/* char */
843 0 : char *p = va_arg(*p_va, char *);
844 0 : if (PyString_Check(arg) && PyString_Size(arg) == 1)
845 0 : *p = PyString_AS_STRING(arg)[0];
846 : else
847 0 : return converterr("char", arg, msgbuf, bufsize);
848 0 : break;
849 : }
850 :
851 : case 's': {/* string */
852 1273 : if (*format == '*') {
853 3 : Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
854 :
855 3 : if (PyString_Check(arg)) {
856 6 : PyBuffer_FillInfo(p, arg,
857 3 : PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
858 : 1, 0);
859 : }
860 : #ifdef Py_USING_UNICODE
861 0 : else if (PyUnicode_Check(arg)) {
862 0 : uarg = UNICODE_DEFAULT_ENCODING(arg);
863 0 : if (uarg == NULL)
864 0 : return converterr(CONV_UNICODE,
865 : arg, msgbuf, bufsize);
866 0 : PyBuffer_FillInfo(p, arg,
867 0 : PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
868 : 1, 0);
869 : }
870 : #endif
871 : else { /* any buffer-like object */
872 : char *buf;
873 0 : if (getbuffer(arg, p, &buf) < 0)
874 0 : return converterr(buf, arg, msgbuf, bufsize);
875 : }
876 3 : if (addcleanup(p, freelist, cleanup_buffer)) {
877 0 : return converterr(
878 : "(cleanup problem)",
879 : arg, msgbuf, bufsize);
880 : }
881 3 : format++;
882 1270 : } else if (*format == '#') {
883 0 : void **p = (void **)va_arg(*p_va, char **);
884 0 : FETCH_SIZE;
885 :
886 0 : if (PyString_Check(arg)) {
887 0 : *p = PyString_AS_STRING(arg);
888 0 : STORE_SIZE(PyString_GET_SIZE(arg));
889 : }
890 : #ifdef Py_USING_UNICODE
891 0 : else if (PyUnicode_Check(arg)) {
892 0 : uarg = UNICODE_DEFAULT_ENCODING(arg);
893 0 : if (uarg == NULL)
894 0 : return converterr(CONV_UNICODE,
895 : arg, msgbuf, bufsize);
896 0 : *p = PyString_AS_STRING(uarg);
897 0 : STORE_SIZE(PyString_GET_SIZE(uarg));
898 : }
899 : #endif
900 : else { /* any buffer-like object */
901 : char *buf;
902 0 : Py_ssize_t count = convertbuffer(arg, p, &buf);
903 0 : if (count < 0)
904 0 : return converterr(buf, arg, msgbuf, bufsize);
905 0 : STORE_SIZE(count);
906 : }
907 0 : format++;
908 : } else {
909 1270 : char **p = va_arg(*p_va, char **);
910 :
911 1270 : if (PyString_Check(arg))
912 1270 : *p = PyString_AS_STRING(arg);
913 : #ifdef Py_USING_UNICODE
914 0 : else if (PyUnicode_Check(arg)) {
915 0 : uarg = UNICODE_DEFAULT_ENCODING(arg);
916 0 : if (uarg == NULL)
917 0 : return converterr(CONV_UNICODE,
918 : arg, msgbuf, bufsize);
919 0 : *p = PyString_AS_STRING(uarg);
920 : }
921 : #endif
922 : else
923 0 : return converterr("string", arg, msgbuf, bufsize);
924 1270 : if ((Py_ssize_t)strlen(*p) != PyString_Size(arg))
925 0 : return converterr("string without null bytes",
926 : arg, msgbuf, bufsize);
927 : }
928 1273 : break;
929 : }
930 :
931 : case 'z': {/* string, may be NULL (None) */
932 0 : if (*format == '*') {
933 0 : Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
934 :
935 0 : if (arg == Py_None)
936 0 : PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
937 0 : else if (PyString_Check(arg)) {
938 0 : PyBuffer_FillInfo(p, arg,
939 0 : PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
940 : 1, 0);
941 : }
942 : #ifdef Py_USING_UNICODE
943 0 : else if (PyUnicode_Check(arg)) {
944 0 : uarg = UNICODE_DEFAULT_ENCODING(arg);
945 0 : if (uarg == NULL)
946 0 : return converterr(CONV_UNICODE,
947 : arg, msgbuf, bufsize);
948 0 : PyBuffer_FillInfo(p, arg,
949 0 : PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
950 : 1, 0);
951 : }
952 : #endif
953 : else { /* any buffer-like object */
954 : char *buf;
955 0 : if (getbuffer(arg, p, &buf) < 0)
956 0 : return converterr(buf, arg, msgbuf, bufsize);
957 : }
958 0 : if (addcleanup(p, freelist, cleanup_buffer)) {
959 0 : return converterr(
960 : "(cleanup problem)",
961 : arg, msgbuf, bufsize);
962 : }
963 0 : format++;
964 0 : } else if (*format == '#') { /* any buffer-like object */
965 0 : void **p = (void **)va_arg(*p_va, char **);
966 0 : FETCH_SIZE;
967 :
968 0 : if (arg == Py_None) {
969 0 : *p = 0;
970 0 : STORE_SIZE(0);
971 : }
972 0 : else if (PyString_Check(arg)) {
973 0 : *p = PyString_AS_STRING(arg);
974 0 : STORE_SIZE(PyString_GET_SIZE(arg));
975 : }
976 : #ifdef Py_USING_UNICODE
977 0 : else if (PyUnicode_Check(arg)) {
978 0 : uarg = UNICODE_DEFAULT_ENCODING(arg);
979 0 : if (uarg == NULL)
980 0 : return converterr(CONV_UNICODE,
981 : arg, msgbuf, bufsize);
982 0 : *p = PyString_AS_STRING(uarg);
983 0 : STORE_SIZE(PyString_GET_SIZE(uarg));
984 : }
985 : #endif
986 : else { /* any buffer-like object */
987 : char *buf;
988 0 : Py_ssize_t count = convertbuffer(arg, p, &buf);
989 0 : if (count < 0)
990 0 : return converterr(buf, arg, msgbuf, bufsize);
991 0 : STORE_SIZE(count);
992 : }
993 0 : format++;
994 : } else {
995 0 : char **p = va_arg(*p_va, char **);
996 :
997 0 : if (arg == Py_None)
998 0 : *p = 0;
999 0 : else if (PyString_Check(arg))
1000 0 : *p = PyString_AS_STRING(arg);
1001 : #ifdef Py_USING_UNICODE
1002 0 : else if (PyUnicode_Check(arg)) {
1003 0 : uarg = UNICODE_DEFAULT_ENCODING(arg);
1004 0 : if (uarg == NULL)
1005 0 : return converterr(CONV_UNICODE,
1006 : arg, msgbuf, bufsize);
1007 0 : *p = PyString_AS_STRING(uarg);
1008 : }
1009 : #endif
1010 : else
1011 0 : return converterr("string or None",
1012 : arg, msgbuf, bufsize);
1013 0 : if (*format == '#') {
1014 0 : FETCH_SIZE;
1015 : assert(0); /* XXX redundant with if-case */
1016 0 : if (arg == Py_None) {
1017 0 : STORE_SIZE(0);
1018 : } else {
1019 0 : STORE_SIZE(PyString_Size(arg));
1020 : }
1021 0 : format++;
1022 : }
1023 0 : else if (*p != NULL &&
1024 0 : (Py_ssize_t)strlen(*p) != PyString_Size(arg))
1025 0 : return converterr(
1026 : "string without null bytes or None",
1027 : arg, msgbuf, bufsize);
1028 : }
1029 0 : 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 81 : encoding = (const char *)va_arg(*p_va, const char *);
1041 : #ifdef Py_USING_UNICODE
1042 81 : if (encoding == NULL)
1043 0 : 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 81 : if (*format == 's')
1051 0 : recode_strings = 1;
1052 81 : else if (*format == 't')
1053 81 : recode_strings = 0;
1054 : else
1055 0 : return converterr(
1056 : "(unknown parser marker combination)",
1057 : arg, msgbuf, bufsize);
1058 81 : buffer = (char **)va_arg(*p_va, char **);
1059 81 : format++;
1060 81 : if (buffer == NULL)
1061 0 : return converterr("(buffer is NULL)",
1062 : arg, msgbuf, bufsize);
1063 :
1064 : /* Encode object */
1065 81 : if (!recode_strings && PyString_Check(arg)) {
1066 81 : s = arg;
1067 81 : Py_INCREF(s);
1068 : }
1069 : else {
1070 : #ifdef Py_USING_UNICODE
1071 : PyObject *u;
1072 :
1073 : /* Convert object to Unicode */
1074 0 : u = PyUnicode_FromObject(arg);
1075 0 : if (u == NULL)
1076 0 : return converterr(
1077 : "string or unicode or text buffer",
1078 : arg, msgbuf, bufsize);
1079 :
1080 : /* Encode object; use default error handling */
1081 0 : s = PyUnicode_AsEncodedString(u,
1082 : encoding,
1083 : NULL);
1084 0 : Py_DECREF(u);
1085 0 : if (s == NULL)
1086 0 : return converterr("(encoding failed)",
1087 : arg, msgbuf, bufsize);
1088 0 : if (!PyString_Check(s)) {
1089 0 : Py_DECREF(s);
1090 0 : 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 81 : size = PyString_GET_SIZE(s);
1099 :
1100 : /* Write output; output is guaranteed to be 0-terminated */
1101 81 : 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 0 : FETCH_SIZE;
1124 :
1125 0 : format++;
1126 0 : if (q == NULL && q2 == NULL) {
1127 0 : Py_DECREF(s);
1128 0 : return converterr(
1129 : "(buffer_len is NULL)",
1130 : arg, msgbuf, bufsize);
1131 : }
1132 0 : if (*buffer == NULL) {
1133 0 : *buffer = PyMem_NEW(char, size + 1);
1134 0 : if (*buffer == NULL) {
1135 0 : Py_DECREF(s);
1136 0 : return converterr(
1137 : "(memory error)",
1138 : arg, msgbuf, bufsize);
1139 : }
1140 0 : if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1141 0 : Py_DECREF(s);
1142 0 : return converterr(
1143 : "(cleanup problem)",
1144 : arg, msgbuf, bufsize);
1145 : }
1146 : } else {
1147 0 : if (size + 1 > BUFFER_LEN) {
1148 0 : Py_DECREF(s);
1149 0 : PyErr_Format(PyExc_TypeError,
1150 : "encoded string too long "
1151 : "(%zd, maximum length %zd)",
1152 0 : (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1153 0 : return "";
1154 : }
1155 : }
1156 0 : memcpy(*buffer,
1157 0 : PyString_AS_STRING(s),
1158 0 : size + 1);
1159 0 : 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 81 : if ((Py_ssize_t)strlen(PyString_AS_STRING(s))
1175 : != size) {
1176 0 : Py_DECREF(s);
1177 0 : return converterr(
1178 : "encoded string without null bytes",
1179 : arg, msgbuf, bufsize);
1180 : }
1181 81 : *buffer = PyMem_NEW(char, size + 1);
1182 81 : if (*buffer == NULL) {
1183 0 : Py_DECREF(s);
1184 0 : return converterr("(memory error)",
1185 : arg, msgbuf, bufsize);
1186 : }
1187 81 : if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1188 0 : Py_DECREF(s);
1189 0 : return converterr("(cleanup problem)",
1190 : arg, msgbuf, bufsize);
1191 : }
1192 162 : memcpy(*buffer,
1193 81 : PyString_AS_STRING(s),
1194 81 : size + 1);
1195 : }
1196 81 : Py_DECREF(s);
1197 81 : break;
1198 : }
1199 :
1200 : #ifdef Py_USING_UNICODE
1201 : case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1202 0 : if (*format == '#') { /* any buffer-like object */
1203 0 : void **p = (void **)va_arg(*p_va, char **);
1204 0 : FETCH_SIZE;
1205 0 : if (PyUnicode_Check(arg)) {
1206 0 : *p = PyUnicode_AS_UNICODE(arg);
1207 0 : STORE_SIZE(PyUnicode_GET_SIZE(arg));
1208 : }
1209 : else {
1210 0 : return converterr("cannot convert raw buffers",
1211 : arg, msgbuf, bufsize);
1212 : }
1213 0 : format++;
1214 : } else {
1215 0 : Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1216 0 : if (PyUnicode_Check(arg)) {
1217 0 : *p = PyUnicode_AS_UNICODE(arg);
1218 0 : if (_ustrlen(*p) != (size_t)PyUnicode_GET_SIZE(arg)) {
1219 0 : return converterr(
1220 : "unicode without null characters",
1221 : arg, msgbuf, bufsize);
1222 : }
1223 : }
1224 : else
1225 0 : return converterr("unicode", arg, msgbuf, bufsize);
1226 : }
1227 0 : break;
1228 : }
1229 : #endif
1230 :
1231 : case 'S': { /* string object */
1232 1062 : PyObject **p = va_arg(*p_va, PyObject **);
1233 1062 : if (PyString_Check(arg))
1234 1062 : *p = arg;
1235 : else
1236 0 : return converterr("string", arg, msgbuf, bufsize);
1237 1062 : break;
1238 : }
1239 :
1240 : #ifdef Py_USING_UNICODE
1241 : case 'U': { /* Unicode object */
1242 0 : PyObject **p = va_arg(*p_va, PyObject **);
1243 0 : if (PyUnicode_Check(arg))
1244 0 : *p = arg;
1245 : else
1246 0 : return converterr("unicode", arg, msgbuf, bufsize);
1247 0 : break;
1248 : }
1249 : #endif
1250 :
1251 : case 'O': { /* object */
1252 : PyTypeObject *type;
1253 : PyObject **p;
1254 27954 : if (*format == '!') {
1255 2517 : type = va_arg(*p_va, PyTypeObject*);
1256 2517 : p = va_arg(*p_va, PyObject **);
1257 2517 : format++;
1258 2517 : if (PyType_IsSubtype(arg->ob_type, type))
1259 2517 : *p = arg;
1260 : else
1261 0 : return converterr(type->tp_name, arg, msgbuf, bufsize);
1262 :
1263 : }
1264 25437 : else if (*format == '?') {
1265 0 : inquiry pred = va_arg(*p_va, inquiry);
1266 0 : p = va_arg(*p_va, PyObject **);
1267 0 : format++;
1268 0 : if ((*pred)(arg))
1269 0 : *p = arg;
1270 : else
1271 0 : return converterr("(unspecified)",
1272 : arg, msgbuf, bufsize);
1273 :
1274 : }
1275 25437 : else if (*format == '&') {
1276 : typedef int (*converter)(PyObject *, void *);
1277 0 : converter convert = va_arg(*p_va, converter);
1278 0 : void *addr = va_arg(*p_va, void *);
1279 0 : format++;
1280 0 : if (! (*convert)(arg, addr))
1281 0 : return converterr("(unspecified)",
1282 : arg, msgbuf, bufsize);
1283 : }
1284 : else {
1285 25437 : p = va_arg(*p_va, PyObject **);
1286 25437 : *p = arg;
1287 : }
1288 27954 : break;
1289 : }
1290 :
1291 : case 'w': { /* memory buffer, read-write access */
1292 0 : void **p = va_arg(*p_va, void **);
1293 : void *res;
1294 0 : PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1295 : Py_ssize_t count;
1296 :
1297 0 : if (pb && pb->bf_releasebuffer && *format != '*')
1298 : /* Buffer must be released, yet caller does not use
1299 : the Py_buffer protocol. */
1300 0 : return converterr("pinned buffer", arg, msgbuf, bufsize);
1301 :
1302 0 : if (pb && pb->bf_getbuffer && *format == '*') {
1303 : /* Caller is interested in Py_buffer, and the object
1304 : supports it directly. */
1305 0 : format++;
1306 0 : if (pb->bf_getbuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1307 0 : PyErr_Clear();
1308 0 : return converterr("read-write buffer", arg, msgbuf, bufsize);
1309 : }
1310 0 : if (addcleanup(p, freelist, cleanup_buffer)) {
1311 0 : return converterr(
1312 : "(cleanup problem)",
1313 : arg, msgbuf, bufsize);
1314 : }
1315 0 : if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1316 0 : return converterr("contiguous buffer", arg, msgbuf, bufsize);
1317 0 : break;
1318 : }
1319 :
1320 0 : if (pb == NULL ||
1321 0 : pb->bf_getwritebuffer == NULL ||
1322 0 : pb->bf_getsegcount == NULL)
1323 0 : return converterr("read-write buffer", arg, msgbuf, bufsize);
1324 0 : if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1325 0 : return converterr("single-segment read-write buffer",
1326 : arg, msgbuf, bufsize);
1327 0 : if ((count = pb->bf_getwritebuffer(arg, 0, &res)) < 0)
1328 0 : return converterr("(unspecified)", arg, msgbuf, bufsize);
1329 0 : if (*format == '*') {
1330 0 : PyBuffer_FillInfo((Py_buffer*)p, arg, res, count, 1, 0);
1331 0 : format++;
1332 : }
1333 : else {
1334 0 : *p = res;
1335 0 : if (*format == '#') {
1336 0 : FETCH_SIZE;
1337 0 : STORE_SIZE(count);
1338 0 : format++;
1339 : }
1340 : }
1341 0 : break;
1342 : }
1343 :
1344 : case 't': { /* 8-bit character buffer, read-only access */
1345 6 : char **p = va_arg(*p_va, char **);
1346 6 : PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1347 : Py_ssize_t count;
1348 :
1349 6 : if (*format++ != '#')
1350 0 : return converterr(
1351 : "invalid use of 't' format character",
1352 : arg, msgbuf, bufsize);
1353 6 : if (!PyType_HasFeature(arg->ob_type,
1354 6 : Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
1355 12 : pb == NULL || pb->bf_getcharbuffer == NULL ||
1356 6 : pb->bf_getsegcount == NULL)
1357 0 : return converterr(
1358 : "string or read-only character buffer",
1359 : arg, msgbuf, bufsize);
1360 :
1361 6 : if (pb->bf_getsegcount(arg, NULL) != 1)
1362 0 : return converterr(
1363 : "string or single-segment read-only buffer",
1364 : arg, msgbuf, bufsize);
1365 :
1366 6 : if (pb->bf_releasebuffer)
1367 0 : return converterr(
1368 : "string or pinned buffer",
1369 : arg, msgbuf, bufsize);
1370 :
1371 6 : count = pb->bf_getcharbuffer(arg, 0, p);
1372 6 : if (count < 0)
1373 0 : return converterr("(unspecified)", arg, msgbuf, bufsize);
1374 : {
1375 6 : FETCH_SIZE;
1376 6 : STORE_SIZE(count);
1377 : }
1378 6 : break;
1379 : }
1380 :
1381 : default:
1382 0 : return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
1383 :
1384 : }
1385 :
1386 43228 : *p_format = format;
1387 43228 : return NULL;
1388 : }
1389 :
1390 : static Py_ssize_t
1391 0 : convertbuffer(PyObject *arg, void **p, char **errmsg)
1392 : {
1393 0 : PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1394 : Py_ssize_t count;
1395 0 : if (pb == NULL ||
1396 0 : pb->bf_getreadbuffer == NULL ||
1397 0 : pb->bf_getsegcount == NULL ||
1398 0 : pb->bf_releasebuffer != NULL) {
1399 0 : *errmsg = "string or read-only buffer";
1400 0 : return -1;
1401 : }
1402 0 : if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
1403 0 : *errmsg = "string or single-segment read-only buffer";
1404 0 : return -1;
1405 : }
1406 0 : if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
1407 0 : *errmsg = "(unspecified)";
1408 : }
1409 0 : return count;
1410 : }
1411 :
1412 : static int
1413 0 : getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
1414 : {
1415 : void *buf;
1416 : Py_ssize_t count;
1417 0 : PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1418 0 : if (pb == NULL) {
1419 0 : *errmsg = "string or buffer";
1420 0 : return -1;
1421 : }
1422 0 : if (pb->bf_getbuffer) {
1423 0 : if (pb->bf_getbuffer(arg, view, 0) < 0) {
1424 0 : *errmsg = "convertible to a buffer";
1425 0 : return -1;
1426 : }
1427 0 : if (!PyBuffer_IsContiguous(view, 'C')) {
1428 0 : *errmsg = "contiguous buffer";
1429 0 : return -1;
1430 : }
1431 0 : return 0;
1432 : }
1433 :
1434 0 : count = convertbuffer(arg, &buf, errmsg);
1435 0 : if (count < 0) {
1436 0 : *errmsg = "convertible to a buffer";
1437 0 : return count;
1438 : }
1439 0 : PyBuffer_FillInfo(view, arg, buf, count, 1, 0);
1440 0 : 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
1448 10452 : PyArg_ParseTupleAndKeywords(PyObject *args,
1449 : PyObject *keywords,
1450 : const char *format,
1451 : char **kwlist, ...)
1452 : {
1453 : int retval;
1454 : va_list va;
1455 :
1456 10452 : if ((args == NULL || !PyTuple_Check(args)) ||
1457 117 : (keywords != NULL && !PyDict_Check(keywords)) ||
1458 10452 : format == NULL ||
1459 : kwlist == NULL)
1460 : {
1461 0 : PyErr_BadInternalCall();
1462 0 : return 0;
1463 : }
1464 :
1465 10452 : va_start(va, kwlist);
1466 10452 : retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1467 10452 : va_end(va);
1468 10452 : return retval;
1469 : }
1470 :
1471 : int
1472 3930 : _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 3930 : if ((args == NULL || !PyTuple_Check(args)) ||
1481 0 : (keywords != NULL && !PyDict_Check(keywords)) ||
1482 3930 : format == NULL ||
1483 : kwlist == NULL)
1484 : {
1485 0 : PyErr_BadInternalCall();
1486 0 : return 0;
1487 : }
1488 :
1489 3930 : va_start(va, kwlist);
1490 3930 : retval = vgetargskeywords(args, keywords, format,
1491 : kwlist, &va, FLAG_SIZE_T);
1492 3930 : va_end(va);
1493 3930 : return retval;
1494 : }
1495 :
1496 :
1497 : int
1498 0 : 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 0 : if ((args == NULL || !PyTuple_Check(args)) ||
1507 0 : (keywords != NULL && !PyDict_Check(keywords)) ||
1508 0 : format == NULL ||
1509 : kwlist == NULL)
1510 : {
1511 0 : PyErr_BadInternalCall();
1512 0 : return 0;
1513 : }
1514 :
1515 : #ifdef VA_LIST_IS_ARRAY
1516 0 : 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 0 : retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1526 0 : return retval;
1527 : }
1528 :
1529 : int
1530 0 : _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 0 : if ((args == NULL || !PyTuple_Check(args)) ||
1539 0 : (keywords != NULL && !PyDict_Check(keywords)) ||
1540 0 : format == NULL ||
1541 : kwlist == NULL)
1542 : {
1543 0 : PyErr_BadInternalCall();
1544 0 : return 0;
1545 : }
1546 :
1547 : #ifdef VA_LIST_IS_ARRAY
1548 0 : 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 0 : retval = vgetargskeywords(args, keywords, format,
1558 : kwlist, &lva, FLAG_SIZE_T);
1559 0 : return retval;
1560 : }
1561 :
1562 : #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
1563 :
1564 : static int
1565 14382 : 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 14382 : int min = INT_MAX;
1572 : int i, len, nargs, nkeywords;
1573 14382 : 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 14382 : fname = strchr(format, ':');
1583 14382 : if (fname) {
1584 14118 : fname++;
1585 14118 : custom_msg = NULL;
1586 : }
1587 : else {
1588 264 : custom_msg = strchr(format,';');
1589 264 : if (custom_msg)
1590 0 : custom_msg++;
1591 : }
1592 :
1593 : /* scan kwlist and get greatest possible nbr of args */
1594 47940 : for (len=0; kwlist[len]; len++)
1595 33558 : continue;
1596 :
1597 14382 : nargs = PyTuple_GET_SIZE(args);
1598 14382 : nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1599 14382 : if (nargs + nkeywords > len) {
1600 0 : 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 0 : return 0;
1608 : }
1609 :
1610 : /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1611 39783 : for (i = 0; i < len; i++) {
1612 30630 : keyword = kwlist[i];
1613 30630 : if (*format == '|') {
1614 13128 : min = i;
1615 13128 : format++;
1616 : }
1617 30630 : if (IS_END_OF_FORMAT(*format)) {
1618 0 : PyErr_Format(PyExc_RuntimeError,
1619 : "More keyword list entries (%d) than "
1620 : "format specifiers (%d)", len, i);
1621 0 : return cleanreturn(0, freelist);
1622 : }
1623 30630 : current_arg = NULL;
1624 30630 : if (nkeywords) {
1625 471 : current_arg = PyDict_GetItemString(keywords, keyword);
1626 : }
1627 30630 : if (current_arg) {
1628 120 : --nkeywords;
1629 120 : if (i < nargs) {
1630 : /* arg present in tuple and in dict */
1631 0 : PyErr_Format(PyExc_TypeError,
1632 : "Argument given by name ('%s') "
1633 : "and position (%d)",
1634 : keyword, i+1);
1635 0 : return cleanreturn(0, freelist);
1636 : }
1637 : }
1638 30510 : else if (nkeywords && PyErr_Occurred())
1639 0 : return cleanreturn(0, freelist);
1640 30510 : else if (i < nargs)
1641 25047 : current_arg = PyTuple_GET_ITEM(args, i);
1642 :
1643 30630 : if (current_arg) {
1644 25167 : msg = convertitem(current_arg, &format, p_va, flags,
1645 : levels, msgbuf, sizeof(msgbuf), &freelist);
1646 25167 : if (msg) {
1647 0 : seterror(i+1, msg, levels, fname, custom_msg);
1648 0 : return cleanreturn(0, freelist);
1649 : }
1650 25167 : continue;
1651 : }
1652 :
1653 5463 : if (i < min) {
1654 0 : PyErr_Format(PyExc_TypeError, "Required argument "
1655 : "'%s' (pos %d) not found",
1656 : keyword, i+1);
1657 0 : 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 5463 : if (!nkeywords)
1664 5229 : return cleanreturn(1, freelist);
1665 :
1666 : /* We are into optional args, skip thru to any remaining
1667 : * keyword args */
1668 234 : msg = skipitem(&format, p_va, flags);
1669 234 : if (msg) {
1670 0 : PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1671 : format);
1672 0 : return cleanreturn(0, freelist);
1673 : }
1674 : }
1675 :
1676 9153 : if (!IS_END_OF_FORMAT(*format) && *format != '|') {
1677 0 : PyErr_Format(PyExc_RuntimeError,
1678 : "more argument specifiers than keyword list entries "
1679 : "(remaining format:'%s')", format);
1680 0 : return cleanreturn(0, freelist);
1681 : }
1682 :
1683 : /* make sure there are no extraneous keyword arguments */
1684 9153 : if (nkeywords > 0) {
1685 : PyObject *key, *value;
1686 0 : Py_ssize_t pos = 0;
1687 0 : while (PyDict_Next(keywords, &pos, &key, &value)) {
1688 0 : int match = 0;
1689 : char *ks;
1690 0 : if (!PyString_Check(key)) {
1691 0 : PyErr_SetString(PyExc_TypeError,
1692 : "keywords must be strings");
1693 0 : return cleanreturn(0, freelist);
1694 : }
1695 0 : ks = PyString_AsString(key);
1696 0 : for (i = 0; i < len; i++) {
1697 0 : if (!strcmp(ks, kwlist[i])) {
1698 0 : match = 1;
1699 0 : break;
1700 : }
1701 : }
1702 0 : if (!match) {
1703 0 : PyErr_Format(PyExc_TypeError,
1704 : "'%s' is an invalid keyword "
1705 : "argument for this function",
1706 : ks);
1707 0 : return cleanreturn(0, freelist);
1708 : }
1709 : }
1710 : }
1711 :
1712 9153 : return cleanreturn(1, freelist);
1713 : }
1714 :
1715 :
1716 : static char *
1717 234 : skipitem(const char **p_format, va_list *p_va, int flags)
1718 : {
1719 234 : const char *format = *p_format;
1720 234 : char c = *format++;
1721 :
1722 234 : 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 0 : (void) va_arg(*p_va, void *);
1747 0 : break;
1748 : }
1749 :
1750 : case 'n': /* Py_ssize_t */
1751 : {
1752 0 : (void) va_arg(*p_va, Py_ssize_t *);
1753 0 : break;
1754 : }
1755 :
1756 : /* string codes */
1757 :
1758 : case 'e': /* string with encoding */
1759 : {
1760 0 : (void) va_arg(*p_va, const char *);
1761 0 : if (!(*format == 's' || *format == 't'))
1762 : /* after 'e', only 's' and 't' is allowed */
1763 0 : goto err;
1764 0 : 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 0 : (void) va_arg(*p_va, char **);
1777 0 : if (*format == '#') {
1778 0 : if (flags & FLAG_SIZE_T)
1779 0 : (void) va_arg(*p_va, Py_ssize_t *);
1780 : else
1781 0 : (void) va_arg(*p_va, int *);
1782 0 : format++;
1783 0 : } else if ((c == 's' || c == 'z') && *format == '*') {
1784 0 : format++;
1785 : }
1786 0 : 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 0 : (void) va_arg(*p_va, PyObject **);
1797 0 : break;
1798 : }
1799 :
1800 : case 'O': /* object */
1801 : {
1802 234 : if (*format == '!') {
1803 0 : format++;
1804 0 : (void) va_arg(*p_va, PyTypeObject*);
1805 0 : (void) va_arg(*p_va, PyObject **);
1806 : }
1807 234 : else if (*format == '&') {
1808 : typedef int (*converter)(PyObject *, void *);
1809 0 : (void) va_arg(*p_va, converter);
1810 0 : (void) va_arg(*p_va, void *);
1811 0 : format++;
1812 : }
1813 : else {
1814 234 : (void) va_arg(*p_va, PyObject **);
1815 : }
1816 234 : break;
1817 : }
1818 :
1819 : case '(': /* bypass tuple, not handled at all previously */
1820 : {
1821 : char *msg;
1822 : for (;;) {
1823 0 : if (*format==')')
1824 0 : break;
1825 0 : if (IS_END_OF_FORMAT(*format))
1826 0 : return "Unmatched left paren in format "
1827 : "string";
1828 0 : msg = skipitem(&format, p_va, flags);
1829 0 : if (msg)
1830 0 : return msg;
1831 0 : }
1832 0 : format++;
1833 0 : break;
1834 : }
1835 :
1836 : case ')':
1837 0 : return "Unmatched right paren in format string";
1838 :
1839 : default:
1840 : err:
1841 0 : return "impossible<bad format char>";
1842 :
1843 : }
1844 :
1845 234 : *p_format = format;
1846 234 : return NULL;
1847 : }
1848 :
1849 :
1850 : int
1851 34743 : 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 34743 : va_start(vargs, max);
1859 : #else
1860 : va_start(vargs);
1861 : #endif
1862 :
1863 : assert(min >= 0);
1864 : assert(min <= max);
1865 34743 : if (!PyTuple_Check(args)) {
1866 0 : va_end(vargs);
1867 0 : PyErr_SetString(PyExc_SystemError,
1868 : "PyArg_UnpackTuple() argument list is not a tuple");
1869 0 : return 0;
1870 : }
1871 34743 : l = PyTuple_GET_SIZE(args);
1872 34743 : if (l < min) {
1873 0 : if (name != NULL)
1874 0 : PyErr_Format(
1875 : PyExc_TypeError,
1876 : "%s expected %s%zd arguments, got %zd",
1877 : name, (min == max ? "" : "at least "), min, l);
1878 : else
1879 0 : 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 0 : va_end(vargs);
1885 0 : return 0;
1886 : }
1887 34743 : if (l > max) {
1888 0 : if (name != NULL)
1889 0 : PyErr_Format(
1890 : PyExc_TypeError,
1891 : "%s expected %s%zd arguments, got %zd",
1892 : name, (min == max ? "" : "at most "), max, l);
1893 : else
1894 0 : 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 0 : va_end(vargs);
1900 0 : return 0;
1901 : }
1902 88930 : for (i = 0; i < l; i++) {
1903 54187 : o = va_arg(vargs, PyObject **);
1904 54187 : *o = PyTuple_GET_ITEM(args, i);
1905 : }
1906 34743 : va_end(vargs);
1907 34743 : 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
1917 3807 : _PyArg_NoKeywords(const char *funcname, PyObject *kw)
1918 : {
1919 3807 : if (kw == NULL)
1920 3807 : return 1;
1921 0 : if (!PyDict_CheckExact(kw)) {
1922 0 : PyErr_BadInternalCall();
1923 0 : return 0;
1924 : }
1925 0 : if (PyDict_Size(kw) == 0)
1926 0 : return 1;
1927 :
1928 0 : PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1929 : funcname);
1930 0 : return 0;
1931 : }
1932 : #ifdef __cplusplus
1933 : };
1934 : #endif
|