Bug Summary

File:Python/getargs.c
Location:line 936, column 17
Description:Dereference of null pointer (loaded from variable 'q')

Annotated Source Code

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