blob: 8d90111e2dec2fccce32f1d438f066303ef60dae [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
10extern "C" {
11#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 *,
Martin v. Löwis15e62742006-02-27 16:46:16 +000017 const char *, char **, ...);
Brett Cannon711e7d92004-07-10 22:20:32 +000018int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
Martin v. Löwis15e62742006-02-27 16:46:16 +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 *);
Martin v. Löwis18e16552006-02-15 17:27:45 +000040static char *convertitem(PyObject *, const char **, va_list *, int, int *,
41 char *, size_t, PyObject **);
42static char *converttuple(PyObject *, const char **, va_list *, int,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +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 *,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +000045 size_t, PyObject **);
Martin v. Löwis18e16552006-02-15 17:27:45 +000046static Py_ssize_t convertbuffer(PyObject *, void **p, char **);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000047
Tim Petersdbd9ba62000-07-09 03:09:57 +000048static int vgetargskeywords(PyObject *, PyObject *,
Martin v. Löwis15e62742006-02-27 16:46:16 +000049 const char *, char **, va_list *, int);
Martin v. Löwis18e16552006-02-15 17:27:45 +000050static char *skipitem(const char **, va_list *, int);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000051
Fred Drake563dfc22001-10-23 14:41:08 +000052int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000053PyArg_Parse(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000054{
55 int retval;
56 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000057
58 va_start(va, format);
Martin v. Löwis18e16552006-02-15 17:27:45 +000059 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
60 va_end(va);
61 return retval;
62}
63
64int
65_PyArg_Parse_SizeT(PyObject *args, char *format, ...)
66{
67 int retval;
68 va_list va;
69
70 va_start(va, format);
71 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000072 va_end(va);
73 return retval;
74}
75
76
Fred Drake563dfc22001-10-23 14:41:08 +000077int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000078PyArg_ParseTuple(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000079{
80 int retval;
81 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000082
83 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000084 retval = vgetargs1(args, format, &va, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000085 va_end(va);
86 return retval;
87}
88
Martin v. Löwis18e16552006-02-15 17:27:45 +000089int
90_PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
91{
92 int retval;
93 va_list va;
94
95 va_start(va, format);
96 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
97 va_end(va);
98 return retval;
99}
100
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000101
102int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000103PyArg_VaParse(PyObject *args, const char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000104{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000105 va_list lva;
106
107#ifdef VA_LIST_IS_ARRAY
108 memcpy(lva, va, sizeof(va_list));
109#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000110#ifdef __va_copy
111 __va_copy(lva, va);
112#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000113 lva = va;
114#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000115#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000116
117 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000118}
119
Martin v. Löwis18e16552006-02-15 17:27:45 +0000120int
121_PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
122{
123 va_list lva;
124
125#ifdef VA_LIST_IS_ARRAY
126 memcpy(lva, va, sizeof(va_list));
127#else
128#ifdef __va_copy
129 __va_copy(lva, va);
130#else
131 lva = va;
132#endif
133#endif
134
135 return vgetargs1(args, format, &lva, FLAG_SIZE_T);
136}
137
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000138
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000139/* Handle cleanup of allocated memory in case of exception */
140
141static int
142addcleanup(void *ptr, PyObject **freelist)
143{
144 PyObject *cobj;
145 if (!*freelist) {
146 *freelist = PyList_New(0);
147 if (!*freelist) {
148 PyMem_FREE(ptr);
149 return -1;
150 }
151 }
152 cobj = PyCObject_FromVoidPtr(ptr, NULL);
153 if (!cobj) {
154 PyMem_FREE(ptr);
155 return -1;
156 }
157 if(PyList_Append(*freelist, cobj)) {
158 PyMem_FREE(ptr);
159 Py_DECREF(cobj);
160 return -1;
161 }
162 Py_DECREF(cobj);
163 return 0;
164}
165
166static int
167cleanreturn(int retval, PyObject *freelist)
168{
169 if(freelist) {
170 if((retval) == 0) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000171 Py_ssize_t len = PyList_GET_SIZE(freelist), i;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000172 for (i = 0; i < len; i++)
173 PyMem_FREE(PyCObject_AsVoidPtr(
174 PyList_GET_ITEM(freelist, i)));
175 }
176 Py_DECREF(freelist);
177 }
178 return retval;
179}
180
181
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000182static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000183vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000184{
185 char msgbuf[256];
186 int levels[32];
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000187 const char *fname = NULL;
188 const char *message = NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000189 int min = -1;
190 int max = 0;
191 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000192 int endfmt = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000193 const char *formatsave = format;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000194 Py_ssize_t i, len;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000195 char *msg;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000196 PyObject *freelist = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000197 int compat = flags & FLAG_COMPAT;
198
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000199 assert(compat || (args != (PyObject*)NULL));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000200 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000201
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000202 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000203 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000204 switch (c) {
205 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000206 if (level == 0)
207 max++;
208 level++;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000209 if (level >= 30)
210 Py_FatalError("too many tuple nesting levels "
211 "in argument format string");
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000212 break;
213 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000214 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000215 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000216 else
217 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000218 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000219 case '\0':
220 endfmt = 1;
221 break;
222 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000223 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000224 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000225 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000226 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000227 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000228 endfmt = 1;
229 break;
230 default:
231 if (level == 0) {
232 if (c == 'O')
233 max++;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000234 else if (isalpha(Py_CHARMASK(c))) {
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000235 if (c != 'e') /* skip encoded */
236 max++;
237 } else if (c == '|')
238 min = max;
239 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000240 break;
241 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000242 }
243
244 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000245 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000246
247 if (min < 0)
248 min = max;
249
250 format = formatsave;
251
252 if (compat) {
253 if (max == 0) {
254 if (args == NULL)
255 return 1;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000256 PyOS_snprintf(msgbuf, sizeof(msgbuf),
257 "%.200s%s takes no arguments",
258 fname==NULL ? "function" : fname,
259 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000260 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000261 return 0;
262 }
263 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000264 if (args == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000265 PyOS_snprintf(msgbuf, sizeof(msgbuf),
266 "%.200s%s takes at least one argument",
267 fname==NULL ? "function" : fname,
268 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000269 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000270 return 0;
271 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000272 msg = convertitem(args, &format, p_va, flags, levels,
273 msgbuf, sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000274 if (msg == NULL)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000275 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000276 seterror(levels[0], msg, levels+1, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000277 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000278 }
279 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000280 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000281 "old style getargs format uses new features");
282 return 0;
283 }
284 }
285
Guido van Rossum79f25d91997-04-29 20:08:16 +0000286 if (!PyTuple_Check(args)) {
287 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000288 "new style getargs format but argument is not a tuple");
289 return 0;
290 }
291
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000292 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000293
294 if (len < min || max < len) {
295 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000296 PyOS_snprintf(msgbuf, sizeof(msgbuf),
297 "%.150s%s takes %s %d argument%s "
Neal Norwitz20dd93f2006-02-19 19:34:15 +0000298 "(%ld given)",
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000299 fname==NULL ? "function" : fname,
300 fname==NULL ? "" : "()",
301 min==max ? "exactly"
302 : len < min ? "at least" : "at most",
303 len < min ? min : max,
304 (len < min ? min : max) == 1 ? "" : "s",
Neal Norwitz9a276172006-02-20 18:57:39 +0000305 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000306 message = msgbuf;
307 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000308 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000309 return 0;
310 }
311
312 for (i = 0; i < len; i++) {
313 if (*format == '|')
314 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000315 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000316 flags, levels, msgbuf,
317 sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000318 if (msg) {
319 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000320 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000321 }
322 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000323
Neal Norwitz4ac13df2005-12-19 06:10:07 +0000324 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000325 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000326 *format != '|' && *format != ':' && *format != ';') {
327 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000328 "bad format string: %.200s", formatsave);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000329 return cleanreturn(0, freelist);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000330 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000331
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000332 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000333}
334
335
336
337static void
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000338seterror(int iarg, const char *msg, int *levels, const char *fname,
339 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000340{
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000341 char buf[512];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000342 int i;
343 char *p = buf;
344
Guido van Rossum79f25d91997-04-29 20:08:16 +0000345 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000346 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000347 else if (message == NULL) {
348 if (fname != NULL) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000349 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000350 p += strlen(p);
351 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000352 if (iarg != 0) {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000353 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000354 "argument %d", iarg);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000355 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000356 p += strlen(p);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000357 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
358 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000359 ", item %d", levels[i]-1);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000360 p += strlen(p);
361 i++;
362 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000363 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000364 else {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000365 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
Ka-Ping Yee20579702001-01-15 22:14:16 +0000366 p += strlen(p);
367 }
Tim Petersfaad5ad2001-12-03 00:43:33 +0000368 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000369 message = buf;
370 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000371 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000372}
373
374
375/* Convert a tuple argument.
376 On entry, *p_format points to the character _after_ the opening '('.
377 On successful exit, *p_format points to the closing ')'.
378 If successful:
379 *p_format and *p_va are updated,
380 *levels and *msgbuf are untouched,
381 and NULL is returned.
382 If the argument is invalid:
383 *p_format is unchanged,
384 *p_va is undefined,
385 *levels is a 0-terminated list of item numbers,
386 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000387 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000388 <typename1> is the name of the expected type, and
389 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000390 and msgbuf is returned.
391*/
392
393static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000394converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
395 int *levels, char *msgbuf, size_t bufsize, int toplevel,
396 PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000397{
398 int level = 0;
399 int n = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000400 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000401 int i;
402
403 for (;;) {
404 int c = *format++;
405 if (c == '(') {
406 if (level == 0)
407 n++;
408 level++;
409 }
410 else if (c == ')') {
411 if (level == 0)
412 break;
413 level--;
414 }
415 else if (c == ':' || c == ';' || c == '\0')
416 break;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000417 else if (level == 0 && isalpha(Py_CHARMASK(c)))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000418 n++;
419 }
420
Ka-Ping Yee20579702001-01-15 22:14:16 +0000421 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000422 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000423 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000424 toplevel ? "expected %d arguments, not %.50s" :
425 "must be %d-item sequence, not %.50s",
426 n,
427 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000428 return msgbuf;
429 }
430
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000431 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000432 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000433 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000434 toplevel ? "expected %d arguments, not %d" :
435 "must be sequence of length %d, not %d",
436 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000437 return msgbuf;
438 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000439
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000440 format = *p_format;
441 for (i = 0; i < n; i++) {
442 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000443 PyObject *item;
444 item = PySequence_GetItem(arg, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000445 if (item == NULL) {
446 PyErr_Clear();
447 levels[0] = i+1;
448 levels[1] = 0;
449 strncpy(msgbuf, "is not retrievable", bufsize);
450 return msgbuf;
451 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000452 msg = convertitem(item, &format, p_va, flags, levels+1,
453 msgbuf, bufsize, freelist);
Guido van Rossum66368cc1999-02-17 23:16:43 +0000454 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
455 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000456 if (msg != NULL) {
457 levels[0] = i+1;
458 return msg;
459 }
460 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000461
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000462 *p_format = format;
463 return NULL;
464}
465
466
467/* Convert a single item. */
468
469static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000470convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
471 int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000472{
473 char *msg;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000474 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000475
476 if (*format == '(' /* ')' */) {
477 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000478 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000479 bufsize, 0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000480 if (msg == NULL)
481 format++;
482 }
483 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000484 msg = convertsimple(arg, &format, p_va, flags,
485 msgbuf, bufsize, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000486 if (msg != NULL)
487 levels[0] = 0;
488 }
489 if (msg == NULL)
490 *p_format = format;
491 return msg;
492}
493
494
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000495
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000496#define UNICODE_DEFAULT_ENCODING(arg) \
497 _PyUnicode_AsDefaultEncodedString(arg, NULL)
498
499/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000500
501static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000502converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000503{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000504 assert(expected != NULL);
505 assert(arg != NULL);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000506 PyOS_snprintf(msgbuf, bufsize,
507 "must be %.50s, not %.50s", expected,
508 arg == Py_None ? "None" : arg->ob_type->tp_name);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000509 return msgbuf;
510}
511
512#define CONV_UNICODE "(unicode conversion error)"
513
Guido van Rossum45aecf42006-03-15 04:58:47 +0000514/* Explicitly check for float arguments when integers are expected.
515 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000516static int
517float_argument_error(PyObject *arg)
518{
Guido van Rossum45aecf42006-03-15 04:58:47 +0000519 if (PyFloat_Check(arg)) {
520 PyErr_SetString(PyExc_TypeError,
521 "integer argument expected, got float" );
Neil Schemenauer5042da62003-02-04 20:59:40 +0000522 return 1;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000523 }
Neil Schemenauer5042da62003-02-04 20:59:40 +0000524 else
525 return 0;
526}
527
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000528/* Convert a non-tuple argument. Return NULL if conversion went OK,
529 or a string with a message describing the failure. The message is
530 formatted as "must be <desired type>, not <actual type>".
531 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000532 Don't call if a tuple is expected.
533
534 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000535*/
536
537static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000538convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000539 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000540{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000541 /* For # codes */
542#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
543 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
544 else q=va_arg(*p_va, int*);
545#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
546#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
547
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000548 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000549 char c = *format++;
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000550 PyObject *uarg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000551
552 switch (c) {
553
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000554 case 'b': { /* unsigned byte -- very short int */
555 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000556 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000557 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000558 return converterr("integer<b>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000559 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000560 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000561 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000562 else if (ival < 0) {
563 PyErr_SetString(PyExc_OverflowError,
564 "unsigned byte integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000565 return converterr("integer<b>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000566 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000567 else if (ival > UCHAR_MAX) {
568 PyErr_SetString(PyExc_OverflowError,
569 "unsigned byte integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000570 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000571 }
572 else
573 *p = (unsigned char) ival;
574 break;
575 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000576
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000577 case 'B': {/* byte sized bitfield - both signed and unsigned
578 values allowed */
579 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000580 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000581 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000582 return converterr("integer<B>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000583 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000584 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000585 return converterr("integer<B>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000586 else
587 *p = (unsigned char) ival;
588 break;
589 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000590
Guido van Rossumfce26e72003-04-18 00:12:30 +0000591 case 'h': {/* signed short int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000592 short *p = va_arg(*p_va, short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000593 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000594 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000595 return converterr("integer<h>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000596 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000597 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000598 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfce26e72003-04-18 00:12:30 +0000599 else if (ival < SHRT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000600 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000601 "signed short integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000602 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000603 }
Guido van Rossumfce26e72003-04-18 00:12:30 +0000604 else if (ival > SHRT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000605 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000606 "signed short integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000607 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000608 }
609 else
610 *p = (short) ival;
611 break;
612 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000613
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000614 case 'H': { /* short int sized bitfield, both signed and
615 unsigned allowed */
616 unsigned short *p = va_arg(*p_va, unsigned short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000617 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000618 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000619 return converterr("integer<H>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000620 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000621 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000622 return converterr("integer<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000623 else
624 *p = (unsigned short) ival;
625 break;
626 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000627
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000628 case 'i': {/* signed int */
629 int *p = va_arg(*p_va, int *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000630 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000631 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000632 return converterr("integer<i>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000633 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000634 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000635 return converterr("integer<i>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000636 else if (ival > INT_MAX) {
637 PyErr_SetString(PyExc_OverflowError,
638 "signed integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000639 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000640 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000641 else if (ival < INT_MIN) {
642 PyErr_SetString(PyExc_OverflowError,
643 "signed integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000644 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000645 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000646 else
647 *p = ival;
648 break;
649 }
650
Thomas Hellera4ea6032003-04-17 18:55:45 +0000651 case 'I': { /* int sized bitfield, both signed and
652 unsigned allowed */
653 unsigned int *p = va_arg(*p_va, unsigned int *);
654 unsigned int ival;
655 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000656 return converterr("integer<I>", arg, msgbuf, bufsize);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000657 ival = (unsigned int)PyInt_AsUnsignedLongMask(arg);
658 if (ival == (unsigned int)-1 && PyErr_Occurred())
Thomas Hellera4ea6032003-04-17 18:55:45 +0000659 return converterr("integer<I>", arg, msgbuf, bufsize);
660 else
661 *p = ival;
662 break;
663 }
664
Martin v. Löwis18e16552006-02-15 17:27:45 +0000665 case 'n': /* Py_ssize_t */
666#if SIZEOF_SIZE_T != SIZEOF_LONG
667 {
668 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
669 Py_ssize_t ival;
670 if (float_argument_error(arg))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000671 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000672 ival = PyInt_AsSsize_t(arg);
673 if (ival == -1 && PyErr_Occurred())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000674 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000675 *p = ival;
676 break;
677 }
678#endif
679 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000680 case 'l': {/* long int */
681 long *p = va_arg(*p_va, long *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000682 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000683 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000684 return converterr("integer<l>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000685 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000686 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000687 return converterr("integer<l>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000688 else
689 *p = ival;
690 break;
691 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000692
693 case 'k': { /* long sized bitfield */
694 unsigned long *p = va_arg(*p_va, unsigned long *);
695 unsigned long ival;
696 if (PyInt_Check(arg))
697 ival = PyInt_AsUnsignedLongMask(arg);
698 else if (PyLong_Check(arg))
699 ival = PyLong_AsUnsignedLongMask(arg);
700 else
701 return converterr("integer<k>", arg, msgbuf, bufsize);
702 *p = ival;
703 break;
704 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000705
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000706#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000707 case 'L': {/* PY_LONG_LONG */
708 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
709 PY_LONG_LONG ival = PyLong_AsLongLong( arg );
710 if( ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000711 return converterr("long<L>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000712 } else {
713 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000714 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000715 break;
716 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000717
718 case 'K': { /* long long sized bitfield */
719 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
720 unsigned PY_LONG_LONG ival;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000721 if (PyLong_Check(arg))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000722 ival = PyLong_AsUnsignedLongLongMask(arg);
723 else
724 return converterr("integer<K>", arg, msgbuf, bufsize);
725 *p = ival;
726 break;
727 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000728#endif
729
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000730 case 'f': {/* float */
731 float *p = va_arg(*p_va, float *);
732 double dval = PyFloat_AsDouble(arg);
733 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000734 return converterr("float<f>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000735 else
736 *p = (float) dval;
737 break;
738 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000739
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000740 case 'd': {/* double */
741 double *p = va_arg(*p_va, double *);
742 double dval = PyFloat_AsDouble(arg);
743 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000744 return converterr("float<d>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000745 else
746 *p = dval;
747 break;
748 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000749
Guido van Rossum530956d1996-07-21 02:27:43 +0000750#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000751 case 'D': {/* complex double */
752 Py_complex *p = va_arg(*p_va, Py_complex *);
753 Py_complex cval;
754 cval = PyComplex_AsCComplex(arg);
755 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000756 return converterr("complex<D>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000757 else
758 *p = cval;
759 break;
760 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000761#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000762
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000763 case 'c': {/* char */
Walter Dörwaldd0941302007-07-01 21:58:22 +0000764 char *p = va_arg(*p_va, char *);
765 if (PyString_Check(arg) && PyString_Size(arg) == 1)
766 *p = PyString_AS_STRING(arg)[0];
767 else if (PyUnicode_Check(arg) &&
768 PyUnicode_GET_SIZE(arg) == 1 &&
769 PyUnicode_AS_UNICODE(arg)[0] < 256)
770 *p = PyUnicode_AS_UNICODE(arg)[0];
771 else
772 return converterr("char < 256", arg, msgbuf, bufsize);
773 break;
774 }
775
776 case 'C': {/* unicode char */
Walter Dörwaldbc1f8862007-06-20 11:02:38 +0000777 int *p = va_arg(*p_va, int *);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000778 if (PyString_Check(arg) && PyString_Size(arg) == 1)
Jeremy Hylton0407aea2001-10-10 02:51:57 +0000779 *p = PyString_AS_STRING(arg)[0];
Guido van Rossum09dc34f2007-05-04 04:17:33 +0000780 else if (PyUnicode_Check(arg) &&
Walter Dörwaldbc1f8862007-06-20 11:02:38 +0000781 PyUnicode_GET_SIZE(arg) == 1)
Guido van Rossum09dc34f2007-05-04 04:17:33 +0000782 *p = PyUnicode_AS_UNICODE(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000783 else
Walter Dörwaldbc1f8862007-06-20 11:02:38 +0000784 return converterr("char", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000785 break;
786 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000787
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000788 case 's': {/* string */
789 if (*format == '#') {
790 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000791 FETCH_SIZE;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000792
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000793 if (PyString_Check(arg)) {
794 *p = PyString_AS_STRING(arg);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000795 STORE_SIZE(PyString_GET_SIZE(arg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000796 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000797 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000798 uarg = UNICODE_DEFAULT_ENCODING(arg);
799 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000800 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000801 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000802 *p = PyString_AS_STRING(uarg);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000803 STORE_SIZE(PyString_GET_SIZE(uarg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000804 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000805 else { /* any buffer-like object */
806 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000807 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000808 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000809 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000810 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000811 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000812 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000813 } else {
814 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000815
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000816 if (PyString_Check(arg))
817 *p = PyString_AS_STRING(arg);
818 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000819 uarg = UNICODE_DEFAULT_ENCODING(arg);
820 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000821 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000822 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000823 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000824 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000825 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000826 return converterr("string", arg, msgbuf, bufsize);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000827 if ((Py_ssize_t)strlen(*p) != PyString_Size(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000828 return converterr("string without null bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000829 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000830 }
831 break;
832 }
833
Walter Dörwald612344f2007-05-04 19:28:21 +0000834 case 'y': {/* bytes */
835 if (*format == '#') {
836 void **p = (void **)va_arg(*p_va, char **);
837 FETCH_SIZE;
838
839 if (PyBytes_Check(arg)) {
840 *p = PyBytes_AS_STRING(arg);
841 STORE_SIZE(PyBytes_GET_SIZE(arg));
842 }
843 else
844 return converterr("bytes", arg, msgbuf, bufsize);
845 format++;
846 } else {
847 char **p = va_arg(*p_va, char **);
848
849 if (PyBytes_Check(arg))
850 *p = PyBytes_AS_STRING(arg);
851 else
852 return converterr("bytes", arg, msgbuf, bufsize);
853 if ((Py_ssize_t)strlen(*p) != PyBytes_Size(arg))
854 return converterr("bytes without null bytes",
855 arg, msgbuf, bufsize);
856 }
857 break;
858 }
859
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000860 case 'z': {/* string, may be NULL (None) */
861 if (*format == '#') { /* any buffer-like object */
862 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000863 FETCH_SIZE;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000864
865 if (arg == Py_None) {
866 *p = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000867 STORE_SIZE(0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000868 }
869 else if (PyString_Check(arg)) {
870 *p = PyString_AS_STRING(arg);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000871 STORE_SIZE(PyString_GET_SIZE(arg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000872 }
873 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000874 uarg = UNICODE_DEFAULT_ENCODING(arg);
875 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000876 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000877 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000878 *p = PyString_AS_STRING(uarg);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000879 STORE_SIZE(PyString_GET_SIZE(uarg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000880 }
881 else { /* any buffer-like object */
882 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000883 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000884 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000885 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000886 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000887 }
888 format++;
889 } else {
890 char **p = va_arg(*p_va, char **);
891
892 if (arg == Py_None)
893 *p = 0;
894 else if (PyString_Check(arg))
Jeremy Hyltona4c8cd72001-10-10 02:51:08 +0000895 *p = PyString_AS_STRING(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000896 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000897 uarg = UNICODE_DEFAULT_ENCODING(arg);
898 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000899 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000900 arg, msgbuf, bufsize);
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000901 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000902 }
903 else
904 return converterr("string or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000905 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000906 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000907 FETCH_SIZE;
Thomas Woutersc3547a32006-03-01 21:31:21 +0000908 assert(0); /* XXX redundant with if-case */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000909 if (arg == Py_None)
910 *q = 0;
911 else
912 *q = PyString_Size(arg);
913 format++;
914 }
915 else if (*p != NULL &&
Thomas Wouters89f507f2006-12-13 04:49:30 +0000916 (Py_ssize_t)strlen(*p) != PyString_Size(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000917 return converterr(
918 "string without null bytes or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000919 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000920 }
921 break;
922 }
923
Guido van Rossumfb67be22007-08-29 18:38:11 +0000924 case 'Z': {/* unicode, may be NULL (None) */
925 if (*format == '#') { /* any buffer-like object */
926 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
927 FETCH_SIZE;
928
929 if (arg == Py_None) {
930 *p = 0;
931 STORE_SIZE(0);
932 }
933 else if (PyUnicode_Check(arg)) {
934 *p = PyUnicode_AS_UNICODE(arg);
935 STORE_SIZE(PyUnicode_GET_SIZE(arg));
936 }
937 format++;
938 } else {
939 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
940
941 if (arg == Py_None)
942 *p = 0;
943 else if (PyUnicode_Check(arg))
944 *p = PyUnicode_AS_UNICODE(arg);
945 else
946 return converterr("string or None",
947 arg, msgbuf, bufsize);
948 }
949 break;
950 }
951
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000952 case 'e': {/* encoded string */
953 char **buffer;
954 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000955 PyObject *s;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000956 int recode_strings;
957 Py_ssize_t size;
Guido van Rossumd70539a2007-05-09 23:35:09 +0000958 const char *ptr;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000959
960 /* Get 'e' parameter: the encoding name */
961 encoding = (const char *)va_arg(*p_va, const char *);
962 if (encoding == NULL)
963 encoding = PyUnicode_GetDefaultEncoding();
964
965 /* Get output buffer parameter:
966 's' (recode all objects via Unicode) or
967 't' (only recode non-string objects)
968 */
969 if (*format == 's')
970 recode_strings = 1;
971 else if (*format == 't')
972 recode_strings = 0;
973 else
974 return converterr(
975 "(unknown parser marker combination)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000976 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000977 buffer = (char **)va_arg(*p_va, char **);
978 format++;
979 if (buffer == NULL)
980 return converterr("(buffer is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000981 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000982
983 /* Encode object */
Guido van Rossumd70539a2007-05-09 23:35:09 +0000984 if (!recode_strings &&
985 (PyString_Check(arg) || PyBytes_Check(arg))) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000986 s = arg;
987 Py_INCREF(s);
Guido van Rossumd70539a2007-05-09 23:35:09 +0000988 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
989 return converterr("(AsCharBuffer failed)",
990 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000991 }
992 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000993 PyObject *u;
994
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000995 /* Convert object to Unicode */
996 u = PyUnicode_FromObject(arg);
997 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000998 return converterr(
999 "string or unicode or text buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001000 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001001
1002 /* Encode object; use default error handling */
1003 s = PyUnicode_AsEncodedString(u,
1004 encoding,
1005 NULL);
1006 Py_DECREF(u);
1007 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001008 return converterr("(encoding failed)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001009 arg, msgbuf, bufsize);
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001010 if (!PyBytes_Check(s)) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001011 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001012 return converterr(
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001013 "(encoder failed to return bytes)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001014 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001015 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001016 size = PyBytes_GET_SIZE(s);
1017 ptr = PyBytes_AS_STRING(s);
Guido van Rossumd70539a2007-05-09 23:35:09 +00001018 if (ptr == NULL)
1019 ptr = "";
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001020 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001021
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001022 /* Write output; output is guaranteed to be 0-terminated */
1023 if (*format == '#') {
1024 /* Using buffer length parameter '#':
1025
1026 - if *buffer is NULL, a new buffer of the
1027 needed size is allocated and the data
1028 copied into it; *buffer is updated to point
1029 to the new buffer; the caller is
1030 responsible for PyMem_Free()ing it after
1031 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001032
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001033 - if *buffer is not NULL, the data is
1034 copied to *buffer; *buffer_len has to be
1035 set to the size of the buffer on input;
1036 buffer overflow is signalled with an error;
1037 buffer has to provide enough room for the
1038 encoded string plus the trailing 0-byte
1039
1040 - in both cases, *buffer_len is updated to
1041 the size of the buffer /excluding/ the
1042 trailing 0-byte
1043
1044 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001045 FETCH_SIZE;
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001046
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001047 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001048 if (q == NULL && q2 == NULL) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001049 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001050 return converterr(
1051 "(buffer_len is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001052 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001053 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001054 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001055 *buffer = PyMem_NEW(char, size + 1);
1056 if (*buffer == NULL) {
1057 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001058 return converterr(
1059 "(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001060 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001061 }
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001062 if(addcleanup(*buffer, freelist)) {
1063 Py_DECREF(s);
1064 return converterr(
1065 "(cleanup problem)",
1066 arg, msgbuf, bufsize);
1067 }
Fred Drake25871c02000-05-03 15:17:02 +00001068 } else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001069 if (size + 1 > BUFFER_LEN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001070 Py_DECREF(s);
1071 return converterr(
1072 "(buffer overflow)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001073 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001074 }
Fred Drake25871c02000-05-03 15:17:02 +00001075 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001076 memcpy(*buffer, ptr, size+1);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001077 STORE_SIZE(size);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001078 } else {
1079 /* Using a 0-terminated buffer:
1080
1081 - the encoded string has to be 0-terminated
1082 for this variant to work; if it is not, an
1083 error raised
Fred Drake25871c02000-05-03 15:17:02 +00001084
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001085 - a new buffer of the needed size is
1086 allocated and the data copied into it;
1087 *buffer is updated to point to the new
1088 buffer; the caller is responsible for
1089 PyMem_Free()ing it after usage
1090
1091 */
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001092 if ((Py_ssize_t)strlen(ptr) != size) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001093 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001094 return converterr(
1095 "(encoded string without NULL bytes)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001096 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001097 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001098 *buffer = PyMem_NEW(char, size + 1);
1099 if (*buffer == NULL) {
1100 Py_DECREF(s);
1101 return converterr("(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001102 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001103 }
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001104 if(addcleanup(*buffer, freelist)) {
1105 Py_DECREF(s);
1106 return converterr("(cleanup problem)",
1107 arg, msgbuf, bufsize);
1108 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001109 memcpy(*buffer, ptr, size+1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001110 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001111 Py_DECREF(s);
1112 break;
1113 }
1114
1115 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1116 if (*format == '#') { /* any buffer-like object */
1117 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001118 FETCH_SIZE;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001119 if (PyUnicode_Check(arg)) {
1120 *p = PyUnicode_AS_UNICODE(arg);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001121 STORE_SIZE(PyUnicode_GET_SIZE(arg));
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001122 }
1123 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001124 return converterr("cannot convert raw buffers",
1125 arg, msgbuf, bufsize);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001126 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001127 format++;
1128 } else {
1129 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Guido van Rossume826ef02000-03-10 23:02:17 +00001130 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001131 *p = PyUnicode_AS_UNICODE(arg);
1132 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001133 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001134 }
1135 break;
1136 }
1137
1138 case 'S': { /* string object */
1139 PyObject **p = va_arg(*p_va, PyObject **);
Guido van Rossum572dbf82007-04-27 23:53:51 +00001140 if (PyString_Check(arg) || PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001141 *p = arg;
1142 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001143 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001144 break;
1145 }
Guido van Rossum617dbc42007-05-07 23:57:08 +00001146
1147 case 'Y': { /* bytes object */
1148 PyObject **p = va_arg(*p_va, PyObject **);
1149 if (PyBytes_Check(arg))
1150 *p = arg;
1151 else
1152 return converterr("bytes", arg, msgbuf, bufsize);
1153 break;
1154 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001155
1156 case 'U': { /* Unicode object */
1157 PyObject **p = va_arg(*p_va, PyObject **);
1158 if (PyUnicode_Check(arg))
1159 *p = arg;
1160 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001161 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001162 break;
1163 }
1164
1165 case 'O': { /* object */
1166 PyTypeObject *type;
1167 PyObject **p;
1168 if (*format == '!') {
1169 type = va_arg(*p_va, PyTypeObject*);
1170 p = va_arg(*p_va, PyObject **);
1171 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +00001172 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +00001173 *p = arg;
1174 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001175 return converterr(type->tp_name, arg, msgbuf, bufsize);
Guido van Rossumfccfe891998-05-15 22:04:07 +00001176
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001177 }
1178 else if (*format == '?') {
1179 inquiry pred = va_arg(*p_va, inquiry);
1180 p = va_arg(*p_va, PyObject **);
1181 format++;
1182 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001183 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001184 else
1185 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001186 arg, msgbuf, bufsize);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001187
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001188 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001189 else if (*format == '&') {
1190 typedef int (*converter)(PyObject *, void *);
1191 converter convert = va_arg(*p_va, converter);
1192 void *addr = va_arg(*p_va, void *);
1193 format++;
1194 if (! (*convert)(arg, addr))
1195 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001196 arg, msgbuf, bufsize);
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001197 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001198 else {
1199 p = va_arg(*p_va, PyObject **);
1200 *p = arg;
1201 }
1202 break;
1203 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001204
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001205
1206 case 'w': { /* memory buffer, read-write access */
1207 void **p = va_arg(*p_va, void **);
1208 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1209 int count;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001210 int temp=-1;
1211 PyBuffer view;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001212
1213 if (pb == NULL ||
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001214 pb->bf_getbuffer == NULL ||
1215 ((temp = (*pb->bf_getbuffer)(arg, &view,
1216 PyBUF_SIMPLE)) != 0) ||
1217 view.readonly == 1) {
1218 if (temp==0 && pb->bf_releasebuffer != NULL) {
1219 (*pb->bf_releasebuffer)(arg, &view);
1220 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001221 return converterr("single-segment read-write buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001222 arg, msgbuf, bufsize);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001223 }
1224
1225 if ((count = view.len) < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001226 return converterr("(unspecified)", arg, msgbuf, bufsize);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001227 *p = view.buf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001228 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001229 FETCH_SIZE;
1230 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001231 format++;
1232 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001233 if (pb->bf_releasebuffer != NULL)
1234 (*pb->bf_releasebuffer)(arg, &view);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001235 break;
1236 }
1237
1238 case 't': { /* 8-bit character buffer, read-only access */
Martin v. Löwis15e62742006-02-27 16:46:16 +00001239 char **p = va_arg(*p_va, char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001240 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001241 int count;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001242 PyBuffer view;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001243
1244 if (*format++ != '#')
1245 return converterr(
1246 "invalid use of 't' format character",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001247 arg, msgbuf, bufsize);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001248 if (pb == NULL || pb->bf_getbuffer == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001249 return converterr(
1250 "string or read-only character buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001251 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001252
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001253 if ((*pb->bf_getbuffer)(arg, &view, PyBUF_CHARACTER) != 0)
1254 return converterr("string or single-segment read-only buffer",
1255 arg, msgbuf, bufsize);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001256
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001257 count = view.len;
1258 *p = view.buf;
1259 /* XXX : shouldn't really release buffer, but it should be O.K.
1260 */
1261 if (pb->bf_releasebuffer != NULL)
1262 (*pb->bf_releasebuffer)(arg, &view);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001263 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001264 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001265 {
1266 FETCH_SIZE;
1267 STORE_SIZE(count);
1268 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001269 break;
1270 }
1271
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001272 default:
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001273 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001274
1275 }
1276
1277 *p_format = format;
1278 return NULL;
1279}
Guido van Rossumaa354651996-08-19 19:32:04 +00001280
Martin v. Löwis18e16552006-02-15 17:27:45 +00001281static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001282convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001283{
1284 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001285 Py_ssize_t count;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001286 PyBuffer view;
1287
1288 *errmsg = NULL;
1289 *p = NULL;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001290 if (pb == NULL ||
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001291 pb->bf_getbuffer == NULL) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001292 *errmsg = "string or read-only buffer";
1293 return -1;
1294 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001295
1296 if ((*pb->bf_getbuffer)(arg, &view, PyBUF_SIMPLE) != 0) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001297 *errmsg = "string or single-segment read-only buffer";
1298 return -1;
1299 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001300 count = view.len;
1301 *p = view.buf;
1302 if (pb->bf_releasebuffer != NULL)
1303 (*pb->bf_releasebuffer)(arg, &view);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001304 return count;
1305}
Guido van Rossumaa354651996-08-19 19:32:04 +00001306
1307/* Support for keyword arguments donated by
1308 Geoff Philbrick <philbric@delphi.hks.com> */
1309
Tim Petersf8cd3e82001-10-27 04:26:57 +00001310/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001311int
1312PyArg_ParseTupleAndKeywords(PyObject *args,
1313 PyObject *keywords,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001314 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001315 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001316{
1317 int retval;
1318 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001319
1320 if ((args == NULL || !PyTuple_Check(args)) ||
1321 (keywords != NULL && !PyDict_Check(keywords)) ||
1322 format == NULL ||
1323 kwlist == NULL)
1324 {
1325 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001326 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001327 }
1328
Guido van Rossumaa354651996-08-19 19:32:04 +00001329 va_start(va, kwlist);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001330 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1331 va_end(va);
1332 return retval;
1333}
1334
1335int
1336_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1337 PyObject *keywords,
1338 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001339 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001340{
1341 int retval;
1342 va_list va;
1343
1344 if ((args == NULL || !PyTuple_Check(args)) ||
1345 (keywords != NULL && !PyDict_Check(keywords)) ||
1346 format == NULL ||
1347 kwlist == NULL)
1348 {
1349 PyErr_BadInternalCall();
1350 return 0;
1351 }
1352
1353 va_start(va, kwlist);
1354 retval = vgetargskeywords(args, keywords, format,
1355 kwlist, &va, FLAG_SIZE_T);
Guido van Rossumaa354651996-08-19 19:32:04 +00001356 va_end(va);
1357 return retval;
1358}
1359
1360
Brett Cannon711e7d92004-07-10 22:20:32 +00001361int
1362PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001363 PyObject *keywords,
1364 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001365 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001366{
1367 int retval;
1368 va_list lva;
1369
1370 if ((args == NULL || !PyTuple_Check(args)) ||
1371 (keywords != NULL && !PyDict_Check(keywords)) ||
1372 format == NULL ||
1373 kwlist == NULL)
1374 {
1375 PyErr_BadInternalCall();
1376 return 0;
1377 }
1378
1379#ifdef VA_LIST_IS_ARRAY
1380 memcpy(lva, va, sizeof(va_list));
1381#else
1382#ifdef __va_copy
1383 __va_copy(lva, va);
1384#else
1385 lva = va;
1386#endif
1387#endif
1388
Martin v. Löwis18e16552006-02-15 17:27:45 +00001389 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1390 return retval;
1391}
1392
1393int
1394_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1395 PyObject *keywords,
1396 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001397 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001398{
1399 int retval;
1400 va_list lva;
1401
1402 if ((args == NULL || !PyTuple_Check(args)) ||
1403 (keywords != NULL && !PyDict_Check(keywords)) ||
1404 format == NULL ||
1405 kwlist == NULL)
1406 {
1407 PyErr_BadInternalCall();
1408 return 0;
1409 }
1410
1411#ifdef VA_LIST_IS_ARRAY
1412 memcpy(lva, va, sizeof(va_list));
1413#else
1414#ifdef __va_copy
1415 __va_copy(lva, va);
1416#else
1417 lva = va;
1418#endif
1419#endif
1420
1421 retval = vgetargskeywords(args, keywords, format,
1422 kwlist, &lva, FLAG_SIZE_T);
Brett Cannon711e7d92004-07-10 22:20:32 +00001423 return retval;
1424}
1425
1426
Guido van Rossumaa354651996-08-19 19:32:04 +00001427static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001428vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001429 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001430{
Tim Petersdc5eff92001-10-27 06:53:00 +00001431 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001432 int levels[32];
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001433 const char *fname, *message;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001434 int min, max;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001435 const char *formatsave;
Tim Petersb639d492001-10-27 07:00:56 +00001436 int i, len, nargs, nkeywords;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001437 const char *msg;
1438 char **p;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001439 PyObject *freelist = NULL;
Tim Petersf4331c12001-10-27 00:17:34 +00001440
Tim Peters45772cd2001-10-27 03:58:40 +00001441 assert(args != NULL && PyTuple_Check(args));
1442 assert(keywords == NULL || PyDict_Check(keywords));
1443 assert(format != NULL);
1444 assert(kwlist != NULL);
1445 assert(p_va != NULL);
1446
Tim Petersf8cd3e82001-10-27 04:26:57 +00001447 /* Search the format:
1448 message <- error msg, if any (else NULL).
Tim Peterscffed4b2001-11-29 03:26:37 +00001449 fname <- routine name, if any (else NULL).
Tim Petersf8cd3e82001-10-27 04:26:57 +00001450 min <- # of required arguments, or -1 if all are required.
1451 max <- most arguments (required + optional).
Tim Peters62d48e12001-10-27 06:42:16 +00001452 Check that kwlist has a non-NULL entry for each arg.
Tim Petersf8cd3e82001-10-27 04:26:57 +00001453 Raise error if a tuple arg spec is found.
1454 */
1455 fname = message = NULL;
Tim Peters6fb26352001-10-27 04:38:11 +00001456 formatsave = format;
Tim Peters62d48e12001-10-27 06:42:16 +00001457 p = kwlist;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001458 min = -1;
1459 max = 0;
1460 while ((i = *format++) != '\0') {
Neal Norwitz30b5c5d2005-12-19 06:05:18 +00001461 if (isalpha(Py_CHARMASK(i)) && i != 'e') {
Tim Petersf8cd3e82001-10-27 04:26:57 +00001462 max++;
Tim Peters62d48e12001-10-27 06:42:16 +00001463 if (*p == NULL) {
Tim Peterscffed4b2001-11-29 03:26:37 +00001464 PyErr_SetString(PyExc_RuntimeError,
1465 "more argument specifiers than "
1466 "keyword list entries");
Tim Peters62d48e12001-10-27 06:42:16 +00001467 return 0;
1468 }
1469 p++;
1470 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001471 else if (i == '|')
1472 min = max;
1473 else if (i == ':') {
1474 fname = format;
1475 break;
1476 }
1477 else if (i == ';') {
1478 message = format;
1479 break;
1480 }
1481 else if (i == '(') {
Tim Peterscffed4b2001-11-29 03:26:37 +00001482 PyErr_SetString(PyExc_RuntimeError,
1483 "tuple found in format when using keyword "
1484 "arguments");
Guido van Rossumaa354651996-08-19 19:32:04 +00001485 return 0;
1486 }
Tim Peters62d48e12001-10-27 06:42:16 +00001487 }
1488 format = formatsave;
1489 if (*p != NULL) {
Tim Peterscffed4b2001-11-29 03:26:37 +00001490 PyErr_SetString(PyExc_RuntimeError,
1491 "more keyword list entries than "
1492 "argument specifiers");
Tim Peters62d48e12001-10-27 06:42:16 +00001493 return 0;
1494 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001495 if (min < 0) {
1496 /* All arguments are required. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001497 min = max;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001498 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001499
Tim Peters6fb26352001-10-27 04:38:11 +00001500 nargs = PyTuple_GET_SIZE(args);
Tim Petersb0872fc2001-10-27 04:45:34 +00001501 nkeywords = keywords == NULL ? 0 : PyDict_Size(keywords);
Tim Petersf8cd3e82001-10-27 04:26:57 +00001502
Guido van Rossumaa354651996-08-19 19:32:04 +00001503 /* make sure there are no duplicate values for an argument;
1504 its not clear when to use the term "keyword argument vs.
1505 keyword parameter in messages */
Tim Petersb054be42001-10-27 05:07:41 +00001506 if (nkeywords > 0) {
Tim Peters6fb26352001-10-27 04:38:11 +00001507 for (i = 0; i < nargs; i++) {
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001508 const char *thiskw = kwlist[i];
Tim Petersa9f47392001-10-27 00:46:09 +00001509 if (thiskw == NULL)
1510 break;
Tim Peters077f5742001-10-27 05:50:39 +00001511 if (PyDict_GetItemString(keywords, thiskw)) {
Tim Petersb054be42001-10-27 05:07:41 +00001512 PyErr_Format(PyExc_TypeError,
1513 "keyword parameter '%s' was given "
1514 "by position and by name",
Tim Petersa9f47392001-10-27 00:46:09 +00001515 thiskw);
Guido van Rossumaa354651996-08-19 19:32:04 +00001516 return 0;
1517 }
Tim Peters0af49162001-10-27 06:14:32 +00001518 else if (PyErr_Occurred())
1519 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001520 }
1521 }
Tim Peters61dde632001-10-27 05:30:17 +00001522
Guido van Rossumaa354651996-08-19 19:32:04 +00001523 /* required arguments missing from args can be supplied by keyword
Georg Brandld7048172006-01-20 17:53:27 +00001524 arguments; set len to the number of positional arguments, and,
Tim Peters62d48e12001-10-27 06:42:16 +00001525 if that's less than the minimum required, add in the number of
1526 required arguments that are supplied by keywords */
Tim Peters6fb26352001-10-27 04:38:11 +00001527 len = nargs;
Tim Peters62d48e12001-10-27 06:42:16 +00001528 if (nkeywords > 0 && nargs < min) {
Tim Peters6fb26352001-10-27 04:38:11 +00001529 for (i = nargs; i < min; i++) {
Tim Peters077f5742001-10-27 05:50:39 +00001530 if (PyDict_GetItemString(keywords, kwlist[i]))
Guido van Rossumaa354651996-08-19 19:32:04 +00001531 len++;
Tim Peters0af49162001-10-27 06:14:32 +00001532 else if (PyErr_Occurred())
1533 return 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001534 }
1535 }
Tim Peters0af49162001-10-27 06:14:32 +00001536
Guido van Rossumaa354651996-08-19 19:32:04 +00001537 /* make sure we got an acceptable number of arguments; the message
1538 is a little confusing with keywords since keyword arguments
1539 which are supplied, but don't match the required arguments
Michael W. Hudson5253c302005-03-30 16:41:55 +00001540 are not included in the "%d given" part of the message
1541 XXX and this isn't a bug!? */
Guido van Rossumaa354651996-08-19 19:32:04 +00001542 if (len < min || max < len) {
1543 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +00001544 PyOS_snprintf(msgbuf, sizeof(msgbuf),
1545 "%.200s%s takes %s %d argument%s "
1546 "(%d given)",
1547 fname==NULL ? "function" : fname,
1548 fname==NULL ? "" : "()",
1549 min==max ? "exactly"
1550 : len < min ? "at least" : "at most",
1551 len < min ? min : max,
1552 (len < min ? min : max) == 1 ? "" : "s",
1553 len);
Guido van Rossumaa354651996-08-19 19:32:04 +00001554 message = msgbuf;
1555 }
1556 PyErr_SetString(PyExc_TypeError, message);
1557 return 0;
1558 }
Tim Petersc2f01122001-10-27 07:25:06 +00001559
1560 /* convert the positional arguments */
Tim Peters6fb26352001-10-27 04:38:11 +00001561 for (i = 0; i < nargs; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001562 if (*format == '|')
1563 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +00001564 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001565 flags, levels, msgbuf, sizeof(msgbuf),
1566 &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001567 if (msg) {
1568 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001569 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001570 }
1571 }
1572
Tim Petersc2f01122001-10-27 07:25:06 +00001573 /* handle no keyword parameters in call */
Tim Petersb054be42001-10-27 05:07:41 +00001574 if (nkeywords == 0)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001575 return cleanreturn(1, freelist);
Tim Petersb054be42001-10-27 05:07:41 +00001576
Guido van Rossumaa354651996-08-19 19:32:04 +00001577 /* convert the keyword arguments; this uses the format
1578 string where it was left after processing args */
Tim Petersb639d492001-10-27 07:00:56 +00001579 for (i = nargs; i < max; i++) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001580 PyObject *item;
Guido van Rossumaa354651996-08-19 19:32:04 +00001581 if (*format == '|')
1582 format++;
Tim Peters077f5742001-10-27 05:50:39 +00001583 item = PyDict_GetItemString(keywords, kwlist[i]);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001584 if (item != NULL) {
Tim Peters077f5742001-10-27 05:50:39 +00001585 Py_INCREF(item);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001586 msg = convertitem(item, &format, p_va, flags, levels,
1587 msgbuf, sizeof(msgbuf), &freelist);
Tim Peters077f5742001-10-27 05:50:39 +00001588 Py_DECREF(item);
Guido van Rossumaa354651996-08-19 19:32:04 +00001589 if (msg) {
1590 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001591 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001592 }
Tim Petersc2f01122001-10-27 07:25:06 +00001593 --nkeywords;
1594 if (nkeywords == 0)
1595 break;
Guido van Rossumaa354651996-08-19 19:32:04 +00001596 }
Tim Peters0af49162001-10-27 06:14:32 +00001597 else if (PyErr_Occurred())
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001598 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001599 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001600 msg = skipitem(&format, p_va, flags);
Guido van Rossumaa354651996-08-19 19:32:04 +00001601 if (msg) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001602 levels[0] = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001603 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001604 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001605 }
1606 }
1607 }
Tim Petersb054be42001-10-27 05:07:41 +00001608
Guido van Rossumaa354651996-08-19 19:32:04 +00001609 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001610 if (nkeywords > 0) {
1611 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001612 Py_ssize_t pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001613 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001614 int match = 0;
Guido van Rossum55474762002-04-04 16:22:30 +00001615 char *ks;
Guido van Rossum572dbf82007-04-27 23:53:51 +00001616 if (!PyString_Check(key) && !PyUnicode_Check(key)) {
Guido van Rossum55474762002-04-04 16:22:30 +00001617 PyErr_SetString(PyExc_TypeError,
1618 "keywords must be strings");
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001619 return cleanreturn(0, freelist);
Guido van Rossum55474762002-04-04 16:22:30 +00001620 }
1621 ks = PyString_AsString(key);
Tim Petersb639d492001-10-27 07:00:56 +00001622 for (i = 0; i < max; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001623 if (!strcmp(ks, kwlist[i])) {
1624 match = 1;
1625 break;
1626 }
1627 }
1628 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001629 PyErr_Format(PyExc_TypeError,
1630 "'%s' is an invalid keyword "
1631 "argument for this function",
1632 ks);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001633 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001634 }
1635 }
1636 }
Tim Petersc2f01122001-10-27 07:25:06 +00001637
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001638 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001639}
1640
1641
1642static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001643skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001644{
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001645 const char *format = *p_format;
Guido van Rossumaa354651996-08-19 19:32:04 +00001646 char c = *format++;
1647
1648 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001649
1650 /* simple codes
1651 * The individual types (second arg of va_arg) are irrelevant */
1652
Guido van Rossumaa354651996-08-19 19:32:04 +00001653 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001654 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001655 case 'h': /* short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001656 case 'H': /* short int as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001657 case 'i': /* int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001658 case 'I': /* int sized bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001659 case 'l': /* long int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001660 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001661#ifdef HAVE_LONG_LONG
Georg Brandl6dd14612005-09-14 19:29:53 +00001662 case 'L': /* PY_LONG_LONG */
1663 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001664#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001665 case 'f': /* float */
Guido van Rossumaa354651996-08-19 19:32:04 +00001666 case 'd': /* double */
Guido van Rossumaa354651996-08-19 19:32:04 +00001667#ifndef WITHOUT_COMPLEX
1668 case 'D': /* complex double */
Georg Brandl6dd14612005-09-14 19:29:53 +00001669#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001670 case 'c': /* char */
1671 {
Georg Brandl6dd14612005-09-14 19:29:53 +00001672 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001673 break;
1674 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001675
1676 case 'n': /* Py_ssize_t */
1677 {
1678 (void) va_arg(*p_va, Py_ssize_t *);
1679 break;
1680 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001681
Georg Brandl6dd14612005-09-14 19:29:53 +00001682 /* string codes */
1683
1684 case 'e': /* string with encoding */
1685 {
1686 (void) va_arg(*p_va, const char *);
1687 if (!(*format == 's' || *format == 't'))
1688 /* after 'e', only 's' and 't' is allowed */
1689 goto err;
1690 format++;
1691 /* explicit fallthrough to string cases */
1692 }
1693
Guido van Rossumaa354651996-08-19 19:32:04 +00001694 case 's': /* string */
Georg Brandl6dd14612005-09-14 19:29:53 +00001695 case 'z': /* string or None */
Walter Dörwald612344f2007-05-04 19:28:21 +00001696 case 'y': /* bytes */
Georg Brandl6dd14612005-09-14 19:29:53 +00001697 case 'u': /* unicode string */
Georg Brandl6dd14612005-09-14 19:29:53 +00001698 case 't': /* buffer, read-only */
1699 case 'w': /* buffer, read-write */
Guido van Rossumaa354651996-08-19 19:32:04 +00001700 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001701 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001702 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001703 if (flags & FLAG_SIZE_T)
1704 (void) va_arg(*p_va, Py_ssize_t *);
1705 else
1706 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001707 format++;
1708 }
1709 break;
1710 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001711
1712 /* object codes */
1713
Guido van Rossumaa354651996-08-19 19:32:04 +00001714 case 'S': /* string object */
Guido van Rossum617dbc42007-05-07 23:57:08 +00001715 case 'Y': /* string object */
Georg Brandl6dd14612005-09-14 19:29:53 +00001716 case 'U': /* unicode string object */
Guido van Rossumaa354651996-08-19 19:32:04 +00001717 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001718 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001719 break;
1720 }
1721
1722 case 'O': /* object */
1723 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001724 if (*format == '!') {
1725 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001726 (void) va_arg(*p_va, PyTypeObject*);
1727 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001728 }
1729#if 0
1730/* I don't know what this is for */
1731 else if (*format == '?') {
1732 inquiry pred = va_arg(*p_va, inquiry);
1733 format++;
1734 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001735 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001736 }
1737 }
1738#endif
1739 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001740 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001741 (void) va_arg(*p_va, converter);
1742 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001743 format++;
1744 }
1745 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001746 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001747 }
1748 break;
1749 }
1750
1751 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001752err:
Guido van Rossumaa354651996-08-19 19:32:04 +00001753 return "impossible<bad format char>";
1754
1755 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001756
1757 /* The "(...)" format code for tuples is not handled here because
1758 * it is not allowed with keyword args. */
Guido van Rossumaa354651996-08-19 19:32:04 +00001759
1760 *p_format = format;
1761 return NULL;
1762}
Fred Drakee4616e62001-10-23 21:09:29 +00001763
1764
1765int
Martin v. Löwis76246742006-03-01 04:06:10 +00001766PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001767{
Martin v. Löwis76246742006-03-01 04:06:10 +00001768 Py_ssize_t i, l;
Fred Drakee4616e62001-10-23 21:09:29 +00001769 PyObject **o;
1770 va_list vargs;
1771
1772#ifdef HAVE_STDARG_PROTOTYPES
1773 va_start(vargs, max);
1774#else
1775 va_start(vargs);
1776#endif
1777
1778 assert(min >= 0);
1779 assert(min <= max);
1780 if (!PyTuple_Check(args)) {
1781 PyErr_SetString(PyExc_SystemError,
1782 "PyArg_UnpackTuple() argument list is not a tuple");
1783 return 0;
1784 }
1785 l = PyTuple_GET_SIZE(args);
1786 if (l < min) {
1787 if (name != NULL)
1788 PyErr_Format(
1789 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001790 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001791 name, (min == max ? "" : "at least "), min, l);
1792 else
1793 PyErr_Format(
1794 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001795 "unpacked tuple should have %s%zd elements,"
1796 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001797 (min == max ? "" : "at least "), min, l);
1798 va_end(vargs);
1799 return 0;
1800 }
1801 if (l > max) {
1802 if (name != NULL)
1803 PyErr_Format(
1804 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001805 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001806 name, (min == max ? "" : "at most "), max, l);
1807 else
1808 PyErr_Format(
1809 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001810 "unpacked tuple should have %s%zd elements,"
1811 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001812 (min == max ? "" : "at most "), max, l);
1813 va_end(vargs);
1814 return 0;
1815 }
1816 for (i = 0; i < l; i++) {
1817 o = va_arg(vargs, PyObject **);
1818 *o = PyTuple_GET_ITEM(args, i);
1819 }
1820 va_end(vargs);
1821 return 1;
1822}
Georg Brandl02c42872005-08-26 06:42:30 +00001823
1824
1825/* For type constructors that don't take keyword args
1826 *
1827 * Sets a TypeError and returns 0 if the kwds dict is
Thomas Wouters89f507f2006-12-13 04:49:30 +00001828 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001829 */
1830int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001831_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001832{
1833 if (kw == NULL)
1834 return 1;
1835 if (!PyDict_CheckExact(kw)) {
1836 PyErr_BadInternalCall();
1837 return 0;
1838 }
1839 if (PyDict_Size(kw) == 0)
1840 return 1;
1841
1842 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1843 funcname);
1844 return 0;
1845}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001846#ifdef __cplusplus
1847};
1848#endif