blob: 17d5993c8ab914ec3fff507551eba037a7ea79ff [file] [log] [blame]
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001
2/* New getargs implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00005
Guido van Rossumc1d50531996-08-21 23:38:24 +00006#include <ctype.h>
7
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00008
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009#ifdef __cplusplus
Guido van Rossum98297ee2007-11-06 21:34:58 +000010extern "C" {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011#endif
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000012int PyArg_Parse(PyObject *, const char *, ...);
13int PyArg_ParseTuple(PyObject *, const char *, ...);
14int PyArg_VaParse(PyObject *, const char *, va_list);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000015
Tim Petersdbd9ba62000-07-09 03:09:57 +000016int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 const char *, char **, ...);
Brett Cannon711e7d92004-07-10 22:20:32 +000018int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 const char *, char **, va_list);
Brett Cannon711e7d92004-07-10 22:20:32 +000020
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000021#ifdef HAVE_DECLSPEC_DLL
22/* Export functions */
23PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, char *, ...);
24PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, char *, ...);
25PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
26 const char *, char **, ...);
27PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
28PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, char *, va_list);
29PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
30 const char *, char **, va_list);
31#endif
32
Martin v. Löwis18e16552006-02-15 17:27:45 +000033#define FLAG_COMPAT 1
34#define FLAG_SIZE_T 2
35
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000036
37/* Forward */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000038static int vgetargs1(PyObject *, const char *, va_list *, int);
39static void seterror(int, const char *, int *, const char *, const char *);
Guido van Rossum98297ee2007-11-06 21:34:58 +000040static char *convertitem(PyObject *, const char **, va_list *, int, int *,
Martin v. Löwis18e16552006-02-15 17:27:45 +000041 char *, size_t, PyObject **);
42static char *converttuple(PyObject *, const char **, va_list *, int,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 int *, char *, size_t, int, PyObject **);
Martin v. Löwis18e16552006-02-15 17:27:45 +000044static char *convertsimple(PyObject *, const char **, va_list *, int, char *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 size_t, PyObject **);
Martin v. Löwis18e16552006-02-15 17:27:45 +000046static Py_ssize_t convertbuffer(PyObject *, void **p, char **);
Martin v. Löwis423be952008-08-13 15:53:07 +000047static int getbuffer(PyObject *, Py_buffer *, char**);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000048
Tim Petersdbd9ba62000-07-09 03:09:57 +000049static int vgetargskeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 const char *, char **, va_list *, int);
Martin v. Löwis18e16552006-02-15 17:27:45 +000051static char *skipitem(const char **, va_list *, int);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000052
Fred Drake563dfc22001-10-23 14:41:08 +000053int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000054PyArg_Parse(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 int retval;
57 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 va_start(va, format);
60 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
61 va_end(va);
62 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +000063}
64
65int
66_PyArg_Parse_SizeT(PyObject *args, char *format, ...)
67{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 int retval;
69 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 va_start(va, format);
72 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
73 va_end(va);
74 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000075}
76
77
Fred Drake563dfc22001-10-23 14:41:08 +000078int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000079PyArg_ParseTuple(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 int retval;
82 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 va_start(va, format);
85 retval = vgetargs1(args, format, &va, 0);
86 va_end(va);
87 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000088}
89
Martin v. Löwis18e16552006-02-15 17:27:45 +000090int
91_PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
92{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 int retval;
94 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 va_start(va, format);
97 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
98 va_end(va);
99 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000100}
101
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000102
103int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000104PyArg_VaParse(PyObject *args, const char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 va_list lva;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000107
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000108 Py_VA_COPY(lva, va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000111}
112
Martin v. Löwis18e16552006-02-15 17:27:45 +0000113int
114_PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000117
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000118 Py_VA_COPY(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 return vgetargs1(args, format, &lva, FLAG_SIZE_T);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000121}
122
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000123
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000124/* Handle cleanup of allocated memory in case of exception */
125
Benjamin Petersonb173f782009-05-05 22:31:58 +0000126#define GETARGS_CAPSULE_NAME_CLEANUP_PTR "getargs.cleanup_ptr"
127#define GETARGS_CAPSULE_NAME_CLEANUP_BUFFER "getargs.cleanup_buffer"
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000128#define GETARGS_CAPSULE_NAME_CLEANUP_CONVERT "getargs.cleanup_convert"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000129
Antoine Pitrouf71995782008-08-29 18:37:05 +0000130static void
Benjamin Petersonb173f782009-05-05 22:31:58 +0000131cleanup_ptr(PyObject *self)
Antoine Pitrouf71995782008-08-29 18:37:05 +0000132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 void *ptr = PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_PTR);
134 if (ptr) {
135 PyMem_FREE(ptr);
136 }
Antoine Pitrouf71995782008-08-29 18:37:05 +0000137}
138
139static void
Benjamin Petersonb173f782009-05-05 22:31:58 +0000140cleanup_buffer(PyObject *self)
Antoine Pitrouf71995782008-08-29 18:37:05 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 Py_buffer *ptr = (Py_buffer *)PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_BUFFER);
143 if (ptr) {
144 PyBuffer_Release(ptr);
145 }
Antoine Pitrouf71995782008-08-29 18:37:05 +0000146}
147
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000148static int
Victor Stinnera093d0d2011-01-04 11:16:48 +0000149addcleanup(void *ptr, PyObject **freelist, int is_buffer)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 PyObject *cobj;
152 const char *name;
Victor Stinnera093d0d2011-01-04 11:16:48 +0000153 PyCapsule_Destructor destr;
154
155 if (is_buffer) {
156 destr = cleanup_buffer;
157 name = GETARGS_CAPSULE_NAME_CLEANUP_BUFFER;
158 } else {
159 destr = cleanup_ptr;
160 name = GETARGS_CAPSULE_NAME_CLEANUP_PTR;
161 }
Benjamin Petersonb173f782009-05-05 22:31:58 +0000162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 if (!*freelist) {
164 *freelist = PyList_New(0);
165 if (!*freelist) {
166 destr(ptr);
167 return -1;
168 }
169 }
Benjamin Petersonb173f782009-05-05 22:31:58 +0000170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000171 cobj = PyCapsule_New(ptr, name, destr);
172 if (!cobj) {
173 destr(ptr);
174 return -1;
175 }
176 if (PyList_Append(*freelist, cobj)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000177 Py_DECREF(cobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 return -1;
179 }
180 Py_DECREF(cobj);
181 return 0;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000182}
183
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000184static void
185cleanup_convert(PyObject *self)
186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 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);
191 if (ptr && destr)
192 destr(NULL, ptr);
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000193}
194
195static int
196addcleanup_convert(void *ptr, PyObject **freelist, int (*destr)(PyObject*,void*))
197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 PyObject *cobj;
199 if (!*freelist) {
200 *freelist = PyList_New(0);
201 if (!*freelist) {
202 destr(NULL, ptr);
203 return -1;
204 }
205 }
206 cobj = PyCapsule_New(ptr, GETARGS_CAPSULE_NAME_CLEANUP_CONVERT,
207 cleanup_convert);
208 if (!cobj) {
209 destr(NULL, 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); /* This will also call destr. */
218 return -1;
219 }
220 Py_DECREF(cobj);
221 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000222}
223
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000224static int
225cleanreturn(int retval, PyObject *freelist)
226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 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), i;
231 for (i = 0; i < len; i++)
232 PyCapsule_SetDestructor(PyList_GET_ITEM(freelist, i), NULL);
233 }
234 Py_XDECREF(freelist);
235 return retval;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000236}
237
238
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000239static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000240vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 char msgbuf[256];
243 int levels[32];
244 const char *fname = NULL;
245 const char *message = NULL;
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;
254 int compat = flags & FLAG_COMPAT;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 assert(compat || (args != (PyObject*)NULL));
257 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 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))) {
292 if (c != 'e') /* skip encoded */
293 max++;
294 } else if (c == '|')
295 min = max;
296 }
297 break;
298 }
299 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 if (level != 0)
302 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 if (min < 0)
305 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (compat) {
310 if (max == 0) {
311 if (args == NULL)
312 return 1;
313 PyOS_snprintf(msgbuf, sizeof(msgbuf),
314 "%.200s%s takes no arguments",
315 fname==NULL ? "function" : fname,
316 fname==NULL ? "" : "()");
317 PyErr_SetString(PyExc_TypeError, msgbuf);
318 return 0;
319 }
320 else if (min == 1 && max == 1) {
321 if (args == NULL) {
322 PyOS_snprintf(msgbuf, sizeof(msgbuf),
323 "%.200s%s takes at least one argument",
324 fname==NULL ? "function" : fname,
325 fname==NULL ? "" : "()");
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)
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 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 if (!PyTuple_Check(args)) {
344 PyErr_SetString(PyExc_SystemError,
345 "new style getargs format but argument is not a tuple");
346 return 0;
347 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 len = PyTuple_GET_SIZE(args);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 if (len < min || max < len) {
352 if (message == NULL) {
353 PyOS_snprintf(msgbuf, sizeof(msgbuf),
354 "%.150s%s takes %s %d argument%s "
355 "(%ld given)",
356 fname==NULL ? "function" : fname,
357 fname==NULL ? "" : "()",
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));
363 message = msgbuf;
364 }
365 PyErr_SetString(PyExc_TypeError, message);
366 return 0;
367 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 for (i = 0; i < len; i++) {
370 if (*format == '|')
371 format++;
372 msg = convertitem(PyTuple_GET_ITEM(args, 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 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
382 *format != '(' &&
383 *format != '|' && *format != ':' && *format != ';') {
384 PyErr_Format(PyExc_SystemError,
385 "bad format string: %.200s", formatsave);
386 return cleanreturn(0, freelist);
387 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000390}
391
392
393
394static void
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000395seterror(int iarg, const char *msg, int *levels, const char *fname,
396 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 char buf[512];
399 int i;
400 char *p = buf;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (PyErr_Occurred())
403 return;
404 else if (message == NULL) {
405 if (fname != NULL) {
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);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000429}
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:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 "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,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000447 and msgbuf is returned.
448*/
449
450static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000452 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000453 PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 int level = 0;
456 int n = 0;
457 const char *format = *p_format;
458 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 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)))
475 n++;
476 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
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 ? "None" : arg->ob_type->tp_name);
485 return msgbuf;
486 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 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 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 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) {
503 PyErr_Clear();
504 levels[0] = i+1;
505 levels[1] = 0;
506 strncpy(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);
513 if (msg != NULL) {
514 levels[0] = i+1;
515 return msg;
516 }
517 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 *p_format = format;
520 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000521}
522
523
524/* Convert a single item. */
525
526static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000527convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
528 int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 char *msg;
531 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 if (*format == '(' /* ')' */) {
534 format++;
535 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
536 bufsize, 0, freelist);
537 if (msg == NULL)
538 format++;
539 }
540 else {
541 msg = convertsimple(arg, &format, p_va, flags,
542 msgbuf, bufsize, freelist);
543 if (msg != NULL)
544 levels[0] = 0;
545 }
546 if (msg == NULL)
547 *p_format = format;
548 return msg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000549}
550
551
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000552
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000553#define UNICODE_DEFAULT_ENCODING(arg) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 _PyUnicode_AsDefaultEncodedString(arg, NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000555
556/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000557
558static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000559converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 assert(expected != NULL);
562 assert(arg != NULL);
563 PyOS_snprintf(msgbuf, bufsize,
564 "must be %.50s, not %.50s", expected,
565 arg == Py_None ? "None" : arg->ob_type->tp_name);
566 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000567}
568
569#define CONV_UNICODE "(unicode conversion error)"
570
Guido van Rossum45aecf42006-03-15 04:58:47 +0000571/* Explicitly check for float arguments when integers are expected.
572 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000573static int
574float_argument_error(PyObject *arg)
575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 if (PyFloat_Check(arg)) {
577 PyErr_SetString(PyExc_TypeError,
578 "integer argument expected, got float" );
579 return 1;
580 }
581 else
582 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000583}
584
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000585/* 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.
Georg Brandl6dd14612005-09-14 19:29:53 +0000589 Don't call if a tuple is expected.
590
591 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000592*/
593
594static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000595convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000596 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* For # codes */
599#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
600 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
601 else q=va_arg(*p_va, int*);
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000602#define STORE_SIZE(s) \
603 if (flags & FLAG_SIZE_T) \
604 *q2=s; \
605 else { \
606 if (INT_MAX < 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 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000613#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
Victor Stinner6ab8e822011-01-04 11:16:49 +0000614#define RETURN_ERR_OCCURRED return msgbuf
Martin v. Löwis18e16552006-02-15 17:27:45 +0000615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 const char *format = *p_format;
617 char c = *format++;
618 PyObject *uarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 case 'b': { /* unsigned byte -- very short int */
623 char *p = va_arg(*p_va, char *);
624 long ival;
625 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000626 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 ival = PyLong_AsLong(arg);
628 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000629 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 else if (ival < 0) {
631 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000632 "unsigned byte integer is less than minimum");
633 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 }
635 else if (ival > UCHAR_MAX) {
636 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000637 "unsigned byte integer is greater than maximum");
638 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 }
640 else
641 *p = (unsigned char) ival;
642 break;
643 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 case 'B': {/* byte sized bitfield - both signed and unsigned
646 values allowed */
647 char *p = va_arg(*p_va, char *);
648 long ival;
649 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000650 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 ival = PyLong_AsUnsignedLongMask(arg);
652 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000653 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 else
655 *p = (unsigned char) ival;
656 break;
657 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 case 'h': {/* signed short int */
660 short *p = va_arg(*p_va, short *);
661 long ival;
662 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000663 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 ival = PyLong_AsLong(arg);
665 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000666 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 else if (ival < SHRT_MIN) {
668 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000669 "signed short integer is less than minimum");
670 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 }
672 else if (ival > SHRT_MAX) {
673 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000674 "signed short integer is greater than maximum");
675 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 }
677 else
678 *p = (short) ival;
679 break;
680 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 case 'H': { /* short int sized bitfield, both signed and
683 unsigned allowed */
684 unsigned short *p = va_arg(*p_va, unsigned short *);
685 long ival;
686 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000687 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 ival = PyLong_AsUnsignedLongMask(arg);
689 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000690 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 else
692 *p = (unsigned short) ival;
693 break;
694 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 case 'i': {/* signed int */
697 int *p = va_arg(*p_va, int *);
698 long ival;
699 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000700 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 ival = PyLong_AsLong(arg);
702 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000703 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 else if (ival > INT_MAX) {
705 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000706 "signed integer is greater than maximum");
707 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 }
709 else if (ival < INT_MIN) {
710 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000711 "signed integer is less than minimum");
712 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 }
714 else
715 *p = ival;
716 break;
717 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 case 'I': { /* int sized bitfield, both signed and
720 unsigned allowed */
721 unsigned int *p = va_arg(*p_va, unsigned int *);
722 unsigned int ival;
723 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000724 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
726 if (ival == (unsigned int)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000727 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 else
729 *p = ival;
730 break;
731 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 case 'n': /* Py_ssize_t */
734 {
735 PyObject *iobj;
736 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
737 Py_ssize_t ival = -1;
738 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000739 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 iobj = PyNumber_Index(arg);
741 if (iobj != NULL) {
742 ival = PyLong_AsSsize_t(iobj);
743 Py_DECREF(iobj);
744 }
745 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000746 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 *p = ival;
748 break;
749 }
750 case 'l': {/* long int */
751 long *p = va_arg(*p_va, long *);
752 long ival;
753 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000754 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 ival = PyLong_AsLong(arg);
756 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000757 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 else
759 *p = ival;
760 break;
761 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 case 'k': { /* long sized bitfield */
764 unsigned long *p = va_arg(*p_va, unsigned long *);
765 unsigned long ival;
766 if (PyLong_Check(arg))
767 ival = PyLong_AsUnsignedLongMask(arg);
768 else
769 return converterr("integer<k>", arg, msgbuf, bufsize);
770 *p = ival;
771 break;
772 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000773
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000774#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 case 'L': {/* PY_LONG_LONG */
776 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
777 PY_LONG_LONG ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000778 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000779 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 ival = PyLong_AsLongLong(arg);
Mark Dickinsonc7301312010-06-10 16:05:10 +0000781 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000782 RETURN_ERR_OCCURRED;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000783 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 break;
786 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 case 'K': { /* long long sized bitfield */
789 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
790 unsigned PY_LONG_LONG ival;
791 if (PyLong_Check(arg))
792 ival = PyLong_AsUnsignedLongLongMask(arg);
793 else
794 return converterr("integer<K>", arg, msgbuf, bufsize);
795 *p = ival;
796 break;
797 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000798#endif
Guido van Rossum98297ee2007-11-06 21:34:58 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 case 'f': {/* float */
801 float *p = va_arg(*p_va, float *);
802 double dval = PyFloat_AsDouble(arg);
803 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000804 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 else
806 *p = (float) dval;
807 break;
808 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 case 'd': {/* double */
811 double *p = va_arg(*p_va, double *);
812 double dval = PyFloat_AsDouble(arg);
813 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000814 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 else
816 *p = dval;
817 break;
818 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 case 'D': {/* complex double */
821 Py_complex *p = va_arg(*p_va, Py_complex *);
822 Py_complex cval;
823 cval = PyComplex_AsCComplex(arg);
824 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000825 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 else
827 *p = cval;
828 break;
829 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 case 'c': {/* char */
832 char *p = va_arg(*p_va, char *);
833 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
834 *p = PyBytes_AS_STRING(arg)[0];
835 else
836 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
837 break;
838 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 case 'C': {/* unicode char */
841 int *p = va_arg(*p_va, int *);
842 if (PyUnicode_Check(arg) &&
843 PyUnicode_GET_SIZE(arg) == 1)
844 *p = PyUnicode_AS_UNICODE(arg)[0];
845 else
846 return converterr("a unicode character", arg, msgbuf, bufsize);
847 break;
848 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000849
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000850 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 case 'y': {/* any buffer-like object, but not PyUnicode */
854 void **p = (void **)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++;
Victor Stinnera093d0d2011-01-04 11:16:48 +0000861 if (addcleanup(p, freelist, 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 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);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000871 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 FETCH_SIZE;
873 STORE_SIZE(count);
874 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000875 } else {
876 if (strlen(*p) != count)
877 return converterr(
878 "bytes without null bytes",
879 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 }
881 break;
882 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000883
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000884 case 's': /* text string */
885 case 'z': /* text string or None */
886 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000888 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000890
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000891 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
893 else if (PyUnicode_Check(arg)) {
894 uarg = UNICODE_DEFAULT_ENCODING(arg);
895 if (uarg == NULL)
896 return converterr(CONV_UNICODE,
897 arg, msgbuf, bufsize);
898 PyBuffer_FillInfo(p, arg,
899 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
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 }
Victor Stinnera093d0d2011-01-04 11:16:48 +0000907 if (addcleanup(p, freelist, 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 return converterr(
909 "(cleanup problem)",
910 arg, msgbuf, bufsize);
911 }
912 format++;
913 } else if (*format == '#') { /* any buffer-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000914 /* "s#" or "z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 void **p = (void **)va_arg(*p_va, char **);
916 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000917
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000918 if (c == 'z' && arg == Py_None) {
919 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 STORE_SIZE(0);
921 }
922 else if (PyUnicode_Check(arg)) {
923 uarg = UNICODE_DEFAULT_ENCODING(arg);
924 if (uarg == NULL)
925 return converterr(CONV_UNICODE,
926 arg, msgbuf, bufsize);
927 *p = PyBytes_AS_STRING(uarg);
928 STORE_SIZE(PyBytes_GET_SIZE(uarg));
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)
935 return converterr(buf, arg, msgbuf, bufsize);
936 STORE_SIZE(count);
937 }
938 format++;
939 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000940 /* "s" or "z" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 char **p = va_arg(*p_va, char **);
942 uarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000943
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000944 if (c == 'z' && arg == Py_None)
945 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 else if (PyUnicode_Check(arg)) {
947 uarg = UNICODE_DEFAULT_ENCODING(arg);
948 if (uarg == NULL)
949 return converterr(CONV_UNICODE,
950 arg, msgbuf, bufsize);
951 *p = PyBytes_AS_STRING(uarg);
952 }
953 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000954 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 arg, msgbuf, bufsize);
Victor Stinner43fb0092010-05-29 00:13:06 +0000956 if (*p != NULL && uarg != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 (Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
958 return converterr(
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000959 c == 'z' ? "str without null bytes or None"
960 : "str without null bytes",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 arg, msgbuf, bufsize);
962 }
963 break;
964 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000965
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000966 case 'u': /* raw unicode buffer (Py_UNICODE *) */
967 case 'Z': /* raw unicode buffer or None */
968 {
Brett Cannonb94767f2011-02-22 20:15:44 +0000969 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 if (*format == '#') { /* any buffer-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000972 /* "s#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +0000974
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000975 if (c == 'Z' && arg == Py_None) {
976 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 STORE_SIZE(0);
978 }
979 else if (PyUnicode_Check(arg)) {
980 *p = PyUnicode_AS_UNICODE(arg);
981 STORE_SIZE(PyUnicode_GET_SIZE(arg));
982 }
Victor Stinner5216e6d2010-06-08 21:45:51 +0000983 else
984 return converterr("str or None", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 format++;
986 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000987 /* "s" or "Z" */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000988 if (c == 'Z' && arg == Py_None)
989 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000990 else if (PyUnicode_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 *p = PyUnicode_AS_UNICODE(arg);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000992 if (Py_UNICODE_strlen(*p) != PyUnicode_GET_SIZE(arg))
993 return converterr(
994 "str without null character or None",
995 arg, msgbuf, bufsize);
996 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000997 return converterr(c == 'Z' ? "str or None" : "str",
998 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 }
1000 break;
1001 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 case 'e': {/* encoded string */
1004 char **buffer;
1005 const char *encoding;
1006 PyObject *s;
1007 int recode_strings;
1008 Py_ssize_t size;
1009 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 /* Get 'e' parameter: the encoding name */
1012 encoding = (const char *)va_arg(*p_va, const char *);
1013 if (encoding == NULL)
1014 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* Get output buffer parameter:
1017 's' (recode all objects via Unicode) or
1018 't' (only recode non-string objects)
1019 */
1020 if (*format == 's')
1021 recode_strings = 1;
1022 else if (*format == 't')
1023 recode_strings = 0;
1024 else
1025 return converterr(
1026 "(unknown parser marker combination)",
1027 arg, msgbuf, bufsize);
1028 buffer = (char **)va_arg(*p_va, char **);
1029 format++;
1030 if (buffer == NULL)
1031 return converterr("(buffer is NULL)",
1032 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 /* Encode object */
1035 if (!recode_strings &&
1036 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1037 s = arg;
1038 Py_INCREF(s);
1039 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1040 return converterr("(AsCharBuffer failed)",
1041 arg, msgbuf, bufsize);
1042 }
1043 else {
1044 PyObject *u;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 /* Convert object to Unicode */
1047 u = PyUnicode_FromObject(arg);
1048 if (u == NULL)
1049 return converterr(
1050 "string or unicode or text buffer",
1051 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 /* Encode object; use default error handling */
1054 s = PyUnicode_AsEncodedString(u,
1055 encoding,
1056 NULL);
1057 Py_DECREF(u);
1058 if (s == NULL)
1059 return converterr("(encoding failed)",
1060 arg, msgbuf, bufsize);
1061 if (!PyBytes_Check(s)) {
1062 Py_DECREF(s);
1063 return converterr(
1064 "(encoder failed to return bytes)",
1065 arg, msgbuf, bufsize);
1066 }
1067 size = PyBytes_GET_SIZE(s);
1068 ptr = PyBytes_AS_STRING(s);
1069 if (ptr == NULL)
1070 ptr = "";
1071 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 /* Write output; output is guaranteed to be 0-terminated */
1074 if (*format == '#') {
1075 /* Using buffer length parameter '#':
1076
1077 - if *buffer is NULL, a new buffer of the
1078 needed size is allocated and the data
1079 copied into it; *buffer is updated to point
1080 to the new buffer; the caller is
1081 responsible for PyMem_Free()ing it after
1082 usage
1083
1084 - if *buffer is not NULL, the data is
1085 copied to *buffer; *buffer_len has to be
1086 set to the size of the buffer on input;
1087 buffer overflow is signalled with an error;
1088 buffer has to provide enough room for the
1089 encoded string plus the trailing 0-byte
1090
1091 - in both cases, *buffer_len is updated to
1092 the size of the buffer /excluding/ the
1093 trailing 0-byte
1094
1095 */
1096 FETCH_SIZE;
1097
1098 format++;
1099 if (q == NULL && q2 == NULL) {
1100 Py_DECREF(s);
1101 return converterr(
1102 "(buffer_len is NULL)",
1103 arg, msgbuf, bufsize);
1104 }
1105 if (*buffer == NULL) {
1106 *buffer = PyMem_NEW(char, size + 1);
1107 if (*buffer == NULL) {
1108 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001109 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001110 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 }
Victor Stinnera093d0d2011-01-04 11:16:48 +00001112 if (addcleanup(*buffer, freelist, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 Py_DECREF(s);
1114 return converterr(
1115 "(cleanup problem)",
1116 arg, msgbuf, bufsize);
1117 }
1118 } else {
1119 if (size + 1 > BUFFER_LEN) {
1120 Py_DECREF(s);
1121 return converterr(
1122 "(buffer overflow)",
1123 arg, msgbuf, bufsize);
1124 }
1125 }
1126 memcpy(*buffer, ptr, size+1);
1127 STORE_SIZE(size);
1128 } else {
1129 /* Using a 0-terminated buffer:
1130
1131 - the encoded string has to be 0-terminated
1132 for this variant to work; if it is not, an
1133 error raised
1134
1135 - a new buffer of the needed size is
1136 allocated and the data copied into it;
1137 *buffer is updated to point to the new
1138 buffer; the caller is responsible for
1139 PyMem_Free()ing it after usage
1140
1141 */
1142 if ((Py_ssize_t)strlen(ptr) != size) {
1143 Py_DECREF(s);
1144 return converterr(
1145 "encoded string without NULL bytes",
1146 arg, msgbuf, bufsize);
1147 }
1148 *buffer = PyMem_NEW(char, size + 1);
1149 if (*buffer == NULL) {
1150 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001151 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001152 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 }
Victor Stinnera093d0d2011-01-04 11:16:48 +00001154 if (addcleanup(*buffer, freelist, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 Py_DECREF(s);
1156 return converterr("(cleanup problem)",
1157 arg, msgbuf, bufsize);
1158 }
1159 memcpy(*buffer, ptr, size+1);
1160 }
1161 Py_DECREF(s);
1162 break;
1163 }
1164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 case 'S': { /* PyBytes object */
1166 PyObject **p = va_arg(*p_va, PyObject **);
1167 if (PyBytes_Check(arg))
1168 *p = arg;
1169 else
1170 return converterr("bytes", arg, msgbuf, bufsize);
1171 break;
1172 }
1173
1174 case 'Y': { /* PyByteArray object */
1175 PyObject **p = va_arg(*p_va, PyObject **);
1176 if (PyByteArray_Check(arg))
1177 *p = arg;
1178 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001179 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 break;
1181 }
1182
1183 case 'U': { /* PyUnicode object */
1184 PyObject **p = va_arg(*p_va, PyObject **);
1185 if (PyUnicode_Check(arg))
1186 *p = arg;
1187 else
1188 return converterr("str", arg, msgbuf, bufsize);
1189 break;
1190 }
1191
1192 case 'O': { /* object */
1193 PyTypeObject *type;
1194 PyObject **p;
1195 if (*format == '!') {
1196 type = va_arg(*p_va, PyTypeObject*);
1197 p = va_arg(*p_va, PyObject **);
1198 format++;
1199 if (PyType_IsSubtype(arg->ob_type, type))
1200 *p = arg;
1201 else
1202 return converterr(type->tp_name, arg, msgbuf, bufsize);
1203
1204 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 else if (*format == '&') {
1206 typedef int (*converter)(PyObject *, void *);
1207 converter convert = va_arg(*p_va, converter);
1208 void *addr = va_arg(*p_va, void *);
1209 int res;
1210 format++;
1211 if (! (res = (*convert)(arg, addr)))
1212 return converterr("(unspecified)",
1213 arg, msgbuf, bufsize);
1214 if (res == Py_CLEANUP_SUPPORTED &&
1215 addcleanup_convert(addr, freelist, convert) == -1)
1216 return converterr("(cleanup problem)",
1217 arg, msgbuf, bufsize);
1218 }
1219 else {
1220 p = va_arg(*p_va, PyObject **);
1221 *p = arg;
1222 }
1223 break;
1224 }
1225
1226
Victor Stinner25e8ec42010-06-25 00:02:38 +00001227 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229
Victor Stinner25e8ec42010-06-25 00:02:38 +00001230 if (*format != '*')
1231 return converterr(
1232 "invalid use of 'w' format character",
1233 arg, msgbuf, bufsize);
1234 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235
Victor Stinner25e8ec42010-06-25 00:02:38 +00001236 /* Caller is interested in Py_buffer, and the object
1237 supports it directly. */
1238 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1239 PyErr_Clear();
1240 return converterr("read-write buffer", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 }
Victor Stinner8182b712010-07-28 00:40:58 +00001242 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1243 PyBuffer_Release((Py_buffer*)p);
1244 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1245 }
Victor Stinnera093d0d2011-01-04 11:16:48 +00001246 if (addcleanup(p, freelist, 1)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001247 return converterr(
1248 "(cleanup problem)",
1249 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 break;
1252 }
1253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 default:
1255 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
1256
1257 }
1258
1259 *p_format = format;
1260 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001261
1262#undef FETCH_SIZE
1263#undef STORE_SIZE
1264#undef BUFFER_LEN
1265#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001266}
Guido van Rossumaa354651996-08-19 19:32:04 +00001267
Martin v. Löwis18e16552006-02-15 17:27:45 +00001268static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001269convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001270{
Victor Stinner5cb62392010-06-06 20:27:51 +00001271 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 Py_ssize_t count;
1273 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 *errmsg = NULL;
1276 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001277 if (pb != NULL && pb->bf_releasebuffer != NULL) {
1278 *errmsg = "read-only pinned buffer";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 return -1;
1280 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001281
Victor Stinner8182b712010-07-28 00:40:58 +00001282 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 count = view.len;
1285 *p = view.buf;
1286 PyBuffer_Release(&view);
1287 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001288}
Guido van Rossumaa354651996-08-19 19:32:04 +00001289
Martin v. Löwis423be952008-08-13 15:53:07 +00001290static int
Neal Norwitz2f99b242008-08-24 05:48:10 +00001291getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001292{
Victor Stinner8182b712010-07-28 00:40:58 +00001293 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 *errmsg = "bytes or buffer";
1295 return -1;
1296 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001297 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001298 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001299 *errmsg = "contiguous buffer";
1300 return -1;
1301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001303}
1304
Guido van Rossumaa354651996-08-19 19:32:04 +00001305/* Support for keyword arguments donated by
1306 Geoff Philbrick <philbric@delphi.hks.com> */
1307
Tim Petersf8cd3e82001-10-27 04:26:57 +00001308/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001309int
1310PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 PyObject *keywords,
1312 const char *format,
1313 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 int retval;
1316 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 if ((args == NULL || !PyTuple_Check(args)) ||
1319 (keywords != NULL && !PyDict_Check(keywords)) ||
1320 format == NULL ||
1321 kwlist == NULL)
1322 {
1323 PyErr_BadInternalCall();
1324 return 0;
1325 }
Tim Peters45772cd2001-10-27 03:58:40 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 va_start(va, kwlist);
1328 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1329 va_end(va);
1330 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001331}
1332
1333int
1334_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 PyObject *keywords,
1336 const char *format,
1337 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 int retval;
1340 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if ((args == NULL || !PyTuple_Check(args)) ||
1343 (keywords != NULL && !PyDict_Check(keywords)) ||
1344 format == NULL ||
1345 kwlist == NULL)
1346 {
1347 PyErr_BadInternalCall();
1348 return 0;
1349 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 va_start(va, kwlist);
1352 retval = vgetargskeywords(args, keywords, format,
1353 kwlist, &va, FLAG_SIZE_T);
1354 va_end(va);
1355 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001356}
1357
1358
Brett Cannon711e7d92004-07-10 22:20:32 +00001359int
1360PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001361 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001362 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001363 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 int retval;
1366 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 if ((args == NULL || !PyTuple_Check(args)) ||
1369 (keywords != NULL && !PyDict_Check(keywords)) ||
1370 format == NULL ||
1371 kwlist == NULL)
1372 {
1373 PyErr_BadInternalCall();
1374 return 0;
1375 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001376
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001377 Py_VA_COPY(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1380 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001381}
1382
1383int
1384_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 PyObject *keywords,
1386 const char *format,
1387 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 int retval;
1390 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 if ((args == NULL || !PyTuple_Check(args)) ||
1393 (keywords != NULL && !PyDict_Check(keywords)) ||
1394 format == NULL ||
1395 kwlist == NULL)
1396 {
1397 PyErr_BadInternalCall();
1398 return 0;
1399 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001400
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001401 Py_VA_COPY(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 retval = vgetargskeywords(args, keywords, format,
1404 kwlist, &lva, FLAG_SIZE_T);
1405 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001406}
1407
Benjamin Petersonfb886362010-04-24 18:21:17 +00001408int
1409PyArg_ValidateKeywordArguments(PyObject *kwargs)
1410{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001411 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 PyErr_BadInternalCall();
1413 return 0;
1414 }
1415 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1416 PyErr_SetString(PyExc_TypeError,
1417 "keyword arguments must be strings");
1418 return 0;
1419 }
1420 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001421}
1422
Christian Heimes380f7f22008-02-28 11:19:05 +00001423#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001424
Guido van Rossumaa354651996-08-19 19:32:04 +00001425static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001426vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 char msgbuf[512];
1430 int levels[32];
1431 const char *fname, *msg, *custom_msg, *keyword;
1432 int min = INT_MAX;
1433 int i, len, nargs, nkeywords;
1434 PyObject *freelist = NULL, *current_arg;
Tim Petersf4331c12001-10-27 00:17:34 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 assert(args != NULL && PyTuple_Check(args));
1437 assert(keywords == NULL || PyDict_Check(keywords));
1438 assert(format != NULL);
1439 assert(kwlist != NULL);
1440 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 /* grab the function name or custom error msg first (mutually exclusive) */
1443 fname = strchr(format, ':');
1444 if (fname) {
1445 fname++;
1446 custom_msg = NULL;
1447 }
1448 else {
1449 custom_msg = strchr(format,';');
1450 if (custom_msg)
1451 custom_msg++;
1452 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 /* scan kwlist and get greatest possible nbr of args */
1455 for (len=0; kwlist[len]; len++)
1456 continue;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 nargs = PyTuple_GET_SIZE(args);
1459 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1460 if (nargs + nkeywords > len) {
1461 PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
1462 "argument%s (%d given)",
1463 (fname == NULL) ? "function" : fname,
1464 (fname == NULL) ? "" : "()",
1465 len,
1466 (len == 1) ? "" : "s",
1467 nargs + nkeywords);
1468 return 0;
1469 }
Tim Petersc2f01122001-10-27 07:25:06 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1472 for (i = 0; i < len; i++) {
1473 keyword = kwlist[i];
1474 if (*format == '|') {
1475 min = i;
1476 format++;
1477 }
1478 if (IS_END_OF_FORMAT(*format)) {
1479 PyErr_Format(PyExc_RuntimeError,
1480 "More keyword list entries (%d) than "
1481 "format specifiers (%d)", len, i);
1482 return cleanreturn(0, freelist);
1483 }
1484 current_arg = NULL;
1485 if (nkeywords) {
1486 current_arg = PyDict_GetItemString(keywords, keyword);
1487 }
1488 if (current_arg) {
1489 --nkeywords;
1490 if (i < nargs) {
1491 /* arg present in tuple and in dict */
1492 PyErr_Format(PyExc_TypeError,
1493 "Argument given by name ('%s') "
1494 "and position (%d)",
1495 keyword, i+1);
1496 return cleanreturn(0, freelist);
1497 }
1498 }
1499 else if (nkeywords && PyErr_Occurred())
1500 return cleanreturn(0, freelist);
1501 else if (i < nargs)
1502 current_arg = PyTuple_GET_ITEM(args, i);
Christian Heimes380f7f22008-02-28 11:19:05 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 if (current_arg) {
1505 msg = convertitem(current_arg, &format, p_va, flags,
1506 levels, msgbuf, sizeof(msgbuf), &freelist);
1507 if (msg) {
1508 seterror(i+1, msg, levels, fname, custom_msg);
1509 return cleanreturn(0, freelist);
1510 }
1511 continue;
1512 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 if (i < min) {
1515 PyErr_Format(PyExc_TypeError, "Required argument "
1516 "'%s' (pos %d) not found",
1517 keyword, i+1);
1518 return cleanreturn(0, freelist);
1519 }
1520 /* current code reports success when all required args
1521 * fulfilled and no keyword args left, with no further
1522 * validation. XXX Maybe skip this in debug build ?
1523 */
1524 if (!nkeywords)
1525 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 /* We are into optional args, skip thru to any remaining
1528 * keyword args */
1529 msg = skipitem(&format, p_va, flags);
1530 if (msg) {
1531 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1532 format);
1533 return cleanreturn(0, freelist);
1534 }
1535 }
Tim Petersb054be42001-10-27 05:07:41 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (!IS_END_OF_FORMAT(*format) && *format != '|') {
1538 PyErr_Format(PyExc_RuntimeError,
1539 "more argument specifiers than keyword list entries "
1540 "(remaining format:'%s')", format);
1541 return cleanreturn(0, freelist);
1542 }
Tim Petersc2f01122001-10-27 07:25:06 +00001543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 /* make sure there are no extraneous keyword arguments */
1545 if (nkeywords > 0) {
1546 PyObject *key, *value;
1547 Py_ssize_t pos = 0;
1548 while (PyDict_Next(keywords, &pos, &key, &value)) {
1549 int match = 0;
1550 char *ks;
1551 if (!PyUnicode_Check(key)) {
1552 PyErr_SetString(PyExc_TypeError,
1553 "keywords must be strings");
1554 return cleanreturn(0, freelist);
1555 }
Victor Stinner93b55132010-05-19 00:54:06 +00001556 /* check that _PyUnicode_AsString() result is not NULL */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 ks = _PyUnicode_AsString(key);
Victor Stinner93b55132010-05-19 00:54:06 +00001558 if (ks != NULL) {
1559 for (i = 0; i < len; i++) {
1560 if (!strcmp(ks, kwlist[i])) {
1561 match = 1;
1562 break;
1563 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 }
1565 }
1566 if (!match) {
1567 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001568 "'%U' is an invalid keyword "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 "argument for this function",
Victor Stinner93b55132010-05-19 00:54:06 +00001570 key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 return cleanreturn(0, freelist);
1572 }
1573 }
1574 }
1575
1576 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001577}
1578
1579
1580static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001581skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 const char *format = *p_format;
1584 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 /* simple codes
1589 * The individual types (second arg of va_arg) are irrelevant */
Georg Brandl6dd14612005-09-14 19:29:53 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 case 'b': /* byte -- very short int */
1592 case 'B': /* byte as bitfield */
1593 case 'h': /* short int */
1594 case 'H': /* short int as bitfield */
1595 case 'i': /* int */
1596 case 'I': /* int sized bitfield */
1597 case 'l': /* long int */
1598 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001599#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 case 'L': /* PY_LONG_LONG */
1601 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001602#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 case 'f': /* float */
1604 case 'd': /* double */
1605 case 'D': /* complex double */
1606 case 'c': /* char */
1607 case 'C': /* unicode char */
1608 {
1609 (void) va_arg(*p_va, void *);
1610 break;
1611 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 case 'n': /* Py_ssize_t */
1614 {
1615 (void) va_arg(*p_va, Py_ssize_t *);
1616 break;
1617 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 case 'e': /* string with encoding */
1622 {
1623 (void) va_arg(*p_va, const char *);
1624 if (!(*format == 's' || *format == 't'))
1625 /* after 'e', only 's' and 't' is allowed */
1626 goto err;
1627 format++;
1628 /* explicit fallthrough to string cases */
1629 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 case 's': /* string */
1632 case 'z': /* string or None */
1633 case 'y': /* bytes */
1634 case 'u': /* unicode string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 case 'w': /* buffer, read-write */
1636 {
1637 (void) va_arg(*p_va, char **);
1638 if (*format == '#') {
1639 if (flags & FLAG_SIZE_T)
1640 (void) va_arg(*p_va, Py_ssize_t *);
1641 else
1642 (void) va_arg(*p_va, int *);
1643 format++;
1644 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
1645 format++;
1646 }
1647 break;
1648 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 /* object codes */
Georg Brandl6dd14612005-09-14 19:29:53 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 case 'S': /* string object */
1653 case 'Y': /* string object */
1654 case 'U': /* unicode string object */
1655 {
1656 (void) va_arg(*p_va, PyObject **);
1657 break;
1658 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 case 'O': /* object */
1661 {
1662 if (*format == '!') {
1663 format++;
1664 (void) va_arg(*p_va, PyTypeObject*);
1665 (void) va_arg(*p_va, PyObject **);
1666 }
1667 else if (*format == '&') {
1668 typedef int (*converter)(PyObject *, void *);
1669 (void) va_arg(*p_va, converter);
1670 (void) va_arg(*p_va, void *);
1671 format++;
1672 }
1673 else {
1674 (void) va_arg(*p_va, PyObject **);
1675 }
1676 break;
1677 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 case '(': /* bypass tuple, not handled at all previously */
1680 {
1681 char *msg;
1682 for (;;) {
1683 if (*format==')')
1684 break;
1685 if (IS_END_OF_FORMAT(*format))
1686 return "Unmatched left paren in format "
1687 "string";
1688 msg = skipitem(&format, p_va, flags);
1689 if (msg)
1690 return msg;
1691 }
1692 format++;
1693 break;
1694 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 case ')':
1697 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001700err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 *p_format = format;
1706 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00001707}
Fred Drakee4616e62001-10-23 21:09:29 +00001708
1709
1710int
Martin v. Löwis76246742006-03-01 04:06:10 +00001711PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 Py_ssize_t i, l;
1714 PyObject **o;
1715 va_list vargs;
Fred Drakee4616e62001-10-23 21:09:29 +00001716
1717#ifdef HAVE_STDARG_PROTOTYPES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 va_start(vargs, max);
Fred Drakee4616e62001-10-23 21:09:29 +00001719#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 va_start(vargs);
Fred Drakee4616e62001-10-23 21:09:29 +00001721#endif
1722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 assert(min >= 0);
1724 assert(min <= max);
1725 if (!PyTuple_Check(args)) {
1726 PyErr_SetString(PyExc_SystemError,
1727 "PyArg_UnpackTuple() argument list is not a tuple");
1728 return 0;
1729 }
1730 l = PyTuple_GET_SIZE(args);
1731 if (l < min) {
1732 if (name != NULL)
1733 PyErr_Format(
1734 PyExc_TypeError,
1735 "%s expected %s%zd arguments, got %zd",
1736 name, (min == max ? "" : "at least "), min, l);
1737 else
1738 PyErr_Format(
1739 PyExc_TypeError,
1740 "unpacked tuple should have %s%zd elements,"
1741 " but has %zd",
1742 (min == max ? "" : "at least "), min, l);
1743 va_end(vargs);
1744 return 0;
1745 }
1746 if (l > max) {
1747 if (name != NULL)
1748 PyErr_Format(
1749 PyExc_TypeError,
1750 "%s expected %s%zd arguments, got %zd",
1751 name, (min == max ? "" : "at most "), max, l);
1752 else
1753 PyErr_Format(
1754 PyExc_TypeError,
1755 "unpacked tuple should have %s%zd elements,"
1756 " but has %zd",
1757 (min == max ? "" : "at most "), max, l);
1758 va_end(vargs);
1759 return 0;
1760 }
1761 for (i = 0; i < l; i++) {
1762 o = va_arg(vargs, PyObject **);
1763 *o = PyTuple_GET_ITEM(args, i);
1764 }
1765 va_end(vargs);
1766 return 1;
Fred Drakee4616e62001-10-23 21:09:29 +00001767}
Georg Brandl02c42872005-08-26 06:42:30 +00001768
1769
1770/* For type constructors that don't take keyword args
1771 *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001772 * Sets a TypeError and returns 0 if the kwds dict is
Thomas Wouters89f507f2006-12-13 04:49:30 +00001773 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001774 */
1775int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001776_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 if (kw == NULL)
1779 return 1;
1780 if (!PyDict_CheckExact(kw)) {
1781 PyErr_BadInternalCall();
1782 return 0;
1783 }
1784 if (PyDict_Size(kw) == 0)
1785 return 1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1788 funcname);
1789 return 0;
Georg Brandl02c42872005-08-26 06:42:30 +00001790}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001791#ifdef __cplusplus
1792};
1793#endif