blob: abf55ce041abfa46f1397769e46bd2986b14f273 [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
Benjamin Petersonb173f782009-05-05 22:31:58 +0000149addcleanup(void *ptr, PyObject **freelist, PyCapsule_Destructor destr)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 PyObject *cobj;
152 const char *name;
Benjamin Petersonb173f782009-05-05 22:31:58 +0000153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 if (!*freelist) {
155 *freelist = PyList_New(0);
156 if (!*freelist) {
157 destr(ptr);
158 return -1;
159 }
160 }
Benjamin Petersonb173f782009-05-05 22:31:58 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 if (destr == cleanup_ptr) {
163 name = GETARGS_CAPSULE_NAME_CLEANUP_PTR;
164 } else if (destr == cleanup_buffer) {
165 name = GETARGS_CAPSULE_NAME_CLEANUP_BUFFER;
166 } else {
167 return -1;
168 }
169 cobj = PyCapsule_New(ptr, name, destr);
170 if (!cobj) {
171 destr(ptr);
172 return -1;
173 }
174 if (PyList_Append(*freelist, cobj)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000175 Py_DECREF(cobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000176 return -1;
177 }
178 Py_DECREF(cobj);
179 return 0;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000180}
181
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000182static void
183cleanup_convert(PyObject *self)
184{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 typedef int (*destr_t)(PyObject *, void *);
186 destr_t destr = (destr_t)PyCapsule_GetContext(self);
187 void *ptr = PyCapsule_GetPointer(self,
188 GETARGS_CAPSULE_NAME_CLEANUP_CONVERT);
189 if (ptr && destr)
190 destr(NULL, ptr);
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000191}
192
193static int
194addcleanup_convert(void *ptr, PyObject **freelist, int (*destr)(PyObject*,void*))
195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 PyObject *cobj;
197 if (!*freelist) {
198 *freelist = PyList_New(0);
199 if (!*freelist) {
200 destr(NULL, ptr);
201 return -1;
202 }
203 }
204 cobj = PyCapsule_New(ptr, GETARGS_CAPSULE_NAME_CLEANUP_CONVERT,
205 cleanup_convert);
206 if (!cobj) {
207 destr(NULL, ptr);
208 return -1;
209 }
210 if (PyCapsule_SetContext(cobj, destr) == -1) {
211 /* This really should not happen. */
212 Py_FatalError("capsule refused setting of context.");
213 }
214 if (PyList_Append(*freelist, cobj)) {
215 Py_DECREF(cobj); /* This will also call destr. */
216 return -1;
217 }
218 Py_DECREF(cobj);
219 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000220}
221
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000222static int
223cleanreturn(int retval, PyObject *freelist)
224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 if (freelist && retval != 0) {
226 /* We were successful, reset the destructors so that they
227 don't get called. */
228 Py_ssize_t len = PyList_GET_SIZE(freelist), i;
229 for (i = 0; i < len; i++)
230 PyCapsule_SetDestructor(PyList_GET_ITEM(freelist, i), NULL);
231 }
232 Py_XDECREF(freelist);
233 return retval;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000234}
235
236
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000237static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000238vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000239{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 char msgbuf[256];
241 int levels[32];
242 const char *fname = NULL;
243 const char *message = NULL;
244 int min = -1;
245 int max = 0;
246 int level = 0;
247 int endfmt = 0;
248 const char *formatsave = format;
249 Py_ssize_t i, len;
250 char *msg;
251 PyObject *freelist = NULL;
252 int compat = flags & FLAG_COMPAT;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 assert(compat || (args != (PyObject*)NULL));
255 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 while (endfmt == 0) {
258 int c = *format++;
259 switch (c) {
260 case '(':
261 if (level == 0)
262 max++;
263 level++;
264 if (level >= 30)
265 Py_FatalError("too many tuple nesting levels "
266 "in argument format string");
267 break;
268 case ')':
269 if (level == 0)
270 Py_FatalError("excess ')' in getargs format");
271 else
272 level--;
273 break;
274 case '\0':
275 endfmt = 1;
276 break;
277 case ':':
278 fname = format;
279 endfmt = 1;
280 break;
281 case ';':
282 message = format;
283 endfmt = 1;
284 break;
285 default:
286 if (level == 0) {
287 if (c == 'O')
288 max++;
289 else if (isalpha(Py_CHARMASK(c))) {
290 if (c != 'e') /* skip encoded */
291 max++;
292 } else if (c == '|')
293 min = max;
294 }
295 break;
296 }
297 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (level != 0)
300 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (min < 0)
303 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (compat) {
308 if (max == 0) {
309 if (args == NULL)
310 return 1;
311 PyOS_snprintf(msgbuf, sizeof(msgbuf),
312 "%.200s%s takes no arguments",
313 fname==NULL ? "function" : fname,
314 fname==NULL ? "" : "()");
315 PyErr_SetString(PyExc_TypeError, msgbuf);
316 return 0;
317 }
318 else if (min == 1 && max == 1) {
319 if (args == NULL) {
320 PyOS_snprintf(msgbuf, sizeof(msgbuf),
321 "%.200s%s takes at least one argument",
322 fname==NULL ? "function" : fname,
323 fname==NULL ? "" : "()");
324 PyErr_SetString(PyExc_TypeError, msgbuf);
325 return 0;
326 }
327 msg = convertitem(args, &format, p_va, flags, levels,
328 msgbuf, sizeof(msgbuf), &freelist);
329 if (msg == NULL)
330 return cleanreturn(1, freelist);
331 seterror(levels[0], msg, levels+1, fname, message);
332 return cleanreturn(0, freelist);
333 }
334 else {
335 PyErr_SetString(PyExc_SystemError,
336 "old style getargs format uses new features");
337 return 0;
338 }
339 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 if (!PyTuple_Check(args)) {
342 PyErr_SetString(PyExc_SystemError,
343 "new style getargs format but argument is not a tuple");
344 return 0;
345 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 len = PyTuple_GET_SIZE(args);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 if (len < min || max < len) {
350 if (message == NULL) {
351 PyOS_snprintf(msgbuf, sizeof(msgbuf),
352 "%.150s%s takes %s %d argument%s "
353 "(%ld given)",
354 fname==NULL ? "function" : fname,
355 fname==NULL ? "" : "()",
356 min==max ? "exactly"
357 : len < min ? "at least" : "at most",
358 len < min ? min : max,
359 (len < min ? min : max) == 1 ? "" : "s",
360 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
361 message = msgbuf;
362 }
363 PyErr_SetString(PyExc_TypeError, message);
364 return 0;
365 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 for (i = 0; i < len; i++) {
368 if (*format == '|')
369 format++;
370 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
371 flags, levels, msgbuf,
372 sizeof(msgbuf), &freelist);
373 if (msg) {
374 seterror(i+1, msg, levels, fname, msg);
375 return cleanreturn(0, freelist);
376 }
377 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
380 *format != '(' &&
381 *format != '|' && *format != ':' && *format != ';') {
382 PyErr_Format(PyExc_SystemError,
383 "bad format string: %.200s", formatsave);
384 return cleanreturn(0, freelist);
385 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000388}
389
390
391
392static void
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000393seterror(int iarg, const char *msg, int *levels, const char *fname,
394 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 char buf[512];
397 int i;
398 char *p = buf;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (PyErr_Occurred())
401 return;
402 else if (message == NULL) {
403 if (fname != NULL) {
404 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
405 p += strlen(p);
406 }
407 if (iarg != 0) {
408 PyOS_snprintf(p, sizeof(buf) - (p - buf),
409 "argument %d", iarg);
410 i = 0;
411 p += strlen(p);
412 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
413 PyOS_snprintf(p, sizeof(buf) - (p - buf),
414 ", item %d", levels[i]-1);
415 p += strlen(p);
416 i++;
417 }
418 }
419 else {
420 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
421 p += strlen(p);
422 }
423 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
424 message = buf;
425 }
426 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000427}
428
429
430/* Convert a tuple argument.
431 On entry, *p_format points to the character _after_ the opening '('.
432 On successful exit, *p_format points to the closing ')'.
433 If successful:
434 *p_format and *p_va are updated,
435 *levels and *msgbuf are untouched,
436 and NULL is returned.
437 If the argument is invalid:
438 *p_format is unchanged,
439 *p_va is undefined,
440 *levels is a 0-terminated list of item numbers,
441 *msgbuf contains an error message, whose format is:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 "must be <typename1>, not <typename2>", where:
443 <typename1> is the name of the expected type, and
444 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000445 and msgbuf is returned.
446*/
447
448static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000449converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000450 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000451 PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 int level = 0;
454 int n = 0;
455 const char *format = *p_format;
456 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 for (;;) {
459 int c = *format++;
460 if (c == '(') {
461 if (level == 0)
462 n++;
463 level++;
464 }
465 else if (c == ')') {
466 if (level == 0)
467 break;
468 level--;
469 }
470 else if (c == ':' || c == ';' || c == '\0')
471 break;
472 else if (level == 0 && isalpha(Py_CHARMASK(c)))
473 n++;
474 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
477 levels[0] = 0;
478 PyOS_snprintf(msgbuf, bufsize,
479 toplevel ? "expected %d arguments, not %.50s" :
480 "must be %d-item sequence, not %.50s",
481 n,
482 arg == Py_None ? "None" : arg->ob_type->tp_name);
483 return msgbuf;
484 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 if ((i = PySequence_Size(arg)) != n) {
487 levels[0] = 0;
488 PyOS_snprintf(msgbuf, bufsize,
489 toplevel ? "expected %d arguments, not %d" :
490 "must be sequence of length %d, not %d",
491 n, i);
492 return msgbuf;
493 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 format = *p_format;
496 for (i = 0; i < n; i++) {
497 char *msg;
498 PyObject *item;
499 item = PySequence_GetItem(arg, i);
500 if (item == NULL) {
501 PyErr_Clear();
502 levels[0] = i+1;
503 levels[1] = 0;
504 strncpy(msgbuf, "is not retrievable", bufsize);
505 return msgbuf;
506 }
507 msg = convertitem(item, &format, p_va, flags, levels+1,
508 msgbuf, bufsize, freelist);
509 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
510 Py_XDECREF(item);
511 if (msg != NULL) {
512 levels[0] = i+1;
513 return msg;
514 }
515 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 *p_format = format;
518 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000519}
520
521
522/* Convert a single item. */
523
524static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000525convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
526 int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 char *msg;
529 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (*format == '(' /* ')' */) {
532 format++;
533 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
534 bufsize, 0, freelist);
535 if (msg == NULL)
536 format++;
537 }
538 else {
539 msg = convertsimple(arg, &format, p_va, flags,
540 msgbuf, bufsize, freelist);
541 if (msg != NULL)
542 levels[0] = 0;
543 }
544 if (msg == NULL)
545 *p_format = format;
546 return msg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000547}
548
549
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000550
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000551#define UNICODE_DEFAULT_ENCODING(arg) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 _PyUnicode_AsDefaultEncodedString(arg, NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000553
554/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000555
556static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000557converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 assert(expected != NULL);
560 assert(arg != NULL);
561 PyOS_snprintf(msgbuf, bufsize,
562 "must be %.50s, not %.50s", expected,
563 arg == Py_None ? "None" : arg->ob_type->tp_name);
564 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000565}
566
567#define CONV_UNICODE "(unicode conversion error)"
568
Guido van Rossum45aecf42006-03-15 04:58:47 +0000569/* Explicitly check for float arguments when integers are expected.
570 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000571static int
572float_argument_error(PyObject *arg)
573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (PyFloat_Check(arg)) {
575 PyErr_SetString(PyExc_TypeError,
576 "integer argument expected, got float" );
577 return 1;
578 }
579 else
580 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000581}
582
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000583/* Convert a non-tuple argument. Return NULL if conversion went OK,
584 or a string with a message describing the failure. The message is
585 formatted as "must be <desired type>, not <actual type>".
586 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000587 Don't call if a tuple is expected.
588
589 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000590*/
591
592static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000593convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000594 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 /* For # codes */
597#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
598 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
599 else q=va_arg(*p_va, int*);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000600#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
601#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 const char *format = *p_format;
604 char c = *format++;
605 PyObject *uarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 case 'b': { /* unsigned byte -- very short int */
610 char *p = va_arg(*p_va, char *);
611 long ival;
612 if (float_argument_error(arg))
613 return converterr("integer<b>", arg, msgbuf, bufsize);
614 ival = PyLong_AsLong(arg);
615 if (ival == -1 && PyErr_Occurred())
616 return converterr("integer<b>", arg, msgbuf, bufsize);
617 else if (ival < 0) {
618 PyErr_SetString(PyExc_OverflowError,
619 "unsigned byte integer is less than minimum");
620 return converterr("integer<b>", arg, msgbuf, bufsize);
621 }
622 else if (ival > UCHAR_MAX) {
623 PyErr_SetString(PyExc_OverflowError,
624 "unsigned byte integer is greater than maximum");
625 return converterr("integer<b>", arg, msgbuf, bufsize);
626 }
627 else
628 *p = (unsigned char) ival;
629 break;
630 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 case 'B': {/* byte sized bitfield - both signed and unsigned
633 values allowed */
634 char *p = va_arg(*p_va, char *);
635 long ival;
636 if (float_argument_error(arg))
637 return converterr("integer<B>", arg, msgbuf, bufsize);
638 ival = PyLong_AsUnsignedLongMask(arg);
639 if (ival == -1 && PyErr_Occurred())
640 return converterr("integer<B>", arg, msgbuf, bufsize);
641 else
642 *p = (unsigned char) ival;
643 break;
644 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 case 'h': {/* signed short int */
647 short *p = va_arg(*p_va, short *);
648 long ival;
649 if (float_argument_error(arg))
650 return converterr("integer<h>", arg, msgbuf, bufsize);
651 ival = PyLong_AsLong(arg);
652 if (ival == -1 && PyErr_Occurred())
653 return converterr("integer<h>", arg, msgbuf, bufsize);
654 else if (ival < SHRT_MIN) {
655 PyErr_SetString(PyExc_OverflowError,
656 "signed short integer is less than minimum");
657 return converterr("integer<h>", arg, msgbuf, bufsize);
658 }
659 else if (ival > SHRT_MAX) {
660 PyErr_SetString(PyExc_OverflowError,
661 "signed short integer is greater than maximum");
662 return converterr("integer<h>", arg, msgbuf, bufsize);
663 }
664 else
665 *p = (short) ival;
666 break;
667 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 case 'H': { /* short int sized bitfield, both signed and
670 unsigned allowed */
671 unsigned short *p = va_arg(*p_va, unsigned short *);
672 long ival;
673 if (float_argument_error(arg))
674 return converterr("integer<H>", arg, msgbuf, bufsize);
675 ival = PyLong_AsUnsignedLongMask(arg);
676 if (ival == -1 && PyErr_Occurred())
677 return converterr("integer<H>", arg, msgbuf, bufsize);
678 else
679 *p = (unsigned short) ival;
680 break;
681 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 case 'i': {/* signed int */
684 int *p = va_arg(*p_va, int *);
685 long ival;
686 if (float_argument_error(arg))
687 return converterr("integer<i>", arg, msgbuf, bufsize);
688 ival = PyLong_AsLong(arg);
689 if (ival == -1 && PyErr_Occurred())
690 return converterr("integer<i>", arg, msgbuf, bufsize);
691 else if (ival > INT_MAX) {
692 PyErr_SetString(PyExc_OverflowError,
693 "signed integer is greater than maximum");
694 return converterr("integer<i>", arg, msgbuf, bufsize);
695 }
696 else if (ival < INT_MIN) {
697 PyErr_SetString(PyExc_OverflowError,
698 "signed integer is less than minimum");
699 return converterr("integer<i>", arg, msgbuf, bufsize);
700 }
701 else
702 *p = ival;
703 break;
704 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 case 'I': { /* int sized bitfield, both signed and
707 unsigned allowed */
708 unsigned int *p = va_arg(*p_va, unsigned int *);
709 unsigned int ival;
710 if (float_argument_error(arg))
711 return converterr("integer<I>", arg, msgbuf, bufsize);
712 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
713 if (ival == (unsigned int)-1 && PyErr_Occurred())
714 return converterr("integer<I>", arg, msgbuf, bufsize);
715 else
716 *p = ival;
717 break;
718 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 case 'n': /* Py_ssize_t */
721 {
722 PyObject *iobj;
723 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
724 Py_ssize_t ival = -1;
725 if (float_argument_error(arg))
726 return converterr("integer<n>", arg, msgbuf, bufsize);
727 iobj = PyNumber_Index(arg);
728 if (iobj != NULL) {
729 ival = PyLong_AsSsize_t(iobj);
730 Py_DECREF(iobj);
731 }
732 if (ival == -1 && PyErr_Occurred())
733 return converterr("integer<n>", arg, msgbuf, bufsize);
734 *p = ival;
735 break;
736 }
737 case 'l': {/* long int */
738 long *p = va_arg(*p_va, long *);
739 long ival;
740 if (float_argument_error(arg))
741 return converterr("integer<l>", arg, msgbuf, bufsize);
742 ival = PyLong_AsLong(arg);
743 if (ival == -1 && PyErr_Occurred())
744 return converterr("integer<l>", arg, msgbuf, bufsize);
745 else
746 *p = ival;
747 break;
748 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 case 'k': { /* long sized bitfield */
751 unsigned long *p = va_arg(*p_va, unsigned long *);
752 unsigned long ival;
753 if (PyLong_Check(arg))
754 ival = PyLong_AsUnsignedLongMask(arg);
755 else
756 return converterr("integer<k>", arg, msgbuf, bufsize);
757 *p = ival;
758 break;
759 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000760
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000761#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 case 'L': {/* PY_LONG_LONG */
763 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
764 PY_LONG_LONG ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000765 if (float_argument_error(arg))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 return converterr("long<L>", arg, msgbuf, bufsize);
767 ival = PyLong_AsLongLong(arg);
Mark Dickinsonc7301312010-06-10 16:05:10 +0000768 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 return converterr("long<L>", arg, msgbuf, bufsize);
Mark Dickinsonc7301312010-06-10 16:05:10 +0000770 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 break;
773 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 case 'K': { /* long long sized bitfield */
776 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
777 unsigned PY_LONG_LONG ival;
778 if (PyLong_Check(arg))
779 ival = PyLong_AsUnsignedLongLongMask(arg);
780 else
781 return converterr("integer<K>", arg, msgbuf, bufsize);
782 *p = ival;
783 break;
784 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000785#endif
Guido van Rossum98297ee2007-11-06 21:34:58 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 case 'f': {/* float */
788 float *p = va_arg(*p_va, float *);
789 double dval = PyFloat_AsDouble(arg);
790 if (PyErr_Occurred())
791 return converterr("float<f>", arg, msgbuf, bufsize);
792 else
793 *p = (float) dval;
794 break;
795 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 case 'd': {/* double */
798 double *p = va_arg(*p_va, double *);
799 double dval = PyFloat_AsDouble(arg);
800 if (PyErr_Occurred())
801 return converterr("float<d>", arg, msgbuf, bufsize);
802 else
803 *p = dval;
804 break;
805 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 case 'D': {/* complex double */
808 Py_complex *p = va_arg(*p_va, Py_complex *);
809 Py_complex cval;
810 cval = PyComplex_AsCComplex(arg);
811 if (PyErr_Occurred())
812 return converterr("complex<D>", arg, msgbuf, bufsize);
813 else
814 *p = cval;
815 break;
816 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 case 'c': {/* char */
819 char *p = va_arg(*p_va, char *);
820 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
821 *p = PyBytes_AS_STRING(arg)[0];
822 else
823 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
824 break;
825 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 case 'C': {/* unicode char */
828 int *p = va_arg(*p_va, int *);
829 if (PyUnicode_Check(arg) &&
830 PyUnicode_GET_SIZE(arg) == 1)
831 *p = PyUnicode_AS_UNICODE(arg)[0];
832 else
833 return converterr("a unicode character", arg, msgbuf, bufsize);
834 break;
835 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000836
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000837 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 case 'y': {/* any buffer-like object, but not PyUnicode */
841 void **p = (void **)va_arg(*p_va, char **);
842 char *buf;
843 Py_ssize_t count;
844 if (*format == '*') {
845 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
846 return converterr(buf, arg, msgbuf, bufsize);
847 format++;
848 if (addcleanup(p, freelist, cleanup_buffer)) {
849 return converterr(
850 "(cleanup problem)",
851 arg, msgbuf, bufsize);
852 }
853 break;
854 }
855 count = convertbuffer(arg, p, &buf);
856 if (count < 0)
857 return converterr(buf, arg, msgbuf, bufsize);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000858 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 FETCH_SIZE;
860 STORE_SIZE(count);
861 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000862 } else {
863 if (strlen(*p) != count)
864 return converterr(
865 "bytes without null bytes",
866 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 }
868 break;
869 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000870
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000871 case 's': /* text string */
872 case 'z': /* text string or None */
873 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000875 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000877
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000878 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
880 else if (PyUnicode_Check(arg)) {
881 uarg = UNICODE_DEFAULT_ENCODING(arg);
882 if (uarg == NULL)
883 return converterr(CONV_UNICODE,
884 arg, msgbuf, bufsize);
885 PyBuffer_FillInfo(p, arg,
886 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
887 1, 0);
888 }
889 else { /* any buffer-like object */
890 char *buf;
891 if (getbuffer(arg, p, &buf) < 0)
892 return converterr(buf, arg, msgbuf, bufsize);
893 }
894 if (addcleanup(p, freelist, cleanup_buffer)) {
895 return converterr(
896 "(cleanup problem)",
897 arg, msgbuf, bufsize);
898 }
899 format++;
900 } else if (*format == '#') { /* any buffer-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000901 /* "s#" or "z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 void **p = (void **)va_arg(*p_va, char **);
903 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000904
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000905 if (c == 'z' && arg == Py_None) {
906 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 STORE_SIZE(0);
908 }
909 else if (PyUnicode_Check(arg)) {
910 uarg = UNICODE_DEFAULT_ENCODING(arg);
911 if (uarg == NULL)
912 return converterr(CONV_UNICODE,
913 arg, msgbuf, bufsize);
914 *p = PyBytes_AS_STRING(uarg);
915 STORE_SIZE(PyBytes_GET_SIZE(uarg));
916 }
917 else { /* any buffer-like object */
918 /* XXX Really? */
919 char *buf;
920 Py_ssize_t count = convertbuffer(arg, p, &buf);
921 if (count < 0)
922 return converterr(buf, arg, msgbuf, bufsize);
923 STORE_SIZE(count);
924 }
925 format++;
926 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000927 /* "s" or "z" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 char **p = va_arg(*p_va, char **);
929 uarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000930
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000931 if (c == 'z' && arg == Py_None)
932 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 else if (PyUnicode_Check(arg)) {
934 uarg = UNICODE_DEFAULT_ENCODING(arg);
935 if (uarg == NULL)
936 return converterr(CONV_UNICODE,
937 arg, msgbuf, bufsize);
938 *p = PyBytes_AS_STRING(uarg);
939 }
940 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000941 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 arg, msgbuf, bufsize);
Victor Stinner43fb0092010-05-29 00:13:06 +0000943 if (*p != NULL && uarg != NULL &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 (Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
945 return converterr(
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000946 c == 'z' ? "str without null bytes or None"
947 : "str without null bytes",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 arg, msgbuf, bufsize);
949 }
950 break;
951 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000952
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000953 case 'u': /* raw unicode buffer (Py_UNICODE *) */
954 case 'Z': /* raw unicode buffer or None */
955 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (*format == '#') { /* any buffer-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000957 /* "s#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
959 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +0000960
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000961 if (c == 'Z' && arg == Py_None) {
962 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 STORE_SIZE(0);
964 }
965 else if (PyUnicode_Check(arg)) {
966 *p = PyUnicode_AS_UNICODE(arg);
967 STORE_SIZE(PyUnicode_GET_SIZE(arg));
968 }
Victor Stinner5216e6d2010-06-08 21:45:51 +0000969 else
970 return converterr("str or None", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 format++;
972 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000973 /* "s" or "Z" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000975
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000976 if (c == 'Z' && arg == Py_None)
977 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000978 else if (PyUnicode_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 *p = PyUnicode_AS_UNICODE(arg);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000980 if (Py_UNICODE_strlen(*p) != PyUnicode_GET_SIZE(arg))
981 return converterr(
982 "str without null character or None",
983 arg, msgbuf, bufsize);
984 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000985 return converterr(c == 'Z' ? "str or None" : "str",
986 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 }
988 break;
989 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 case 'e': {/* encoded string */
992 char **buffer;
993 const char *encoding;
994 PyObject *s;
995 int recode_strings;
996 Py_ssize_t size;
997 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 /* Get 'e' parameter: the encoding name */
1000 encoding = (const char *)va_arg(*p_va, const char *);
1001 if (encoding == NULL)
1002 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 /* Get output buffer parameter:
1005 's' (recode all objects via Unicode) or
1006 't' (only recode non-string objects)
1007 */
1008 if (*format == 's')
1009 recode_strings = 1;
1010 else if (*format == 't')
1011 recode_strings = 0;
1012 else
1013 return converterr(
1014 "(unknown parser marker combination)",
1015 arg, msgbuf, bufsize);
1016 buffer = (char **)va_arg(*p_va, char **);
1017 format++;
1018 if (buffer == NULL)
1019 return converterr("(buffer is NULL)",
1020 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 /* Encode object */
1023 if (!recode_strings &&
1024 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1025 s = arg;
1026 Py_INCREF(s);
1027 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1028 return converterr("(AsCharBuffer failed)",
1029 arg, msgbuf, bufsize);
1030 }
1031 else {
1032 PyObject *u;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 /* Convert object to Unicode */
1035 u = PyUnicode_FromObject(arg);
1036 if (u == NULL)
1037 return converterr(
1038 "string or unicode or text buffer",
1039 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 /* Encode object; use default error handling */
1042 s = PyUnicode_AsEncodedString(u,
1043 encoding,
1044 NULL);
1045 Py_DECREF(u);
1046 if (s == NULL)
1047 return converterr("(encoding failed)",
1048 arg, msgbuf, bufsize);
1049 if (!PyBytes_Check(s)) {
1050 Py_DECREF(s);
1051 return converterr(
1052 "(encoder failed to return bytes)",
1053 arg, msgbuf, bufsize);
1054 }
1055 size = PyBytes_GET_SIZE(s);
1056 ptr = PyBytes_AS_STRING(s);
1057 if (ptr == NULL)
1058 ptr = "";
1059 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 /* Write output; output is guaranteed to be 0-terminated */
1062 if (*format == '#') {
1063 /* Using buffer length parameter '#':
1064
1065 - if *buffer is NULL, a new buffer of the
1066 needed size is allocated and the data
1067 copied into it; *buffer is updated to point
1068 to the new buffer; the caller is
1069 responsible for PyMem_Free()ing it after
1070 usage
1071
1072 - if *buffer is not NULL, the data is
1073 copied to *buffer; *buffer_len has to be
1074 set to the size of the buffer on input;
1075 buffer overflow is signalled with an error;
1076 buffer has to provide enough room for the
1077 encoded string plus the trailing 0-byte
1078
1079 - in both cases, *buffer_len is updated to
1080 the size of the buffer /excluding/ the
1081 trailing 0-byte
1082
1083 */
1084 FETCH_SIZE;
1085
1086 format++;
1087 if (q == NULL && q2 == NULL) {
1088 Py_DECREF(s);
1089 return converterr(
1090 "(buffer_len is NULL)",
1091 arg, msgbuf, bufsize);
1092 }
1093 if (*buffer == NULL) {
1094 *buffer = PyMem_NEW(char, size + 1);
1095 if (*buffer == NULL) {
1096 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001097 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 return converterr(
1099 "(memory error)",
1100 arg, msgbuf, bufsize);
1101 }
1102 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1103 Py_DECREF(s);
1104 return converterr(
1105 "(cleanup problem)",
1106 arg, msgbuf, bufsize);
1107 }
1108 } else {
1109 if (size + 1 > BUFFER_LEN) {
1110 Py_DECREF(s);
1111 return converterr(
1112 "(buffer overflow)",
1113 arg, msgbuf, bufsize);
1114 }
1115 }
1116 memcpy(*buffer, ptr, size+1);
1117 STORE_SIZE(size);
1118 } else {
1119 /* Using a 0-terminated buffer:
1120
1121 - the encoded string has to be 0-terminated
1122 for this variant to work; if it is not, an
1123 error raised
1124
1125 - a new buffer of the needed size is
1126 allocated and the data copied into it;
1127 *buffer is updated to point to the new
1128 buffer; the caller is responsible for
1129 PyMem_Free()ing it after usage
1130
1131 */
1132 if ((Py_ssize_t)strlen(ptr) != size) {
1133 Py_DECREF(s);
1134 return converterr(
1135 "encoded string without NULL bytes",
1136 arg, msgbuf, bufsize);
1137 }
1138 *buffer = PyMem_NEW(char, size + 1);
1139 if (*buffer == NULL) {
1140 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001141 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 return converterr("(memory error)",
1143 arg, msgbuf, bufsize);
1144 }
1145 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
1146 Py_DECREF(s);
1147 return converterr("(cleanup problem)",
1148 arg, msgbuf, bufsize);
1149 }
1150 memcpy(*buffer, ptr, size+1);
1151 }
1152 Py_DECREF(s);
1153 break;
1154 }
1155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 case 'S': { /* PyBytes object */
1157 PyObject **p = va_arg(*p_va, PyObject **);
1158 if (PyBytes_Check(arg))
1159 *p = arg;
1160 else
1161 return converterr("bytes", arg, msgbuf, bufsize);
1162 break;
1163 }
1164
1165 case 'Y': { /* PyByteArray object */
1166 PyObject **p = va_arg(*p_va, PyObject **);
1167 if (PyByteArray_Check(arg))
1168 *p = arg;
1169 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001170 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 break;
1172 }
1173
1174 case 'U': { /* PyUnicode object */
1175 PyObject **p = va_arg(*p_va, PyObject **);
1176 if (PyUnicode_Check(arg))
1177 *p = arg;
1178 else
1179 return converterr("str", arg, msgbuf, bufsize);
1180 break;
1181 }
1182
1183 case 'O': { /* object */
1184 PyTypeObject *type;
1185 PyObject **p;
1186 if (*format == '!') {
1187 type = va_arg(*p_va, PyTypeObject*);
1188 p = va_arg(*p_va, PyObject **);
1189 format++;
1190 if (PyType_IsSubtype(arg->ob_type, type))
1191 *p = arg;
1192 else
1193 return converterr(type->tp_name, arg, msgbuf, bufsize);
1194
1195 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 else if (*format == '&') {
1197 typedef int (*converter)(PyObject *, void *);
1198 converter convert = va_arg(*p_va, converter);
1199 void *addr = va_arg(*p_va, void *);
1200 int res;
1201 format++;
1202 if (! (res = (*convert)(arg, addr)))
1203 return converterr("(unspecified)",
1204 arg, msgbuf, bufsize);
1205 if (res == Py_CLEANUP_SUPPORTED &&
1206 addcleanup_convert(addr, freelist, convert) == -1)
1207 return converterr("(cleanup problem)",
1208 arg, msgbuf, bufsize);
1209 }
1210 else {
1211 p = va_arg(*p_va, PyObject **);
1212 *p = arg;
1213 }
1214 break;
1215 }
1216
1217
Victor Stinner25e8ec42010-06-25 00:02:38 +00001218 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220
Victor Stinner25e8ec42010-06-25 00:02:38 +00001221 if (*format != '*')
1222 return converterr(
1223 "invalid use of 'w' format character",
1224 arg, msgbuf, bufsize);
1225 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226
Victor Stinner25e8ec42010-06-25 00:02:38 +00001227 /* Caller is interested in Py_buffer, and the object
1228 supports it directly. */
1229 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1230 PyErr_Clear();
1231 return converterr("read-write buffer", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 }
Victor Stinner8182b712010-07-28 00:40:58 +00001233 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1234 PyBuffer_Release((Py_buffer*)p);
1235 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1236 }
Victor Stinner25e8ec42010-06-25 00:02:38 +00001237 if (addcleanup(p, freelist, cleanup_buffer)) {
1238 return converterr(
1239 "(cleanup problem)",
1240 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 break;
1243 }
1244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 default:
1246 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
1247
1248 }
1249
1250 *p_format = format;
1251 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001252}
Guido van Rossumaa354651996-08-19 19:32:04 +00001253
Martin v. Löwis18e16552006-02-15 17:27:45 +00001254static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001255convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001256{
Victor Stinner5cb62392010-06-06 20:27:51 +00001257 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 Py_ssize_t count;
1259 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 *errmsg = NULL;
1262 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001263 if (pb != NULL && pb->bf_releasebuffer != NULL) {
1264 *errmsg = "read-only pinned buffer";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 return -1;
1266 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001267
Victor Stinner8182b712010-07-28 00:40:58 +00001268 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 count = view.len;
1271 *p = view.buf;
1272 PyBuffer_Release(&view);
1273 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001274}
Guido van Rossumaa354651996-08-19 19:32:04 +00001275
Martin v. Löwis423be952008-08-13 15:53:07 +00001276static int
Neal Norwitz2f99b242008-08-24 05:48:10 +00001277getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001278{
Victor Stinner8182b712010-07-28 00:40:58 +00001279 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 *errmsg = "bytes or buffer";
1281 return -1;
1282 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001283 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001284 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001285 *errmsg = "contiguous buffer";
1286 return -1;
1287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001289}
1290
Guido van Rossumaa354651996-08-19 19:32:04 +00001291/* Support for keyword arguments donated by
1292 Geoff Philbrick <philbric@delphi.hks.com> */
1293
Tim Petersf8cd3e82001-10-27 04:26:57 +00001294/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001295int
1296PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 PyObject *keywords,
1298 const char *format,
1299 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 int retval;
1302 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 if ((args == NULL || !PyTuple_Check(args)) ||
1305 (keywords != NULL && !PyDict_Check(keywords)) ||
1306 format == NULL ||
1307 kwlist == NULL)
1308 {
1309 PyErr_BadInternalCall();
1310 return 0;
1311 }
Tim Peters45772cd2001-10-27 03:58:40 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 va_start(va, kwlist);
1314 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1315 va_end(va);
1316 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001317}
1318
1319int
1320_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 PyObject *keywords,
1322 const char *format,
1323 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 int retval;
1326 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 if ((args == NULL || !PyTuple_Check(args)) ||
1329 (keywords != NULL && !PyDict_Check(keywords)) ||
1330 format == NULL ||
1331 kwlist == NULL)
1332 {
1333 PyErr_BadInternalCall();
1334 return 0;
1335 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 va_start(va, kwlist);
1338 retval = vgetargskeywords(args, keywords, format,
1339 kwlist, &va, FLAG_SIZE_T);
1340 va_end(va);
1341 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001342}
1343
1344
Brett Cannon711e7d92004-07-10 22:20:32 +00001345int
1346PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001347 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001348 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001349 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 int retval;
1352 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if ((args == NULL || !PyTuple_Check(args)) ||
1355 (keywords != NULL && !PyDict_Check(keywords)) ||
1356 format == NULL ||
1357 kwlist == NULL)
1358 {
1359 PyErr_BadInternalCall();
1360 return 0;
1361 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001362
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001363 Py_VA_COPY(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1366 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001367}
1368
1369int
1370_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 PyObject *keywords,
1372 const char *format,
1373 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 int retval;
1376 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 if ((args == NULL || !PyTuple_Check(args)) ||
1379 (keywords != NULL && !PyDict_Check(keywords)) ||
1380 format == NULL ||
1381 kwlist == NULL)
1382 {
1383 PyErr_BadInternalCall();
1384 return 0;
1385 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001386
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001387 Py_VA_COPY(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 retval = vgetargskeywords(args, keywords, format,
1390 kwlist, &lva, FLAG_SIZE_T);
1391 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001392}
1393
Benjamin Petersonfb886362010-04-24 18:21:17 +00001394int
1395PyArg_ValidateKeywordArguments(PyObject *kwargs)
1396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 if (!PyDict_CheckExact(kwargs)) {
1398 PyErr_BadInternalCall();
1399 return 0;
1400 }
1401 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1402 PyErr_SetString(PyExc_TypeError,
1403 "keyword arguments must be strings");
1404 return 0;
1405 }
1406 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001407}
1408
Christian Heimes380f7f22008-02-28 11:19:05 +00001409#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001410
Guido van Rossumaa354651996-08-19 19:32:04 +00001411static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001412vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 char msgbuf[512];
1416 int levels[32];
1417 const char *fname, *msg, *custom_msg, *keyword;
1418 int min = INT_MAX;
1419 int i, len, nargs, nkeywords;
1420 PyObject *freelist = NULL, *current_arg;
Tim Petersf4331c12001-10-27 00:17:34 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 assert(args != NULL && PyTuple_Check(args));
1423 assert(keywords == NULL || PyDict_Check(keywords));
1424 assert(format != NULL);
1425 assert(kwlist != NULL);
1426 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 /* grab the function name or custom error msg first (mutually exclusive) */
1429 fname = strchr(format, ':');
1430 if (fname) {
1431 fname++;
1432 custom_msg = NULL;
1433 }
1434 else {
1435 custom_msg = strchr(format,';');
1436 if (custom_msg)
1437 custom_msg++;
1438 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 /* scan kwlist and get greatest possible nbr of args */
1441 for (len=0; kwlist[len]; len++)
1442 continue;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 nargs = PyTuple_GET_SIZE(args);
1445 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1446 if (nargs + nkeywords > len) {
1447 PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
1448 "argument%s (%d given)",
1449 (fname == NULL) ? "function" : fname,
1450 (fname == NULL) ? "" : "()",
1451 len,
1452 (len == 1) ? "" : "s",
1453 nargs + nkeywords);
1454 return 0;
1455 }
Tim Petersc2f01122001-10-27 07:25:06 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1458 for (i = 0; i < len; i++) {
1459 keyword = kwlist[i];
1460 if (*format == '|') {
1461 min = i;
1462 format++;
1463 }
1464 if (IS_END_OF_FORMAT(*format)) {
1465 PyErr_Format(PyExc_RuntimeError,
1466 "More keyword list entries (%d) than "
1467 "format specifiers (%d)", len, i);
1468 return cleanreturn(0, freelist);
1469 }
1470 current_arg = NULL;
1471 if (nkeywords) {
1472 current_arg = PyDict_GetItemString(keywords, keyword);
1473 }
1474 if (current_arg) {
1475 --nkeywords;
1476 if (i < nargs) {
1477 /* arg present in tuple and in dict */
1478 PyErr_Format(PyExc_TypeError,
1479 "Argument given by name ('%s') "
1480 "and position (%d)",
1481 keyword, i+1);
1482 return cleanreturn(0, freelist);
1483 }
1484 }
1485 else if (nkeywords && PyErr_Occurred())
1486 return cleanreturn(0, freelist);
1487 else if (i < nargs)
1488 current_arg = PyTuple_GET_ITEM(args, i);
Christian Heimes380f7f22008-02-28 11:19:05 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 if (current_arg) {
1491 msg = convertitem(current_arg, &format, p_va, flags,
1492 levels, msgbuf, sizeof(msgbuf), &freelist);
1493 if (msg) {
1494 seterror(i+1, msg, levels, fname, custom_msg);
1495 return cleanreturn(0, freelist);
1496 }
1497 continue;
1498 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 if (i < min) {
1501 PyErr_Format(PyExc_TypeError, "Required argument "
1502 "'%s' (pos %d) not found",
1503 keyword, i+1);
1504 return cleanreturn(0, freelist);
1505 }
1506 /* current code reports success when all required args
1507 * fulfilled and no keyword args left, with no further
1508 * validation. XXX Maybe skip this in debug build ?
1509 */
1510 if (!nkeywords)
1511 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 /* We are into optional args, skip thru to any remaining
1514 * keyword args */
1515 msg = skipitem(&format, p_va, flags);
1516 if (msg) {
1517 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1518 format);
1519 return cleanreturn(0, freelist);
1520 }
1521 }
Tim Petersb054be42001-10-27 05:07:41 +00001522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (!IS_END_OF_FORMAT(*format) && *format != '|') {
1524 PyErr_Format(PyExc_RuntimeError,
1525 "more argument specifiers than keyword list entries "
1526 "(remaining format:'%s')", format);
1527 return cleanreturn(0, freelist);
1528 }
Tim Petersc2f01122001-10-27 07:25:06 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 /* make sure there are no extraneous keyword arguments */
1531 if (nkeywords > 0) {
1532 PyObject *key, *value;
1533 Py_ssize_t pos = 0;
1534 while (PyDict_Next(keywords, &pos, &key, &value)) {
1535 int match = 0;
1536 char *ks;
1537 if (!PyUnicode_Check(key)) {
1538 PyErr_SetString(PyExc_TypeError,
1539 "keywords must be strings");
1540 return cleanreturn(0, freelist);
1541 }
Victor Stinner93b55132010-05-19 00:54:06 +00001542 /* check that _PyUnicode_AsString() result is not NULL */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 ks = _PyUnicode_AsString(key);
Victor Stinner93b55132010-05-19 00:54:06 +00001544 if (ks != NULL) {
1545 for (i = 0; i < len; i++) {
1546 if (!strcmp(ks, kwlist[i])) {
1547 match = 1;
1548 break;
1549 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 }
1551 }
1552 if (!match) {
1553 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001554 "'%U' is an invalid keyword "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 "argument for this function",
Victor Stinner93b55132010-05-19 00:54:06 +00001556 key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 return cleanreturn(0, freelist);
1558 }
1559 }
1560 }
1561
1562 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001563}
1564
1565
1566static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001567skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 const char *format = *p_format;
1570 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 /* simple codes
1575 * The individual types (second arg of va_arg) are irrelevant */
Georg Brandl6dd14612005-09-14 19:29:53 +00001576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 case 'b': /* byte -- very short int */
1578 case 'B': /* byte as bitfield */
1579 case 'h': /* short int */
1580 case 'H': /* short int as bitfield */
1581 case 'i': /* int */
1582 case 'I': /* int sized bitfield */
1583 case 'l': /* long int */
1584 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001585#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 case 'L': /* PY_LONG_LONG */
1587 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001588#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 case 'f': /* float */
1590 case 'd': /* double */
1591 case 'D': /* complex double */
1592 case 'c': /* char */
1593 case 'C': /* unicode char */
1594 {
1595 (void) va_arg(*p_va, void *);
1596 break;
1597 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 case 'n': /* Py_ssize_t */
1600 {
1601 (void) va_arg(*p_va, Py_ssize_t *);
1602 break;
1603 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 case 'e': /* string with encoding */
1608 {
1609 (void) va_arg(*p_va, const char *);
1610 if (!(*format == 's' || *format == 't'))
1611 /* after 'e', only 's' and 't' is allowed */
1612 goto err;
1613 format++;
1614 /* explicit fallthrough to string cases */
1615 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 case 's': /* string */
1618 case 'z': /* string or None */
1619 case 'y': /* bytes */
1620 case 'u': /* unicode string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 case 'w': /* buffer, read-write */
1622 {
1623 (void) va_arg(*p_va, char **);
1624 if (*format == '#') {
1625 if (flags & FLAG_SIZE_T)
1626 (void) va_arg(*p_va, Py_ssize_t *);
1627 else
1628 (void) va_arg(*p_va, int *);
1629 format++;
1630 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
1631 format++;
1632 }
1633 break;
1634 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 /* object codes */
Georg Brandl6dd14612005-09-14 19:29:53 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 case 'S': /* string object */
1639 case 'Y': /* string object */
1640 case 'U': /* unicode string object */
1641 {
1642 (void) va_arg(*p_va, PyObject **);
1643 break;
1644 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 case 'O': /* object */
1647 {
1648 if (*format == '!') {
1649 format++;
1650 (void) va_arg(*p_va, PyTypeObject*);
1651 (void) va_arg(*p_va, PyObject **);
1652 }
1653 else if (*format == '&') {
1654 typedef int (*converter)(PyObject *, void *);
1655 (void) va_arg(*p_va, converter);
1656 (void) va_arg(*p_va, void *);
1657 format++;
1658 }
1659 else {
1660 (void) va_arg(*p_va, PyObject **);
1661 }
1662 break;
1663 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 case '(': /* bypass tuple, not handled at all previously */
1666 {
1667 char *msg;
1668 for (;;) {
1669 if (*format==')')
1670 break;
1671 if (IS_END_OF_FORMAT(*format))
1672 return "Unmatched left paren in format "
1673 "string";
1674 msg = skipitem(&format, p_va, flags);
1675 if (msg)
1676 return msg;
1677 }
1678 format++;
1679 break;
1680 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 case ')':
1683 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00001684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001686err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00001688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 *p_format = format;
1692 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00001693}
Fred Drakee4616e62001-10-23 21:09:29 +00001694
1695
1696int
Martin v. Löwis76246742006-03-01 04:06:10 +00001697PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 Py_ssize_t i, l;
1700 PyObject **o;
1701 va_list vargs;
Fred Drakee4616e62001-10-23 21:09:29 +00001702
1703#ifdef HAVE_STDARG_PROTOTYPES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 va_start(vargs, max);
Fred Drakee4616e62001-10-23 21:09:29 +00001705#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 va_start(vargs);
Fred Drakee4616e62001-10-23 21:09:29 +00001707#endif
1708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 assert(min >= 0);
1710 assert(min <= max);
1711 if (!PyTuple_Check(args)) {
1712 PyErr_SetString(PyExc_SystemError,
1713 "PyArg_UnpackTuple() argument list is not a tuple");
1714 return 0;
1715 }
1716 l = PyTuple_GET_SIZE(args);
1717 if (l < min) {
1718 if (name != NULL)
1719 PyErr_Format(
1720 PyExc_TypeError,
1721 "%s expected %s%zd arguments, got %zd",
1722 name, (min == max ? "" : "at least "), min, l);
1723 else
1724 PyErr_Format(
1725 PyExc_TypeError,
1726 "unpacked tuple should have %s%zd elements,"
1727 " but has %zd",
1728 (min == max ? "" : "at least "), min, l);
1729 va_end(vargs);
1730 return 0;
1731 }
1732 if (l > max) {
1733 if (name != NULL)
1734 PyErr_Format(
1735 PyExc_TypeError,
1736 "%s expected %s%zd arguments, got %zd",
1737 name, (min == max ? "" : "at most "), max, 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 most "), max, l);
1744 va_end(vargs);
1745 return 0;
1746 }
1747 for (i = 0; i < l; i++) {
1748 o = va_arg(vargs, PyObject **);
1749 *o = PyTuple_GET_ITEM(args, i);
1750 }
1751 va_end(vargs);
1752 return 1;
Fred Drakee4616e62001-10-23 21:09:29 +00001753}
Georg Brandl02c42872005-08-26 06:42:30 +00001754
1755
1756/* For type constructors that don't take keyword args
1757 *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001758 * Sets a TypeError and returns 0 if the kwds dict is
Thomas Wouters89f507f2006-12-13 04:49:30 +00001759 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001760 */
1761int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001762_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 if (kw == NULL)
1765 return 1;
1766 if (!PyDict_CheckExact(kw)) {
1767 PyErr_BadInternalCall();
1768 return 0;
1769 }
1770 if (PyDict_Size(kw) == 0)
1771 return 1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1774 funcname);
1775 return 0;
Georg Brandl02c42872005-08-26 06:42:30 +00001776}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001777#ifdef __cplusplus
1778};
1779#endif