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