blob: 17c4ee5955be598e491727d22902d8967adf6281 [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 {
49 int first_available;
50 freelistentry_t *entries;
51} freelist_t;
52
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000053
54/* Forward */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000055static int vgetargs1(PyObject *, const char *, va_list *, int);
56static void seterror(int, const char *, int *, const char *, const char *);
Guido van Rossum98297ee2007-11-06 21:34:58 +000057static char *convertitem(PyObject *, const char **, va_list *, int, int *,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040058 char *, size_t, freelist_t *);
Martin v. Löwis18e16552006-02-15 17:27:45 +000059static char *converttuple(PyObject *, const char **, va_list *, int,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040060 int *, char *, size_t, int, freelist_t *);
Martin v. Löwis18e16552006-02-15 17:27:45 +000061static char *convertsimple(PyObject *, const char **, va_list *, int, char *,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040062 size_t, freelist_t *);
Martin v. Löwis18e16552006-02-15 17:27:45 +000063static Py_ssize_t convertbuffer(PyObject *, void **p, char **);
Martin v. Löwis423be952008-08-13 15:53:07 +000064static int getbuffer(PyObject *, Py_buffer *, char**);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000065
Tim Petersdbd9ba62000-07-09 03:09:57 +000066static int vgetargskeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 const char *, char **, va_list *, int);
Martin v. Löwis18e16552006-02-15 17:27:45 +000068static char *skipitem(const char **, va_list *, int);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000069
Fred Drake563dfc22001-10-23 14:41:08 +000070int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000071PyArg_Parse(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 int retval;
74 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 va_start(va, format);
77 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
78 va_end(va);
79 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +000080}
81
82int
83_PyArg_Parse_SizeT(PyObject *args, char *format, ...)
84{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 int retval;
86 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 va_start(va, format);
89 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
90 va_end(va);
91 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000092}
93
94
Fred Drake563dfc22001-10-23 14:41:08 +000095int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000096PyArg_ParseTuple(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 int retval;
99 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 va_start(va, format);
102 retval = vgetargs1(args, format, &va, 0);
103 va_end(va);
104 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000105}
106
Martin v. Löwis18e16552006-02-15 17:27:45 +0000107int
108_PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...)
109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 int retval;
111 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 va_start(va, format);
114 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
115 va_end(va);
116 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000117}
118
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000119
120int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000121PyArg_VaParse(PyObject *args, const char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 va_list lva;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000124
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000125 Py_VA_COPY(lva, va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000128}
129
Martin v. Löwis18e16552006-02-15 17:27:45 +0000130int
131_PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va)
132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000134
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000135 Py_VA_COPY(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 return vgetargs1(args, format, &lva, FLAG_SIZE_T);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000138}
139
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000140
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000141/* Handle cleanup of allocated memory in case of exception */
142
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400143static int
144cleanup_ptr(PyObject *self, void *ptr)
Antoine Pitrouf71995782008-08-29 18:37:05 +0000145{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 if (ptr) {
147 PyMem_FREE(ptr);
148 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000150}
151
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000152static int
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400153cleanup_buffer(PyObject *self, void *ptr)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000154{
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400155 Py_buffer *buf = (Py_buffer *)ptr;
156 if (buf) {
157 PyBuffer_Release(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400159 return 0;
160}
161
162static int
163addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
164{
165 int index;
166
167 index = freelist->first_available;
168 freelist->first_available += 1;
169
170 freelist->entries[index].item = ptr;
171 freelist->entries[index].destructor = destructor;
172
173 return 0;
174}
175
176static int
177cleanreturn(int retval, freelist_t *freelist)
178{
179 int index;
180
181 if (retval == 0) {
182 /* A failure occurred, therefore execute all of the cleanup
183 functions.
184 */
185 for (index = 0; index < freelist->first_available; ++index) {
186 freelist->entries[index].destructor(NULL,
187 freelist->entries[index].item);
188 }
189 }
Benjamin Petersonf7c13212012-03-16 12:23:39 -0500190 PyMem_FREE(freelist->entries);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 return retval;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000192}
193
194
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000195static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000196vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 char msgbuf[256];
199 int levels[32];
200 const char *fname = NULL;
201 const char *message = NULL;
202 int min = -1;
203 int max = 0;
204 int level = 0;
205 int endfmt = 0;
206 const char *formatsave = format;
207 Py_ssize_t i, len;
208 char *msg;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400209 freelist_t freelist = {0, NULL};
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 int compat = flags & FLAG_COMPAT;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 assert(compat || (args != (PyObject*)NULL));
213 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 while (endfmt == 0) {
216 int c = *format++;
217 switch (c) {
218 case '(':
219 if (level == 0)
220 max++;
221 level++;
222 if (level >= 30)
223 Py_FatalError("too many tuple nesting levels "
224 "in argument format string");
225 break;
226 case ')':
227 if (level == 0)
228 Py_FatalError("excess ')' in getargs format");
229 else
230 level--;
231 break;
232 case '\0':
233 endfmt = 1;
234 break;
235 case ':':
236 fname = format;
237 endfmt = 1;
238 break;
239 case ';':
240 message = format;
241 endfmt = 1;
242 break;
243 default:
244 if (level == 0) {
245 if (c == 'O')
246 max++;
247 else if (isalpha(Py_CHARMASK(c))) {
248 if (c != 'e') /* skip encoded */
249 max++;
250 } else if (c == '|')
251 min = max;
252 }
253 break;
254 }
255 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 if (level != 0)
258 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if (min < 0)
261 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000264
Benjamin Petersonf7c13212012-03-16 12:23:39 -0500265 freelist.entries = PyMem_NEW(freelistentry_t, max);
Benjamin Peterson7ed67272012-03-16 12:21:02 -0500266 if (freelist.entries == NULL) {
267 PyErr_NoMemory();
268 return 0;
269 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 if (compat) {
272 if (max == 0) {
273 if (args == NULL)
274 return 1;
Victor Stinner6ced7c42011-03-21 18:15:42 +0100275 PyErr_Format(PyExc_TypeError,
276 "%.200s%s takes no arguments",
277 fname==NULL ? "function" : fname,
278 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400279 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 }
281 else if (min == 1 && max == 1) {
282 if (args == NULL) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100283 PyErr_Format(PyExc_TypeError,
284 "%.200s%s takes at least one argument",
285 fname==NULL ? "function" : fname,
286 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400287 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 }
289 msg = convertitem(args, &format, p_va, flags, levels,
290 msgbuf, sizeof(msgbuf), &freelist);
291 if (msg == NULL)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400292 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 seterror(levels[0], msg, levels+1, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400294 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 }
296 else {
297 PyErr_SetString(PyExc_SystemError,
298 "old style getargs format uses new features");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400299 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 }
301 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 if (!PyTuple_Check(args)) {
304 PyErr_SetString(PyExc_SystemError,
305 "new style getargs format but argument is not a tuple");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400306 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 len = PyTuple_GET_SIZE(args);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (len < min || max < len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100312 if (message == NULL)
313 PyErr_Format(PyExc_TypeError,
314 "%.150s%s takes %s %d argument%s (%ld given)",
315 fname==NULL ? "function" : fname,
316 fname==NULL ? "" : "()",
317 min==max ? "exactly"
318 : len < min ? "at least" : "at most",
319 len < min ? min : max,
320 (len < min ? min : max) == 1 ? "" : "s",
321 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
322 else
323 PyErr_SetString(PyExc_TypeError, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400324 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 for (i = 0; i < len; i++) {
328 if (*format == '|')
329 format++;
330 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
331 flags, levels, msgbuf,
332 sizeof(msgbuf), &freelist);
333 if (msg) {
334 seterror(i+1, msg, levels, fname, msg);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400335 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 }
337 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
340 *format != '(' &&
341 *format != '|' && *format != ':' && *format != ';') {
342 PyErr_Format(PyExc_SystemError,
343 "bad format string: %.200s", formatsave);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400344 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000346
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400347 return cleanreturn(1, &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000348}
349
350
351
352static void
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000353seterror(int iarg, const char *msg, int *levels, const char *fname,
354 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 char buf[512];
357 int i;
358 char *p = buf;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (PyErr_Occurred())
361 return;
362 else if (message == NULL) {
363 if (fname != NULL) {
364 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
365 p += strlen(p);
366 }
367 if (iarg != 0) {
368 PyOS_snprintf(p, sizeof(buf) - (p - buf),
369 "argument %d", iarg);
370 i = 0;
371 p += strlen(p);
372 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
373 PyOS_snprintf(p, sizeof(buf) - (p - buf),
374 ", item %d", levels[i]-1);
375 p += strlen(p);
376 i++;
377 }
378 }
379 else {
380 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
381 p += strlen(p);
382 }
383 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
384 message = buf;
385 }
386 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000387}
388
389
390/* Convert a tuple argument.
391 On entry, *p_format points to the character _after_ the opening '('.
392 On successful exit, *p_format points to the closing ')'.
393 If successful:
394 *p_format and *p_va are updated,
395 *levels and *msgbuf are untouched,
396 and NULL is returned.
397 If the argument is invalid:
398 *p_format is unchanged,
399 *p_va is undefined,
400 *levels is a 0-terminated list of item numbers,
401 *msgbuf contains an error message, whose format is:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 "must be <typename1>, not <typename2>", where:
403 <typename1> is the name of the expected type, and
404 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000405 and msgbuf is returned.
406*/
407
408static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000409converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000410 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400411 freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 int level = 0;
414 int n = 0;
415 const char *format = *p_format;
416 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 for (;;) {
419 int c = *format++;
420 if (c == '(') {
421 if (level == 0)
422 n++;
423 level++;
424 }
425 else if (c == ')') {
426 if (level == 0)
427 break;
428 level--;
429 }
430 else if (c == ':' || c == ';' || c == '\0')
431 break;
432 else if (level == 0 && isalpha(Py_CHARMASK(c)))
433 n++;
434 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
437 levels[0] = 0;
438 PyOS_snprintf(msgbuf, bufsize,
439 toplevel ? "expected %d arguments, not %.50s" :
440 "must be %d-item sequence, not %.50s",
441 n,
442 arg == Py_None ? "None" : arg->ob_type->tp_name);
443 return msgbuf;
444 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if ((i = PySequence_Size(arg)) != n) {
447 levels[0] = 0;
448 PyOS_snprintf(msgbuf, bufsize,
449 toplevel ? "expected %d arguments, not %d" :
450 "must be sequence of length %d, not %d",
451 n, i);
452 return msgbuf;
453 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 format = *p_format;
456 for (i = 0; i < n; i++) {
457 char *msg;
458 PyObject *item;
459 item = PySequence_GetItem(arg, i);
460 if (item == NULL) {
461 PyErr_Clear();
462 levels[0] = i+1;
463 levels[1] = 0;
464 strncpy(msgbuf, "is not retrievable", bufsize);
465 return msgbuf;
466 }
467 msg = convertitem(item, &format, p_va, flags, levels+1,
468 msgbuf, bufsize, freelist);
469 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
470 Py_XDECREF(item);
471 if (msg != NULL) {
472 levels[0] = i+1;
473 return msg;
474 }
475 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 *p_format = format;
478 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000479}
480
481
482/* Convert a single item. */
483
484static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000485convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400486 int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 char *msg;
489 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 if (*format == '(' /* ')' */) {
492 format++;
493 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
494 bufsize, 0, freelist);
495 if (msg == NULL)
496 format++;
497 }
498 else {
499 msg = convertsimple(arg, &format, p_va, flags,
500 msgbuf, bufsize, freelist);
501 if (msg != NULL)
502 levels[0] = 0;
503 }
504 if (msg == NULL)
505 *p_format = format;
506 return msg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000507}
508
509
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000510
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000511/* 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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 assert(expected != NULL);
517 assert(arg != NULL);
518 PyOS_snprintf(msgbuf, bufsize,
519 "must be %.50s, not %.50s", expected,
520 arg == Py_None ? "None" : arg->ob_type->tp_name);
521 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000522}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (PyFloat_Check(arg)) {
532 PyErr_SetString(PyExc_TypeError,
533 "integer argument expected, got float" );
534 return 1;
535 }
536 else
537 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000538}
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,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400551 char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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*);
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000557#define STORE_SIZE(s) \
558 if (flags & FLAG_SIZE_T) \
559 *q2=s; \
560 else { \
561 if (INT_MAX < s) { \
562 PyErr_SetString(PyExc_OverflowError, \
563 "size does not fit in an int"); \
564 return converterr("", arg, msgbuf, bufsize); \
565 } \
566 *q=s; \
567 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000568#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
Victor Stinner6ab8e822011-01-04 11:16:49 +0000569#define RETURN_ERR_OCCURRED return msgbuf
Martin v. Löwis18e16552006-02-15 17:27:45 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 const char *format = *p_format;
572 char c = *format++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200573 char *sarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 case 'b': { /* unsigned byte -- very short int */
578 char *p = va_arg(*p_va, char *);
579 long ival;
580 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000581 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 ival = PyLong_AsLong(arg);
583 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000584 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 else if (ival < 0) {
586 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000587 "unsigned byte integer is less than minimum");
588 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 }
590 else if (ival > UCHAR_MAX) {
591 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000592 "unsigned byte integer is greater than maximum");
593 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 }
595 else
596 *p = (unsigned char) ival;
597 break;
598 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 case 'B': {/* byte sized bitfield - both signed and unsigned
601 values allowed */
602 char *p = va_arg(*p_va, char *);
603 long ival;
604 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000605 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 ival = PyLong_AsUnsignedLongMask(arg);
607 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000608 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 else
610 *p = (unsigned char) ival;
611 break;
612 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 case 'h': {/* signed short int */
615 short *p = va_arg(*p_va, short *);
616 long ival;
617 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000618 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 ival = PyLong_AsLong(arg);
620 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000621 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 else if (ival < SHRT_MIN) {
623 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000624 "signed short integer is less than minimum");
625 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 }
627 else if (ival > SHRT_MAX) {
628 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000629 "signed short integer is greater than maximum");
630 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 }
632 else
633 *p = (short) ival;
634 break;
635 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 case 'H': { /* short int sized bitfield, both signed and
638 unsigned allowed */
639 unsigned short *p = va_arg(*p_va, unsigned short *);
640 long ival;
641 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000642 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 ival = PyLong_AsUnsignedLongMask(arg);
644 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000645 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 else
647 *p = (unsigned short) ival;
648 break;
649 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 case 'i': {/* signed int */
652 int *p = va_arg(*p_va, int *);
653 long ival;
654 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000655 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 ival = PyLong_AsLong(arg);
657 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000658 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 else if (ival > INT_MAX) {
660 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000661 "signed integer is greater than maximum");
662 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 }
664 else if (ival < INT_MIN) {
665 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000666 "signed integer is less than minimum");
667 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 }
669 else
670 *p = ival;
671 break;
672 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 case 'I': { /* int sized bitfield, both signed and
675 unsigned allowed */
676 unsigned int *p = va_arg(*p_va, unsigned int *);
677 unsigned int ival;
678 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000679 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
681 if (ival == (unsigned int)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000682 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 else
684 *p = ival;
685 break;
686 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 case 'n': /* Py_ssize_t */
689 {
690 PyObject *iobj;
691 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
692 Py_ssize_t ival = -1;
693 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000694 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 iobj = PyNumber_Index(arg);
696 if (iobj != NULL) {
697 ival = PyLong_AsSsize_t(iobj);
698 Py_DECREF(iobj);
699 }
700 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000701 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 *p = ival;
703 break;
704 }
705 case 'l': {/* long int */
706 long *p = va_arg(*p_va, long *);
707 long ival;
708 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000709 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 ival = PyLong_AsLong(arg);
711 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000712 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 else
714 *p = ival;
715 break;
716 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 case 'k': { /* long sized bitfield */
719 unsigned long *p = va_arg(*p_va, unsigned long *);
720 unsigned long ival;
721 if (PyLong_Check(arg))
722 ival = PyLong_AsUnsignedLongMask(arg);
723 else
724 return converterr("integer<k>", arg, msgbuf, bufsize);
725 *p = ival;
726 break;
727 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000728
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000729#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 case 'L': {/* PY_LONG_LONG */
731 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
732 PY_LONG_LONG ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000733 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000734 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 ival = PyLong_AsLongLong(arg);
Mark Dickinsonc7301312010-06-10 16:05:10 +0000736 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000737 RETURN_ERR_OCCURRED;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000738 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 break;
741 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 case 'K': { /* long long sized bitfield */
744 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
745 unsigned PY_LONG_LONG ival;
746 if (PyLong_Check(arg))
747 ival = PyLong_AsUnsignedLongLongMask(arg);
748 else
749 return converterr("integer<K>", arg, msgbuf, bufsize);
750 *p = ival;
751 break;
752 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000753#endif
Guido van Rossum98297ee2007-11-06 21:34:58 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 case 'f': {/* float */
756 float *p = va_arg(*p_va, float *);
757 double dval = PyFloat_AsDouble(arg);
758 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000759 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 else
761 *p = (float) dval;
762 break;
763 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 case 'd': {/* double */
766 double *p = va_arg(*p_va, double *);
767 double dval = PyFloat_AsDouble(arg);
768 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000769 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 else
771 *p = dval;
772 break;
773 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 case 'D': {/* complex double */
776 Py_complex *p = va_arg(*p_va, Py_complex *);
777 Py_complex cval;
778 cval = PyComplex_AsCComplex(arg);
779 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000780 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 else
782 *p = cval;
783 break;
784 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 case 'c': {/* char */
787 char *p = va_arg(*p_va, char *);
788 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
789 *p = PyBytes_AS_STRING(arg)[0];
Eli Bendersky906b88f2011-07-29 07:05:08 +0300790 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
791 *p = PyByteArray_AS_STRING(arg)[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 else
793 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
794 break;
795 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 case 'C': {/* unicode char */
798 int *p = va_arg(*p_va, int *);
Victor Stinnere1335c72011-10-04 20:53:03 +0200799 int kind;
800 void *data;
801
802 if (!PyUnicode_Check(arg))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 return converterr("a unicode character", arg, msgbuf, bufsize);
Victor Stinnere1335c72011-10-04 20:53:03 +0200804
805 if (PyUnicode_READY(arg))
806 RETURN_ERR_OCCURRED;
807
808 if (PyUnicode_GET_LENGTH(arg) != 1)
809 return converterr("a unicode character", arg, msgbuf, bufsize);
810
811 kind = PyUnicode_KIND(arg);
812 data = PyUnicode_DATA(arg);
813 *p = PyUnicode_READ(kind, data, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 break;
815 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000816
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700817 case 'p': {/* boolean *p*redicate */
818 int *p = va_arg(*p_va, int *);
819 int val = PyObject_IsTrue(arg);
820 if (val > 0)
821 *p = 1;
822 else if (val == 0)
823 *p = 0;
824 else
825 RETURN_ERR_OCCURRED;
826 break;
827 }
828
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000829 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 case 'y': {/* any buffer-like object, but not PyUnicode */
833 void **p = (void **)va_arg(*p_va, char **);
834 char *buf;
835 Py_ssize_t count;
836 if (*format == '*') {
837 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
838 return converterr(buf, arg, msgbuf, bufsize);
839 format++;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400840 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 return converterr(
842 "(cleanup problem)",
843 arg, msgbuf, bufsize);
844 }
845 break;
846 }
847 count = convertbuffer(arg, p, &buf);
848 if (count < 0)
849 return converterr(buf, arg, msgbuf, bufsize);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000850 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 FETCH_SIZE;
852 STORE_SIZE(count);
853 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000854 } else {
855 if (strlen(*p) != count)
856 return converterr(
857 "bytes without null bytes",
858 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 }
860 break;
861 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000862
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000863 case 's': /* text string */
864 case 'z': /* text string or None */
865 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000867 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000869
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000870 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
872 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200873 Py_ssize_t len;
874 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
875 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 return converterr(CONV_UNICODE,
877 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200878 PyBuffer_FillInfo(p, arg, sarg, len, 1, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 }
880 else { /* any buffer-like object */
881 char *buf;
882 if (getbuffer(arg, p, &buf) < 0)
883 return converterr(buf, arg, msgbuf, bufsize);
884 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400885 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 return converterr(
887 "(cleanup problem)",
888 arg, msgbuf, bufsize);
889 }
890 format++;
891 } else if (*format == '#') { /* any buffer-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000892 /* "s#" or "z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 void **p = (void **)va_arg(*p_va, char **);
894 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000895
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000896 if (c == 'z' && arg == Py_None) {
897 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 STORE_SIZE(0);
899 }
900 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200901 Py_ssize_t len;
902 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
903 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 return converterr(CONV_UNICODE,
905 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200906 *p = sarg;
907 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 }
909 else { /* any buffer-like object */
910 /* XXX Really? */
911 char *buf;
912 Py_ssize_t count = convertbuffer(arg, p, &buf);
913 if (count < 0)
914 return converterr(buf, arg, msgbuf, bufsize);
915 STORE_SIZE(count);
916 }
917 format++;
918 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000919 /* "s" or "z" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 char **p = va_arg(*p_va, char **);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200921 Py_ssize_t len;
922 sarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000923
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000924 if (c == 'z' && arg == Py_None)
925 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200927 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
928 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return converterr(CONV_UNICODE,
930 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200931 *p = sarg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 }
933 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000934 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200936 if (*p != NULL && sarg != NULL && (Py_ssize_t) strlen(*p) != len)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 return converterr(
Victor Stinner9d663d02011-10-13 23:25:03 +0200938 c == 'z' ? "str without null characters or None"
939 : "str without null characters",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 arg, msgbuf, bufsize);
941 }
942 break;
943 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000944
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000945 case 'u': /* raw unicode buffer (Py_UNICODE *) */
946 case 'Z': /* raw unicode buffer or None */
947 {
Brett Cannonb94767f2011-02-22 20:15:44 +0000948 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 if (*format == '#') { /* any buffer-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000951 /* "s#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +0000953
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000954 if (c == 'Z' && arg == Py_None) {
955 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 STORE_SIZE(0);
957 }
958 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200959 Py_ssize_t len;
960 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200961 if (*p == NULL)
962 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200963 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 }
Victor Stinner5216e6d2010-06-08 21:45:51 +0000965 else
966 return converterr("str or None", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 format++;
968 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000969 /* "s" or "Z" */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000970 if (c == 'Z' && arg == Py_None)
971 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000972 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200973 Py_ssize_t len;
974 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200975 if (*p == NULL)
976 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200977 if (Py_UNICODE_strlen(*p) != len)
Victor Stinner06e49dd2010-06-13 18:21:50 +0000978 return converterr(
Victor Stinner9d663d02011-10-13 23:25:03 +0200979 "str without null characters or None",
Victor Stinner06e49dd2010-06-13 18:21:50 +0000980 arg, msgbuf, bufsize);
981 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000982 return converterr(c == 'Z' ? "str or None" : "str",
983 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 }
985 break;
986 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 case 'e': {/* encoded string */
989 char **buffer;
990 const char *encoding;
991 PyObject *s;
992 int recode_strings;
993 Py_ssize_t size;
994 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 /* Get 'e' parameter: the encoding name */
997 encoding = (const char *)va_arg(*p_va, const char *);
998 if (encoding == NULL)
999 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 /* Get output buffer parameter:
1002 's' (recode all objects via Unicode) or
1003 't' (only recode non-string objects)
1004 */
1005 if (*format == 's')
1006 recode_strings = 1;
1007 else if (*format == 't')
1008 recode_strings = 0;
1009 else
1010 return converterr(
1011 "(unknown parser marker combination)",
1012 arg, msgbuf, bufsize);
1013 buffer = (char **)va_arg(*p_va, char **);
1014 format++;
1015 if (buffer == NULL)
1016 return converterr("(buffer is NULL)",
1017 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 /* Encode object */
1020 if (!recode_strings &&
1021 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1022 s = arg;
1023 Py_INCREF(s);
1024 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1025 return converterr("(AsCharBuffer failed)",
1026 arg, msgbuf, bufsize);
1027 }
1028 else {
1029 PyObject *u;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 /* Convert object to Unicode */
1032 u = PyUnicode_FromObject(arg);
1033 if (u == NULL)
1034 return converterr(
1035 "string or unicode or text buffer",
1036 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 /* Encode object; use default error handling */
1039 s = PyUnicode_AsEncodedString(u,
1040 encoding,
1041 NULL);
1042 Py_DECREF(u);
1043 if (s == NULL)
1044 return converterr("(encoding failed)",
1045 arg, msgbuf, bufsize);
1046 if (!PyBytes_Check(s)) {
1047 Py_DECREF(s);
1048 return converterr(
1049 "(encoder failed to return bytes)",
1050 arg, msgbuf, bufsize);
1051 }
1052 size = PyBytes_GET_SIZE(s);
1053 ptr = PyBytes_AS_STRING(s);
1054 if (ptr == NULL)
1055 ptr = "";
1056 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 /* Write output; output is guaranteed to be 0-terminated */
1059 if (*format == '#') {
1060 /* Using buffer length parameter '#':
1061
1062 - if *buffer is NULL, a new buffer of the
1063 needed size is allocated and the data
1064 copied into it; *buffer is updated to point
1065 to the new buffer; the caller is
1066 responsible for PyMem_Free()ing it after
1067 usage
1068
1069 - if *buffer is not NULL, the data is
1070 copied to *buffer; *buffer_len has to be
1071 set to the size of the buffer on input;
1072 buffer overflow is signalled with an error;
1073 buffer has to provide enough room for the
1074 encoded string plus the trailing 0-byte
1075
1076 - in both cases, *buffer_len is updated to
1077 the size of the buffer /excluding/ the
1078 trailing 0-byte
1079
1080 */
1081 FETCH_SIZE;
1082
1083 format++;
1084 if (q == NULL && q2 == NULL) {
1085 Py_DECREF(s);
1086 return converterr(
1087 "(buffer_len is NULL)",
1088 arg, msgbuf, bufsize);
1089 }
1090 if (*buffer == NULL) {
1091 *buffer = PyMem_NEW(char, size + 1);
1092 if (*buffer == NULL) {
1093 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001094 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001095 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001097 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 Py_DECREF(s);
1099 return converterr(
1100 "(cleanup problem)",
1101 arg, msgbuf, bufsize);
1102 }
1103 } else {
1104 if (size + 1 > BUFFER_LEN) {
1105 Py_DECREF(s);
1106 return converterr(
1107 "(buffer overflow)",
1108 arg, msgbuf, bufsize);
1109 }
1110 }
1111 memcpy(*buffer, ptr, size+1);
1112 STORE_SIZE(size);
1113 } else {
1114 /* Using a 0-terminated buffer:
1115
1116 - the encoded string has to be 0-terminated
1117 for this variant to work; if it is not, an
1118 error raised
1119
1120 - a new buffer of the needed size is
1121 allocated and the data copied into it;
1122 *buffer is updated to point to the new
1123 buffer; the caller is responsible for
1124 PyMem_Free()ing it after usage
1125
1126 */
1127 if ((Py_ssize_t)strlen(ptr) != size) {
1128 Py_DECREF(s);
1129 return converterr(
1130 "encoded string without NULL bytes",
1131 arg, msgbuf, bufsize);
1132 }
1133 *buffer = PyMem_NEW(char, size + 1);
1134 if (*buffer == NULL) {
1135 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001136 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001137 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001139 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 Py_DECREF(s);
1141 return converterr("(cleanup problem)",
1142 arg, msgbuf, bufsize);
1143 }
1144 memcpy(*buffer, ptr, size+1);
1145 }
1146 Py_DECREF(s);
1147 break;
1148 }
1149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 case 'S': { /* PyBytes object */
1151 PyObject **p = va_arg(*p_va, PyObject **);
1152 if (PyBytes_Check(arg))
1153 *p = arg;
1154 else
1155 return converterr("bytes", arg, msgbuf, bufsize);
1156 break;
1157 }
1158
1159 case 'Y': { /* PyByteArray object */
1160 PyObject **p = va_arg(*p_va, PyObject **);
1161 if (PyByteArray_Check(arg))
1162 *p = arg;
1163 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001164 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 break;
1166 }
1167
1168 case 'U': { /* PyUnicode object */
1169 PyObject **p = va_arg(*p_va, PyObject **);
1170 if (PyUnicode_Check(arg))
1171 *p = arg;
1172 else
1173 return converterr("str", arg, msgbuf, bufsize);
1174 break;
1175 }
1176
1177 case 'O': { /* object */
1178 PyTypeObject *type;
1179 PyObject **p;
1180 if (*format == '!') {
1181 type = va_arg(*p_va, PyTypeObject*);
1182 p = va_arg(*p_va, PyObject **);
1183 format++;
1184 if (PyType_IsSubtype(arg->ob_type, type))
1185 *p = arg;
1186 else
1187 return converterr(type->tp_name, arg, msgbuf, bufsize);
1188
1189 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 else if (*format == '&') {
1191 typedef int (*converter)(PyObject *, void *);
1192 converter convert = va_arg(*p_va, converter);
1193 void *addr = va_arg(*p_va, void *);
1194 int res;
1195 format++;
1196 if (! (res = (*convert)(arg, addr)))
1197 return converterr("(unspecified)",
1198 arg, msgbuf, bufsize);
1199 if (res == Py_CLEANUP_SUPPORTED &&
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001200 addcleanup(addr, freelist, convert) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 return converterr("(cleanup problem)",
1202 arg, msgbuf, bufsize);
1203 }
1204 else {
1205 p = va_arg(*p_va, PyObject **);
1206 *p = arg;
1207 }
1208 break;
1209 }
1210
1211
Victor Stinner25e8ec42010-06-25 00:02:38 +00001212 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214
Victor Stinner25e8ec42010-06-25 00:02:38 +00001215 if (*format != '*')
1216 return converterr(
1217 "invalid use of 'w' format character",
1218 arg, msgbuf, bufsize);
1219 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220
Victor Stinner25e8ec42010-06-25 00:02:38 +00001221 /* Caller is interested in Py_buffer, and the object
1222 supports it directly. */
1223 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1224 PyErr_Clear();
1225 return converterr("read-write buffer", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 }
Victor Stinner8182b712010-07-28 00:40:58 +00001227 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1228 PyBuffer_Release((Py_buffer*)p);
1229 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1230 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001231 if (addcleanup(p, freelist, cleanup_buffer)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001232 return converterr(
1233 "(cleanup problem)",
1234 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 break;
1237 }
1238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 default:
1240 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
1241
1242 }
1243
1244 *p_format = format;
1245 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001246
1247#undef FETCH_SIZE
1248#undef STORE_SIZE
1249#undef BUFFER_LEN
1250#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001251}
Guido van Rossumaa354651996-08-19 19:32:04 +00001252
Martin v. Löwis18e16552006-02-15 17:27:45 +00001253static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001254convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001255{
Victor Stinner5cb62392010-06-06 20:27:51 +00001256 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 Py_ssize_t count;
1258 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 *errmsg = NULL;
1261 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001262 if (pb != NULL && pb->bf_releasebuffer != NULL) {
1263 *errmsg = "read-only pinned buffer";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 return -1;
1265 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001266
Victor Stinner8182b712010-07-28 00:40:58 +00001267 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 count = view.len;
1270 *p = view.buf;
1271 PyBuffer_Release(&view);
1272 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001273}
Guido van Rossumaa354651996-08-19 19:32:04 +00001274
Martin v. Löwis423be952008-08-13 15:53:07 +00001275static int
Neal Norwitz2f99b242008-08-24 05:48:10 +00001276getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001277{
Victor Stinner8182b712010-07-28 00:40:58 +00001278 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 *errmsg = "bytes or buffer";
1280 return -1;
1281 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001282 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001283 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001284 *errmsg = "contiguous buffer";
1285 return -1;
1286 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001288}
1289
Guido van Rossumaa354651996-08-19 19:32:04 +00001290/* Support for keyword arguments donated by
1291 Geoff Philbrick <philbric@delphi.hks.com> */
1292
Tim Petersf8cd3e82001-10-27 04:26:57 +00001293/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001294int
1295PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 PyObject *keywords,
1297 const char *format,
1298 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 int retval;
1301 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 if ((args == NULL || !PyTuple_Check(args)) ||
1304 (keywords != NULL && !PyDict_Check(keywords)) ||
1305 format == NULL ||
1306 kwlist == NULL)
1307 {
1308 PyErr_BadInternalCall();
1309 return 0;
1310 }
Tim Peters45772cd2001-10-27 03:58:40 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 va_start(va, kwlist);
1313 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1314 va_end(va);
1315 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001316}
1317
1318int
1319_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 PyObject *keywords,
1321 const char *format,
1322 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 int retval;
1325 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 if ((args == NULL || !PyTuple_Check(args)) ||
1328 (keywords != NULL && !PyDict_Check(keywords)) ||
1329 format == NULL ||
1330 kwlist == NULL)
1331 {
1332 PyErr_BadInternalCall();
1333 return 0;
1334 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 va_start(va, kwlist);
1337 retval = vgetargskeywords(args, keywords, format,
1338 kwlist, &va, FLAG_SIZE_T);
1339 va_end(va);
1340 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001341}
1342
1343
Brett Cannon711e7d92004-07-10 22:20:32 +00001344int
1345PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001346 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001347 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001348 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 int retval;
1351 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 if ((args == NULL || !PyTuple_Check(args)) ||
1354 (keywords != NULL && !PyDict_Check(keywords)) ||
1355 format == NULL ||
1356 kwlist == NULL)
1357 {
1358 PyErr_BadInternalCall();
1359 return 0;
1360 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001361
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001362 Py_VA_COPY(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1365 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001366}
1367
1368int
1369_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 PyObject *keywords,
1371 const char *format,
1372 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 int retval;
1375 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if ((args == NULL || !PyTuple_Check(args)) ||
1378 (keywords != NULL && !PyDict_Check(keywords)) ||
1379 format == NULL ||
1380 kwlist == NULL)
1381 {
1382 PyErr_BadInternalCall();
1383 return 0;
1384 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001385
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001386 Py_VA_COPY(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 retval = vgetargskeywords(args, keywords, format,
1389 kwlist, &lva, FLAG_SIZE_T);
1390 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001391}
1392
Benjamin Petersonfb886362010-04-24 18:21:17 +00001393int
1394PyArg_ValidateKeywordArguments(PyObject *kwargs)
1395{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001396 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 PyErr_BadInternalCall();
1398 return 0;
1399 }
1400 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1401 PyErr_SetString(PyExc_TypeError,
1402 "keyword arguments must be strings");
1403 return 0;
1404 }
1405 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001406}
1407
Christian Heimes380f7f22008-02-28 11:19:05 +00001408#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001409
Guido van Rossumaa354651996-08-19 19:32:04 +00001410static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001411vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 char msgbuf[512];
1415 int levels[32];
1416 const char *fname, *msg, *custom_msg, *keyword;
1417 int min = INT_MAX;
Larry Hastings83a9f482012-03-20 20:06:16 +00001418 int max = INT_MAX;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 int i, len, nargs, nkeywords;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001420 PyObject *current_arg;
1421 freelist_t freelist = {0, NULL};
Tim Petersf4331c12001-10-27 00:17:34 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 assert(args != NULL && PyTuple_Check(args));
1424 assert(keywords == NULL || PyDict_Check(keywords));
1425 assert(format != NULL);
1426 assert(kwlist != NULL);
1427 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 /* grab the function name or custom error msg first (mutually exclusive) */
1430 fname = strchr(format, ':');
1431 if (fname) {
1432 fname++;
1433 custom_msg = NULL;
1434 }
1435 else {
1436 custom_msg = strchr(format,';');
1437 if (custom_msg)
1438 custom_msg++;
1439 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 /* scan kwlist and get greatest possible nbr of args */
1442 for (len=0; kwlist[len]; len++)
1443 continue;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001444
Benjamin Petersonf7c13212012-03-16 12:23:39 -05001445 freelist.entries = PyMem_NEW(freelistentry_t, len);
Benjamin Peterson7ed67272012-03-16 12:21:02 -05001446 if (freelist.entries == NULL) {
1447 PyErr_NoMemory();
1448 return 0;
1449 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 nargs = PyTuple_GET_SIZE(args);
1452 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1453 if (nargs + nkeywords > len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +01001454 PyErr_Format(PyExc_TypeError,
1455 "%s%s takes at most %d argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 (fname == NULL) ? "function" : fname,
1457 (fname == NULL) ? "" : "()",
1458 len,
1459 (len == 1) ? "" : "s",
1460 nargs + nkeywords);
Benjamin Peterson01feaec2012-03-16 13:25:58 -05001461 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 }
Tim Petersc2f01122001-10-27 07:25:06 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1465 for (i = 0; i < len; i++) {
1466 keyword = kwlist[i];
1467 if (*format == '|') {
Larry Hastings83a9f482012-03-20 20:06:16 +00001468 if (min != INT_MAX) {
1469 PyErr_SetString(PyExc_RuntimeError,
1470 "Invalid format string (| specified twice)");
1471 return cleanreturn(0, &freelist);
1472 }
1473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 min = i;
1475 format++;
Larry Hastings83a9f482012-03-20 20:06:16 +00001476
1477 if (max != INT_MAX) {
1478 PyErr_SetString(PyExc_RuntimeError,
1479 "Invalid format string ($ before |)");
1480 return cleanreturn(0, &freelist);
1481 }
1482 }
1483 if (*format == '$') {
1484 if (max != INT_MAX) {
1485 PyErr_SetString(PyExc_RuntimeError,
1486 "Invalid format string ($ specified twice)");
1487 return cleanreturn(0, &freelist);
1488 }
1489
1490 max = i;
1491 format++;
1492
1493 if (max < nargs) {
1494 PyErr_Format(PyExc_TypeError,
1495 "Function takes %s %d positional arguments"
1496 " (%d given)",
1497 (min != INT_MAX) ? "at most" : "exactly",
1498 max, nargs);
1499 return cleanreturn(0, &freelist);
1500 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 }
1502 if (IS_END_OF_FORMAT(*format)) {
1503 PyErr_Format(PyExc_RuntimeError,
1504 "More keyword list entries (%d) than "
1505 "format specifiers (%d)", len, i);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001506 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 }
1508 current_arg = NULL;
1509 if (nkeywords) {
1510 current_arg = PyDict_GetItemString(keywords, keyword);
1511 }
1512 if (current_arg) {
1513 --nkeywords;
1514 if (i < nargs) {
1515 /* arg present in tuple and in dict */
1516 PyErr_Format(PyExc_TypeError,
1517 "Argument given by name ('%s') "
1518 "and position (%d)",
1519 keyword, i+1);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001520 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 }
1522 }
1523 else if (nkeywords && PyErr_Occurred())
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001524 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 else if (i < nargs)
1526 current_arg = PyTuple_GET_ITEM(args, i);
Christian Heimes380f7f22008-02-28 11:19:05 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 if (current_arg) {
1529 msg = convertitem(current_arg, &format, p_va, flags,
1530 levels, msgbuf, sizeof(msgbuf), &freelist);
1531 if (msg) {
1532 seterror(i+1, msg, levels, fname, custom_msg);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001533 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 }
1535 continue;
1536 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 if (i < min) {
1539 PyErr_Format(PyExc_TypeError, "Required argument "
1540 "'%s' (pos %d) not found",
1541 keyword, i+1);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001542 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 }
1544 /* current code reports success when all required args
1545 * fulfilled and no keyword args left, with no further
1546 * validation. XXX Maybe skip this in debug build ?
1547 */
1548 if (!nkeywords)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001549 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 /* We are into optional args, skip thru to any remaining
1552 * keyword args */
1553 msg = skipitem(&format, p_va, flags);
1554 if (msg) {
1555 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1556 format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001557 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 }
1559 }
Tim Petersb054be42001-10-27 05:07:41 +00001560
Larry Hastings83a9f482012-03-20 20:06:16 +00001561 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 PyErr_Format(PyExc_RuntimeError,
1563 "more argument specifiers than keyword list entries "
1564 "(remaining format:'%s')", format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001565 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 }
Tim Petersc2f01122001-10-27 07:25:06 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 /* make sure there are no extraneous keyword arguments */
1569 if (nkeywords > 0) {
1570 PyObject *key, *value;
1571 Py_ssize_t pos = 0;
1572 while (PyDict_Next(keywords, &pos, &key, &value)) {
1573 int match = 0;
1574 char *ks;
1575 if (!PyUnicode_Check(key)) {
1576 PyErr_SetString(PyExc_TypeError,
1577 "keywords must be strings");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001578 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 }
Victor Stinner93b55132010-05-19 00:54:06 +00001580 /* check that _PyUnicode_AsString() result is not NULL */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 ks = _PyUnicode_AsString(key);
Victor Stinner93b55132010-05-19 00:54:06 +00001582 if (ks != NULL) {
1583 for (i = 0; i < len; i++) {
1584 if (!strcmp(ks, kwlist[i])) {
1585 match = 1;
1586 break;
1587 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 }
1589 }
1590 if (!match) {
1591 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001592 "'%U' is an invalid keyword "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 "argument for this function",
Victor Stinner93b55132010-05-19 00:54:06 +00001594 key);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001595 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 }
1597 }
1598 }
1599
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001600 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001601}
1602
1603
1604static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001605skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 const char *format = *p_format;
1608 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001611
Larry Hastingsa3479012012-05-08 23:52:03 -07001612 /*
1613 * codes that take a single data pointer as an argument
1614 * (the type of the pointer is irrelevant)
1615 */
Georg Brandl6dd14612005-09-14 19:29:53 +00001616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 case 'b': /* byte -- very short int */
1618 case 'B': /* byte as bitfield */
1619 case 'h': /* short int */
1620 case 'H': /* short int as bitfield */
1621 case 'i': /* int */
1622 case 'I': /* int sized bitfield */
1623 case 'l': /* long int */
1624 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001625#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 case 'L': /* PY_LONG_LONG */
1627 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001628#endif
Larry Hastingsa3479012012-05-08 23:52:03 -07001629 case 'n': /* Py_ssize_t */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 case 'f': /* float */
1631 case 'd': /* double */
1632 case 'D': /* complex double */
1633 case 'c': /* char */
1634 case 'C': /* unicode char */
Larry Hastings10ba07a2012-05-07 02:44:50 -07001635 case 'p': /* boolean predicate */
Larry Hastingsa3479012012-05-08 23:52:03 -07001636 case 'S': /* string object */
1637 case 'Y': /* string object */
1638 case 'U': /* unicode string object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 {
1640 (void) va_arg(*p_va, void *);
1641 break;
1642 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 case 'e': /* string with encoding */
1647 {
1648 (void) va_arg(*p_va, const char *);
1649 if (!(*format == 's' || *format == 't'))
1650 /* after 'e', only 's' and 't' is allowed */
1651 goto err;
1652 format++;
1653 /* explicit fallthrough to string cases */
1654 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 case 's': /* string */
1657 case 'z': /* string or None */
1658 case 'y': /* bytes */
1659 case 'u': /* unicode string */
Larry Hastingsd9e4a412012-05-08 03:51:18 -07001660 case 'Z': /* unicode string or None */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 case 'w': /* buffer, read-write */
1662 {
1663 (void) va_arg(*p_va, char **);
1664 if (*format == '#') {
1665 if (flags & FLAG_SIZE_T)
1666 (void) va_arg(*p_va, Py_ssize_t *);
1667 else
1668 (void) va_arg(*p_va, int *);
1669 format++;
1670 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
1671 format++;
1672 }
1673 break;
1674 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 case 'O': /* object */
1677 {
1678 if (*format == '!') {
1679 format++;
1680 (void) va_arg(*p_va, PyTypeObject*);
1681 (void) va_arg(*p_va, PyObject **);
1682 }
1683 else if (*format == '&') {
1684 typedef int (*converter)(PyObject *, void *);
1685 (void) va_arg(*p_va, converter);
1686 (void) va_arg(*p_va, void *);
1687 format++;
1688 }
1689 else {
1690 (void) va_arg(*p_va, PyObject **);
1691 }
1692 break;
1693 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 case '(': /* bypass tuple, not handled at all previously */
1696 {
1697 char *msg;
1698 for (;;) {
1699 if (*format==')')
1700 break;
1701 if (IS_END_OF_FORMAT(*format))
1702 return "Unmatched left paren in format "
1703 "string";
1704 msg = skipitem(&format, p_va, flags);
1705 if (msg)
1706 return msg;
1707 }
1708 format++;
1709 break;
1710 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 case ')':
1713 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001716err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 *p_format = format;
1722 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00001723}
Fred Drakee4616e62001-10-23 21:09:29 +00001724
1725
1726int
Martin v. Löwis76246742006-03-01 04:06:10 +00001727PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 Py_ssize_t i, l;
1730 PyObject **o;
1731 va_list vargs;
Fred Drakee4616e62001-10-23 21:09:29 +00001732
1733#ifdef HAVE_STDARG_PROTOTYPES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 va_start(vargs, max);
Fred Drakee4616e62001-10-23 21:09:29 +00001735#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 va_start(vargs);
Fred Drakee4616e62001-10-23 21:09:29 +00001737#endif
1738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 assert(min >= 0);
1740 assert(min <= max);
1741 if (!PyTuple_Check(args)) {
1742 PyErr_SetString(PyExc_SystemError,
1743 "PyArg_UnpackTuple() argument list is not a tuple");
1744 return 0;
1745 }
1746 l = PyTuple_GET_SIZE(args);
1747 if (l < min) {
1748 if (name != NULL)
1749 PyErr_Format(
1750 PyExc_TypeError,
1751 "%s expected %s%zd arguments, got %zd",
1752 name, (min == max ? "" : "at least "), min, l);
1753 else
1754 PyErr_Format(
1755 PyExc_TypeError,
1756 "unpacked tuple should have %s%zd elements,"
1757 " but has %zd",
1758 (min == max ? "" : "at least "), min, l);
1759 va_end(vargs);
1760 return 0;
1761 }
1762 if (l > max) {
1763 if (name != NULL)
1764 PyErr_Format(
1765 PyExc_TypeError,
1766 "%s expected %s%zd arguments, got %zd",
1767 name, (min == max ? "" : "at most "), max, l);
1768 else
1769 PyErr_Format(
1770 PyExc_TypeError,
1771 "unpacked tuple should have %s%zd elements,"
1772 " but has %zd",
1773 (min == max ? "" : "at most "), max, l);
1774 va_end(vargs);
1775 return 0;
1776 }
1777 for (i = 0; i < l; i++) {
1778 o = va_arg(vargs, PyObject **);
1779 *o = PyTuple_GET_ITEM(args, i);
1780 }
1781 va_end(vargs);
1782 return 1;
Fred Drakee4616e62001-10-23 21:09:29 +00001783}
Georg Brandl02c42872005-08-26 06:42:30 +00001784
1785
1786/* For type constructors that don't take keyword args
1787 *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001788 * Sets a TypeError and returns 0 if the kwds dict is
Thomas Wouters89f507f2006-12-13 04:49:30 +00001789 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001790 */
1791int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001792_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 if (kw == NULL)
1795 return 1;
1796 if (!PyDict_CheckExact(kw)) {
1797 PyErr_BadInternalCall();
1798 return 0;
1799 }
1800 if (PyDict_Size(kw) == 0)
1801 return 1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1804 funcname);
1805 return 0;
Georg Brandl02c42872005-08-26 06:42:30 +00001806}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001807#ifdef __cplusplus
1808};
1809#endif