blob: 13c3f4b9a6ef4dfe9241e15a4a0dd11ba92fd1eb [file] [log] [blame]
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001
2/* New getargs implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00005
Guido van Rossumc1d50531996-08-21 23:38:24 +00006#include <ctype.h>
7
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00008
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009#ifdef __cplusplus
Guido van Rossum98297ee2007-11-06 21:34:58 +000010extern "C" {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011#endif
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000012int PyArg_Parse(PyObject *, const char *, ...);
13int PyArg_ParseTuple(PyObject *, const char *, ...);
14int PyArg_VaParse(PyObject *, const char *, va_list);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000015
Tim Petersdbd9ba62000-07-09 03:09:57 +000016int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
Martin v. Löwis15e62742006-02-27 16:46:16 +000017 const char *, char **, ...);
Brett Cannon711e7d92004-07-10 22:20:32 +000018int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
Martin v. Löwis15e62742006-02-27 16:46:16 +000019 const char *, char **, va_list);
Brett Cannon711e7d92004-07-10 22:20:32 +000020
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000021#ifdef HAVE_DECLSPEC_DLL
22/* Export functions */
23PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, char *, ...);
24PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, char *, ...);
25PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
26 const char *, char **, ...);
27PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
28PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, char *, va_list);
29PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
30 const char *, char **, va_list);
31#endif
32
Martin v. Löwis18e16552006-02-15 17:27:45 +000033#define FLAG_COMPAT 1
34#define FLAG_SIZE_T 2
35
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000036
37/* Forward */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000038static int vgetargs1(PyObject *, const char *, va_list *, int);
39static void seterror(int, const char *, int *, const char *, const char *);
Guido van Rossum98297ee2007-11-06 21:34:58 +000040static char *convertitem(PyObject *, const char **, va_list *, int, int *,
Martin v. Löwis18e16552006-02-15 17:27:45 +000041 char *, size_t, PyObject **);
42static char *converttuple(PyObject *, const char **, va_list *, int,
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öwis423be952008-08-13 15:53:07 +000047static int getbuffer(PyObject *, Py_buffer *, char**);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000048
Tim Petersdbd9ba62000-07-09 03:09:57 +000049static int vgetargskeywords(PyObject *, PyObject *,
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 Rossum98297ee2007-11-06 21:34:58 +000058
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000059 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;
Guido van Rossum98297ee2007-11-06 21:34:58 +000070
Martin v. Löwis18e16552006-02-15 17:27:45 +000071 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 Rossum98297ee2007-11-06 21:34:58 +000083
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000084 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;
Guido van Rossum98297ee2007-11-06 21:34:58 +000095
Martin v. Löwis18e16552006-02-15 17:27:45 +000096 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
142static int
143addcleanup(void *ptr, PyObject **freelist)
144{
145 PyObject *cobj;
146 if (!*freelist) {
147 *freelist = PyList_New(0);
148 if (!*freelist) {
149 PyMem_FREE(ptr);
150 return -1;
151 }
152 }
153 cobj = PyCObject_FromVoidPtr(ptr, NULL);
154 if (!cobj) {
155 PyMem_FREE(ptr);
156 return -1;
157 }
Christian Heimes836baa52008-02-26 08:18:30 +0000158 if (PyList_Append(*freelist, cobj)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000159 PyMem_FREE(ptr);
160 Py_DECREF(cobj);
161 return -1;
162 }
163 Py_DECREF(cobj);
164 return 0;
165}
166
167static int
168cleanreturn(int retval, PyObject *freelist)
169{
Christian Heimes836baa52008-02-26 08:18:30 +0000170 if (freelist) {
171 if (retval == 0) {
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000172 Py_ssize_t len = PyList_GET_SIZE(freelist), i;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000173 for (i = 0; i < len; i++)
174 PyMem_FREE(PyCObject_AsVoidPtr(
175 PyList_GET_ITEM(freelist, i)));
176 }
177 Py_DECREF(freelist);
178 }
179 return retval;
180}
181
182
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000183static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000184vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000185{
186 char msgbuf[256];
187 int levels[32];
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000188 const char *fname = NULL;
189 const char *message = NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000190 int min = -1;
191 int max = 0;
192 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000193 int endfmt = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000194 const char *formatsave = format;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000195 Py_ssize_t i, len;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000196 char *msg;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000197 PyObject *freelist = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000198 int compat = flags & FLAG_COMPAT;
199
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000200 assert(compat || (args != (PyObject*)NULL));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000201 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000202
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000203 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000204 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000205 switch (c) {
206 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000207 if (level == 0)
208 max++;
209 level++;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000210 if (level >= 30)
211 Py_FatalError("too many tuple nesting levels "
212 "in argument format string");
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000213 break;
214 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000215 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000216 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000217 else
218 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000219 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000220 case '\0':
221 endfmt = 1;
222 break;
223 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000224 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000225 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000226 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000227 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000228 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000229 endfmt = 1;
230 break;
231 default:
232 if (level == 0) {
233 if (c == 'O')
234 max++;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000235 else if (isalpha(Py_CHARMASK(c))) {
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000236 if (c != 'e') /* skip encoded */
237 max++;
238 } else if (c == '|')
239 min = max;
240 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000241 break;
242 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000243 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000244
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000245 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000247
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000248 if (min < 0)
249 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000250
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000251 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000252
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000253 if (compat) {
254 if (max == 0) {
255 if (args == NULL)
256 return 1;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000257 PyOS_snprintf(msgbuf, sizeof(msgbuf),
258 "%.200s%s takes no arguments",
259 fname==NULL ? "function" : fname,
260 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000261 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000262 return 0;
263 }
264 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000265 if (args == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000266 PyOS_snprintf(msgbuf, sizeof(msgbuf),
267 "%.200s%s takes at least one argument",
268 fname==NULL ? "function" : fname,
269 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000270 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000271 return 0;
272 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000273 msg = convertitem(args, &format, p_va, flags, levels,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000274 msgbuf, sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000275 if (msg == NULL)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000276 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000277 seterror(levels[0], msg, levels+1, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000278 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000279 }
280 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000282 "old style getargs format uses new features");
283 return 0;
284 }
285 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000286
Guido van Rossum79f25d91997-04-29 20:08:16 +0000287 if (!PyTuple_Check(args)) {
288 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000289 "new style getargs format but argument is not a tuple");
290 return 0;
291 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000292
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000293 len = PyTuple_GET_SIZE(args);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000294
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000295 if (len < min || max < len) {
296 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000297 PyOS_snprintf(msgbuf, sizeof(msgbuf),
298 "%.150s%s takes %s %d argument%s "
Neal Norwitz20dd93f2006-02-19 19:34:15 +0000299 "(%ld given)",
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000300 fname==NULL ? "function" : fname,
301 fname==NULL ? "" : "()",
302 min==max ? "exactly"
303 : len < min ? "at least" : "at most",
304 len < min ? min : max,
305 (len < min ? min : max) == 1 ? "" : "s",
Neal Norwitz9a276172006-02-20 18:57:39 +0000306 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000307 message = msgbuf;
308 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000309 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000310 return 0;
311 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000312
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000313 for (i = 0; i < len; i++) {
314 if (*format == '|')
315 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000316 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000317 flags, levels, msgbuf,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000318 sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000319 if (msg) {
320 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000321 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000322 }
323 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000324
Neal Norwitz4ac13df2005-12-19 06:10:07 +0000325 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000326 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000327 *format != '|' && *format != ':' && *format != ';') {
328 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000329 "bad format string: %.200s", formatsave);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000330 return cleanreturn(0, freelist);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000331 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000332
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000333 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000334}
335
336
337
338static void
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000339seterror(int iarg, const char *msg, int *levels, const char *fname,
340 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000341{
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000342 char buf[512];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000343 int i;
344 char *p = buf;
345
Guido van Rossum79f25d91997-04-29 20:08:16 +0000346 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000347 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000348 else if (message == NULL) {
349 if (fname != NULL) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000350 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000351 p += strlen(p);
352 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000353 if (iarg != 0) {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000354 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000355 "argument %d", iarg);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000356 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000357 p += strlen(p);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000358 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
359 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000360 ", item %d", levels[i]-1);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000361 p += strlen(p);
362 i++;
363 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000364 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000365 else {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000366 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
Ka-Ping Yee20579702001-01-15 22:14:16 +0000367 p += strlen(p);
368 }
Tim Petersfaad5ad2001-12-03 00:43:33 +0000369 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000370 message = buf;
371 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000372 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000373}
374
375
376/* Convert a tuple argument.
377 On entry, *p_format points to the character _after_ the opening '('.
378 On successful exit, *p_format points to the closing ')'.
379 If successful:
380 *p_format and *p_va are updated,
381 *levels and *msgbuf are untouched,
382 and NULL is returned.
383 If the argument is invalid:
384 *p_format is unchanged,
385 *p_va is undefined,
386 *levels is a 0-terminated list of item numbers,
387 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000388 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000389 <typename1> is the name of the expected type, and
390 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000391 and msgbuf is returned.
392*/
393
394static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000395converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000396 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000397 PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000398{
399 int level = 0;
400 int n = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000401 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000402 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000403
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000404 for (;;) {
405 int c = *format++;
406 if (c == '(') {
407 if (level == 0)
408 n++;
409 level++;
410 }
411 else if (c == ')') {
412 if (level == 0)
413 break;
414 level--;
415 }
416 else if (c == ':' || c == ';' || c == '\0')
417 break;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000418 else if (level == 0 && isalpha(Py_CHARMASK(c)))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000419 n++;
420 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000421
Christian Heimes72b710a2008-05-26 13:28:38 +0000422 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000423 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000424 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000425 toplevel ? "expected %d arguments, not %.50s" :
426 "must be %d-item sequence, not %.50s",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000427 n,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000428 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000429 return msgbuf;
430 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000431
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000432 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000433 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000434 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000435 toplevel ? "expected %d arguments, not %d" :
436 "must be sequence of length %d, not %d",
437 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000438 return msgbuf;
439 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000440
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000441 format = *p_format;
442 for (i = 0; i < n; i++) {
443 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000444 PyObject *item;
445 item = PySequence_GetItem(arg, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000446 if (item == NULL) {
447 PyErr_Clear();
448 levels[0] = i+1;
449 levels[1] = 0;
450 strncpy(msgbuf, "is not retrievable", bufsize);
451 return msgbuf;
452 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000453 msg = convertitem(item, &format, p_va, flags, levels+1,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000454 msgbuf, bufsize, freelist);
Guido van Rossum66368cc1999-02-17 23:16:43 +0000455 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
456 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000457 if (msg != NULL) {
458 levels[0] = i+1;
459 return msg;
460 }
461 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000462
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000463 *p_format = format;
464 return NULL;
465}
466
467
468/* Convert a single item. */
469
470static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000471convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
472 int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000473{
474 char *msg;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000475 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000476
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000477 if (*format == '(' /* ')' */) {
478 format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000479 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000480 bufsize, 0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000481 if (msg == NULL)
482 format++;
483 }
484 else {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000485 msg = convertsimple(arg, &format, p_va, flags,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000486 msgbuf, bufsize, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000487 if (msg != NULL)
488 levels[0] = 0;
489 }
490 if (msg == NULL)
491 *p_format = format;
492 return msg;
493}
494
495
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000496
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000497#define UNICODE_DEFAULT_ENCODING(arg) \
498 _PyUnicode_AsDefaultEncodedString(arg, NULL)
499
500/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000501
502static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000503converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000504{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000505 assert(expected != NULL);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000506 assert(arg != NULL);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000507 PyOS_snprintf(msgbuf, bufsize,
508 "must be %.50s, not %.50s", expected,
509 arg == Py_None ? "None" : arg->ob_type->tp_name);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000510 return msgbuf;
511}
512
513#define CONV_UNICODE "(unicode conversion error)"
514
Guido van Rossum45aecf42006-03-15 04:58:47 +0000515/* Explicitly check for float arguments when integers are expected.
516 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000517static int
518float_argument_error(PyObject *arg)
519{
Guido van Rossum45aecf42006-03-15 04:58:47 +0000520 if (PyFloat_Check(arg)) {
521 PyErr_SetString(PyExc_TypeError,
522 "integer argument expected, got float" );
Neil Schemenauer5042da62003-02-04 20:59:40 +0000523 return 1;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000524 }
Neil Schemenauer5042da62003-02-04 20:59:40 +0000525 else
526 return 0;
527}
528
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000529/* Convert a non-tuple argument. Return NULL if conversion went OK,
530 or a string with a message describing the failure. The message is
531 formatted as "must be <desired type>, not <actual type>".
532 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000533 Don't call if a tuple is expected.
534
535 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000536*/
537
538static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000539convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000540 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000541{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000542 /* For # codes */
543#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
544 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
545 else q=va_arg(*p_va, int*);
546#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
547#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
548
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000549 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000550 char c = *format++;
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000551 PyObject *uarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000552
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000553 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000554
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000555 case 'b': { /* unsigned byte -- very short int */
556 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000557 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000558 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000559 return converterr("integer<b>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000560 ival = PyLong_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000561 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000562 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000563 else if (ival < 0) {
564 PyErr_SetString(PyExc_OverflowError,
565 "unsigned byte integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000566 return converterr("integer<b>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000567 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000568 else if (ival > UCHAR_MAX) {
569 PyErr_SetString(PyExc_OverflowError,
570 "unsigned byte integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000571 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000572 }
573 else
574 *p = (unsigned char) ival;
575 break;
576 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000577
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000578 case 'B': {/* byte sized bitfield - both signed and unsigned
Guido van Rossum98297ee2007-11-06 21:34:58 +0000579 values allowed */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000580 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000581 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000582 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000583 return converterr("integer<B>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000584 ival = PyLong_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000585 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000586 return converterr("integer<B>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000587 else
588 *p = (unsigned char) ival;
589 break;
590 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000591
Guido van Rossumfce26e72003-04-18 00:12:30 +0000592 case 'h': {/* signed short int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000593 short *p = va_arg(*p_va, short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000594 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000595 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000596 return converterr("integer<h>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000597 ival = PyLong_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000598 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000599 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfce26e72003-04-18 00:12:30 +0000600 else if (ival < SHRT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000601 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000602 "signed short integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000603 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000604 }
Guido van Rossumfce26e72003-04-18 00:12:30 +0000605 else if (ival > SHRT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000606 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000607 "signed short integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000608 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000609 }
610 else
611 *p = (short) ival;
612 break;
613 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000614
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000615 case 'H': { /* short int sized bitfield, both signed and
Guido van Rossum98297ee2007-11-06 21:34:58 +0000616 unsigned allowed */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000617 unsigned short *p = va_arg(*p_va, unsigned short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000618 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000619 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000620 return converterr("integer<H>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000621 ival = PyLong_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000622 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000623 return converterr("integer<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000624 else
625 *p = (unsigned short) ival;
626 break;
627 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000628
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000629 case 'i': {/* signed int */
630 int *p = va_arg(*p_va, int *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000631 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000632 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000633 return converterr("integer<i>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000634 ival = PyLong_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000635 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000636 return converterr("integer<i>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000637 else if (ival > INT_MAX) {
638 PyErr_SetString(PyExc_OverflowError,
639 "signed integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000640 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000641 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000642 else if (ival < INT_MIN) {
643 PyErr_SetString(PyExc_OverflowError,
644 "signed integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000645 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000646 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000647 else
648 *p = ival;
649 break;
650 }
651
Thomas Hellera4ea6032003-04-17 18:55:45 +0000652 case 'I': { /* int sized bitfield, both signed and
Guido van Rossum98297ee2007-11-06 21:34:58 +0000653 unsigned allowed */
Thomas Hellera4ea6032003-04-17 18:55:45 +0000654 unsigned int *p = va_arg(*p_va, unsigned int *);
655 unsigned int ival;
656 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000657 return converterr("integer<I>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000658 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000659 if (ival == (unsigned int)-1 && PyErr_Occurred())
Thomas Hellera4ea6032003-04-17 18:55:45 +0000660 return converterr("integer<I>", arg, msgbuf, bufsize);
661 else
662 *p = ival;
663 break;
664 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000665
Martin v. Löwis18e16552006-02-15 17:27:45 +0000666 case 'n': /* Py_ssize_t */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000667 {
Neal Norwitzb879f572007-08-31 05:20:36 +0000668 PyObject *iobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000669 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
Neal Norwitzb879f572007-08-31 05:20:36 +0000670 Py_ssize_t ival = -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000671 if (float_argument_error(arg))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672 return converterr("integer<n>", arg, msgbuf, bufsize);
Neal Norwitzb879f572007-08-31 05:20:36 +0000673 iobj = PyNumber_Index(arg);
Christian Heimesbcd2c082008-05-08 01:20:25 +0000674 if (iobj != NULL) {
Trent Nelson35133582008-04-22 19:02:40 +0000675 ival = PyLong_AsSsize_t(iobj);
Christian Heimesbcd2c082008-05-08 01:20:25 +0000676 Py_DECREF(iobj);
677 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000678 if (ival == -1 && PyErr_Occurred())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000679 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000680 *p = ival;
681 break;
682 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000683 case 'l': {/* long int */
684 long *p = va_arg(*p_va, long *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000685 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000686 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000687 return converterr("integer<l>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000688 ival = PyLong_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000689 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000690 return converterr("integer<l>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000691 else
692 *p = ival;
693 break;
694 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000695
696 case 'k': { /* long sized bitfield */
697 unsigned long *p = va_arg(*p_va, unsigned long *);
698 unsigned long ival;
Georg Brandle1a0d112007-10-23 19:24:22 +0000699 if (PyLong_Check(arg))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000700 ival = PyLong_AsUnsignedLongMask(arg);
701 else
702 return converterr("integer<k>", arg, msgbuf, bufsize);
703 *p = ival;
704 break;
705 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000706
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000707#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000708 case 'L': {/* PY_LONG_LONG */
709 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
710 PY_LONG_LONG ival = PyLong_AsLongLong( arg );
Christian Heimes836baa52008-02-26 08:18:30 +0000711 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000712 return converterr("long<L>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000713 } else {
714 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000715 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000716 break;
717 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000718
719 case 'K': { /* long long sized bitfield */
720 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
721 unsigned PY_LONG_LONG ival;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000722 if (PyLong_Check(arg))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000723 ival = PyLong_AsUnsignedLongLongMask(arg);
724 else
725 return converterr("integer<K>", arg, msgbuf, bufsize);
726 *p = ival;
727 break;
728 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000729#endif
Guido van Rossum98297ee2007-11-06 21:34:58 +0000730
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000731 case 'f': {/* float */
732 float *p = va_arg(*p_va, float *);
733 double dval = PyFloat_AsDouble(arg);
734 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000735 return converterr("float<f>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000736 else
737 *p = (float) dval;
738 break;
739 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000740
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000741 case 'd': {/* double */
742 double *p = va_arg(*p_va, double *);
743 double dval = PyFloat_AsDouble(arg);
744 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000745 return converterr("float<d>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000746 else
747 *p = dval;
748 break;
749 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000750
Guido van Rossum530956d1996-07-21 02:27:43 +0000751#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000752 case 'D': {/* complex double */
753 Py_complex *p = va_arg(*p_va, Py_complex *);
754 Py_complex cval;
755 cval = PyComplex_AsCComplex(arg);
756 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000757 return converterr("complex<D>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000758 else
759 *p = cval;
760 break;
761 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000762#endif /* WITHOUT_COMPLEX */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000763
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000764 case 'c': {/* char */
Walter Dörwaldd0941302007-07-01 21:58:22 +0000765 char *p = va_arg(*p_va, char *);
Christian Heimes72b710a2008-05-26 13:28:38 +0000766 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
767 *p = PyBytes_AS_STRING(arg)[0];
Walter Dörwaldd0941302007-07-01 21:58:22 +0000768 else if (PyUnicode_Check(arg) &&
769 PyUnicode_GET_SIZE(arg) == 1 &&
770 PyUnicode_AS_UNICODE(arg)[0] < 256)
Amaury Forgeot d'Arc39599dc2007-11-22 02:48:12 +0000771 *p = (char)PyUnicode_AS_UNICODE(arg)[0];
Walter Dörwaldd0941302007-07-01 21:58:22 +0000772 else
773 return converterr("char < 256", arg, msgbuf, bufsize);
774 break;
775 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000776
Walter Dörwaldd0941302007-07-01 21:58:22 +0000777 case 'C': {/* unicode char */
Walter Dörwaldbc1f8862007-06-20 11:02:38 +0000778 int *p = va_arg(*p_va, int *);
Christian Heimes72b710a2008-05-26 13:28:38 +0000779 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
780 *p = PyBytes_AS_STRING(arg)[0];
Guido van Rossum09dc34f2007-05-04 04:17:33 +0000781 else if (PyUnicode_Check(arg) &&
Walter Dörwaldbc1f8862007-06-20 11:02:38 +0000782 PyUnicode_GET_SIZE(arg) == 1)
Guido van Rossum09dc34f2007-05-04 04:17:33 +0000783 *p = PyUnicode_AS_UNICODE(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000784 else
Walter Dörwaldbc1f8862007-06-20 11:02:38 +0000785 return converterr("char", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000786 break;
787 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000788
789 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w', 't' codes all
790 need to be cleaned up! */
791
792 case 's': {/* text string */
Martin v. Löwis423be952008-08-13 15:53:07 +0000793 if (*format == '*') {
794 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
795
796 if (PyUnicode_Check(arg)) {
797 uarg = UNICODE_DEFAULT_ENCODING(arg);
798 if (uarg == NULL)
799 return converterr(CONV_UNICODE,
800 arg, msgbuf, bufsize);
801 PyBuffer_FillInfo(p, arg,
802 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
803 1, 0);
804 }
805 else { /* any buffer-like object */
806 char *buf;
807 if (getbuffer(arg, p, &buf) < 0)
808 return converterr(buf, arg, msgbuf, bufsize);
809 }
810 format++;
811 } else if (*format == '#') {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000812 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000813 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000814
815 if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000816 uarg = UNICODE_DEFAULT_ENCODING(arg);
817 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000818 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000819 arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +0000820 *p = PyBytes_AS_STRING(uarg);
821 STORE_SIZE(PyBytes_GET_SIZE(uarg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000822 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000823 else { /* any buffer-like object */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000824 /* XXX Really? */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000825 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000826 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000827 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000828 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000829 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000830 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000831 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000832 } else {
833 char **p = va_arg(*p_va, char **);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000834
835 if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000836 uarg = UNICODE_DEFAULT_ENCODING(arg);
837 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000838 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000839 arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +0000840 *p = PyBytes_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000841 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000842 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000843 return converterr("string", arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +0000844 if ((Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000845 return converterr("string without null bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000846 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000847 }
848 break;
849 }
850
Guido van Rossum98297ee2007-11-06 21:34:58 +0000851 case 'y': {/* any buffer-like object, but not PyUnicode */
852 void **p = (void **)va_arg(*p_va, char **);
853 char *buf;
Martin v. Löwis423be952008-08-13 15:53:07 +0000854 Py_ssize_t count;
855 if (*format == '*') {
856 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
857 return converterr(buf, arg, msgbuf, bufsize);
858 format++;
859 break;
860 }
861 count = convertbuffer(arg, p, &buf);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000862 if (count < 0)
863 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis423be952008-08-13 15:53:07 +0000864 else if (*format == '#') {
Walter Dörwald612344f2007-05-04 19:28:21 +0000865 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000866 STORE_SIZE(count);
Walter Dörwald612344f2007-05-04 19:28:21 +0000867 format++;
Walter Dörwald612344f2007-05-04 19:28:21 +0000868 }
869 break;
870 }
871
Guido van Rossum98297ee2007-11-06 21:34:58 +0000872 case 'z': {/* like 's' or 's#', but None is okay, stored as NULL */
Martin v. Löwis423be952008-08-13 15:53:07 +0000873 if (*format == '*') {
874 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
875
876 if (arg == Py_None)
877 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
878 else if (PyUnicode_Check(arg)) {
879 uarg = UNICODE_DEFAULT_ENCODING(arg);
880 if (uarg == NULL)
881 return converterr(CONV_UNICODE,
882 arg, msgbuf, bufsize);
883 PyBuffer_FillInfo(p, arg,
884 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
885 1, 0);
886 }
887 else { /* any buffer-like object */
888 char *buf;
889 if (getbuffer(arg, p, &buf) < 0)
890 return converterr(buf, arg, msgbuf, bufsize);
891 }
892 format++;
893 } else if (*format == '#') { /* any buffer-like object */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000894 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000895 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000896
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000897 if (arg == Py_None) {
898 *p = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000899 STORE_SIZE(0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000900 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000901 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000902 uarg = UNICODE_DEFAULT_ENCODING(arg);
903 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000904 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000905 arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +0000906 *p = PyBytes_AS_STRING(uarg);
907 STORE_SIZE(PyBytes_GET_SIZE(uarg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000908 }
909 else { /* any buffer-like object */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000910 /* XXX Really? */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000911 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000912 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000913 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000914 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000915 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000916 }
917 format++;
918 } else {
919 char **p = va_arg(*p_va, char **);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +0000920 uarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000921
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000922 if (arg == Py_None)
923 *p = 0;
Christian Heimes72b710a2008-05-26 13:28:38 +0000924 else if (PyBytes_Check(arg)) {
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +0000925 /* Enable null byte check below */
926 uarg = arg;
Christian Heimes72b710a2008-05-26 13:28:38 +0000927 *p = PyBytes_AS_STRING(arg);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +0000928 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000929 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000930 uarg = UNICODE_DEFAULT_ENCODING(arg);
931 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000932 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000933 arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +0000934 *p = PyBytes_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000935 }
936 else
Guido van Rossum98297ee2007-11-06 21:34:58 +0000937 return converterr("string or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000938 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000939 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000940 FETCH_SIZE;
Thomas Woutersc3547a32006-03-01 21:31:21 +0000941 assert(0); /* XXX redundant with if-case */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000942 if (arg == Py_None) {
943 STORE_SIZE(0);
944 }
945 else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000946 STORE_SIZE(PyBytes_Size(arg));
Guido van Rossum98297ee2007-11-06 21:34:58 +0000947 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000948 format++;
949 }
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +0000950 else if (*p != NULL && uarg != NULL &&
Christian Heimes72b710a2008-05-26 13:28:38 +0000951 (Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000952 return converterr(
Guido van Rossum98297ee2007-11-06 21:34:58 +0000953 "string without null bytes or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000954 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000955 }
956 break;
957 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000958
Guido van Rossumfb67be22007-08-29 18:38:11 +0000959 case 'Z': {/* unicode, may be NULL (None) */
960 if (*format == '#') { /* any buffer-like object */
961 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
962 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000963
Guido van Rossumfb67be22007-08-29 18:38:11 +0000964 if (arg == Py_None) {
965 *p = 0;
966 STORE_SIZE(0);
967 }
968 else if (PyUnicode_Check(arg)) {
969 *p = PyUnicode_AS_UNICODE(arg);
970 STORE_SIZE(PyUnicode_GET_SIZE(arg));
971 }
972 format++;
973 } else {
974 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000975
Guido van Rossumfb67be22007-08-29 18:38:11 +0000976 if (arg == Py_None)
977 *p = 0;
978 else if (PyUnicode_Check(arg))
979 *p = PyUnicode_AS_UNICODE(arg);
980 else
Guido van Rossum98297ee2007-11-06 21:34:58 +0000981 return converterr("string or None",
Guido van Rossumfb67be22007-08-29 18:38:11 +0000982 arg, msgbuf, bufsize);
983 }
984 break;
985 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000986
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000987 case 'e': {/* encoded string */
988 char **buffer;
989 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000990 PyObject *s;
Guido van Rossumf15a29f2007-05-04 00:41:39 +0000991 int recode_strings;
992 Py_ssize_t size;
Guido van Rossumd70539a2007-05-09 23:35:09 +0000993 const char *ptr;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000994
995 /* Get 'e' parameter: the encoding name */
996 encoding = (const char *)va_arg(*p_va, const char *);
997 if (encoding == NULL)
998 encoding = PyUnicode_GetDefaultEncoding();
Guido van Rossum98297ee2007-11-06 21:34:58 +0000999
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001000 /* Get output buffer parameter:
1001 's' (recode all objects via Unicode) or
Guido van Rossum98297ee2007-11-06 21:34:58 +00001002 't' (only recode non-string objects)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001003 */
1004 if (*format == 's')
1005 recode_strings = 1;
1006 else if (*format == 't')
1007 recode_strings = 0;
1008 else
1009 return converterr(
1010 "(unknown parser marker combination)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001011 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001012 buffer = (char **)va_arg(*p_va, char **);
1013 format++;
1014 if (buffer == NULL)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001015 return converterr("(buffer is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001016 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001017
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001018 /* Encode object */
Guido van Rossumd70539a2007-05-09 23:35:09 +00001019 if (!recode_strings &&
Christian Heimes72b710a2008-05-26 13:28:38 +00001020 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001021 s = arg;
1022 Py_INCREF(s);
Guido van Rossumd70539a2007-05-09 23:35:09 +00001023 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1024 return converterr("(AsCharBuffer failed)",
1025 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001026 }
1027 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001028 PyObject *u;
1029
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001030 /* Convert object to Unicode */
1031 u = PyUnicode_FromObject(arg);
1032 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001033 return converterr(
Guido van Rossum98297ee2007-11-06 21:34:58 +00001034 "string or unicode or text buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001035 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001036
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001037 /* Encode object; use default error handling */
1038 s = PyUnicode_AsEncodedString(u,
1039 encoding,
1040 NULL);
1041 Py_DECREF(u);
1042 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001043 return converterr("(encoding failed)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001044 arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +00001045 if (!PyBytes_Check(s)) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001046 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001047 return converterr(
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001048 "(encoder failed to return bytes)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001049 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001050 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001051 size = PyBytes_GET_SIZE(s);
1052 ptr = PyBytes_AS_STRING(s);
Guido van Rossumd70539a2007-05-09 23:35:09 +00001053 if (ptr == NULL)
1054 ptr = "";
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001055 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001056
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001057 /* Write output; output is guaranteed to be 0-terminated */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001058 if (*format == '#') {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001059 /* Using buffer length parameter '#':
Guido van Rossum98297ee2007-11-06 21:34:58 +00001060
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001061 - if *buffer is NULL, a new buffer of the
1062 needed size is allocated and the data
1063 copied into it; *buffer is updated to point
1064 to the new buffer; the caller is
1065 responsible for PyMem_Free()ing it after
Guido van Rossum98297ee2007-11-06 21:34:58 +00001066 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001067
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001068 - if *buffer is not NULL, the data is
1069 copied to *buffer; *buffer_len has to be
1070 set to the size of the buffer on input;
1071 buffer overflow is signalled with an error;
1072 buffer has to provide enough room for the
1073 encoded string plus the trailing 0-byte
Guido van Rossum98297ee2007-11-06 21:34:58 +00001074
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001075 - in both cases, *buffer_len is updated to
1076 the size of the buffer /excluding/ the
Guido van Rossum98297ee2007-11-06 21:34:58 +00001077 trailing 0-byte
1078
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001079 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001080 FETCH_SIZE;
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001081
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001082 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001083 if (q == NULL && q2 == NULL) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001084 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001085 return converterr(
1086 "(buffer_len is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001087 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001088 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001089 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001090 *buffer = PyMem_NEW(char, size + 1);
1091 if (*buffer == NULL) {
1092 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001093 return converterr(
1094 "(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001095 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001096 }
Christian Heimes836baa52008-02-26 08:18:30 +00001097 if (addcleanup(*buffer, freelist)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001098 Py_DECREF(s);
1099 return converterr(
1100 "(cleanup problem)",
1101 arg, msgbuf, bufsize);
1102 }
Fred Drake25871c02000-05-03 15:17:02 +00001103 } else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001104 if (size + 1 > BUFFER_LEN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001105 Py_DECREF(s);
1106 return converterr(
Guido van Rossum98297ee2007-11-06 21:34:58 +00001107 "(buffer overflow)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001108 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001109 }
Fred Drake25871c02000-05-03 15:17:02 +00001110 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001111 memcpy(*buffer, ptr, size+1);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001112 STORE_SIZE(size);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001113 } else {
1114 /* Using a 0-terminated buffer:
Guido van Rossum98297ee2007-11-06 21:34:58 +00001115
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001116 - the encoded string has to be 0-terminated
1117 for this variant to work; if it is not, an
Guido van Rossum98297ee2007-11-06 21:34:58 +00001118 error raised
Fred Drake25871c02000-05-03 15:17:02 +00001119
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001120 - a new buffer of the needed size is
1121 allocated and the data copied into it;
1122 *buffer is updated to point to the new
1123 buffer; the caller is responsible for
1124 PyMem_Free()ing it after usage
1125
1126 */
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001127 if ((Py_ssize_t)strlen(ptr) != size) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001128 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001129 return converterr(
1130 "(encoded string without NULL bytes)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001131 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001132 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001133 *buffer = PyMem_NEW(char, size + 1);
1134 if (*buffer == NULL) {
1135 Py_DECREF(s);
1136 return converterr("(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001137 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001138 }
Christian Heimes836baa52008-02-26 08:18:30 +00001139 if (addcleanup(*buffer, freelist)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001140 Py_DECREF(s);
1141 return converterr("(cleanup problem)",
1142 arg, msgbuf, bufsize);
1143 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001144 memcpy(*buffer, ptr, 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
1150 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001151 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1152 if (!PyUnicode_Check(arg))
1153 return converterr("str", arg, msgbuf, bufsize);
1154 *p = PyUnicode_AS_UNICODE(arg);
1155 if (*format == '#') { /* store pointer and size */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001156 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001157 STORE_SIZE(PyUnicode_GET_SIZE(arg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001158 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001159 }
1160 break;
1161 }
1162
Christian Heimes72b710a2008-05-26 13:28:38 +00001163 case 'S': { /* PyBytes object */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001164 PyObject **p = va_arg(*p_va, PyObject **);
Christian Heimes72b710a2008-05-26 13:28:38 +00001165 if (PyBytes_Check(arg))
Guido van Rossum617dbc42007-05-07 23:57:08 +00001166 *p = arg;
1167 else
1168 return converterr("bytes", arg, msgbuf, bufsize);
1169 break;
1170 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001171
Christian Heimes9c4756e2008-05-26 13:22:05 +00001172 case 'Y': { /* PyByteArray object */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001173 PyObject **p = va_arg(*p_va, PyObject **);
Christian Heimes9c4756e2008-05-26 13:22:05 +00001174 if (PyByteArray_Check(arg))
Guido van Rossum98297ee2007-11-06 21:34:58 +00001175 *p = arg;
1176 else
1177 return converterr("buffer", arg, msgbuf, bufsize);
1178 break;
1179 }
1180
1181 case 'U': { /* PyUnicode object */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001182 PyObject **p = va_arg(*p_va, PyObject **);
1183 if (PyUnicode_Check(arg))
1184 *p = arg;
1185 else
Guido van Rossum98297ee2007-11-06 21:34:58 +00001186 return converterr("str", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001187 break;
1188 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001189
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001190 case 'O': { /* object */
1191 PyTypeObject *type;
1192 PyObject **p;
1193 if (*format == '!') {
1194 type = va_arg(*p_va, PyTypeObject*);
1195 p = va_arg(*p_va, PyObject **);
1196 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +00001197 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +00001198 *p = arg;
1199 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001200 return converterr(type->tp_name, arg, msgbuf, bufsize);
Guido van Rossumfccfe891998-05-15 22:04:07 +00001201
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001202 }
1203 else if (*format == '?') {
1204 inquiry pred = va_arg(*p_va, inquiry);
1205 p = va_arg(*p_va, PyObject **);
1206 format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001207 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001208 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001209 else
Guido van Rossum98297ee2007-11-06 21:34:58 +00001210 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001211 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001212
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001213 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001214 else if (*format == '&') {
1215 typedef int (*converter)(PyObject *, void *);
1216 converter convert = va_arg(*p_va, converter);
1217 void *addr = va_arg(*p_va, void *);
1218 format++;
1219 if (! (*convert)(arg, addr))
Guido van Rossum98297ee2007-11-06 21:34:58 +00001220 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001221 arg, msgbuf, bufsize);
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001222 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001223 else {
1224 p = va_arg(*p_va, PyObject **);
1225 *p = arg;
1226 }
1227 break;
1228 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001229
1230
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001231 case 'w': { /* memory buffer, read-write access */
1232 void **p = va_arg(*p_va, void **);
1233 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Christian Heimes4e30a842007-11-30 22:12:06 +00001234 Py_ssize_t count;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001235 int temp=-1;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001236 Py_buffer view;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001237
Martin v. Löwis423be952008-08-13 15:53:07 +00001238 if (pb && pb->bf_releasebuffer && *format != '*')
1239 /* Buffer must be released, yet caller does not use
1240 the Py_buffer protocol. */
1241 return converterr("pinned buffer", arg, msgbuf, bufsize);
1242
1243
1244 if (pb && pb->bf_getbuffer && *format == '*') {
1245 /* Caller is interested in Py_buffer, and the object
1246 supports it directly. */
1247 format++;
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001248 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +00001249 PyErr_Clear();
1250 return converterr("read-write buffer", arg, msgbuf, bufsize);
1251 }
1252 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1253 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1254 break;
1255 }
1256
1257 /* Here we have processed w*, only w and w# remain. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001258 if (pb == NULL ||
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001259 pb->bf_getbuffer == NULL ||
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001260 ((temp = PyObject_GetBuffer(arg, &view,
1261 PyBUF_SIMPLE)) != 0) ||
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001262 view.readonly == 1) {
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001263 if (temp==0) {
1264 PyBuffer_Release(&view);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001265 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001266 return converterr("single-segment read-write buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001267 arg, msgbuf, bufsize);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001268 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001269
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001270 if ((count = view.len) < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001271 return converterr("(unspecified)", arg, msgbuf, bufsize);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001272 *p = view.buf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001273 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001274 FETCH_SIZE;
1275 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001276 format++;
1277 }
1278 break;
1279 }
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001280
1281 /*TEO: This can be eliminated --- here only for backward
1282 compatibility */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001283 case 't': { /* 8-bit character buffer, read-only access */
Martin v. Löwis15e62742006-02-27 16:46:16 +00001284 char **p = va_arg(*p_va, char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001285 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Christian Heimes4e30a842007-11-30 22:12:06 +00001286 Py_ssize_t count;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001287 Py_buffer view;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001288
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001289 if (*format++ != '#')
1290 return converterr(
Guido van Rossum98297ee2007-11-06 21:34:58 +00001291 "invalid use of 't' format character",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001292 arg, msgbuf, bufsize);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001293 if (pb == NULL || pb->bf_getbuffer == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001294 return converterr(
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00001295 "bytes or read-only character buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001296 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001297
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001298 if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) != 0)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001299 return converterr("string or single-segment read-only buffer",
1300 arg, msgbuf, bufsize);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001301
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001302 count = view.len;
1303 *p = view.buf;
Martin v. Löwis423be952008-08-13 15:53:07 +00001304 if (pb->bf_releasebuffer)
1305 return converterr(
1306 "string or pinned buffer",
1307 arg, msgbuf, bufsize);
1308
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001309 PyBuffer_Release(&view);
1310
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001311 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001312 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001313 {
1314 FETCH_SIZE;
1315 STORE_SIZE(count);
1316 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001317 break;
1318 }
1319
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001320 default:
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001321 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001322
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001323 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001324
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001325 *p_format = format;
1326 return NULL;
1327}
Guido van Rossumaa354651996-08-19 19:32:04 +00001328
Martin v. Löwis18e16552006-02-15 17:27:45 +00001329static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001330convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001331{
1332 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001333 Py_ssize_t count;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001334 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001335
1336 *errmsg = NULL;
1337 *p = NULL;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001338 if (pb == NULL ||
Martin v. Löwis423be952008-08-13 15:53:07 +00001339 pb->bf_getbuffer == NULL ||
1340 pb->bf_releasebuffer != NULL) {
Guido van Rossumb0834002007-11-21 21:53:51 +00001341 *errmsg = "bytes or read-only buffer";
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001342 return -1;
1343 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001344
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001345 if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) != 0) {
Guido van Rossumb0834002007-11-21 21:53:51 +00001346 *errmsg = "bytes or single-segment read-only buffer";
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001347 return -1;
1348 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001349 count = view.len;
1350 *p = view.buf;
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001351 PyBuffer_Release(&view);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001352 return count;
1353}
Guido van Rossumaa354651996-08-19 19:32:04 +00001354
Martin v. Löwis423be952008-08-13 15:53:07 +00001355/* XXX for 3.x, getbuffer and convertbuffer can probably
1356 be merged again. */
1357static int
Neal Norwitz2f99b242008-08-24 05:48:10 +00001358getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +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) {
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001368 if (PyObject_GetBuffer(arg, view, 0) < 0) {
Neal Norwitz2f99b242008-08-24 05:48:10 +00001369 *errmsg = "convertible to a buffer";
Martin v. Löwis423be952008-08-13 15:53:07 +00001370 return -1;
Neal Norwitz2f99b242008-08-24 05:48:10 +00001371 }
Martin v. Löwis423be952008-08-13 15:53:07 +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 Norwitz2f99b242008-08-24 05:48:10 +00001380 if (count < 0) {
1381 *errmsg = "convertible to a buffer";
Martin v. Löwis423be952008-08-13 15:53:07 +00001382 return count;
Neal Norwitz2f99b242008-08-24 05:48:10 +00001383 }
Martin v. Löwis423be952008-08-13 15:53:07 +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,
Guido van Rossum98297ee2007-11-06 21:34:58 +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);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001411 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001412 va_end(va);
1413 return retval;
1414}
1415
1416int
1417_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1418 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001419 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);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001435 retval = vgetargskeywords(args, keywords, format,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001436 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,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001445 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
Guido van Rossum98297ee2007-11-06 21:34:58 +00001470 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001471 return retval;
1472}
1473
1474int
1475_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1476 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001477 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
Guido van Rossum98297ee2007-11-06 21:34:58 +00001502 retval = vgetargskeywords(args, keywords, format,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001503 kwlist, &lva, FLAG_SIZE_T);
Brett Cannon711e7d92004-07-10 22:20:32 +00001504 return retval;
1505}
1506
Christian Heimes380f7f22008-02-28 11:19:05 +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 Heimes380f7f22008-02-28 11:19:05 +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 Heimes380f7f22008-02-28 11:19:05 +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 Heimes380f7f22008-02-28 11:19:05 +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 Heimes380f7f22008-02-28 11:19:05 +00001532 else {
1533 custom_msg = strchr(format,';');
1534 if (custom_msg)
1535 custom_msg++;
Tim Peters62d48e12001-10-27 06:42:16 +00001536 }
Christian Heimes380f7f22008-02-28 11:19:05 +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 Heimes380f7f22008-02-28 11:19:05 +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 Heimes380f7f22008-02-28 11:19:05 +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 Heimes380f7f22008-02-28 11:19:05 +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 Heimes380f7f22008-02-28 11:19:05 +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
Christian Heimes380f7f22008-02-28 11:19:05 +00001621 if (!IS_END_OF_FORMAT(*format)) {
1622 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;
Georg Brandld8b690f2008-05-16 17:28:50 +00001635 if (!PyUnicode_Check(key)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001636 PyErr_SetString(PyExc_TypeError,
Guido van Rossum55474762002-04-04 16:22:30 +00001637 "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 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001640 ks = _PyUnicode_AsString(key);
Christian Heimes380f7f22008-02-28 11:19:05 +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 Heimes380f7f22008-02-28 11:19:05 +00001664 const char *format = *p_format;
Guido van Rossumaa354651996-08-19 19:32:04 +00001665 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001666
Guido van Rossumaa354651996-08-19 19:32:04 +00001667 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 Rossum98297ee2007-11-06 21:34:58 +00001700
Georg Brandl6dd14612005-09-14 19:29:53 +00001701 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001702
Georg Brandl6dd14612005-09-14 19:29:53 +00001703 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 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001712
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 */
Walter Dörwald612344f2007-05-04 19:28:21 +00001715 case 'y': /* bytes */
Georg Brandl6dd14612005-09-14 19:29:53 +00001716 case 'u': /* unicode string */
Georg Brandl6dd14612005-09-14 19:29:53 +00001717 case 't': /* buffer, read-only */
1718 case 'w': /* buffer, read-write */
Guido van Rossumaa354651996-08-19 19:32:04 +00001719 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001720 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001721 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001722 if (flags & FLAG_SIZE_T)
1723 (void) va_arg(*p_va, Py_ssize_t *);
1724 else
1725 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001726 format++;
Martin v. Löwis423be952008-08-13 15:53:07 +00001727 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
1728 format++;
Guido van Rossumaa354651996-08-19 19:32:04 +00001729 }
1730 break;
1731 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001732
1733 /* object codes */
1734
Guido van Rossumaa354651996-08-19 19:32:04 +00001735 case 'S': /* string object */
Guido van Rossum617dbc42007-05-07 23:57:08 +00001736 case 'Y': /* string object */
Georg Brandl6dd14612005-09-14 19:29:53 +00001737 case 'U': /* unicode string object */
Guido van Rossumaa354651996-08-19 19:32:04 +00001738 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001739 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001740 break;
1741 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001742
Guido van Rossumaa354651996-08-19 19:32:04 +00001743 case 'O': /* object */
1744 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001745 if (*format == '!') {
1746 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001747 (void) va_arg(*p_va, PyTypeObject*);
1748 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001749 }
1750#if 0
1751/* I don't know what this is for */
1752 else if (*format == '?') {
1753 inquiry pred = va_arg(*p_va, inquiry);
1754 format++;
1755 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001756 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001757 }
1758 }
1759#endif
1760 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001761 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001762 (void) va_arg(*p_va, converter);
1763 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001764 format++;
1765 }
1766 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001767 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001768 }
1769 break;
1770 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001771
Christian Heimes380f7f22008-02-28 11:19:05 +00001772 case '(': /* bypass tuple, not handled at all previously */
1773 {
1774 char *msg;
1775 for (;;) {
1776 if (*format==')')
1777 break;
1778 if (IS_END_OF_FORMAT(*format))
1779 return "Unmatched left paren in format "
1780 "string";
1781 msg = skipitem(&format, p_va, flags);
1782 if (msg)
1783 return msg;
1784 }
1785 format++;
1786 break;
1787 }
1788
1789 case ')':
1790 return "Unmatched right paren in format string";
1791
Guido van Rossumaa354651996-08-19 19:32:04 +00001792 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001793err:
Guido van Rossumaa354651996-08-19 19:32:04 +00001794 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00001795
Guido van Rossumaa354651996-08-19 19:32:04 +00001796 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001797
Guido van Rossumaa354651996-08-19 19:32:04 +00001798 *p_format = format;
1799 return NULL;
1800}
Fred Drakee4616e62001-10-23 21:09:29 +00001801
1802
1803int
Martin v. Löwis76246742006-03-01 04:06:10 +00001804PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001805{
Martin v. Löwis76246742006-03-01 04:06:10 +00001806 Py_ssize_t i, l;
Fred Drakee4616e62001-10-23 21:09:29 +00001807 PyObject **o;
1808 va_list vargs;
1809
1810#ifdef HAVE_STDARG_PROTOTYPES
1811 va_start(vargs, max);
1812#else
1813 va_start(vargs);
1814#endif
1815
1816 assert(min >= 0);
1817 assert(min <= max);
1818 if (!PyTuple_Check(args)) {
1819 PyErr_SetString(PyExc_SystemError,
1820 "PyArg_UnpackTuple() argument list is not a tuple");
1821 return 0;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001822 }
Fred Drakee4616e62001-10-23 21:09:29 +00001823 l = PyTuple_GET_SIZE(args);
1824 if (l < min) {
1825 if (name != NULL)
1826 PyErr_Format(
1827 PyExc_TypeError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001828 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001829 name, (min == max ? "" : "at least "), min, l);
1830 else
1831 PyErr_Format(
1832 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001833 "unpacked tuple should have %s%zd elements,"
Guido van Rossum98297ee2007-11-06 21:34:58 +00001834 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001835 (min == max ? "" : "at least "), min, l);
1836 va_end(vargs);
1837 return 0;
1838 }
1839 if (l > max) {
1840 if (name != NULL)
1841 PyErr_Format(
1842 PyExc_TypeError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001843 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001844 name, (min == max ? "" : "at most "), max, l);
1845 else
1846 PyErr_Format(
1847 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001848 "unpacked tuple should have %s%zd elements,"
Guido van Rossum98297ee2007-11-06 21:34:58 +00001849 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001850 (min == max ? "" : "at most "), max, l);
1851 va_end(vargs);
1852 return 0;
1853 }
1854 for (i = 0; i < l; i++) {
1855 o = va_arg(vargs, PyObject **);
1856 *o = PyTuple_GET_ITEM(args, i);
1857 }
1858 va_end(vargs);
1859 return 1;
1860}
Georg Brandl02c42872005-08-26 06:42:30 +00001861
1862
1863/* For type constructors that don't take keyword args
1864 *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001865 * Sets a TypeError and returns 0 if the kwds dict is
Thomas Wouters89f507f2006-12-13 04:49:30 +00001866 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001867 */
1868int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001869_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001870{
1871 if (kw == NULL)
1872 return 1;
1873 if (!PyDict_CheckExact(kw)) {
1874 PyErr_BadInternalCall();
1875 return 0;
1876 }
1877 if (PyDict_Size(kw) == 0)
1878 return 1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001879
1880 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
Georg Brandl02c42872005-08-26 06:42:30 +00001881 funcname);
1882 return 0;
1883}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001884#ifdef __cplusplus
1885};
1886#endif