blob: d24857d29cccd033c0769374addf48a70e6c71c8 [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) {
331 seterror(i+1, msg, levels, fname, message);
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
529float_argument_error(PyObject *arg)
530{
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
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000539/* Convert a non-tuple argument. Return NULL if conversion went OK,
540 or a string with a message describing the failure. The message is
541 formatted as "must be <desired type>, not <actual type>".
542 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000543 Don't call if a tuple is expected.
544
545 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000546*/
547
548static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000549convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000550 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000551{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000552 /* For # codes */
553#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
554 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
555 else q=va_arg(*p_va, int*);
556#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
557#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
558
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000559 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000560 char c = *format++;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000561#ifdef Py_USING_UNICODE
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000562 PyObject *uarg;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000563#endif
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000564
565 switch (c) {
566
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000567 case 'b': { /* unsigned byte -- very short int */
568 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000569 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000570 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000571 return converterr("integer<b>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000572 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000573 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000574 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000575 else if (ival < 0) {
576 PyErr_SetString(PyExc_OverflowError,
577 "unsigned byte integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000578 return converterr("integer<b>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000579 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000580 else if (ival > UCHAR_MAX) {
581 PyErr_SetString(PyExc_OverflowError,
582 "unsigned byte integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000583 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000584 }
585 else
586 *p = (unsigned char) ival;
587 break;
588 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000589
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000590 case 'B': {/* byte sized bitfield - both signed and unsigned
591 values allowed */
592 char *p = va_arg(*p_va, char *);
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<B>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000596 ival = PyInt_AsUnsignedLongMask(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<B>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000599 else
600 *p = (unsigned char) ival;
601 break;
602 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000603
Guido van Rossumfce26e72003-04-18 00:12:30 +0000604 case 'h': {/* signed short int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000605 short *p = va_arg(*p_va, short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000606 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000607 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000608 return converterr("integer<h>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000609 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000610 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000611 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfce26e72003-04-18 00:12:30 +0000612 else if (ival < SHRT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000613 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000614 "signed short integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000615 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000616 }
Guido van Rossumfce26e72003-04-18 00:12:30 +0000617 else if (ival > SHRT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000618 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000619 "signed short integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000620 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000621 }
622 else
623 *p = (short) ival;
624 break;
625 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000626
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000627 case 'H': { /* short int sized bitfield, both signed and
628 unsigned allowed */
629 unsigned short *p = va_arg(*p_va, unsigned short *);
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<H>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000633 ival = PyInt_AsUnsignedLongMask(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<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000636 else
637 *p = (unsigned short) ival;
638 break;
639 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000640
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000641 case 'i': {/* signed int */
642 int *p = va_arg(*p_va, int *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000643 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000644 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000645 return converterr("integer<i>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000646 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000647 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000648 return converterr("integer<i>", arg, msgbuf, bufsize);
Georg Brandl98251f82006-06-08 13:31:07 +0000649 else if (ival > INT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000650 PyErr_SetString(PyExc_OverflowError,
651 "signed integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000652 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000653 }
Georg Brandl98251f82006-06-08 13:31:07 +0000654 else if (ival < INT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000655 PyErr_SetString(PyExc_OverflowError,
656 "signed integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000657 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000658 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000659 else
660 *p = ival;
661 break;
662 }
663
Thomas Hellera4ea6032003-04-17 18:55:45 +0000664 case 'I': { /* int sized bitfield, both signed and
665 unsigned allowed */
666 unsigned int *p = va_arg(*p_va, unsigned int *);
667 unsigned int ival;
668 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000669 return converterr("integer<I>", arg, msgbuf, bufsize);
Skip Montanarob5079722006-04-18 00:57:15 +0000670 ival = (unsigned int)PyInt_AsUnsignedLongMask(arg);
671 if (ival == (unsigned int)-1 && PyErr_Occurred())
Thomas Hellera4ea6032003-04-17 18:55:45 +0000672 return converterr("integer<I>", arg, msgbuf, bufsize);
673 else
674 *p = ival;
675 break;
676 }
677
Martin v. Löwis18e16552006-02-15 17:27:45 +0000678 case 'n': /* Py_ssize_t */
679#if SIZEOF_SIZE_T != SIZEOF_LONG
680 {
681 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
682 Py_ssize_t ival;
683 if (float_argument_error(arg))
Georg Brandl7f573f72006-04-13 07:59:30 +0000684 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000685 ival = PyInt_AsSsize_t(arg);
686 if (ival == -1 && PyErr_Occurred())
Georg Brandl7f573f72006-04-13 07:59:30 +0000687 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000688 *p = ival;
689 break;
690 }
691#endif
692 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000693 case 'l': {/* long int */
694 long *p = va_arg(*p_va, long *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000695 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000696 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000697 return converterr("integer<l>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000698 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000699 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000700 return converterr("integer<l>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000701 else
702 *p = ival;
703 break;
704 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000705
706 case 'k': { /* long sized bitfield */
707 unsigned long *p = va_arg(*p_va, unsigned long *);
708 unsigned long ival;
709 if (PyInt_Check(arg))
710 ival = PyInt_AsUnsignedLongMask(arg);
711 else if (PyLong_Check(arg))
712 ival = PyLong_AsUnsignedLongMask(arg);
713 else
714 return converterr("integer<k>", arg, msgbuf, bufsize);
715 *p = ival;
716 break;
717 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000718
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000719#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000720 case 'L': {/* PY_LONG_LONG */
721 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
722 PY_LONG_LONG ival = PyLong_AsLongLong( arg );
Neal Norwitzdf6ac3d2008-02-26 05:23:51 +0000723 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000724 return converterr("long<L>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000725 } else {
726 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000727 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000728 break;
729 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000730
731 case 'K': { /* long long sized bitfield */
732 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
733 unsigned PY_LONG_LONG ival;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000734 if (PyInt_Check(arg))
735 ival = PyInt_AsUnsignedLongMask(arg);
736 else if (PyLong_Check(arg))
737 ival = PyLong_AsUnsignedLongLongMask(arg);
738 else
739 return converterr("integer<K>", arg, msgbuf, bufsize);
740 *p = ival;
741 break;
742 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000743#endif
744
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000745 case 'f': {/* float */
746 float *p = va_arg(*p_va, float *);
747 double dval = PyFloat_AsDouble(arg);
748 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000749 return converterr("float<f>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000750 else
751 *p = (float) dval;
752 break;
753 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000754
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000755 case 'd': {/* double */
756 double *p = va_arg(*p_va, double *);
757 double dval = PyFloat_AsDouble(arg);
758 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000759 return converterr("float<d>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000760 else
761 *p = dval;
762 break;
763 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000764
Guido van Rossum530956d1996-07-21 02:27:43 +0000765#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000766 case 'D': {/* complex double */
767 Py_complex *p = va_arg(*p_va, Py_complex *);
768 Py_complex cval;
769 cval = PyComplex_AsCComplex(arg);
770 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000771 return converterr("complex<D>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000772 else
773 *p = cval;
774 break;
775 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000776#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000777
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000778 case 'c': {/* char */
779 char *p = va_arg(*p_va, char *);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000780 if (PyString_Check(arg) && PyString_Size(arg) == 1)
781 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000782 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000783 return converterr("char", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000784 break;
785 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000786
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000787 case 's': {/* string */
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000788 if (*format == '*') {
789 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
790
791 if (PyString_Check(arg)) {
792 PyBuffer_FillInfo(p, arg,
793 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
794 1, 0);
795 }
796#ifdef Py_USING_UNICODE
797 else if (PyUnicode_Check(arg)) {
798 uarg = UNICODE_DEFAULT_ENCODING(arg);
799 if (uarg == NULL)
800 return converterr(CONV_UNICODE,
801 arg, msgbuf, bufsize);
802 PyBuffer_FillInfo(p, arg,
803 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
804 1, 0);
805 }
806#endif
807 else { /* any buffer-like object */
808 char *buf;
809 if (getbuffer(arg, p, &buf) < 0)
810 return converterr(buf, arg, msgbuf, bufsize);
811 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000812 if (addcleanup(p, freelist, cleanup_buffer)) {
813 return converterr(
814 "(cleanup problem)",
815 arg, msgbuf, bufsize);
816 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000817 format++;
818 } else if (*format == '#') {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000819 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000820 FETCH_SIZE;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000821
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000822 if (PyString_Check(arg)) {
823 *p = PyString_AS_STRING(arg);
824 STORE_SIZE(PyString_GET_SIZE(arg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000825 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000826#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000827 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000828 uarg = UNICODE_DEFAULT_ENCODING(arg);
829 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000830 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000831 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000832 *p = PyString_AS_STRING(uarg);
833 STORE_SIZE(PyString_GET_SIZE(uarg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000834 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000835#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000836 else { /* any buffer-like object */
837 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000838 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000839 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000840 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000841 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000842 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000843 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000844 } else {
845 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000846
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000847 if (PyString_Check(arg))
848 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000849#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000850 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000851 uarg = UNICODE_DEFAULT_ENCODING(arg);
852 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000853 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000854 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000855 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000856 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000857#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000858 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000859 return converterr("string", arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000860 if ((Py_ssize_t)strlen(*p) != PyString_Size(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000861 return converterr("string without null bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000862 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000863 }
864 break;
865 }
866
867 case 'z': {/* string, may be NULL (None) */
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000868 if (*format == '*') {
869 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
870
871 if (arg == Py_None)
872 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
873 else if (PyString_Check(arg)) {
874 PyBuffer_FillInfo(p, arg,
875 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
876 1, 0);
877 }
878#ifdef Py_USING_UNICODE
879 else if (PyUnicode_Check(arg)) {
880 uarg = UNICODE_DEFAULT_ENCODING(arg);
881 if (uarg == NULL)
882 return converterr(CONV_UNICODE,
883 arg, msgbuf, bufsize);
884 PyBuffer_FillInfo(p, arg,
885 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
886 1, 0);
887 }
888#endif
889 else { /* any buffer-like object */
890 char *buf;
891 if (getbuffer(arg, p, &buf) < 0)
892 return converterr(buf, arg, msgbuf, bufsize);
893 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000894 if (addcleanup(p, freelist, cleanup_buffer)) {
895 return converterr(
896 "(cleanup problem)",
897 arg, msgbuf, bufsize);
898 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000899 format++;
900 } else if (*format == '#') { /* any buffer-like object */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000901 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000902 FETCH_SIZE;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000903
904 if (arg == Py_None) {
905 *p = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000906 STORE_SIZE(0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000907 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000908 else if (PyString_Check(arg)) {
909 *p = PyString_AS_STRING(arg);
910 STORE_SIZE(PyString_GET_SIZE(arg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000911 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000912#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000913 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000914 uarg = UNICODE_DEFAULT_ENCODING(arg);
915 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000916 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000917 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000918 *p = PyString_AS_STRING(uarg);
919 STORE_SIZE(PyString_GET_SIZE(uarg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000920 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000921#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000922 else { /* any buffer-like object */
923 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000924 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000925 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000926 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000927 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000928 }
929 format++;
930 } else {
931 char **p = va_arg(*p_va, char **);
932
933 if (arg == Py_None)
934 *p = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000935 else if (PyString_Check(arg))
936 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000937#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000938 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000939 uarg = UNICODE_DEFAULT_ENCODING(arg);
940 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000941 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000942 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000943 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000944 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000945#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000946 else
947 return converterr("string or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000948 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000949 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000950 FETCH_SIZE;
Thomas Woutersc3547a32006-03-01 21:31:21 +0000951 assert(0); /* XXX redundant with if-case */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000952 if (arg == Py_None)
953 *q = 0;
954 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000955 *q = PyString_Size(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000956 format++;
957 }
958 else if (*p != NULL &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000959 (Py_ssize_t)strlen(*p) != PyString_Size(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000960 return converterr(
961 "string without null bytes or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000962 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000963 }
964 break;
965 }
966
967 case 'e': {/* encoded string */
968 char **buffer;
969 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000970 PyObject *s;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +0000971 Py_ssize_t size;
972 int recode_strings;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000973
974 /* Get 'e' parameter: the encoding name */
975 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000976#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000977 if (encoding == NULL)
978 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000979#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000980
981 /* Get output buffer parameter:
982 's' (recode all objects via Unicode) or
983 't' (only recode non-string objects)
984 */
985 if (*format == 's')
986 recode_strings = 1;
987 else if (*format == 't')
988 recode_strings = 0;
989 else
990 return converterr(
991 "(unknown parser marker combination)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000992 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000993 buffer = (char **)va_arg(*p_va, char **);
994 format++;
995 if (buffer == NULL)
996 return converterr("(buffer is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000997 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000998
999 /* Encode object */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001000 if (!recode_strings && PyString_Check(arg)) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001001 s = arg;
1002 Py_INCREF(s);
1003 }
1004 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001005#ifdef Py_USING_UNICODE
1006 PyObject *u;
1007
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001008 /* Convert object to Unicode */
1009 u = PyUnicode_FromObject(arg);
1010 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001011 return converterr(
1012 "string or unicode or text buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001013 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001014
1015 /* Encode object; use default error handling */
1016 s = PyUnicode_AsEncodedString(u,
1017 encoding,
1018 NULL);
1019 Py_DECREF(u);
1020 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001021 return converterr("(encoding failed)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001022 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001023 if (!PyString_Check(s)) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001024 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001025 return converterr(
1026 "(encoder failed to return a string)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001027 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001028 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001029#else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001030 return converterr("string<e>", arg, msgbuf, bufsize);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001031#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001032 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001033 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001034
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001035 /* Write output; output is guaranteed to be 0-terminated */
1036 if (*format == '#') {
1037 /* Using buffer length parameter '#':
1038
1039 - if *buffer is NULL, a new buffer of the
1040 needed size is allocated and the data
1041 copied into it; *buffer is updated to point
1042 to the new buffer; the caller is
1043 responsible for PyMem_Free()ing it after
1044 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001045
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001046 - if *buffer is not NULL, the data is
1047 copied to *buffer; *buffer_len has to be
1048 set to the size of the buffer on input;
1049 buffer overflow is signalled with an error;
1050 buffer has to provide enough room for the
1051 encoded string plus the trailing 0-byte
1052
1053 - in both cases, *buffer_len is updated to
1054 the size of the buffer /excluding/ the
1055 trailing 0-byte
1056
1057 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001058 FETCH_SIZE;
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001059
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001060 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001061 if (q == NULL && q2 == NULL) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001062 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001063 return converterr(
1064 "(buffer_len is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001065 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001066 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001067 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001068 *buffer = PyMem_NEW(char, size + 1);
1069 if (*buffer == NULL) {
1070 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001071 return converterr(
1072 "(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001073 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001074 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +00001075 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001076 Py_DECREF(s);
1077 return converterr(
1078 "(cleanup problem)",
1079 arg, msgbuf, bufsize);
1080 }
Fred Drake25871c02000-05-03 15:17:02 +00001081 } else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001082 if (size + 1 > BUFFER_LEN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001083 Py_DECREF(s);
1084 return converterr(
1085 "(buffer overflow)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001086 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001087 }
Fred Drake25871c02000-05-03 15:17:02 +00001088 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001089 memcpy(*buffer,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001090 PyString_AS_STRING(s),
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001091 size + 1);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001092 STORE_SIZE(size);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001093 } else {
1094 /* Using a 0-terminated buffer:
1095
1096 - the encoded string has to be 0-terminated
1097 for this variant to work; if it is not, an
1098 error raised
Fred Drake25871c02000-05-03 15:17:02 +00001099
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001100 - a new buffer of the needed size is
1101 allocated and the data copied into it;
1102 *buffer is updated to point to the new
1103 buffer; the caller is responsible for
1104 PyMem_Free()ing it after usage
1105
1106 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001107 if ((Py_ssize_t)strlen(PyString_AS_STRING(s))
Armin Rigo7ccbca92006-10-04 12:17:45 +00001108 != size) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001109 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001110 return converterr(
Georg Brandl6c59e722009-04-05 11:54:07 +00001111 "encoded string without NULL bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001112 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001113 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001114 *buffer = PyMem_NEW(char, size + 1);
1115 if (*buffer == NULL) {
1116 Py_DECREF(s);
1117 return converterr("(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001118 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001119 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +00001120 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001121 Py_DECREF(s);
1122 return converterr("(cleanup problem)",
1123 arg, msgbuf, bufsize);
1124 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001125 memcpy(*buffer,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001126 PyString_AS_STRING(s),
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001127 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001128 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001129 Py_DECREF(s);
1130 break;
1131 }
1132
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001133#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001134 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1135 if (*format == '#') { /* any buffer-like object */
1136 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001137 FETCH_SIZE;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001138 if (PyUnicode_Check(arg)) {
1139 *p = PyUnicode_AS_UNICODE(arg);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001140 STORE_SIZE(PyUnicode_GET_SIZE(arg));
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001141 }
1142 else {
Neal Norwitz61546162006-04-14 05:20:28 +00001143 return converterr("cannot convert raw buffers",
1144 arg, msgbuf, bufsize);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001145 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001146 format++;
1147 } else {
1148 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Guido van Rossume826ef02000-03-10 23:02:17 +00001149 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001150 *p = PyUnicode_AS_UNICODE(arg);
1151 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001152 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001153 }
1154 break;
1155 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001156#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001157
1158 case 'S': { /* string object */
1159 PyObject **p = va_arg(*p_va, PyObject **);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001160 if (PyString_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001161 *p = arg;
1162 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001163 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001164 break;
1165 }
1166
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001167#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001168 case 'U': { /* Unicode object */
1169 PyObject **p = va_arg(*p_va, PyObject **);
1170 if (PyUnicode_Check(arg))
1171 *p = arg;
1172 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001173 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001174 break;
1175 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001176#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001177
1178 case 'O': { /* object */
1179 PyTypeObject *type;
1180 PyObject **p;
1181 if (*format == '!') {
1182 type = va_arg(*p_va, PyTypeObject*);
1183 p = va_arg(*p_va, PyObject **);
1184 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +00001185 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +00001186 *p = arg;
1187 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001188 return converterr(type->tp_name, arg, msgbuf, bufsize);
Guido van Rossumfccfe891998-05-15 22:04:07 +00001189
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001190 }
1191 else if (*format == '?') {
1192 inquiry pred = va_arg(*p_va, inquiry);
1193 p = va_arg(*p_va, PyObject **);
1194 format++;
1195 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001196 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001197 else
1198 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001199 arg, msgbuf, bufsize);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001200
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001201 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001202 else if (*format == '&') {
1203 typedef int (*converter)(PyObject *, void *);
1204 converter convert = va_arg(*p_va, converter);
1205 void *addr = va_arg(*p_va, void *);
1206 format++;
1207 if (! (*convert)(arg, addr))
1208 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001209 arg, msgbuf, bufsize);
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001210 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001211 else {
1212 p = va_arg(*p_va, PyObject **);
1213 *p = arg;
1214 }
1215 break;
1216 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001217
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001218
1219 case 'w': { /* memory buffer, read-write access */
1220 void **p = va_arg(*p_va, void **);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001221 void *res;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001222 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +00001223 Py_ssize_t count;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001224
1225 if (pb && pb->bf_releasebuffer && *format != '*')
1226 /* Buffer must be released, yet caller does not use
1227 the Py_buffer protocol. */
1228 return converterr("pinned buffer", arg, msgbuf, bufsize);
1229
1230 if (pb && pb->bf_getbuffer && *format == '*') {
1231 /* Caller is interested in Py_buffer, and the object
1232 supports it directly. */
1233 format++;
1234 if (pb->bf_getbuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1235 PyErr_Clear();
1236 return converterr("read-write buffer", arg, msgbuf, bufsize);
1237 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +00001238 if (addcleanup(p, freelist, cleanup_buffer)) {
1239 return converterr(
1240 "(cleanup problem)",
1241 arg, msgbuf, bufsize);
1242 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001243 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1244 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1245 break;
1246 }
1247
1248 if (pb == NULL ||
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001249 pb->bf_getwritebuffer == NULL ||
1250 pb->bf_getsegcount == NULL)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001251 return converterr("read-write buffer", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001252 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1253 return converterr("single-segment read-write buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001254 arg, msgbuf, bufsize);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001255 if ((count = pb->bf_getwritebuffer(arg, 0, &res)) < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001256 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001257 if (*format == '*') {
1258 PyBuffer_FillInfo((Py_buffer*)p, arg, res, count, 1, 0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001259 format++;
1260 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001261 else {
1262 *p = res;
1263 if (*format == '#') {
1264 FETCH_SIZE;
1265 STORE_SIZE(count);
1266 format++;
1267 }
1268 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001269 break;
1270 }
1271
1272 case 't': { /* 8-bit character buffer, read-only access */
Martin v. Löwis15e62742006-02-27 16:46:16 +00001273 char **p = va_arg(*p_va, char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001274 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +00001275 Py_ssize_t count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001276
1277 if (*format++ != '#')
1278 return converterr(
1279 "invalid use of 't' format character",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001280 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001281 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +00001282 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
1283 pb == NULL || pb->bf_getcharbuffer == NULL ||
1284 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001285 return converterr(
1286 "string or read-only character buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001287 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001288
Jeremy Hylton4819e972001-10-11 14:40:37 +00001289 if (pb->bf_getsegcount(arg, NULL) != 1)
1290 return converterr(
1291 "string or single-segment read-only buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001292 arg, msgbuf, bufsize);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001293
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001294 if (pb->bf_releasebuffer)
1295 return converterr(
1296 "string or pinned buffer",
1297 arg, msgbuf, bufsize);
1298
Jeremy Hylton4819e972001-10-11 14:40:37 +00001299 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001300 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001301 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001302 {
1303 FETCH_SIZE;
1304 STORE_SIZE(count);
1305 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001306 break;
1307 }
1308
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001309 default:
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001310 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001311
1312 }
1313
1314 *p_format = format;
1315 return NULL;
1316}
Guido van Rossumaa354651996-08-19 19:32:04 +00001317
Martin v. Löwis18e16552006-02-15 17:27:45 +00001318static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001319convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001320{
1321 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001322 Py_ssize_t count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001323 if (pb == NULL ||
1324 pb->bf_getreadbuffer == NULL ||
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001325 pb->bf_getsegcount == NULL ||
1326 pb->bf_releasebuffer != NULL) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001327 *errmsg = "string or read-only buffer";
1328 return -1;
1329 }
1330 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
1331 *errmsg = "string or single-segment read-only buffer";
1332 return -1;
1333 }
1334 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
1335 *errmsg = "(unspecified)";
1336 }
1337 return count;
1338}
Guido van Rossumaa354651996-08-19 19:32:04 +00001339
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001340static int
Neal Norwitz18aa3882008-08-24 05:04:52 +00001341getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001342{
1343 void *buf;
1344 Py_ssize_t count;
1345 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1346 if (pb == NULL) {
1347 *errmsg = "string or buffer";
1348 return -1;
1349 }
1350 if (pb->bf_getbuffer) {
Neal Norwitz18aa3882008-08-24 05:04:52 +00001351 if (pb->bf_getbuffer(arg, view, 0) < 0) {
1352 *errmsg = "convertible to a buffer";
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001353 return -1;
Neal Norwitz18aa3882008-08-24 05:04:52 +00001354 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001355 if (!PyBuffer_IsContiguous(view, 'C')) {
1356 *errmsg = "contiguous buffer";
1357 return -1;
1358 }
1359 return 0;
1360 }
1361
1362 count = convertbuffer(arg, &buf, errmsg);
Neal Norwitz18aa3882008-08-24 05:04:52 +00001363 if (count < 0) {
1364 *errmsg = "convertible to a buffer";
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001365 return count;
Neal Norwitz18aa3882008-08-24 05:04:52 +00001366 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001367 PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
1368 return 0;
1369}
1370
Guido van Rossumaa354651996-08-19 19:32:04 +00001371/* Support for keyword arguments donated by
1372 Geoff Philbrick <philbric@delphi.hks.com> */
1373
Tim Petersf8cd3e82001-10-27 04:26:57 +00001374/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001375int
1376PyArg_ParseTupleAndKeywords(PyObject *args,
1377 PyObject *keywords,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001378 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001379 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001380{
1381 int retval;
1382 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001383
1384 if ((args == NULL || !PyTuple_Check(args)) ||
1385 (keywords != NULL && !PyDict_Check(keywords)) ||
1386 format == NULL ||
1387 kwlist == NULL)
1388 {
1389 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001390 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001391 }
1392
Guido van Rossumaa354651996-08-19 19:32:04 +00001393 va_start(va, kwlist);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001394 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1395 va_end(va);
1396 return retval;
1397}
1398
1399int
1400_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1401 PyObject *keywords,
1402 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001403 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001404{
1405 int retval;
1406 va_list va;
1407
1408 if ((args == NULL || !PyTuple_Check(args)) ||
1409 (keywords != NULL && !PyDict_Check(keywords)) ||
1410 format == NULL ||
1411 kwlist == NULL)
1412 {
1413 PyErr_BadInternalCall();
1414 return 0;
1415 }
1416
1417 va_start(va, kwlist);
1418 retval = vgetargskeywords(args, keywords, format,
1419 kwlist, &va, FLAG_SIZE_T);
Guido van Rossumaa354651996-08-19 19:32:04 +00001420 va_end(va);
1421 return retval;
1422}
1423
1424
Brett Cannon711e7d92004-07-10 22:20:32 +00001425int
1426PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001427 PyObject *keywords,
1428 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001429 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001430{
1431 int retval;
1432 va_list lva;
1433
1434 if ((args == NULL || !PyTuple_Check(args)) ||
1435 (keywords != NULL && !PyDict_Check(keywords)) ||
1436 format == NULL ||
1437 kwlist == NULL)
1438 {
1439 PyErr_BadInternalCall();
1440 return 0;
1441 }
1442
1443#ifdef VA_LIST_IS_ARRAY
1444 memcpy(lva, va, sizeof(va_list));
1445#else
1446#ifdef __va_copy
1447 __va_copy(lva, va);
1448#else
1449 lva = va;
1450#endif
1451#endif
1452
Martin v. Löwis18e16552006-02-15 17:27:45 +00001453 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1454 return retval;
1455}
1456
1457int
1458_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1459 PyObject *keywords,
1460 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001461 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001462{
1463 int retval;
1464 va_list lva;
1465
1466 if ((args == NULL || !PyTuple_Check(args)) ||
1467 (keywords != NULL && !PyDict_Check(keywords)) ||
1468 format == NULL ||
1469 kwlist == NULL)
1470 {
1471 PyErr_BadInternalCall();
1472 return 0;
1473 }
1474
1475#ifdef VA_LIST_IS_ARRAY
1476 memcpy(lva, va, sizeof(va_list));
1477#else
1478#ifdef __va_copy
1479 __va_copy(lva, va);
1480#else
1481 lva = va;
1482#endif
1483#endif
1484
1485 retval = vgetargskeywords(args, keywords, format,
1486 kwlist, &lva, FLAG_SIZE_T);
Brett Cannon711e7d92004-07-10 22:20:32 +00001487 return retval;
1488}
1489
Christian Heimesea837932008-02-26 17:23:51 +00001490#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001491
Guido van Rossumaa354651996-08-19 19:32:04 +00001492static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001493vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001494 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001495{
Tim Petersdc5eff92001-10-27 06:53:00 +00001496 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001497 int levels[32];
Christian Heimesea837932008-02-26 17:23:51 +00001498 const char *fname, *msg, *custom_msg, *keyword;
1499 int min = INT_MAX;
Tim Petersb639d492001-10-27 07:00:56 +00001500 int i, len, nargs, nkeywords;
Christian Heimesea837932008-02-26 17:23:51 +00001501 PyObject *freelist = NULL, *current_arg;
Tim Petersf4331c12001-10-27 00:17:34 +00001502
Tim Peters45772cd2001-10-27 03:58:40 +00001503 assert(args != NULL && PyTuple_Check(args));
1504 assert(keywords == NULL || PyDict_Check(keywords));
1505 assert(format != NULL);
1506 assert(kwlist != NULL);
1507 assert(p_va != NULL);
1508
Christian Heimesea837932008-02-26 17:23:51 +00001509 /* grab the function name or custom error msg first (mutually exclusive) */
1510 fname = strchr(format, ':');
1511 if (fname) {
1512 fname++;
1513 custom_msg = NULL;
Tim Peters62d48e12001-10-27 06:42:16 +00001514 }
Christian Heimesea837932008-02-26 17:23:51 +00001515 else {
1516 custom_msg = strchr(format,';');
1517 if (custom_msg)
1518 custom_msg++;
Tim Peters62d48e12001-10-27 06:42:16 +00001519 }
Christian Heimesea837932008-02-26 17:23:51 +00001520
1521 /* scan kwlist and get greatest possible nbr of args */
1522 for (len=0; kwlist[len]; len++)
1523 continue;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001524
Tim Peters6fb26352001-10-27 04:38:11 +00001525 nargs = PyTuple_GET_SIZE(args);
Christian Heimesea837932008-02-26 17:23:51 +00001526 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1527 if (nargs + nkeywords > len) {
1528 PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
1529 "argument%s (%d given)",
1530 (fname == NULL) ? "function" : fname,
1531 (fname == NULL) ? "" : "()",
1532 len,
1533 (len == 1) ? "" : "s",
1534 nargs + nkeywords);
Guido van Rossumaa354651996-08-19 19:32:04 +00001535 return 0;
1536 }
Tim Petersc2f01122001-10-27 07:25:06 +00001537
Christian Heimesea837932008-02-26 17:23:51 +00001538 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1539 for (i = 0; i < len; i++) {
1540 keyword = kwlist[i];
1541 if (*format == '|') {
1542 min = i;
Guido van Rossumaa354651996-08-19 19:32:04 +00001543 format++;
Christian Heimesea837932008-02-26 17:23:51 +00001544 }
1545 if (IS_END_OF_FORMAT(*format)) {
1546 PyErr_Format(PyExc_RuntimeError,
1547 "More keyword list entries (%d) than "
1548 "format specifiers (%d)", len, i);
1549 return cleanreturn(0, freelist);
1550 }
1551 current_arg = NULL;
1552 if (nkeywords) {
1553 current_arg = PyDict_GetItemString(keywords, keyword);
1554 }
1555 if (current_arg) {
1556 --nkeywords;
1557 if (i < nargs) {
1558 /* arg present in tuple and in dict */
1559 PyErr_Format(PyExc_TypeError,
1560 "Argument given by name ('%s') "
1561 "and position (%d)",
1562 keyword, i+1);
1563 return cleanreturn(0, freelist);
1564 }
1565 }
1566 else if (nkeywords && PyErr_Occurred())
1567 return cleanreturn(0, freelist);
1568 else if (i < nargs)
1569 current_arg = PyTuple_GET_ITEM(args, i);
1570
1571 if (current_arg) {
1572 msg = convertitem(current_arg, &format, p_va, flags,
1573 levels, msgbuf, sizeof(msgbuf), &freelist);
1574 if (msg) {
1575 seterror(i+1, msg, levels, fname, custom_msg);
1576 return cleanreturn(0, freelist);
1577 }
1578 continue;
1579 }
1580
1581 if (i < min) {
1582 PyErr_Format(PyExc_TypeError, "Required argument "
1583 "'%s' (pos %d) not found",
1584 keyword, i+1);
1585 return cleanreturn(0, freelist);
1586 }
1587 /* current code reports success when all required args
1588 * fulfilled and no keyword args left, with no further
1589 * validation. XXX Maybe skip this in debug build ?
1590 */
1591 if (!nkeywords)
1592 return cleanreturn(1, freelist);
1593
1594 /* We are into optional args, skip thru to any remaining
1595 * keyword args */
1596 msg = skipitem(&format, p_va, flags);
Guido van Rossumaa354651996-08-19 19:32:04 +00001597 if (msg) {
Christian Heimesea837932008-02-26 17:23:51 +00001598 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1599 format);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001600 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001601 }
1602 }
1603
Benjamin Peterson4caef5c2008-12-22 20:51:15 +00001604 if (!IS_END_OF_FORMAT(*format) && *format != '|') {
Christian Heimesea837932008-02-26 17:23:51 +00001605 PyErr_Format(PyExc_RuntimeError,
1606 "more argument specifiers than keyword list entries "
1607 "(remaining format:'%s')", format);
1608 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001609 }
Tim Petersb054be42001-10-27 05:07:41 +00001610
Guido van Rossumaa354651996-08-19 19:32:04 +00001611 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001612 if (nkeywords > 0) {
1613 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001614 Py_ssize_t pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001615 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001616 int match = 0;
Guido van Rossum55474762002-04-04 16:22:30 +00001617 char *ks;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001618 if (!PyString_Check(key)) {
Guido van Rossum55474762002-04-04 16:22:30 +00001619 PyErr_SetString(PyExc_TypeError,
1620 "keywords must be strings");
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001621 return cleanreturn(0, freelist);
Guido van Rossum55474762002-04-04 16:22:30 +00001622 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001623 ks = PyString_AsString(key);
Christian Heimesea837932008-02-26 17:23:51 +00001624 for (i = 0; i < len; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001625 if (!strcmp(ks, kwlist[i])) {
1626 match = 1;
1627 break;
1628 }
1629 }
1630 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001631 PyErr_Format(PyExc_TypeError,
1632 "'%s' is an invalid keyword "
1633 "argument for this function",
1634 ks);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001635 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001636 }
1637 }
1638 }
Tim Petersc2f01122001-10-27 07:25:06 +00001639
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001640 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001641}
1642
1643
1644static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001645skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001646{
Christian Heimesea837932008-02-26 17:23:51 +00001647 const char *format = *p_format;
Guido van Rossumaa354651996-08-19 19:32:04 +00001648 char c = *format++;
1649
1650 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001651
1652 /* simple codes
1653 * The individual types (second arg of va_arg) are irrelevant */
1654
Guido van Rossumaa354651996-08-19 19:32:04 +00001655 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001656 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001657 case 'h': /* short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001658 case 'H': /* short int as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001659 case 'i': /* int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001660 case 'I': /* int sized bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001661 case 'l': /* long int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001662 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001663#ifdef HAVE_LONG_LONG
Georg Brandl6dd14612005-09-14 19:29:53 +00001664 case 'L': /* PY_LONG_LONG */
1665 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001666#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001667 case 'f': /* float */
Guido van Rossumaa354651996-08-19 19:32:04 +00001668 case 'd': /* double */
Guido van Rossumaa354651996-08-19 19:32:04 +00001669#ifndef WITHOUT_COMPLEX
1670 case 'D': /* complex double */
Georg Brandl6dd14612005-09-14 19:29:53 +00001671#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001672 case 'c': /* char */
1673 {
Georg Brandl6dd14612005-09-14 19:29:53 +00001674 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001675 break;
1676 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001677
1678 case 'n': /* Py_ssize_t */
1679 {
1680 (void) va_arg(*p_va, Py_ssize_t *);
1681 break;
1682 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001683
Georg Brandl6dd14612005-09-14 19:29:53 +00001684 /* string codes */
1685
1686 case 'e': /* string with encoding */
1687 {
1688 (void) va_arg(*p_va, const char *);
1689 if (!(*format == 's' || *format == 't'))
1690 /* after 'e', only 's' and 't' is allowed */
1691 goto err;
1692 format++;
1693 /* explicit fallthrough to string cases */
1694 }
1695
Guido van Rossumaa354651996-08-19 19:32:04 +00001696 case 's': /* string */
Georg Brandl6dd14612005-09-14 19:29:53 +00001697 case 'z': /* string or None */
1698#ifdef Py_USING_UNICODE
1699 case 'u': /* unicode string */
1700#endif
1701 case 't': /* buffer, read-only */
1702 case 'w': /* buffer, read-write */
Guido van Rossumaa354651996-08-19 19:32:04 +00001703 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001704 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001705 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001706 if (flags & FLAG_SIZE_T)
1707 (void) va_arg(*p_va, Py_ssize_t *);
1708 else
1709 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001710 format++;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001711 } else if ((c == 's' || c == 'z') && *format == '*') {
1712 format++;
Guido van Rossumaa354651996-08-19 19:32:04 +00001713 }
1714 break;
1715 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001716
1717 /* object codes */
1718
Guido van Rossumaa354651996-08-19 19:32:04 +00001719 case 'S': /* string object */
Georg Brandl6dd14612005-09-14 19:29:53 +00001720#ifdef Py_USING_UNICODE
1721 case 'U': /* unicode string object */
1722#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001723 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001724 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001725 break;
1726 }
1727
1728 case 'O': /* object */
1729 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001730 if (*format == '!') {
1731 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001732 (void) va_arg(*p_va, PyTypeObject*);
1733 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001734 }
1735#if 0
1736/* I don't know what this is for */
1737 else if (*format == '?') {
1738 inquiry pred = va_arg(*p_va, inquiry);
1739 format++;
1740 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001741 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001742 }
1743 }
1744#endif
1745 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001746 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001747 (void) va_arg(*p_va, converter);
1748 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001749 format++;
1750 }
1751 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001752 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001753 }
1754 break;
1755 }
Christian Heimesea837932008-02-26 17:23:51 +00001756
1757 case '(': /* bypass tuple, not handled at all previously */
1758 {
1759 char *msg;
1760 for (;;) {
1761 if (*format==')')
1762 break;
1763 if (IS_END_OF_FORMAT(*format))
1764 return "Unmatched left paren in format "
1765 "string";
1766 msg = skipitem(&format, p_va, flags);
1767 if (msg)
1768 return msg;
1769 }
1770 format++;
1771 break;
1772 }
1773
1774 case ')':
1775 return "Unmatched right paren in format string";
1776
Guido van Rossumaa354651996-08-19 19:32:04 +00001777 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001778err:
Guido van Rossumaa354651996-08-19 19:32:04 +00001779 return "impossible<bad format char>";
1780
1781 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001782
Guido van Rossumaa354651996-08-19 19:32:04 +00001783 *p_format = format;
1784 return NULL;
1785}
Fred Drakee4616e62001-10-23 21:09:29 +00001786
1787
1788int
Martin v. Löwis76246742006-03-01 04:06:10 +00001789PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001790{
Martin v. Löwis76246742006-03-01 04:06:10 +00001791 Py_ssize_t i, l;
Fred Drakee4616e62001-10-23 21:09:29 +00001792 PyObject **o;
1793 va_list vargs;
1794
1795#ifdef HAVE_STDARG_PROTOTYPES
1796 va_start(vargs, max);
1797#else
1798 va_start(vargs);
1799#endif
1800
1801 assert(min >= 0);
1802 assert(min <= max);
1803 if (!PyTuple_Check(args)) {
1804 PyErr_SetString(PyExc_SystemError,
1805 "PyArg_UnpackTuple() argument list is not a tuple");
1806 return 0;
1807 }
1808 l = PyTuple_GET_SIZE(args);
1809 if (l < min) {
1810 if (name != NULL)
1811 PyErr_Format(
1812 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001813 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001814 name, (min == max ? "" : "at least "), min, l);
1815 else
1816 PyErr_Format(
1817 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001818 "unpacked tuple should have %s%zd elements,"
1819 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001820 (min == max ? "" : "at least "), min, l);
1821 va_end(vargs);
1822 return 0;
1823 }
1824 if (l > max) {
1825 if (name != NULL)
1826 PyErr_Format(
1827 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001828 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001829 name, (min == max ? "" : "at most "), max, l);
1830 else
1831 PyErr_Format(
1832 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001833 "unpacked tuple should have %s%zd elements,"
1834 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001835 (min == max ? "" : "at most "), max, l);
1836 va_end(vargs);
1837 return 0;
1838 }
1839 for (i = 0; i < l; i++) {
1840 o = va_arg(vargs, PyObject **);
1841 *o = PyTuple_GET_ITEM(args, i);
1842 }
1843 va_end(vargs);
1844 return 1;
1845}
Georg Brandl02c42872005-08-26 06:42:30 +00001846
1847
1848/* For type constructors that don't take keyword args
1849 *
1850 * Sets a TypeError and returns 0 if the kwds dict is
Walter Dörwaldd14bf612006-09-21 15:09:55 +00001851 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001852 */
1853int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001854_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001855{
1856 if (kw == NULL)
1857 return 1;
1858 if (!PyDict_CheckExact(kw)) {
1859 PyErr_BadInternalCall();
1860 return 0;
1861 }
1862 if (PyDict_Size(kw) == 0)
1863 return 1;
1864
1865 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1866 funcname);
1867 return 0;
1868}
Anthony Baxter97300382006-04-12 04:38:54 +00001869#ifdef __cplusplus
1870};
1871#endif