blob: 287b5d9be8a637935e5dd49f9a9db574c070c3a2 [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
Anthony Baxter97300382006-04-12 04:38:54 +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
Martin v. Löwis5cb69362006-04-14 09:08:42 +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 **);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +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 *,
Martin v. Löwis15e62742006-02-27 16:46:16 +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{
56 int retval;
57 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000058
59 va_start(va, format);
Martin v. Löwis18e16552006-02-15 17:27:45 +000060 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
61 va_end(va);
62 return retval;
63}
64
65int
66_PyArg_Parse_SizeT(PyObject *args, char *format, ...)
67{
68 int retval;
69 va_list va;
70
71 va_start(va, format);
72 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000073 va_end(va);
74 return retval;
75}
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{
81 int retval;
82 va_list va;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000083
84 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000085 retval = vgetargs1(args, format, &va, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000086 va_end(va);
87 return retval;
88}
89
Martin v. Löwis18e16552006-02-15 17:27:45 +000090int
91_PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
92{
93 int retval;
94 va_list va;
95
96 va_start(va, format);
97 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
98 va_end(va);
99 return retval;
100}
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{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000106 va_list lva;
107
108#ifdef VA_LIST_IS_ARRAY
109 memcpy(lva, va, sizeof(va_list));
110#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000111#ifdef __va_copy
112 __va_copy(lva, va);
113#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000114 lva = va;
115#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000116#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000117
118 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000119}
120
Martin v. Löwis18e16552006-02-15 17:27:45 +0000121int
122_PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
123{
124 va_list lva;
125
126#ifdef VA_LIST_IS_ARRAY
127 memcpy(lva, va, sizeof(va_list));
128#else
129#ifdef __va_copy
130 __va_copy(lva, va);
131#else
132 lva = va;
133#endif
134#endif
135
136 return vgetargs1(args, format, &lva, FLAG_SIZE_T);
137}
138
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000139
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000140/* Handle cleanup of allocated memory in case of exception */
141
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000142static void
143cleanup_ptr(void *ptr)
144{
145 PyMem_FREE(ptr);
146}
147
148static void
149cleanup_buffer(void *ptr)
150{
151 PyBuffer_Release((Py_buffer *) ptr);
152}
153
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000154static int
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000155addcleanup(void *ptr, PyObject **freelist, void (*destr)(void *))
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000156{
157 PyObject *cobj;
158 if (!*freelist) {
159 *freelist = PyList_New(0);
160 if (!*freelist) {
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000161 destr(ptr);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000162 return -1;
163 }
164 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000165 cobj = PyCObject_FromVoidPtr(ptr, destr);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000166 if (!cobj) {
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000167 destr(ptr);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000168 return -1;
169 }
Neal Norwitzdf6ac3d2008-02-26 05:23:51 +0000170 if (PyList_Append(*freelist, cobj)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000171 Py_DECREF(cobj);
172 return -1;
173 }
174 Py_DECREF(cobj);
175 return 0;
176}
177
178static int
179cleanreturn(int retval, PyObject *freelist)
180{
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000181 if (freelist && retval != 0) {
182 /* We were successful, reset the destructors so that they
183 don't get called. */
184 Py_ssize_t len = PyList_GET_SIZE(freelist), i;
185 for (i = 0; i < len; i++)
186 ((PyCObject *) PyList_GET_ITEM(freelist, i))
187 ->destructor = NULL;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000188 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000189 Py_XDECREF(freelist);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000190 return retval;
191}
192
193
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000194static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000195vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000196{
197 char msgbuf[256];
198 int levels[32];
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000199 const char *fname = NULL;
200 const char *message = NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000201 int min = -1;
202 int max = 0;
203 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000204 int endfmt = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000205 const char *formatsave = format;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000206 Py_ssize_t i, len;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000207 char *msg;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000208 PyObject *freelist = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000209 int compat = flags & FLAG_COMPAT;
210
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000211 assert(compat || (args != (PyObject*)NULL));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000212 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000213
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000214 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000215 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000216 switch (c) {
217 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000218 if (level == 0)
219 max++;
220 level++;
Georg Brandl209307e2006-08-09 07:03:22 +0000221 if (level >= 30)
222 Py_FatalError("too many tuple nesting levels "
223 "in argument format string");
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000224 break;
225 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000226 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000227 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000228 else
229 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000230 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000231 case '\0':
232 endfmt = 1;
233 break;
234 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000235 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000236 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000237 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000238 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000239 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000240 endfmt = 1;
241 break;
242 default:
243 if (level == 0) {
244 if (c == 'O')
245 max++;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000246 else if (isalpha(Py_CHARMASK(c))) {
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000247 if (c != 'e') /* skip encoded */
248 max++;
249 } else if (c == '|')
250 min = max;
251 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000252 break;
253 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000254 }
255
256 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000257 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000258
259 if (min < 0)
260 min = max;
261
262 format = formatsave;
263
264 if (compat) {
265 if (max == 0) {
266 if (args == NULL)
267 return 1;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000268 PyOS_snprintf(msgbuf, sizeof(msgbuf),
269 "%.200s%s takes no arguments",
270 fname==NULL ? "function" : fname,
271 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000272 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000273 return 0;
274 }
275 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000276 if (args == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000277 PyOS_snprintf(msgbuf, sizeof(msgbuf),
278 "%.200s%s takes at least one argument",
279 fname==NULL ? "function" : fname,
280 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000282 return 0;
283 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000284 msg = convertitem(args, &format, p_va, flags, levels,
285 msgbuf, sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000286 if (msg == NULL)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000287 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000288 seterror(levels[0], msg, levels+1, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000289 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000290 }
291 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000292 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000293 "old style getargs format uses new features");
294 return 0;
295 }
296 }
297
Guido van Rossum79f25d91997-04-29 20:08:16 +0000298 if (!PyTuple_Check(args)) {
299 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000300 "new style getargs format but argument is not a tuple");
301 return 0;
302 }
303
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000304 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000305
306 if (len < min || max < len) {
307 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000308 PyOS_snprintf(msgbuf, sizeof(msgbuf),
309 "%.150s%s takes %s %d argument%s "
Neal Norwitz20dd93f2006-02-19 19:34:15 +0000310 "(%ld given)",
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000311 fname==NULL ? "function" : fname,
312 fname==NULL ? "" : "()",
313 min==max ? "exactly"
314 : len < min ? "at least" : "at most",
315 len < min ? min : max,
316 (len < min ? min : max) == 1 ? "" : "s",
Neal Norwitz9a276172006-02-20 18:57:39 +0000317 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000318 message = msgbuf;
319 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000320 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000321 return 0;
322 }
323
324 for (i = 0; i < len; i++) {
325 if (*format == '|')
326 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000327 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000328 flags, levels, msgbuf,
329 sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000330 if (msg) {
Sean Reifscheider9279e7d2009-08-01 23:54:55 +0000331 seterror(i+1, msg, levels, fname, msg);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000332 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000333 }
334 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000335
Neal Norwitz4ac13df2005-12-19 06:10:07 +0000336 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000337 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000338 *format != '|' && *format != ':' && *format != ';') {
339 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000340 "bad format string: %.200s", formatsave);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000341 return cleanreturn(0, freelist);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000342 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000343
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000344 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000345}
346
347
348
349static void
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000350seterror(int iarg, const char *msg, int *levels, const char *fname,
351 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000352{
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000353 char buf[512];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000354 int i;
355 char *p = buf;
356
Guido van Rossum79f25d91997-04-29 20:08:16 +0000357 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000358 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000359 else if (message == NULL) {
360 if (fname != NULL) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000361 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000362 p += strlen(p);
363 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000364 if (iarg != 0) {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000365 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000366 "argument %d", iarg);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000367 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000368 p += strlen(p);
Georg Brandl5f135782006-07-26 08:03:10 +0000369 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
370 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000371 ", item %d", levels[i]-1);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000372 p += strlen(p);
373 i++;
374 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000375 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000376 else {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000377 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
Ka-Ping Yee20579702001-01-15 22:14:16 +0000378 p += strlen(p);
379 }
Tim Petersfaad5ad2001-12-03 00:43:33 +0000380 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000381 message = buf;
382 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000384}
385
386
387/* Convert a tuple argument.
388 On entry, *p_format points to the character _after_ the opening '('.
389 On successful exit, *p_format points to the closing ')'.
390 If successful:
391 *p_format and *p_va are updated,
392 *levels and *msgbuf are untouched,
393 and NULL is returned.
394 If the argument is invalid:
395 *p_format is unchanged,
396 *p_va is undefined,
397 *levels is a 0-terminated list of item numbers,
398 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000399 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000400 <typename1> is the name of the expected type, and
401 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000402 and msgbuf is returned.
403*/
404
405static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000406converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
407 int *levels, char *msgbuf, size_t bufsize, int toplevel,
408 PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000409{
410 int level = 0;
411 int n = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000412 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000413 int i;
414
415 for (;;) {
416 int c = *format++;
417 if (c == '(') {
418 if (level == 0)
419 n++;
420 level++;
421 }
422 else if (c == ')') {
423 if (level == 0)
424 break;
425 level--;
426 }
427 else if (c == ':' || c == ';' || c == '\0')
428 break;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000429 else if (level == 0 && isalpha(Py_CHARMASK(c)))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000430 n++;
431 }
432
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000433 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000434 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000435 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000436 toplevel ? "expected %d arguments, not %.50s" :
437 "must be %d-item sequence, not %.50s",
438 n,
439 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000440 return msgbuf;
441 }
442
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000443 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000444 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000445 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000446 toplevel ? "expected %d arguments, not %d" :
447 "must be sequence of length %d, not %d",
448 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000449 return msgbuf;
450 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000451
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000452 format = *p_format;
453 for (i = 0; i < n; i++) {
454 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000455 PyObject *item;
456 item = PySequence_GetItem(arg, i);
Georg Brandl5f135782006-07-26 08:03:10 +0000457 if (item == NULL) {
458 PyErr_Clear();
459 levels[0] = i+1;
460 levels[1] = 0;
461 strncpy(msgbuf, "is not retrievable", bufsize);
462 return msgbuf;
463 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000464 msg = convertitem(item, &format, p_va, flags, levels+1,
465 msgbuf, bufsize, freelist);
Guido van Rossum66368cc1999-02-17 23:16:43 +0000466 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
467 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000468 if (msg != NULL) {
469 levels[0] = i+1;
470 return msg;
471 }
472 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000473
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000474 *p_format = format;
475 return NULL;
476}
477
478
479/* Convert a single item. */
480
481static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
483 int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000484{
485 char *msg;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000486 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000487
488 if (*format == '(' /* ')' */) {
489 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000490 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000491 bufsize, 0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000492 if (msg == NULL)
493 format++;
494 }
495 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000496 msg = convertsimple(arg, &format, p_va, flags,
497 msgbuf, bufsize, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000498 if (msg != NULL)
499 levels[0] = 0;
500 }
501 if (msg == NULL)
502 *p_format = format;
503 return msg;
504}
505
506
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000507
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000508#define UNICODE_DEFAULT_ENCODING(arg) \
509 _PyUnicode_AsDefaultEncodedString(arg, NULL)
510
511/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000512
513static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000514converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000515{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000516 assert(expected != NULL);
517 assert(arg != NULL);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000518 PyOS_snprintf(msgbuf, bufsize,
519 "must be %.50s, not %.50s", expected,
520 arg == Py_None ? "None" : arg->ob_type->tp_name);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000521 return msgbuf;
522}
523
524#define CONV_UNICODE "(unicode conversion error)"
525
Neil Schemenauer5042da62003-02-04 20:59:40 +0000526/* explicitly check for float arguments when integers are expected. For now
527 * signal a warning. Returns true if an exception was raised. */
528static int
Mark Dickinson1b34d252010-01-01 17:27:30 +0000529float_argument_warning(PyObject *arg)
Neil Schemenauer5042da62003-02-04 20:59:40 +0000530{
531 if (PyFloat_Check(arg) &&
532 PyErr_Warn(PyExc_DeprecationWarning,
533 "integer argument expected, got float" ))
534 return 1;
535 else
536 return 0;
537}
538
Mark Dickinson1b34d252010-01-01 17:27:30 +0000539/* explicitly check for float arguments when integers are expected. Raises
540 TypeError and returns true for float arguments. */
541static int
542float_argument_error(PyObject *arg)
543{
544 if (PyFloat_Check(arg)) {
545 PyErr_SetString(PyExc_TypeError,
546 "integer argument expected, got float");
547 return 1;
548 }
549 else
550 return 0;
551}
552
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000553/* Convert a non-tuple argument. Return NULL if conversion went OK,
554 or a string with a message describing the failure. The message is
555 formatted as "must be <desired type>, not <actual type>".
556 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000557 Don't call if a tuple is expected.
558
559 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000560*/
561
562static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000563convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000564 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000565{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000566 /* For # codes */
567#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
568 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
569 else q=va_arg(*p_va, int*);
570#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
571#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
572
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000573 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000574 char c = *format++;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000575#ifdef Py_USING_UNICODE
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000576 PyObject *uarg;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000577#endif
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000578
579 switch (c) {
580
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000581 case 'b': { /* unsigned byte -- very short int */
582 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000583 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000584 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000585 return converterr("integer<b>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000586 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000587 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000588 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000589 else if (ival < 0) {
590 PyErr_SetString(PyExc_OverflowError,
591 "unsigned byte integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000592 return converterr("integer<b>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000593 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000594 else if (ival > UCHAR_MAX) {
595 PyErr_SetString(PyExc_OverflowError,
596 "unsigned byte integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000597 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000598 }
599 else
600 *p = (unsigned char) ival;
601 break;
602 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000603
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000604 case 'B': {/* byte sized bitfield - both signed and unsigned
605 values allowed */
606 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000607 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000608 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000609 return converterr("integer<B>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000610 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000611 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000612 return converterr("integer<B>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000613 else
614 *p = (unsigned char) ival;
615 break;
616 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000617
Guido van Rossumfce26e72003-04-18 00:12:30 +0000618 case 'h': {/* signed short int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000619 short *p = va_arg(*p_va, short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000620 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000621 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000622 return converterr("integer<h>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000623 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000624 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000625 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfce26e72003-04-18 00:12:30 +0000626 else if (ival < SHRT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000627 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000628 "signed short integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000629 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000630 }
Guido van Rossumfce26e72003-04-18 00:12:30 +0000631 else if (ival > SHRT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000632 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000633 "signed short integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000634 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000635 }
636 else
637 *p = (short) ival;
638 break;
639 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000640
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000641 case 'H': { /* short int sized bitfield, both signed and
642 unsigned allowed */
643 unsigned short *p = va_arg(*p_va, unsigned short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000644 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000645 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000646 return converterr("integer<H>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000647 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000648 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000649 return converterr("integer<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000650 else
651 *p = (unsigned short) ival;
652 break;
653 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000654
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000655 case 'i': {/* signed int */
656 int *p = va_arg(*p_va, int *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000657 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000658 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000659 return converterr("integer<i>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000660 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000661 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000662 return converterr("integer<i>", arg, msgbuf, bufsize);
Georg Brandl98251f82006-06-08 13:31:07 +0000663 else if (ival > INT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000664 PyErr_SetString(PyExc_OverflowError,
665 "signed integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000666 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000667 }
Georg Brandl98251f82006-06-08 13:31:07 +0000668 else if (ival < INT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000669 PyErr_SetString(PyExc_OverflowError,
670 "signed integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000671 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000672 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000673 else
674 *p = ival;
675 break;
676 }
677
Thomas Hellera4ea6032003-04-17 18:55:45 +0000678 case 'I': { /* int sized bitfield, both signed and
679 unsigned allowed */
680 unsigned int *p = va_arg(*p_va, unsigned int *);
681 unsigned int ival;
682 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000683 return converterr("integer<I>", arg, msgbuf, bufsize);
Skip Montanarob5079722006-04-18 00:57:15 +0000684 ival = (unsigned int)PyInt_AsUnsignedLongMask(arg);
685 if (ival == (unsigned int)-1 && PyErr_Occurred())
Thomas Hellera4ea6032003-04-17 18:55:45 +0000686 return converterr("integer<I>", arg, msgbuf, bufsize);
687 else
688 *p = ival;
689 break;
690 }
691
Martin v. Löwis18e16552006-02-15 17:27:45 +0000692 case 'n': /* Py_ssize_t */
693#if SIZEOF_SIZE_T != SIZEOF_LONG
694 {
695 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
696 Py_ssize_t ival;
697 if (float_argument_error(arg))
Georg Brandl7f573f72006-04-13 07:59:30 +0000698 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000699 ival = PyInt_AsSsize_t(arg);
700 if (ival == -1 && PyErr_Occurred())
Georg Brandl7f573f72006-04-13 07:59:30 +0000701 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000702 *p = ival;
703 break;
704 }
705#endif
706 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000707 case 'l': {/* long int */
708 long *p = va_arg(*p_va, long *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000709 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000710 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000711 return converterr("integer<l>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000712 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000713 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000714 return converterr("integer<l>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000715 else
716 *p = ival;
717 break;
718 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000719
720 case 'k': { /* long sized bitfield */
721 unsigned long *p = va_arg(*p_va, unsigned long *);
722 unsigned long ival;
723 if (PyInt_Check(arg))
724 ival = PyInt_AsUnsignedLongMask(arg);
725 else if (PyLong_Check(arg))
726 ival = PyLong_AsUnsignedLongMask(arg);
727 else
728 return converterr("integer<k>", arg, msgbuf, bufsize);
729 *p = ival;
730 break;
731 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000732
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000733#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000734 case 'L': {/* PY_LONG_LONG */
735 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
Mark Dickinson1b34d252010-01-01 17:27:30 +0000736 PY_LONG_LONG ival;
737 if (float_argument_warning(arg))
738 return converterr("long<L>", arg, msgbuf, bufsize);
739 ival = PyLong_AsLongLong(arg);
Neal Norwitzdf6ac3d2008-02-26 05:23:51 +0000740 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000741 return converterr("long<L>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000742 } else {
743 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000744 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000745 break;
746 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000747
748 case 'K': { /* long long sized bitfield */
749 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
750 unsigned PY_LONG_LONG ival;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000751 if (PyInt_Check(arg))
752 ival = PyInt_AsUnsignedLongMask(arg);
753 else if (PyLong_Check(arg))
754 ival = PyLong_AsUnsignedLongLongMask(arg);
755 else
756 return converterr("integer<K>", arg, msgbuf, bufsize);
757 *p = ival;
758 break;
759 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000760#endif
761
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000762 case 'f': {/* float */
763 float *p = va_arg(*p_va, float *);
764 double dval = PyFloat_AsDouble(arg);
765 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000766 return converterr("float<f>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000767 else
768 *p = (float) dval;
769 break;
770 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000771
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000772 case 'd': {/* double */
773 double *p = va_arg(*p_va, double *);
774 double dval = PyFloat_AsDouble(arg);
775 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000776 return converterr("float<d>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000777 else
778 *p = dval;
779 break;
780 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000781
Guido van Rossum530956d1996-07-21 02:27:43 +0000782#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000783 case 'D': {/* complex double */
784 Py_complex *p = va_arg(*p_va, Py_complex *);
785 Py_complex cval;
786 cval = PyComplex_AsCComplex(arg);
787 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000788 return converterr("complex<D>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000789 else
790 *p = cval;
791 break;
792 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000793#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000794
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000795 case 'c': {/* char */
796 char *p = va_arg(*p_va, char *);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000797 if (PyString_Check(arg) && PyString_Size(arg) == 1)
798 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000799 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000800 return converterr("char", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000801 break;
802 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000803
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000804 case 's': {/* string */
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000805 if (*format == '*') {
806 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
807
808 if (PyString_Check(arg)) {
809 PyBuffer_FillInfo(p, arg,
810 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
811 1, 0);
812 }
813#ifdef Py_USING_UNICODE
814 else if (PyUnicode_Check(arg)) {
815 uarg = UNICODE_DEFAULT_ENCODING(arg);
816 if (uarg == NULL)
817 return converterr(CONV_UNICODE,
818 arg, msgbuf, bufsize);
819 PyBuffer_FillInfo(p, arg,
820 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
821 1, 0);
822 }
823#endif
824 else { /* any buffer-like object */
825 char *buf;
826 if (getbuffer(arg, p, &buf) < 0)
827 return converterr(buf, arg, msgbuf, bufsize);
828 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000829 if (addcleanup(p, freelist, cleanup_buffer)) {
830 return converterr(
831 "(cleanup problem)",
832 arg, msgbuf, bufsize);
833 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000834 format++;
835 } else if (*format == '#') {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000836 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000837 FETCH_SIZE;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000838
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000839 if (PyString_Check(arg)) {
840 *p = PyString_AS_STRING(arg);
841 STORE_SIZE(PyString_GET_SIZE(arg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000842 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000843#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000844 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000845 uarg = UNICODE_DEFAULT_ENCODING(arg);
846 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000847 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000848 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000849 *p = PyString_AS_STRING(uarg);
850 STORE_SIZE(PyString_GET_SIZE(uarg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000851 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000852#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000853 else { /* any buffer-like object */
854 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000855 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000856 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000857 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000858 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000859 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000860 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000861 } else {
862 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000863
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000864 if (PyString_Check(arg))
865 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000866#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000867 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000868 uarg = UNICODE_DEFAULT_ENCODING(arg);
869 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000870 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000871 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000872 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000873 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000874#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000875 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000876 return converterr("string", arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000877 if ((Py_ssize_t)strlen(*p) != PyString_Size(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000878 return converterr("string without null bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000879 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000880 }
881 break;
882 }
883
884 case 'z': {/* string, may be NULL (None) */
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000885 if (*format == '*') {
886 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
887
888 if (arg == Py_None)
889 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
890 else if (PyString_Check(arg)) {
891 PyBuffer_FillInfo(p, arg,
892 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
893 1, 0);
894 }
895#ifdef Py_USING_UNICODE
896 else if (PyUnicode_Check(arg)) {
897 uarg = UNICODE_DEFAULT_ENCODING(arg);
898 if (uarg == NULL)
899 return converterr(CONV_UNICODE,
900 arg, msgbuf, bufsize);
901 PyBuffer_FillInfo(p, arg,
902 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
903 1, 0);
904 }
905#endif
906 else { /* any buffer-like object */
907 char *buf;
908 if (getbuffer(arg, p, &buf) < 0)
909 return converterr(buf, arg, msgbuf, bufsize);
910 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000911 if (addcleanup(p, freelist, cleanup_buffer)) {
912 return converterr(
913 "(cleanup problem)",
914 arg, msgbuf, bufsize);
915 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000916 format++;
917 } else if (*format == '#') { /* any buffer-like object */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000918 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000919 FETCH_SIZE;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000920
921 if (arg == Py_None) {
922 *p = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000923 STORE_SIZE(0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000924 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000925 else if (PyString_Check(arg)) {
926 *p = PyString_AS_STRING(arg);
927 STORE_SIZE(PyString_GET_SIZE(arg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000928 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000929#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000930 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000931 uarg = UNICODE_DEFAULT_ENCODING(arg);
932 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000933 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000934 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000935 *p = PyString_AS_STRING(uarg);
936 STORE_SIZE(PyString_GET_SIZE(uarg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000937 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000938#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000939 else { /* any buffer-like object */
940 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000941 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000942 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000943 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000944 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000945 }
946 format++;
947 } else {
948 char **p = va_arg(*p_va, char **);
949
950 if (arg == Py_None)
951 *p = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000952 else if (PyString_Check(arg))
953 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000954#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000955 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000956 uarg = UNICODE_DEFAULT_ENCODING(arg);
957 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000958 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000959 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000960 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000961 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000962#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000963 else
964 return converterr("string or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000965 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000966 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000967 FETCH_SIZE;
Thomas Woutersc3547a32006-03-01 21:31:21 +0000968 assert(0); /* XXX redundant with if-case */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000969 if (arg == Py_None)
970 *q = 0;
971 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000972 *q = PyString_Size(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000973 format++;
974 }
975 else if (*p != NULL &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000976 (Py_ssize_t)strlen(*p) != PyString_Size(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000977 return converterr(
978 "string without null bytes or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000979 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000980 }
981 break;
982 }
983
984 case 'e': {/* encoded string */
985 char **buffer;
986 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000987 PyObject *s;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +0000988 Py_ssize_t size;
989 int recode_strings;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000990
991 /* Get 'e' parameter: the encoding name */
992 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000993#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000994 if (encoding == NULL)
995 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000996#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000997
998 /* Get output buffer parameter:
999 's' (recode all objects via Unicode) or
1000 't' (only recode non-string objects)
1001 */
1002 if (*format == 's')
1003 recode_strings = 1;
1004 else if (*format == 't')
1005 recode_strings = 0;
1006 else
1007 return converterr(
1008 "(unknown parser marker combination)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001009 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001010 buffer = (char **)va_arg(*p_va, char **);
1011 format++;
1012 if (buffer == NULL)
1013 return converterr("(buffer is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001014 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001015
1016 /* Encode object */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001017 if (!recode_strings && PyString_Check(arg)) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001018 s = arg;
1019 Py_INCREF(s);
1020 }
1021 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001022#ifdef Py_USING_UNICODE
1023 PyObject *u;
1024
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001025 /* Convert object to Unicode */
1026 u = PyUnicode_FromObject(arg);
1027 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001028 return converterr(
1029 "string or unicode or text buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001030 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001031
1032 /* Encode object; use default error handling */
1033 s = PyUnicode_AsEncodedString(u,
1034 encoding,
1035 NULL);
1036 Py_DECREF(u);
1037 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001038 return converterr("(encoding failed)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001039 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001040 if (!PyString_Check(s)) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001041 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001042 return converterr(
1043 "(encoder failed to return a string)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001044 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001045 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001046#else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001047 return converterr("string<e>", arg, msgbuf, bufsize);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001048#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001049 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001050 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001051
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001052 /* Write output; output is guaranteed to be 0-terminated */
1053 if (*format == '#') {
1054 /* Using buffer length parameter '#':
1055
1056 - if *buffer is NULL, a new buffer of the
1057 needed size is allocated and the data
1058 copied into it; *buffer is updated to point
1059 to the new buffer; the caller is
1060 responsible for PyMem_Free()ing it after
1061 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001062
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001063 - if *buffer is not NULL, the data is
1064 copied to *buffer; *buffer_len has to be
1065 set to the size of the buffer on input;
1066 buffer overflow is signalled with an error;
1067 buffer has to provide enough room for the
1068 encoded string plus the trailing 0-byte
1069
1070 - in both cases, *buffer_len is updated to
1071 the size of the buffer /excluding/ the
1072 trailing 0-byte
1073
1074 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001075 FETCH_SIZE;
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001076
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001077 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001078 if (q == NULL && q2 == NULL) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001079 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001080 return converterr(
1081 "(buffer_len is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001082 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001083 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001084 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001085 *buffer = PyMem_NEW(char, size + 1);
1086 if (*buffer == NULL) {
1087 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001088 return converterr(
1089 "(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001090 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001091 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +00001092 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001093 Py_DECREF(s);
1094 return converterr(
1095 "(cleanup problem)",
1096 arg, msgbuf, bufsize);
1097 }
Fred Drake25871c02000-05-03 15:17:02 +00001098 } else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001099 if (size + 1 > BUFFER_LEN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001100 Py_DECREF(s);
1101 return converterr(
1102 "(buffer overflow)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001103 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001104 }
Fred Drake25871c02000-05-03 15:17:02 +00001105 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001106 memcpy(*buffer,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001107 PyString_AS_STRING(s),
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001108 size + 1);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001109 STORE_SIZE(size);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001110 } else {
1111 /* Using a 0-terminated buffer:
1112
1113 - the encoded string has to be 0-terminated
1114 for this variant to work; if it is not, an
1115 error raised
Fred Drake25871c02000-05-03 15:17:02 +00001116
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001117 - a new buffer of the needed size is
1118 allocated and the data copied into it;
1119 *buffer is updated to point to the new
1120 buffer; the caller is responsible for
1121 PyMem_Free()ing it after usage
1122
1123 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001124 if ((Py_ssize_t)strlen(PyString_AS_STRING(s))
Armin Rigo7ccbca92006-10-04 12:17:45 +00001125 != size) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001126 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001127 return converterr(
Georg Brandl6c59e722009-04-05 11:54:07 +00001128 "encoded string without NULL bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001129 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001130 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001131 *buffer = PyMem_NEW(char, size + 1);
1132 if (*buffer == NULL) {
1133 Py_DECREF(s);
1134 return converterr("(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001135 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001136 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +00001137 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001138 Py_DECREF(s);
1139 return converterr("(cleanup problem)",
1140 arg, msgbuf, bufsize);
1141 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001142 memcpy(*buffer,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001143 PyString_AS_STRING(s),
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001144 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001145 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001146 Py_DECREF(s);
1147 break;
1148 }
1149
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001150#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001151 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1152 if (*format == '#') { /* any buffer-like object */
1153 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001154 FETCH_SIZE;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001155 if (PyUnicode_Check(arg)) {
1156 *p = PyUnicode_AS_UNICODE(arg);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001157 STORE_SIZE(PyUnicode_GET_SIZE(arg));
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001158 }
1159 else {
Neal Norwitz61546162006-04-14 05:20:28 +00001160 return converterr("cannot convert raw buffers",
1161 arg, msgbuf, bufsize);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001162 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001163 format++;
1164 } else {
1165 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Guido van Rossume826ef02000-03-10 23:02:17 +00001166 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001167 *p = PyUnicode_AS_UNICODE(arg);
1168 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001169 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001170 }
1171 break;
1172 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001173#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001174
1175 case 'S': { /* string object */
1176 PyObject **p = va_arg(*p_va, PyObject **);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001177 if (PyString_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001178 *p = arg;
1179 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001180 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001181 break;
1182 }
1183
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001184#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001185 case 'U': { /* Unicode object */
1186 PyObject **p = va_arg(*p_va, PyObject **);
1187 if (PyUnicode_Check(arg))
1188 *p = arg;
1189 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001190 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001191 break;
1192 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001193#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001194
1195 case 'O': { /* object */
1196 PyTypeObject *type;
1197 PyObject **p;
1198 if (*format == '!') {
1199 type = va_arg(*p_va, PyTypeObject*);
1200 p = va_arg(*p_va, PyObject **);
1201 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +00001202 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +00001203 *p = arg;
1204 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001205 return converterr(type->tp_name, arg, msgbuf, bufsize);
Guido van Rossumfccfe891998-05-15 22:04:07 +00001206
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001207 }
1208 else if (*format == '?') {
1209 inquiry pred = va_arg(*p_va, inquiry);
1210 p = va_arg(*p_va, PyObject **);
1211 format++;
1212 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001213 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001214 else
1215 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001216 arg, msgbuf, bufsize);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001217
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001218 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001219 else if (*format == '&') {
1220 typedef int (*converter)(PyObject *, void *);
1221 converter convert = va_arg(*p_va, converter);
1222 void *addr = va_arg(*p_va, void *);
1223 format++;
1224 if (! (*convert)(arg, addr))
1225 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001226 arg, msgbuf, bufsize);
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001227 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001228 else {
1229 p = va_arg(*p_va, PyObject **);
1230 *p = arg;
1231 }
1232 break;
1233 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001234
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001235
1236 case 'w': { /* memory buffer, read-write access */
1237 void **p = va_arg(*p_va, void **);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001238 void *res;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001239 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +00001240 Py_ssize_t count;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001241
1242 if (pb && pb->bf_releasebuffer && *format != '*')
1243 /* Buffer must be released, yet caller does not use
1244 the Py_buffer protocol. */
1245 return converterr("pinned buffer", arg, msgbuf, bufsize);
1246
1247 if (pb && pb->bf_getbuffer && *format == '*') {
1248 /* Caller is interested in Py_buffer, and the object
1249 supports it directly. */
1250 format++;
1251 if (pb->bf_getbuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1252 PyErr_Clear();
1253 return converterr("read-write buffer", arg, msgbuf, bufsize);
1254 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +00001255 if (addcleanup(p, freelist, cleanup_buffer)) {
1256 return converterr(
1257 "(cleanup problem)",
1258 arg, msgbuf, bufsize);
1259 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001260 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1261 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1262 break;
1263 }
1264
1265 if (pb == NULL ||
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001266 pb->bf_getwritebuffer == NULL ||
1267 pb->bf_getsegcount == NULL)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001268 return converterr("read-write buffer", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001269 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1270 return converterr("single-segment read-write buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001271 arg, msgbuf, bufsize);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001272 if ((count = pb->bf_getwritebuffer(arg, 0, &res)) < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001273 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001274 if (*format == '*') {
1275 PyBuffer_FillInfo((Py_buffer*)p, arg, res, count, 1, 0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001276 format++;
1277 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001278 else {
1279 *p = res;
1280 if (*format == '#') {
1281 FETCH_SIZE;
1282 STORE_SIZE(count);
1283 format++;
1284 }
1285 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001286 break;
1287 }
1288
1289 case 't': { /* 8-bit character buffer, read-only access */
Martin v. Löwis15e62742006-02-27 16:46:16 +00001290 char **p = va_arg(*p_va, char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001291 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +00001292 Py_ssize_t count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001293
1294 if (*format++ != '#')
1295 return converterr(
1296 "invalid use of 't' format character",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001297 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001298 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +00001299 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
1300 pb == NULL || pb->bf_getcharbuffer == NULL ||
1301 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001302 return converterr(
1303 "string or read-only character buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001304 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001305
Jeremy Hylton4819e972001-10-11 14:40:37 +00001306 if (pb->bf_getsegcount(arg, NULL) != 1)
1307 return converterr(
1308 "string or single-segment read-only buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001309 arg, msgbuf, bufsize);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001310
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001311 if (pb->bf_releasebuffer)
1312 return converterr(
1313 "string or pinned buffer",
1314 arg, msgbuf, bufsize);
1315
Jeremy Hylton4819e972001-10-11 14:40:37 +00001316 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001317 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001318 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001319 {
1320 FETCH_SIZE;
1321 STORE_SIZE(count);
1322 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001323 break;
1324 }
1325
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001326 default:
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001327 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001328
1329 }
1330
1331 *p_format = format;
1332 return NULL;
1333}
Guido van Rossumaa354651996-08-19 19:32:04 +00001334
Martin v. Löwis18e16552006-02-15 17:27:45 +00001335static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001336convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001337{
1338 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001339 Py_ssize_t count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001340 if (pb == NULL ||
1341 pb->bf_getreadbuffer == NULL ||
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001342 pb->bf_getsegcount == NULL ||
1343 pb->bf_releasebuffer != NULL) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001344 *errmsg = "string or read-only buffer";
1345 return -1;
1346 }
1347 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
1348 *errmsg = "string or single-segment read-only buffer";
1349 return -1;
1350 }
1351 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
1352 *errmsg = "(unspecified)";
1353 }
1354 return count;
1355}
Guido van Rossumaa354651996-08-19 19:32:04 +00001356
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001357static int
Neal Norwitz18aa3882008-08-24 05:04:52 +00001358getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001359{
1360 void *buf;
1361 Py_ssize_t count;
1362 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1363 if (pb == NULL) {
1364 *errmsg = "string or buffer";
1365 return -1;
1366 }
1367 if (pb->bf_getbuffer) {
Neal Norwitz18aa3882008-08-24 05:04:52 +00001368 if (pb->bf_getbuffer(arg, view, 0) < 0) {
1369 *errmsg = "convertible to a buffer";
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001370 return -1;
Neal Norwitz18aa3882008-08-24 05:04:52 +00001371 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001372 if (!PyBuffer_IsContiguous(view, 'C')) {
1373 *errmsg = "contiguous buffer";
1374 return -1;
1375 }
1376 return 0;
1377 }
1378
1379 count = convertbuffer(arg, &buf, errmsg);
Neal Norwitz18aa3882008-08-24 05:04:52 +00001380 if (count < 0) {
1381 *errmsg = "convertible to a buffer";
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001382 return count;
Neal Norwitz18aa3882008-08-24 05:04:52 +00001383 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001384 PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
1385 return 0;
1386}
1387
Guido van Rossumaa354651996-08-19 19:32:04 +00001388/* Support for keyword arguments donated by
1389 Geoff Philbrick <philbric@delphi.hks.com> */
1390
Tim Petersf8cd3e82001-10-27 04:26:57 +00001391/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001392int
1393PyArg_ParseTupleAndKeywords(PyObject *args,
1394 PyObject *keywords,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001395 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001396 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001397{
1398 int retval;
1399 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001400
1401 if ((args == NULL || !PyTuple_Check(args)) ||
1402 (keywords != NULL && !PyDict_Check(keywords)) ||
1403 format == NULL ||
1404 kwlist == NULL)
1405 {
1406 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001407 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001408 }
1409
Guido van Rossumaa354651996-08-19 19:32:04 +00001410 va_start(va, kwlist);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001411 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1412 va_end(va);
1413 return retval;
1414}
1415
1416int
1417_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1418 PyObject *keywords,
1419 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001420 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001421{
1422 int retval;
1423 va_list va;
1424
1425 if ((args == NULL || !PyTuple_Check(args)) ||
1426 (keywords != NULL && !PyDict_Check(keywords)) ||
1427 format == NULL ||
1428 kwlist == NULL)
1429 {
1430 PyErr_BadInternalCall();
1431 return 0;
1432 }
1433
1434 va_start(va, kwlist);
1435 retval = vgetargskeywords(args, keywords, format,
1436 kwlist, &va, FLAG_SIZE_T);
Guido van Rossumaa354651996-08-19 19:32:04 +00001437 va_end(va);
1438 return retval;
1439}
1440
1441
Brett Cannon711e7d92004-07-10 22:20:32 +00001442int
1443PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001444 PyObject *keywords,
1445 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001446 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001447{
1448 int retval;
1449 va_list lva;
1450
1451 if ((args == NULL || !PyTuple_Check(args)) ||
1452 (keywords != NULL && !PyDict_Check(keywords)) ||
1453 format == NULL ||
1454 kwlist == NULL)
1455 {
1456 PyErr_BadInternalCall();
1457 return 0;
1458 }
1459
1460#ifdef VA_LIST_IS_ARRAY
1461 memcpy(lva, va, sizeof(va_list));
1462#else
1463#ifdef __va_copy
1464 __va_copy(lva, va);
1465#else
1466 lva = va;
1467#endif
1468#endif
1469
Martin v. Löwis18e16552006-02-15 17:27:45 +00001470 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1471 return retval;
1472}
1473
1474int
1475_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1476 PyObject *keywords,
1477 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001478 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001479{
1480 int retval;
1481 va_list lva;
1482
1483 if ((args == NULL || !PyTuple_Check(args)) ||
1484 (keywords != NULL && !PyDict_Check(keywords)) ||
1485 format == NULL ||
1486 kwlist == NULL)
1487 {
1488 PyErr_BadInternalCall();
1489 return 0;
1490 }
1491
1492#ifdef VA_LIST_IS_ARRAY
1493 memcpy(lva, va, sizeof(va_list));
1494#else
1495#ifdef __va_copy
1496 __va_copy(lva, va);
1497#else
1498 lva = va;
1499#endif
1500#endif
1501
1502 retval = vgetargskeywords(args, keywords, format,
1503 kwlist, &lva, FLAG_SIZE_T);
Brett Cannon711e7d92004-07-10 22:20:32 +00001504 return retval;
1505}
1506
Christian Heimesea837932008-02-26 17:23:51 +00001507#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001508
Guido van Rossumaa354651996-08-19 19:32:04 +00001509static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001510vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001511 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001512{
Tim Petersdc5eff92001-10-27 06:53:00 +00001513 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001514 int levels[32];
Christian Heimesea837932008-02-26 17:23:51 +00001515 const char *fname, *msg, *custom_msg, *keyword;
1516 int min = INT_MAX;
Tim Petersb639d492001-10-27 07:00:56 +00001517 int i, len, nargs, nkeywords;
Christian Heimesea837932008-02-26 17:23:51 +00001518 PyObject *freelist = NULL, *current_arg;
Tim Petersf4331c12001-10-27 00:17:34 +00001519
Tim Peters45772cd2001-10-27 03:58:40 +00001520 assert(args != NULL && PyTuple_Check(args));
1521 assert(keywords == NULL || PyDict_Check(keywords));
1522 assert(format != NULL);
1523 assert(kwlist != NULL);
1524 assert(p_va != NULL);
1525
Christian Heimesea837932008-02-26 17:23:51 +00001526 /* grab the function name or custom error msg first (mutually exclusive) */
1527 fname = strchr(format, ':');
1528 if (fname) {
1529 fname++;
1530 custom_msg = NULL;
Tim Peters62d48e12001-10-27 06:42:16 +00001531 }
Christian Heimesea837932008-02-26 17:23:51 +00001532 else {
1533 custom_msg = strchr(format,';');
1534 if (custom_msg)
1535 custom_msg++;
Tim Peters62d48e12001-10-27 06:42:16 +00001536 }
Christian Heimesea837932008-02-26 17:23:51 +00001537
1538 /* scan kwlist and get greatest possible nbr of args */
1539 for (len=0; kwlist[len]; len++)
1540 continue;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001541
Tim Peters6fb26352001-10-27 04:38:11 +00001542 nargs = PyTuple_GET_SIZE(args);
Christian Heimesea837932008-02-26 17:23:51 +00001543 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1544 if (nargs + nkeywords > len) {
1545 PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
1546 "argument%s (%d given)",
1547 (fname == NULL) ? "function" : fname,
1548 (fname == NULL) ? "" : "()",
1549 len,
1550 (len == 1) ? "" : "s",
1551 nargs + nkeywords);
Guido van Rossumaa354651996-08-19 19:32:04 +00001552 return 0;
1553 }
Tim Petersc2f01122001-10-27 07:25:06 +00001554
Christian Heimesea837932008-02-26 17:23:51 +00001555 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1556 for (i = 0; i < len; i++) {
1557 keyword = kwlist[i];
1558 if (*format == '|') {
1559 min = i;
Guido van Rossumaa354651996-08-19 19:32:04 +00001560 format++;
Christian Heimesea837932008-02-26 17:23:51 +00001561 }
1562 if (IS_END_OF_FORMAT(*format)) {
1563 PyErr_Format(PyExc_RuntimeError,
1564 "More keyword list entries (%d) than "
1565 "format specifiers (%d)", len, i);
1566 return cleanreturn(0, freelist);
1567 }
1568 current_arg = NULL;
1569 if (nkeywords) {
1570 current_arg = PyDict_GetItemString(keywords, keyword);
1571 }
1572 if (current_arg) {
1573 --nkeywords;
1574 if (i < nargs) {
1575 /* arg present in tuple and in dict */
1576 PyErr_Format(PyExc_TypeError,
1577 "Argument given by name ('%s') "
1578 "and position (%d)",
1579 keyword, i+1);
1580 return cleanreturn(0, freelist);
1581 }
1582 }
1583 else if (nkeywords && PyErr_Occurred())
1584 return cleanreturn(0, freelist);
1585 else if (i < nargs)
1586 current_arg = PyTuple_GET_ITEM(args, i);
1587
1588 if (current_arg) {
1589 msg = convertitem(current_arg, &format, p_va, flags,
1590 levels, msgbuf, sizeof(msgbuf), &freelist);
1591 if (msg) {
1592 seterror(i+1, msg, levels, fname, custom_msg);
1593 return cleanreturn(0, freelist);
1594 }
1595 continue;
1596 }
1597
1598 if (i < min) {
1599 PyErr_Format(PyExc_TypeError, "Required argument "
1600 "'%s' (pos %d) not found",
1601 keyword, i+1);
1602 return cleanreturn(0, freelist);
1603 }
1604 /* current code reports success when all required args
1605 * fulfilled and no keyword args left, with no further
1606 * validation. XXX Maybe skip this in debug build ?
1607 */
1608 if (!nkeywords)
1609 return cleanreturn(1, freelist);
1610
1611 /* We are into optional args, skip thru to any remaining
1612 * keyword args */
1613 msg = skipitem(&format, p_va, flags);
Guido van Rossumaa354651996-08-19 19:32:04 +00001614 if (msg) {
Christian Heimesea837932008-02-26 17:23:51 +00001615 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1616 format);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001617 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001618 }
1619 }
1620
Benjamin Peterson4caef5c2008-12-22 20:51:15 +00001621 if (!IS_END_OF_FORMAT(*format) && *format != '|') {
Christian Heimesea837932008-02-26 17:23:51 +00001622 PyErr_Format(PyExc_RuntimeError,
1623 "more argument specifiers than keyword list entries "
1624 "(remaining format:'%s')", format);
1625 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001626 }
Tim Petersb054be42001-10-27 05:07:41 +00001627
Guido van Rossumaa354651996-08-19 19:32:04 +00001628 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001629 if (nkeywords > 0) {
1630 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001631 Py_ssize_t pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001632 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001633 int match = 0;
Guido van Rossum55474762002-04-04 16:22:30 +00001634 char *ks;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001635 if (!PyString_Check(key)) {
Guido van Rossum55474762002-04-04 16:22:30 +00001636 PyErr_SetString(PyExc_TypeError,
1637 "keywords must be strings");
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001638 return cleanreturn(0, freelist);
Guido van Rossum55474762002-04-04 16:22:30 +00001639 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001640 ks = PyString_AsString(key);
Christian Heimesea837932008-02-26 17:23:51 +00001641 for (i = 0; i < len; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001642 if (!strcmp(ks, kwlist[i])) {
1643 match = 1;
1644 break;
1645 }
1646 }
1647 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001648 PyErr_Format(PyExc_TypeError,
1649 "'%s' is an invalid keyword "
1650 "argument for this function",
1651 ks);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001652 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001653 }
1654 }
1655 }
Tim Petersc2f01122001-10-27 07:25:06 +00001656
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001657 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001658}
1659
1660
1661static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001662skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001663{
Christian Heimesea837932008-02-26 17:23:51 +00001664 const char *format = *p_format;
Guido van Rossumaa354651996-08-19 19:32:04 +00001665 char c = *format++;
1666
1667 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001668
1669 /* simple codes
1670 * The individual types (second arg of va_arg) are irrelevant */
1671
Guido van Rossumaa354651996-08-19 19:32:04 +00001672 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001673 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001674 case 'h': /* short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001675 case 'H': /* short int as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001676 case 'i': /* int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001677 case 'I': /* int sized bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001678 case 'l': /* long int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001679 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001680#ifdef HAVE_LONG_LONG
Georg Brandl6dd14612005-09-14 19:29:53 +00001681 case 'L': /* PY_LONG_LONG */
1682 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001683#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001684 case 'f': /* float */
Guido van Rossumaa354651996-08-19 19:32:04 +00001685 case 'd': /* double */
Guido van Rossumaa354651996-08-19 19:32:04 +00001686#ifndef WITHOUT_COMPLEX
1687 case 'D': /* complex double */
Georg Brandl6dd14612005-09-14 19:29:53 +00001688#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001689 case 'c': /* char */
1690 {
Georg Brandl6dd14612005-09-14 19:29:53 +00001691 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001692 break;
1693 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001694
1695 case 'n': /* Py_ssize_t */
1696 {
1697 (void) va_arg(*p_va, Py_ssize_t *);
1698 break;
1699 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001700
Georg Brandl6dd14612005-09-14 19:29:53 +00001701 /* string codes */
1702
1703 case 'e': /* string with encoding */
1704 {
1705 (void) va_arg(*p_va, const char *);
1706 if (!(*format == 's' || *format == 't'))
1707 /* after 'e', only 's' and 't' is allowed */
1708 goto err;
1709 format++;
1710 /* explicit fallthrough to string cases */
1711 }
1712
Guido van Rossumaa354651996-08-19 19:32:04 +00001713 case 's': /* string */
Georg Brandl6dd14612005-09-14 19:29:53 +00001714 case 'z': /* string or None */
1715#ifdef Py_USING_UNICODE
1716 case 'u': /* unicode string */
1717#endif
1718 case 't': /* buffer, read-only */
1719 case 'w': /* buffer, read-write */
Guido van Rossumaa354651996-08-19 19:32:04 +00001720 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001721 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001722 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001723 if (flags & FLAG_SIZE_T)
1724 (void) va_arg(*p_va, Py_ssize_t *);
1725 else
1726 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001727 format++;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001728 } else if ((c == 's' || c == 'z') && *format == '*') {
1729 format++;
Guido van Rossumaa354651996-08-19 19:32:04 +00001730 }
1731 break;
1732 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001733
1734 /* object codes */
1735
Guido van Rossumaa354651996-08-19 19:32:04 +00001736 case 'S': /* string object */
Georg Brandl6dd14612005-09-14 19:29:53 +00001737#ifdef Py_USING_UNICODE
1738 case 'U': /* unicode string object */
1739#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001740 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001741 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001742 break;
1743 }
1744
1745 case 'O': /* object */
1746 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001747 if (*format == '!') {
1748 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001749 (void) va_arg(*p_va, PyTypeObject*);
1750 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001751 }
1752#if 0
1753/* I don't know what this is for */
1754 else if (*format == '?') {
1755 inquiry pred = va_arg(*p_va, inquiry);
1756 format++;
1757 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001758 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001759 }
1760 }
1761#endif
1762 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001763 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001764 (void) va_arg(*p_va, converter);
1765 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001766 format++;
1767 }
1768 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001769 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001770 }
1771 break;
1772 }
Christian Heimesea837932008-02-26 17:23:51 +00001773
1774 case '(': /* bypass tuple, not handled at all previously */
1775 {
1776 char *msg;
1777 for (;;) {
1778 if (*format==')')
1779 break;
1780 if (IS_END_OF_FORMAT(*format))
1781 return "Unmatched left paren in format "
1782 "string";
1783 msg = skipitem(&format, p_va, flags);
1784 if (msg)
1785 return msg;
1786 }
1787 format++;
1788 break;
1789 }
1790
1791 case ')':
1792 return "Unmatched right paren in format string";
1793
Guido van Rossumaa354651996-08-19 19:32:04 +00001794 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001795err:
Guido van Rossumaa354651996-08-19 19:32:04 +00001796 return "impossible<bad format char>";
1797
1798 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001799
Guido van Rossumaa354651996-08-19 19:32:04 +00001800 *p_format = format;
1801 return NULL;
1802}
Fred Drakee4616e62001-10-23 21:09:29 +00001803
1804
1805int
Martin v. Löwis76246742006-03-01 04:06:10 +00001806PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001807{
Martin v. Löwis76246742006-03-01 04:06:10 +00001808 Py_ssize_t i, l;
Fred Drakee4616e62001-10-23 21:09:29 +00001809 PyObject **o;
1810 va_list vargs;
1811
1812#ifdef HAVE_STDARG_PROTOTYPES
1813 va_start(vargs, max);
1814#else
1815 va_start(vargs);
1816#endif
1817
1818 assert(min >= 0);
1819 assert(min <= max);
1820 if (!PyTuple_Check(args)) {
1821 PyErr_SetString(PyExc_SystemError,
1822 "PyArg_UnpackTuple() argument list is not a tuple");
1823 return 0;
1824 }
1825 l = PyTuple_GET_SIZE(args);
1826 if (l < min) {
1827 if (name != NULL)
1828 PyErr_Format(
1829 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001830 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001831 name, (min == max ? "" : "at least "), min, l);
1832 else
1833 PyErr_Format(
1834 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001835 "unpacked tuple should have %s%zd elements,"
1836 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001837 (min == max ? "" : "at least "), min, l);
1838 va_end(vargs);
1839 return 0;
1840 }
1841 if (l > max) {
1842 if (name != NULL)
1843 PyErr_Format(
1844 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001845 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001846 name, (min == max ? "" : "at most "), max, l);
1847 else
1848 PyErr_Format(
1849 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001850 "unpacked tuple should have %s%zd elements,"
1851 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001852 (min == max ? "" : "at most "), max, l);
1853 va_end(vargs);
1854 return 0;
1855 }
1856 for (i = 0; i < l; i++) {
1857 o = va_arg(vargs, PyObject **);
1858 *o = PyTuple_GET_ITEM(args, i);
1859 }
1860 va_end(vargs);
1861 return 1;
1862}
Georg Brandl02c42872005-08-26 06:42:30 +00001863
1864
1865/* For type constructors that don't take keyword args
1866 *
1867 * Sets a TypeError and returns 0 if the kwds dict is
Walter Dörwaldd14bf612006-09-21 15:09:55 +00001868 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001869 */
1870int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001871_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001872{
1873 if (kw == NULL)
1874 return 1;
1875 if (!PyDict_CheckExact(kw)) {
1876 PyErr_BadInternalCall();
1877 return 0;
1878 }
1879 if (PyDict_Size(kw) == 0)
1880 return 1;
1881
1882 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1883 funcname);
1884 return 0;
1885}
Anthony Baxter97300382006-04-12 04:38:54 +00001886#ifdef __cplusplus
1887};
1888#endif