blob: 008a4346fb8509a401b8623c23701ba512f8a77f [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
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030021int _PyArg_ParseTupleAndKeywordsFast(PyObject *, PyObject *,
22 struct _PyArg_Parser *, ...);
23int _PyArg_VaParseTupleAndKeywordsFast(PyObject *, PyObject *,
24 struct _PyArg_Parser *, va_list);
25
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000026#ifdef HAVE_DECLSPEC_DLL
27/* Export functions */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020028PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...);
29PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
31 const char *, char **, ...);
32PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020033PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
35 const char *, char **, va_list);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030036
37PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
38 struct _PyArg_Parser *, ...);
39PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
40 struct _PyArg_Parser *, va_list);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041#endif
42
Martin v. Löwis18e16552006-02-15 17:27:45 +000043#define FLAG_COMPAT 1
44#define FLAG_SIZE_T 2
45
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040046typedef int (*destr_t)(PyObject *, void *);
47
48
49/* Keep track of "objects" that have been allocated or initialized and
50 which will need to be deallocated or cleaned up somehow if overall
51 parsing fails.
52*/
53typedef struct {
54 void *item;
55 destr_t destructor;
56} freelistentry_t;
57
58typedef struct {
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040059 freelistentry_t *entries;
Antoine Pitrou7056cb22013-02-17 01:04:57 +010060 int first_available;
61 int entries_malloced;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040062} freelist_t;
63
Antoine Pitrou7056cb22013-02-17 01:04:57 +010064#define STATIC_FREELIST_ENTRIES 8
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000065
66/* Forward */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000067static int vgetargs1(PyObject *, const char *, va_list *, int);
Victor Stinner84bb1cf2013-05-17 00:12:04 +020068static void seterror(Py_ssize_t, const char *, int *, const char *, const char *);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020069static const char *convertitem(PyObject *, const char **, va_list *, int, int *,
70 char *, size_t, freelist_t *);
71static const char *converttuple(PyObject *, const char **, va_list *, int,
72 int *, char *, size_t, int, freelist_t *);
73static const char *convertsimple(PyObject *, const char **, va_list *, int,
74 char *, size_t, freelist_t *);
75static Py_ssize_t convertbuffer(PyObject *, void **p, const char **);
76static int getbuffer(PyObject *, Py_buffer *, const char**);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000077
Tim Petersdbd9ba62000-07-09 03:09:57 +000078static int vgetargskeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 const char *, char **, va_list *, int);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030080static int vgetargskeywordsfast(PyObject *, PyObject *,
81 struct _PyArg_Parser *, va_list *, int);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020082static const char *skipitem(const char **, va_list *, int);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000083
Fred Drake563dfc22001-10-23 14:41:08 +000084int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000085PyArg_Parse(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 int retval;
88 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 va_start(va, format);
91 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
92 va_end(va);
93 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +000094}
95
96int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020097_PyArg_Parse_SizeT(PyObject *args, const char *format, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +000098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 int retval;
100 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 va_start(va, format);
103 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
104 va_end(va);
105 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000106}
107
108
Fred Drake563dfc22001-10-23 14:41:08 +0000109int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000110PyArg_ParseTuple(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 int retval;
113 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 va_start(va, format);
116 retval = vgetargs1(args, format, &va, 0);
117 va_end(va);
118 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000119}
120
Martin v. Löwis18e16552006-02-15 17:27:45 +0000121int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200122_PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 int retval;
125 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 va_start(va, format);
128 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
129 va_end(va);
130 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000131}
132
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000133
134int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000135PyArg_VaParse(PyObject *args, const char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 va_list lva;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000138
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000139 Py_VA_COPY(lva, va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 return vgetargs1(args, format, &lva, 0);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000142}
143
Martin v. Löwis18e16552006-02-15 17:27:45 +0000144int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200145_PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000146{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000148
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000149 Py_VA_COPY(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 return vgetargs1(args, format, &lva, FLAG_SIZE_T);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000152}
153
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000154
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000155/* Handle cleanup of allocated memory in case of exception */
156
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400157static int
158cleanup_ptr(PyObject *self, void *ptr)
Antoine Pitrouf71995782008-08-29 18:37:05 +0000159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 if (ptr) {
161 PyMem_FREE(ptr);
162 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000164}
165
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000166static int
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400167cleanup_buffer(PyObject *self, void *ptr)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000168{
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400169 Py_buffer *buf = (Py_buffer *)ptr;
170 if (buf) {
171 PyBuffer_Release(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400173 return 0;
174}
175
176static int
177addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
178{
179 int index;
180
181 index = freelist->first_available;
182 freelist->first_available += 1;
183
184 freelist->entries[index].item = ptr;
185 freelist->entries[index].destructor = destructor;
186
187 return 0;
188}
189
190static int
191cleanreturn(int retval, freelist_t *freelist)
192{
193 int index;
194
195 if (retval == 0) {
196 /* A failure occurred, therefore execute all of the cleanup
197 functions.
198 */
199 for (index = 0; index < freelist->first_available; ++index) {
200 freelist->entries[index].destructor(NULL,
201 freelist->entries[index].item);
202 }
203 }
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100204 if (freelist->entries_malloced)
205 PyMem_FREE(freelist->entries);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 return retval;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000207}
208
209
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000210static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000211vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 char msgbuf[256];
214 int levels[32];
215 const char *fname = NULL;
216 const char *message = NULL;
217 int min = -1;
218 int max = 0;
219 int level = 0;
220 int endfmt = 0;
221 const char *formatsave = format;
222 Py_ssize_t i, len;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200223 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 int compat = flags & FLAG_COMPAT;
Benjamin Peterson40be9e52014-02-11 10:09:27 -0500225 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
226 freelist_t freelist;
227
228 freelist.entries = static_entries;
229 freelist.first_available = 0;
230 freelist.entries_malloced = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 assert(compat || (args != (PyObject*)NULL));
233 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 while (endfmt == 0) {
236 int c = *format++;
237 switch (c) {
238 case '(':
239 if (level == 0)
240 max++;
241 level++;
242 if (level >= 30)
243 Py_FatalError("too many tuple nesting levels "
244 "in argument format string");
245 break;
246 case ')':
247 if (level == 0)
248 Py_FatalError("excess ')' in getargs format");
249 else
250 level--;
251 break;
252 case '\0':
253 endfmt = 1;
254 break;
255 case ':':
256 fname = format;
257 endfmt = 1;
258 break;
259 case ';':
260 message = format;
261 endfmt = 1;
262 break;
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100263 case '|':
264 if (level == 0)
265 min = max;
266 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 default:
268 if (level == 0) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100269 if (Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (c != 'e') /* skip encoded */
271 max++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 }
273 break;
274 }
275 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 if (level != 0)
278 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 if (min < 0)
281 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000284
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100285 if (max > STATIC_FREELIST_ENTRIES) {
286 freelist.entries = PyMem_NEW(freelistentry_t, max);
287 if (freelist.entries == NULL) {
288 PyErr_NoMemory();
289 return 0;
290 }
291 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -0500292 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 if (compat) {
295 if (max == 0) {
296 if (args == NULL)
297 return 1;
Victor Stinner6ced7c42011-03-21 18:15:42 +0100298 PyErr_Format(PyExc_TypeError,
299 "%.200s%s takes no arguments",
300 fname==NULL ? "function" : fname,
301 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400302 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 }
304 else if (min == 1 && max == 1) {
305 if (args == NULL) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100306 PyErr_Format(PyExc_TypeError,
307 "%.200s%s takes at least one argument",
308 fname==NULL ? "function" : fname,
309 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400310 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 }
312 msg = convertitem(args, &format, p_va, flags, levels,
313 msgbuf, sizeof(msgbuf), &freelist);
314 if (msg == NULL)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400315 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 seterror(levels[0], msg, levels+1, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400317 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 }
319 else {
320 PyErr_SetString(PyExc_SystemError,
321 "old style getargs format uses new features");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400322 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 }
324 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 if (!PyTuple_Check(args)) {
327 PyErr_SetString(PyExc_SystemError,
328 "new style getargs format but argument is not a tuple");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400329 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 len = PyTuple_GET_SIZE(args);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 if (len < min || max < len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100335 if (message == NULL)
336 PyErr_Format(PyExc_TypeError,
337 "%.150s%s takes %s %d argument%s (%ld given)",
338 fname==NULL ? "function" : fname,
339 fname==NULL ? "" : "()",
340 min==max ? "exactly"
341 : len < min ? "at least" : "at most",
342 len < min ? min : max,
343 (len < min ? min : max) == 1 ? "" : "s",
344 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
345 else
346 PyErr_SetString(PyExc_TypeError, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400347 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 for (i = 0; i < len; i++) {
351 if (*format == '|')
352 format++;
353 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
354 flags, levels, msgbuf,
355 sizeof(msgbuf), &freelist);
356 if (msg) {
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200357 seterror(i+1, msg, levels, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400358 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 }
360 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000361
Antoine Pitrou4de74572013-02-09 23:11:27 +0100362 if (*format != '\0' && !Py_ISALPHA(Py_CHARMASK(*format)) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 *format != '(' &&
364 *format != '|' && *format != ':' && *format != ';') {
365 PyErr_Format(PyExc_SystemError,
366 "bad format string: %.200s", formatsave);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400367 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000369
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400370 return cleanreturn(1, &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000371}
372
373
374
375static void
Victor Stinner84bb1cf2013-05-17 00:12:04 +0200376seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000377 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 char buf[512];
380 int i;
381 char *p = buf;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 if (PyErr_Occurred())
384 return;
385 else if (message == NULL) {
386 if (fname != NULL) {
387 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
388 p += strlen(p);
389 }
390 if (iarg != 0) {
391 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Richard Oudkerk25296ce2013-05-18 17:35:19 +0100392 "argument %" PY_FORMAT_SIZE_T "d", iarg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 i = 0;
394 p += strlen(p);
Georg Brandl142ad662013-10-14 07:01:11 +0200395 while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 PyOS_snprintf(p, sizeof(buf) - (p - buf),
397 ", item %d", levels[i]-1);
398 p += strlen(p);
399 i++;
400 }
401 }
402 else {
403 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
404 p += strlen(p);
405 }
406 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
407 message = buf;
408 }
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +0200409 if (msg[0] == '(') {
410 PyErr_SetString(PyExc_SystemError, message);
411 }
412 else {
413 PyErr_SetString(PyExc_TypeError, message);
414 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000415}
416
417
418/* Convert a tuple argument.
419 On entry, *p_format points to the character _after_ the opening '('.
420 On successful exit, *p_format points to the closing ')'.
421 If successful:
422 *p_format and *p_va are updated,
423 *levels and *msgbuf are untouched,
424 and NULL is returned.
425 If the argument is invalid:
426 *p_format is unchanged,
427 *p_va is undefined,
428 *levels is a 0-terminated list of item numbers,
429 *msgbuf contains an error message, whose format is:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 "must be <typename1>, not <typename2>", where:
431 <typename1> is the name of the expected type, and
432 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000433 and msgbuf is returned.
434*/
435
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200436static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000437converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000438 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400439 freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 int level = 0;
442 int n = 0;
443 const char *format = *p_format;
444 int i;
Victor Stinner74387f52013-11-18 01:21:12 +0100445 Py_ssize_t len;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 for (;;) {
448 int c = *format++;
449 if (c == '(') {
450 if (level == 0)
451 n++;
452 level++;
453 }
454 else if (c == ')') {
455 if (level == 0)
456 break;
457 level--;
458 }
459 else if (c == ':' || c == ';' || c == '\0')
460 break;
Antoine Pitrou4de74572013-02-09 23:11:27 +0100461 else if (level == 0 && Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 n++;
463 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
466 levels[0] = 0;
467 PyOS_snprintf(msgbuf, bufsize,
468 toplevel ? "expected %d arguments, not %.50s" :
469 "must be %d-item sequence, not %.50s",
470 n,
471 arg == Py_None ? "None" : arg->ob_type->tp_name);
472 return msgbuf;
473 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000474
Victor Stinner74387f52013-11-18 01:21:12 +0100475 len = PySequence_Size(arg);
476 if (len != n) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 levels[0] = 0;
Victor Stinner74387f52013-11-18 01:21:12 +0100478 if (toplevel) {
479 PyOS_snprintf(msgbuf, bufsize,
480 "expected %d arguments, not %" PY_FORMAT_SIZE_T "d",
481 n, len);
482 }
483 else {
484 PyOS_snprintf(msgbuf, bufsize,
485 "must be sequence of length %d, "
486 "not %" PY_FORMAT_SIZE_T "d",
487 n, len);
488 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 return msgbuf;
490 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 format = *p_format;
493 for (i = 0; i < n; i++) {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200494 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 PyObject *item;
496 item = PySequence_GetItem(arg, i);
497 if (item == NULL) {
498 PyErr_Clear();
499 levels[0] = i+1;
500 levels[1] = 0;
501 strncpy(msgbuf, "is not retrievable", bufsize);
502 return msgbuf;
503 }
504 msg = convertitem(item, &format, p_va, flags, levels+1,
505 msgbuf, bufsize, freelist);
506 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
507 Py_XDECREF(item);
508 if (msg != NULL) {
509 levels[0] = i+1;
510 return msg;
511 }
512 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 *p_format = format;
515 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000516}
517
518
519/* Convert a single item. */
520
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200521static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000522convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400523 int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000524{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200525 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (*format == '(' /* ')' */) {
529 format++;
530 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
531 bufsize, 0, freelist);
532 if (msg == NULL)
533 format++;
534 }
535 else {
536 msg = convertsimple(arg, &format, p_va, flags,
537 msgbuf, bufsize, freelist);
538 if (msg != NULL)
539 levels[0] = 0;
540 }
541 if (msg == NULL)
542 *p_format = format;
543 return msg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000544}
545
546
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000547
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000548/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000549
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200550static const char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000551converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 assert(expected != NULL);
554 assert(arg != NULL);
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200555 if (expected[0] == '(') {
556 PyOS_snprintf(msgbuf, bufsize,
557 "%.100s", expected);
558 }
559 else {
560 PyOS_snprintf(msgbuf, bufsize,
561 "must be %.50s, not %.50s", expected,
562 arg == Py_None ? "None" : arg->ob_type->tp_name);
563 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000565}
566
567#define CONV_UNICODE "(unicode conversion error)"
568
Guido van Rossum45aecf42006-03-15 04:58:47 +0000569/* Explicitly check for float arguments when integers are expected.
570 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000571static int
572float_argument_error(PyObject *arg)
573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 if (PyFloat_Check(arg)) {
575 PyErr_SetString(PyExc_TypeError,
576 "integer argument expected, got float" );
577 return 1;
578 }
579 else
580 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000581}
582
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000583/* Convert a non-tuple argument. Return NULL if conversion went OK,
584 or a string with a message describing the failure. The message is
585 formatted as "must be <desired type>, not <actual type>".
586 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000587 Don't call if a tuple is expected.
588
589 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000590*/
591
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200592static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000593convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400594 char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 /* For # codes */
597#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
598 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
599 else q=va_arg(*p_va, int*);
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000600#define STORE_SIZE(s) \
601 if (flags & FLAG_SIZE_T) \
602 *q2=s; \
603 else { \
604 if (INT_MAX < s) { \
605 PyErr_SetString(PyExc_OverflowError, \
606 "size does not fit in an int"); \
607 return converterr("", arg, msgbuf, bufsize); \
608 } \
Victor Stinner9550ef32013-06-05 01:18:13 +0200609 *q = (int)s; \
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000610 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000611#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
Victor Stinner6ab8e822011-01-04 11:16:49 +0000612#define RETURN_ERR_OCCURRED return msgbuf
Martin v. Löwis18e16552006-02-15 17:27:45 +0000613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 const char *format = *p_format;
615 char c = *format++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200616 char *sarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 case 'b': { /* unsigned byte -- very short int */
621 char *p = va_arg(*p_va, char *);
622 long ival;
623 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000624 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 ival = PyLong_AsLong(arg);
626 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000627 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 else if (ival < 0) {
629 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000630 "unsigned byte integer is less than minimum");
631 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 }
633 else if (ival > UCHAR_MAX) {
634 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000635 "unsigned byte integer is greater than maximum");
636 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 }
638 else
639 *p = (unsigned char) ival;
640 break;
641 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 case 'B': {/* byte sized bitfield - both signed and unsigned
644 values allowed */
645 char *p = va_arg(*p_va, char *);
646 long ival;
647 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000648 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 ival = PyLong_AsUnsignedLongMask(arg);
650 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000651 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 else
653 *p = (unsigned char) ival;
654 break;
655 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 case 'h': {/* signed short int */
658 short *p = va_arg(*p_va, short *);
659 long ival;
660 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000661 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 ival = PyLong_AsLong(arg);
663 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000664 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 else if (ival < SHRT_MIN) {
666 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000667 "signed short integer is less than minimum");
668 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 }
670 else if (ival > SHRT_MAX) {
671 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000672 "signed short integer is greater than maximum");
673 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 }
675 else
676 *p = (short) ival;
677 break;
678 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 case 'H': { /* short int sized bitfield, both signed and
681 unsigned allowed */
682 unsigned short *p = va_arg(*p_va, unsigned short *);
683 long ival;
684 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000685 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 ival = PyLong_AsUnsignedLongMask(arg);
687 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000688 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 else
690 *p = (unsigned short) ival;
691 break;
692 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 case 'i': {/* signed int */
695 int *p = va_arg(*p_va, int *);
696 long ival;
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 ival = PyLong_AsLong(arg);
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 else if (ival > INT_MAX) {
703 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000704 "signed integer is greater than maximum");
705 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 }
707 else if (ival < INT_MIN) {
708 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000709 "signed integer is less than minimum");
710 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 }
712 else
713 *p = ival;
714 break;
715 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 case 'I': { /* int sized bitfield, both signed and
718 unsigned allowed */
719 unsigned int *p = va_arg(*p_va, unsigned int *);
720 unsigned int ival;
721 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000722 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
724 if (ival == (unsigned int)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000725 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 else
727 *p = ival;
728 break;
729 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 case 'n': /* Py_ssize_t */
732 {
733 PyObject *iobj;
734 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
735 Py_ssize_t ival = -1;
736 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000737 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 iobj = PyNumber_Index(arg);
739 if (iobj != NULL) {
740 ival = PyLong_AsSsize_t(iobj);
741 Py_DECREF(iobj);
742 }
743 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000744 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 *p = ival;
746 break;
747 }
748 case 'l': {/* long int */
749 long *p = va_arg(*p_va, long *);
750 long ival;
751 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000752 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 ival = PyLong_AsLong(arg);
754 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000755 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 else
757 *p = ival;
758 break;
759 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 case 'k': { /* long sized bitfield */
762 unsigned long *p = va_arg(*p_va, unsigned long *);
763 unsigned long ival;
764 if (PyLong_Check(arg))
765 ival = PyLong_AsUnsignedLongMask(arg);
766 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200767 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 *p = ival;
769 break;
770 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 case 'L': {/* PY_LONG_LONG */
773 PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * );
774 PY_LONG_LONG ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000775 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000776 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 ival = PyLong_AsLongLong(arg);
Mark Dickinsonc7301312010-06-10 16:05:10 +0000778 if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000779 RETURN_ERR_OCCURRED;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000780 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 break;
783 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 case 'K': { /* long long sized bitfield */
786 unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *);
787 unsigned PY_LONG_LONG ival;
788 if (PyLong_Check(arg))
789 ival = PyLong_AsUnsignedLongLongMask(arg);
790 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200791 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 *p = ival;
793 break;
794 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 case 'f': {/* float */
797 float *p = va_arg(*p_va, float *);
798 double dval = PyFloat_AsDouble(arg);
799 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000800 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 else
802 *p = (float) dval;
803 break;
804 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 case 'd': {/* double */
807 double *p = va_arg(*p_va, double *);
808 double dval = PyFloat_AsDouble(arg);
809 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000810 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 else
812 *p = dval;
813 break;
814 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 case 'D': {/* complex double */
817 Py_complex *p = va_arg(*p_va, Py_complex *);
818 Py_complex cval;
819 cval = PyComplex_AsCComplex(arg);
820 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000821 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 else
823 *p = cval;
824 break;
825 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 case 'c': {/* char */
828 char *p = va_arg(*p_va, char *);
829 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
830 *p = PyBytes_AS_STRING(arg)[0];
Eli Bendersky906b88f2011-07-29 07:05:08 +0300831 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
832 *p = PyByteArray_AS_STRING(arg)[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 else
834 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
835 break;
836 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 case 'C': {/* unicode char */
839 int *p = va_arg(*p_va, int *);
Victor Stinnere1335c72011-10-04 20:53:03 +0200840 int kind;
841 void *data;
842
843 if (!PyUnicode_Check(arg))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 return converterr("a unicode character", arg, msgbuf, bufsize);
Victor Stinnere1335c72011-10-04 20:53:03 +0200845
846 if (PyUnicode_READY(arg))
847 RETURN_ERR_OCCURRED;
848
849 if (PyUnicode_GET_LENGTH(arg) != 1)
850 return converterr("a unicode character", arg, msgbuf, bufsize);
851
852 kind = PyUnicode_KIND(arg);
853 data = PyUnicode_DATA(arg);
854 *p = PyUnicode_READ(kind, data, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 break;
856 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000857
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700858 case 'p': {/* boolean *p*redicate */
859 int *p = va_arg(*p_va, int *);
860 int val = PyObject_IsTrue(arg);
861 if (val > 0)
862 *p = 1;
863 else if (val == 0)
864 *p = 0;
865 else
866 RETURN_ERR_OCCURRED;
867 break;
868 }
869
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000870 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000872
Serhiy Storchakab757c832014-12-05 22:25:22 +0200873 case 'y': {/* any bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 void **p = (void **)va_arg(*p_va, char **);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200875 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 Py_ssize_t count;
877 if (*format == '*') {
878 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
879 return converterr(buf, arg, msgbuf, bufsize);
880 format++;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400881 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 return converterr(
883 "(cleanup problem)",
884 arg, msgbuf, bufsize);
885 }
886 break;
887 }
888 count = convertbuffer(arg, p, &buf);
889 if (count < 0)
890 return converterr(buf, arg, msgbuf, bufsize);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000891 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 FETCH_SIZE;
893 STORE_SIZE(count);
894 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000895 } else {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300896 if (strlen(*p) != (size_t)count) {
897 PyErr_SetString(PyExc_ValueError, "embedded null byte");
898 RETURN_ERR_OCCURRED;
899 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 }
901 break;
902 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000903
Serhiy Storchakab757c832014-12-05 22:25:22 +0200904 case 's': /* text string or bytes-like object */
905 case 'z': /* text string, bytes-like object or None */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000906 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000908 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000910
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000911 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
913 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200914 Py_ssize_t len;
915 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
916 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 return converterr(CONV_UNICODE,
918 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200919 PyBuffer_FillInfo(p, arg, sarg, len, 1, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 }
Serhiy Storchakab757c832014-12-05 22:25:22 +0200921 else { /* any bytes-like object */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200922 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 if (getbuffer(arg, p, &buf) < 0)
924 return converterr(buf, arg, msgbuf, bufsize);
925 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400926 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 return converterr(
928 "(cleanup problem)",
929 arg, msgbuf, bufsize);
930 }
931 format++;
Serhiy Storchakab757c832014-12-05 22:25:22 +0200932 } else if (*format == '#') { /* a string or read-only bytes-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000933 /* "s#" or "z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 void **p = (void **)va_arg(*p_va, char **);
935 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000936
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000937 if (c == 'z' && arg == Py_None) {
938 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 STORE_SIZE(0);
940 }
941 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200942 Py_ssize_t len;
943 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
944 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 return converterr(CONV_UNICODE,
946 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200947 *p = sarg;
948 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 }
Serhiy Storchakab757c832014-12-05 22:25:22 +0200950 else { /* read-only bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 /* XXX Really? */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200952 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 Py_ssize_t count = convertbuffer(arg, p, &buf);
954 if (count < 0)
955 return converterr(buf, arg, msgbuf, bufsize);
956 STORE_SIZE(count);
957 }
958 format++;
959 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000960 /* "s" or "z" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 char **p = va_arg(*p_va, char **);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200962 Py_ssize_t len;
963 sarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000964
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000965 if (c == 'z' && arg == Py_None)
966 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200968 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
969 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 return converterr(CONV_UNICODE,
971 arg, msgbuf, bufsize);
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300972 if (strlen(sarg) != (size_t)len) {
973 PyErr_SetString(PyExc_ValueError, "embedded null character");
974 RETURN_ERR_OCCURRED;
975 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200976 *p = sarg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 }
978 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000979 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 }
982 break;
983 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000984
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000985 case 'u': /* raw unicode buffer (Py_UNICODE *) */
986 case 'Z': /* raw unicode buffer or None */
987 {
Brett Cannonb94767f2011-02-22 20:15:44 +0000988 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
989
Serhiy Storchakab757c832014-12-05 22:25:22 +0200990 if (*format == '#') {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +0300991 /* "u#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +0000993
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000994 if (c == 'Z' && arg == Py_None) {
995 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 STORE_SIZE(0);
997 }
998 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +0200999 Py_ssize_t len;
1000 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001001 if (*p == NULL)
1002 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001003 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 }
Victor Stinner5216e6d2010-06-08 21:45:51 +00001005 else
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001006 return converterr(c == 'Z' ? "str or None" : "str",
1007 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 format++;
1009 } else {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001010 /* "u" or "Z" */
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001011 if (c == 'Z' && arg == Py_None)
1012 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +00001013 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001014 Py_ssize_t len;
1015 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001016 if (*p == NULL)
1017 RETURN_ERR_OCCURRED;
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001018 if (Py_UNICODE_strlen(*p) != (size_t)len) {
1019 PyErr_SetString(PyExc_ValueError, "embedded null character");
1020 RETURN_ERR_OCCURRED;
1021 }
Victor Stinner06e49dd2010-06-13 18:21:50 +00001022 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001023 return converterr(c == 'Z' ? "str or None" : "str",
1024 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 }
1026 break;
1027 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 case 'e': {/* encoded string */
1030 char **buffer;
1031 const char *encoding;
1032 PyObject *s;
1033 int recode_strings;
1034 Py_ssize_t size;
1035 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +00001036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 /* Get 'e' parameter: the encoding name */
1038 encoding = (const char *)va_arg(*p_va, const char *);
1039 if (encoding == NULL)
1040 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 /* Get output buffer parameter:
1043 's' (recode all objects via Unicode) or
1044 't' (only recode non-string objects)
1045 */
1046 if (*format == 's')
1047 recode_strings = 1;
1048 else if (*format == 't')
1049 recode_strings = 0;
1050 else
1051 return converterr(
1052 "(unknown parser marker combination)",
1053 arg, msgbuf, bufsize);
1054 buffer = (char **)va_arg(*p_va, char **);
1055 format++;
1056 if (buffer == NULL)
1057 return converterr("(buffer is NULL)",
1058 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 /* Encode object */
1061 if (!recode_strings &&
1062 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1063 s = arg;
1064 Py_INCREF(s);
1065 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1066 return converterr("(AsCharBuffer failed)",
1067 arg, msgbuf, bufsize);
1068 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001069 else if (PyUnicode_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 /* Encode object; use default error handling */
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001071 s = PyUnicode_AsEncodedString(arg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 encoding,
1073 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (s == NULL)
1075 return converterr("(encoding failed)",
1076 arg, msgbuf, bufsize);
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001077 assert(PyBytes_Check(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 size = PyBytes_GET_SIZE(s);
1079 ptr = PyBytes_AS_STRING(s);
1080 if (ptr == NULL)
1081 ptr = "";
1082 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001083 else {
1084 return converterr(
1085 recode_strings ? "str" : "str, bytes or bytearray",
1086 arg, msgbuf, bufsize);
1087 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 /* Write output; output is guaranteed to be 0-terminated */
1090 if (*format == '#') {
1091 /* Using buffer length parameter '#':
1092
1093 - if *buffer is NULL, a new buffer of the
1094 needed size is allocated and the data
1095 copied into it; *buffer is updated to point
1096 to the new buffer; the caller is
1097 responsible for PyMem_Free()ing it after
1098 usage
1099
1100 - if *buffer is not NULL, the data is
1101 copied to *buffer; *buffer_len has to be
1102 set to the size of the buffer on input;
1103 buffer overflow is signalled with an error;
1104 buffer has to provide enough room for the
1105 encoded string plus the trailing 0-byte
1106
1107 - in both cases, *buffer_len is updated to
1108 the size of the buffer /excluding/ the
1109 trailing 0-byte
1110
1111 */
1112 FETCH_SIZE;
1113
1114 format++;
1115 if (q == NULL && q2 == NULL) {
1116 Py_DECREF(s);
1117 return converterr(
1118 "(buffer_len is NULL)",
1119 arg, msgbuf, bufsize);
1120 }
1121 if (*buffer == NULL) {
1122 *buffer = PyMem_NEW(char, size + 1);
1123 if (*buffer == NULL) {
1124 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001125 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001126 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001128 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 Py_DECREF(s);
1130 return converterr(
1131 "(cleanup problem)",
1132 arg, msgbuf, bufsize);
1133 }
1134 } else {
1135 if (size + 1 > BUFFER_LEN) {
1136 Py_DECREF(s);
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +02001137 PyErr_Format(PyExc_ValueError,
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001138 "encoded string too long "
1139 "(%zd, maximum length %zd)",
1140 (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1141 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 }
1143 }
1144 memcpy(*buffer, ptr, size+1);
1145 STORE_SIZE(size);
1146 } else {
1147 /* Using a 0-terminated buffer:
1148
1149 - the encoded string has to be 0-terminated
1150 for this variant to work; if it is not, an
1151 error raised
1152
1153 - a new buffer of the needed size is
1154 allocated and the data copied into it;
1155 *buffer is updated to point to the new
1156 buffer; the caller is responsible for
1157 PyMem_Free()ing it after usage
1158
1159 */
1160 if ((Py_ssize_t)strlen(ptr) != size) {
1161 Py_DECREF(s);
1162 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001163 "encoded string without null bytes",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 arg, msgbuf, bufsize);
1165 }
1166 *buffer = PyMem_NEW(char, size + 1);
1167 if (*buffer == NULL) {
1168 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001169 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001170 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001172 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 Py_DECREF(s);
1174 return converterr("(cleanup problem)",
1175 arg, msgbuf, bufsize);
1176 }
1177 memcpy(*buffer, ptr, size+1);
1178 }
1179 Py_DECREF(s);
1180 break;
1181 }
1182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 case 'S': { /* PyBytes object */
1184 PyObject **p = va_arg(*p_va, PyObject **);
1185 if (PyBytes_Check(arg))
1186 *p = arg;
1187 else
1188 return converterr("bytes", arg, msgbuf, bufsize);
1189 break;
1190 }
1191
1192 case 'Y': { /* PyByteArray object */
1193 PyObject **p = va_arg(*p_va, PyObject **);
1194 if (PyByteArray_Check(arg))
1195 *p = arg;
1196 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001197 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 break;
1199 }
1200
1201 case 'U': { /* PyUnicode object */
1202 PyObject **p = va_arg(*p_va, PyObject **);
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001203 if (PyUnicode_Check(arg)) {
1204 if (PyUnicode_READY(arg) == -1)
1205 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 *p = arg;
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001207 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 else
1209 return converterr("str", arg, msgbuf, bufsize);
1210 break;
1211 }
1212
1213 case 'O': { /* object */
1214 PyTypeObject *type;
1215 PyObject **p;
1216 if (*format == '!') {
1217 type = va_arg(*p_va, PyTypeObject*);
1218 p = va_arg(*p_va, PyObject **);
1219 format++;
1220 if (PyType_IsSubtype(arg->ob_type, type))
1221 *p = arg;
1222 else
1223 return converterr(type->tp_name, arg, msgbuf, bufsize);
1224
1225 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 else if (*format == '&') {
1227 typedef int (*converter)(PyObject *, void *);
1228 converter convert = va_arg(*p_va, converter);
1229 void *addr = va_arg(*p_va, void *);
1230 int res;
1231 format++;
1232 if (! (res = (*convert)(arg, addr)))
1233 return converterr("(unspecified)",
1234 arg, msgbuf, bufsize);
1235 if (res == Py_CLEANUP_SUPPORTED &&
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001236 addcleanup(addr, freelist, convert) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 return converterr("(cleanup problem)",
1238 arg, msgbuf, bufsize);
1239 }
1240 else {
1241 p = va_arg(*p_va, PyObject **);
1242 *p = arg;
1243 }
1244 break;
1245 }
1246
1247
Victor Stinner25e8ec42010-06-25 00:02:38 +00001248 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250
Victor Stinner25e8ec42010-06-25 00:02:38 +00001251 if (*format != '*')
1252 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001253 "(invalid use of 'w' format character)",
Victor Stinner25e8ec42010-06-25 00:02:38 +00001254 arg, msgbuf, bufsize);
1255 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256
Victor Stinner25e8ec42010-06-25 00:02:38 +00001257 /* Caller is interested in Py_buffer, and the object
1258 supports it directly. */
1259 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1260 PyErr_Clear();
R David Murray861470c2014-10-05 11:47:01 -04001261 return converterr("read-write bytes-like object",
1262 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 }
Victor Stinner8182b712010-07-28 00:40:58 +00001264 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1265 PyBuffer_Release((Py_buffer*)p);
1266 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1267 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001268 if (addcleanup(p, freelist, cleanup_buffer)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001269 return converterr(
1270 "(cleanup problem)",
1271 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 break;
1274 }
1275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 default:
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001277 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278
1279 }
1280
1281 *p_format = format;
1282 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001283
1284#undef FETCH_SIZE
1285#undef STORE_SIZE
1286#undef BUFFER_LEN
1287#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001288}
Guido van Rossumaa354651996-08-19 19:32:04 +00001289
Martin v. Löwis18e16552006-02-15 17:27:45 +00001290static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001291convertbuffer(PyObject *arg, void **p, const char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001292{
Victor Stinner5cb62392010-06-06 20:27:51 +00001293 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 Py_ssize_t count;
1295 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 *errmsg = NULL;
1298 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001299 if (pb != NULL && pb->bf_releasebuffer != NULL) {
R David Murray861470c2014-10-05 11:47:01 -04001300 *errmsg = "read-only bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 return -1;
1302 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001303
Victor Stinner8182b712010-07-28 00:40:58 +00001304 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 count = view.len;
1307 *p = view.buf;
1308 PyBuffer_Release(&view);
1309 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001310}
Guido van Rossumaa354651996-08-19 19:32:04 +00001311
Martin v. Löwis423be952008-08-13 15:53:07 +00001312static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001313getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001314{
Victor Stinner8182b712010-07-28 00:40:58 +00001315 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
R David Murray861470c2014-10-05 11:47:01 -04001316 *errmsg = "bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 return -1;
1318 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001319 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001320 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001321 *errmsg = "contiguous buffer";
1322 return -1;
1323 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001325}
1326
Guido van Rossumaa354651996-08-19 19:32:04 +00001327/* Support for keyword arguments donated by
1328 Geoff Philbrick <philbric@delphi.hks.com> */
1329
Tim Petersf8cd3e82001-10-27 04:26:57 +00001330/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001331int
1332PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 PyObject *keywords,
1334 const char *format,
1335 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 int retval;
1338 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if ((args == NULL || !PyTuple_Check(args)) ||
1341 (keywords != NULL && !PyDict_Check(keywords)) ||
1342 format == NULL ||
1343 kwlist == NULL)
1344 {
1345 PyErr_BadInternalCall();
1346 return 0;
1347 }
Tim Peters45772cd2001-10-27 03:58:40 +00001348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 va_start(va, kwlist);
1350 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1351 va_end(va);
1352 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001353}
1354
1355int
1356_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 PyObject *keywords,
1358 const char *format,
1359 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 int retval;
1362 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 if ((args == NULL || !PyTuple_Check(args)) ||
1365 (keywords != NULL && !PyDict_Check(keywords)) ||
1366 format == NULL ||
1367 kwlist == NULL)
1368 {
1369 PyErr_BadInternalCall();
1370 return 0;
1371 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 va_start(va, kwlist);
1374 retval = vgetargskeywords(args, keywords, format,
1375 kwlist, &va, FLAG_SIZE_T);
1376 va_end(va);
1377 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001378}
1379
1380
Brett Cannon711e7d92004-07-10 22:20:32 +00001381int
1382PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001383 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001384 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001385 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 int retval;
1388 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 if ((args == NULL || !PyTuple_Check(args)) ||
1391 (keywords != NULL && !PyDict_Check(keywords)) ||
1392 format == NULL ||
1393 kwlist == NULL)
1394 {
1395 PyErr_BadInternalCall();
1396 return 0;
1397 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001398
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001399 Py_VA_COPY(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
1402 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001403}
1404
1405int
1406_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 PyObject *keywords,
1408 const char *format,
1409 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 int retval;
1412 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 if ((args == NULL || !PyTuple_Check(args)) ||
1415 (keywords != NULL && !PyDict_Check(keywords)) ||
1416 format == NULL ||
1417 kwlist == NULL)
1418 {
1419 PyErr_BadInternalCall();
1420 return 0;
1421 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001422
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001423 Py_VA_COPY(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 retval = vgetargskeywords(args, keywords, format,
1426 kwlist, &lva, FLAG_SIZE_T);
1427 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001428}
1429
Benjamin Petersonfb886362010-04-24 18:21:17 +00001430int
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001431_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1432 struct _PyArg_Parser *parser, ...)
1433{
1434 int retval;
1435 va_list va;
1436
1437 if ((args == NULL || !PyTuple_Check(args)) ||
1438 (keywords != NULL && !PyDict_Check(keywords)) ||
1439 parser == NULL)
1440 {
1441 PyErr_BadInternalCall();
1442 return 0;
1443 }
1444
1445 va_start(va, parser);
1446 retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1447 va_end(va);
1448 return retval;
1449}
1450
1451int
1452_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1453 struct _PyArg_Parser *parser, ...)
1454{
1455 int retval;
1456 va_list va;
1457
1458 if ((args == NULL || !PyTuple_Check(args)) ||
1459 (keywords != NULL && !PyDict_Check(keywords)) ||
1460 parser == NULL)
1461 {
1462 PyErr_BadInternalCall();
1463 return 0;
1464 }
1465
1466 va_start(va, parser);
1467 retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
1468 va_end(va);
1469 return retval;
1470}
1471
1472
1473int
1474_PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1475 struct _PyArg_Parser *parser, va_list va)
1476{
1477 int retval;
1478 va_list lva;
1479
1480 if ((args == NULL || !PyTuple_Check(args)) ||
1481 (keywords != NULL && !PyDict_Check(keywords)) ||
1482 parser == NULL)
1483 {
1484 PyErr_BadInternalCall();
1485 return 0;
1486 }
1487
1488 Py_VA_COPY(lva, va);
1489
1490 retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
1491 return retval;
1492}
1493
1494int
1495_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1496 struct _PyArg_Parser *parser, va_list va)
1497{
1498 int retval;
1499 va_list lva;
1500
1501 if ((args == NULL || !PyTuple_Check(args)) ||
1502 (keywords != NULL && !PyDict_Check(keywords)) ||
1503 parser == NULL)
1504 {
1505 PyErr_BadInternalCall();
1506 return 0;
1507 }
1508
1509 Py_VA_COPY(lva, va);
1510
1511 retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
1512 return retval;
1513}
1514
1515int
Benjamin Petersonfb886362010-04-24 18:21:17 +00001516PyArg_ValidateKeywordArguments(PyObject *kwargs)
1517{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001518 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 PyErr_BadInternalCall();
1520 return 0;
1521 }
1522 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1523 PyErr_SetString(PyExc_TypeError,
1524 "keyword arguments must be strings");
1525 return 0;
1526 }
1527 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001528}
1529
Christian Heimes380f7f22008-02-28 11:19:05 +00001530#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001531
Guido van Rossumaa354651996-08-19 19:32:04 +00001532static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001533vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 char msgbuf[512];
1537 int levels[32];
1538 const char *fname, *msg, *custom_msg, *keyword;
1539 int min = INT_MAX;
Larry Hastings83a9f482012-03-20 20:06:16 +00001540 int max = INT_MAX;
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001541 int i, pos, len;
1542 int skip = 0;
Victor Stinner74387f52013-11-18 01:21:12 +01001543 Py_ssize_t nargs, nkeywords;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001544 PyObject *current_arg;
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001545 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
Benjamin Peterson40be9e52014-02-11 10:09:27 -05001546 freelist_t freelist;
1547
1548 freelist.entries = static_entries;
1549 freelist.first_available = 0;
1550 freelist.entries_malloced = 0;
Tim Petersf4331c12001-10-27 00:17:34 +00001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 assert(args != NULL && PyTuple_Check(args));
1553 assert(keywords == NULL || PyDict_Check(keywords));
1554 assert(format != NULL);
1555 assert(kwlist != NULL);
1556 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 /* grab the function name or custom error msg first (mutually exclusive) */
1559 fname = strchr(format, ':');
1560 if (fname) {
1561 fname++;
1562 custom_msg = NULL;
1563 }
1564 else {
1565 custom_msg = strchr(format,';');
1566 if (custom_msg)
1567 custom_msg++;
1568 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001569
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001570 /* scan kwlist and count the number of positional-only parameters */
1571 for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1572 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 /* scan kwlist and get greatest possible nbr of args */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001574 for (len = pos; kwlist[len]; len++) {
1575 if (!*kwlist[len]) {
1576 PyErr_SetString(PyExc_SystemError,
1577 "Empty keyword parameter name");
1578 return cleanreturn(0, &freelist);
1579 }
1580 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001581
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001582 if (len > STATIC_FREELIST_ENTRIES) {
1583 freelist.entries = PyMem_NEW(freelistentry_t, len);
1584 if (freelist.entries == NULL) {
1585 PyErr_NoMemory();
1586 return 0;
1587 }
1588 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -05001589 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 nargs = PyTuple_GET_SIZE(args);
1592 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1593 if (nargs + nkeywords > len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +01001594 PyErr_Format(PyExc_TypeError,
Victor Stinnercb29ec52013-11-18 02:05:31 +01001595 "%s%s takes at most %d argument%s (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 (fname == NULL) ? "function" : fname,
1597 (fname == NULL) ? "" : "()",
1598 len,
1599 (len == 1) ? "" : "s",
1600 nargs + nkeywords);
Benjamin Peterson01feaec2012-03-16 13:25:58 -05001601 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 }
Tim Petersc2f01122001-10-27 07:25:06 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1605 for (i = 0; i < len; i++) {
1606 keyword = kwlist[i];
1607 if (*format == '|') {
Larry Hastings83a9f482012-03-20 20:06:16 +00001608 if (min != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001609 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001610 "Invalid format string (| specified twice)");
1611 return cleanreturn(0, &freelist);
1612 }
1613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 min = i;
1615 format++;
Larry Hastings83a9f482012-03-20 20:06:16 +00001616
1617 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001618 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001619 "Invalid format string ($ before |)");
1620 return cleanreturn(0, &freelist);
1621 }
1622 }
1623 if (*format == '$') {
1624 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001625 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001626 "Invalid format string ($ specified twice)");
1627 return cleanreturn(0, &freelist);
1628 }
1629
1630 max = i;
1631 format++;
1632
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001633 if (max < pos) {
1634 PyErr_SetString(PyExc_SystemError,
1635 "Empty parameter name after $");
1636 return cleanreturn(0, &freelist);
1637 }
1638 if (skip) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001639 /* Now we know the minimal and the maximal numbers of
1640 * positional arguments and can raise an exception with
1641 * informative message (see below). */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001642 break;
1643 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001644 if (max < nargs) {
1645 PyErr_Format(PyExc_TypeError,
1646 "Function takes %s %d positional arguments"
1647 " (%d given)",
1648 (min != INT_MAX) ? "at most" : "exactly",
1649 max, nargs);
1650 return cleanreturn(0, &freelist);
1651 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 }
1653 if (IS_END_OF_FORMAT(*format)) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001654 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 "More keyword list entries (%d) than "
1656 "format specifiers (%d)", len, i);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001657 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001659 if (!skip) {
1660 current_arg = NULL;
1661 if (nkeywords && i >= pos) {
1662 current_arg = PyDict_GetItemString(keywords, keyword);
1663 if (!current_arg && PyErr_Occurred()) {
1664 return cleanreturn(0, &freelist);
1665 }
1666 }
1667 if (current_arg) {
1668 --nkeywords;
1669 if (i < nargs) {
1670 /* arg present in tuple and in dict */
1671 PyErr_Format(PyExc_TypeError,
1672 "Argument given by name ('%s') "
1673 "and position (%d)",
1674 keyword, i+1);
1675 return cleanreturn(0, &freelist);
1676 }
1677 }
1678 else if (i < nargs)
1679 current_arg = PyTuple_GET_ITEM(args, i);
1680
1681 if (current_arg) {
1682 msg = convertitem(current_arg, &format, p_va, flags,
1683 levels, msgbuf, sizeof(msgbuf), &freelist);
1684 if (msg) {
1685 seterror(i+1, msg, levels, fname, custom_msg);
1686 return cleanreturn(0, &freelist);
1687 }
1688 continue;
1689 }
1690
1691 if (i < min) {
1692 if (i < pos) {
1693 assert (min == INT_MAX);
1694 assert (max == INT_MAX);
1695 skip = 1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001696 /* At that moment we still don't know the minimal and
1697 * the maximal numbers of positional arguments. Raising
1698 * an exception is deferred until we encounter | and $
1699 * or the end of the format. */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001700 }
1701 else {
1702 PyErr_Format(PyExc_TypeError, "Required argument "
1703 "'%s' (pos %d) not found",
1704 keyword, i+1);
1705 return cleanreturn(0, &freelist);
1706 }
1707 }
1708 /* current code reports success when all required args
1709 * fulfilled and no keyword args left, with no further
1710 * validation. XXX Maybe skip this in debug build ?
1711 */
1712 if (!nkeywords && !skip) {
1713 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 }
1715 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 /* We are into optional args, skip thru to any remaining
1718 * keyword args */
1719 msg = skipitem(&format, p_va, flags);
1720 if (msg) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001721 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001723 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 }
1725 }
Tim Petersb054be42001-10-27 05:07:41 +00001726
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001727 if (skip) {
1728 PyErr_Format(PyExc_TypeError,
1729 "Function takes %s %d positional arguments"
1730 " (%d given)",
1731 (Py_MIN(pos, min) < i) ? "at least" : "exactly",
1732 Py_MIN(pos, min), nargs);
1733 return cleanreturn(0, &freelist);
1734 }
1735
Larry Hastings83a9f482012-03-20 20:06:16 +00001736 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001737 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 "more argument specifiers than keyword list entries "
1739 "(remaining format:'%s')", format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001740 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 }
Tim Petersc2f01122001-10-27 07:25:06 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 /* make sure there are no extraneous keyword arguments */
1744 if (nkeywords > 0) {
1745 PyObject *key, *value;
1746 Py_ssize_t pos = 0;
1747 while (PyDict_Next(keywords, &pos, &key, &value)) {
1748 int match = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 if (!PyUnicode_Check(key)) {
1750 PyErr_SetString(PyExc_TypeError,
1751 "keywords must be strings");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001752 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 }
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001754 for (i = 0; i < len; i++) {
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001755 if (*kwlist[i] && !PyUnicode_CompareWithASCIIString(key, kwlist[i])) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001756 match = 1;
1757 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 }
1759 }
1760 if (!match) {
1761 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001762 "'%U' is an invalid keyword "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 "argument for this function",
Victor Stinner93b55132010-05-19 00:54:06 +00001764 key);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001765 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 }
1767 }
1768 }
1769
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001770 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001771}
1772
1773
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001774/* List of static parsers. */
1775static struct _PyArg_Parser *static_arg_parsers = NULL;
1776
1777static int
1778parser_init(struct _PyArg_Parser *parser)
1779{
1780 const char * const *keywords;
1781 const char *format, *msg;
1782 int i, len, min, max, nkw;
1783 PyObject *kwtuple;
1784
1785 assert(parser->format != NULL);
1786 assert(parser->keywords != NULL);
1787 if (parser->kwtuple != NULL) {
1788 return 1;
1789 }
1790
1791 /* grab the function name or custom error msg first (mutually exclusive) */
1792 parser->fname = strchr(parser->format, ':');
1793 if (parser->fname) {
1794 parser->fname++;
1795 parser->custom_msg = NULL;
1796 }
1797 else {
1798 parser->custom_msg = strchr(parser->format,';');
1799 if (parser->custom_msg)
1800 parser->custom_msg++;
1801 }
1802
1803 keywords = parser->keywords;
1804 /* scan keywords and count the number of positional-only parameters */
1805 for (i = 0; keywords[i] && !*keywords[i]; i++) {
1806 }
1807 parser->pos = i;
1808 /* scan keywords and get greatest possible nbr of args */
1809 for (; keywords[i]; i++) {
1810 if (!*keywords[i]) {
1811 PyErr_SetString(PyExc_SystemError,
1812 "Empty keyword parameter name");
1813 return 0;
1814 }
1815 }
1816 len = i;
1817
1818 min = max = INT_MAX;
1819 format = parser->format;
1820 for (i = 0; i < len; i++) {
1821 if (*format == '|') {
1822 if (min != INT_MAX) {
1823 PyErr_SetString(PyExc_SystemError,
1824 "Invalid format string (| specified twice)");
1825 return 0;
1826 }
1827 if (max != INT_MAX) {
1828 PyErr_SetString(PyExc_SystemError,
1829 "Invalid format string ($ before |)");
1830 return 0;
1831 }
1832 min = i;
1833 format++;
1834 }
1835 if (*format == '$') {
1836 if (max != INT_MAX) {
1837 PyErr_SetString(PyExc_SystemError,
1838 "Invalid format string ($ specified twice)");
1839 return 0;
1840 }
1841 if (i < parser->pos) {
1842 PyErr_SetString(PyExc_SystemError,
1843 "Empty parameter name after $");
1844 return 0;
1845 }
1846 max = i;
1847 format++;
1848 }
1849 if (IS_END_OF_FORMAT(*format)) {
1850 PyErr_Format(PyExc_SystemError,
1851 "More keyword list entries (%d) than "
1852 "format specifiers (%d)", len, i);
1853 return 0;
1854 }
1855
1856 msg = skipitem(&format, NULL, 0);
1857 if (msg) {
1858 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1859 format);
1860 return 0;
1861 }
1862 }
1863 parser->min = Py_MIN(min, len);
1864 parser->max = Py_MIN(max, len);
1865
1866 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1867 PyErr_Format(PyExc_SystemError,
1868 "more argument specifiers than keyword list entries "
1869 "(remaining format:'%s')", format);
1870 return 0;
1871 }
1872
1873 nkw = len - parser->pos;
1874 kwtuple = PyTuple_New(nkw);
1875 if (kwtuple == NULL) {
1876 return 0;
1877 }
1878 keywords = parser->keywords + parser->pos;
1879 for (i = 0; i < nkw; i++) {
1880 PyObject *str = PyUnicode_FromString(keywords[i]);
1881 if (str == NULL) {
1882 Py_DECREF(kwtuple);
1883 return 0;
1884 }
1885 PyUnicode_InternInPlace(&str);
1886 PyTuple_SET_ITEM(kwtuple, i, str);
1887 }
1888 parser->kwtuple = kwtuple;
1889
1890 assert(parser->next == NULL);
1891 parser->next = static_arg_parsers;
1892 static_arg_parsers = parser;
1893 return 1;
1894}
1895
1896static void
1897parser_clear(struct _PyArg_Parser *parser)
1898{
1899 Py_CLEAR(parser->kwtuple);
1900}
1901
1902static int
1903vgetargskeywordsfast(PyObject *args, PyObject *keywords,
1904 struct _PyArg_Parser *parser,
1905 va_list *p_va, int flags)
1906{
1907 PyObject *kwtuple;
1908 char msgbuf[512];
1909 int levels[32];
1910 const char *format;
1911 const char *msg;
1912 PyObject *keyword;
1913 int i, pos, len;
1914 Py_ssize_t nargs, nkeywords;
1915 PyObject *current_arg;
1916 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
1917 freelist_t freelist;
1918
1919 freelist.entries = static_entries;
1920 freelist.first_available = 0;
1921 freelist.entries_malloced = 0;
1922
1923 assert(args != NULL && PyTuple_Check(args));
1924 assert(keywords == NULL || PyDict_Check(keywords));
1925 assert(parser != NULL);
1926 assert(p_va != NULL);
1927
1928 if (!parser_init(parser)) {
1929 return 0;
1930 }
1931
1932 kwtuple = parser->kwtuple;
1933 pos = parser->pos;
1934 len = pos + PyTuple_GET_SIZE(kwtuple);
1935
1936 if (len > STATIC_FREELIST_ENTRIES) {
1937 freelist.entries = PyMem_NEW(freelistentry_t, len);
1938 if (freelist.entries == NULL) {
1939 PyErr_NoMemory();
1940 return 0;
1941 }
1942 freelist.entries_malloced = 1;
1943 }
1944
1945 nargs = PyTuple_GET_SIZE(args);
1946 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1947 if (nargs + nkeywords > len) {
1948 PyErr_Format(PyExc_TypeError,
1949 "%s%s takes at most %d argument%s (%zd given)",
1950 (parser->fname == NULL) ? "function" : parser->fname,
1951 (parser->fname == NULL) ? "" : "()",
1952 len,
1953 (len == 1) ? "" : "s",
1954 nargs + nkeywords);
1955 return cleanreturn(0, &freelist);
1956 }
1957 if (parser->max < nargs) {
1958 PyErr_Format(PyExc_TypeError,
1959 "Function takes %s %d positional arguments (%d given)",
1960 (parser->min != INT_MAX) ? "at most" : "exactly",
1961 parser->max, nargs);
1962 return cleanreturn(0, &freelist);
1963 }
1964
1965 format = parser->format;
1966 /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
1967 for (i = 0; i < len; i++) {
1968 keyword = (i >= pos) ? PyTuple_GET_ITEM(kwtuple, i - pos) : NULL;
1969 if (*format == '|') {
1970 format++;
1971 }
1972 if (*format == '$') {
1973 format++;
1974 }
1975 assert(!IS_END_OF_FORMAT(*format));
1976
1977 current_arg = NULL;
1978 if (nkeywords && i >= pos) {
1979 current_arg = PyDict_GetItem(keywords, keyword);
1980 if (!current_arg && PyErr_Occurred()) {
1981 return cleanreturn(0, &freelist);
1982 }
1983 }
1984 if (current_arg) {
1985 --nkeywords;
1986 if (i < nargs) {
1987 /* arg present in tuple and in dict */
1988 PyErr_Format(PyExc_TypeError,
1989 "Argument given by name ('%U') "
1990 "and position (%d)",
1991 keyword, i+1);
1992 return cleanreturn(0, &freelist);
1993 }
1994 }
1995 else if (i < nargs)
1996 current_arg = PyTuple_GET_ITEM(args, i);
1997
1998 if (current_arg) {
1999 msg = convertitem(current_arg, &format, p_va, flags,
2000 levels, msgbuf, sizeof(msgbuf), &freelist);
2001 if (msg) {
2002 seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2003 return cleanreturn(0, &freelist);
2004 }
2005 continue;
2006 }
2007
2008 if (i < parser->min) {
2009 /* Less arguments than required */
2010 if (i < pos) {
2011 PyErr_Format(PyExc_TypeError,
2012 "Function takes %s %d positional arguments"
2013 " (%d given)",
2014 (Py_MIN(pos, parser->min) < parser->max) ? "at least" : "exactly",
2015 Py_MIN(pos, parser->min), nargs);
2016 }
2017 else {
2018 PyErr_Format(PyExc_TypeError, "Required argument "
2019 "'%U' (pos %d) not found",
2020 keyword, i+1);
2021 }
2022 return cleanreturn(0, &freelist);
2023 }
2024 /* current code reports success when all required args
2025 * fulfilled and no keyword args left, with no further
2026 * validation. XXX Maybe skip this in debug build ?
2027 */
2028 if (!nkeywords) {
2029 return cleanreturn(1, &freelist);
2030 }
2031
2032 /* We are into optional args, skip thru to any remaining
2033 * keyword args */
2034 msg = skipitem(&format, p_va, flags);
2035 assert(msg == NULL);
2036 }
2037
2038 assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2039
2040 /* make sure there are no extraneous keyword arguments */
2041 if (nkeywords > 0) {
2042 PyObject *key, *value;
2043 Py_ssize_t pos = 0;
2044 while (PyDict_Next(keywords, &pos, &key, &value)) {
2045 int match;
2046 if (!PyUnicode_Check(key)) {
2047 PyErr_SetString(PyExc_TypeError,
2048 "keywords must be strings");
2049 return cleanreturn(0, &freelist);
2050 }
2051 match = PySequence_Contains(kwtuple, key);
2052 if (match <= 0) {
2053 if (!match) {
2054 PyErr_Format(PyExc_TypeError,
2055 "'%U' is an invalid keyword "
2056 "argument for this function",
2057 key);
2058 }
2059 return cleanreturn(0, &freelist);
2060 }
2061 }
2062 }
2063
2064 return cleanreturn(1, &freelist);
2065}
2066
2067
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002068static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002069skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00002070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 const char *format = *p_format;
2072 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00002075
Larry Hastingsa3479012012-05-08 23:52:03 -07002076 /*
2077 * codes that take a single data pointer as an argument
2078 * (the type of the pointer is irrelevant)
2079 */
Georg Brandl6dd14612005-09-14 19:29:53 +00002080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 case 'b': /* byte -- very short int */
2082 case 'B': /* byte as bitfield */
2083 case 'h': /* short int */
2084 case 'H': /* short int as bitfield */
2085 case 'i': /* int */
2086 case 'I': /* int sized bitfield */
2087 case 'l': /* long int */
2088 case 'k': /* long int sized bitfield */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 case 'L': /* PY_LONG_LONG */
2090 case 'K': /* PY_LONG_LONG sized bitfield */
Larry Hastingsa3479012012-05-08 23:52:03 -07002091 case 'n': /* Py_ssize_t */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 case 'f': /* float */
2093 case 'd': /* double */
2094 case 'D': /* complex double */
2095 case 'c': /* char */
2096 case 'C': /* unicode char */
Larry Hastings10ba07a2012-05-07 02:44:50 -07002097 case 'p': /* boolean predicate */
Larry Hastingsa3479012012-05-08 23:52:03 -07002098 case 'S': /* string object */
2099 case 'Y': /* string object */
2100 case 'U': /* unicode string object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002102 if (p_va != NULL) {
2103 (void) va_arg(*p_va, void *);
2104 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 break;
2106 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 case 'e': /* string with encoding */
2111 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002112 if (p_va != NULL) {
2113 (void) va_arg(*p_va, const char *);
2114 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 if (!(*format == 's' || *format == 't'))
2116 /* after 'e', only 's' and 't' is allowed */
2117 goto err;
2118 format++;
2119 /* explicit fallthrough to string cases */
2120 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 case 's': /* string */
2123 case 'z': /* string or None */
2124 case 'y': /* bytes */
2125 case 'u': /* unicode string */
Larry Hastingsd9e4a412012-05-08 03:51:18 -07002126 case 'Z': /* unicode string or None */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 case 'w': /* buffer, read-write */
2128 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002129 if (p_va != NULL) {
2130 (void) va_arg(*p_va, char **);
2131 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 if (*format == '#') {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002133 if (p_va != NULL) {
2134 if (flags & FLAG_SIZE_T)
2135 (void) va_arg(*p_va, Py_ssize_t *);
2136 else
2137 (void) va_arg(*p_va, int *);
2138 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 format++;
2140 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
2141 format++;
2142 }
2143 break;
2144 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 case 'O': /* object */
2147 {
2148 if (*format == '!') {
2149 format++;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002150 if (p_va != NULL) {
2151 (void) va_arg(*p_va, PyTypeObject*);
2152 (void) va_arg(*p_va, PyObject **);
2153 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 }
2155 else if (*format == '&') {
2156 typedef int (*converter)(PyObject *, void *);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002157 if (p_va != NULL) {
2158 (void) va_arg(*p_va, converter);
2159 (void) va_arg(*p_va, void *);
2160 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 format++;
2162 }
2163 else {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002164 if (p_va != NULL) {
2165 (void) va_arg(*p_va, PyObject **);
2166 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 }
2168 break;
2169 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 case '(': /* bypass tuple, not handled at all previously */
2172 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002173 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 for (;;) {
2175 if (*format==')')
2176 break;
2177 if (IS_END_OF_FORMAT(*format))
2178 return "Unmatched left paren in format "
2179 "string";
2180 msg = skipitem(&format, p_va, flags);
2181 if (msg)
2182 return msg;
2183 }
2184 format++;
2185 break;
2186 }
Christian Heimes380f7f22008-02-28 11:19:05 +00002187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 case ')':
2189 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00002190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00002192err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00002194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 *p_format = format;
2198 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00002199}
Fred Drakee4616e62001-10-23 21:09:29 +00002200
2201
2202int
Martin v. Löwis76246742006-03-01 04:06:10 +00002203PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00002204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 Py_ssize_t i, l;
2206 PyObject **o;
2207 va_list vargs;
Fred Drakee4616e62001-10-23 21:09:29 +00002208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 assert(min >= 0);
2210 assert(min <= max);
2211 if (!PyTuple_Check(args)) {
2212 PyErr_SetString(PyExc_SystemError,
2213 "PyArg_UnpackTuple() argument list is not a tuple");
2214 return 0;
2215 }
2216 l = PyTuple_GET_SIZE(args);
2217 if (l < min) {
2218 if (name != NULL)
2219 PyErr_Format(
2220 PyExc_TypeError,
2221 "%s expected %s%zd arguments, got %zd",
2222 name, (min == max ? "" : "at least "), min, l);
2223 else
2224 PyErr_Format(
2225 PyExc_TypeError,
2226 "unpacked tuple should have %s%zd elements,"
2227 " but has %zd",
2228 (min == max ? "" : "at least "), min, l);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 return 0;
2230 }
Raymond Hettinger94230232016-03-26 03:02:48 -07002231 if (l == 0)
2232 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 if (l > max) {
2234 if (name != NULL)
2235 PyErr_Format(
2236 PyExc_TypeError,
2237 "%s expected %s%zd arguments, got %zd",
2238 name, (min == max ? "" : "at most "), max, l);
2239 else
2240 PyErr_Format(
2241 PyExc_TypeError,
2242 "unpacked tuple should have %s%zd elements,"
2243 " but has %zd",
2244 (min == max ? "" : "at most "), max, l);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 return 0;
2246 }
Raymond Hettinger94230232016-03-26 03:02:48 -07002247
2248#ifdef HAVE_STDARG_PROTOTYPES
2249 va_start(vargs, max);
2250#else
2251 va_start(vargs);
2252#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 for (i = 0; i < l; i++) {
2254 o = va_arg(vargs, PyObject **);
2255 *o = PyTuple_GET_ITEM(args, i);
2256 }
2257 va_end(vargs);
2258 return 1;
Fred Drakee4616e62001-10-23 21:09:29 +00002259}
Georg Brandl02c42872005-08-26 06:42:30 +00002260
2261
2262/* For type constructors that don't take keyword args
2263 *
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002264 * Sets a TypeError and returns 0 if the args/kwargs is
Thomas Wouters89f507f2006-12-13 04:49:30 +00002265 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00002266 */
2267int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002268_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00002269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 if (kw == NULL)
2271 return 1;
2272 if (!PyDict_CheckExact(kw)) {
2273 PyErr_BadInternalCall();
2274 return 0;
2275 }
2276 if (PyDict_Size(kw) == 0)
2277 return 1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
2280 funcname);
2281 return 0;
Georg Brandl02c42872005-08-26 06:42:30 +00002282}
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002283
2284
2285int
2286_PyArg_NoPositional(const char *funcname, PyObject *args)
2287{
2288 if (args == NULL)
2289 return 1;
2290 if (!PyTuple_CheckExact(args)) {
2291 PyErr_BadInternalCall();
2292 return 0;
2293 }
2294 if (PyTuple_GET_SIZE(args) == 0)
2295 return 1;
2296
2297 PyErr_Format(PyExc_TypeError, "%s does not take positional arguments",
2298 funcname);
2299 return 0;
2300}
2301
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002302void
2303_PyArg_Fini(void)
2304{
2305 struct _PyArg_Parser *tmp, *s = static_arg_parsers;
2306 while (s) {
2307 tmp = s->next;
2308 s->next = NULL;
2309 parser_clear(s);
2310 s = tmp;
2311 }
2312 static_arg_parsers = NULL;
2313}
2314
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002315#ifdef __cplusplus
2316};
2317#endif