blob: b112650910ce9b7a76efc91a7ab6d0b6f3981a86 [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
Larry Hastings402b73f2010-03-25 00:54:54 +0000142#define GETARGS_CAPSULE_NAME_CLEANUP_PTR "getargs.cleanup_ptr"
143#define GETARGS_CAPSULE_NAME_CLEANUP_BUFFER "getargs.cleanup_buffer"
144
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000145static void
Larry Hastings402b73f2010-03-25 00:54:54 +0000146cleanup_ptr(PyObject *self)
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000147{
Larry Hastings402b73f2010-03-25 00:54:54 +0000148 void *ptr = PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_PTR);
149 if (ptr) {
150 PyMem_FREE(ptr);
151 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000152}
153
154static void
Larry Hastings402b73f2010-03-25 00:54:54 +0000155cleanup_buffer(PyObject *self)
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000156{
Larry Hastings402b73f2010-03-25 00:54:54 +0000157 Py_buffer *ptr = (Py_buffer *)PyCapsule_GetPointer(self, GETARGS_CAPSULE_NAME_CLEANUP_BUFFER);
158 if (ptr) {
159 PyBuffer_Release(ptr);
160 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000161}
162
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000163static int
Larry Hastings402b73f2010-03-25 00:54:54 +0000164addcleanup(void *ptr, PyObject **freelist, PyCapsule_Destructor destr)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000165{
166 PyObject *cobj;
Larry Hastings402b73f2010-03-25 00:54:54 +0000167 const char *name;
168
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000169 if (!*freelist) {
170 *freelist = PyList_New(0);
171 if (!*freelist) {
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000172 destr(ptr);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000173 return -1;
174 }
175 }
Larry Hastings402b73f2010-03-25 00:54:54 +0000176
177 if (destr == cleanup_ptr) {
178 name = GETARGS_CAPSULE_NAME_CLEANUP_PTR;
179 } else if (destr == cleanup_buffer) {
180 name = GETARGS_CAPSULE_NAME_CLEANUP_BUFFER;
181 } else {
182 return -1;
183 }
184 cobj = PyCapsule_New(ptr, name, destr);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000185 if (!cobj) {
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000186 destr(ptr);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000187 return -1;
188 }
Neal Norwitzdf6ac3d2008-02-26 05:23:51 +0000189 if (PyList_Append(*freelist, cobj)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000190 Py_DECREF(cobj);
191 return -1;
192 }
193 Py_DECREF(cobj);
194 return 0;
195}
196
197static int
198cleanreturn(int retval, PyObject *freelist)
199{
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000200 if (freelist && retval != 0) {
201 /* We were successful, reset the destructors so that they
202 don't get called. */
203 Py_ssize_t len = PyList_GET_SIZE(freelist), i;
204 for (i = 0; i < len; i++)
Larry Hastings402b73f2010-03-25 00:54:54 +0000205 PyCapsule_SetDestructor(PyList_GET_ITEM(freelist, i), NULL);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000206 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000207 Py_XDECREF(freelist);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000208 return retval;
209}
210
211
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000212static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000213vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000214{
215 char msgbuf[256];
216 int levels[32];
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000217 const char *fname = NULL;
218 const char *message = NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000219 int min = -1;
220 int max = 0;
221 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000222 int endfmt = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000223 const char *formatsave = format;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000224 Py_ssize_t i, len;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000225 char *msg;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000226 PyObject *freelist = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000227 int compat = flags & FLAG_COMPAT;
228
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000229 assert(compat || (args != (PyObject*)NULL));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000230 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000231
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000232 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000233 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000234 switch (c) {
235 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000236 if (level == 0)
237 max++;
238 level++;
Georg Brandl209307e2006-08-09 07:03:22 +0000239 if (level >= 30)
240 Py_FatalError("too many tuple nesting levels "
241 "in argument format string");
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000242 break;
243 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000244 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000245 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000246 else
247 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000248 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000249 case '\0':
250 endfmt = 1;
251 break;
252 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000253 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000254 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000255 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000256 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000257 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000258 endfmt = 1;
259 break;
260 default:
261 if (level == 0) {
262 if (c == 'O')
263 max++;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000264 else if (isalpha(Py_CHARMASK(c))) {
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000265 if (c != 'e') /* skip encoded */
266 max++;
267 } else if (c == '|')
268 min = max;
269 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000270 break;
271 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000272 }
273
274 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000275 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000276
277 if (min < 0)
278 min = max;
279
280 format = formatsave;
281
282 if (compat) {
283 if (max == 0) {
284 if (args == NULL)
285 return 1;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000286 PyOS_snprintf(msgbuf, sizeof(msgbuf),
287 "%.200s%s takes no arguments",
288 fname==NULL ? "function" : fname,
289 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000290 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000291 return 0;
292 }
293 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000294 if (args == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000295 PyOS_snprintf(msgbuf, sizeof(msgbuf),
296 "%.200s%s takes at least one argument",
297 fname==NULL ? "function" : fname,
298 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000299 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000300 return 0;
301 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000302 msg = convertitem(args, &format, p_va, flags, levels,
303 msgbuf, sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000304 if (msg == NULL)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000305 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000306 seterror(levels[0], msg, levels+1, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000307 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000308 }
309 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000310 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000311 "old style getargs format uses new features");
312 return 0;
313 }
314 }
315
Guido van Rossum79f25d91997-04-29 20:08:16 +0000316 if (!PyTuple_Check(args)) {
317 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000318 "new style getargs format but argument is not a tuple");
319 return 0;
320 }
321
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000322 len = PyTuple_GET_SIZE(args);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000323
324 if (len < min || max < len) {
325 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000326 PyOS_snprintf(msgbuf, sizeof(msgbuf),
327 "%.150s%s takes %s %d argument%s "
Neal Norwitz20dd93f2006-02-19 19:34:15 +0000328 "(%ld given)",
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000329 fname==NULL ? "function" : fname,
330 fname==NULL ? "" : "()",
331 min==max ? "exactly"
332 : len < min ? "at least" : "at most",
333 len < min ? min : max,
334 (len < min ? min : max) == 1 ? "" : "s",
Neal Norwitz9a276172006-02-20 18:57:39 +0000335 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000336 message = msgbuf;
337 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000338 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000339 return 0;
340 }
341
342 for (i = 0; i < len; i++) {
343 if (*format == '|')
344 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000345 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000346 flags, levels, msgbuf,
347 sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000348 if (msg) {
Sean Reifscheider9279e7d2009-08-01 23:54:55 +0000349 seterror(i+1, msg, levels, fname, msg);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000350 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000351 }
352 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000353
Neal Norwitz4ac13df2005-12-19 06:10:07 +0000354 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000355 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000356 *format != '|' && *format != ':' && *format != ';') {
357 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000358 "bad format string: %.200s", formatsave);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000359 return cleanreturn(0, freelist);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000360 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000361
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000362 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000363}
364
365
366
367static void
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000368seterror(int iarg, const char *msg, int *levels, const char *fname,
369 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000370{
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000371 char buf[512];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000372 int i;
373 char *p = buf;
374
Guido van Rossum79f25d91997-04-29 20:08:16 +0000375 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000376 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000377 else if (message == NULL) {
378 if (fname != NULL) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000379 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000380 p += strlen(p);
381 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000382 if (iarg != 0) {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000383 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000384 "argument %d", iarg);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000385 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000386 p += strlen(p);
Georg Brandl5f135782006-07-26 08:03:10 +0000387 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
388 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000389 ", item %d", levels[i]-1);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000390 p += strlen(p);
391 i++;
392 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000393 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000394 else {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000395 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
Ka-Ping Yee20579702001-01-15 22:14:16 +0000396 p += strlen(p);
397 }
Tim Petersfaad5ad2001-12-03 00:43:33 +0000398 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000399 message = buf;
400 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000401 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000402}
403
404
405/* Convert a tuple argument.
406 On entry, *p_format points to the character _after_ the opening '('.
407 On successful exit, *p_format points to the closing ')'.
408 If successful:
409 *p_format and *p_va are updated,
410 *levels and *msgbuf are untouched,
411 and NULL is returned.
412 If the argument is invalid:
413 *p_format is unchanged,
414 *p_va is undefined,
415 *levels is a 0-terminated list of item numbers,
416 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000417 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000418 <typename1> is the name of the expected type, and
419 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000420 and msgbuf is returned.
421*/
422
423static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000424converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
425 int *levels, char *msgbuf, size_t bufsize, int toplevel,
426 PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000427{
428 int level = 0;
429 int n = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000430 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000431 int i;
432
433 for (;;) {
434 int c = *format++;
435 if (c == '(') {
436 if (level == 0)
437 n++;
438 level++;
439 }
440 else if (c == ')') {
441 if (level == 0)
442 break;
443 level--;
444 }
445 else if (c == ':' || c == ';' || c == '\0')
446 break;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000447 else if (level == 0 && isalpha(Py_CHARMASK(c)))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000448 n++;
449 }
450
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000451 if (!PySequence_Check(arg) || PyString_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000452 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000453 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000454 toplevel ? "expected %d arguments, not %.50s" :
455 "must be %d-item sequence, not %.50s",
456 n,
457 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000458 return msgbuf;
459 }
460
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000461 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000462 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000463 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000464 toplevel ? "expected %d arguments, not %d" :
465 "must be sequence of length %d, not %d",
466 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000467 return msgbuf;
468 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000469
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000470 format = *p_format;
471 for (i = 0; i < n; i++) {
472 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000473 PyObject *item;
474 item = PySequence_GetItem(arg, i);
Georg Brandl5f135782006-07-26 08:03:10 +0000475 if (item == NULL) {
476 PyErr_Clear();
477 levels[0] = i+1;
478 levels[1] = 0;
479 strncpy(msgbuf, "is not retrievable", bufsize);
480 return msgbuf;
481 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482 msg = convertitem(item, &format, p_va, flags, levels+1,
483 msgbuf, bufsize, freelist);
Guido van Rossum66368cc1999-02-17 23:16:43 +0000484 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
485 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000486 if (msg != NULL) {
487 levels[0] = i+1;
488 return msg;
489 }
490 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000491
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000492 *p_format = format;
493 return NULL;
494}
495
496
497/* Convert a single item. */
498
499static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000500convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
501 int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000502{
503 char *msg;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000504 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000505
506 if (*format == '(' /* ')' */) {
507 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000508 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000509 bufsize, 0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000510 if (msg == NULL)
511 format++;
512 }
513 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000514 msg = convertsimple(arg, &format, p_va, flags,
515 msgbuf, bufsize, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000516 if (msg != NULL)
517 levels[0] = 0;
518 }
519 if (msg == NULL)
520 *p_format = format;
521 return msg;
522}
523
524
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000525
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000526#define UNICODE_DEFAULT_ENCODING(arg) \
527 _PyUnicode_AsDefaultEncodedString(arg, NULL)
528
529/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000530
531static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000532converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000533{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000534 assert(expected != NULL);
535 assert(arg != NULL);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000536 PyOS_snprintf(msgbuf, bufsize,
537 "must be %.50s, not %.50s", expected,
538 arg == Py_None ? "None" : arg->ob_type->tp_name);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000539 return msgbuf;
540}
541
542#define CONV_UNICODE "(unicode conversion error)"
543
Neil Schemenauer5042da62003-02-04 20:59:40 +0000544/* explicitly check for float arguments when integers are expected. For now
545 * signal a warning. Returns true if an exception was raised. */
546static int
Mark Dickinson1b34d252010-01-01 17:27:30 +0000547float_argument_warning(PyObject *arg)
Neil Schemenauer5042da62003-02-04 20:59:40 +0000548{
549 if (PyFloat_Check(arg) &&
550 PyErr_Warn(PyExc_DeprecationWarning,
551 "integer argument expected, got float" ))
552 return 1;
553 else
554 return 0;
555}
556
Mark Dickinson1b34d252010-01-01 17:27:30 +0000557/* explicitly check for float arguments when integers are expected. Raises
558 TypeError and returns true for float arguments. */
559static int
560float_argument_error(PyObject *arg)
561{
562 if (PyFloat_Check(arg)) {
563 PyErr_SetString(PyExc_TypeError,
564 "integer argument expected, got float");
565 return 1;
566 }
567 else
568 return 0;
569}
570
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000571/* Convert a non-tuple argument. Return NULL if conversion went OK,
572 or a string with a message describing the failure. The message is
573 formatted as "must be <desired type>, not <actual type>".
574 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000575 Don't call if a tuple is expected.
576
577 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000578*/
579
580static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000581convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000582 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000583{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000584 /* For # codes */
585#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
586 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
587 else q=va_arg(*p_va, int*);
588#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
589#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
590
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000591 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000592 char c = *format++;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000593#ifdef Py_USING_UNICODE
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000594 PyObject *uarg;
Walter Dörwalddffda2e2002-11-21 20:23:11 +0000595#endif
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000596
597 switch (c) {
598
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000599 case 'b': { /* unsigned byte -- very short int */
600 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000601 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000602 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000603 return converterr("integer<b>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000604 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000605 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000606 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000607 else if (ival < 0) {
608 PyErr_SetString(PyExc_OverflowError,
609 "unsigned byte integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000610 return converterr("integer<b>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000611 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000612 else if (ival > UCHAR_MAX) {
613 PyErr_SetString(PyExc_OverflowError,
614 "unsigned byte integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000615 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000616 }
617 else
618 *p = (unsigned char) ival;
619 break;
620 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000621
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000622 case 'B': {/* byte sized bitfield - both signed and unsigned
623 values allowed */
624 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000625 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000626 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000627 return converterr("integer<B>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000628 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000629 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000630 return converterr("integer<B>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000631 else
632 *p = (unsigned char) ival;
633 break;
634 }
Jack Jansencc22fbe2000-08-05 21:29:58 +0000635
Guido van Rossumfce26e72003-04-18 00:12:30 +0000636 case 'h': {/* signed short int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000637 short *p = va_arg(*p_va, short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000638 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000639 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000640 return converterr("integer<h>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000641 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000642 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000643 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfce26e72003-04-18 00:12:30 +0000644 else if (ival < SHRT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000645 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000646 "signed short integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000647 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000648 }
Guido van Rossumfce26e72003-04-18 00:12:30 +0000649 else if (ival > SHRT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000650 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000651 "signed short integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000652 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000653 }
654 else
655 *p = (short) ival;
656 break;
657 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000658
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000659 case 'H': { /* short int sized bitfield, both signed and
660 unsigned allowed */
661 unsigned short *p = va_arg(*p_va, unsigned short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000662 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000663 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000664 return converterr("integer<H>", arg, msgbuf, bufsize);
Thomas Hellera4ea6032003-04-17 18:55:45 +0000665 ival = PyInt_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000666 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000667 return converterr("integer<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000668 else
669 *p = (unsigned short) ival;
670 break;
671 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000672
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000673 case 'i': {/* signed int */
674 int *p = va_arg(*p_va, int *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000675 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000676 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000677 return converterr("integer<i>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000678 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000679 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000680 return converterr("integer<i>", arg, msgbuf, bufsize);
Georg Brandl98251f82006-06-08 13:31:07 +0000681 else if (ival > INT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000682 PyErr_SetString(PyExc_OverflowError,
683 "signed integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000684 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000685 }
Georg Brandl98251f82006-06-08 13:31:07 +0000686 else if (ival < INT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000687 PyErr_SetString(PyExc_OverflowError,
688 "signed integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000689 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000690 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000691 else
692 *p = ival;
693 break;
694 }
695
Thomas Hellera4ea6032003-04-17 18:55:45 +0000696 case 'I': { /* int sized bitfield, both signed and
697 unsigned allowed */
698 unsigned int *p = va_arg(*p_va, unsigned int *);
699 unsigned int ival;
700 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000701 return converterr("integer<I>", arg, msgbuf, bufsize);
Skip Montanarob5079722006-04-18 00:57:15 +0000702 ival = (unsigned int)PyInt_AsUnsignedLongMask(arg);
703 if (ival == (unsigned int)-1 && PyErr_Occurred())
Thomas Hellera4ea6032003-04-17 18:55:45 +0000704 return converterr("integer<I>", arg, msgbuf, bufsize);
705 else
706 *p = ival;
707 break;
708 }
709
Martin v. Löwis18e16552006-02-15 17:27:45 +0000710 case 'n': /* Py_ssize_t */
711#if SIZEOF_SIZE_T != SIZEOF_LONG
712 {
713 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
714 Py_ssize_t ival;
715 if (float_argument_error(arg))
Georg Brandl7f573f72006-04-13 07:59:30 +0000716 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000717 ival = PyInt_AsSsize_t(arg);
718 if (ival == -1 && PyErr_Occurred())
Georg Brandl7f573f72006-04-13 07:59:30 +0000719 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000720 *p = ival;
721 break;
722 }
723#endif
724 /* Fall through from 'n' to 'l' if Py_ssize_t is int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000725 case 'l': {/* long int */
726 long *p = va_arg(*p_va, long *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000727 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000728 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000729 return converterr("integer<l>", arg, msgbuf, bufsize);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000730 ival = PyInt_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000731 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000732 return converterr("integer<l>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000733 else
734 *p = ival;
735 break;
736 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000737
738 case 'k': { /* long sized bitfield */
739 unsigned long *p = va_arg(*p_va, unsigned long *);
740 unsigned long ival;
741 if (PyInt_Check(arg))
742 ival = PyInt_AsUnsignedLongMask(arg);
743 else if (PyLong_Check(arg))
744 ival = PyLong_AsUnsignedLongMask(arg);
745 else
746 return converterr("integer<k>", arg, msgbuf, bufsize);
747 *p = ival;
748 break;
749 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000750
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000751#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000752 case 'L': {/* PY_LONG_LONG */
753 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
Mark Dickinson1b34d252010-01-01 17:27:30 +0000754 PY_LONG_LONG ival;
755 if (float_argument_warning(arg))
756 return converterr("long<L>", arg, msgbuf, bufsize);
757 ival = PyLong_AsLongLong(arg);
Neal Norwitzdf6ac3d2008-02-26 05:23:51 +0000758 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000759 return converterr("long<L>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000760 } else {
761 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000762 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000763 break;
764 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000765
766 case 'K': { /* long long sized bitfield */
767 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
768 unsigned PY_LONG_LONG ival;
Thomas Hellera4ea6032003-04-17 18:55:45 +0000769 if (PyInt_Check(arg))
770 ival = PyInt_AsUnsignedLongMask(arg);
771 else if (PyLong_Check(arg))
772 ival = PyLong_AsUnsignedLongLongMask(arg);
773 else
774 return converterr("integer<K>", arg, msgbuf, bufsize);
775 *p = ival;
776 break;
777 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000778#endif
779
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000780 case 'f': {/* float */
781 float *p = va_arg(*p_va, float *);
782 double dval = PyFloat_AsDouble(arg);
783 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000784 return converterr("float<f>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000785 else
786 *p = (float) dval;
787 break;
788 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000789
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000790 case 'd': {/* double */
791 double *p = va_arg(*p_va, double *);
792 double dval = PyFloat_AsDouble(arg);
793 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000794 return converterr("float<d>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000795 else
796 *p = dval;
797 break;
798 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000799
Guido van Rossum530956d1996-07-21 02:27:43 +0000800#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000801 case 'D': {/* complex double */
802 Py_complex *p = va_arg(*p_va, Py_complex *);
803 Py_complex cval;
804 cval = PyComplex_AsCComplex(arg);
805 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000806 return converterr("complex<D>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000807 else
808 *p = cval;
809 break;
810 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000811#endif /* WITHOUT_COMPLEX */
Guido van Rossum8a5c5d21996-01-12 01:09:56 +0000812
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000813 case 'c': {/* char */
814 char *p = va_arg(*p_va, char *);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000815 if (PyString_Check(arg) && PyString_Size(arg) == 1)
816 *p = PyString_AS_STRING(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000817 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000818 return converterr("char", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000819 break;
820 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000821
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000822 case 's': {/* string */
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000823 if (*format == '*') {
824 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
825
826 if (PyString_Check(arg)) {
827 PyBuffer_FillInfo(p, arg,
828 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
829 1, 0);
830 }
831#ifdef Py_USING_UNICODE
832 else if (PyUnicode_Check(arg)) {
833 uarg = UNICODE_DEFAULT_ENCODING(arg);
834 if (uarg == NULL)
835 return converterr(CONV_UNICODE,
836 arg, msgbuf, bufsize);
837 PyBuffer_FillInfo(p, arg,
838 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
839 1, 0);
840 }
841#endif
842 else { /* any buffer-like object */
843 char *buf;
844 if (getbuffer(arg, p, &buf) < 0)
845 return converterr(buf, arg, msgbuf, bufsize);
846 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000847 if (addcleanup(p, freelist, cleanup_buffer)) {
848 return converterr(
849 "(cleanup problem)",
850 arg, msgbuf, bufsize);
851 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000852 format++;
853 } else if (*format == '#') {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000854 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000855 FETCH_SIZE;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000856
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000857 if (PyString_Check(arg)) {
858 *p = PyString_AS_STRING(arg);
859 STORE_SIZE(PyString_GET_SIZE(arg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000860 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000861#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000862 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000863 uarg = UNICODE_DEFAULT_ENCODING(arg);
864 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000865 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000866 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000867 *p = PyString_AS_STRING(uarg);
868 STORE_SIZE(PyString_GET_SIZE(uarg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000869 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000870#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000871 else { /* any buffer-like object */
872 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000873 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000874 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000875 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000876 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000877 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000878 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000879 } else {
880 char **p = va_arg(*p_va, char **);
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000881
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000882 if (PyString_Check(arg))
883 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000884#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000885 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000886 uarg = UNICODE_DEFAULT_ENCODING(arg);
887 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000888 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000889 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000890 *p = PyString_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000891 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000892#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000893 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000894 return converterr("string", arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000895 if ((Py_ssize_t)strlen(*p) != PyString_Size(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000896 return converterr("string without null bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000897 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000898 }
899 break;
900 }
901
902 case 'z': {/* string, may be NULL (None) */
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000903 if (*format == '*') {
904 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
905
906 if (arg == Py_None)
907 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
908 else if (PyString_Check(arg)) {
909 PyBuffer_FillInfo(p, arg,
910 PyString_AS_STRING(arg), PyString_GET_SIZE(arg),
911 1, 0);
912 }
913#ifdef Py_USING_UNICODE
914 else if (PyUnicode_Check(arg)) {
915 uarg = UNICODE_DEFAULT_ENCODING(arg);
916 if (uarg == NULL)
917 return converterr(CONV_UNICODE,
918 arg, msgbuf, bufsize);
919 PyBuffer_FillInfo(p, arg,
920 PyString_AS_STRING(uarg), PyString_GET_SIZE(uarg),
921 1, 0);
922 }
923#endif
924 else { /* any buffer-like object */
925 char *buf;
926 if (getbuffer(arg, p, &buf) < 0)
927 return converterr(buf, arg, msgbuf, bufsize);
928 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +0000929 if (addcleanup(p, freelist, cleanup_buffer)) {
930 return converterr(
931 "(cleanup problem)",
932 arg, msgbuf, bufsize);
933 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +0000934 format++;
935 } else if (*format == '#') { /* any buffer-like object */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000936 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000937 FETCH_SIZE;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000938
939 if (arg == Py_None) {
940 *p = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000941 STORE_SIZE(0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000942 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000943 else if (PyString_Check(arg)) {
944 *p = PyString_AS_STRING(arg);
945 STORE_SIZE(PyString_GET_SIZE(arg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000946 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000947#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000948 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000949 uarg = UNICODE_DEFAULT_ENCODING(arg);
950 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000951 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000952 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000953 *p = PyString_AS_STRING(uarg);
954 STORE_SIZE(PyString_GET_SIZE(uarg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000955 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000956#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000957 else { /* any buffer-like object */
958 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000959 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000960 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000961 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000962 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000963 }
964 format++;
965 } else {
966 char **p = va_arg(*p_va, char **);
967
968 if (arg == Py_None)
969 *p = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000970 else if (PyString_Check(arg))
971 *p = PyString_AS_STRING(arg);
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000972#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000973 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000974 uarg = UNICODE_DEFAULT_ENCODING(arg);
975 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000976 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000977 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000978 *p = PyString_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000979 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000980#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000981 else
982 return converterr("string or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000983 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000984 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000985 FETCH_SIZE;
Thomas Woutersc3547a32006-03-01 21:31:21 +0000986 assert(0); /* XXX redundant with if-case */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000987 if (arg == Py_None)
988 *q = 0;
989 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000990 *q = PyString_Size(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000991 format++;
992 }
993 else if (*p != NULL &&
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000994 (Py_ssize_t)strlen(*p) != PyString_Size(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000995 return converterr(
996 "string without null bytes or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000997 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000998 }
999 break;
1000 }
1001
1002 case 'e': {/* encoded string */
1003 char **buffer;
1004 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001005 PyObject *s;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +00001006 Py_ssize_t size;
1007 int recode_strings;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001008
1009 /* Get 'e' parameter: the encoding name */
1010 encoding = (const char *)va_arg(*p_va, const char *);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001011#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001012 if (encoding == NULL)
1013 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001014#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001015
1016 /* Get output buffer parameter:
1017 's' (recode all objects via Unicode) or
1018 't' (only recode non-string objects)
1019 */
1020 if (*format == 's')
1021 recode_strings = 1;
1022 else if (*format == 't')
1023 recode_strings = 0;
1024 else
1025 return converterr(
1026 "(unknown parser marker combination)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001027 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001028 buffer = (char **)va_arg(*p_va, char **);
1029 format++;
1030 if (buffer == NULL)
1031 return converterr("(buffer is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001032 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001033
1034 /* Encode object */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001035 if (!recode_strings && PyString_Check(arg)) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001036 s = arg;
1037 Py_INCREF(s);
1038 }
1039 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001040#ifdef Py_USING_UNICODE
1041 PyObject *u;
1042
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001043 /* Convert object to Unicode */
1044 u = PyUnicode_FromObject(arg);
1045 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001046 return converterr(
1047 "string or unicode or text buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001048 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001049
1050 /* Encode object; use default error handling */
1051 s = PyUnicode_AsEncodedString(u,
1052 encoding,
1053 NULL);
1054 Py_DECREF(u);
1055 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001056 return converterr("(encoding failed)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001057 arg, msgbuf, bufsize);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001058 if (!PyString_Check(s)) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001059 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001060 return converterr(
1061 "(encoder failed to return a string)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001062 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001063 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001064#else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001065 return converterr("string<e>", arg, msgbuf, bufsize);
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001066#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001067 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001068 size = PyString_GET_SIZE(s);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001069
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001070 /* Write output; output is guaranteed to be 0-terminated */
1071 if (*format == '#') {
1072 /* Using buffer length parameter '#':
1073
1074 - if *buffer is NULL, a new buffer of the
1075 needed size is allocated and the data
1076 copied into it; *buffer is updated to point
1077 to the new buffer; the caller is
1078 responsible for PyMem_Free()ing it after
1079 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001080
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001081 - if *buffer is not NULL, the data is
1082 copied to *buffer; *buffer_len has to be
1083 set to the size of the buffer on input;
1084 buffer overflow is signalled with an error;
1085 buffer has to provide enough room for the
1086 encoded string plus the trailing 0-byte
1087
1088 - in both cases, *buffer_len is updated to
1089 the size of the buffer /excluding/ the
1090 trailing 0-byte
1091
1092 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001093 FETCH_SIZE;
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001094
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001095 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001096 if (q == NULL && q2 == NULL) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001097 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001098 return converterr(
1099 "(buffer_len is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001100 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001101 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001102 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001103 *buffer = PyMem_NEW(char, size + 1);
1104 if (*buffer == NULL) {
1105 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001106 return converterr(
1107 "(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001108 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001109 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +00001110 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001111 Py_DECREF(s);
1112 return converterr(
1113 "(cleanup problem)",
1114 arg, msgbuf, bufsize);
1115 }
Fred Drake25871c02000-05-03 15:17:02 +00001116 } else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001117 if (size + 1 > BUFFER_LEN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001118 Py_DECREF(s);
1119 return converterr(
1120 "(buffer overflow)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001121 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001122 }
Fred Drake25871c02000-05-03 15:17:02 +00001123 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001124 memcpy(*buffer,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001125 PyString_AS_STRING(s),
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001126 size + 1);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001127 STORE_SIZE(size);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001128 } else {
1129 /* Using a 0-terminated buffer:
1130
1131 - the encoded string has to be 0-terminated
1132 for this variant to work; if it is not, an
1133 error raised
Fred Drake25871c02000-05-03 15:17:02 +00001134
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001135 - a new buffer of the needed size is
1136 allocated and the data copied into it;
1137 *buffer is updated to point to the new
1138 buffer; the caller is responsible for
1139 PyMem_Free()ing it after usage
1140
1141 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001142 if ((Py_ssize_t)strlen(PyString_AS_STRING(s))
Armin Rigo7ccbca92006-10-04 12:17:45 +00001143 != size) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001144 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001145 return converterr(
Georg Brandl6c59e722009-04-05 11:54:07 +00001146 "encoded string without NULL bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001147 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001148 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001149 *buffer = PyMem_NEW(char, size + 1);
1150 if (*buffer == NULL) {
1151 Py_DECREF(s);
1152 return converterr("(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001153 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001154 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +00001155 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001156 Py_DECREF(s);
1157 return converterr("(cleanup problem)",
1158 arg, msgbuf, bufsize);
1159 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001160 memcpy(*buffer,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001161 PyString_AS_STRING(s),
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001162 size + 1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001163 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001164 Py_DECREF(s);
1165 break;
1166 }
1167
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001168#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001169 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
1170 if (*format == '#') { /* any buffer-like object */
1171 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001172 FETCH_SIZE;
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001173 if (PyUnicode_Check(arg)) {
1174 *p = PyUnicode_AS_UNICODE(arg);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001175 STORE_SIZE(PyUnicode_GET_SIZE(arg));
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001176 }
1177 else {
Neal Norwitz61546162006-04-14 05:20:28 +00001178 return converterr("cannot convert raw buffers",
1179 arg, msgbuf, bufsize);
Marc-André Lemburg3e3eacb2002-01-09 16:21:27 +00001180 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001181 format++;
1182 } else {
1183 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Guido van Rossume826ef02000-03-10 23:02:17 +00001184 if (PyUnicode_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001185 *p = PyUnicode_AS_UNICODE(arg);
1186 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001187 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001188 }
1189 break;
1190 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001191#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001192
1193 case 'S': { /* string object */
1194 PyObject **p = va_arg(*p_va, PyObject **);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001195 if (PyString_Check(arg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001196 *p = arg;
1197 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001198 return converterr("string", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001199 break;
1200 }
1201
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001202#ifdef Py_USING_UNICODE
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001203 case 'U': { /* Unicode object */
1204 PyObject **p = va_arg(*p_va, PyObject **);
1205 if (PyUnicode_Check(arg))
1206 *p = arg;
1207 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001208 return converterr("unicode", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001209 break;
1210 }
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001211#endif
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001212
1213 case 'O': { /* object */
1214 PyTypeObject *type;
1215 PyObject **p;
1216 if (*format == '!') {
1217 type = va_arg(*p_va, PyTypeObject*);
1218 p = va_arg(*p_va, PyObject **);
1219 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +00001220 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +00001221 *p = arg;
1222 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001223 return converterr(type->tp_name, arg, msgbuf, bufsize);
Guido van Rossumfccfe891998-05-15 22:04:07 +00001224
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001225 }
1226 else if (*format == '?') {
1227 inquiry pred = va_arg(*p_va, inquiry);
1228 p = va_arg(*p_va, PyObject **);
1229 format++;
1230 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001231 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001232 else
1233 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001234 arg, msgbuf, bufsize);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001235
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001236 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001237 else if (*format == '&') {
1238 typedef int (*converter)(PyObject *, void *);
1239 converter convert = va_arg(*p_va, converter);
1240 void *addr = va_arg(*p_va, void *);
1241 format++;
1242 if (! (*convert)(arg, addr))
1243 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001244 arg, msgbuf, bufsize);
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001245 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001246 else {
1247 p = va_arg(*p_va, PyObject **);
1248 *p = arg;
1249 }
1250 break;
1251 }
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001252
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001253
1254 case 'w': { /* memory buffer, read-write access */
1255 void **p = va_arg(*p_va, void **);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001256 void *res;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001257 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +00001258 Py_ssize_t count;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001259
1260 if (pb && pb->bf_releasebuffer && *format != '*')
1261 /* Buffer must be released, yet caller does not use
1262 the Py_buffer protocol. */
1263 return converterr("pinned buffer", arg, msgbuf, bufsize);
1264
1265 if (pb && pb->bf_getbuffer && *format == '*') {
1266 /* Caller is interested in Py_buffer, and the object
1267 supports it directly. */
1268 format++;
1269 if (pb->bf_getbuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1270 PyErr_Clear();
1271 return converterr("read-write buffer", arg, msgbuf, bufsize);
1272 }
Antoine Pitroud4ae97b2008-08-29 18:39:48 +00001273 if (addcleanup(p, freelist, cleanup_buffer)) {
1274 return converterr(
1275 "(cleanup problem)",
1276 arg, msgbuf, bufsize);
1277 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001278 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1279 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1280 break;
1281 }
1282
1283 if (pb == NULL ||
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001284 pb->bf_getwritebuffer == NULL ||
1285 pb->bf_getsegcount == NULL)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001286 return converterr("read-write buffer", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001287 if ((*pb->bf_getsegcount)(arg, NULL) != 1)
1288 return converterr("single-segment read-write buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001289 arg, msgbuf, bufsize);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001290 if ((count = pb->bf_getwritebuffer(arg, 0, &res)) < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001291 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001292 if (*format == '*') {
1293 PyBuffer_FillInfo((Py_buffer*)p, arg, res, count, 1, 0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001294 format++;
1295 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001296 else {
1297 *p = res;
1298 if (*format == '#') {
1299 FETCH_SIZE;
1300 STORE_SIZE(count);
1301 format++;
1302 }
1303 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001304 break;
1305 }
1306
1307 case 't': { /* 8-bit character buffer, read-only access */
Martin v. Löwis15e62742006-02-27 16:46:16 +00001308 char **p = va_arg(*p_va, char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001309 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Amaury Forgeot d'Arcdafd32b2007-11-30 20:51:40 +00001310 Py_ssize_t count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001311
1312 if (*format++ != '#')
1313 return converterr(
1314 "invalid use of 't' format character",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001315 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001316 if (!PyType_HasFeature(arg->ob_type,
Jeremy Hylton4819e972001-10-11 14:40:37 +00001317 Py_TPFLAGS_HAVE_GETCHARBUFFER) ||
1318 pb == NULL || pb->bf_getcharbuffer == NULL ||
1319 pb->bf_getsegcount == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001320 return converterr(
1321 "string or read-only character buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001322 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001323
Jeremy Hylton4819e972001-10-11 14:40:37 +00001324 if (pb->bf_getsegcount(arg, NULL) != 1)
1325 return converterr(
1326 "string or single-segment read-only buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001327 arg, msgbuf, bufsize);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001328
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001329 if (pb->bf_releasebuffer)
1330 return converterr(
1331 "string or pinned buffer",
1332 arg, msgbuf, bufsize);
1333
Jeremy Hylton4819e972001-10-11 14:40:37 +00001334 count = pb->bf_getcharbuffer(arg, 0, p);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001335 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001336 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001337 {
1338 FETCH_SIZE;
1339 STORE_SIZE(count);
1340 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001341 break;
1342 }
1343
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001344 default:
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001345 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001346
1347 }
1348
1349 *p_format = format;
1350 return NULL;
1351}
Guido van Rossumaa354651996-08-19 19:32:04 +00001352
Martin v. Löwis18e16552006-02-15 17:27:45 +00001353static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001354convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001355{
1356 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001357 Py_ssize_t count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001358 if (pb == NULL ||
1359 pb->bf_getreadbuffer == NULL ||
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001360 pb->bf_getsegcount == NULL ||
1361 pb->bf_releasebuffer != NULL) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001362 *errmsg = "string or read-only buffer";
1363 return -1;
1364 }
1365 if ((*pb->bf_getsegcount)(arg, NULL) != 1) {
1366 *errmsg = "string or single-segment read-only buffer";
1367 return -1;
1368 }
1369 if ((count = (*pb->bf_getreadbuffer)(arg, 0, p)) < 0) {
1370 *errmsg = "(unspecified)";
1371 }
1372 return count;
1373}
Guido van Rossumaa354651996-08-19 19:32:04 +00001374
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001375static int
Neal Norwitz18aa3882008-08-24 05:04:52 +00001376getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001377{
1378 void *buf;
1379 Py_ssize_t count;
1380 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1381 if (pb == NULL) {
1382 *errmsg = "string or buffer";
1383 return -1;
1384 }
1385 if (pb->bf_getbuffer) {
Neal Norwitz18aa3882008-08-24 05:04:52 +00001386 if (pb->bf_getbuffer(arg, view, 0) < 0) {
1387 *errmsg = "convertible to a buffer";
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001388 return -1;
Neal Norwitz18aa3882008-08-24 05:04:52 +00001389 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001390 if (!PyBuffer_IsContiguous(view, 'C')) {
1391 *errmsg = "contiguous buffer";
1392 return -1;
1393 }
1394 return 0;
1395 }
1396
1397 count = convertbuffer(arg, &buf, errmsg);
Neal Norwitz18aa3882008-08-24 05:04:52 +00001398 if (count < 0) {
1399 *errmsg = "convertible to a buffer";
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001400 return count;
Neal Norwitz18aa3882008-08-24 05:04:52 +00001401 }
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001402 PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
1403 return 0;
1404}
1405
Guido van Rossumaa354651996-08-19 19:32:04 +00001406/* Support for keyword arguments donated by
1407 Geoff Philbrick <philbric@delphi.hks.com> */
1408
Tim Petersf8cd3e82001-10-27 04:26:57 +00001409/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001410int
1411PyArg_ParseTupleAndKeywords(PyObject *args,
1412 PyObject *keywords,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001413 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001414 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001415{
1416 int retval;
1417 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001418
1419 if ((args == NULL || !PyTuple_Check(args)) ||
1420 (keywords != NULL && !PyDict_Check(keywords)) ||
1421 format == NULL ||
1422 kwlist == NULL)
1423 {
1424 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001425 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001426 }
1427
Guido van Rossumaa354651996-08-19 19:32:04 +00001428 va_start(va, kwlist);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001429 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1430 va_end(va);
1431 return retval;
1432}
1433
1434int
1435_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1436 PyObject *keywords,
1437 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001438 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001439{
1440 int retval;
1441 va_list va;
1442
1443 if ((args == NULL || !PyTuple_Check(args)) ||
1444 (keywords != NULL && !PyDict_Check(keywords)) ||
1445 format == NULL ||
1446 kwlist == NULL)
1447 {
1448 PyErr_BadInternalCall();
1449 return 0;
1450 }
1451
1452 va_start(va, kwlist);
1453 retval = vgetargskeywords(args, keywords, format,
1454 kwlist, &va, FLAG_SIZE_T);
Guido van Rossumaa354651996-08-19 19:32:04 +00001455 va_end(va);
1456 return retval;
1457}
1458
1459
Brett Cannon711e7d92004-07-10 22:20:32 +00001460int
1461PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001462 PyObject *keywords,
1463 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001464 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001465{
1466 int retval;
1467 va_list lva;
1468
1469 if ((args == NULL || !PyTuple_Check(args)) ||
1470 (keywords != NULL && !PyDict_Check(keywords)) ||
1471 format == NULL ||
1472 kwlist == NULL)
1473 {
1474 PyErr_BadInternalCall();
1475 return 0;
1476 }
1477
1478#ifdef VA_LIST_IS_ARRAY
1479 memcpy(lva, va, sizeof(va_list));
1480#else
1481#ifdef __va_copy
1482 __va_copy(lva, va);
1483#else
1484 lva = va;
1485#endif
1486#endif
1487
Martin v. Löwis18e16552006-02-15 17:27:45 +00001488 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1489 return retval;
1490}
1491
1492int
1493_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1494 PyObject *keywords,
1495 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001496 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001497{
1498 int retval;
1499 va_list lva;
1500
1501 if ((args == NULL || !PyTuple_Check(args)) ||
1502 (keywords != NULL && !PyDict_Check(keywords)) ||
1503 format == NULL ||
1504 kwlist == NULL)
1505 {
1506 PyErr_BadInternalCall();
1507 return 0;
1508 }
1509
1510#ifdef VA_LIST_IS_ARRAY
1511 memcpy(lva, va, sizeof(va_list));
1512#else
1513#ifdef __va_copy
1514 __va_copy(lva, va);
1515#else
1516 lva = va;
1517#endif
1518#endif
1519
1520 retval = vgetargskeywords(args, keywords, format,
1521 kwlist, &lva, FLAG_SIZE_T);
Brett Cannon711e7d92004-07-10 22:20:32 +00001522 return retval;
1523}
1524
Christian Heimesea837932008-02-26 17:23:51 +00001525#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001526
Guido van Rossumaa354651996-08-19 19:32:04 +00001527static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001528vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001529 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001530{
Tim Petersdc5eff92001-10-27 06:53:00 +00001531 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001532 int levels[32];
Christian Heimesea837932008-02-26 17:23:51 +00001533 const char *fname, *msg, *custom_msg, *keyword;
1534 int min = INT_MAX;
Tim Petersb639d492001-10-27 07:00:56 +00001535 int i, len, nargs, nkeywords;
Christian Heimesea837932008-02-26 17:23:51 +00001536 PyObject *freelist = NULL, *current_arg;
Tim Petersf4331c12001-10-27 00:17:34 +00001537
Tim Peters45772cd2001-10-27 03:58:40 +00001538 assert(args != NULL && PyTuple_Check(args));
1539 assert(keywords == NULL || PyDict_Check(keywords));
1540 assert(format != NULL);
1541 assert(kwlist != NULL);
1542 assert(p_va != NULL);
1543
Christian Heimesea837932008-02-26 17:23:51 +00001544 /* grab the function name or custom error msg first (mutually exclusive) */
1545 fname = strchr(format, ':');
1546 if (fname) {
1547 fname++;
1548 custom_msg = NULL;
Tim Peters62d48e12001-10-27 06:42:16 +00001549 }
Christian Heimesea837932008-02-26 17:23:51 +00001550 else {
1551 custom_msg = strchr(format,';');
1552 if (custom_msg)
1553 custom_msg++;
Tim Peters62d48e12001-10-27 06:42:16 +00001554 }
Christian Heimesea837932008-02-26 17:23:51 +00001555
1556 /* scan kwlist and get greatest possible nbr of args */
1557 for (len=0; kwlist[len]; len++)
1558 continue;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001559
Tim Peters6fb26352001-10-27 04:38:11 +00001560 nargs = PyTuple_GET_SIZE(args);
Christian Heimesea837932008-02-26 17:23:51 +00001561 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1562 if (nargs + nkeywords > len) {
1563 PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
1564 "argument%s (%d given)",
1565 (fname == NULL) ? "function" : fname,
1566 (fname == NULL) ? "" : "()",
1567 len,
1568 (len == 1) ? "" : "s",
1569 nargs + nkeywords);
Guido van Rossumaa354651996-08-19 19:32:04 +00001570 return 0;
1571 }
Tim Petersc2f01122001-10-27 07:25:06 +00001572
Christian Heimesea837932008-02-26 17:23:51 +00001573 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1574 for (i = 0; i < len; i++) {
1575 keyword = kwlist[i];
1576 if (*format == '|') {
1577 min = i;
Guido van Rossumaa354651996-08-19 19:32:04 +00001578 format++;
Christian Heimesea837932008-02-26 17:23:51 +00001579 }
1580 if (IS_END_OF_FORMAT(*format)) {
1581 PyErr_Format(PyExc_RuntimeError,
1582 "More keyword list entries (%d) than "
1583 "format specifiers (%d)", len, i);
1584 return cleanreturn(0, freelist);
1585 }
1586 current_arg = NULL;
1587 if (nkeywords) {
1588 current_arg = PyDict_GetItemString(keywords, keyword);
1589 }
1590 if (current_arg) {
1591 --nkeywords;
1592 if (i < nargs) {
1593 /* arg present in tuple and in dict */
1594 PyErr_Format(PyExc_TypeError,
1595 "Argument given by name ('%s') "
1596 "and position (%d)",
1597 keyword, i+1);
1598 return cleanreturn(0, freelist);
1599 }
1600 }
1601 else if (nkeywords && PyErr_Occurred())
1602 return cleanreturn(0, freelist);
1603 else if (i < nargs)
1604 current_arg = PyTuple_GET_ITEM(args, i);
1605
1606 if (current_arg) {
1607 msg = convertitem(current_arg, &format, p_va, flags,
1608 levels, msgbuf, sizeof(msgbuf), &freelist);
1609 if (msg) {
1610 seterror(i+1, msg, levels, fname, custom_msg);
1611 return cleanreturn(0, freelist);
1612 }
1613 continue;
1614 }
1615
1616 if (i < min) {
1617 PyErr_Format(PyExc_TypeError, "Required argument "
1618 "'%s' (pos %d) not found",
1619 keyword, i+1);
1620 return cleanreturn(0, freelist);
1621 }
1622 /* current code reports success when all required args
1623 * fulfilled and no keyword args left, with no further
1624 * validation. XXX Maybe skip this in debug build ?
1625 */
1626 if (!nkeywords)
1627 return cleanreturn(1, freelist);
1628
1629 /* We are into optional args, skip thru to any remaining
1630 * keyword args */
1631 msg = skipitem(&format, p_va, flags);
Guido van Rossumaa354651996-08-19 19:32:04 +00001632 if (msg) {
Christian Heimesea837932008-02-26 17:23:51 +00001633 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1634 format);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001635 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001636 }
1637 }
1638
Benjamin Peterson4caef5c2008-12-22 20:51:15 +00001639 if (!IS_END_OF_FORMAT(*format) && *format != '|') {
Christian Heimesea837932008-02-26 17:23:51 +00001640 PyErr_Format(PyExc_RuntimeError,
1641 "more argument specifiers than keyword list entries "
1642 "(remaining format:'%s')", format);
1643 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001644 }
Tim Petersb054be42001-10-27 05:07:41 +00001645
Guido van Rossumaa354651996-08-19 19:32:04 +00001646 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001647 if (nkeywords > 0) {
1648 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001649 Py_ssize_t pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001650 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001651 int match = 0;
Guido van Rossum55474762002-04-04 16:22:30 +00001652 char *ks;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001653 if (!PyString_Check(key)) {
Guido van Rossum55474762002-04-04 16:22:30 +00001654 PyErr_SetString(PyExc_TypeError,
1655 "keywords must be strings");
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001656 return cleanreturn(0, freelist);
Guido van Rossum55474762002-04-04 16:22:30 +00001657 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001658 ks = PyString_AsString(key);
Christian Heimesea837932008-02-26 17:23:51 +00001659 for (i = 0; i < len; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001660 if (!strcmp(ks, kwlist[i])) {
1661 match = 1;
1662 break;
1663 }
1664 }
1665 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001666 PyErr_Format(PyExc_TypeError,
1667 "'%s' is an invalid keyword "
1668 "argument for this function",
1669 ks);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001670 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001671 }
1672 }
1673 }
Tim Petersc2f01122001-10-27 07:25:06 +00001674
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001675 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001676}
1677
1678
1679static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001680skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001681{
Christian Heimesea837932008-02-26 17:23:51 +00001682 const char *format = *p_format;
Guido van Rossumaa354651996-08-19 19:32:04 +00001683 char c = *format++;
1684
1685 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001686
1687 /* simple codes
1688 * The individual types (second arg of va_arg) are irrelevant */
1689
Guido van Rossumaa354651996-08-19 19:32:04 +00001690 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001691 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001692 case 'h': /* short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001693 case 'H': /* short int as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001694 case 'i': /* int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001695 case 'I': /* int sized bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001696 case 'l': /* long int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001697 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001698#ifdef HAVE_LONG_LONG
Georg Brandl6dd14612005-09-14 19:29:53 +00001699 case 'L': /* PY_LONG_LONG */
1700 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001701#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001702 case 'f': /* float */
Guido van Rossumaa354651996-08-19 19:32:04 +00001703 case 'd': /* double */
Guido van Rossumaa354651996-08-19 19:32:04 +00001704#ifndef WITHOUT_COMPLEX
1705 case 'D': /* complex double */
Georg Brandl6dd14612005-09-14 19:29:53 +00001706#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001707 case 'c': /* char */
1708 {
Georg Brandl6dd14612005-09-14 19:29:53 +00001709 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001710 break;
1711 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001712
1713 case 'n': /* Py_ssize_t */
1714 {
1715 (void) va_arg(*p_va, Py_ssize_t *);
1716 break;
1717 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001718
Georg Brandl6dd14612005-09-14 19:29:53 +00001719 /* string codes */
1720
1721 case 'e': /* string with encoding */
1722 {
1723 (void) va_arg(*p_va, const char *);
1724 if (!(*format == 's' || *format == 't'))
1725 /* after 'e', only 's' and 't' is allowed */
1726 goto err;
1727 format++;
1728 /* explicit fallthrough to string cases */
1729 }
1730
Guido van Rossumaa354651996-08-19 19:32:04 +00001731 case 's': /* string */
Georg Brandl6dd14612005-09-14 19:29:53 +00001732 case 'z': /* string or None */
1733#ifdef Py_USING_UNICODE
1734 case 'u': /* unicode string */
1735#endif
1736 case 't': /* buffer, read-only */
1737 case 'w': /* buffer, read-write */
Guido van Rossumaa354651996-08-19 19:32:04 +00001738 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001739 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001740 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001741 if (flags & FLAG_SIZE_T)
1742 (void) va_arg(*p_va, Py_ssize_t *);
1743 else
1744 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001745 format++;
Martin v. Löwisf91d46a2008-08-12 14:49:50 +00001746 } else if ((c == 's' || c == 'z') && *format == '*') {
1747 format++;
Guido van Rossumaa354651996-08-19 19:32:04 +00001748 }
1749 break;
1750 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001751
1752 /* object codes */
1753
Guido van Rossumaa354651996-08-19 19:32:04 +00001754 case 'S': /* string object */
Georg Brandl6dd14612005-09-14 19:29:53 +00001755#ifdef Py_USING_UNICODE
1756 case 'U': /* unicode string object */
1757#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001758 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001759 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001760 break;
1761 }
1762
1763 case 'O': /* object */
1764 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001765 if (*format == '!') {
1766 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001767 (void) va_arg(*p_va, PyTypeObject*);
1768 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001769 }
1770#if 0
1771/* I don't know what this is for */
1772 else if (*format == '?') {
1773 inquiry pred = va_arg(*p_va, inquiry);
1774 format++;
1775 if ((*pred)(arg)) {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001776 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001777 }
1778 }
1779#endif
1780 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001781 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001782 (void) va_arg(*p_va, converter);
1783 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001784 format++;
1785 }
1786 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001787 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001788 }
1789 break;
1790 }
Christian Heimesea837932008-02-26 17:23:51 +00001791
1792 case '(': /* bypass tuple, not handled at all previously */
1793 {
1794 char *msg;
1795 for (;;) {
1796 if (*format==')')
1797 break;
1798 if (IS_END_OF_FORMAT(*format))
1799 return "Unmatched left paren in format "
1800 "string";
1801 msg = skipitem(&format, p_va, flags);
1802 if (msg)
1803 return msg;
1804 }
1805 format++;
1806 break;
1807 }
1808
1809 case ')':
1810 return "Unmatched right paren in format string";
1811
Guido van Rossumaa354651996-08-19 19:32:04 +00001812 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001813err:
Guido van Rossumaa354651996-08-19 19:32:04 +00001814 return "impossible<bad format char>";
1815
1816 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001817
Guido van Rossumaa354651996-08-19 19:32:04 +00001818 *p_format = format;
1819 return NULL;
1820}
Fred Drakee4616e62001-10-23 21:09:29 +00001821
1822
1823int
Martin v. Löwis76246742006-03-01 04:06:10 +00001824PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001825{
Martin v. Löwis76246742006-03-01 04:06:10 +00001826 Py_ssize_t i, l;
Fred Drakee4616e62001-10-23 21:09:29 +00001827 PyObject **o;
1828 va_list vargs;
1829
1830#ifdef HAVE_STDARG_PROTOTYPES
1831 va_start(vargs, max);
1832#else
1833 va_start(vargs);
1834#endif
1835
1836 assert(min >= 0);
1837 assert(min <= max);
1838 if (!PyTuple_Check(args)) {
1839 PyErr_SetString(PyExc_SystemError,
1840 "PyArg_UnpackTuple() argument list is not a tuple");
1841 return 0;
1842 }
1843 l = PyTuple_GET_SIZE(args);
1844 if (l < min) {
1845 if (name != NULL)
1846 PyErr_Format(
1847 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001848 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001849 name, (min == max ? "" : "at least "), min, l);
1850 else
1851 PyErr_Format(
1852 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001853 "unpacked tuple should have %s%zd elements,"
1854 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001855 (min == max ? "" : "at least "), min, l);
1856 va_end(vargs);
1857 return 0;
1858 }
1859 if (l > max) {
1860 if (name != NULL)
1861 PyErr_Format(
1862 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001863 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001864 name, (min == max ? "" : "at most "), max, l);
1865 else
1866 PyErr_Format(
1867 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001868 "unpacked tuple should have %s%zd elements,"
1869 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001870 (min == max ? "" : "at most "), max, l);
1871 va_end(vargs);
1872 return 0;
1873 }
1874 for (i = 0; i < l; i++) {
1875 o = va_arg(vargs, PyObject **);
1876 *o = PyTuple_GET_ITEM(args, i);
1877 }
1878 va_end(vargs);
1879 return 1;
1880}
Georg Brandl02c42872005-08-26 06:42:30 +00001881
1882
1883/* For type constructors that don't take keyword args
1884 *
1885 * Sets a TypeError and returns 0 if the kwds dict is
Walter Dörwaldd14bf612006-09-21 15:09:55 +00001886 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001887 */
1888int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001889_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001890{
1891 if (kw == NULL)
1892 return 1;
1893 if (!PyDict_CheckExact(kw)) {
1894 PyErr_BadInternalCall();
1895 return 0;
1896 }
1897 if (PyDict_Size(kw) == 0)
1898 return 1;
1899
1900 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1901 funcname);
1902 return 0;
1903}
Anthony Baxter97300382006-04-12 04:38:54 +00001904#ifdef __cplusplus
1905};
1906#endif