blob: 7c9774fb82eb64fd50dc2a334bc7dc0874c1afaf [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
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 }
Neal Norwitzdf6ac3d2008-02-26 05:23:51 +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{
Neal Norwitzdf6ac3d2008-02-26 05:23:51 +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++;
Georg Brandl209307e2006-08-09 07:03:22 +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 }
244
245 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000246 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000247
248 if (min < 0)
249 min = max;
250
251 format = formatsave;
252
253 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 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000273 msg = convertitem(args, &format, p_va, flags, levels,
274 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 }
286
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 }
292
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000293 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000294
295 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 }
312
313 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,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000317 flags, levels, msgbuf,
318 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 Rossumfe3f1a21994-09-29 09:42:55 +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);
Georg Brandl5f135782006-07-26 08:03:10 +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,
396 int *levels, char *msgbuf, size_t bufsize, int toplevel,
397 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;
403
404 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 }
421
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000422 if (!PySequence_Check(arg) || PyString_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",
427 n,
428 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000429 return msgbuf;
430 }
431
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);
Georg Brandl5f135782006-07-26 08:03:10 +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 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000453 msg = convertitem(item, &format, p_va, flags, levels+1,
454 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 Rossumfe3f1a21994-09-29 09:42:55 +0000476
477 if (*format == '(' /* ')' */) {
478 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +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 {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000485 msg = convertsimple(arg, &format, p_va, flags,
486 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);
506 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
Neil Schemenauer5042da62003-02-04 20:59:40 +0000515/* explicitly check for float arguments when integers are expected. For now
516 * signal a warning. Returns true if an exception was raised. */
517static int
518float_argument_error(PyObject *arg)
519{
520 if (PyFloat_Check(arg) &&
521 PyErr_Warn(PyExc_DeprecationWarning,
522 "integer argument expected, got float" ))
523 return 1;
524 else
525 return 0;
526}
527
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000528/* Convert a non-tuple argument. Return NULL if conversion went OK,
529 or a string with a message describing the failure. The message is
530 formatted as "must be <desired type>, not <actual type>".
531 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000532 Don't call if a tuple is expected.
533
534 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000535*/
536
537static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000538convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000539 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000540{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000541 /* For # codes */
542#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
543 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
544 else q=va_arg(*p_va, int*);
545#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
546#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
547
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000548 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000549 char c = *format++;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000550#ifdef Py_USING_UNICODE
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000551 PyObject *uarg;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000552#endif
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000553
554 switch (c) {
555
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000556 case 'b': { /* unsigned byte -- very short int */
557 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000558 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000559 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000560 return converterr("integer<b>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000561 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000562 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000563 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000564 else if (ival < 0) {
565 PyErr_SetString(PyExc_OverflowError,
566 "unsigned byte integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000567 return converterr("integer<b>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000568 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000569 else if (ival > UCHAR_MAX) {
570 PyErr_SetString(PyExc_OverflowError,
571 "unsigned byte integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000572 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000573 }
574 else
575 *p = (unsigned char) ival;
576 break;
577 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000578
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000579 case 'B': {/* byte sized bitfield - both signed and unsigned
580 values allowed */
581 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000582 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000583 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000584 return converterr("integer<B>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000585 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000586 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000587 return converterr("integer<B>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000588 else
589 *p = (unsigned char) ival;
590 break;
591 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000592
Guido van Rossumfce26e72003-04-18 00:12:30 +0000593 case 'h': {/* signed short int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000594 short *p = va_arg(*p_va, short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000595 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000596 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000597 return converterr("integer<h>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000598 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000599 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000600 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfce26e72003-04-18 00:12:30 +0000601 else if (ival < SHRT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000602 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000603 "signed short integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000604 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000605 }
Guido van Rossumfce26e72003-04-18 00:12:30 +0000606 else if (ival > SHRT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000607 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000608 "signed short integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000609 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000610 }
611 else
612 *p = (short) ival;
613 break;
614 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000615
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000616 case 'H': { /* short int sized bitfield, both signed and
617 unsigned allowed */
618 unsigned short *p = va_arg(*p_va, unsigned short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000619 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000620 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000621 return converterr("integer<H>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000622 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000623 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000624 return converterr("integer<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000625 else
626 *p = (unsigned short) ival;
627 break;
628 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000629
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000630 case 'i': {/* signed int */
631 int *p = va_arg(*p_va, int *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000632 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000633 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000634 return converterr("integer<i>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000635 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000636 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000637 return converterr("integer<i>", arg, msgbuf, bufsize);
Georg Brandl98251f82006-06-08 13:31:07 +0000638 else if (ival > INT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000639 PyErr_SetString(PyExc_OverflowError,
640 "signed integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000641 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000642 }
Georg Brandl98251f82006-06-08 13:31:07 +0000643 else if (ival < INT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000644 PyErr_SetString(PyExc_OverflowError,
645 "signed integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000646 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000647 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000648 else
649 *p = ival;
650 break;
651 }
652
Thomas Hellera4ea6032003-04-17 18:55:45 +0000653 case 'I': { /* int sized bitfield, both signed and
654 unsigned allowed */
655 unsigned int *p = va_arg(*p_va, unsigned int *);
656 unsigned int ival;
657 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000658 return converterr("integer<I>", arg, msgbuf, bufsize);
Skip Montanarob5079722006-04-18 00:57:15 +0000659 ival = (unsigned int)PyInt_AsUnsignedLongMask(arg);
660 if (ival == (unsigned int)-1 && PyErr_Occurred())
Thomas Hellera4ea6032003-04-17 18:55:45 +0000661 return converterr("integer<I>", arg, msgbuf, bufsize);
662 else
663 *p = ival;
664 break;
665 }
666
Martin v. Löwis18e16552006-02-15 17:27:45 +0000667 case 'n': /* Py_ssize_t */
668#if SIZEOF_SIZE_T != SIZEOF_LONG
669 {
670 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
671 Py_ssize_t ival;
672 if (float_argument_error(arg))
Georg Brandl7f573f72006-04-13 07:59:30 +0000673 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000674 ival = PyInt_AsSsize_t(arg);
675 if (ival == -1 && PyErr_Occurred())
Georg Brandl7f573f72006-04-13 07:59:30 +0000676 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000677 *p = ival;
678 break;
679 }
680#endif
681 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000682 case 'l': {/* long int */
683 long *p = va_arg(*p_va, long *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000684 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000685 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000686 return converterr("integer<l>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000687 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000688 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000689 return converterr("integer<l>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000690 else
691 *p = ival;
692 break;
693 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000694
695 case 'k': { /* long sized bitfield */
696 unsigned long *p = va_arg(*p_va, unsigned long *);
697 unsigned long ival;
698 if (PyInt_Check(arg))
699 ival = PyInt_AsUnsignedLongMask(arg);
700 else if (PyLong_Check(arg))
701 ival = PyLong_AsUnsignedLongMask(arg);
702 else
703 return converterr("integer<k>", arg, msgbuf, bufsize);
704 *p = ival;
705 break;
706 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000707
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000708#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000709 case 'L': {/* PY_LONG_LONG */
710 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
711 PY_LONG_LONG ival = PyLong_AsLongLong( arg );
Neal Norwitzdf6ac3d2008-02-26 05:23:51 +0000712 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000713 return converterr("long<L>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000714 } else {
715 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000716 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000717 break;
718 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000719
720 case 'K': { /* long long sized bitfield */
721 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
722 unsigned PY_LONG_LONG ival;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000723 if (PyInt_Check(arg))
724 ival = PyInt_AsUnsignedLongMask(arg);
725 else if (PyLong_Check(arg))
726 ival = PyLong_AsUnsignedLongLongMask(arg);
727 else
728 return converterr("integer<K>", arg, msgbuf, bufsize);
729 *p = ival;
730 break;
731 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000732#endif
733
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000734 case 'f': {/* float */
735 float *p = va_arg(*p_va, float *);
736 double dval = PyFloat_AsDouble(arg);
737 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000738 return converterr("float<f>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000739 else
740 *p = (float) dval;
741 break;
742 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000743
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000744 case 'd': {/* double */
745 double *p = va_arg(*p_va, double *);
746 double dval = PyFloat_AsDouble(arg);
747 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000748 return converterr("float<d>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000749 else
750 *p = dval;
751 break;
752 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000753
Guido van Rossum530956d1996-07-21 02:27:43 +0000754#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000755 case 'D': {/* complex double */
756 Py_complex *p = va_arg(*p_va, Py_complex *);
757 Py_complex cval;
758 cval = PyComplex_AsCComplex(arg);
759 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000760 return converterr("complex<D>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000761 else
762 *p = cval;
763 break;
764 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000765#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000766
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000767 case 'c': {/* char */
768 char *p = va_arg(*p_va, char *);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000769 if (PyString_Check(arg) && PyString_Size(arg) == 1)
770 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000771 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000772 return converterr("char", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000773 break;
774 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000775
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000776 case 's': {/* string */
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000777 if (*format == '*') {
778 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
779
780 if (PyString_Check(arg)) {
781 PyBuffer_FillInfo(p, arg,
782 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
783 1, 0);
784 }
785#ifdef Py_USING_UNICODE
786 else if (PyUnicode_Check(arg)) {
787 uarg = UNICODE_DEFAULT_ENCODING(arg);
788 if (uarg == NULL)
789 return converterr(CONV_UNICODE,
790 arg, msgbuf, bufsize);
791 PyBuffer_FillInfo(p, arg,
792 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
793 1, 0);
794 }
795#endif
796 else { /* any buffer-like object */
797 char *buf;
798 if (getbuffer(arg, p, &buf) < 0)
799 return converterr(buf, arg, msgbuf, bufsize);
800 }
801 format++;
802 } else if (*format == '#') {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000803 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000804 FETCH_SIZE;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000805
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000806 if (PyString_Check(arg)) {
807 *p = PyString_AS_STRING(arg);
808 STORE_SIZE(PyString_GET_SIZE(arg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000809 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000810#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000811 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000812 uarg = UNICODE_DEFAULT_ENCODING(arg);
813 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000814 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000815 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000816 *p = PyString_AS_STRING(uarg);
817 STORE_SIZE(PyString_GET_SIZE(uarg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000818 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000819#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000820 else { /* any buffer-like object */
821 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000822 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000823 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000824 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000825 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000826 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000827 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000828 } else {
829 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000830
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000831 if (PyString_Check(arg))
832 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000833#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000834 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000835 uarg = UNICODE_DEFAULT_ENCODING(arg);
836 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000837 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000838 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000839 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000840 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000841#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000842 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000843 return converterr("string", arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000844 if ((Py_ssize_t)strlen(*p) != PyString_Size(arg))
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
851 case 'z': {/* string, may be NULL (None) */
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000852 if (*format == '*') {
853 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
854
855 if (arg == Py_None)
856 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
857 else if (PyString_Check(arg)) {
858 PyBuffer_FillInfo(p, arg,
859 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
860 1, 0);
861 }
862#ifdef Py_USING_UNICODE
863 else if (PyUnicode_Check(arg)) {
864 uarg = UNICODE_DEFAULT_ENCODING(arg);
865 if (uarg == NULL)
866 return converterr(CONV_UNICODE,
867 arg, msgbuf, bufsize);
868 PyBuffer_FillInfo(p, arg,
869 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
870 1, 0);
871 }
872#endif
873 else { /* any buffer-like object */
874 char *buf;
875 if (getbuffer(arg, p, &buf) < 0)
876 return converterr(buf, arg, msgbuf, bufsize);
877 }
878 format++;
879 } else if (*format == '#') { /* any buffer-like object */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000880 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000881 FETCH_SIZE;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000882
883 if (arg == Py_None) {
884 *p = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000885 STORE_SIZE(0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000886 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000887 else if (PyString_Check(arg)) {
888 *p = PyString_AS_STRING(arg);
889 STORE_SIZE(PyString_GET_SIZE(arg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000890 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000891#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000892 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000893 uarg = UNICODE_DEFAULT_ENCODING(arg);
894 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000895 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000896 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000897 *p = PyString_AS_STRING(uarg);
898 STORE_SIZE(PyString_GET_SIZE(uarg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000899 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000900#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000901 else { /* any buffer-like object */
902 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000903 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000904 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000905 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000906 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000907 }
908 format++;
909 } else {
910 char **p = va_arg(*p_va, char **);
911
912 if (arg == Py_None)
913 *p = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000914 else if (PyString_Check(arg))
915 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000916#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000917 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000918 uarg = UNICODE_DEFAULT_ENCODING(arg);
919 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000920 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000921 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000922 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000923 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000924#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000925 else
926 return converterr("string or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000927 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000928 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000929 FETCH_SIZE;
Thomas Woutersc3547a32006-03-01 21:31:21 +0000930 assert(0); /* XXX redundant with if-case */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000931 if (arg == Py_None)
932 *q = 0;
933 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000934 *q = PyString_Size(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000935 format++;
936 }
937 else if (*p != NULL &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000938 (Py_ssize_t)strlen(*p) != PyString_Size(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000939 return converterr(
940 "string without null bytes or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000941 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000942 }
943 break;
944 }
945
946 case 'e': {/* encoded string */
947 char **buffer;
948 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000949 PyObject *s;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +0000950 Py_ssize_t size;
951 int recode_strings;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000952
953 /* Get 'e' parameter: the encoding name */
954 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000955#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000956 if (encoding == NULL)
957 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000958#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000959
960 /* Get output buffer parameter:
961 's' (recode all objects via Unicode) or
962 't' (only recode non-string objects)
963 */
964 if (*format == 's')
965 recode_strings = 1;
966 else if (*format == 't')
967 recode_strings = 0;
968 else
969 return converterr(
970 "(unknown parser marker combination)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000971 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000972 buffer = (char **)va_arg(*p_va, char **);
973 format++;
974 if (buffer == NULL)
975 return converterr("(buffer is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000976 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000977
978 /* Encode object */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000979 if (!recode_strings && PyString_Check(arg)) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000980 s = arg;
981 Py_INCREF(s);
982 }
983 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000984#ifdef Py_USING_UNICODE
985 PyObject *u;
986
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000987 /* Convert object to Unicode */
988 u = PyUnicode_FromObject(arg);
989 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000990 return converterr(
991 "string or unicode or text buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000992 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000993
994 /* Encode object; use default error handling */
995 s = PyUnicode_AsEncodedString(u,
996 encoding,
997 NULL);
998 Py_DECREF(u);
999 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001000 return converterr("(encoding failed)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001001 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001002 if (!PyString_Check(s)) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001003 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001004 return converterr(
1005 "(encoder failed to return a string)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001006 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001007 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001008#else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001009 return converterr("string<e>", arg, msgbuf, bufsize);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001010#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001011 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001012 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001013
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001014 /* Write output; output is guaranteed to be 0-terminated */
1015 if (*format == '#') {
1016 /* Using buffer length parameter '#':
1017
1018 - if *buffer is NULL, a new buffer of the
1019 needed size is allocated and the data
1020 copied into it; *buffer is updated to point
1021 to the new buffer; the caller is
1022 responsible for PyMem_Free()ing it after
1023 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001024
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001025 - if *buffer is not NULL, the data is
1026 copied to *buffer; *buffer_len has to be
1027 set to the size of the buffer on input;
1028 buffer overflow is signalled with an error;
1029 buffer has to provide enough room for the
1030 encoded string plus the trailing 0-byte
1031
1032 - in both cases, *buffer_len is updated to
1033 the size of the buffer /excluding/ the
1034 trailing 0-byte
1035
1036 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001037 FETCH_SIZE;
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001038
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001039 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001040 if (q == NULL && q2 == NULL) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001041 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001042 return converterr(
1043 "(buffer_len is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001044 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001045 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001046 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001047 *buffer = PyMem_NEW(char, size + 1);
1048 if (*buffer == NULL) {
1049 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001050 return converterr(
1051 "(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001052 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001053 }
Neal Norwitzdf6ac3d2008-02-26 05:23:51 +00001054 if (addcleanup(*buffer, freelist)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001055 Py_DECREF(s);
1056 return converterr(
1057 "(cleanup problem)",
1058 arg, msgbuf, bufsize);
1059 }
Fred Drake25871c02000-05-03 15:17:02 +00001060 } else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001061 if (size + 1 > BUFFER_LEN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001062 Py_DECREF(s);
1063 return converterr(
1064 "(buffer overflow)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001065 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001066 }
Fred Drake25871c02000-05-03 15:17:02 +00001067 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001068 memcpy(*buffer,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001069 PyString_AS_STRING(s),
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001070 size + 1);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001071 STORE_SIZE(size);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001072 } else {
1073 /* Using a 0-terminated buffer:
1074
1075 - the encoded string has to be 0-terminated
1076 for this variant to work; if it is not, an
1077 error raised
Fred Drake25871c02000-05-03 15:17:02 +00001078
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001079 - a new buffer of the needed size is
1080 allocated and the data copied into it;
1081 *buffer is updated to point to the new
1082 buffer; the caller is responsible for
1083 PyMem_Free()ing it after usage
1084
1085 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001086 if ((Py_ssize_t)strlen(PyString_AS_STRING(s))
Armin Rigo7ccbca92006-10-04 12:17:45 +00001087 != size) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001088 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001089 return converterr(
1090 "(encoded string without NULL bytes)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001091 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001092 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001093 *buffer = PyMem_NEW(char, size + 1);
1094 if (*buffer == NULL) {
1095 Py_DECREF(s);
1096 return converterr("(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001097 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001098 }
Neal Norwitzdf6ac3d2008-02-26 05:23:51 +00001099 if (addcleanup(*buffer, freelist)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001100 Py_DECREF(s);
1101 return converterr("(cleanup problem)",
1102 arg, msgbuf, bufsize);
1103 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001104 memcpy(*buffer,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001105 PyString_AS_STRING(s),
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001106 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001107 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001108 Py_DECREF(s);
1109 break;
1110 }
1111
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001112#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001113 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1114 if (*format == '#') { /* any buffer-like object */
1115 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001116 FETCH_SIZE;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001117 if (PyUnicode_Check(arg)) {
1118 *p = PyUnicode_AS_UNICODE(arg);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001119 STORE_SIZE(PyUnicode_GET_SIZE(arg));
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001120 }
1121 else {
Neal Norwitz61546162006-04-14 05:20:28 +00001122 return converterr("cannot convert raw buffers",
1123 arg, msgbuf, bufsize);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001124 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001125 format++;
1126 } else {
1127 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Guido van Rossume826ef02000-03-10 23:02:17 +00001128 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001129 *p = PyUnicode_AS_UNICODE(arg);
1130 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001131 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001132 }
1133 break;
1134 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001135#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001136
1137 case 'S': { /* string object */
1138 PyObject **p = va_arg(*p_va, PyObject **);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001139 if (PyString_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001140 *p = arg;
1141 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001142 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001143 break;
1144 }
1145
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001146#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001147 case 'U': { /* Unicode object */
1148 PyObject **p = va_arg(*p_va, PyObject **);
1149 if (PyUnicode_Check(arg))
1150 *p = 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 break;
1154 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001155#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001156
1157 case 'O': { /* object */
1158 PyTypeObject *type;
1159 PyObject **p;
1160 if (*format == '!') {
1161 type = va_arg(*p_va, PyTypeObject*);
1162 p = va_arg(*p_va, PyObject **);
1163 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +00001164 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +00001165 *p = arg;
1166 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001167 return converterr(type->tp_name, arg, msgbuf, bufsize);
Guido van Rossumfccfe891998-05-15 22:04:07 +00001168
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001169 }
1170 else if (*format == '?') {
1171 inquiry pred = va_arg(*p_va, inquiry);
1172 p = va_arg(*p_va, PyObject **);
1173 format++;
1174 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001175 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001176 else
1177 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001178 arg, msgbuf, bufsize);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001179
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001180 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001181 else if (*format == '&') {
1182 typedef int (*converter)(PyObject *, void *);
1183 converter convert = va_arg(*p_va, converter);
1184 void *addr = va_arg(*p_va, void *);
1185 format++;
1186 if (! (*convert)(arg, addr))
1187 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001188 arg, msgbuf, bufsize);
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001189 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001190 else {
1191 p = va_arg(*p_va, PyObject **);
1192 *p = arg;
1193 }
1194 break;
1195 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001196
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001197
1198 case 'w': { /* memory buffer, read-write access */
1199 void **p = va_arg(*p_va, void **);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001200 void *res;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001201 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +00001202 Py_ssize_t count;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001203
1204 if (pb && pb->bf_releasebuffer && *format != '*')
1205 /* Buffer must be released, yet caller does not use
1206 the Py_buffer protocol. */
1207 return converterr("pinned buffer", arg, msgbuf, bufsize);
1208
1209 if (pb && pb->bf_getbuffer && *format == '*') {
1210 /* Caller is interested in Py_buffer, and the object
1211 supports it directly. */
1212 format++;
1213 if (pb->bf_getbuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1214 PyErr_Clear();
1215 return converterr("read-write buffer", arg, msgbuf, bufsize);
1216 }
1217 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1218 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1219 break;
1220 }
1221
1222 if (pb == NULL ||
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001223 pb->bf_getwritebuffer == NULL ||
1224 pb->bf_getsegcount == NULL)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001225 return converterr("read-write buffer", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001226 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1227 return converterr("single-segment read-write buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001228 arg, msgbuf, bufsize);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001229 if ((count = pb->bf_getwritebuffer(arg, 0, &res)) < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001230 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001231 if (*format == '*') {
1232 PyBuffer_FillInfo((Py_buffer*)p, arg, res, count, 1, 0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001233 format++;
1234 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001235 else {
1236 *p = res;
1237 if (*format == '#') {
1238 FETCH_SIZE;
1239 STORE_SIZE(count);
1240 format++;
1241 }
1242 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001243 break;
1244 }
1245
1246 case 't': { /* 8-bit character buffer, read-only access */
Martin v. Löwis15e62742006-02-27 16:46:16 +00001247 char **p = va_arg(*p_va, char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001248 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +00001249 Py_ssize_t count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001250
1251 if (*format++ != '#')
1252 return converterr(
1253 "invalid use of 't' format character",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001254 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001255 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +00001256 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
1257 pb == NULL || pb->bf_getcharbuffer == NULL ||
1258 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001259 return converterr(
1260 "string or read-only character buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001261 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001262
Jeremy Hylton4819e972001-10-11 14:40:37 +00001263 if (pb->bf_getsegcount(arg, NULL) != 1)
1264 return converterr(
1265 "string or single-segment read-only buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001266 arg, msgbuf, bufsize);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001267
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001268 if (pb->bf_releasebuffer)
1269 return converterr(
1270 "string or pinned buffer",
1271 arg, msgbuf, bufsize);
1272
Jeremy Hylton4819e972001-10-11 14:40:37 +00001273 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001274 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001275 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001276 {
1277 FETCH_SIZE;
1278 STORE_SIZE(count);
1279 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001280 break;
1281 }
1282
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001283 default:
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001284 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001285
1286 }
1287
1288 *p_format = format;
1289 return NULL;
1290}
Guido van Rossumaa354651996-08-19 19:32:04 +00001291
Martin v. Löwis18e16552006-02-15 17:27:45 +00001292static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001293convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001294{
1295 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001296 Py_ssize_t count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001297 if (pb == NULL ||
1298 pb->bf_getreadbuffer == NULL ||
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001299 pb->bf_getsegcount == NULL ||
1300 pb->bf_releasebuffer != NULL) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001301 *errmsg = "string or read-only buffer";
1302 return -1;
1303 }
1304 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
1305 *errmsg = "string or single-segment read-only buffer";
1306 return -1;
1307 }
1308 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
1309 *errmsg = "(unspecified)";
1310 }
1311 return count;
1312}
Guido van Rossumaa354651996-08-19 19:32:04 +00001313
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001314static int
1315getbuffer(PyObject *arg, Py_buffer *view, char**errmsg)
1316{
1317 void *buf;
1318 Py_ssize_t count;
1319 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1320 if (pb == NULL) {
1321 *errmsg = "string or buffer";
1322 return -1;
1323 }
1324 if (pb->bf_getbuffer) {
1325 if (pb->bf_getbuffer(arg, view, 0) < 0)
1326 return -1;
1327 if (!PyBuffer_IsContiguous(view, 'C')) {
1328 *errmsg = "contiguous buffer";
1329 return -1;
1330 }
1331 return 0;
1332 }
1333
1334 count = convertbuffer(arg, &buf, errmsg);
1335 if (count < 0)
1336 return count;
1337 PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
1338 return 0;
1339}
1340
Guido van Rossumaa354651996-08-19 19:32:04 +00001341/* Support for keyword arguments donated by
1342 Geoff Philbrick <philbric@delphi.hks.com> */
1343
Tim Petersf8cd3e82001-10-27 04:26:57 +00001344/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001345int
1346PyArg_ParseTupleAndKeywords(PyObject *args,
1347 PyObject *keywords,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001348 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001349 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001350{
1351 int retval;
1352 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001353
1354 if ((args == NULL || !PyTuple_Check(args)) ||
1355 (keywords != NULL && !PyDict_Check(keywords)) ||
1356 format == NULL ||
1357 kwlist == NULL)
1358 {
1359 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001360 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001361 }
1362
Guido van Rossumaa354651996-08-19 19:32:04 +00001363 va_start(va, kwlist);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001364 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1365 va_end(va);
1366 return retval;
1367}
1368
1369int
1370_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1371 PyObject *keywords,
1372 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001373 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001374{
1375 int retval;
1376 va_list va;
1377
1378 if ((args == NULL || !PyTuple_Check(args)) ||
1379 (keywords != NULL && !PyDict_Check(keywords)) ||
1380 format == NULL ||
1381 kwlist == NULL)
1382 {
1383 PyErr_BadInternalCall();
1384 return 0;
1385 }
1386
1387 va_start(va, kwlist);
1388 retval = vgetargskeywords(args, keywords, format,
1389 kwlist, &va, FLAG_SIZE_T);
Guido van Rossumaa354651996-08-19 19:32:04 +00001390 va_end(va);
1391 return retval;
1392}
1393
1394
Brett Cannon711e7d92004-07-10 22:20:32 +00001395int
1396PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001397 PyObject *keywords,
1398 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001399 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001400{
1401 int retval;
1402 va_list lva;
1403
1404 if ((args == NULL || !PyTuple_Check(args)) ||
1405 (keywords != NULL && !PyDict_Check(keywords)) ||
1406 format == NULL ||
1407 kwlist == NULL)
1408 {
1409 PyErr_BadInternalCall();
1410 return 0;
1411 }
1412
1413#ifdef VA_LIST_IS_ARRAY
1414 memcpy(lva, va, sizeof(va_list));
1415#else
1416#ifdef __va_copy
1417 __va_copy(lva, va);
1418#else
1419 lva = va;
1420#endif
1421#endif
1422
Martin v. Löwis18e16552006-02-15 17:27:45 +00001423 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1424 return retval;
1425}
1426
1427int
1428_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1429 PyObject *keywords,
1430 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001431 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001432{
1433 int retval;
1434 va_list lva;
1435
1436 if ((args == NULL || !PyTuple_Check(args)) ||
1437 (keywords != NULL && !PyDict_Check(keywords)) ||
1438 format == NULL ||
1439 kwlist == NULL)
1440 {
1441 PyErr_BadInternalCall();
1442 return 0;
1443 }
1444
1445#ifdef VA_LIST_IS_ARRAY
1446 memcpy(lva, va, sizeof(va_list));
1447#else
1448#ifdef __va_copy
1449 __va_copy(lva, va);
1450#else
1451 lva = va;
1452#endif
1453#endif
1454
1455 retval = vgetargskeywords(args, keywords, format,
1456 kwlist, &lva, FLAG_SIZE_T);
Brett Cannon711e7d92004-07-10 22:20:32 +00001457 return retval;
1458}
1459
Christian Heimesea837932008-02-26 17:23:51 +00001460#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001461
Guido van Rossumaa354651996-08-19 19:32:04 +00001462static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001463vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001464 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001465{
Tim Petersdc5eff92001-10-27 06:53:00 +00001466 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001467 int levels[32];
Christian Heimesea837932008-02-26 17:23:51 +00001468 const char *fname, *msg, *custom_msg, *keyword;
1469 int min = INT_MAX;
Tim Petersb639d492001-10-27 07:00:56 +00001470 int i, len, nargs, nkeywords;
Christian Heimesea837932008-02-26 17:23:51 +00001471 PyObject *freelist = NULL, *current_arg;
Tim Petersf4331c12001-10-27 00:17:34 +00001472
Tim Peters45772cd2001-10-27 03:58:40 +00001473 assert(args != NULL && PyTuple_Check(args));
1474 assert(keywords == NULL || PyDict_Check(keywords));
1475 assert(format != NULL);
1476 assert(kwlist != NULL);
1477 assert(p_va != NULL);
1478
Christian Heimesea837932008-02-26 17:23:51 +00001479 /* grab the function name or custom error msg first (mutually exclusive) */
1480 fname = strchr(format, ':');
1481 if (fname) {
1482 fname++;
1483 custom_msg = NULL;
Tim Peters62d48e12001-10-27 06:42:16 +00001484 }
Christian Heimesea837932008-02-26 17:23:51 +00001485 else {
1486 custom_msg = strchr(format,';');
1487 if (custom_msg)
1488 custom_msg++;
Tim Peters62d48e12001-10-27 06:42:16 +00001489 }
Christian Heimesea837932008-02-26 17:23:51 +00001490
1491 /* scan kwlist and get greatest possible nbr of args */
1492 for (len=0; kwlist[len]; len++)
1493 continue;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001494
Tim Peters6fb26352001-10-27 04:38:11 +00001495 nargs = PyTuple_GET_SIZE(args);
Christian Heimesea837932008-02-26 17:23:51 +00001496 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1497 if (nargs + nkeywords > len) {
1498 PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
1499 "argument%s (%d given)",
1500 (fname == NULL) ? "function" : fname,
1501 (fname == NULL) ? "" : "()",
1502 len,
1503 (len == 1) ? "" : "s",
1504 nargs + nkeywords);
Guido van Rossumaa354651996-08-19 19:32:04 +00001505 return 0;
1506 }
Tim Petersc2f01122001-10-27 07:25:06 +00001507
Christian Heimesea837932008-02-26 17:23:51 +00001508 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1509 for (i = 0; i < len; i++) {
1510 keyword = kwlist[i];
1511 if (*format == '|') {
1512 min = i;
Guido van Rossumaa354651996-08-19 19:32:04 +00001513 format++;
Christian Heimesea837932008-02-26 17:23:51 +00001514 }
1515 if (IS_END_OF_FORMAT(*format)) {
1516 PyErr_Format(PyExc_RuntimeError,
1517 "More keyword list entries (%d) than "
1518 "format specifiers (%d)", len, i);
1519 return cleanreturn(0, freelist);
1520 }
1521 current_arg = NULL;
1522 if (nkeywords) {
1523 current_arg = PyDict_GetItemString(keywords, keyword);
1524 }
1525 if (current_arg) {
1526 --nkeywords;
1527 if (i < nargs) {
1528 /* arg present in tuple and in dict */
1529 PyErr_Format(PyExc_TypeError,
1530 "Argument given by name ('%s') "
1531 "and position (%d)",
1532 keyword, i+1);
1533 return cleanreturn(0, freelist);
1534 }
1535 }
1536 else if (nkeywords && PyErr_Occurred())
1537 return cleanreturn(0, freelist);
1538 else if (i < nargs)
1539 current_arg = PyTuple_GET_ITEM(args, i);
1540
1541 if (current_arg) {
1542 msg = convertitem(current_arg, &format, p_va, flags,
1543 levels, msgbuf, sizeof(msgbuf), &freelist);
1544 if (msg) {
1545 seterror(i+1, msg, levels, fname, custom_msg);
1546 return cleanreturn(0, freelist);
1547 }
1548 continue;
1549 }
1550
1551 if (i < min) {
1552 PyErr_Format(PyExc_TypeError, "Required argument "
1553 "'%s' (pos %d) not found",
1554 keyword, i+1);
1555 return cleanreturn(0, freelist);
1556 }
1557 /* current code reports success when all required args
1558 * fulfilled and no keyword args left, with no further
1559 * validation. XXX Maybe skip this in debug build ?
1560 */
1561 if (!nkeywords)
1562 return cleanreturn(1, freelist);
1563
1564 /* We are into optional args, skip thru to any remaining
1565 * keyword args */
1566 msg = skipitem(&format, p_va, flags);
Guido van Rossumaa354651996-08-19 19:32:04 +00001567 if (msg) {
Christian Heimesea837932008-02-26 17:23:51 +00001568 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1569 format);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001570 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001571 }
1572 }
1573
Christian Heimesea837932008-02-26 17:23:51 +00001574 if (!IS_END_OF_FORMAT(*format)) {
1575 PyErr_Format(PyExc_RuntimeError,
1576 "more argument specifiers than keyword list entries "
1577 "(remaining format:'%s')", format);
1578 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001579 }
Tim Petersb054be42001-10-27 05:07:41 +00001580
Guido van Rossumaa354651996-08-19 19:32:04 +00001581 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001582 if (nkeywords > 0) {
1583 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001584 Py_ssize_t pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001585 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001586 int match = 0;
Guido van Rossum55474762002-04-04 16:22:30 +00001587 char *ks;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001588 if (!PyString_Check(key)) {
Guido van Rossum55474762002-04-04 16:22:30 +00001589 PyErr_SetString(PyExc_TypeError,
1590 "keywords must be strings");
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001591 return cleanreturn(0, freelist);
Guido van Rossum55474762002-04-04 16:22:30 +00001592 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001593 ks = PyString_AsString(key);
Christian Heimesea837932008-02-26 17:23:51 +00001594 for (i = 0; i < len; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001595 if (!strcmp(ks, kwlist[i])) {
1596 match = 1;
1597 break;
1598 }
1599 }
1600 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001601 PyErr_Format(PyExc_TypeError,
1602 "'%s' is an invalid keyword "
1603 "argument for this function",
1604 ks);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001605 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001606 }
1607 }
1608 }
Tim Petersc2f01122001-10-27 07:25:06 +00001609
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001610 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001611}
1612
1613
1614static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001615skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001616{
Christian Heimesea837932008-02-26 17:23:51 +00001617 const char *format = *p_format;
Guido van Rossumaa354651996-08-19 19:32:04 +00001618 char c = *format++;
1619
1620 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001621
1622 /* simple codes
1623 * The individual types (second arg of va_arg) are irrelevant */
1624
Guido van Rossumaa354651996-08-19 19:32:04 +00001625 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001626 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001627 case 'h': /* short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001628 case 'H': /* short int as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001629 case 'i': /* int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001630 case 'I': /* int sized bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001631 case 'l': /* long int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001632 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001633#ifdef HAVE_LONG_LONG
Georg Brandl6dd14612005-09-14 19:29:53 +00001634 case 'L': /* PY_LONG_LONG */
1635 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001636#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001637 case 'f': /* float */
Guido van Rossumaa354651996-08-19 19:32:04 +00001638 case 'd': /* double */
Guido van Rossumaa354651996-08-19 19:32:04 +00001639#ifndef WITHOUT_COMPLEX
1640 case 'D': /* complex double */
Georg Brandl6dd14612005-09-14 19:29:53 +00001641#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001642 case 'c': /* char */
1643 {
Georg Brandl6dd14612005-09-14 19:29:53 +00001644 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001645 break;
1646 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001647
1648 case 'n': /* Py_ssize_t */
1649 {
1650 (void) va_arg(*p_va, Py_ssize_t *);
1651 break;
1652 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001653
Georg Brandl6dd14612005-09-14 19:29:53 +00001654 /* string codes */
1655
1656 case 'e': /* string with encoding */
1657 {
1658 (void) va_arg(*p_va, const char *);
1659 if (!(*format == 's' || *format == 't'))
1660 /* after 'e', only 's' and 't' is allowed */
1661 goto err;
1662 format++;
1663 /* explicit fallthrough to string cases */
1664 }
1665
Guido van Rossumaa354651996-08-19 19:32:04 +00001666 case 's': /* string */
Georg Brandl6dd14612005-09-14 19:29:53 +00001667 case 'z': /* string or None */
1668#ifdef Py_USING_UNICODE
1669 case 'u': /* unicode string */
1670#endif
1671 case 't': /* buffer, read-only */
1672 case 'w': /* buffer, read-write */
Guido van Rossumaa354651996-08-19 19:32:04 +00001673 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001674 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001675 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001676 if (flags & FLAG_SIZE_T)
1677 (void) va_arg(*p_va, Py_ssize_t *);
1678 else
1679 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001680 format++;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001681 } else if ((c == 's' || c == 'z') && *format == '*') {
1682 format++;
Guido van Rossumaa354651996-08-19 19:32:04 +00001683 }
1684 break;
1685 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001686
1687 /* object codes */
1688
Guido van Rossumaa354651996-08-19 19:32:04 +00001689 case 'S': /* string object */
Georg Brandl6dd14612005-09-14 19:29:53 +00001690#ifdef Py_USING_UNICODE
1691 case 'U': /* unicode string object */
1692#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001693 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001694 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001695 break;
1696 }
1697
1698 case 'O': /* object */
1699 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001700 if (*format == '!') {
1701 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001702 (void) va_arg(*p_va, PyTypeObject*);
1703 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001704 }
1705#if 0
1706/* I don't know what this is for */
1707 else if (*format == '?') {
1708 inquiry pred = va_arg(*p_va, inquiry);
1709 format++;
1710 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001711 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001712 }
1713 }
1714#endif
1715 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001716 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001717 (void) va_arg(*p_va, converter);
1718 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001719 format++;
1720 }
1721 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001722 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001723 }
1724 break;
1725 }
Christian Heimesea837932008-02-26 17:23:51 +00001726
1727 case '(': /* bypass tuple, not handled at all previously */
1728 {
1729 char *msg;
1730 for (;;) {
1731 if (*format==')')
1732 break;
1733 if (IS_END_OF_FORMAT(*format))
1734 return "Unmatched left paren in format "
1735 "string";
1736 msg = skipitem(&format, p_va, flags);
1737 if (msg)
1738 return msg;
1739 }
1740 format++;
1741 break;
1742 }
1743
1744 case ')':
1745 return "Unmatched right paren in format string";
1746
Guido van Rossumaa354651996-08-19 19:32:04 +00001747 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001748err:
Guido van Rossumaa354651996-08-19 19:32:04 +00001749 return "impossible<bad format char>";
1750
1751 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001752
Guido van Rossumaa354651996-08-19 19:32:04 +00001753 *p_format = format;
1754 return NULL;
1755}
Fred Drakee4616e62001-10-23 21:09:29 +00001756
1757
1758int
Martin v. Löwis76246742006-03-01 04:06:10 +00001759PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001760{
Martin v. Löwis76246742006-03-01 04:06:10 +00001761 Py_ssize_t i, l;
Fred Drakee4616e62001-10-23 21:09:29 +00001762 PyObject **o;
1763 va_list vargs;
1764
1765#ifdef HAVE_STDARG_PROTOTYPES
1766 va_start(vargs, max);
1767#else
1768 va_start(vargs);
1769#endif
1770
1771 assert(min >= 0);
1772 assert(min <= max);
1773 if (!PyTuple_Check(args)) {
1774 PyErr_SetString(PyExc_SystemError,
1775 "PyArg_UnpackTuple() argument list is not a tuple");
1776 return 0;
1777 }
1778 l = PyTuple_GET_SIZE(args);
1779 if (l < min) {
1780 if (name != NULL)
1781 PyErr_Format(
1782 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001783 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001784 name, (min == max ? "" : "at least "), min, l);
1785 else
1786 PyErr_Format(
1787 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001788 "unpacked tuple should have %s%zd elements,"
1789 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001790 (min == max ? "" : "at least "), min, l);
1791 va_end(vargs);
1792 return 0;
1793 }
1794 if (l > max) {
1795 if (name != NULL)
1796 PyErr_Format(
1797 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001798 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001799 name, (min == max ? "" : "at most "), max, l);
1800 else
1801 PyErr_Format(
1802 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001803 "unpacked tuple should have %s%zd elements,"
1804 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001805 (min == max ? "" : "at most "), max, l);
1806 va_end(vargs);
1807 return 0;
1808 }
1809 for (i = 0; i < l; i++) {
1810 o = va_arg(vargs, PyObject **);
1811 *o = PyTuple_GET_ITEM(args, i);
1812 }
1813 va_end(vargs);
1814 return 1;
1815}
Georg Brandl02c42872005-08-26 06:42:30 +00001816
1817
1818/* For type constructors that don't take keyword args
1819 *
1820 * Sets a TypeError and returns 0 if the kwds dict is
Walter Dörwaldd14bf612006-09-21 15:09:55 +00001821 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001822 */
1823int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001824_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001825{
1826 if (kw == NULL)
1827 return 1;
1828 if (!PyDict_CheckExact(kw)) {
1829 PyErr_BadInternalCall();
1830 return 0;
1831 }
1832 if (PyDict_Size(kw) == 0)
1833 return 1;
1834
1835 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1836 funcname);
1837 return 0;
1838}
Anthony Baxter97300382006-04-12 04:38:54 +00001839#ifdef __cplusplus
1840};
1841#endif