blob: 3ab59b3e42d87ed1af3b38d8811de1cdae109cbf [file] [log] [blame]
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001
2/* New getargs implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00005
Guido van Rossumc1d50531996-08-21 23:38:24 +00006#include <ctype.h>
7
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00008
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009#ifdef __cplusplus
Guido van Rossum98297ee2007-11-06 21:34:58 +000010extern "C" {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011#endif
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000012int PyArg_Parse(PyObject *, const char *, ...);
13int PyArg_ParseTuple(PyObject *, const char *, ...);
14int PyArg_VaParse(PyObject *, const char *, va_list);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000015
Tim Petersdbd9ba62000-07-09 03:09:57 +000016int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
Martin v. Löwis15e62742006-02-27 16:46:16 +000017 const char *, char **, ...);
Brett Cannon711e7d92004-07-10 22:20:32 +000018int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
Martin v. Löwis15e62742006-02-27 16:46:16 +000019 const char *, char **, va_list);
Brett Cannon711e7d92004-07-10 22:20:32 +000020
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000021#ifdef HAVE_DECLSPEC_DLL
22/* Export functions */
23PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, char *, ...);
24PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, char *, ...);
25PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
26 const char *, char **, ...);
27PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
28PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, char *, va_list);
29PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
30 const char *, char **, va_list);
31#endif
32
Martin v. Löwis18e16552006-02-15 17:27:45 +000033#define FLAG_COMPAT 1
34#define FLAG_SIZE_T 2
35
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000036
37/* Forward */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000038static int vgetargs1(PyObject *, const char *, va_list *, int);
39static void seterror(int, const char *, int *, const char *, const char *);
Guido van Rossum98297ee2007-11-06 21:34:58 +000040static char *convertitem(PyObject *, const char **, va_list *, int, int *,
Martin v. Löwis18e16552006-02-15 17:27:45 +000041 char *, size_t, PyObject **);
42static char *converttuple(PyObject *, const char **, va_list *, int,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +000043 int *, char *, size_t, int, PyObject **);
Martin v. Löwis18e16552006-02-15 17:27:45 +000044static char *convertsimple(PyObject *, const char **, va_list *, int, char *,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +000045 size_t, PyObject **);
Martin v. Löwis18e16552006-02-15 17:27:45 +000046static Py_ssize_t convertbuffer(PyObject *, void **p, char **);
Martin v. Löwis423be952008-08-13 15:53:07 +000047static int getbuffer(PyObject *, Py_buffer *, char**);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000048
Tim Petersdbd9ba62000-07-09 03:09:57 +000049static int vgetargskeywords(PyObject *, PyObject *,
Martin v. Löwis15e62742006-02-27 16:46:16 +000050 const char *, char **, va_list *, int);
Martin v. Löwis18e16552006-02-15 17:27:45 +000051static char *skipitem(const char **, va_list *, int);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000052
Fred Drake563dfc22001-10-23 14:41:08 +000053int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000054PyArg_Parse(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000055{
56 int retval;
57 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000058
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000059 va_start(va, format);
Martin v. Löwis18e16552006-02-15 17:27:45 +000060 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
61 va_end(va);
62 return retval;
63}
64
65int
66_PyArg_Parse_SizeT(PyObject *args, char *format, ...)
67{
68 int retval;
69 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000070
Martin v. Löwis18e16552006-02-15 17:27:45 +000071 va_start(va, format);
72 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000073 va_end(va);
74 return retval;
75}
76
77
Fred Drake563dfc22001-10-23 14:41:08 +000078int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000079PyArg_ParseTuple(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000080{
81 int retval;
82 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000083
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000084 va_start(va, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000085 retval = vgetargs1(args, format, &va, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000086 va_end(va);
87 return retval;
88}
89
Martin v. Löwis18e16552006-02-15 17:27:45 +000090int
91_PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
92{
93 int retval;
94 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000095
Martin v. Löwis18e16552006-02-15 17:27:45 +000096 va_start(va, format);
97 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
98 va_end(va);
99 return retval;
100}
101
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000102
103int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000104PyArg_VaParse(PyObject *args, const char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000105{
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000106 va_list lva;
107
108#ifdef VA_LIST_IS_ARRAY
109 memcpy(lva, va, sizeof(va_list));
110#else
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000111#ifdef __va_copy
112 __va_copy(lva, va);
113#else
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000114 lva = va;
115#endif
Martin v. Löwis75d2d942002-07-28 10:23:27 +0000116#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000117
118 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000119}
120
Martin v. Löwis18e16552006-02-15 17:27:45 +0000121int
122_PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
123{
124 va_list lva;
125
126#ifdef VA_LIST_IS_ARRAY
127 memcpy(lva, va, sizeof(va_list));
128#else
129#ifdef __va_copy
130 __va_copy(lva, va);
131#else
132 lva = va;
133#endif
134#endif
135
136 return vgetargs1(args, format, &lva, FLAG_SIZE_T);
137}
138
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000139
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000140/* Handle cleanup of allocated memory in case of exception */
141
Antoine Pitrouf71995782008-08-29 18:37:05 +0000142static void
143cleanup_ptr(void *ptr)
144{
145 PyMem_FREE(ptr);
146}
147
148static void
149cleanup_buffer(void *ptr)
150{
151 PyBuffer_Release((Py_buffer *) ptr);
152}
153
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000154static int
Antoine Pitrouf71995782008-08-29 18:37:05 +0000155addcleanup(void *ptr, PyObject **freelist, void (*destr)(void *))
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000156{
157 PyObject *cobj;
158 if (!*freelist) {
159 *freelist = PyList_New(0);
160 if (!*freelist) {
Antoine Pitrouf71995782008-08-29 18:37:05 +0000161 destr(ptr);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000162 return -1;
163 }
164 }
Antoine Pitrouf71995782008-08-29 18:37:05 +0000165 cobj = PyCObject_FromVoidPtr(ptr, destr);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000166 if (!cobj) {
Antoine Pitrouf71995782008-08-29 18:37:05 +0000167 destr(ptr);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000168 return -1;
169 }
Christian Heimes836baa52008-02-26 08:18:30 +0000170 if (PyList_Append(*freelist, cobj)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000171 Py_DECREF(cobj);
172 return -1;
173 }
174 Py_DECREF(cobj);
175 return 0;
176}
177
178static int
179cleanreturn(int retval, PyObject *freelist)
180{
Antoine Pitrouf71995782008-08-29 18:37:05 +0000181 if (freelist && retval != 0) {
182 /* We were successful, reset the destructors so that they
183 don't get called. */
184 Py_ssize_t len = PyList_GET_SIZE(freelist), i;
185 for (i = 0; i < len; i++)
186 ((PyCObject *) PyList_GET_ITEM(freelist, i))
187 ->destructor = NULL;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000188 }
Antoine Pitrouf71995782008-08-29 18:37:05 +0000189 Py_XDECREF(freelist);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000190 return retval;
191}
192
193
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000194static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000195vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000196{
197 char msgbuf[256];
198 int levels[32];
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000199 const char *fname = NULL;
200 const char *message = NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000201 int min = -1;
202 int max = 0;
203 int level = 0;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000204 int endfmt = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000205 const char *formatsave = format;
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000206 Py_ssize_t i, len;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000207 char *msg;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000208 PyObject *freelist = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000209 int compat = flags & FLAG_COMPAT;
210
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000211 assert(compat || (args != (PyObject*)NULL));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000212 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000213
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000214 while (endfmt == 0) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000215 int c = *format++;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000216 switch (c) {
217 case '(':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000218 if (level == 0)
219 max++;
220 level++;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221 if (level >= 30)
222 Py_FatalError("too many tuple nesting levels "
223 "in argument format string");
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000224 break;
225 case ')':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000226 if (level == 0)
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000227 Py_FatalError("excess ')' in getargs format");
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000228 else
229 level--;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000230 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000231 case '\0':
232 endfmt = 1;
233 break;
234 case ':':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000235 fname = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000236 endfmt = 1;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000237 break;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000238 case ';':
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000239 message = format;
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000240 endfmt = 1;
241 break;
242 default:
243 if (level == 0) {
244 if (c == 'O')
245 max++;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000246 else if (isalpha(Py_CHARMASK(c))) {
Jeremy Hylton25916bd2001-05-29 17:46:19 +0000247 if (c != 'e') /* skip encoded */
248 max++;
249 } else if (c == '|')
250 min = max;
251 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000252 break;
253 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000254 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000255
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000256 if (level != 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +0000257 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000258
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000259 if (min < 0)
260 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000261
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000262 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000263
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000264 if (compat) {
265 if (max == 0) {
266 if (args == NULL)
267 return 1;
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000268 PyOS_snprintf(msgbuf, sizeof(msgbuf),
269 "%.200s%s takes no arguments",
270 fname==NULL ? "function" : fname,
271 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000272 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000273 return 0;
274 }
275 else if (min == 1 && max == 1) {
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000276 if (args == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000277 PyOS_snprintf(msgbuf, sizeof(msgbuf),
278 "%.200s%s takes at least one argument",
279 fname==NULL ? "function" : fname,
280 fname==NULL ? "" : "()");
Guido van Rossum79f25d91997-04-29 20:08:16 +0000281 PyErr_SetString(PyExc_TypeError, msgbuf);
Guido van Rossum13d0ed11994-11-10 22:35:48 +0000282 return 0;
283 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000284 msg = convertitem(args, &format, p_va, flags, levels,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000285 msgbuf, sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000286 if (msg == NULL)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000287 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000288 seterror(levels[0], msg, levels+1, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000289 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000290 }
291 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +0000292 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000293 "old style getargs format uses new features");
294 return 0;
295 }
296 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000297
Guido van Rossum79f25d91997-04-29 20:08:16 +0000298 if (!PyTuple_Check(args)) {
299 PyErr_SetString(PyExc_SystemError,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000300 "new style getargs format but argument is not a tuple");
301 return 0;
302 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000303
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000304 len = PyTuple_GET_SIZE(args);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000305
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000306 if (len < min || max < len) {
307 if (message == NULL) {
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000308 PyOS_snprintf(msgbuf, sizeof(msgbuf),
309 "%.150s%s takes %s %d argument%s "
Neal Norwitz20dd93f2006-02-19 19:34:15 +0000310 "(%ld given)",
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000311 fname==NULL ? "function" : fname,
312 fname==NULL ? "" : "()",
313 min==max ? "exactly"
314 : len < min ? "at least" : "at most",
315 len < min ? min : max,
316 (len < min ? min : max) == 1 ? "" : "s",
Neal Norwitz9a276172006-02-20 18:57:39 +0000317 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000318 message = msgbuf;
319 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000320 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000321 return 0;
322 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000323
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000324 for (i = 0; i < len; i++) {
325 if (*format == '|')
326 format++;
Jeremy Hylton0f8117f2001-05-18 20:57:38 +0000327 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000328 flags, levels, msgbuf,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000329 sizeof(msgbuf), &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000330 if (msg) {
331 seterror(i+1, msg, levels, fname, message);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000332 return cleanreturn(0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000333 }
334 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000335
Neal Norwitz4ac13df2005-12-19 06:10:07 +0000336 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
Guido van Rossum7d4f68c1997-12-19 04:25:23 +0000337 *format != '(' &&
Guido van Rossum231a41e1997-12-09 20:36:39 +0000338 *format != '|' && *format != ':' && *format != ';') {
339 PyErr_Format(PyExc_SystemError,
Guido van Rossum0d6b49e1998-01-19 22:22:44 +0000340 "bad format string: %.200s", formatsave);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000341 return cleanreturn(0, freelist);
Guido van Rossum231a41e1997-12-09 20:36:39 +0000342 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000343
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000344 return cleanreturn(1, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000345}
346
347
348
349static void
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000350seterror(int iarg, const char *msg, int *levels, const char *fname,
351 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000352{
Marc-André Lemburgd4c0a9c2001-11-28 11:47:00 +0000353 char buf[512];
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000354 int i;
355 char *p = buf;
356
Guido van Rossum79f25d91997-04-29 20:08:16 +0000357 if (PyErr_Occurred())
Guido van Rossum64fc6491995-01-21 14:09:37 +0000358 return;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000359 else if (message == NULL) {
360 if (fname != NULL) {
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000361 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000362 p += strlen(p);
363 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000364 if (iarg != 0) {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000365 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000366 "argument %d", iarg);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000367 i = 0;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000368 p += strlen(p);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000369 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
370 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Jeremy Hyltonf16e05e2001-11-28 21:46:59 +0000371 ", item %d", levels[i]-1);
Ka-Ping Yee20579702001-01-15 22:14:16 +0000372 p += strlen(p);
373 i++;
374 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000375 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000376 else {
Tim Petersfaad5ad2001-12-03 00:43:33 +0000377 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
Ka-Ping Yee20579702001-01-15 22:14:16 +0000378 p += strlen(p);
379 }
Tim Petersfaad5ad2001-12-03 00:43:33 +0000380 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000381 message = buf;
382 }
Guido van Rossum79f25d91997-04-29 20:08:16 +0000383 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000384}
385
386
387/* Convert a tuple argument.
388 On entry, *p_format points to the character _after_ the opening '('.
389 On successful exit, *p_format points to the closing ')'.
390 If successful:
391 *p_format and *p_va are updated,
392 *levels and *msgbuf are untouched,
393 and NULL is returned.
394 If the argument is invalid:
395 *p_format is unchanged,
396 *p_va is undefined,
397 *levels is a 0-terminated list of item numbers,
398 *msgbuf contains an error message, whose format is:
Ka-Ping Yee20579702001-01-15 22:14:16 +0000399 "must be <typename1>, not <typename2>", where:
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000400 <typename1> is the name of the expected type, and
401 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000402 and msgbuf is returned.
403*/
404
405static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000406converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000407 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000408 PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000409{
410 int level = 0;
411 int n = 0;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000412 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000413 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000414
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000415 for (;;) {
416 int c = *format++;
417 if (c == '(') {
418 if (level == 0)
419 n++;
420 level++;
421 }
422 else if (c == ')') {
423 if (level == 0)
424 break;
425 level--;
426 }
427 else if (c == ':' || c == ';' || c == '\0')
428 break;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000429 else if (level == 0 && isalpha(Py_CHARMASK(c)))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000430 n++;
431 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000432
Christian Heimes72b710a2008-05-26 13:28:38 +0000433 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000434 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000435 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000436 toplevel ? "expected %d arguments, not %.50s" :
437 "must be %d-item sequence, not %.50s",
Guido van Rossum98297ee2007-11-06 21:34:58 +0000438 n,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000439 arg == Py_None ? "None" : arg->ob_type->tp_name);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000440 return msgbuf;
441 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000442
Jeremy Hylton03657cf2000-07-12 13:05:33 +0000443 if ((i = PySequence_Size(arg)) != n) {
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000444 levels[0] = 0;
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000445 PyOS_snprintf(msgbuf, bufsize,
Jeremy Hylton23ae9872001-11-28 20:29:22 +0000446 toplevel ? "expected %d arguments, not %d" :
447 "must be sequence of length %d, not %d",
448 n, i);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000449 return msgbuf;
450 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000451
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000452 format = *p_format;
453 for (i = 0; i < n; i++) {
454 char *msg;
Guido van Rossum66368cc1999-02-17 23:16:43 +0000455 PyObject *item;
456 item = PySequence_GetItem(arg, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000457 if (item == NULL) {
458 PyErr_Clear();
459 levels[0] = i+1;
460 levels[1] = 0;
461 strncpy(msgbuf, "is not retrievable", bufsize);
462 return msgbuf;
463 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000464 msg = convertitem(item, &format, p_va, flags, levels+1,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000465 msgbuf, bufsize, freelist);
Guido van Rossum66368cc1999-02-17 23:16:43 +0000466 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
467 Py_XDECREF(item);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000468 if (msg != NULL) {
469 levels[0] = i+1;
470 return msg;
471 }
472 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000473
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000474 *p_format = format;
475 return NULL;
476}
477
478
479/* Convert a single item. */
480
481static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
483 int *levels, char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000484{
485 char *msg;
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000486 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000487
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000488 if (*format == '(' /* ')' */) {
489 format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000490 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000491 bufsize, 0, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000492 if (msg == NULL)
493 format++;
494 }
495 else {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000496 msg = convertsimple(arg, &format, p_va, flags,
Martin v. Löwis18e16552006-02-15 17:27:45 +0000497 msgbuf, bufsize, freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000498 if (msg != NULL)
499 levels[0] = 0;
500 }
501 if (msg == NULL)
502 *p_format = format;
503 return msg;
504}
505
506
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000507
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000508#define UNICODE_DEFAULT_ENCODING(arg) \
509 _PyUnicode_AsDefaultEncodedString(arg, NULL)
510
511/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000512
513static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000514converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000515{
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000516 assert(expected != NULL);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000517 assert(arg != NULL);
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000518 PyOS_snprintf(msgbuf, bufsize,
519 "must be %.50s, not %.50s", expected,
520 arg == Py_None ? "None" : arg->ob_type->tp_name);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000521 return msgbuf;
522}
523
524#define CONV_UNICODE "(unicode conversion error)"
525
Guido van Rossum45aecf42006-03-15 04:58:47 +0000526/* Explicitly check for float arguments when integers are expected.
527 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000528static int
529float_argument_error(PyObject *arg)
530{
Guido van Rossum45aecf42006-03-15 04:58:47 +0000531 if (PyFloat_Check(arg)) {
532 PyErr_SetString(PyExc_TypeError,
533 "integer argument expected, got float" );
Neil Schemenauer5042da62003-02-04 20:59:40 +0000534 return 1;
Guido van Rossum45aecf42006-03-15 04:58:47 +0000535 }
Neil Schemenauer5042da62003-02-04 20:59:40 +0000536 else
537 return 0;
538}
539
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000540/* Convert a non-tuple argument. Return NULL if conversion went OK,
541 or a string with a message describing the failure. The message is
542 formatted as "must be <desired type>, not <actual type>".
543 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000544 Don't call if a tuple is expected.
545
546 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000547*/
548
549static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000550convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000551 char *msgbuf, size_t bufsize, PyObject **freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000552{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000553 /* For # codes */
554#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
555 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
556 else q=va_arg(*p_va, int*);
557#define STORE_SIZE(s) if (flags & FLAG_SIZE_T) *q2=s; else *q=s;
558#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
559
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000560 const char *format = *p_format;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000561 char c = *format++;
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000562 PyObject *uarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000563
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000564 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000565
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000566 case 'b': { /* unsigned byte -- very short int */
567 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000568 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000569 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000570 return converterr("integer<b>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000571 ival = PyLong_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000572 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000573 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000574 else if (ival < 0) {
575 PyErr_SetString(PyExc_OverflowError,
576 "unsigned byte integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000577 return converterr("integer<b>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000578 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000579 else if (ival > UCHAR_MAX) {
580 PyErr_SetString(PyExc_OverflowError,
581 "unsigned byte integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000582 return converterr("integer<b>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000583 }
584 else
585 *p = (unsigned char) ival;
586 break;
587 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000588
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000589 case 'B': {/* byte sized bitfield - both signed and unsigned
Guido van Rossum98297ee2007-11-06 21:34:58 +0000590 values allowed */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000591 char *p = va_arg(*p_va, char *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000592 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000593 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000594 return converterr("integer<B>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000595 ival = PyLong_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000596 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000597 return converterr("integer<B>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000598 else
599 *p = (unsigned char) ival;
600 break;
601 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000602
Guido van Rossumfce26e72003-04-18 00:12:30 +0000603 case 'h': {/* signed short int */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000604 short *p = va_arg(*p_va, short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000605 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000606 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000607 return converterr("integer<h>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000608 ival = PyLong_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000609 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000610 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfce26e72003-04-18 00:12:30 +0000611 else if (ival < SHRT_MIN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000612 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000613 "signed short integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000614 return converterr("integer<h>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000615 }
Guido van Rossumfce26e72003-04-18 00:12:30 +0000616 else if (ival > SHRT_MAX) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000617 PyErr_SetString(PyExc_OverflowError,
Guido van Rossumfce26e72003-04-18 00:12:30 +0000618 "signed short integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000619 return converterr("integer<h>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000620 }
621 else
622 *p = (short) ival;
623 break;
624 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000625
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000626 case 'H': { /* short int sized bitfield, both signed and
Guido van Rossum98297ee2007-11-06 21:34:58 +0000627 unsigned allowed */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000628 unsigned short *p = va_arg(*p_va, unsigned short *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000629 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000630 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000631 return converterr("integer<H>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000632 ival = PyLong_AsUnsignedLongMask(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000633 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000634 return converterr("integer<H>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000635 else
636 *p = (unsigned short) ival;
637 break;
638 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000639
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000640 case 'i': {/* signed int */
641 int *p = va_arg(*p_va, int *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000642 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000643 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000644 return converterr("integer<i>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000645 ival = PyLong_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000646 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000647 return converterr("integer<i>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000648 else if (ival > INT_MAX) {
649 PyErr_SetString(PyExc_OverflowError,
650 "signed integer is greater than maximum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000651 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000652 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000653 else if (ival < INT_MIN) {
654 PyErr_SetString(PyExc_OverflowError,
655 "signed integer is less than minimum");
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000656 return converterr("integer<i>", arg, msgbuf, bufsize);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000657 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000658 else
659 *p = ival;
660 break;
661 }
662
Thomas Hellera4ea6032003-04-17 18:55:45 +0000663 case 'I': { /* int sized bitfield, both signed and
Guido van Rossum98297ee2007-11-06 21:34:58 +0000664 unsigned allowed */
Thomas Hellera4ea6032003-04-17 18:55:45 +0000665 unsigned int *p = va_arg(*p_va, unsigned int *);
666 unsigned int ival;
667 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000668 return converterr("integer<I>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000669 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670 if (ival == (unsigned int)-1 && PyErr_Occurred())
Thomas Hellera4ea6032003-04-17 18:55:45 +0000671 return converterr("integer<I>", arg, msgbuf, bufsize);
672 else
673 *p = ival;
674 break;
675 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000676
Martin v. Löwis18e16552006-02-15 17:27:45 +0000677 case 'n': /* Py_ssize_t */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000678 {
Neal Norwitzb879f572007-08-31 05:20:36 +0000679 PyObject *iobj;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000680 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
Neal Norwitzb879f572007-08-31 05:20:36 +0000681 Py_ssize_t ival = -1;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000682 if (float_argument_error(arg))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683 return converterr("integer<n>", arg, msgbuf, bufsize);
Neal Norwitzb879f572007-08-31 05:20:36 +0000684 iobj = PyNumber_Index(arg);
Christian Heimesbcd2c082008-05-08 01:20:25 +0000685 if (iobj != NULL) {
Trent Nelson35133582008-04-22 19:02:40 +0000686 ival = PyLong_AsSsize_t(iobj);
Christian Heimesbcd2c082008-05-08 01:20:25 +0000687 Py_DECREF(iobj);
688 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000689 if (ival == -1 && PyErr_Occurred())
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690 return converterr("integer<n>", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000691 *p = ival;
692 break;
693 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000694 case 'l': {/* long int */
695 long *p = va_arg(*p_va, long *);
Neil Schemenauerb808e992003-01-24 22:15:21 +0000696 long ival;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000697 if (float_argument_error(arg))
Michael W. Hudson34553382004-08-07 17:57:16 +0000698 return converterr("integer<l>", arg, msgbuf, bufsize);
Christian Heimes217cfd12007-12-02 14:31:20 +0000699 ival = PyLong_AsLong(arg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000700 if (ival == -1 && PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000701 return converterr("integer<l>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000702 else
703 *p = ival;
704 break;
705 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000706
707 case 'k': { /* long sized bitfield */
708 unsigned long *p = va_arg(*p_va, unsigned long *);
709 unsigned long ival;
Georg Brandle1a0d112007-10-23 19:24:22 +0000710 if (PyLong_Check(arg))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000711 ival = PyLong_AsUnsignedLongMask(arg);
712 else
713 return converterr("integer<k>", arg, msgbuf, bufsize);
714 *p = ival;
715 break;
716 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000717
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000718#ifdef HAVE_LONG_LONG
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000719 case 'L': {/* PY_LONG_LONG */
720 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
721 PY_LONG_LONG ival = PyLong_AsLongLong( arg );
Christian Heimes836baa52008-02-26 08:18:30 +0000722 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred() ) {
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000723 return converterr("long<L>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000724 } else {
725 *p = ival;
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000726 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000727 break;
728 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000729
730 case 'K': { /* long long sized bitfield */
731 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
732 unsigned PY_LONG_LONG ival;
Guido van Rossumddefaf32007-01-14 03:31:43 +0000733 if (PyLong_Check(arg))
Thomas Hellera4ea6032003-04-17 18:55:45 +0000734 ival = PyLong_AsUnsignedLongLongMask(arg);
735 else
736 return converterr("integer<K>", arg, msgbuf, bufsize);
737 *p = ival;
738 break;
739 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000740#endif
Guido van Rossum98297ee2007-11-06 21:34:58 +0000741
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000742 case 'f': {/* float */
743 float *p = va_arg(*p_va, float *);
744 double dval = PyFloat_AsDouble(arg);
745 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000746 return converterr("float<f>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000747 else
748 *p = (float) dval;
749 break;
750 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000751
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000752 case 'd': {/* double */
753 double *p = va_arg(*p_va, double *);
754 double dval = PyFloat_AsDouble(arg);
755 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000756 return converterr("float<d>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000757 else
758 *p = dval;
759 break;
760 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000761
Guido van Rossum530956d1996-07-21 02:27:43 +0000762#ifndef WITHOUT_COMPLEX
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000763 case 'D': {/* complex double */
764 Py_complex *p = va_arg(*p_va, Py_complex *);
765 Py_complex cval;
766 cval = PyComplex_AsCComplex(arg);
767 if (PyErr_Occurred())
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000768 return converterr("complex<D>", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000769 else
770 *p = cval;
771 break;
772 }
Guido van Rossum530956d1996-07-21 02:27:43 +0000773#endif /* WITHOUT_COMPLEX */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000774
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000775 case 'c': {/* char */
Walter Dörwaldd0941302007-07-01 21:58:22 +0000776 char *p = va_arg(*p_va, char *);
Christian Heimes72b710a2008-05-26 13:28:38 +0000777 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
778 *p = PyBytes_AS_STRING(arg)[0];
Walter Dörwaldd0941302007-07-01 21:58:22 +0000779 else if (PyUnicode_Check(arg) &&
780 PyUnicode_GET_SIZE(arg) == 1 &&
781 PyUnicode_AS_UNICODE(arg)[0] < 256)
Amaury Forgeot d'Arc39599dc2007-11-22 02:48:12 +0000782 *p = (char)PyUnicode_AS_UNICODE(arg)[0];
Walter Dörwaldd0941302007-07-01 21:58:22 +0000783 else
784 return converterr("char < 256", arg, msgbuf, bufsize);
785 break;
786 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000787
Walter Dörwaldd0941302007-07-01 21:58:22 +0000788 case 'C': {/* unicode char */
Walter Dörwaldbc1f8862007-06-20 11:02:38 +0000789 int *p = va_arg(*p_va, int *);
Christian Heimes72b710a2008-05-26 13:28:38 +0000790 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
791 *p = PyBytes_AS_STRING(arg)[0];
Guido van Rossum09dc34f2007-05-04 04:17:33 +0000792 else if (PyUnicode_Check(arg) &&
Walter Dörwaldbc1f8862007-06-20 11:02:38 +0000793 PyUnicode_GET_SIZE(arg) == 1)
Guido van Rossum09dc34f2007-05-04 04:17:33 +0000794 *p = PyUnicode_AS_UNICODE(arg)[0];
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000795 else
Walter Dörwaldbc1f8862007-06-20 11:02:38 +0000796 return converterr("char", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000797 break;
798 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000799
800 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w', 't' codes all
801 need to be cleaned up! */
802
803 case 's': {/* text string */
Martin v. Löwis423be952008-08-13 15:53:07 +0000804 if (*format == '*') {
805 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
806
807 if (PyUnicode_Check(arg)) {
808 uarg = UNICODE_DEFAULT_ENCODING(arg);
809 if (uarg == NULL)
810 return converterr(CONV_UNICODE,
811 arg, msgbuf, bufsize);
812 PyBuffer_FillInfo(p, arg,
813 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
814 1, 0);
815 }
816 else { /* any buffer-like object */
817 char *buf;
818 if (getbuffer(arg, p, &buf) < 0)
819 return converterr(buf, arg, msgbuf, bufsize);
820 }
Antoine Pitrouf71995782008-08-29 18:37:05 +0000821 if (addcleanup(p, freelist, cleanup_buffer)) {
822 return converterr(
823 "(cleanup problem)",
824 arg, msgbuf, bufsize);
825 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000826 format++;
827 } else if (*format == '#') {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000828 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000829 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000830
831 if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000832 uarg = UNICODE_DEFAULT_ENCODING(arg);
833 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000834 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000835 arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +0000836 *p = PyBytes_AS_STRING(uarg);
837 STORE_SIZE(PyBytes_GET_SIZE(uarg));
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000838 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000839 else { /* any buffer-like object */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000840 /* XXX Really? */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000841 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000842 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000843 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000844 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000845 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000846 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +0000847 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000848 } else {
849 char **p = va_arg(*p_va, char **);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000850
851 if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000852 uarg = UNICODE_DEFAULT_ENCODING(arg);
853 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000854 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000855 arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +0000856 *p = PyBytes_AS_STRING(uarg);
Marc-André Lemburg6f15e572001-05-02 17:16:16 +0000857 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000858 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000859 return converterr("string", arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +0000860 if ((Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000861 return converterr("string without null bytes",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000862 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000863 }
864 break;
865 }
866
Guido van Rossum98297ee2007-11-06 21:34:58 +0000867 case 'y': {/* any buffer-like object, but not PyUnicode */
868 void **p = (void **)va_arg(*p_va, char **);
869 char *buf;
Martin v. Löwis423be952008-08-13 15:53:07 +0000870 Py_ssize_t count;
871 if (*format == '*') {
872 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
873 return converterr(buf, arg, msgbuf, bufsize);
874 format++;
Antoine Pitrouf71995782008-08-29 18:37:05 +0000875 if (addcleanup(p, freelist, cleanup_buffer)) {
876 return converterr(
877 "(cleanup problem)",
878 arg, msgbuf, bufsize);
879 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000880 break;
881 }
882 count = convertbuffer(arg, p, &buf);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000883 if (count < 0)
884 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis423be952008-08-13 15:53:07 +0000885 else if (*format == '#') {
Walter Dörwald612344f2007-05-04 19:28:21 +0000886 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000887 STORE_SIZE(count);
Walter Dörwald612344f2007-05-04 19:28:21 +0000888 format++;
Walter Dörwald612344f2007-05-04 19:28:21 +0000889 }
890 break;
891 }
892
Guido van Rossum98297ee2007-11-06 21:34:58 +0000893 case 'z': {/* like 's' or 's#', but None is okay, stored as NULL */
Martin v. Löwis423be952008-08-13 15:53:07 +0000894 if (*format == '*') {
895 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
896
897 if (arg == Py_None)
898 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
899 else if (PyUnicode_Check(arg)) {
900 uarg = UNICODE_DEFAULT_ENCODING(arg);
901 if (uarg == NULL)
902 return converterr(CONV_UNICODE,
903 arg, msgbuf, bufsize);
904 PyBuffer_FillInfo(p, arg,
905 PyBytes_AS_STRING(uarg), PyBytes_GET_SIZE(uarg),
906 1, 0);
907 }
908 else { /* any buffer-like object */
909 char *buf;
910 if (getbuffer(arg, p, &buf) < 0)
911 return converterr(buf, arg, msgbuf, bufsize);
912 }
Antoine Pitrouf71995782008-08-29 18:37:05 +0000913 if (addcleanup(p, freelist, cleanup_buffer)) {
914 return converterr(
915 "(cleanup problem)",
916 arg, msgbuf, bufsize);
917 }
Martin v. Löwis423be952008-08-13 15:53:07 +0000918 format++;
919 } else if (*format == '#') { /* any buffer-like object */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000920 void **p = (void **)va_arg(*p_va, char **);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000921 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000922
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000923 if (arg == Py_None) {
924 *p = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000925 STORE_SIZE(0);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000926 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000927 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000928 uarg = UNICODE_DEFAULT_ENCODING(arg);
929 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000930 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000931 arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +0000932 *p = PyBytes_AS_STRING(uarg);
933 STORE_SIZE(PyBytes_GET_SIZE(uarg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000934 }
935 else { /* any buffer-like object */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000936 /* XXX Really? */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000937 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000938 Py_ssize_t count = convertbuffer(arg, p, &buf);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000939 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000940 return converterr(buf, arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000941 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000942 }
943 format++;
944 } else {
945 char **p = va_arg(*p_va, char **);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +0000946 uarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000947
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000948 if (arg == Py_None)
949 *p = 0;
Christian Heimes72b710a2008-05-26 13:28:38 +0000950 else if (PyBytes_Check(arg)) {
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +0000951 /* Enable null byte check below */
952 uarg = arg;
Christian Heimes72b710a2008-05-26 13:28:38 +0000953 *p = PyBytes_AS_STRING(arg);
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +0000954 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000955 else if (PyUnicode_Check(arg)) {
Jeremy Hylton77b8b672001-09-10 01:54:43 +0000956 uarg = UNICODE_DEFAULT_ENCODING(arg);
957 if (uarg == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000958 return converterr(CONV_UNICODE,
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000959 arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +0000960 *p = PyBytes_AS_STRING(uarg);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000961 }
962 else
Guido van Rossum98297ee2007-11-06 21:34:58 +0000963 return converterr("string or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000964 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000965 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000966 FETCH_SIZE;
Thomas Woutersc3547a32006-03-01 21:31:21 +0000967 assert(0); /* XXX redundant with if-case */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000968 if (arg == Py_None) {
969 STORE_SIZE(0);
970 }
971 else {
Christian Heimes72b710a2008-05-26 13:28:38 +0000972 STORE_SIZE(PyBytes_Size(arg));
Guido van Rossum98297ee2007-11-06 21:34:58 +0000973 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000974 format++;
975 }
Amaury Forgeot d'Arc07404592008-05-12 13:19:07 +0000976 else if (*p != NULL && uarg != NULL &&
Christian Heimes72b710a2008-05-26 13:28:38 +0000977 (Py_ssize_t) strlen(*p) != PyBytes_GET_SIZE(uarg))
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000978 return converterr(
Guido van Rossum98297ee2007-11-06 21:34:58 +0000979 "string without null bytes or None",
Jeremy Hyltonb048b262001-11-28 22:14:37 +0000980 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000981 }
982 break;
983 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000984
Guido van Rossumfb67be22007-08-29 18:38:11 +0000985 case 'Z': {/* unicode, may be NULL (None) */
986 if (*format == '#') { /* any buffer-like object */
987 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
988 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000989
Guido van Rossumfb67be22007-08-29 18:38:11 +0000990 if (arg == Py_None) {
991 *p = 0;
992 STORE_SIZE(0);
993 }
994 else if (PyUnicode_Check(arg)) {
995 *p = PyUnicode_AS_UNICODE(arg);
996 STORE_SIZE(PyUnicode_GET_SIZE(arg));
997 }
998 format++;
999 } else {
1000 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001001
Guido van Rossumfb67be22007-08-29 18:38:11 +00001002 if (arg == Py_None)
1003 *p = 0;
1004 else if (PyUnicode_Check(arg))
1005 *p = PyUnicode_AS_UNICODE(arg);
1006 else
Guido van Rossum98297ee2007-11-06 21:34:58 +00001007 return converterr("string or None",
Guido van Rossumfb67be22007-08-29 18:38:11 +00001008 arg, msgbuf, bufsize);
1009 }
1010 break;
1011 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001012
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001013 case 'e': {/* encoded string */
1014 char **buffer;
1015 const char *encoding;
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001016 PyObject *s;
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001017 int recode_strings;
1018 Py_ssize_t size;
Guido van Rossumd70539a2007-05-09 23:35:09 +00001019 const char *ptr;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001020
1021 /* Get 'e' parameter: the encoding name */
1022 encoding = (const char *)va_arg(*p_va, const char *);
1023 if (encoding == NULL)
1024 encoding = PyUnicode_GetDefaultEncoding();
Guido van Rossum98297ee2007-11-06 21:34:58 +00001025
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001026 /* Get output buffer parameter:
1027 's' (recode all objects via Unicode) or
Guido van Rossum98297ee2007-11-06 21:34:58 +00001028 't' (only recode non-string objects)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001029 */
1030 if (*format == 's')
1031 recode_strings = 1;
1032 else if (*format == 't')
1033 recode_strings = 0;
1034 else
1035 return converterr(
1036 "(unknown parser marker combination)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001037 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001038 buffer = (char **)va_arg(*p_va, char **);
1039 format++;
1040 if (buffer == NULL)
Guido van Rossum98297ee2007-11-06 21:34:58 +00001041 return converterr("(buffer is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001042 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001043
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001044 /* Encode object */
Guido van Rossumd70539a2007-05-09 23:35:09 +00001045 if (!recode_strings &&
Christian Heimes72b710a2008-05-26 13:28:38 +00001046 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001047 s = arg;
1048 Py_INCREF(s);
Guido van Rossumd70539a2007-05-09 23:35:09 +00001049 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1050 return converterr("(AsCharBuffer failed)",
1051 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001052 }
1053 else {
Martin v. Löwis339d0f72001-08-17 18:39:25 +00001054 PyObject *u;
1055
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001056 /* Convert object to Unicode */
1057 u = PyUnicode_FromObject(arg);
1058 if (u == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001059 return converterr(
Guido van Rossum98297ee2007-11-06 21:34:58 +00001060 "string or unicode or text buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001061 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001062
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001063 /* Encode object; use default error handling */
1064 s = PyUnicode_AsEncodedString(u,
1065 encoding,
1066 NULL);
1067 Py_DECREF(u);
1068 if (s == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001069 return converterr("(encoding failed)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001070 arg, msgbuf, bufsize);
Christian Heimes72b710a2008-05-26 13:28:38 +00001071 if (!PyBytes_Check(s)) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001072 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001073 return converterr(
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001074 "(encoder failed to return bytes)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001075 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001076 }
Christian Heimes72b710a2008-05-26 13:28:38 +00001077 size = PyBytes_GET_SIZE(s);
1078 ptr = PyBytes_AS_STRING(s);
Guido van Rossumd70539a2007-05-09 23:35:09 +00001079 if (ptr == NULL)
1080 ptr = "";
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001081 }
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001082
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001083 /* Write output; output is guaranteed to be 0-terminated */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001084 if (*format == '#') {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001085 /* Using buffer length parameter '#':
Guido van Rossum98297ee2007-11-06 21:34:58 +00001086
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001087 - if *buffer is NULL, a new buffer of the
1088 needed size is allocated and the data
1089 copied into it; *buffer is updated to point
1090 to the new buffer; the caller is
1091 responsible for PyMem_Free()ing it after
Guido van Rossum98297ee2007-11-06 21:34:58 +00001092 usage
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001093
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001094 - if *buffer is not NULL, the data is
1095 copied to *buffer; *buffer_len has to be
1096 set to the size of the buffer on input;
1097 buffer overflow is signalled with an error;
1098 buffer has to provide enough room for the
1099 encoded string plus the trailing 0-byte
Guido van Rossum98297ee2007-11-06 21:34:58 +00001100
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001101 - in both cases, *buffer_len is updated to
1102 the size of the buffer /excluding/ the
Guido van Rossum98297ee2007-11-06 21:34:58 +00001103 trailing 0-byte
1104
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001105 */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001106 FETCH_SIZE;
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001107
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001108 format++;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001109 if (q == NULL && q2 == NULL) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001110 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001111 return converterr(
1112 "(buffer_len is NULL)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001113 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001114 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001115 if (*buffer == NULL) {
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001116 *buffer = PyMem_NEW(char, size + 1);
1117 if (*buffer == NULL) {
1118 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001119 return converterr(
1120 "(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001121 arg, msgbuf, bufsize);
Guido van Rossumd8855fd2000-03-24 22:14:19 +00001122 }
Antoine Pitrouf71995782008-08-29 18:37:05 +00001123 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001124 Py_DECREF(s);
1125 return converterr(
1126 "(cleanup problem)",
1127 arg, msgbuf, bufsize);
1128 }
Fred Drake25871c02000-05-03 15:17:02 +00001129 } else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001130 if (size + 1 > BUFFER_LEN) {
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001131 Py_DECREF(s);
1132 return converterr(
Guido van Rossum98297ee2007-11-06 21:34:58 +00001133 "(buffer overflow)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001134 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001135 }
Fred Drake25871c02000-05-03 15:17:02 +00001136 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001137 memcpy(*buffer, ptr, size+1);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001138 STORE_SIZE(size);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001139 } else {
1140 /* Using a 0-terminated buffer:
Guido van Rossum98297ee2007-11-06 21:34:58 +00001141
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001142 - the encoded string has to be 0-terminated
1143 for this variant to work; if it is not, an
Guido van Rossum98297ee2007-11-06 21:34:58 +00001144 error raised
Fred Drake25871c02000-05-03 15:17:02 +00001145
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001146 - a new buffer of the needed size is
1147 allocated and the data copied into it;
1148 *buffer is updated to point to the new
1149 buffer; the caller is responsible for
1150 PyMem_Free()ing it after usage
1151
1152 */
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001153 if ((Py_ssize_t)strlen(ptr) != size) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001154 Py_DECREF(s);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001155 return converterr(
1156 "(encoded string without NULL bytes)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001157 arg, msgbuf, bufsize);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001158 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001159 *buffer = PyMem_NEW(char, size + 1);
1160 if (*buffer == NULL) {
1161 Py_DECREF(s);
1162 return converterr("(memory error)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001163 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001164 }
Antoine Pitrouf71995782008-08-29 18:37:05 +00001165 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001166 Py_DECREF(s);
1167 return converterr("(cleanup problem)",
1168 arg, msgbuf, bufsize);
1169 }
Guido van Rossumf15a29f2007-05-04 00:41:39 +00001170 memcpy(*buffer, ptr, size+1);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001171 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001172 Py_DECREF(s);
1173 break;
1174 }
1175
1176 case 'u': {/* raw unicode buffer (Py_UNICODE *) */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001177 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1178 if (!PyUnicode_Check(arg))
1179 return converterr("str", arg, msgbuf, bufsize);
1180 *p = PyUnicode_AS_UNICODE(arg);
1181 if (*format == '#') { /* store pointer and size */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001182 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001183 STORE_SIZE(PyUnicode_GET_SIZE(arg));
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001184 format++;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001185 }
1186 break;
1187 }
1188
Christian Heimes72b710a2008-05-26 13:28:38 +00001189 case 'S': { /* PyBytes object */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001190 PyObject **p = va_arg(*p_va, PyObject **);
Christian Heimes72b710a2008-05-26 13:28:38 +00001191 if (PyBytes_Check(arg))
Guido van Rossum617dbc42007-05-07 23:57:08 +00001192 *p = arg;
1193 else
1194 return converterr("bytes", arg, msgbuf, bufsize);
1195 break;
1196 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001197
Christian Heimes9c4756e2008-05-26 13:22:05 +00001198 case 'Y': { /* PyByteArray object */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001199 PyObject **p = va_arg(*p_va, PyObject **);
Christian Heimes9c4756e2008-05-26 13:22:05 +00001200 if (PyByteArray_Check(arg))
Guido van Rossum98297ee2007-11-06 21:34:58 +00001201 *p = arg;
1202 else
1203 return converterr("buffer", arg, msgbuf, bufsize);
1204 break;
1205 }
1206
1207 case 'U': { /* PyUnicode object */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001208 PyObject **p = va_arg(*p_va, PyObject **);
1209 if (PyUnicode_Check(arg))
1210 *p = arg;
1211 else
Guido van Rossum98297ee2007-11-06 21:34:58 +00001212 return converterr("str", arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001213 break;
1214 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001215
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001216 case 'O': { /* object */
1217 PyTypeObject *type;
1218 PyObject **p;
1219 if (*format == '!') {
1220 type = va_arg(*p_va, PyTypeObject*);
1221 p = va_arg(*p_va, PyObject **);
1222 format++;
Guido van Rossumcbfc8552001-08-28 16:37:51 +00001223 if (PyType_IsSubtype(arg->ob_type, type))
Guido van Rossume826ef02000-03-10 23:02:17 +00001224 *p = arg;
1225 else
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001226 return converterr(type->tp_name, arg, msgbuf, bufsize);
Guido van Rossumfccfe891998-05-15 22:04:07 +00001227
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001228 }
1229 else if (*format == '?') {
1230 inquiry pred = va_arg(*p_va, inquiry);
1231 p = va_arg(*p_va, PyObject **);
1232 format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001233 if ((*pred)(arg))
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001234 *p = arg;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001235 else
Guido van Rossum98297ee2007-11-06 21:34:58 +00001236 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001237 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001238
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001239 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001240 else if (*format == '&') {
1241 typedef int (*converter)(PyObject *, void *);
1242 converter convert = va_arg(*p_va, converter);
1243 void *addr = va_arg(*p_va, void *);
1244 format++;
1245 if (! (*convert)(arg, addr))
Guido van Rossum98297ee2007-11-06 21:34:58 +00001246 return converterr("(unspecified)",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001247 arg, msgbuf, bufsize);
Guido van Rossumb317f8a1998-10-08 02:21:21 +00001248 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001249 else {
1250 p = va_arg(*p_va, PyObject **);
1251 *p = arg;
1252 }
1253 break;
1254 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001255
1256
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001257 case 'w': { /* memory buffer, read-write access */
1258 void **p = va_arg(*p_va, void **);
1259 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Christian Heimes4e30a842007-11-30 22:12:06 +00001260 Py_ssize_t count;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001261 int temp=-1;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001262 Py_buffer view;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001263
Martin v. Löwis423be952008-08-13 15:53:07 +00001264 if (pb && pb->bf_releasebuffer && *format != '*')
1265 /* Buffer must be released, yet caller does not use
1266 the Py_buffer protocol. */
1267 return converterr("pinned buffer", arg, msgbuf, bufsize);
1268
1269
1270 if (pb && pb->bf_getbuffer && *format == '*') {
1271 /* Caller is interested in Py_buffer, and the object
1272 supports it directly. */
1273 format++;
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001274 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
Martin v. Löwis423be952008-08-13 15:53:07 +00001275 PyErr_Clear();
1276 return converterr("read-write buffer", arg, msgbuf, bufsize);
1277 }
Antoine Pitrouf71995782008-08-29 18:37:05 +00001278 if (addcleanup(p, freelist, cleanup_buffer)) {
1279 return converterr(
1280 "(cleanup problem)",
1281 arg, msgbuf, bufsize);
1282 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001283 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C'))
1284 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1285 break;
1286 }
1287
1288 /* Here we have processed w*, only w and w# remain. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001289 if (pb == NULL ||
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001290 pb->bf_getbuffer == NULL ||
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001291 ((temp = PyObject_GetBuffer(arg, &view,
1292 PyBUF_SIMPLE)) != 0) ||
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001293 view.readonly == 1) {
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001294 if (temp==0) {
1295 PyBuffer_Release(&view);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001296 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001297 return converterr("single-segment read-write buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001298 arg, msgbuf, bufsize);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001299 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001300
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001301 if ((count = view.len) < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001302 return converterr("(unspecified)", arg, msgbuf, bufsize);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001303 *p = view.buf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001304 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001305 FETCH_SIZE;
1306 STORE_SIZE(count);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001307 format++;
1308 }
1309 break;
1310 }
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001311
1312 /*TEO: This can be eliminated --- here only for backward
1313 compatibility */
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001314 case 't': { /* 8-bit character buffer, read-only access */
Martin v. Löwis15e62742006-02-27 16:46:16 +00001315 char **p = va_arg(*p_va, char **);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001316 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Christian Heimes4e30a842007-11-30 22:12:06 +00001317 Py_ssize_t count;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001318 Py_buffer view;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001319
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001320 if (*format++ != '#')
1321 return converterr(
Guido van Rossum98297ee2007-11-06 21:34:58 +00001322 "invalid use of 't' format character",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001323 arg, msgbuf, bufsize);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001324 if (pb == NULL || pb->bf_getbuffer == NULL)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001325 return converterr(
Alexandre Vassalotti70a23712007-10-14 02:05:51 +00001326 "bytes or read-only character buffer",
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001327 arg, msgbuf, bufsize);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001328
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001329 if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) != 0)
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001330 return converterr("string or single-segment read-only buffer",
1331 arg, msgbuf, bufsize);
Jeremy Hylton4819e972001-10-11 14:40:37 +00001332
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001333 count = view.len;
1334 *p = view.buf;
Martin v. Löwis423be952008-08-13 15:53:07 +00001335 if (pb->bf_releasebuffer)
1336 return converterr(
1337 "string or pinned buffer",
1338 arg, msgbuf, bufsize);
1339
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001340 PyBuffer_Release(&view);
1341
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001342 if (count < 0)
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001343 return converterr("(unspecified)", arg, msgbuf, bufsize);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001344 {
1345 FETCH_SIZE;
1346 STORE_SIZE(count);
1347 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001348 break;
1349 }
1350
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001351 default:
Jeremy Hyltonb048b262001-11-28 22:14:37 +00001352 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001353
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001354 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001355
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001356 *p_format = format;
1357 return NULL;
1358}
Guido van Rossumaa354651996-08-19 19:32:04 +00001359
Martin v. Löwis18e16552006-02-15 17:27:45 +00001360static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001361convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001362{
1363 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001364 Py_ssize_t count;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +00001365 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001366
1367 *errmsg = NULL;
1368 *p = NULL;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001369 if (pb == NULL ||
Martin v. Löwis423be952008-08-13 15:53:07 +00001370 pb->bf_getbuffer == NULL ||
1371 pb->bf_releasebuffer != NULL) {
Guido van Rossumb0834002007-11-21 21:53:51 +00001372 *errmsg = "bytes or read-only buffer";
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001373 return -1;
1374 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001375
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001376 if (PyObject_GetBuffer(arg, &view, PyBUF_SIMPLE) != 0) {
Guido van Rossumb0834002007-11-21 21:53:51 +00001377 *errmsg = "bytes or single-segment read-only buffer";
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001378 return -1;
1379 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001380 count = view.len;
1381 *p = view.buf;
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001382 PyBuffer_Release(&view);
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001383 return count;
1384}
Guido van Rossumaa354651996-08-19 19:32:04 +00001385
Martin v. Löwis423be952008-08-13 15:53:07 +00001386/* XXX for 3.x, getbuffer and convertbuffer can probably
1387 be merged again. */
1388static int
Neal Norwitz2f99b242008-08-24 05:48:10 +00001389getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001390{
1391 void *buf;
1392 Py_ssize_t count;
1393 PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
1394 if (pb == NULL) {
Benjamin Peterson81d90a22008-12-27 02:58:34 +00001395 *errmsg = "bytes or buffer";
Martin v. Löwis423be952008-08-13 15:53:07 +00001396 return -1;
1397 }
1398 if (pb->bf_getbuffer) {
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001399 if (PyObject_GetBuffer(arg, view, 0) < 0) {
Neal Norwitz2f99b242008-08-24 05:48:10 +00001400 *errmsg = "convertible to a buffer";
Martin v. Löwis423be952008-08-13 15:53:07 +00001401 return -1;
Neal Norwitz2f99b242008-08-24 05:48:10 +00001402 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001403 if (!PyBuffer_IsContiguous(view, 'C')) {
1404 *errmsg = "contiguous buffer";
1405 return -1;
1406 }
1407 return 0;
1408 }
1409
1410 count = convertbuffer(arg, &buf, errmsg);
Neal Norwitz2f99b242008-08-24 05:48:10 +00001411 if (count < 0) {
1412 *errmsg = "convertible to a buffer";
Martin v. Löwis423be952008-08-13 15:53:07 +00001413 return count;
Neal Norwitz2f99b242008-08-24 05:48:10 +00001414 }
Martin v. Löwis423be952008-08-13 15:53:07 +00001415 PyBuffer_FillInfo(view, NULL, buf, count, 1, 0);
1416 return 0;
1417}
1418
Guido van Rossumaa354651996-08-19 19:32:04 +00001419/* Support for keyword arguments donated by
1420 Geoff Philbrick <philbric@delphi.hks.com> */
1421
Tim Petersf8cd3e82001-10-27 04:26:57 +00001422/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001423int
1424PyArg_ParseTupleAndKeywords(PyObject *args,
1425 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001426 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001427 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001428{
1429 int retval;
1430 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001431
1432 if ((args == NULL || !PyTuple_Check(args)) ||
1433 (keywords != NULL && !PyDict_Check(keywords)) ||
1434 format == NULL ||
1435 kwlist == NULL)
1436 {
1437 PyErr_BadInternalCall();
Tim Petersf8cd3e82001-10-27 04:26:57 +00001438 return 0;
Tim Peters45772cd2001-10-27 03:58:40 +00001439 }
1440
Guido van Rossumaa354651996-08-19 19:32:04 +00001441 va_start(va, kwlist);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001442 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001443 va_end(va);
1444 return retval;
1445}
1446
1447int
1448_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
1449 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001450 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001451 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001452{
1453 int retval;
1454 va_list va;
1455
1456 if ((args == NULL || !PyTuple_Check(args)) ||
1457 (keywords != NULL && !PyDict_Check(keywords)) ||
1458 format == NULL ||
1459 kwlist == NULL)
1460 {
1461 PyErr_BadInternalCall();
1462 return 0;
1463 }
1464
1465 va_start(va, kwlist);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001466 retval = vgetargskeywords(args, keywords, format,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001467 kwlist, &va, FLAG_SIZE_T);
Guido van Rossumaa354651996-08-19 19:32:04 +00001468 va_end(va);
1469 return retval;
1470}
1471
1472
Brett Cannon711e7d92004-07-10 22:20:32 +00001473int
1474PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001475 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001476 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001477 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001478{
1479 int retval;
1480 va_list lva;
1481
1482 if ((args == NULL || !PyTuple_Check(args)) ||
1483 (keywords != NULL && !PyDict_Check(keywords)) ||
1484 format == NULL ||
1485 kwlist == NULL)
1486 {
1487 PyErr_BadInternalCall();
1488 return 0;
1489 }
1490
1491#ifdef VA_LIST_IS_ARRAY
1492 memcpy(lva, va, sizeof(va_list));
1493#else
1494#ifdef __va_copy
1495 __va_copy(lva, va);
1496#else
1497 lva = va;
1498#endif
1499#endif
1500
Guido van Rossum98297ee2007-11-06 21:34:58 +00001501 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001502 return retval;
1503}
1504
1505int
1506_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
1507 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001508 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001509 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001510{
1511 int retval;
1512 va_list lva;
1513
1514 if ((args == NULL || !PyTuple_Check(args)) ||
1515 (keywords != NULL && !PyDict_Check(keywords)) ||
1516 format == NULL ||
1517 kwlist == NULL)
1518 {
1519 PyErr_BadInternalCall();
1520 return 0;
1521 }
1522
1523#ifdef VA_LIST_IS_ARRAY
1524 memcpy(lva, va, sizeof(va_list));
1525#else
1526#ifdef __va_copy
1527 __va_copy(lva, va);
1528#else
1529 lva = va;
1530#endif
1531#endif
1532
Guido van Rossum98297ee2007-11-06 21:34:58 +00001533 retval = vgetargskeywords(args, keywords, format,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001534 kwlist, &lva, FLAG_SIZE_T);
Brett Cannon711e7d92004-07-10 22:20:32 +00001535 return retval;
1536}
1537
Christian Heimes380f7f22008-02-28 11:19:05 +00001538#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001539
Guido van Rossumaa354651996-08-19 19:32:04 +00001540static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001541vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001542 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001543{
Tim Petersdc5eff92001-10-27 06:53:00 +00001544 char msgbuf[512];
Guido van Rossumaa354651996-08-19 19:32:04 +00001545 int levels[32];
Christian Heimes380f7f22008-02-28 11:19:05 +00001546 const char *fname, *msg, *custom_msg, *keyword;
1547 int min = INT_MAX;
Tim Petersb639d492001-10-27 07:00:56 +00001548 int i, len, nargs, nkeywords;
Christian Heimes380f7f22008-02-28 11:19:05 +00001549 PyObject *freelist = NULL, *current_arg;
Tim Petersf4331c12001-10-27 00:17:34 +00001550
Tim Peters45772cd2001-10-27 03:58:40 +00001551 assert(args != NULL && PyTuple_Check(args));
1552 assert(keywords == NULL || PyDict_Check(keywords));
1553 assert(format != NULL);
1554 assert(kwlist != NULL);
1555 assert(p_va != NULL);
1556
Christian Heimes380f7f22008-02-28 11:19:05 +00001557 /* grab the function name or custom error msg first (mutually exclusive) */
1558 fname = strchr(format, ':');
1559 if (fname) {
1560 fname++;
1561 custom_msg = NULL;
Tim Peters62d48e12001-10-27 06:42:16 +00001562 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001563 else {
1564 custom_msg = strchr(format,';');
1565 if (custom_msg)
1566 custom_msg++;
Tim Peters62d48e12001-10-27 06:42:16 +00001567 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001568
1569 /* scan kwlist and get greatest possible nbr of args */
1570 for (len=0; kwlist[len]; len++)
1571 continue;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001572
Tim Peters6fb26352001-10-27 04:38:11 +00001573 nargs = PyTuple_GET_SIZE(args);
Christian Heimes380f7f22008-02-28 11:19:05 +00001574 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1575 if (nargs + nkeywords > len) {
1576 PyErr_Format(PyExc_TypeError, "%s%s takes at most %d "
1577 "argument%s (%d given)",
1578 (fname == NULL) ? "function" : fname,
1579 (fname == NULL) ? "" : "()",
1580 len,
1581 (len == 1) ? "" : "s",
1582 nargs + nkeywords);
Guido van Rossumaa354651996-08-19 19:32:04 +00001583 return 0;
1584 }
Tim Petersc2f01122001-10-27 07:25:06 +00001585
Christian Heimes380f7f22008-02-28 11:19:05 +00001586 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1587 for (i = 0; i < len; i++) {
1588 keyword = kwlist[i];
1589 if (*format == '|') {
1590 min = i;
Guido van Rossumaa354651996-08-19 19:32:04 +00001591 format++;
Christian Heimes380f7f22008-02-28 11:19:05 +00001592 }
1593 if (IS_END_OF_FORMAT(*format)) {
1594 PyErr_Format(PyExc_RuntimeError,
1595 "More keyword list entries (%d) than "
1596 "format specifiers (%d)", len, i);
1597 return cleanreturn(0, freelist);
1598 }
1599 current_arg = NULL;
1600 if (nkeywords) {
1601 current_arg = PyDict_GetItemString(keywords, keyword);
1602 }
1603 if (current_arg) {
1604 --nkeywords;
1605 if (i < nargs) {
1606 /* arg present in tuple and in dict */
1607 PyErr_Format(PyExc_TypeError,
1608 "Argument given by name ('%s') "
1609 "and position (%d)",
1610 keyword, i+1);
1611 return cleanreturn(0, freelist);
1612 }
1613 }
1614 else if (nkeywords && PyErr_Occurred())
1615 return cleanreturn(0, freelist);
1616 else if (i < nargs)
1617 current_arg = PyTuple_GET_ITEM(args, i);
1618
1619 if (current_arg) {
1620 msg = convertitem(current_arg, &format, p_va, flags,
1621 levels, msgbuf, sizeof(msgbuf), &freelist);
1622 if (msg) {
1623 seterror(i+1, msg, levels, fname, custom_msg);
1624 return cleanreturn(0, freelist);
1625 }
1626 continue;
1627 }
1628
1629 if (i < min) {
1630 PyErr_Format(PyExc_TypeError, "Required argument "
1631 "'%s' (pos %d) not found",
1632 keyword, i+1);
1633 return cleanreturn(0, freelist);
1634 }
1635 /* current code reports success when all required args
1636 * fulfilled and no keyword args left, with no further
1637 * validation. XXX Maybe skip this in debug build ?
1638 */
1639 if (!nkeywords)
1640 return cleanreturn(1, freelist);
1641
1642 /* We are into optional args, skip thru to any remaining
1643 * keyword args */
1644 msg = skipitem(&format, p_va, flags);
Guido van Rossumaa354651996-08-19 19:32:04 +00001645 if (msg) {
Christian Heimes380f7f22008-02-28 11:19:05 +00001646 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1647 format);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001648 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001649 }
1650 }
1651
Benjamin Peterson92035012008-12-27 16:00:54 +00001652 if (!IS_END_OF_FORMAT(*format) && *format != '|') {
Christian Heimes380f7f22008-02-28 11:19:05 +00001653 PyErr_Format(PyExc_RuntimeError,
1654 "more argument specifiers than keyword list entries "
1655 "(remaining format:'%s')", format);
1656 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001657 }
Tim Petersb054be42001-10-27 05:07:41 +00001658
Guido van Rossumaa354651996-08-19 19:32:04 +00001659 /* make sure there are no extraneous keyword arguments */
Tim Petersc2f01122001-10-27 07:25:06 +00001660 if (nkeywords > 0) {
1661 PyObject *key, *value;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001662 Py_ssize_t pos = 0;
Guido van Rossumaa354651996-08-19 19:32:04 +00001663 while (PyDict_Next(keywords, &pos, &key, &value)) {
Tim Petersc2f01122001-10-27 07:25:06 +00001664 int match = 0;
Guido van Rossum55474762002-04-04 16:22:30 +00001665 char *ks;
Georg Brandld8b690f2008-05-16 17:28:50 +00001666 if (!PyUnicode_Check(key)) {
Guido van Rossum98297ee2007-11-06 21:34:58 +00001667 PyErr_SetString(PyExc_TypeError,
Guido van Rossum55474762002-04-04 16:22:30 +00001668 "keywords must be strings");
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001669 return cleanreturn(0, freelist);
Guido van Rossum55474762002-04-04 16:22:30 +00001670 }
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001671 ks = _PyUnicode_AsString(key);
Christian Heimes380f7f22008-02-28 11:19:05 +00001672 for (i = 0; i < len; i++) {
Guido van Rossumaa354651996-08-19 19:32:04 +00001673 if (!strcmp(ks, kwlist[i])) {
1674 match = 1;
1675 break;
1676 }
1677 }
1678 if (!match) {
Tim Petersc2f01122001-10-27 07:25:06 +00001679 PyErr_Format(PyExc_TypeError,
1680 "'%s' is an invalid keyword "
1681 "argument for this function",
1682 ks);
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001683 return cleanreturn(0, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001684 }
1685 }
1686 }
Tim Petersc2f01122001-10-27 07:25:06 +00001687
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +00001688 return cleanreturn(1, freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001689}
1690
1691
1692static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001693skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001694{
Christian Heimes380f7f22008-02-28 11:19:05 +00001695 const char *format = *p_format;
Guido van Rossumaa354651996-08-19 19:32:04 +00001696 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001697
Guido van Rossumaa354651996-08-19 19:32:04 +00001698 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001699
1700 /* simple codes
1701 * The individual types (second arg of va_arg) are irrelevant */
1702
Guido van Rossumaa354651996-08-19 19:32:04 +00001703 case 'b': /* byte -- very short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001704 case 'B': /* byte as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001705 case 'h': /* short int */
Jack Jansencc22fbe2000-08-05 21:29:58 +00001706 case 'H': /* short int as bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001707 case 'i': /* int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001708 case 'I': /* int sized bitfield */
Guido van Rossumaa354651996-08-19 19:32:04 +00001709 case 'l': /* long int */
Georg Brandl6dd14612005-09-14 19:29:53 +00001710 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001711#ifdef HAVE_LONG_LONG
Georg Brandl6dd14612005-09-14 19:29:53 +00001712 case 'L': /* PY_LONG_LONG */
1713 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001714#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001715 case 'f': /* float */
Guido van Rossumaa354651996-08-19 19:32:04 +00001716 case 'd': /* double */
Guido van Rossumaa354651996-08-19 19:32:04 +00001717#ifndef WITHOUT_COMPLEX
1718 case 'D': /* complex double */
Georg Brandl6dd14612005-09-14 19:29:53 +00001719#endif
Guido van Rossumaa354651996-08-19 19:32:04 +00001720 case 'c': /* char */
1721 {
Georg Brandl6dd14612005-09-14 19:29:53 +00001722 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001723 break;
1724 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001725
1726 case 'n': /* Py_ssize_t */
1727 {
1728 (void) va_arg(*p_va, Py_ssize_t *);
1729 break;
1730 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001731
Georg Brandl6dd14612005-09-14 19:29:53 +00001732 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001733
Georg Brandl6dd14612005-09-14 19:29:53 +00001734 case 'e': /* string with encoding */
1735 {
1736 (void) va_arg(*p_va, const char *);
1737 if (!(*format == 's' || *format == 't'))
1738 /* after 'e', only 's' and 't' is allowed */
1739 goto err;
1740 format++;
1741 /* explicit fallthrough to string cases */
1742 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001743
Guido van Rossumaa354651996-08-19 19:32:04 +00001744 case 's': /* string */
Georg Brandl6dd14612005-09-14 19:29:53 +00001745 case 'z': /* string or None */
Walter Dörwald612344f2007-05-04 19:28:21 +00001746 case 'y': /* bytes */
Georg Brandl6dd14612005-09-14 19:29:53 +00001747 case 'u': /* unicode string */
Georg Brandl6dd14612005-09-14 19:29:53 +00001748 case 't': /* buffer, read-only */
1749 case 'w': /* buffer, read-write */
Guido van Rossumaa354651996-08-19 19:32:04 +00001750 {
Guido van Rossum80bb9651996-12-05 23:27:02 +00001751 (void) va_arg(*p_va, char **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001752 if (*format == '#') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001753 if (flags & FLAG_SIZE_T)
1754 (void) va_arg(*p_va, Py_ssize_t *);
1755 else
1756 (void) va_arg(*p_va, int *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001757 format++;
Martin v. Löwis423be952008-08-13 15:53:07 +00001758 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
1759 format++;
Guido van Rossumaa354651996-08-19 19:32:04 +00001760 }
1761 break;
1762 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001763
1764 /* object codes */
1765
Guido van Rossumaa354651996-08-19 19:32:04 +00001766 case 'S': /* string object */
Guido van Rossum617dbc42007-05-07 23:57:08 +00001767 case 'Y': /* string object */
Georg Brandl6dd14612005-09-14 19:29:53 +00001768 case 'U': /* unicode string object */
Guido van Rossumaa354651996-08-19 19:32:04 +00001769 {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001770 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001771 break;
1772 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001773
Guido van Rossumaa354651996-08-19 19:32:04 +00001774 case 'O': /* object */
1775 {
Guido van Rossumaa354651996-08-19 19:32:04 +00001776 if (*format == '!') {
1777 format++;
Guido van Rossum79f25d91997-04-29 20:08:16 +00001778 (void) va_arg(*p_va, PyTypeObject*);
1779 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001780 }
1781#if 0
1782/* I don't know what this is for */
1783 else if (*format == '?') {
1784 inquiry pred = va_arg(*p_va, inquiry);
1785 format++;
1786 if ((*pred)(arg)) {
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 }
1790#endif
1791 else if (*format == '&') {
Tim Petersdbd9ba62000-07-09 03:09:57 +00001792 typedef int (*converter)(PyObject *, void *);
Guido van Rossum80bb9651996-12-05 23:27:02 +00001793 (void) va_arg(*p_va, converter);
1794 (void) va_arg(*p_va, void *);
Guido van Rossumaa354651996-08-19 19:32:04 +00001795 format++;
1796 }
1797 else {
Guido van Rossum79f25d91997-04-29 20:08:16 +00001798 (void) va_arg(*p_va, PyObject **);
Guido van Rossumaa354651996-08-19 19:32:04 +00001799 }
1800 break;
1801 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001802
Christian Heimes380f7f22008-02-28 11:19:05 +00001803 case '(': /* bypass tuple, not handled at all previously */
1804 {
1805 char *msg;
1806 for (;;) {
1807 if (*format==')')
1808 break;
1809 if (IS_END_OF_FORMAT(*format))
1810 return "Unmatched left paren in format "
1811 "string";
1812 msg = skipitem(&format, p_va, flags);
1813 if (msg)
1814 return msg;
1815 }
1816 format++;
1817 break;
1818 }
1819
1820 case ')':
1821 return "Unmatched right paren in format string";
1822
Guido van Rossumaa354651996-08-19 19:32:04 +00001823 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001824err:
Guido van Rossumaa354651996-08-19 19:32:04 +00001825 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00001826
Guido van Rossumaa354651996-08-19 19:32:04 +00001827 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001828
Guido van Rossumaa354651996-08-19 19:32:04 +00001829 *p_format = format;
1830 return NULL;
1831}
Fred Drakee4616e62001-10-23 21:09:29 +00001832
1833
1834int
Martin v. Löwis76246742006-03-01 04:06:10 +00001835PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001836{
Martin v. Löwis76246742006-03-01 04:06:10 +00001837 Py_ssize_t i, l;
Fred Drakee4616e62001-10-23 21:09:29 +00001838 PyObject **o;
1839 va_list vargs;
1840
1841#ifdef HAVE_STDARG_PROTOTYPES
1842 va_start(vargs, max);
1843#else
1844 va_start(vargs);
1845#endif
1846
1847 assert(min >= 0);
1848 assert(min <= max);
1849 if (!PyTuple_Check(args)) {
1850 PyErr_SetString(PyExc_SystemError,
1851 "PyArg_UnpackTuple() argument list is not a tuple");
1852 return 0;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001853 }
Fred Drakee4616e62001-10-23 21:09:29 +00001854 l = PyTuple_GET_SIZE(args);
1855 if (l < min) {
1856 if (name != NULL)
1857 PyErr_Format(
1858 PyExc_TypeError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001859 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001860 name, (min == max ? "" : "at least "), min, l);
1861 else
1862 PyErr_Format(
1863 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001864 "unpacked tuple should have %s%zd elements,"
Guido van Rossum98297ee2007-11-06 21:34:58 +00001865 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001866 (min == max ? "" : "at least "), min, l);
1867 va_end(vargs);
1868 return 0;
1869 }
1870 if (l > max) {
1871 if (name != NULL)
1872 PyErr_Format(
1873 PyExc_TypeError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001874 "%s expected %s%zd arguments, got %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001875 name, (min == max ? "" : "at most "), max, l);
1876 else
1877 PyErr_Format(
1878 PyExc_TypeError,
Thomas Wouters572a9f32006-03-01 05:38:39 +00001879 "unpacked tuple should have %s%zd elements,"
Guido van Rossum98297ee2007-11-06 21:34:58 +00001880 " but has %zd",
Fred Drakee4616e62001-10-23 21:09:29 +00001881 (min == max ? "" : "at most "), max, l);
1882 va_end(vargs);
1883 return 0;
1884 }
1885 for (i = 0; i < l; i++) {
1886 o = va_arg(vargs, PyObject **);
1887 *o = PyTuple_GET_ITEM(args, i);
1888 }
1889 va_end(vargs);
1890 return 1;
1891}
Georg Brandl02c42872005-08-26 06:42:30 +00001892
1893
1894/* For type constructors that don't take keyword args
1895 *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001896 * Sets a TypeError and returns 0 if the kwds dict is
Thomas Wouters89f507f2006-12-13 04:49:30 +00001897 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001898 */
1899int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001900_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001901{
1902 if (kw == NULL)
1903 return 1;
1904 if (!PyDict_CheckExact(kw)) {
1905 PyErr_BadInternalCall();
1906 return 0;
1907 }
1908 if (PyDict_Size(kw) == 0)
1909 return 1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001910
1911 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
Georg Brandl02c42872005-08-26 06:42:30 +00001912 funcname);
1913 return 0;
1914}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001915#ifdef __cplusplus
1916};
1917#endif