blob: 268a1134ea644f5db0e5bd600b42beeb7a48a5a3 [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
Benjamin Petersonb173f782009-05-05 22:31:58 +0000143#define GETARGS_CAPSULE_NAME_CLEANUP_PTR "getargs.cleanup_ptr"
144#define GETARGS_CAPSULE_NAME_CLEANUP_BUFFER "getargs.cleanup_buffer"
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000145#define GETARGS_CAPSULE_NAME_CLEANUP_CONVERT "getargs.cleanup_convert"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000146
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400147static int
148cleanup_ptr(PyObject *self, void *ptr)
Antoine Pitrouf71995782008-08-29 18:37:05 +0000149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 if (ptr) {
151 PyMem_FREE(ptr);
152 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000154}
155
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000156static int
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400157cleanup_buffer(PyObject *self, void *ptr)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000158{
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400159 Py_buffer *buf = (Py_buffer *)ptr;
160 if (buf) {
161 PyBuffer_Release(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400163 return 0;
164}
165
166static int
167addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
168{
169 int index;
170
171 index = freelist->first_available;
172 freelist->first_available += 1;
173
174 freelist->entries[index].item = ptr;
175 freelist->entries[index].destructor = destructor;
176
177 return 0;
178}
179
180static int
181cleanreturn(int retval, freelist_t *freelist)
182{
183 int index;
184
185 if (retval == 0) {
186 /* A failure occurred, therefore execute all of the cleanup
187 functions.
188 */
189 for (index = 0; index < freelist->first_available; ++index) {
190 freelist->entries[index].destructor(NULL,
191 freelist->entries[index].item);
192 }
193 }
194 PyMem_Free(freelist->entries);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 return retval;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000196}
197
198
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000199static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000200vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 char msgbuf[256];
203 int levels[32];
204 const char *fname = NULL;
205 const char *message = NULL;
206 int min = -1;
207 int max = 0;
208 int level = 0;
209 int endfmt = 0;
210 const char *formatsave = format;
211 Py_ssize_t i, len;
212 char *msg;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400213 freelist_t freelist = {0, NULL};
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;
247 default:
248 if (level == 0) {
249 if (c == 'O')
250 max++;
251 else if (isalpha(Py_CHARMASK(c))) {
252 if (c != 'e') /* skip encoded */
253 max++;
254 } else if (c == '|')
255 min = max;
256 }
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
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400269 freelist.entries = PyMem_New(freelistentry_t, max);
Benjamin Peterson7ed67272012-03-16 12:21:02 -0500270 if (freelist.entries == NULL) {
271 PyErr_NoMemory();
272 return 0;
273 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 if (compat) {
276 if (max == 0) {
277 if (args == NULL)
278 return 1;
Victor Stinner6ced7c42011-03-21 18:15:42 +0100279 PyErr_Format(PyExc_TypeError,
280 "%.200s%s takes no arguments",
281 fname==NULL ? "function" : fname,
282 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400283 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 }
285 else if (min == 1 && max == 1) {
286 if (args == NULL) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100287 PyErr_Format(PyExc_TypeError,
288 "%.200s%s takes at least one argument",
289 fname==NULL ? "function" : fname,
290 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400291 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 }
293 msg = convertitem(args, &format, p_va, flags, levels,
294 msgbuf, sizeof(msgbuf), &freelist);
295 if (msg == NULL)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400296 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 seterror(levels[0], msg, levels+1, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400298 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 }
300 else {
301 PyErr_SetString(PyExc_SystemError,
302 "old style getargs format uses new features");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400303 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 }
305 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (!PyTuple_Check(args)) {
308 PyErr_SetString(PyExc_SystemError,
309 "new style getargs format but argument is not a tuple");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400310 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 len = PyTuple_GET_SIZE(args);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 if (len < min || max < len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100316 if (message == NULL)
317 PyErr_Format(PyExc_TypeError,
318 "%.150s%s takes %s %d argument%s (%ld given)",
319 fname==NULL ? "function" : fname,
320 fname==NULL ? "" : "()",
321 min==max ? "exactly"
322 : len < min ? "at least" : "at most",
323 len < min ? min : max,
324 (len < min ? min : max) == 1 ? "" : "s",
325 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
326 else
327 PyErr_SetString(PyExc_TypeError, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400328 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 for (i = 0; i < len; i++) {
332 if (*format == '|')
333 format++;
334 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
335 flags, levels, msgbuf,
336 sizeof(msgbuf), &freelist);
337 if (msg) {
338 seterror(i+1, msg, levels, fname, msg);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400339 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 }
341 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) &&
344 *format != '(' &&
345 *format != '|' && *format != ':' && *format != ';') {
346 PyErr_Format(PyExc_SystemError,
347 "bad format string: %.200s", formatsave);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400348 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000350
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400351 return cleanreturn(1, &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000352}
353
354
355
356static void
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000357seterror(int iarg, const char *msg, int *levels, const char *fname,
358 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 char buf[512];
361 int i;
362 char *p = buf;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 if (PyErr_Occurred())
365 return;
366 else if (message == NULL) {
367 if (fname != NULL) {
368 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
369 p += strlen(p);
370 }
371 if (iarg != 0) {
372 PyOS_snprintf(p, sizeof(buf) - (p - buf),
373 "argument %d", iarg);
374 i = 0;
375 p += strlen(p);
376 while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) {
377 PyOS_snprintf(p, sizeof(buf) - (p - buf),
378 ", item %d", levels[i]-1);
379 p += strlen(p);
380 i++;
381 }
382 }
383 else {
384 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
385 p += strlen(p);
386 }
387 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
388 message = buf;
389 }
390 PyErr_SetString(PyExc_TypeError, message);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000391}
392
393
394/* Convert a tuple argument.
395 On entry, *p_format points to the character _after_ the opening '('.
396 On successful exit, *p_format points to the closing ')'.
397 If successful:
398 *p_format and *p_va are updated,
399 *levels and *msgbuf are untouched,
400 and NULL is returned.
401 If the argument is invalid:
402 *p_format is unchanged,
403 *p_va is undefined,
404 *levels is a 0-terminated list of item numbers,
405 *msgbuf contains an error message, whose format is:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 "must be <typename1>, not <typename2>", where:
407 <typename1> is the name of the expected type, and
408 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000409 and msgbuf is returned.
410*/
411
412static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000413converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000414 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400415 freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 int level = 0;
418 int n = 0;
419 const char *format = *p_format;
420 int i;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 for (;;) {
423 int c = *format++;
424 if (c == '(') {
425 if (level == 0)
426 n++;
427 level++;
428 }
429 else if (c == ')') {
430 if (level == 0)
431 break;
432 level--;
433 }
434 else if (c == ':' || c == ';' || c == '\0')
435 break;
436 else if (level == 0 && isalpha(Py_CHARMASK(c)))
437 n++;
438 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
441 levels[0] = 0;
442 PyOS_snprintf(msgbuf, bufsize,
443 toplevel ? "expected %d arguments, not %.50s" :
444 "must be %d-item sequence, not %.50s",
445 n,
446 arg == Py_None ? "None" : arg->ob_type->tp_name);
447 return msgbuf;
448 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if ((i = PySequence_Size(arg)) != n) {
451 levels[0] = 0;
452 PyOS_snprintf(msgbuf, bufsize,
453 toplevel ? "expected %d arguments, not %d" :
454 "must be sequence of length %d, not %d",
455 n, i);
456 return msgbuf;
457 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 format = *p_format;
460 for (i = 0; i < n; i++) {
461 char *msg;
462 PyObject *item;
463 item = PySequence_GetItem(arg, i);
464 if (item == NULL) {
465 PyErr_Clear();
466 levels[0] = i+1;
467 levels[1] = 0;
468 strncpy(msgbuf, "is not retrievable", bufsize);
469 return msgbuf;
470 }
471 msg = convertitem(item, &format, p_va, flags, levels+1,
472 msgbuf, bufsize, freelist);
473 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
474 Py_XDECREF(item);
475 if (msg != NULL) {
476 levels[0] = i+1;
477 return msg;
478 }
479 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 *p_format = format;
482 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000483}
484
485
486/* Convert a single item. */
487
488static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000489convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400490 int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 char *msg;
493 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 if (*format == '(' /* ')' */) {
496 format++;
497 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
498 bufsize, 0, freelist);
499 if (msg == NULL)
500 format++;
501 }
502 else {
503 msg = convertsimple(arg, &format, p_va, flags,
504 msgbuf, bufsize, freelist);
505 if (msg != NULL)
506 levels[0] = 0;
507 }
508 if (msg == NULL)
509 *p_format = format;
510 return msg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000511}
512
513
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000514
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000515/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000516
517static char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000518converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 assert(expected != NULL);
521 assert(arg != NULL);
522 PyOS_snprintf(msgbuf, bufsize,
523 "must be %.50s, not %.50s", expected,
524 arg == Py_None ? "None" : arg->ob_type->tp_name);
525 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000526}
527
528#define CONV_UNICODE "(unicode conversion error)"
529
Guido van Rossum45aecf42006-03-15 04:58:47 +0000530/* Explicitly check for float arguments when integers are expected.
531 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000532static int
533float_argument_error(PyObject *arg)
534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 if (PyFloat_Check(arg)) {
536 PyErr_SetString(PyExc_TypeError,
537 "integer argument expected, got float" );
538 return 1;
539 }
540 else
541 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000542}
543
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000544/* Convert a non-tuple argument. Return NULL if conversion went OK,
545 or a string with a message describing the failure. The message is
546 formatted as "must be <desired type>, not <actual type>".
547 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000548 Don't call if a tuple is expected.
549
550 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000551*/
552
553static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000554convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400555 char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* For # codes */
558#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
559 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
560 else q=va_arg(*p_va, int*);
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000561#define STORE_SIZE(s) \
562 if (flags & FLAG_SIZE_T) \
563 *q2=s; \
564 else { \
565 if (INT_MAX < s) { \
566 PyErr_SetString(PyExc_OverflowError, \
567 "size does not fit in an int"); \
568 return converterr("", arg, msgbuf, bufsize); \
569 } \
570 *q=s; \
571 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000572#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
Victor Stinner6ab8e822011-01-04 11:16:49 +0000573#define RETURN_ERR_OCCURRED return msgbuf
Martin v. Löwis18e16552006-02-15 17:27:45 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 const char *format = *p_format;
576 char c = *format++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200577 char *sarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 case 'b': { /* unsigned byte -- very short int */
582 char *p = va_arg(*p_va, char *);
583 long ival;
584 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000585 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 ival = PyLong_AsLong(arg);
587 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000588 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 else if (ival < 0) {
590 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000591 "unsigned byte integer is less than minimum");
592 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 }
594 else if (ival > UCHAR_MAX) {
595 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000596 "unsigned byte integer is greater than maximum");
597 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 }
599 else
600 *p = (unsigned char) ival;
601 break;
602 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 case 'B': {/* byte sized bitfield - both signed and unsigned
605 values allowed */
606 char *p = va_arg(*p_va, char *);
607 long ival;
608 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000609 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 ival = PyLong_AsUnsignedLongMask(arg);
611 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000612 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 else
614 *p = (unsigned char) ival;
615 break;
616 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 case 'h': {/* signed short int */
619 short *p = va_arg(*p_va, short *);
620 long ival;
621 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000622 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 ival = PyLong_AsLong(arg);
624 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000625 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 else if (ival < SHRT_MIN) {
627 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000628 "signed short integer is less than minimum");
629 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 }
631 else if (ival > SHRT_MAX) {
632 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000633 "signed short integer is greater than maximum");
634 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 }
636 else
637 *p = (short) ival;
638 break;
639 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 case 'H': { /* short int sized bitfield, both signed and
642 unsigned allowed */
643 unsigned short *p = va_arg(*p_va, unsigned short *);
644 long ival;
645 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000646 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 ival = PyLong_AsUnsignedLongMask(arg);
648 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000649 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 else
651 *p = (unsigned short) ival;
652 break;
653 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 case 'i': {/* signed int */
656 int *p = va_arg(*p_va, int *);
657 long ival;
658 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000659 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 ival = PyLong_AsLong(arg);
661 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000662 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 else if (ival > INT_MAX) {
664 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000665 "signed integer is greater than maximum");
666 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 }
668 else if (ival < INT_MIN) {
669 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000670 "signed integer is less than minimum");
671 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 }
673 else
674 *p = ival;
675 break;
676 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 case 'I': { /* int sized bitfield, both signed and
679 unsigned allowed */
680 unsigned int *p = va_arg(*p_va, unsigned int *);
681 unsigned int ival;
682 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000683 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
685 if (ival == (unsigned int)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000686 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 else
688 *p = ival;
689 break;
690 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 case 'n': /* Py_ssize_t */
693 {
694 PyObject *iobj;
695 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
696 Py_ssize_t ival = -1;
697 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000698 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 iobj = PyNumber_Index(arg);
700 if (iobj != NULL) {
701 ival = PyLong_AsSsize_t(iobj);
702 Py_DECREF(iobj);
703 }
704 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000705 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 *p = ival;
707 break;
708 }
709 case 'l': {/* long int */
710 long *p = va_arg(*p_va, long *);
711 long ival;
712 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000713 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 ival = PyLong_AsLong(arg);
715 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000716 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 else
718 *p = ival;
719 break;
720 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 case 'k': { /* long sized bitfield */
723 unsigned long *p = va_arg(*p_va, unsigned long *);
724 unsigned long ival;
725 if (PyLong_Check(arg))
726 ival = PyLong_AsUnsignedLongMask(arg);
727 else
728 return converterr("integer<k>", arg, msgbuf, bufsize);
729 *p = ival;
730 break;
731 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000732
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000733#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 case 'L': {/* PY_LONG_LONG */
735 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
736 PY_LONG_LONG ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000737 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000738 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 ival = PyLong_AsLongLong(arg);
Mark Dickinsonc7301312010-06-10 16:05:10 +0000740 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000741 RETURN_ERR_OCCURRED;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000742 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 break;
745 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 case 'K': { /* long long sized bitfield */
748 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
749 unsigned PY_LONG_LONG ival;
750 if (PyLong_Check(arg))
751 ival = PyLong_AsUnsignedLongLongMask(arg);
752 else
753 return converterr("integer<K>", arg, msgbuf, bufsize);
754 *p = ival;
755 break;
756 }
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000757#endif
Guido van Rossum98297ee2007-11-06 21:34:58 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 case 'f': {/* float */
760 float *p = va_arg(*p_va, float *);
761 double dval = PyFloat_AsDouble(arg);
762 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000763 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 else
765 *p = (float) dval;
766 break;
767 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 case 'd': {/* double */
770 double *p = va_arg(*p_va, double *);
771 double dval = PyFloat_AsDouble(arg);
772 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000773 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 else
775 *p = dval;
776 break;
777 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 case 'D': {/* complex double */
780 Py_complex *p = va_arg(*p_va, Py_complex *);
781 Py_complex cval;
782 cval = PyComplex_AsCComplex(arg);
783 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000784 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 else
786 *p = cval;
787 break;
788 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 case 'c': {/* char */
791 char *p = va_arg(*p_va, char *);
792 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
793 *p = PyBytes_AS_STRING(arg)[0];
Eli Bendersky906b88f2011-07-29 07:05:08 +0300794 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
795 *p = PyByteArray_AS_STRING(arg)[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 else
797 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
798 break;
799 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 case 'C': {/* unicode char */
802 int *p = va_arg(*p_va, int *);
Victor Stinnere1335c72011-10-04 20:53:03 +0200803 int kind;
804 void *data;
805
806 if (!PyUnicode_Check(arg))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 return converterr("a unicode character", arg, msgbuf, bufsize);
Victor Stinnere1335c72011-10-04 20:53:03 +0200808
809 if (PyUnicode_READY(arg))
810 RETURN_ERR_OCCURRED;
811
812 if (PyUnicode_GET_LENGTH(arg) != 1)
813 return converterr("a unicode character", arg, msgbuf, bufsize);
814
815 kind = PyUnicode_KIND(arg);
816 data = PyUnicode_DATA(arg);
817 *p = PyUnicode_READ(kind, data, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 break;
819 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000820
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000821 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 case 'y': {/* any buffer-like object, but not PyUnicode */
825 void **p = (void **)va_arg(*p_va, char **);
826 char *buf;
827 Py_ssize_t count;
828 if (*format == '*') {
829 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
830 return converterr(buf, arg, msgbuf, bufsize);
831 format++;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400832 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 return converterr(
834 "(cleanup problem)",
835 arg, msgbuf, bufsize);
836 }
837 break;
838 }
839 count = convertbuffer(arg, p, &buf);
840 if (count < 0)
841 return converterr(buf, arg, msgbuf, bufsize);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000842 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 FETCH_SIZE;
844 STORE_SIZE(count);
845 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000846 } else {
847 if (strlen(*p) != count)
848 return converterr(
849 "bytes without null bytes",
850 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 }
852 break;
853 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000854
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000855 case 's': /* text string */
856 case 'z': /* text string or None */
857 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000859 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000861
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000862 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
864 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200865 Py_ssize_t len;
866 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
867 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 return converterr(CONV_UNICODE,
869 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200870 PyBuffer_FillInfo(p, arg, sarg, len, 1, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 }
872 else { /* any buffer-like object */
873 char *buf;
874 if (getbuffer(arg, p, &buf) < 0)
875 return converterr(buf, arg, msgbuf, bufsize);
876 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400877 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 return converterr(
879 "(cleanup problem)",
880 arg, msgbuf, bufsize);
881 }
882 format++;
883 } else if (*format == '#') { /* any buffer-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000884 /* "s#" or "z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 void **p = (void **)va_arg(*p_va, char **);
886 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000887
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000888 if (c == 'z' && arg == Py_None) {
889 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 STORE_SIZE(0);
891 }
892 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200893 Py_ssize_t len;
894 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
895 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 return converterr(CONV_UNICODE,
897 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200898 *p = sarg;
899 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 }
901 else { /* any buffer-like object */
902 /* XXX Really? */
903 char *buf;
904 Py_ssize_t count = convertbuffer(arg, p, &buf);
905 if (count < 0)
906 return converterr(buf, arg, msgbuf, bufsize);
907 STORE_SIZE(count);
908 }
909 format++;
910 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000911 /* "s" or "z" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 char **p = va_arg(*p_va, char **);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200913 Py_ssize_t len;
914 sarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000915
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000916 if (c == 'z' && arg == Py_None)
917 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200919 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
920 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 return converterr(CONV_UNICODE,
922 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200923 *p = sarg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 }
925 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000926 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200928 if (*p != NULL && sarg != NULL && (Py_ssize_t) strlen(*p) != len)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return converterr(
Victor Stinner9d663d02011-10-13 23:25:03 +0200930 c == 'z' ? "str without null characters or None"
931 : "str without null characters",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 arg, msgbuf, bufsize);
933 }
934 break;
935 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000936
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000937 case 'u': /* raw unicode buffer (Py_UNICODE *) */
938 case 'Z': /* raw unicode buffer or None */
939 {
Brett Cannonb94767f2011-02-22 20:15:44 +0000940 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 if (*format == '#') { /* any buffer-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000943 /* "s#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +0000945
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000946 if (c == 'Z' && arg == Py_None) {
947 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 STORE_SIZE(0);
949 }
950 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200951 Py_ssize_t len;
952 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200953 if (*p == NULL)
954 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200955 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 }
Victor Stinner5216e6d2010-06-08 21:45:51 +0000957 else
958 return converterr("str or None", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 format++;
960 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000961 /* "s" or "Z" */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000962 if (c == 'Z' && arg == Py_None)
963 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000964 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200965 Py_ssize_t len;
966 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200967 if (*p == NULL)
968 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200969 if (Py_UNICODE_strlen(*p) != len)
Victor Stinner06e49dd2010-06-13 18:21:50 +0000970 return converterr(
Victor Stinner9d663d02011-10-13 23:25:03 +0200971 "str without null characters or None",
Victor Stinner06e49dd2010-06-13 18:21:50 +0000972 arg, msgbuf, bufsize);
973 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000974 return converterr(c == 'Z' ? "str or None" : "str",
975 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 }
977 break;
978 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 case 'e': {/* encoded string */
981 char **buffer;
982 const char *encoding;
983 PyObject *s;
984 int recode_strings;
985 Py_ssize_t size;
986 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 /* Get 'e' parameter: the encoding name */
989 encoding = (const char *)va_arg(*p_va, const char *);
990 if (encoding == NULL)
991 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 /* Get output buffer parameter:
994 's' (recode all objects via Unicode) or
995 't' (only recode non-string objects)
996 */
997 if (*format == 's')
998 recode_strings = 1;
999 else if (*format == 't')
1000 recode_strings = 0;
1001 else
1002 return converterr(
1003 "(unknown parser marker combination)",
1004 arg, msgbuf, bufsize);
1005 buffer = (char **)va_arg(*p_va, char **);
1006 format++;
1007 if (buffer == NULL)
1008 return converterr("(buffer is NULL)",
1009 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 /* Encode object */
1012 if (!recode_strings &&
1013 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1014 s = arg;
1015 Py_INCREF(s);
1016 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1017 return converterr("(AsCharBuffer failed)",
1018 arg, msgbuf, bufsize);
1019 }
1020 else {
1021 PyObject *u;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 /* Convert object to Unicode */
1024 u = PyUnicode_FromObject(arg);
1025 if (u == NULL)
1026 return converterr(
1027 "string or unicode or text buffer",
1028 arg, msgbuf, bufsize);
Guido van Rossum98297ee2007-11-06 21:34:58 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 /* Encode object; use default error handling */
1031 s = PyUnicode_AsEncodedString(u,
1032 encoding,
1033 NULL);
1034 Py_DECREF(u);
1035 if (s == NULL)
1036 return converterr("(encoding failed)",
1037 arg, msgbuf, bufsize);
1038 if (!PyBytes_Check(s)) {
1039 Py_DECREF(s);
1040 return converterr(
1041 "(encoder failed to return bytes)",
1042 arg, msgbuf, bufsize);
1043 }
1044 size = PyBytes_GET_SIZE(s);
1045 ptr = PyBytes_AS_STRING(s);
1046 if (ptr == NULL)
1047 ptr = "";
1048 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 /* Write output; output is guaranteed to be 0-terminated */
1051 if (*format == '#') {
1052 /* Using buffer length parameter '#':
1053
1054 - if *buffer is NULL, a new buffer of the
1055 needed size is allocated and the data
1056 copied into it; *buffer is updated to point
1057 to the new buffer; the caller is
1058 responsible for PyMem_Free()ing it after
1059 usage
1060
1061 - if *buffer is not NULL, the data is
1062 copied to *buffer; *buffer_len has to be
1063 set to the size of the buffer on input;
1064 buffer overflow is signalled with an error;
1065 buffer has to provide enough room for the
1066 encoded string plus the trailing 0-byte
1067
1068 - in both cases, *buffer_len is updated to
1069 the size of the buffer /excluding/ the
1070 trailing 0-byte
1071
1072 */
1073 FETCH_SIZE;
1074
1075 format++;
1076 if (q == NULL && q2 == NULL) {
1077 Py_DECREF(s);
1078 return converterr(
1079 "(buffer_len is NULL)",
1080 arg, msgbuf, bufsize);
1081 }
1082 if (*buffer == NULL) {
1083 *buffer = PyMem_NEW(char, size + 1);
1084 if (*buffer == NULL) {
1085 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001086 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001087 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001089 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 Py_DECREF(s);
1091 return converterr(
1092 "(cleanup problem)",
1093 arg, msgbuf, bufsize);
1094 }
1095 } else {
1096 if (size + 1 > BUFFER_LEN) {
1097 Py_DECREF(s);
1098 return converterr(
1099 "(buffer overflow)",
1100 arg, msgbuf, bufsize);
1101 }
1102 }
1103 memcpy(*buffer, ptr, size+1);
1104 STORE_SIZE(size);
1105 } else {
1106 /* Using a 0-terminated buffer:
1107
1108 - the encoded string has to be 0-terminated
1109 for this variant to work; if it is not, an
1110 error raised
1111
1112 - a new buffer of the needed size is
1113 allocated and the data copied into it;
1114 *buffer is updated to point to the new
1115 buffer; the caller is responsible for
1116 PyMem_Free()ing it after usage
1117
1118 */
1119 if ((Py_ssize_t)strlen(ptr) != size) {
1120 Py_DECREF(s);
1121 return converterr(
1122 "encoded string without NULL bytes",
1123 arg, msgbuf, bufsize);
1124 }
1125 *buffer = PyMem_NEW(char, size + 1);
1126 if (*buffer == NULL) {
1127 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001128 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001129 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001131 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 Py_DECREF(s);
1133 return converterr("(cleanup problem)",
1134 arg, msgbuf, bufsize);
1135 }
1136 memcpy(*buffer, ptr, size+1);
1137 }
1138 Py_DECREF(s);
1139 break;
1140 }
1141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 case 'S': { /* PyBytes object */
1143 PyObject **p = va_arg(*p_va, PyObject **);
1144 if (PyBytes_Check(arg))
1145 *p = arg;
1146 else
1147 return converterr("bytes", arg, msgbuf, bufsize);
1148 break;
1149 }
1150
1151 case 'Y': { /* PyByteArray object */
1152 PyObject **p = va_arg(*p_va, PyObject **);
1153 if (PyByteArray_Check(arg))
1154 *p = arg;
1155 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001156 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 break;
1158 }
1159
1160 case 'U': { /* PyUnicode object */
1161 PyObject **p = va_arg(*p_va, PyObject **);
1162 if (PyUnicode_Check(arg))
1163 *p = arg;
1164 else
1165 return converterr("str", arg, msgbuf, bufsize);
1166 break;
1167 }
1168
1169 case 'O': { /* object */
1170 PyTypeObject *type;
1171 PyObject **p;
1172 if (*format == '!') {
1173 type = va_arg(*p_va, PyTypeObject*);
1174 p = va_arg(*p_va, PyObject **);
1175 format++;
1176 if (PyType_IsSubtype(arg->ob_type, type))
1177 *p = arg;
1178 else
1179 return converterr(type->tp_name, arg, msgbuf, bufsize);
1180
1181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 else if (*format == '&') {
1183 typedef int (*converter)(PyObject *, void *);
1184 converter convert = va_arg(*p_va, converter);
1185 void *addr = va_arg(*p_va, void *);
1186 int res;
1187 format++;
1188 if (! (res = (*convert)(arg, addr)))
1189 return converterr("(unspecified)",
1190 arg, msgbuf, bufsize);
1191 if (res == Py_CLEANUP_SUPPORTED &&
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001192 addcleanup(addr, freelist, convert) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 return converterr("(cleanup problem)",
1194 arg, msgbuf, bufsize);
1195 }
1196 else {
1197 p = va_arg(*p_va, PyObject **);
1198 *p = arg;
1199 }
1200 break;
1201 }
1202
1203
Victor Stinner25e8ec42010-06-25 00:02:38 +00001204 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206
Victor Stinner25e8ec42010-06-25 00:02:38 +00001207 if (*format != '*')
1208 return converterr(
1209 "invalid use of 'w' format character",
1210 arg, msgbuf, bufsize);
1211 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212
Victor Stinner25e8ec42010-06-25 00:02:38 +00001213 /* Caller is interested in Py_buffer, and the object
1214 supports it directly. */
1215 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1216 PyErr_Clear();
1217 return converterr("read-write buffer", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 }
Victor Stinner8182b712010-07-28 00:40:58 +00001219 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1220 PyBuffer_Release((Py_buffer*)p);
1221 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1222 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001223 if (addcleanup(p, freelist, cleanup_buffer)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001224 return converterr(
1225 "(cleanup problem)",
1226 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 break;
1229 }
1230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 default:
1232 return converterr("impossible<bad format char>", arg, msgbuf, bufsize);
1233
1234 }
1235
1236 *p_format = format;
1237 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001238
1239#undef FETCH_SIZE
1240#undef STORE_SIZE
1241#undef BUFFER_LEN
1242#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001243}
Guido van Rossumaa354651996-08-19 19:32:04 +00001244
Martin v. Löwis18e16552006-02-15 17:27:45 +00001245static Py_ssize_t
Fred Drake563dfc22001-10-23 14:41:08 +00001246convertbuffer(PyObject *arg, void **p, char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001247{
Victor Stinner5cb62392010-06-06 20:27:51 +00001248 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 Py_ssize_t count;
1250 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 *errmsg = NULL;
1253 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001254 if (pb != NULL && pb->bf_releasebuffer != NULL) {
1255 *errmsg = "read-only pinned buffer";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 return -1;
1257 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001258
Victor Stinner8182b712010-07-28 00:40:58 +00001259 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 count = view.len;
1262 *p = view.buf;
1263 PyBuffer_Release(&view);
1264 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001265}
Guido van Rossumaa354651996-08-19 19:32:04 +00001266
Martin v. Löwis423be952008-08-13 15:53:07 +00001267static int
Neal Norwitz2f99b242008-08-24 05:48:10 +00001268getbuffer(PyObject *arg, Py_buffer *view, char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001269{
Victor Stinner8182b712010-07-28 00:40:58 +00001270 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 *errmsg = "bytes or buffer";
1272 return -1;
1273 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001274 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001275 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001276 *errmsg = "contiguous buffer";
1277 return -1;
1278 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001280}
1281
Guido van Rossumaa354651996-08-19 19:32:04 +00001282/* Support for keyword arguments donated by
1283 Geoff Philbrick <philbric@delphi.hks.com> */
1284
Tim Petersf8cd3e82001-10-27 04:26:57 +00001285/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001286int
1287PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 PyObject *keywords,
1289 const char *format,
1290 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 int retval;
1293 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 if ((args == NULL || !PyTuple_Check(args)) ||
1296 (keywords != NULL && !PyDict_Check(keywords)) ||
1297 format == NULL ||
1298 kwlist == NULL)
1299 {
1300 PyErr_BadInternalCall();
1301 return 0;
1302 }
Tim Peters45772cd2001-10-27 03:58:40 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 va_start(va, kwlist);
1305 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1306 va_end(va);
1307 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001308}
1309
1310int
1311_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 PyObject *keywords,
1313 const char *format,
1314 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 int retval;
1317 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 if ((args == NULL || !PyTuple_Check(args)) ||
1320 (keywords != NULL && !PyDict_Check(keywords)) ||
1321 format == NULL ||
1322 kwlist == NULL)
1323 {
1324 PyErr_BadInternalCall();
1325 return 0;
1326 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 va_start(va, kwlist);
1329 retval = vgetargskeywords(args, keywords, format,
1330 kwlist, &va, FLAG_SIZE_T);
1331 va_end(va);
1332 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001333}
1334
1335
Brett Cannon711e7d92004-07-10 22:20:32 +00001336int
1337PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001338 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001339 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001340 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 int retval;
1343 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 if ((args == NULL || !PyTuple_Check(args)) ||
1346 (keywords != NULL && !PyDict_Check(keywords)) ||
1347 format == NULL ||
1348 kwlist == NULL)
1349 {
1350 PyErr_BadInternalCall();
1351 return 0;
1352 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001353
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001354 Py_VA_COPY(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1357 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001358}
1359
1360int
1361_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 PyObject *keywords,
1363 const char *format,
1364 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 int retval;
1367 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if ((args == NULL || !PyTuple_Check(args)) ||
1370 (keywords != NULL && !PyDict_Check(keywords)) ||
1371 format == NULL ||
1372 kwlist == NULL)
1373 {
1374 PyErr_BadInternalCall();
1375 return 0;
1376 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001377
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001378 Py_VA_COPY(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 retval = vgetargskeywords(args, keywords, format,
1381 kwlist, &lva, FLAG_SIZE_T);
1382 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001383}
1384
Benjamin Petersonfb886362010-04-24 18:21:17 +00001385int
1386PyArg_ValidateKeywordArguments(PyObject *kwargs)
1387{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001388 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 PyErr_BadInternalCall();
1390 return 0;
1391 }
1392 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1393 PyErr_SetString(PyExc_TypeError,
1394 "keyword arguments must be strings");
1395 return 0;
1396 }
1397 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001398}
1399
Christian Heimes380f7f22008-02-28 11:19:05 +00001400#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001401
Guido van Rossumaa354651996-08-19 19:32:04 +00001402static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001403vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 char msgbuf[512];
1407 int levels[32];
1408 const char *fname, *msg, *custom_msg, *keyword;
1409 int min = INT_MAX;
1410 int i, len, nargs, nkeywords;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001411 PyObject *current_arg;
1412 freelist_t freelist = {0, NULL};
Tim Petersf4331c12001-10-27 00:17:34 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 assert(args != NULL && PyTuple_Check(args));
1415 assert(keywords == NULL || PyDict_Check(keywords));
1416 assert(format != NULL);
1417 assert(kwlist != NULL);
1418 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 /* grab the function name or custom error msg first (mutually exclusive) */
1421 fname = strchr(format, ':');
1422 if (fname) {
1423 fname++;
1424 custom_msg = NULL;
1425 }
1426 else {
1427 custom_msg = strchr(format,';');
1428 if (custom_msg)
1429 custom_msg++;
1430 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 /* scan kwlist and get greatest possible nbr of args */
1433 for (len=0; kwlist[len]; len++)
1434 continue;
Tim Petersf8cd3e82001-10-27 04:26:57 +00001435
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001436 freelist.entries = PyMem_New(freelistentry_t, len);
Benjamin Peterson7ed67272012-03-16 12:21:02 -05001437 if (freelist.entries == NULL) {
1438 PyErr_NoMemory();
1439 return 0;
1440 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 nargs = PyTuple_GET_SIZE(args);
1443 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1444 if (nargs + nkeywords > len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +01001445 PyErr_Format(PyExc_TypeError,
1446 "%s%s takes at most %d argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 (fname == NULL) ? "function" : fname,
1448 (fname == NULL) ? "" : "()",
1449 len,
1450 (len == 1) ? "" : "s",
1451 nargs + nkeywords);
1452 return 0;
1453 }
Tim Petersc2f01122001-10-27 07:25:06 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1456 for (i = 0; i < len; i++) {
1457 keyword = kwlist[i];
1458 if (*format == '|') {
1459 min = i;
1460 format++;
1461 }
1462 if (IS_END_OF_FORMAT(*format)) {
1463 PyErr_Format(PyExc_RuntimeError,
1464 "More keyword list entries (%d) than "
1465 "format specifiers (%d)", len, i);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001466 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 }
1468 current_arg = NULL;
1469 if (nkeywords) {
1470 current_arg = PyDict_GetItemString(keywords, keyword);
1471 }
1472 if (current_arg) {
1473 --nkeywords;
1474 if (i < nargs) {
1475 /* arg present in tuple and in dict */
1476 PyErr_Format(PyExc_TypeError,
1477 "Argument given by name ('%s') "
1478 "and position (%d)",
1479 keyword, i+1);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001480 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 }
1482 }
1483 else if (nkeywords && PyErr_Occurred())
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001484 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 else if (i < nargs)
1486 current_arg = PyTuple_GET_ITEM(args, i);
Christian Heimes380f7f22008-02-28 11:19:05 +00001487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 if (current_arg) {
1489 msg = convertitem(current_arg, &format, p_va, flags,
1490 levels, msgbuf, sizeof(msgbuf), &freelist);
1491 if (msg) {
1492 seterror(i+1, msg, levels, fname, custom_msg);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001493 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 }
1495 continue;
1496 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 if (i < min) {
1499 PyErr_Format(PyExc_TypeError, "Required argument "
1500 "'%s' (pos %d) not found",
1501 keyword, i+1);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001502 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 }
1504 /* current code reports success when all required args
1505 * fulfilled and no keyword args left, with no further
1506 * validation. XXX Maybe skip this in debug build ?
1507 */
1508 if (!nkeywords)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001509 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 /* We are into optional args, skip thru to any remaining
1512 * keyword args */
1513 msg = skipitem(&format, p_va, flags);
1514 if (msg) {
1515 PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg,
1516 format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001517 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 }
1519 }
Tim Petersb054be42001-10-27 05:07:41 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (!IS_END_OF_FORMAT(*format) && *format != '|') {
1522 PyErr_Format(PyExc_RuntimeError,
1523 "more argument specifiers than keyword list entries "
1524 "(remaining format:'%s')", format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001525 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 }
Tim Petersc2f01122001-10-27 07:25:06 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 /* make sure there are no extraneous keyword arguments */
1529 if (nkeywords > 0) {
1530 PyObject *key, *value;
1531 Py_ssize_t pos = 0;
1532 while (PyDict_Next(keywords, &pos, &key, &value)) {
1533 int match = 0;
1534 char *ks;
1535 if (!PyUnicode_Check(key)) {
1536 PyErr_SetString(PyExc_TypeError,
1537 "keywords must be strings");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001538 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 }
Victor Stinner93b55132010-05-19 00:54:06 +00001540 /* check that _PyUnicode_AsString() result is not NULL */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 ks = _PyUnicode_AsString(key);
Victor Stinner93b55132010-05-19 00:54:06 +00001542 if (ks != NULL) {
1543 for (i = 0; i < len; i++) {
1544 if (!strcmp(ks, kwlist[i])) {
1545 match = 1;
1546 break;
1547 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 }
1549 }
1550 if (!match) {
1551 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001552 "'%U' is an invalid keyword "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 "argument for this function",
Victor Stinner93b55132010-05-19 00:54:06 +00001554 key);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001555 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 }
1557 }
1558 }
1559
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001560 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001561}
1562
1563
1564static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001565skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 const char *format = *p_format;
1568 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 /* simple codes
1573 * The individual types (second arg of va_arg) are irrelevant */
Georg Brandl6dd14612005-09-14 19:29:53 +00001574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 case 'b': /* byte -- very short int */
1576 case 'B': /* byte as bitfield */
1577 case 'h': /* short int */
1578 case 'H': /* short int as bitfield */
1579 case 'i': /* int */
1580 case 'I': /* int sized bitfield */
1581 case 'l': /* long int */
1582 case 'k': /* long int sized bitfield */
Guido van Rossum3dbba6e1999-01-25 21:48:56 +00001583#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 case 'L': /* PY_LONG_LONG */
1585 case 'K': /* PY_LONG_LONG sized bitfield */
Guido van Rossum1a8791e1998-08-04 22:46:29 +00001586#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 case 'f': /* float */
1588 case 'd': /* double */
1589 case 'D': /* complex double */
1590 case 'c': /* char */
1591 case 'C': /* unicode char */
1592 {
1593 (void) va_arg(*p_va, void *);
1594 break;
1595 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 case 'n': /* Py_ssize_t */
1598 {
1599 (void) va_arg(*p_va, Py_ssize_t *);
1600 break;
1601 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00001604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 case 'e': /* string with encoding */
1606 {
1607 (void) va_arg(*p_va, const char *);
1608 if (!(*format == 's' || *format == 't'))
1609 /* after 'e', only 's' and 't' is allowed */
1610 goto err;
1611 format++;
1612 /* explicit fallthrough to string cases */
1613 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 case 's': /* string */
1616 case 'z': /* string or None */
1617 case 'y': /* bytes */
1618 case 'u': /* unicode string */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 case 'w': /* buffer, read-write */
1620 {
1621 (void) va_arg(*p_va, char **);
1622 if (*format == '#') {
1623 if (flags & FLAG_SIZE_T)
1624 (void) va_arg(*p_va, Py_ssize_t *);
1625 else
1626 (void) va_arg(*p_va, int *);
1627 format++;
1628 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
1629 format++;
1630 }
1631 break;
1632 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 /* object codes */
Georg Brandl6dd14612005-09-14 19:29:53 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 case 'S': /* string object */
1637 case 'Y': /* string object */
1638 case 'U': /* unicode string object */
1639 {
1640 (void) va_arg(*p_va, PyObject **);
1641 break;
1642 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 case 'O': /* object */
1645 {
1646 if (*format == '!') {
1647 format++;
1648 (void) va_arg(*p_va, PyTypeObject*);
1649 (void) va_arg(*p_va, PyObject **);
1650 }
1651 else if (*format == '&') {
1652 typedef int (*converter)(PyObject *, void *);
1653 (void) va_arg(*p_va, converter);
1654 (void) va_arg(*p_va, void *);
1655 format++;
1656 }
1657 else {
1658 (void) va_arg(*p_va, PyObject **);
1659 }
1660 break;
1661 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 case '(': /* bypass tuple, not handled at all previously */
1664 {
1665 char *msg;
1666 for (;;) {
1667 if (*format==')')
1668 break;
1669 if (IS_END_OF_FORMAT(*format))
1670 return "Unmatched left paren in format "
1671 "string";
1672 msg = skipitem(&format, p_va, flags);
1673 if (msg)
1674 return msg;
1675 }
1676 format++;
1677 break;
1678 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 case ')':
1681 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00001684err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00001686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 }
Georg Brandl6dd14612005-09-14 19:29:53 +00001688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 *p_format = format;
1690 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00001691}
Fred Drakee4616e62001-10-23 21:09:29 +00001692
1693
1694int
Martin v. Löwis76246742006-03-01 04:06:10 +00001695PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00001696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 Py_ssize_t i, l;
1698 PyObject **o;
1699 va_list vargs;
Fred Drakee4616e62001-10-23 21:09:29 +00001700
1701#ifdef HAVE_STDARG_PROTOTYPES
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 va_start(vargs, max);
Fred Drakee4616e62001-10-23 21:09:29 +00001703#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 va_start(vargs);
Fred Drakee4616e62001-10-23 21:09:29 +00001705#endif
1706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 assert(min >= 0);
1708 assert(min <= max);
1709 if (!PyTuple_Check(args)) {
1710 PyErr_SetString(PyExc_SystemError,
1711 "PyArg_UnpackTuple() argument list is not a tuple");
1712 return 0;
1713 }
1714 l = PyTuple_GET_SIZE(args);
1715 if (l < min) {
1716 if (name != NULL)
1717 PyErr_Format(
1718 PyExc_TypeError,
1719 "%s expected %s%zd arguments, got %zd",
1720 name, (min == max ? "" : "at least "), min, l);
1721 else
1722 PyErr_Format(
1723 PyExc_TypeError,
1724 "unpacked tuple should have %s%zd elements,"
1725 " but has %zd",
1726 (min == max ? "" : "at least "), min, l);
1727 va_end(vargs);
1728 return 0;
1729 }
1730 if (l > max) {
1731 if (name != NULL)
1732 PyErr_Format(
1733 PyExc_TypeError,
1734 "%s expected %s%zd arguments, got %zd",
1735 name, (min == max ? "" : "at most "), max, l);
1736 else
1737 PyErr_Format(
1738 PyExc_TypeError,
1739 "unpacked tuple should have %s%zd elements,"
1740 " but has %zd",
1741 (min == max ? "" : "at most "), max, l);
1742 va_end(vargs);
1743 return 0;
1744 }
1745 for (i = 0; i < l; i++) {
1746 o = va_arg(vargs, PyObject **);
1747 *o = PyTuple_GET_ITEM(args, i);
1748 }
1749 va_end(vargs);
1750 return 1;
Fred Drakee4616e62001-10-23 21:09:29 +00001751}
Georg Brandl02c42872005-08-26 06:42:30 +00001752
1753
1754/* For type constructors that don't take keyword args
1755 *
Guido van Rossum98297ee2007-11-06 21:34:58 +00001756 * Sets a TypeError and returns 0 if the kwds dict is
Thomas Wouters89f507f2006-12-13 04:49:30 +00001757 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00001758 */
1759int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001760_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00001761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 if (kw == NULL)
1763 return 1;
1764 if (!PyDict_CheckExact(kw)) {
1765 PyErr_BadInternalCall();
1766 return 0;
1767 }
1768 if (PyDict_Size(kw) == 0)
1769 return 1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
1772 funcname);
1773 return 0;
Georg Brandl02c42872005-08-26 06:42:30 +00001774}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001775#ifdef __cplusplus
1776};
1777#endif