blob: 2c2628f9a1285d99b4bcb030cadbebb008160c28 [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 *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000017 const char *, char **, ...);
Brett Cannon711e7d92004-07-10 22:20:32 +000018int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040036typedef int (*destr_t)(PyObject *, void *);
37
38
39/* Keep track of "objects" that have been allocated or initialized and
40 which will need to be deallocated or cleaned up somehow if overall
41 parsing fails.
42*/
43typedef struct {
44 void *item;
45 destr_t destructor;
46} freelistentry_t;
47
48typedef struct {
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040049 freelistentry_t *entries;
Antoine Pitrou7056cb22013-02-17 01:04:57 +010050 int first_available;
51 int entries_malloced;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040052} freelist_t;
53
Antoine Pitrou7056cb22013-02-17 01:04:57 +010054#define STATIC_FREELIST_ENTRIES 8
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000055
56/* Forward */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000057static int vgetargs1(PyObject *, const char *, va_list *, int);
Victor Stinner84bb1cf2013-05-17 00:12:04 +020058static void seterror(Py_ssize_t, const char *, int *, const char *, const char *);
Guido van Rossum98297ee2007-11-06 21:34:58 +000059static char *convertitem(PyObject *, const char **, va_list *, int, int *,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040060 char *, size_t, freelist_t *);
Martin v. Löwis18e16552006-02-15 17:27:45 +000061static char *converttuple(PyObject *, const char **, va_list *, int,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040062 int *, char *, size_t, int, freelist_t *);
Martin v. Löwis18e16552006-02-15 17:27:45 +000063static char *convertsimple(PyObject *, const char **, va_list *, int, char *,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040064 size_t, freelist_t *);
Martin v. Löwis18e16552006-02-15 17:27:45 +000065static Py_ssize_t convertbuffer(PyObject *, void **p, char **);
Martin v. Löwis423be952008-08-13 15:53:07 +000066static int getbuffer(PyObject *, Py_buffer *, char**);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000067
Tim Petersdbd9ba62000-07-09 03:09:57 +000068static int vgetargskeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 const char *, char **, va_list *, int);
Martin v. Löwis18e16552006-02-15 17:27:45 +000070static char *skipitem(const char **, va_list *, int);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000071
Fred Drake563dfc22001-10-23 14:41:08 +000072int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000073PyArg_Parse(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 int retval;
76 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 va_start(va, format);
79 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
80 va_end(va);
81 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +000082}
83
84int
85_PyArg_Parse_SizeT(PyObject *args, char *format, ...)
86{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 int retval;
88 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 va_start(va, format);
91 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
92 va_end(va);
93 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000094}
95
96
Fred Drake563dfc22001-10-23 14:41:08 +000097int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000098PyArg_ParseTuple(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 int retval;
101 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 va_start(va, format);
104 retval = vgetargs1(args, format, &va, 0);
105 va_end(va);
106 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000107}
108
Martin v. Löwis18e16552006-02-15 17:27:45 +0000109int
110_PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 int retval;
113 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 va_start(va, format);
116 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
117 va_end(va);
118 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000119}
120
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000121
122int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000123PyArg_VaParse(PyObject *args, const char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 va_list lva;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000126
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000127 Py_VA_COPY(lva, va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000130}
131
Martin v. Löwis18e16552006-02-15 17:27:45 +0000132int
133_PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000136
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000137 Py_VA_COPY(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 return vgetargs1(args, format, &lva, FLAG_SIZE_T);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000140}
141
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000142
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000143/* Handle cleanup of allocated memory in case of exception */
144
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400145static int
146cleanup_ptr(PyObject *self, void *ptr)
Antoine Pitrouf71995782008-08-29 18:37:05 +0000147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 if (ptr) {
149 PyMem_FREE(ptr);
150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000152}
153
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000154static int
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400155cleanup_buffer(PyObject *self, void *ptr)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000156{
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400157 Py_buffer *buf = (Py_buffer *)ptr;
158 if (buf) {
159 PyBuffer_Release(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400161 return 0;
162}
163
164static int
165addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
166{
167 int index;
168
169 index = freelist->first_available;
170 freelist->first_available += 1;
171
172 freelist->entries[index].item = ptr;
173 freelist->entries[index].destructor = destructor;
174
175 return 0;
176}
177
178static int
179cleanreturn(int retval, freelist_t *freelist)
180{
181 int index;
182
183 if (retval == 0) {
184 /* A failure occurred, therefore execute all of the cleanup
185 functions.
186 */
187 for (index = 0; index < freelist->first_available; ++index) {
188 freelist->entries[index].destructor(NULL,
189 freelist->entries[index].item);
190 }
191 }
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100192 if (freelist->entries_malloced)
193 PyMem_FREE(freelist->entries);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 return retval;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000195}
196
197
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000198static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000199vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 char msgbuf[256];
202 int levels[32];
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100203 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
204 freelist_t freelist = {static_entries, 0, 0};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 const char *fname = NULL;
206 const char *message = NULL;
207 int min = -1;
208 int max = 0;
209 int level = 0;
210 int endfmt = 0;
211 const char *formatsave = format;
212 Py_ssize_t i, len;
213 char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 int compat = flags & FLAG_COMPAT;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 assert(compat || (args != (PyObject*)NULL));
217 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 while (endfmt == 0) {
220 int c = *format++;
221 switch (c) {
222 case '(':
223 if (level == 0)
224 max++;
225 level++;
226 if (level >= 30)
227 Py_FatalError("too many tuple nesting levels "
228 "in argument format string");
229 break;
230 case ')':
231 if (level == 0)
232 Py_FatalError("excess ')' in getargs format");
233 else
234 level--;
235 break;
236 case '\0':
237 endfmt = 1;
238 break;
239 case ':':
240 fname = format;
241 endfmt = 1;
242 break;
243 case ';':
244 message = format;
245 endfmt = 1;
246 break;
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100247 case '|':
248 if (level == 0)
249 min = max;
250 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 default:
252 if (level == 0) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100253 if (Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 if (c != 'e') /* skip encoded */
255 max++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 }
257 break;
258 }
259 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 if (level != 0)
262 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 if (min < 0)
265 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000268
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100269 if (max > STATIC_FREELIST_ENTRIES) {
270 freelist.entries = PyMem_NEW(freelistentry_t, max);
271 if (freelist.entries == NULL) {
272 PyErr_NoMemory();
273 return 0;
274 }
275 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -0500276 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 if (compat) {
279 if (max == 0) {
280 if (args == NULL)
281 return 1;
Victor Stinner6ced7c42011-03-21 18:15:42 +0100282 PyErr_Format(PyExc_TypeError,
283 "%.200s%s takes no arguments",
284 fname==NULL ? "function" : fname,
285 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400286 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 }
288 else if (min == 1 && max == 1) {
289 if (args == NULL) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100290 PyErr_Format(PyExc_TypeError,
291 "%.200s%s takes at least one argument",
292 fname==NULL ? "function" : fname,
293 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400294 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 }
296 msg = convertitem(args, &format, p_va, flags, levels,
297 msgbuf, sizeof(msgbuf), &freelist);
298 if (msg == NULL)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400299 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 seterror(levels[0], msg, levels+1, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400301 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 }
303 else {
304 PyErr_SetString(PyExc_SystemError,
305 "old style getargs format uses new features");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400306 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 }
308 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (!PyTuple_Check(args)) {
311 PyErr_SetString(PyExc_SystemError,
312 "new style getargs format but argument is not a tuple");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400313 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 len = PyTuple_GET_SIZE(args);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 if (len < min || max < len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100319 if (message == NULL)
320 PyErr_Format(PyExc_TypeError,
321 "%.150s%s takes %s %d argument%s (%ld given)",
322 fname==NULL ? "function" : fname,
323 fname==NULL ? "" : "()",
324 min==max ? "exactly"
325 : len < min ? "at least" : "at most",
326 len < min ? min : max,
327 (len < min ? min : max) == 1 ? "" : "s",
328 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
329 else
330 PyErr_SetString(PyExc_TypeError, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400331 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 for (i = 0; i < len; i++) {
335 if (*format == '|')
336 format++;
337 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
338 flags, levels, msgbuf,
339 sizeof(msgbuf), &freelist);
340 if (msg) {
341 seterror(i+1, msg, levels, fname, msg);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400342 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 }
344 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000345
Antoine Pitrou4de74572013-02-09 23:11:27 +0100346 if (*format != '\0' && !Py_ISALPHA(Py_CHARMASK(*format)) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 *format != '(' &&
348 *format != '|' && *format != ':' && *format != ';') {
349 PyErr_Format(PyExc_SystemError,
350 "bad format string: %.200s", formatsave);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400351 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000353
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400354 return cleanreturn(1, &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000355}
356
357
358
359static void
Victor Stinner84bb1cf2013-05-17 00:12:04 +0200360seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000361 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 char buf[512];
364 int i;
365 char *p = buf;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 if (PyErr_Occurred())
368 return;
369 else if (message == NULL) {
370 if (fname != NULL) {
371 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
372 p += strlen(p);
373 }
374 if (iarg != 0) {
375 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Richard Oudkerk25296ce2013-05-18 17:35:19 +0100376 "argument %" PY_FORMAT_SIZE_T "d", iarg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 i = 0;
378 p += strlen(p);
379 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
380 PyOS_snprintf(p, sizeof(buf) - (p - buf),
381 ", item %d", levels[i]-1);
382 p += strlen(p);
383 i++;
384 }
385 }
386 else {
387 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
388 p += strlen(p);
389 }
390 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
391 message = buf;
392 }
393 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000394}
395
396
397/* Convert a tuple argument.
398 On entry, *p_format points to the character _after_ the opening '('.
399 On successful exit, *p_format points to the closing ')'.
400 If successful:
401 *p_format and *p_va are updated,
402 *levels and *msgbuf are untouched,
403 and NULL is returned.
404 If the argument is invalid:
405 *p_format is unchanged,
406 *p_va is undefined,
407 *levels is a 0-terminated list of item numbers,
408 *msgbuf contains an error message, whose format is:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 "must be <typename1>, not <typename2>", where:
410 <typename1> is the name of the expected type, and
411 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000412 and msgbuf is returned.
413*/
414
415static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000416converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000417 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400418 freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 int level = 0;
421 int n = 0;
422 const char *format = *p_format;
423 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 for (;;) {
426 int c = *format++;
427 if (c == '(') {
428 if (level == 0)
429 n++;
430 level++;
431 }
432 else if (c == ')') {
433 if (level == 0)
434 break;
435 level--;
436 }
437 else if (c == ':' || c == ';' || c == '\0')
438 break;
Antoine Pitrou4de74572013-02-09 23:11:27 +0100439 else if (level == 0 && Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 n++;
441 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
444 levels[0] = 0;
445 PyOS_snprintf(msgbuf, bufsize,
446 toplevel ? "expected %d arguments, not %.50s" :
447 "must be %d-item sequence, not %.50s",
448 n,
449 arg == Py_None ? "None" : arg->ob_type->tp_name);
450 return msgbuf;
451 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if ((i = PySequence_Size(arg)) != n) {
454 levels[0] = 0;
455 PyOS_snprintf(msgbuf, bufsize,
456 toplevel ? "expected %d arguments, not %d" :
457 "must be sequence of length %d, not %d",
458 n, i);
459 return msgbuf;
460 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 format = *p_format;
463 for (i = 0; i < n; i++) {
464 char *msg;
465 PyObject *item;
466 item = PySequence_GetItem(arg, i);
467 if (item == NULL) {
468 PyErr_Clear();
469 levels[0] = i+1;
470 levels[1] = 0;
471 strncpy(msgbuf, "is not retrievable", bufsize);
472 return msgbuf;
473 }
474 msg = convertitem(item, &format, p_va, flags, levels+1,
475 msgbuf, bufsize, freelist);
476 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
477 Py_XDECREF(item);
478 if (msg != NULL) {
479 levels[0] = i+1;
480 return msg;
481 }
482 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 *p_format = format;
485 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000486}
487
488
489/* Convert a single item. */
490
491static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000492convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400493 int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 char *msg;
496 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (*format == '(' /* ')' */) {
499 format++;
500 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
501 bufsize, 0, freelist);
502 if (msg == NULL)
503 format++;
504 }
505 else {
506 msg = convertsimple(arg, &format, p_va, flags,
507 msgbuf, bufsize, freelist);
508 if (msg != NULL)
509 levels[0] = 0;
510 }
511 if (msg == NULL)
512 *p_format = format;
513 return msg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000514}
515
516
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000517
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000518/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000519
520static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000521converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 assert(expected != NULL);
524 assert(arg != NULL);
525 PyOS_snprintf(msgbuf, bufsize,
526 "must be %.50s, not %.50s", expected,
527 arg == Py_None ? "None" : arg->ob_type->tp_name);
528 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000529}
530
531#define CONV_UNICODE "(unicode conversion error)"
532
Guido van Rossum45aecf42006-03-15 04:58:47 +0000533/* Explicitly check for float arguments when integers are expected.
534 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000535static int
536float_argument_error(PyObject *arg)
537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (PyFloat_Check(arg)) {
539 PyErr_SetString(PyExc_TypeError,
540 "integer argument expected, got float" );
541 return 1;
542 }
543 else
544 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000545}
546
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000547/* Convert a non-tuple argument. Return NULL if conversion went OK,
548 or a string with a message describing the failure. The message is
549 formatted as "must be <desired type>, not <actual type>".
550 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000551 Don't call if a tuple is expected.
552
553 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000554*/
555
556static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000557convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400558 char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* For # codes */
561#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
562 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
563 else q=va_arg(*p_va, int*);
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000564#define STORE_SIZE(s) \
565 if (flags & FLAG_SIZE_T) \
566 *q2=s; \
567 else { \
568 if (INT_MAX < s) { \
569 PyErr_SetString(PyExc_OverflowError, \
570 "size does not fit in an int"); \
571 return converterr("", arg, msgbuf, bufsize); \
572 } \
Victor Stinner9550ef32013-06-05 01:18:13 +0200573 *q = (int)s; \
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000574 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000575#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
Victor Stinner6ab8e822011-01-04 11:16:49 +0000576#define RETURN_ERR_OCCURRED return msgbuf
Martin v. Löwis18e16552006-02-15 17:27:45 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 const char *format = *p_format;
579 char c = *format++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200580 char *sarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 case 'b': { /* unsigned byte -- very short int */
585 char *p = va_arg(*p_va, char *);
586 long ival;
587 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000588 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 ival = PyLong_AsLong(arg);
590 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000591 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 else if (ival < 0) {
593 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000594 "unsigned byte integer is less than minimum");
595 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 }
597 else if (ival > UCHAR_MAX) {
598 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000599 "unsigned byte integer is greater than maximum");
600 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 }
602 else
603 *p = (unsigned char) ival;
604 break;
605 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 case 'B': {/* byte sized bitfield - both signed and unsigned
608 values allowed */
609 char *p = va_arg(*p_va, char *);
610 long ival;
611 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000612 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 ival = PyLong_AsUnsignedLongMask(arg);
614 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000615 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 else
617 *p = (unsigned char) ival;
618 break;
619 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 case 'h': {/* signed short int */
622 short *p = va_arg(*p_va, short *);
623 long ival;
624 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000625 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 ival = PyLong_AsLong(arg);
627 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000628 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 else if (ival < SHRT_MIN) {
630 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000631 "signed short integer is less than minimum");
632 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 }
634 else if (ival > SHRT_MAX) {
635 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000636 "signed short integer is greater than maximum");
637 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 }
639 else
640 *p = (short) ival;
641 break;
642 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 case 'H': { /* short int sized bitfield, both signed and
645 unsigned allowed */
646 unsigned short *p = va_arg(*p_va, unsigned short *);
647 long ival;
648 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000649 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 ival = PyLong_AsUnsignedLongMask(arg);
651 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000652 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 else
654 *p = (unsigned short) ival;
655 break;
656 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 case 'i': {/* signed int */
659 int *p = va_arg(*p_va, int *);
660 long ival;
661 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000662 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 ival = PyLong_AsLong(arg);
664 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000665 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 else if (ival > INT_MAX) {
667 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000668 "signed integer is greater than maximum");
669 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 }
671 else if (ival < INT_MIN) {
672 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000673 "signed integer is less than minimum");
674 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 }
676 else
677 *p = ival;
678 break;
679 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 case 'I': { /* int sized bitfield, both signed and
682 unsigned allowed */
683 unsigned int *p = va_arg(*p_va, unsigned int *);
684 unsigned int ival;
685 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000686 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
688 if (ival == (unsigned int)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000689 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 else
691 *p = ival;
692 break;
693 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 case 'n': /* Py_ssize_t */
696 {
697 PyObject *iobj;
698 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
699 Py_ssize_t ival = -1;
700 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000701 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 iobj = PyNumber_Index(arg);
703 if (iobj != NULL) {
704 ival = PyLong_AsSsize_t(iobj);
705 Py_DECREF(iobj);
706 }
707 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000708 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 *p = ival;
710 break;
711 }
712 case 'l': {/* long int */
713 long *p = va_arg(*p_va, long *);
714 long ival;
715 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000716 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 ival = PyLong_AsLong(arg);
718 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000719 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 else
721 *p = ival;
722 break;
723 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 case 'k': { /* long sized bitfield */
726 unsigned long *p = va_arg(*p_va, unsigned long *);
727 unsigned long ival;
728 if (PyLong_Check(arg))
729 ival = PyLong_AsUnsignedLongMask(arg);
730 else
731 return converterr("integer<k>", arg, msgbuf, bufsize);
732 *p = ival;
733 break;
734 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000735
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000736#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 case 'L': {/* PY_LONG_LONG */
738 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
739 PY_LONG_LONG ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000740 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000741 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 ival = PyLong_AsLongLong(arg);
Mark Dickinsonc7301312010-06-10 16:05:10 +0000743 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000744 RETURN_ERR_OCCURRED;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000745 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 break;
748 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 case 'K': { /* long long sized bitfield */
751 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
752 unsigned PY_LONG_LONG ival;
753 if (PyLong_Check(arg))
754 ival = PyLong_AsUnsignedLongLongMask(arg);
755 else
756 return converterr("integer<K>", arg, msgbuf, bufsize);
757 *p = ival;
758 break;
759 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000760#endif
Guido van Rossum98297ee2007-11-06 21:34:58 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 case 'f': {/* float */
763 float *p = va_arg(*p_va, float *);
764 double dval = PyFloat_AsDouble(arg);
765 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000766 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 else
768 *p = (float) dval;
769 break;
770 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 case 'd': {/* double */
773 double *p = va_arg(*p_va, double *);
774 double dval = PyFloat_AsDouble(arg);
775 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000776 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 else
778 *p = dval;
779 break;
780 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 case 'D': {/* complex double */
783 Py_complex *p = va_arg(*p_va, Py_complex *);
784 Py_complex cval;
785 cval = PyComplex_AsCComplex(arg);
786 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000787 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 else
789 *p = cval;
790 break;
791 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 case 'c': {/* char */
794 char *p = va_arg(*p_va, char *);
795 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
796 *p = PyBytes_AS_STRING(arg)[0];
Eli Bendersky906b88f2011-07-29 07:05:08 +0300797 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
798 *p = PyByteArray_AS_STRING(arg)[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 else
800 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
801 break;
802 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 case 'C': {/* unicode char */
805 int *p = va_arg(*p_va, int *);
Victor Stinnere1335c72011-10-04 20:53:03 +0200806 int kind;
807 void *data;
808
809 if (!PyUnicode_Check(arg))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 return converterr("a unicode character", arg, msgbuf, bufsize);
Victor Stinnere1335c72011-10-04 20:53:03 +0200811
812 if (PyUnicode_READY(arg))
813 RETURN_ERR_OCCURRED;
814
815 if (PyUnicode_GET_LENGTH(arg) != 1)
816 return converterr("a unicode character", arg, msgbuf, bufsize);
817
818 kind = PyUnicode_KIND(arg);
819 data = PyUnicode_DATA(arg);
820 *p = PyUnicode_READ(kind, data, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 break;
822 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000823
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700824 case 'p': {/* boolean *p*redicate */
825 int *p = va_arg(*p_va, int *);
826 int val = PyObject_IsTrue(arg);
827 if (val > 0)
828 *p = 1;
829 else if (val == 0)
830 *p = 0;
831 else
832 RETURN_ERR_OCCURRED;
833 break;
834 }
835
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000836 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 case 'y': {/* any buffer-like object, but not PyUnicode */
840 void **p = (void **)va_arg(*p_va, char **);
841 char *buf;
842 Py_ssize_t count;
843 if (*format == '*') {
844 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
845 return converterr(buf, arg, msgbuf, bufsize);
846 format++;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400847 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 return converterr(
849 "(cleanup problem)",
850 arg, msgbuf, bufsize);
851 }
852 break;
853 }
854 count = convertbuffer(arg, p, &buf);
855 if (count < 0)
856 return converterr(buf, arg, msgbuf, bufsize);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000857 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 FETCH_SIZE;
859 STORE_SIZE(count);
860 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000861 } else {
862 if (strlen(*p) != count)
863 return converterr(
864 "bytes without null bytes",
865 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 }
867 break;
868 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000869
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000870 case 's': /* text string */
871 case 'z': /* text string or None */
872 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000874 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000876
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000877 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
879 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200880 Py_ssize_t len;
881 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
882 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 return converterr(CONV_UNICODE,
884 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200885 PyBuffer_FillInfo(p, arg, sarg, len, 1, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 }
887 else { /* any buffer-like object */
888 char *buf;
889 if (getbuffer(arg, p, &buf) < 0)
890 return converterr(buf, arg, msgbuf, bufsize);
891 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400892 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 return converterr(
894 "(cleanup problem)",
895 arg, msgbuf, bufsize);
896 }
897 format++;
898 } else if (*format == '#') { /* any buffer-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000899 /* "s#" or "z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 void **p = (void **)va_arg(*p_va, char **);
901 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000902
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000903 if (c == 'z' && arg == Py_None) {
904 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 STORE_SIZE(0);
906 }
907 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200908 Py_ssize_t len;
909 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
910 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 return converterr(CONV_UNICODE,
912 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200913 *p = sarg;
914 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 }
916 else { /* any buffer-like object */
917 /* XXX Really? */
918 char *buf;
919 Py_ssize_t count = convertbuffer(arg, p, &buf);
920 if (count < 0)
921 return converterr(buf, arg, msgbuf, bufsize);
922 STORE_SIZE(count);
923 }
924 format++;
925 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000926 /* "s" or "z" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 char **p = va_arg(*p_va, char **);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200928 Py_ssize_t len;
929 sarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000930
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000931 if (c == 'z' && arg == Py_None)
932 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200934 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
935 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 return converterr(CONV_UNICODE,
937 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200938 *p = sarg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 }
940 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000941 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200943 if (*p != NULL && sarg != NULL && (Py_ssize_t) strlen(*p) != len)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 return converterr(
Victor Stinner9d663d02011-10-13 23:25:03 +0200945 c == 'z' ? "str without null characters or None"
946 : "str without null characters",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 arg, msgbuf, bufsize);
948 }
949 break;
950 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000951
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000952 case 'u': /* raw unicode buffer (Py_UNICODE *) */
953 case 'Z': /* raw unicode buffer or None */
954 {
Brett Cannonb94767f2011-02-22 20:15:44 +0000955 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 if (*format == '#') { /* any buffer-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000958 /* "s#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +0000960
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000961 if (c == 'Z' && arg == Py_None) {
962 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 STORE_SIZE(0);
964 }
965 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200966 Py_ssize_t len;
967 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200968 if (*p == NULL)
969 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200970 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 }
Victor Stinner5216e6d2010-06-08 21:45:51 +0000972 else
973 return converterr("str or None", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 format++;
975 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000976 /* "s" or "Z" */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000977 if (c == 'Z' && arg == Py_None)
978 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000979 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200980 Py_ssize_t len;
981 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200982 if (*p == NULL)
983 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200984 if (Py_UNICODE_strlen(*p) != len)
Victor Stinner06e49dd2010-06-13 18:21:50 +0000985 return converterr(
Victor Stinner9d663d02011-10-13 23:25:03 +0200986 "str without null characters or None",
Victor Stinner06e49dd2010-06-13 18:21:50 +0000987 arg, msgbuf, bufsize);
988 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000989 return converterr(c == 'Z' ? "str or None" : "str",
990 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 }
992 break;
993 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 case 'e': {/* encoded string */
996 char **buffer;
997 const char *encoding;
998 PyObject *s;
999 int recode_strings;
1000 Py_ssize_t size;
1001 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 /* Get 'e' parameter: the encoding name */
1004 encoding = (const char *)va_arg(*p_va, const char *);
1005 if (encoding == NULL)
1006 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 /* Get output buffer parameter:
1009 's' (recode all objects via Unicode) or
1010 't' (only recode non-string objects)
1011 */
1012 if (*format == 's')
1013 recode_strings = 1;
1014 else if (*format == 't')
1015 recode_strings = 0;
1016 else
1017 return converterr(
1018 "(unknown parser marker combination)",
1019 arg, msgbuf, bufsize);
1020 buffer = (char **)va_arg(*p_va, char **);
1021 format++;
1022 if (buffer == NULL)
1023 return converterr("(buffer is NULL)",
1024 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 /* Encode object */
1027 if (!recode_strings &&
1028 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1029 s = arg;
1030 Py_INCREF(s);
1031 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1032 return converterr("(AsCharBuffer failed)",
1033 arg, msgbuf, bufsize);
1034 }
1035 else {
1036 PyObject *u;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 /* Convert object to Unicode */
1039 u = PyUnicode_FromObject(arg);
1040 if (u == NULL)
1041 return converterr(
1042 "string or unicode or text buffer",
1043 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 /* Encode object; use default error handling */
1046 s = PyUnicode_AsEncodedString(u,
1047 encoding,
1048 NULL);
1049 Py_DECREF(u);
1050 if (s == NULL)
1051 return converterr("(encoding failed)",
1052 arg, msgbuf, bufsize);
1053 if (!PyBytes_Check(s)) {
1054 Py_DECREF(s);
1055 return converterr(
1056 "(encoder failed to return bytes)",
1057 arg, msgbuf, bufsize);
1058 }
1059 size = PyBytes_GET_SIZE(s);
1060 ptr = PyBytes_AS_STRING(s);
1061 if (ptr == NULL)
1062 ptr = "";
1063 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 /* Write output; output is guaranteed to be 0-terminated */
1066 if (*format == '#') {
1067 /* Using buffer length parameter '#':
1068
1069 - if *buffer is NULL, a new buffer of the
1070 needed size is allocated and the data
1071 copied into it; *buffer is updated to point
1072 to the new buffer; the caller is
1073 responsible for PyMem_Free()ing it after
1074 usage
1075
1076 - if *buffer is not NULL, the data is
1077 copied to *buffer; *buffer_len has to be
1078 set to the size of the buffer on input;
1079 buffer overflow is signalled with an error;
1080 buffer has to provide enough room for the
1081 encoded string plus the trailing 0-byte
1082
1083 - in both cases, *buffer_len is updated to
1084 the size of the buffer /excluding/ the
1085 trailing 0-byte
1086
1087 */
1088 FETCH_SIZE;
1089
1090 format++;
1091 if (q == NULL && q2 == NULL) {
1092 Py_DECREF(s);
1093 return converterr(
1094 "(buffer_len is NULL)",
1095 arg, msgbuf, bufsize);
1096 }
1097 if (*buffer == NULL) {
1098 *buffer = PyMem_NEW(char, size + 1);
1099 if (*buffer == NULL) {
1100 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001101 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001102 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001104 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 Py_DECREF(s);
1106 return converterr(
1107 "(cleanup problem)",
1108 arg, msgbuf, bufsize);
1109 }
1110 } else {
1111 if (size + 1 > BUFFER_LEN) {
1112 Py_DECREF(s);
1113 return converterr(
1114 "(buffer overflow)",
1115 arg, msgbuf, bufsize);
1116 }
1117 }
1118 memcpy(*buffer, ptr, size+1);
1119 STORE_SIZE(size);
1120 } else {
1121 /* Using a 0-terminated buffer:
1122
1123 - the encoded string has to be 0-terminated
1124 for this variant to work; if it is not, an
1125 error raised
1126
1127 - a new buffer of the needed size is
1128 allocated and the data copied into it;
1129 *buffer is updated to point to the new
1130 buffer; the caller is responsible for
1131 PyMem_Free()ing it after usage
1132
1133 */
1134 if ((Py_ssize_t)strlen(ptr) != size) {
1135 Py_DECREF(s);
1136 return converterr(
1137 "encoded string without NULL bytes",
1138 arg, msgbuf, bufsize);
1139 }
1140 *buffer = PyMem_NEW(char, size + 1);
1141 if (*buffer == NULL) {
1142 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001143 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001144 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001146 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 Py_DECREF(s);
1148 return converterr("(cleanup problem)",
1149 arg, msgbuf, bufsize);
1150 }
1151 memcpy(*buffer, ptr, size+1);
1152 }
1153 Py_DECREF(s);
1154 break;
1155 }
1156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 case 'S': { /* PyBytes object */
1158 PyObject **p = va_arg(*p_va, PyObject **);
1159 if (PyBytes_Check(arg))
1160 *p = arg;
1161 else
1162 return converterr("bytes", arg, msgbuf, bufsize);
1163 break;
1164 }
1165
1166 case 'Y': { /* PyByteArray object */
1167 PyObject **p = va_arg(*p_va, PyObject **);
1168 if (PyByteArray_Check(arg))
1169 *p = arg;
1170 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001171 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 break;
1173 }
1174
1175 case 'U': { /* PyUnicode object */
1176 PyObject **p = va_arg(*p_va, PyObject **);
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001177 if (PyUnicode_Check(arg)) {
1178 if (PyUnicode_READY(arg) == -1)
1179 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 *p = arg;
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 else
1183 return converterr("str", arg, msgbuf, bufsize);
1184 break;
1185 }
1186
1187 case 'O': { /* object */
1188 PyTypeObject *type;
1189 PyObject **p;
1190 if (*format == '!') {
1191 type = va_arg(*p_va, PyTypeObject*);
1192 p = va_arg(*p_va, PyObject **);
1193 format++;
1194 if (PyType_IsSubtype(arg->ob_type, type))
1195 *p = arg;
1196 else
1197 return converterr(type->tp_name, arg, msgbuf, bufsize);
1198
1199 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 else if (*format == '&') {
1201 typedef int (*converter)(PyObject *, void *);
1202 converter convert = va_arg(*p_va, converter);
1203 void *addr = va_arg(*p_va, void *);
1204 int res;
1205 format++;
1206 if (! (res = (*convert)(arg, addr)))
1207 return converterr("(unspecified)",
1208 arg, msgbuf, bufsize);
1209 if (res == Py_CLEANUP_SUPPORTED &&
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001210 addcleanup(addr, freelist, convert) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 return converterr("(cleanup problem)",
1212 arg, msgbuf, bufsize);
1213 }
1214 else {
1215 p = va_arg(*p_va, PyObject **);
1216 *p = arg;
1217 }
1218 break;
1219 }
1220
1221
Victor Stinner25e8ec42010-06-25 00:02:38 +00001222 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224
Victor Stinner25e8ec42010-06-25 00:02:38 +00001225 if (*format != '*')
1226 return converterr(
1227 "invalid use of 'w' format character",
1228 arg, msgbuf, bufsize);
1229 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230
Victor Stinner25e8ec42010-06-25 00:02:38 +00001231 /* Caller is interested in Py_buffer, and the object
1232 supports it directly. */
1233 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1234 PyErr_Clear();
1235 return converterr("read-write buffer", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 }
Victor Stinner8182b712010-07-28 00:40:58 +00001237 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1238 PyBuffer_Release((Py_buffer*)p);
1239 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1240 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001241 if (addcleanup(p, freelist, cleanup_buffer)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001242 return converterr(
1243 "(cleanup problem)",
1244 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 break;
1247 }
1248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 default:
1250 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
1251
1252 }
1253
1254 *p_format = format;
1255 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001256
1257#undef FETCH_SIZE
1258#undef STORE_SIZE
1259#undef BUFFER_LEN
1260#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001261}
Guido van Rossumaa354651996-08-19 19:32:04 +00001262
Martin v. Löwis18e16552006-02-15 17:27:45 +00001263static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001264convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001265{
Victor Stinner5cb62392010-06-06 20:27:51 +00001266 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 Py_ssize_t count;
1268 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 *errmsg = NULL;
1271 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001272 if (pb != NULL && pb->bf_releasebuffer != NULL) {
1273 *errmsg = "read-only pinned buffer";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 return -1;
1275 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001276
Victor Stinner8182b712010-07-28 00:40:58 +00001277 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 count = view.len;
1280 *p = view.buf;
1281 PyBuffer_Release(&view);
1282 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001283}
Guido van Rossumaa354651996-08-19 19:32:04 +00001284
Martin v. Löwis423be952008-08-13 15:53:07 +00001285static int
Neal Norwitz2f99b242008-08-24 05:48:10 +00001286getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001287{
Victor Stinner8182b712010-07-28 00:40:58 +00001288 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 *errmsg = "bytes or buffer";
1290 return -1;
1291 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001292 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001293 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001294 *errmsg = "contiguous buffer";
1295 return -1;
1296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001298}
1299
Guido van Rossumaa354651996-08-19 19:32:04 +00001300/* Support for keyword arguments donated by
1301 Geoff Philbrick <philbric@delphi.hks.com> */
1302
Tim Petersf8cd3e82001-10-27 04:26:57 +00001303/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001304int
1305PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 PyObject *keywords,
1307 const char *format,
1308 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001309{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 int retval;
1311 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 if ((args == NULL || !PyTuple_Check(args)) ||
1314 (keywords != NULL && !PyDict_Check(keywords)) ||
1315 format == NULL ||
1316 kwlist == NULL)
1317 {
1318 PyErr_BadInternalCall();
1319 return 0;
1320 }
Tim Peters45772cd2001-10-27 03:58:40 +00001321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 va_start(va, kwlist);
1323 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1324 va_end(va);
1325 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001326}
1327
1328int
1329_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 PyObject *keywords,
1331 const char *format,
1332 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 int retval;
1335 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 if ((args == NULL || !PyTuple_Check(args)) ||
1338 (keywords != NULL && !PyDict_Check(keywords)) ||
1339 format == NULL ||
1340 kwlist == NULL)
1341 {
1342 PyErr_BadInternalCall();
1343 return 0;
1344 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 va_start(va, kwlist);
1347 retval = vgetargskeywords(args, keywords, format,
1348 kwlist, &va, FLAG_SIZE_T);
1349 va_end(va);
1350 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001351}
1352
1353
Brett Cannon711e7d92004-07-10 22:20:32 +00001354int
1355PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001356 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001357 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001358 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 int retval;
1361 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 if ((args == NULL || !PyTuple_Check(args)) ||
1364 (keywords != NULL && !PyDict_Check(keywords)) ||
1365 format == NULL ||
1366 kwlist == NULL)
1367 {
1368 PyErr_BadInternalCall();
1369 return 0;
1370 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001371
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001372 Py_VA_COPY(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1375 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001376}
1377
1378int
1379_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 PyObject *keywords,
1381 const char *format,
1382 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 int retval;
1385 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 if ((args == NULL || !PyTuple_Check(args)) ||
1388 (keywords != NULL && !PyDict_Check(keywords)) ||
1389 format == NULL ||
1390 kwlist == NULL)
1391 {
1392 PyErr_BadInternalCall();
1393 return 0;
1394 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001395
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001396 Py_VA_COPY(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 retval = vgetargskeywords(args, keywords, format,
1399 kwlist, &lva, FLAG_SIZE_T);
1400 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001401}
1402
Benjamin Petersonfb886362010-04-24 18:21:17 +00001403int
1404PyArg_ValidateKeywordArguments(PyObject *kwargs)
1405{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001406 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 PyErr_BadInternalCall();
1408 return 0;
1409 }
1410 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1411 PyErr_SetString(PyExc_TypeError,
1412 "keyword arguments must be strings");
1413 return 0;
1414 }
1415 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001416}
1417
Christian Heimes380f7f22008-02-28 11:19:05 +00001418#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001419
Guido van Rossumaa354651996-08-19 19:32:04 +00001420static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001421vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 char msgbuf[512];
1425 int levels[32];
1426 const char *fname, *msg, *custom_msg, *keyword;
1427 int min = INT_MAX;
Larry Hastings83a9f482012-03-20 20:06:16 +00001428 int max = INT_MAX;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 int i, len, nargs, nkeywords;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001430 PyObject *current_arg;
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001431 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
1432 freelist_t freelist = {static_entries, 0, 0};
Tim Petersf4331c12001-10-27 00:17:34 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 assert(args != NULL && PyTuple_Check(args));
1435 assert(keywords == NULL || PyDict_Check(keywords));
1436 assert(format != NULL);
1437 assert(kwlist != NULL);
1438 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 /* grab the function name or custom error msg first (mutually exclusive) */
1441 fname = strchr(format, ':');
1442 if (fname) {
1443 fname++;
1444 custom_msg = NULL;
1445 }
1446 else {
1447 custom_msg = strchr(format,';');
1448 if (custom_msg)
1449 custom_msg++;
1450 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 /* scan kwlist and get greatest possible nbr of args */
1453 for (len=0; kwlist[len]; len++)
1454 continue;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001455
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001456 if (len > STATIC_FREELIST_ENTRIES) {
1457 freelist.entries = PyMem_NEW(freelistentry_t, len);
1458 if (freelist.entries == NULL) {
1459 PyErr_NoMemory();
1460 return 0;
1461 }
1462 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -05001463 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 nargs = PyTuple_GET_SIZE(args);
1466 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1467 if (nargs + nkeywords > len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +01001468 PyErr_Format(PyExc_TypeError,
1469 "%s%s takes at most %d argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 (fname == NULL) ? "function" : fname,
1471 (fname == NULL) ? "" : "()",
1472 len,
1473 (len == 1) ? "" : "s",
1474 nargs + nkeywords);
Benjamin Peterson01feaec2012-03-16 13:25:58 -05001475 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 }
Tim Petersc2f01122001-10-27 07:25:06 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1479 for (i = 0; i < len; i++) {
1480 keyword = kwlist[i];
1481 if (*format == '|') {
Larry Hastings83a9f482012-03-20 20:06:16 +00001482 if (min != INT_MAX) {
1483 PyErr_SetString(PyExc_RuntimeError,
1484 "Invalid format string (| specified twice)");
1485 return cleanreturn(0, &freelist);
1486 }
1487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 min = i;
1489 format++;
Larry Hastings83a9f482012-03-20 20:06:16 +00001490
1491 if (max != INT_MAX) {
1492 PyErr_SetString(PyExc_RuntimeError,
1493 "Invalid format string ($ before |)");
1494 return cleanreturn(0, &freelist);
1495 }
1496 }
1497 if (*format == '$') {
1498 if (max != INT_MAX) {
1499 PyErr_SetString(PyExc_RuntimeError,
1500 "Invalid format string ($ specified twice)");
1501 return cleanreturn(0, &freelist);
1502 }
1503
1504 max = i;
1505 format++;
1506
1507 if (max < nargs) {
1508 PyErr_Format(PyExc_TypeError,
1509 "Function takes %s %d positional arguments"
1510 " (%d given)",
1511 (min != INT_MAX) ? "at most" : "exactly",
1512 max, nargs);
1513 return cleanreturn(0, &freelist);
1514 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 }
1516 if (IS_END_OF_FORMAT(*format)) {
1517 PyErr_Format(PyExc_RuntimeError,
1518 "More keyword list entries (%d) than "
1519 "format specifiers (%d)", len, i);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001520 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 }
1522 current_arg = NULL;
1523 if (nkeywords) {
1524 current_arg = PyDict_GetItemString(keywords, keyword);
1525 }
1526 if (current_arg) {
1527 --nkeywords;
1528 if (i < nargs) {
1529 /* arg present in tuple and in dict */
1530 PyErr_Format(PyExc_TypeError,
1531 "Argument given by name ('%s') "
1532 "and position (%d)",
1533 keyword, i+1);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001534 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 }
1536 }
1537 else if (nkeywords && PyErr_Occurred())
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001538 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 else if (i < nargs)
1540 current_arg = PyTuple_GET_ITEM(args, i);
Christian Heimes380f7f22008-02-28 11:19:05 +00001541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 if (current_arg) {
1543 msg = convertitem(current_arg, &format, p_va, flags,
1544 levels, msgbuf, sizeof(msgbuf), &freelist);
1545 if (msg) {
1546 seterror(i+1, msg, levels, fname, custom_msg);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001547 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 }
1549 continue;
1550 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 if (i < min) {
1553 PyErr_Format(PyExc_TypeError, "Required argument "
1554 "'%s' (pos %d) not found",
1555 keyword, i+1);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001556 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 }
1558 /* current code reports success when all required args
1559 * fulfilled and no keyword args left, with no further
1560 * validation. XXX Maybe skip this in debug build ?
1561 */
1562 if (!nkeywords)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001563 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 /* We are into optional args, skip thru to any remaining
1566 * keyword args */
1567 msg = skipitem(&format, p_va, flags);
1568 if (msg) {
1569 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1570 format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001571 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 }
1573 }
Tim Petersb054be42001-10-27 05:07:41 +00001574
Larry Hastings83a9f482012-03-20 20:06:16 +00001575 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 PyErr_Format(PyExc_RuntimeError,
1577 "more argument specifiers than keyword list entries "
1578 "(remaining format:'%s')", format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001579 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 }
Tim Petersc2f01122001-10-27 07:25:06 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 /* make sure there are no extraneous keyword arguments */
1583 if (nkeywords > 0) {
1584 PyObject *key, *value;
1585 Py_ssize_t pos = 0;
1586 while (PyDict_Next(keywords, &pos, &key, &value)) {
1587 int match = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 if (!PyUnicode_Check(key)) {
1589 PyErr_SetString(PyExc_TypeError,
1590 "keywords must be strings");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001591 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 }
Victor Stinner93b55132010-05-19 00:54:06 +00001593 /* check that _PyUnicode_AsString() result is not NULL */
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001594 for (i = 0; i < len; i++) {
1595 if (!PyUnicode_CompareWithASCIIString(key, kwlist[i])) {
1596 match = 1;
1597 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 }
1599 }
1600 if (!match) {
1601 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001602 "'%U' is an invalid keyword "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 "argument for this function",
Victor Stinner93b55132010-05-19 00:54:06 +00001604 key);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001605 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 }
1607 }
1608 }
1609
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001610 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001611}
1612
1613
1614static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001615skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 const char *format = *p_format;
1618 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001621
Larry Hastingsa3479012012-05-08 23:52:03 -07001622 /*
1623 * codes that take a single data pointer as an argument
1624 * (the type of the pointer is irrelevant)
1625 */
Georg Brandl6dd14612005-09-14 19:29:53 +00001626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 case 'b': /* byte -- very short int */
1628 case 'B': /* byte as bitfield */
1629 case 'h': /* short int */
1630 case 'H': /* short int as bitfield */
1631 case 'i': /* int */
1632 case 'I': /* int sized bitfield */
1633 case 'l': /* long int */
1634 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001635#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 case 'L': /* PY_LONG_LONG */
1637 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001638#endif
Larry Hastingsa3479012012-05-08 23:52:03 -07001639 case 'n': /* Py_ssize_t */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 case 'f': /* float */
1641 case 'd': /* double */
1642 case 'D': /* complex double */
1643 case 'c': /* char */
1644 case 'C': /* unicode char */
Larry Hastings10ba07a2012-05-07 02:44:50 -07001645 case 'p': /* boolean predicate */
Larry Hastingsa3479012012-05-08 23:52:03 -07001646 case 'S': /* string object */
1647 case 'Y': /* string object */
1648 case 'U': /* unicode string object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 {
1650 (void) va_arg(*p_va, void *);
1651 break;
1652 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 case 'e': /* string with encoding */
1657 {
1658 (void) va_arg(*p_va, const char *);
1659 if (!(*format == 's' || *format == 't'))
1660 /* after 'e', only 's' and 't' is allowed */
1661 goto err;
1662 format++;
1663 /* explicit fallthrough to string cases */
1664 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 case 's': /* string */
1667 case 'z': /* string or None */
1668 case 'y': /* bytes */
1669 case 'u': /* unicode string */
Larry Hastingsd9e4a412012-05-08 03:51:18 -07001670 case 'Z': /* unicode string or None */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 case 'w': /* buffer, read-write */
1672 {
1673 (void) va_arg(*p_va, char **);
1674 if (*format == '#') {
1675 if (flags & FLAG_SIZE_T)
1676 (void) va_arg(*p_va, Py_ssize_t *);
1677 else
1678 (void) va_arg(*p_va, int *);
1679 format++;
1680 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
1681 format++;
1682 }
1683 break;
1684 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 case 'O': /* object */
1687 {
1688 if (*format == '!') {
1689 format++;
1690 (void) va_arg(*p_va, PyTypeObject*);
1691 (void) va_arg(*p_va, PyObject **);
1692 }
1693 else if (*format == '&') {
1694 typedef int (*converter)(PyObject *, void *);
1695 (void) va_arg(*p_va, converter);
1696 (void) va_arg(*p_va, void *);
1697 format++;
1698 }
1699 else {
1700 (void) va_arg(*p_va, PyObject **);
1701 }
1702 break;
1703 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 case '(': /* bypass tuple, not handled at all previously */
1706 {
1707 char *msg;
1708 for (;;) {
1709 if (*format==')')
1710 break;
1711 if (IS_END_OF_FORMAT(*format))
1712 return "Unmatched left paren in format "
1713 "string";
1714 msg = skipitem(&format, p_va, flags);
1715 if (msg)
1716 return msg;
1717 }
1718 format++;
1719 break;
1720 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 case ')':
1723 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00001724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001726err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00001728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 *p_format = format;
1732 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00001733}
Fred Drakee4616e62001-10-23 21:09:29 +00001734
1735
1736int
Martin v. Löwis76246742006-03-01 04:06:10 +00001737PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 Py_ssize_t i, l;
1740 PyObject **o;
1741 va_list vargs;
Fred Drakee4616e62001-10-23 21:09:29 +00001742
1743#ifdef HAVE_STDARG_PROTOTYPES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 va_start(vargs, max);
Fred Drakee4616e62001-10-23 21:09:29 +00001745#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 va_start(vargs);
Fred Drakee4616e62001-10-23 21:09:29 +00001747#endif
1748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 assert(min >= 0);
1750 assert(min <= max);
1751 if (!PyTuple_Check(args)) {
Christian Heimes48d8d212012-09-10 13:16:45 +02001752 va_end(vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 PyErr_SetString(PyExc_SystemError,
1754 "PyArg_UnpackTuple() argument list is not a tuple");
1755 return 0;
1756 }
1757 l = PyTuple_GET_SIZE(args);
1758 if (l < min) {
1759 if (name != NULL)
1760 PyErr_Format(
1761 PyExc_TypeError,
1762 "%s expected %s%zd arguments, got %zd",
1763 name, (min == max ? "" : "at least "), min, l);
1764 else
1765 PyErr_Format(
1766 PyExc_TypeError,
1767 "unpacked tuple should have %s%zd elements,"
1768 " but has %zd",
1769 (min == max ? "" : "at least "), min, l);
1770 va_end(vargs);
1771 return 0;
1772 }
1773 if (l > max) {
1774 if (name != NULL)
1775 PyErr_Format(
1776 PyExc_TypeError,
1777 "%s expected %s%zd arguments, got %zd",
1778 name, (min == max ? "" : "at most "), max, l);
1779 else
1780 PyErr_Format(
1781 PyExc_TypeError,
1782 "unpacked tuple should have %s%zd elements,"
1783 " but has %zd",
1784 (min == max ? "" : "at most "), max, l);
1785 va_end(vargs);
1786 return 0;
1787 }
1788 for (i = 0; i < l; i++) {
1789 o = va_arg(vargs, PyObject **);
1790 *o = PyTuple_GET_ITEM(args, i);
1791 }
1792 va_end(vargs);
1793 return 1;
Fred Drakee4616e62001-10-23 21:09:29 +00001794}
Georg Brandl02c42872005-08-26 06:42:30 +00001795
1796
1797/* For type constructors that don't take keyword args
1798 *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001799 * Sets a TypeError and returns 0 if the kwds dict is
Thomas Wouters89f507f2006-12-13 04:49:30 +00001800 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001801 */
1802int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001803_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001804{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 if (kw == NULL)
1806 return 1;
1807 if (!PyDict_CheckExact(kw)) {
1808 PyErr_BadInternalCall();
1809 return 0;
1810 }
1811 if (PyDict_Size(kw) == 0)
1812 return 1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1815 funcname);
1816 return 0;
Georg Brandl02c42872005-08-26 06:42:30 +00001817}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001818#ifdef __cplusplus
1819};
1820#endif