blob: 616c6eb107377fc2dfbc9289e09b9671fd25cf04 [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 *, ...);
Benjamin Peterson819a46f2016-09-09 20:45:06 -070029PyAPI_FUNC(int) _PyArg_ParseStack_SizeT(PyObject **args, Py_ssize_t nargs, PyObject *kwnames,
30 struct _PyArg_Parser *parser, ...);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020031PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000032PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
33 const char *, char **, ...);
34PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020035PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
37 const char *, char **, va_list);
Benjamin Peterson4eef5052016-09-10 17:04:36 -070038
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030039PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
40 struct _PyArg_Parser *, ...);
41PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
42 struct _PyArg_Parser *, va_list);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043#endif
44
Martin v. Löwis18e16552006-02-15 17:27:45 +000045#define FLAG_COMPAT 1
46#define FLAG_SIZE_T 2
47
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040048typedef int (*destr_t)(PyObject *, void *);
49
50
51/* Keep track of "objects" that have been allocated or initialized and
52 which will need to be deallocated or cleaned up somehow if overall
53 parsing fails.
54*/
55typedef struct {
56 void *item;
57 destr_t destructor;
58} freelistentry_t;
59
60typedef struct {
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040061 freelistentry_t *entries;
Antoine Pitrou7056cb22013-02-17 01:04:57 +010062 int first_available;
63 int entries_malloced;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040064} freelist_t;
65
Antoine Pitrou7056cb22013-02-17 01:04:57 +010066#define STATIC_FREELIST_ENTRIES 8
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000067
68/* Forward */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000069static int vgetargs1(PyObject *, const char *, va_list *, int);
Victor Stinner84bb1cf2013-05-17 00:12:04 +020070static void seterror(Py_ssize_t, const char *, int *, const char *, const char *);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020071static const char *convertitem(PyObject *, const char **, va_list *, int, int *,
72 char *, size_t, freelist_t *);
73static const char *converttuple(PyObject *, const char **, va_list *, int,
74 int *, char *, size_t, int, freelist_t *);
75static const char *convertsimple(PyObject *, const char **, va_list *, int,
76 char *, size_t, freelist_t *);
77static Py_ssize_t convertbuffer(PyObject *, void **p, const char **);
78static int getbuffer(PyObject *, Py_buffer *, const char**);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000079
Tim Petersdbd9ba62000-07-09 03:09:57 +000080static int vgetargskeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 const char *, char **, va_list *, int);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030082static int vgetargskeywordsfast(PyObject *, PyObject *,
83 struct _PyArg_Parser *, va_list *, int);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -070084static int vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
85 PyObject *keywords, PyObject *kwnames,
86 struct _PyArg_Parser *parser,
87 va_list *p_va, int flags);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020088static const char *skipitem(const char **, va_list *, int);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000089
Fred Drake563dfc22001-10-23 14:41:08 +000090int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000091PyArg_Parse(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 int retval;
94 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +000095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 va_start(va, format);
97 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
98 va_end(va);
99 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000100}
101
102int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200103_PyArg_Parse_SizeT(PyObject *args, const char *format, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 int retval;
106 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 va_start(va, format);
109 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
110 va_end(va);
111 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000112}
113
114
Fred Drake563dfc22001-10-23 14:41:08 +0000115int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000116PyArg_ParseTuple(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 int retval;
119 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 va_start(va, format);
122 retval = vgetargs1(args, format, &va, 0);
123 va_end(va);
124 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000125}
126
Martin v. Löwis18e16552006-02-15 17:27:45 +0000127int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200128_PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 int retval;
131 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 va_start(va, format);
134 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
135 va_end(va);
136 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000137}
138
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000139
140int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000141PyArg_VaParse(PyObject *args, const char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 va_list lva;
Christian Heimes2f2fee12016-09-21 11:37:27 +0200144 int retval;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000145
Benjamin Peterson0c212142016-09-20 20:39:33 -0700146 va_copy(lva, va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000147
Christian Heimes2f2fee12016-09-21 11:37:27 +0200148 retval = vgetargs1(args, format, &lva, 0);
149 va_end(lva);
150 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000151}
152
Martin v. Löwis18e16552006-02-15 17:27:45 +0000153int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200154_PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 va_list lva;
Christian Heimes2f2fee12016-09-21 11:37:27 +0200157 int retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000158
Benjamin Peterson0c212142016-09-20 20:39:33 -0700159 va_copy(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000160
Christian Heimes2f2fee12016-09-21 11:37:27 +0200161 retval = vgetargs1(args, format, &lva, FLAG_SIZE_T);
162 va_end(lva);
163 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000164}
165
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000166
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000167/* Handle cleanup of allocated memory in case of exception */
168
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400169static int
170cleanup_ptr(PyObject *self, void *ptr)
Antoine Pitrouf71995782008-08-29 18:37:05 +0000171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 if (ptr) {
173 PyMem_FREE(ptr);
174 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000176}
177
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000178static int
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400179cleanup_buffer(PyObject *self, void *ptr)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000180{
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400181 Py_buffer *buf = (Py_buffer *)ptr;
182 if (buf) {
183 PyBuffer_Release(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400185 return 0;
186}
187
188static int
189addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
190{
191 int index;
192
193 index = freelist->first_available;
194 freelist->first_available += 1;
195
196 freelist->entries[index].item = ptr;
197 freelist->entries[index].destructor = destructor;
198
199 return 0;
200}
201
202static int
203cleanreturn(int retval, freelist_t *freelist)
204{
205 int index;
206
207 if (retval == 0) {
208 /* A failure occurred, therefore execute all of the cleanup
209 functions.
210 */
211 for (index = 0; index < freelist->first_available; ++index) {
212 freelist->entries[index].destructor(NULL,
213 freelist->entries[index].item);
214 }
215 }
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100216 if (freelist->entries_malloced)
217 PyMem_FREE(freelist->entries);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 return retval;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000219}
220
221
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000222static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000223vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 char msgbuf[256];
226 int levels[32];
227 const char *fname = NULL;
228 const char *message = NULL;
229 int min = -1;
230 int max = 0;
231 int level = 0;
232 int endfmt = 0;
233 const char *formatsave = format;
234 Py_ssize_t i, len;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200235 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 int compat = flags & FLAG_COMPAT;
Benjamin Peterson40be9e52014-02-11 10:09:27 -0500237 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
238 freelist_t freelist;
239
240 freelist.entries = static_entries;
241 freelist.first_available = 0;
242 freelist.entries_malloced = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 assert(compat || (args != (PyObject*)NULL));
245 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 while (endfmt == 0) {
248 int c = *format++;
249 switch (c) {
250 case '(':
251 if (level == 0)
252 max++;
253 level++;
254 if (level >= 30)
255 Py_FatalError("too many tuple nesting levels "
256 "in argument format string");
257 break;
258 case ')':
259 if (level == 0)
260 Py_FatalError("excess ')' in getargs format");
261 else
262 level--;
263 break;
264 case '\0':
265 endfmt = 1;
266 break;
267 case ':':
268 fname = format;
269 endfmt = 1;
270 break;
271 case ';':
272 message = format;
273 endfmt = 1;
274 break;
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100275 case '|':
276 if (level == 0)
277 min = max;
278 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 default:
280 if (level == 0) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100281 if (Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 if (c != 'e') /* skip encoded */
283 max++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 }
285 break;
286 }
287 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 if (level != 0)
290 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 if (min < 0)
293 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000296
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100297 if (max > STATIC_FREELIST_ENTRIES) {
298 freelist.entries = PyMem_NEW(freelistentry_t, max);
299 if (freelist.entries == NULL) {
300 PyErr_NoMemory();
301 return 0;
302 }
303 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -0500304 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (compat) {
307 if (max == 0) {
308 if (args == NULL)
309 return 1;
Victor Stinner6ced7c42011-03-21 18:15:42 +0100310 PyErr_Format(PyExc_TypeError,
311 "%.200s%s takes no arguments",
312 fname==NULL ? "function" : fname,
313 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400314 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 }
316 else if (min == 1 && max == 1) {
317 if (args == NULL) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100318 PyErr_Format(PyExc_TypeError,
319 "%.200s%s takes at least one argument",
320 fname==NULL ? "function" : fname,
321 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400322 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 }
324 msg = convertitem(args, &format, p_va, flags, levels,
325 msgbuf, sizeof(msgbuf), &freelist);
326 if (msg == NULL)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400327 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 seterror(levels[0], msg, levels+1, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400329 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 }
331 else {
332 PyErr_SetString(PyExc_SystemError,
333 "old style getargs format uses new features");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400334 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 }
336 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 if (!PyTuple_Check(args)) {
339 PyErr_SetString(PyExc_SystemError,
340 "new style getargs format but argument is not a tuple");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400341 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 len = PyTuple_GET_SIZE(args);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 if (len < min || max < len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100347 if (message == NULL)
348 PyErr_Format(PyExc_TypeError,
349 "%.150s%s takes %s %d argument%s (%ld given)",
350 fname==NULL ? "function" : fname,
351 fname==NULL ? "" : "()",
352 min==max ? "exactly"
353 : len < min ? "at least" : "at most",
354 len < min ? min : max,
355 (len < min ? min : max) == 1 ? "" : "s",
356 Py_SAFE_DOWNCAST(len, Py_ssize_t, long));
357 else
358 PyErr_SetString(PyExc_TypeError, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400359 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 for (i = 0; i < len; i++) {
363 if (*format == '|')
364 format++;
365 msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va,
366 flags, levels, msgbuf,
367 sizeof(msgbuf), &freelist);
368 if (msg) {
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200369 seterror(i+1, msg, levels, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400370 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 }
372 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000373
Antoine Pitrou4de74572013-02-09 23:11:27 +0100374 if (*format != '\0' && !Py_ISALPHA(Py_CHARMASK(*format)) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 *format != '(' &&
376 *format != '|' && *format != ':' && *format != ';') {
377 PyErr_Format(PyExc_SystemError,
378 "bad format string: %.200s", formatsave);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400379 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000381
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400382 return cleanreturn(1, &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000383}
384
385
386
387static void
Victor Stinner84bb1cf2013-05-17 00:12:04 +0200388seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000389 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 char buf[512];
392 int i;
393 char *p = buf;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (PyErr_Occurred())
396 return;
397 else if (message == NULL) {
398 if (fname != NULL) {
399 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
400 p += strlen(p);
401 }
402 if (iarg != 0) {
403 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Richard Oudkerk25296ce2013-05-18 17:35:19 +0100404 "argument %" PY_FORMAT_SIZE_T "d", iarg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 i = 0;
406 p += strlen(p);
Georg Brandl142ad662013-10-14 07:01:11 +0200407 while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 PyOS_snprintf(p, sizeof(buf) - (p - buf),
409 ", item %d", levels[i]-1);
410 p += strlen(p);
411 i++;
412 }
413 }
414 else {
415 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
416 p += strlen(p);
417 }
418 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
419 message = buf;
420 }
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +0200421 if (msg[0] == '(') {
422 PyErr_SetString(PyExc_SystemError, message);
423 }
424 else {
425 PyErr_SetString(PyExc_TypeError, message);
426 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000427}
428
429
430/* Convert a tuple argument.
431 On entry, *p_format points to the character _after_ the opening '('.
432 On successful exit, *p_format points to the closing ')'.
433 If successful:
434 *p_format and *p_va are updated,
435 *levels and *msgbuf are untouched,
436 and NULL is returned.
437 If the argument is invalid:
438 *p_format is unchanged,
439 *p_va is undefined,
440 *levels is a 0-terminated list of item numbers,
441 *msgbuf contains an error message, whose format is:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 "must be <typename1>, not <typename2>", where:
443 <typename1> is the name of the expected type, and
444 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000445 and msgbuf is returned.
446*/
447
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200448static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000449converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000450 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400451 freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 int level = 0;
454 int n = 0;
455 const char *format = *p_format;
456 int i;
Victor Stinner74387f52013-11-18 01:21:12 +0100457 Py_ssize_t len;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 for (;;) {
460 int c = *format++;
461 if (c == '(') {
462 if (level == 0)
463 n++;
464 level++;
465 }
466 else if (c == ')') {
467 if (level == 0)
468 break;
469 level--;
470 }
471 else if (c == ':' || c == ';' || c == '\0')
472 break;
Antoine Pitrou4de74572013-02-09 23:11:27 +0100473 else if (level == 0 && Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 n++;
475 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
478 levels[0] = 0;
479 PyOS_snprintf(msgbuf, bufsize,
480 toplevel ? "expected %d arguments, not %.50s" :
481 "must be %d-item sequence, not %.50s",
482 n,
483 arg == Py_None ? "None" : arg->ob_type->tp_name);
484 return msgbuf;
485 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000486
Victor Stinner74387f52013-11-18 01:21:12 +0100487 len = PySequence_Size(arg);
488 if (len != n) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 levels[0] = 0;
Victor Stinner74387f52013-11-18 01:21:12 +0100490 if (toplevel) {
491 PyOS_snprintf(msgbuf, bufsize,
492 "expected %d arguments, not %" PY_FORMAT_SIZE_T "d",
493 n, len);
494 }
495 else {
496 PyOS_snprintf(msgbuf, bufsize,
497 "must be sequence of length %d, "
498 "not %" PY_FORMAT_SIZE_T "d",
499 n, len);
500 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 return msgbuf;
502 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 format = *p_format;
505 for (i = 0; i < n; i++) {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200506 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 PyObject *item;
508 item = PySequence_GetItem(arg, i);
509 if (item == NULL) {
510 PyErr_Clear();
511 levels[0] = i+1;
512 levels[1] = 0;
513 strncpy(msgbuf, "is not retrievable", bufsize);
514 return msgbuf;
515 }
516 msg = convertitem(item, &format, p_va, flags, levels+1,
517 msgbuf, bufsize, freelist);
518 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
519 Py_XDECREF(item);
520 if (msg != NULL) {
521 levels[0] = i+1;
522 return msg;
523 }
524 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 *p_format = format;
527 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000528}
529
530
531/* Convert a single item. */
532
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200533static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000534convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400535 int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000536{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200537 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (*format == '(' /* ')' */) {
541 format++;
542 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
543 bufsize, 0, freelist);
544 if (msg == NULL)
545 format++;
546 }
547 else {
548 msg = convertsimple(arg, &format, p_va, flags,
549 msgbuf, bufsize, freelist);
550 if (msg != NULL)
551 levels[0] = 0;
552 }
553 if (msg == NULL)
554 *p_format = format;
555 return msg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000556}
557
558
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000559
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000560/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000561
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200562static const char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000563converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 assert(expected != NULL);
566 assert(arg != NULL);
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200567 if (expected[0] == '(') {
568 PyOS_snprintf(msgbuf, bufsize,
569 "%.100s", expected);
570 }
571 else {
572 PyOS_snprintf(msgbuf, bufsize,
573 "must be %.50s, not %.50s", expected,
574 arg == Py_None ? "None" : arg->ob_type->tp_name);
575 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000577}
578
579#define CONV_UNICODE "(unicode conversion error)"
580
Guido van Rossum45aecf42006-03-15 04:58:47 +0000581/* Explicitly check for float arguments when integers are expected.
582 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000583static int
584float_argument_error(PyObject *arg)
585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 if (PyFloat_Check(arg)) {
587 PyErr_SetString(PyExc_TypeError,
588 "integer argument expected, got float" );
589 return 1;
590 }
591 else
592 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000593}
594
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000595/* Convert a non-tuple argument. Return NULL if conversion went OK,
596 or a string with a message describing the failure. The message is
597 formatted as "must be <desired type>, not <actual type>".
598 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000599 Don't call if a tuple is expected.
600
601 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000602*/
603
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200604static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000605convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400606 char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 /* For # codes */
609#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
610 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
611 else q=va_arg(*p_va, int*);
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000612#define STORE_SIZE(s) \
613 if (flags & FLAG_SIZE_T) \
614 *q2=s; \
615 else { \
616 if (INT_MAX < s) { \
617 PyErr_SetString(PyExc_OverflowError, \
618 "size does not fit in an int"); \
619 return converterr("", arg, msgbuf, bufsize); \
620 } \
Victor Stinner9550ef32013-06-05 01:18:13 +0200621 *q = (int)s; \
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000622 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000623#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
Victor Stinner6ab8e822011-01-04 11:16:49 +0000624#define RETURN_ERR_OCCURRED return msgbuf
Martin v. Löwis18e16552006-02-15 17:27:45 +0000625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 const char *format = *p_format;
627 char c = *format++;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200628 char *sarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 case 'b': { /* unsigned byte -- very short int */
633 char *p = va_arg(*p_va, char *);
634 long ival;
635 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000636 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 ival = PyLong_AsLong(arg);
638 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000639 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 else if (ival < 0) {
641 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000642 "unsigned byte integer is less than minimum");
643 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 }
645 else if (ival > UCHAR_MAX) {
646 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000647 "unsigned byte integer is greater than maximum");
648 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 }
650 else
651 *p = (unsigned char) ival;
652 break;
653 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 case 'B': {/* byte sized bitfield - both signed and unsigned
656 values allowed */
657 char *p = va_arg(*p_va, char *);
658 long ival;
659 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000660 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 ival = PyLong_AsUnsignedLongMask(arg);
662 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000663 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 else
665 *p = (unsigned char) ival;
666 break;
667 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 case 'h': {/* signed short int */
670 short *p = va_arg(*p_va, short *);
671 long ival;
672 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000673 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 ival = PyLong_AsLong(arg);
675 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000676 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 else if (ival < SHRT_MIN) {
678 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000679 "signed short integer is less than minimum");
680 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 }
682 else if (ival > SHRT_MAX) {
683 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000684 "signed short integer is greater than maximum");
685 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 }
687 else
688 *p = (short) ival;
689 break;
690 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 case 'H': { /* short int sized bitfield, both signed and
693 unsigned allowed */
694 unsigned short *p = va_arg(*p_va, unsigned short *);
695 long ival;
696 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000697 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 ival = PyLong_AsUnsignedLongMask(arg);
699 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000700 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 else
702 *p = (unsigned short) ival;
703 break;
704 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 case 'i': {/* signed int */
707 int *p = va_arg(*p_va, int *);
708 long ival;
709 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000710 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 ival = PyLong_AsLong(arg);
712 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000713 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 else if (ival > INT_MAX) {
715 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000716 "signed integer is greater than maximum");
717 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 }
719 else if (ival < INT_MIN) {
720 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000721 "signed integer is less than minimum");
722 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 }
724 else
725 *p = ival;
726 break;
727 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 case 'I': { /* int sized bitfield, both signed and
730 unsigned allowed */
731 unsigned int *p = va_arg(*p_va, unsigned int *);
732 unsigned int ival;
733 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000734 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
736 if (ival == (unsigned int)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000737 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 else
739 *p = ival;
740 break;
741 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 case 'n': /* Py_ssize_t */
744 {
745 PyObject *iobj;
746 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
747 Py_ssize_t ival = -1;
748 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000749 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 iobj = PyNumber_Index(arg);
751 if (iobj != NULL) {
752 ival = PyLong_AsSsize_t(iobj);
753 Py_DECREF(iobj);
754 }
755 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000756 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 *p = ival;
758 break;
759 }
760 case 'l': {/* long int */
761 long *p = va_arg(*p_va, long *);
762 long ival;
763 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000764 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 ival = PyLong_AsLong(arg);
766 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000767 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 else
769 *p = ival;
770 break;
771 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 case 'k': { /* long sized bitfield */
774 unsigned long *p = va_arg(*p_va, unsigned long *);
775 unsigned long ival;
776 if (PyLong_Check(arg))
777 ival = PyLong_AsUnsignedLongMask(arg);
778 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200779 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 *p = ival;
781 break;
782 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000783
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700784 case 'L': {/* long long */
785 long long *p = va_arg( *p_va, long long * );
786 long long ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000787 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000788 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 ival = PyLong_AsLongLong(arg);
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700790 if (ival == (long long)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000791 RETURN_ERR_OCCURRED;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000792 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 break;
795 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 case 'K': { /* long long sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700798 unsigned long long *p = va_arg(*p_va, unsigned long long *);
799 unsigned long long ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 if (PyLong_Check(arg))
801 ival = PyLong_AsUnsignedLongLongMask(arg);
802 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200803 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 *p = ival;
805 break;
806 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 case 'f': {/* float */
809 float *p = va_arg(*p_va, float *);
810 double dval = PyFloat_AsDouble(arg);
811 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000812 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 else
814 *p = (float) dval;
815 break;
816 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 case 'd': {/* double */
819 double *p = va_arg(*p_va, double *);
820 double dval = PyFloat_AsDouble(arg);
821 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000822 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 else
824 *p = dval;
825 break;
826 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 case 'D': {/* complex double */
829 Py_complex *p = va_arg(*p_va, Py_complex *);
830 Py_complex cval;
831 cval = PyComplex_AsCComplex(arg);
832 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000833 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 else
835 *p = cval;
836 break;
837 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 case 'c': {/* char */
840 char *p = va_arg(*p_va, char *);
841 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
842 *p = PyBytes_AS_STRING(arg)[0];
Eli Bendersky906b88f2011-07-29 07:05:08 +0300843 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
844 *p = PyByteArray_AS_STRING(arg)[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 else
846 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
847 break;
848 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 case 'C': {/* unicode char */
851 int *p = va_arg(*p_va, int *);
Victor Stinnere1335c72011-10-04 20:53:03 +0200852 int kind;
853 void *data;
854
855 if (!PyUnicode_Check(arg))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 return converterr("a unicode character", arg, msgbuf, bufsize);
Victor Stinnere1335c72011-10-04 20:53:03 +0200857
858 if (PyUnicode_READY(arg))
859 RETURN_ERR_OCCURRED;
860
861 if (PyUnicode_GET_LENGTH(arg) != 1)
862 return converterr("a unicode character", arg, msgbuf, bufsize);
863
864 kind = PyUnicode_KIND(arg);
865 data = PyUnicode_DATA(arg);
866 *p = PyUnicode_READ(kind, data, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 break;
868 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000869
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700870 case 'p': {/* boolean *p*redicate */
871 int *p = va_arg(*p_va, int *);
872 int val = PyObject_IsTrue(arg);
873 if (val > 0)
874 *p = 1;
875 else if (val == 0)
876 *p = 0;
877 else
878 RETURN_ERR_OCCURRED;
879 break;
880 }
881
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000882 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000884
Serhiy Storchakab757c832014-12-05 22:25:22 +0200885 case 'y': {/* any bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 void **p = (void **)va_arg(*p_va, char **);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200887 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 Py_ssize_t count;
889 if (*format == '*') {
890 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
891 return converterr(buf, arg, msgbuf, bufsize);
892 format++;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400893 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 return converterr(
895 "(cleanup problem)",
896 arg, msgbuf, bufsize);
897 }
898 break;
899 }
900 count = convertbuffer(arg, p, &buf);
901 if (count < 0)
902 return converterr(buf, arg, msgbuf, bufsize);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000903 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 FETCH_SIZE;
905 STORE_SIZE(count);
906 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000907 } else {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300908 if (strlen(*p) != (size_t)count) {
909 PyErr_SetString(PyExc_ValueError, "embedded null byte");
910 RETURN_ERR_OCCURRED;
911 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 }
913 break;
914 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000915
Serhiy Storchakab757c832014-12-05 22:25:22 +0200916 case 's': /* text string or bytes-like object */
917 case 'z': /* text string, bytes-like object or None */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000918 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000920 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000922
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000923 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
925 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200926 Py_ssize_t len;
927 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
928 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return converterr(CONV_UNICODE,
930 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200931 PyBuffer_FillInfo(p, arg, sarg, len, 1, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 }
Serhiy Storchakab757c832014-12-05 22:25:22 +0200933 else { /* any bytes-like object */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200934 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 if (getbuffer(arg, p, &buf) < 0)
936 return converterr(buf, arg, msgbuf, bufsize);
937 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400938 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 return converterr(
940 "(cleanup problem)",
941 arg, msgbuf, bufsize);
942 }
943 format++;
Serhiy Storchakab757c832014-12-05 22:25:22 +0200944 } else if (*format == '#') { /* a string or read-only bytes-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000945 /* "s#" or "z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 void **p = (void **)va_arg(*p_va, char **);
947 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000948
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000949 if (c == 'z' && arg == Py_None) {
950 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 STORE_SIZE(0);
952 }
953 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200954 Py_ssize_t len;
955 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
956 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 return converterr(CONV_UNICODE,
958 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200959 *p = sarg;
960 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 }
Serhiy Storchakab757c832014-12-05 22:25:22 +0200962 else { /* read-only bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 /* XXX Really? */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200964 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 Py_ssize_t count = convertbuffer(arg, p, &buf);
966 if (count < 0)
967 return converterr(buf, arg, msgbuf, bufsize);
968 STORE_SIZE(count);
969 }
970 format++;
971 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000972 /* "s" or "z" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 char **p = va_arg(*p_va, char **);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200974 Py_ssize_t len;
975 sarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000976
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000977 if (c == 'z' && arg == Py_None)
978 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200980 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
981 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 return converterr(CONV_UNICODE,
983 arg, msgbuf, bufsize);
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300984 if (strlen(sarg) != (size_t)len) {
985 PyErr_SetString(PyExc_ValueError, "embedded null character");
986 RETURN_ERR_OCCURRED;
987 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200988 *p = sarg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 }
990 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000991 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 }
994 break;
995 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000996
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000997 case 'u': /* raw unicode buffer (Py_UNICODE *) */
998 case 'Z': /* raw unicode buffer or None */
999 {
Brett Cannonb94767f2011-02-22 20:15:44 +00001000 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1001
Serhiy Storchakab757c832014-12-05 22:25:22 +02001002 if (*format == '#') {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001003 /* "u#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001005
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001006 if (c == 'Z' && arg == Py_None) {
1007 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 STORE_SIZE(0);
1009 }
1010 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001011 Py_ssize_t len;
1012 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001013 if (*p == NULL)
1014 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001015 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 }
Victor Stinner5216e6d2010-06-08 21:45:51 +00001017 else
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001018 return converterr(c == 'Z' ? "str or None" : "str",
1019 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 format++;
1021 } else {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001022 /* "u" or "Z" */
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001023 if (c == 'Z' && arg == Py_None)
1024 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +00001025 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001026 Py_ssize_t len;
1027 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001028 if (*p == NULL)
1029 RETURN_ERR_OCCURRED;
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001030 if (Py_UNICODE_strlen(*p) != (size_t)len) {
1031 PyErr_SetString(PyExc_ValueError, "embedded null character");
1032 RETURN_ERR_OCCURRED;
1033 }
Victor Stinner06e49dd2010-06-13 18:21:50 +00001034 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001035 return converterr(c == 'Z' ? "str or None" : "str",
1036 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 }
1038 break;
1039 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 case 'e': {/* encoded string */
1042 char **buffer;
1043 const char *encoding;
1044 PyObject *s;
1045 int recode_strings;
1046 Py_ssize_t size;
1047 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 /* Get 'e' parameter: the encoding name */
1050 encoding = (const char *)va_arg(*p_va, const char *);
1051 if (encoding == NULL)
1052 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +00001053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 /* Get output buffer parameter:
1055 's' (recode all objects via Unicode) or
1056 't' (only recode non-string objects)
1057 */
1058 if (*format == 's')
1059 recode_strings = 1;
1060 else if (*format == 't')
1061 recode_strings = 0;
1062 else
1063 return converterr(
1064 "(unknown parser marker combination)",
1065 arg, msgbuf, bufsize);
1066 buffer = (char **)va_arg(*p_va, char **);
1067 format++;
1068 if (buffer == NULL)
1069 return converterr("(buffer is NULL)",
1070 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 /* Encode object */
1073 if (!recode_strings &&
1074 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1075 s = arg;
1076 Py_INCREF(s);
1077 if (PyObject_AsCharBuffer(s, &ptr, &size) < 0)
1078 return converterr("(AsCharBuffer failed)",
1079 arg, msgbuf, bufsize);
1080 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001081 else if (PyUnicode_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 /* Encode object; use default error handling */
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001083 s = PyUnicode_AsEncodedString(arg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 encoding,
1085 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 if (s == NULL)
1087 return converterr("(encoding failed)",
1088 arg, msgbuf, bufsize);
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001089 assert(PyBytes_Check(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 size = PyBytes_GET_SIZE(s);
1091 ptr = PyBytes_AS_STRING(s);
1092 if (ptr == NULL)
1093 ptr = "";
1094 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001095 else {
1096 return converterr(
1097 recode_strings ? "str" : "str, bytes or bytearray",
1098 arg, msgbuf, bufsize);
1099 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 /* Write output; output is guaranteed to be 0-terminated */
1102 if (*format == '#') {
1103 /* Using buffer length parameter '#':
1104
1105 - if *buffer is NULL, a new buffer of the
1106 needed size is allocated and the data
1107 copied into it; *buffer is updated to point
1108 to the new buffer; the caller is
1109 responsible for PyMem_Free()ing it after
1110 usage
1111
1112 - if *buffer is not NULL, the data is
1113 copied to *buffer; *buffer_len has to be
1114 set to the size of the buffer on input;
1115 buffer overflow is signalled with an error;
1116 buffer has to provide enough room for the
1117 encoded string plus the trailing 0-byte
1118
1119 - in both cases, *buffer_len is updated to
1120 the size of the buffer /excluding/ the
1121 trailing 0-byte
1122
1123 */
1124 FETCH_SIZE;
1125
1126 format++;
1127 if (q == NULL && q2 == NULL) {
1128 Py_DECREF(s);
1129 return converterr(
1130 "(buffer_len is NULL)",
1131 arg, msgbuf, bufsize);
1132 }
1133 if (*buffer == NULL) {
1134 *buffer = PyMem_NEW(char, size + 1);
1135 if (*buffer == NULL) {
1136 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001137 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001138 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001140 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 Py_DECREF(s);
1142 return converterr(
1143 "(cleanup problem)",
1144 arg, msgbuf, bufsize);
1145 }
1146 } else {
1147 if (size + 1 > BUFFER_LEN) {
1148 Py_DECREF(s);
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +02001149 PyErr_Format(PyExc_ValueError,
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001150 "encoded string too long "
1151 "(%zd, maximum length %zd)",
1152 (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1153 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 }
1155 }
1156 memcpy(*buffer, ptr, size+1);
1157 STORE_SIZE(size);
1158 } else {
1159 /* Using a 0-terminated buffer:
1160
1161 - the encoded string has to be 0-terminated
1162 for this variant to work; if it is not, an
1163 error raised
1164
1165 - a new buffer of the needed size is
1166 allocated and the data copied into it;
1167 *buffer is updated to point to the new
1168 buffer; the caller is responsible for
1169 PyMem_Free()ing it after usage
1170
1171 */
1172 if ((Py_ssize_t)strlen(ptr) != size) {
1173 Py_DECREF(s);
1174 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001175 "encoded string without null bytes",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 arg, msgbuf, bufsize);
1177 }
1178 *buffer = PyMem_NEW(char, size + 1);
1179 if (*buffer == NULL) {
1180 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001181 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001182 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001184 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 Py_DECREF(s);
1186 return converterr("(cleanup problem)",
1187 arg, msgbuf, bufsize);
1188 }
1189 memcpy(*buffer, ptr, size+1);
1190 }
1191 Py_DECREF(s);
1192 break;
1193 }
1194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 case 'S': { /* PyBytes object */
1196 PyObject **p = va_arg(*p_va, PyObject **);
1197 if (PyBytes_Check(arg))
1198 *p = arg;
1199 else
1200 return converterr("bytes", arg, msgbuf, bufsize);
1201 break;
1202 }
1203
1204 case 'Y': { /* PyByteArray object */
1205 PyObject **p = va_arg(*p_va, PyObject **);
1206 if (PyByteArray_Check(arg))
1207 *p = arg;
1208 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001209 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 break;
1211 }
1212
1213 case 'U': { /* PyUnicode object */
1214 PyObject **p = va_arg(*p_va, PyObject **);
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001215 if (PyUnicode_Check(arg)) {
1216 if (PyUnicode_READY(arg) == -1)
1217 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 *p = arg;
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001219 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 else
1221 return converterr("str", arg, msgbuf, bufsize);
1222 break;
1223 }
1224
1225 case 'O': { /* object */
1226 PyTypeObject *type;
1227 PyObject **p;
1228 if (*format == '!') {
1229 type = va_arg(*p_va, PyTypeObject*);
1230 p = va_arg(*p_va, PyObject **);
1231 format++;
1232 if (PyType_IsSubtype(arg->ob_type, type))
1233 *p = arg;
1234 else
1235 return converterr(type->tp_name, arg, msgbuf, bufsize);
1236
1237 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 else if (*format == '&') {
1239 typedef int (*converter)(PyObject *, void *);
1240 converter convert = va_arg(*p_va, converter);
1241 void *addr = va_arg(*p_va, void *);
1242 int res;
1243 format++;
1244 if (! (res = (*convert)(arg, addr)))
1245 return converterr("(unspecified)",
1246 arg, msgbuf, bufsize);
1247 if (res == Py_CLEANUP_SUPPORTED &&
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001248 addcleanup(addr, freelist, convert) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 return converterr("(cleanup problem)",
1250 arg, msgbuf, bufsize);
1251 }
1252 else {
1253 p = va_arg(*p_va, PyObject **);
1254 *p = arg;
1255 }
1256 break;
1257 }
1258
1259
Victor Stinner25e8ec42010-06-25 00:02:38 +00001260 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262
Victor Stinner25e8ec42010-06-25 00:02:38 +00001263 if (*format != '*')
1264 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001265 "(invalid use of 'w' format character)",
Victor Stinner25e8ec42010-06-25 00:02:38 +00001266 arg, msgbuf, bufsize);
1267 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268
Victor Stinner25e8ec42010-06-25 00:02:38 +00001269 /* Caller is interested in Py_buffer, and the object
1270 supports it directly. */
1271 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1272 PyErr_Clear();
R David Murray861470c2014-10-05 11:47:01 -04001273 return converterr("read-write bytes-like object",
1274 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 }
Victor Stinner8182b712010-07-28 00:40:58 +00001276 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1277 PyBuffer_Release((Py_buffer*)p);
1278 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1279 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001280 if (addcleanup(p, freelist, cleanup_buffer)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001281 return converterr(
1282 "(cleanup problem)",
1283 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 break;
1286 }
1287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 default:
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001289 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290
1291 }
1292
1293 *p_format = format;
1294 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001295
1296#undef FETCH_SIZE
1297#undef STORE_SIZE
1298#undef BUFFER_LEN
1299#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001300}
Guido van Rossumaa354651996-08-19 19:32:04 +00001301
Martin v. Löwis18e16552006-02-15 17:27:45 +00001302static Py_ssize_t
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001303convertbuffer(PyObject *arg, void **p, const char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001304{
Victor Stinner5cb62392010-06-06 20:27:51 +00001305 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 Py_ssize_t count;
1307 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 *errmsg = NULL;
1310 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001311 if (pb != NULL && pb->bf_releasebuffer != NULL) {
R David Murray861470c2014-10-05 11:47:01 -04001312 *errmsg = "read-only bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 return -1;
1314 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001315
Victor Stinner8182b712010-07-28 00:40:58 +00001316 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 count = view.len;
1319 *p = view.buf;
1320 PyBuffer_Release(&view);
1321 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001322}
Guido van Rossumaa354651996-08-19 19:32:04 +00001323
Martin v. Löwis423be952008-08-13 15:53:07 +00001324static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001325getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001326{
Victor Stinner8182b712010-07-28 00:40:58 +00001327 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
R David Murray861470c2014-10-05 11:47:01 -04001328 *errmsg = "bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 return -1;
1330 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001331 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001332 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001333 *errmsg = "contiguous buffer";
1334 return -1;
1335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001337}
1338
Guido van Rossumaa354651996-08-19 19:32:04 +00001339/* Support for keyword arguments donated by
1340 Geoff Philbrick <philbric@delphi.hks.com> */
1341
Tim Petersf8cd3e82001-10-27 04:26:57 +00001342/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001343int
1344PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 PyObject *keywords,
1346 const char *format,
1347 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 int retval;
1350 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if ((args == NULL || !PyTuple_Check(args)) ||
1353 (keywords != NULL && !PyDict_Check(keywords)) ||
1354 format == NULL ||
1355 kwlist == NULL)
1356 {
1357 PyErr_BadInternalCall();
1358 return 0;
1359 }
Tim Peters45772cd2001-10-27 03:58:40 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 va_start(va, kwlist);
1362 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1363 va_end(va);
1364 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001365}
1366
1367int
1368_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 PyObject *keywords,
1370 const char *format,
1371 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 int retval;
1374 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 if ((args == NULL || !PyTuple_Check(args)) ||
1377 (keywords != NULL && !PyDict_Check(keywords)) ||
1378 format == NULL ||
1379 kwlist == NULL)
1380 {
1381 PyErr_BadInternalCall();
1382 return 0;
1383 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 va_start(va, kwlist);
1386 retval = vgetargskeywords(args, keywords, format,
1387 kwlist, &va, FLAG_SIZE_T);
1388 va_end(va);
1389 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001390}
1391
1392
Brett Cannon711e7d92004-07-10 22:20:32 +00001393int
1394PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001395 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001396 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001397 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001398{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 int retval;
1400 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 if ((args == NULL || !PyTuple_Check(args)) ||
1403 (keywords != NULL && !PyDict_Check(keywords)) ||
1404 format == NULL ||
1405 kwlist == NULL)
1406 {
1407 PyErr_BadInternalCall();
1408 return 0;
1409 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001410
Benjamin Peterson0c212142016-09-20 20:39:33 -07001411 va_copy(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001414 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001416}
1417
1418int
1419_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 PyObject *keywords,
1421 const char *format,
1422 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 int retval;
1425 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 if ((args == NULL || !PyTuple_Check(args)) ||
1428 (keywords != NULL && !PyDict_Check(keywords)) ||
1429 format == NULL ||
1430 kwlist == NULL)
1431 {
1432 PyErr_BadInternalCall();
1433 return 0;
1434 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001435
Benjamin Peterson0c212142016-09-20 20:39:33 -07001436 va_copy(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 retval = vgetargskeywords(args, keywords, format,
1439 kwlist, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001440 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001442}
1443
Benjamin Petersonfb886362010-04-24 18:21:17 +00001444int
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001445_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1446 struct _PyArg_Parser *parser, ...)
1447{
1448 int retval;
1449 va_list va;
1450
1451 if ((args == NULL || !PyTuple_Check(args)) ||
1452 (keywords != NULL && !PyDict_Check(keywords)) ||
1453 parser == NULL)
1454 {
1455 PyErr_BadInternalCall();
1456 return 0;
1457 }
1458
1459 va_start(va, parser);
1460 retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1461 va_end(va);
1462 return retval;
1463}
1464
1465int
1466_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1467 struct _PyArg_Parser *parser, ...)
1468{
1469 int retval;
1470 va_list va;
1471
1472 if ((args == NULL || !PyTuple_Check(args)) ||
1473 (keywords != NULL && !PyDict_Check(keywords)) ||
1474 parser == NULL)
1475 {
1476 PyErr_BadInternalCall();
1477 return 0;
1478 }
1479
1480 va_start(va, parser);
1481 retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
1482 va_end(va);
1483 return retval;
1484}
1485
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001486int
1487_PyArg_ParseStack(PyObject **args, Py_ssize_t nargs, PyObject *kwnames,
1488 struct _PyArg_Parser *parser, ...)
1489{
1490 int retval;
1491 va_list va;
1492
1493 if ((kwnames != NULL && !PyTuple_Check(kwnames)) ||
1494 parser == NULL)
1495 {
1496 PyErr_BadInternalCall();
1497 return 0;
1498 }
1499
1500 va_start(va, parser);
1501 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1502 va_end(va);
1503 return retval;
1504}
1505
1506int
1507_PyArg_ParseStack_SizeT(PyObject **args, Py_ssize_t nargs, PyObject *kwnames,
1508 struct _PyArg_Parser *parser, ...)
1509{
1510 int retval;
1511 va_list va;
1512
1513 if ((kwnames != NULL && !PyTuple_Check(kwnames)) ||
1514 parser == NULL)
1515 {
1516 PyErr_BadInternalCall();
1517 return 0;
1518 }
1519
1520 va_start(va, parser);
1521 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T);
1522 va_end(va);
1523 return retval;
1524}
1525
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001526
1527int
1528_PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1529 struct _PyArg_Parser *parser, va_list va)
1530{
1531 int retval;
1532 va_list lva;
1533
1534 if ((args == NULL || !PyTuple_Check(args)) ||
1535 (keywords != NULL && !PyDict_Check(keywords)) ||
1536 parser == NULL)
1537 {
1538 PyErr_BadInternalCall();
1539 return 0;
1540 }
1541
Benjamin Peterson0c212142016-09-20 20:39:33 -07001542 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001543
1544 retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001545 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001546 return retval;
1547}
1548
1549int
1550_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1551 struct _PyArg_Parser *parser, va_list va)
1552{
1553 int retval;
1554 va_list lva;
1555
1556 if ((args == NULL || !PyTuple_Check(args)) ||
1557 (keywords != NULL && !PyDict_Check(keywords)) ||
1558 parser == NULL)
1559 {
1560 PyErr_BadInternalCall();
1561 return 0;
1562 }
1563
Benjamin Peterson0c212142016-09-20 20:39:33 -07001564 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001565
1566 retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001567 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001568 return retval;
1569}
1570
1571int
Benjamin Petersonfb886362010-04-24 18:21:17 +00001572PyArg_ValidateKeywordArguments(PyObject *kwargs)
1573{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001574 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 PyErr_BadInternalCall();
1576 return 0;
1577 }
1578 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1579 PyErr_SetString(PyExc_TypeError,
1580 "keyword arguments must be strings");
1581 return 0;
1582 }
1583 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001584}
1585
Christian Heimes380f7f22008-02-28 11:19:05 +00001586#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001587
Guido van Rossumaa354651996-08-19 19:32:04 +00001588static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001589vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 char msgbuf[512];
1593 int levels[32];
1594 const char *fname, *msg, *custom_msg, *keyword;
1595 int min = INT_MAX;
Larry Hastings83a9f482012-03-20 20:06:16 +00001596 int max = INT_MAX;
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001597 int i, pos, len;
1598 int skip = 0;
Victor Stinner74387f52013-11-18 01:21:12 +01001599 Py_ssize_t nargs, nkeywords;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001600 PyObject *current_arg;
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001601 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
Benjamin Peterson40be9e52014-02-11 10:09:27 -05001602 freelist_t freelist;
1603
1604 freelist.entries = static_entries;
1605 freelist.first_available = 0;
1606 freelist.entries_malloced = 0;
Tim Petersf4331c12001-10-27 00:17:34 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 assert(args != NULL && PyTuple_Check(args));
1609 assert(keywords == NULL || PyDict_Check(keywords));
1610 assert(format != NULL);
1611 assert(kwlist != NULL);
1612 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 /* grab the function name or custom error msg first (mutually exclusive) */
1615 fname = strchr(format, ':');
1616 if (fname) {
1617 fname++;
1618 custom_msg = NULL;
1619 }
1620 else {
1621 custom_msg = strchr(format,';');
1622 if (custom_msg)
1623 custom_msg++;
1624 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001625
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001626 /* scan kwlist and count the number of positional-only parameters */
1627 for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1628 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 /* scan kwlist and get greatest possible nbr of args */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001630 for (len = pos; kwlist[len]; len++) {
1631 if (!*kwlist[len]) {
1632 PyErr_SetString(PyExc_SystemError,
1633 "Empty keyword parameter name");
1634 return cleanreturn(0, &freelist);
1635 }
1636 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001637
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001638 if (len > STATIC_FREELIST_ENTRIES) {
1639 freelist.entries = PyMem_NEW(freelistentry_t, len);
1640 if (freelist.entries == NULL) {
1641 PyErr_NoMemory();
1642 return 0;
1643 }
1644 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -05001645 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 nargs = PyTuple_GET_SIZE(args);
1648 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1649 if (nargs + nkeywords > len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +01001650 PyErr_Format(PyExc_TypeError,
Victor Stinnercb29ec52013-11-18 02:05:31 +01001651 "%s%s takes at most %d argument%s (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 (fname == NULL) ? "function" : fname,
1653 (fname == NULL) ? "" : "()",
1654 len,
1655 (len == 1) ? "" : "s",
1656 nargs + nkeywords);
Benjamin Peterson01feaec2012-03-16 13:25:58 -05001657 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 }
Tim Petersc2f01122001-10-27 07:25:06 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1661 for (i = 0; i < len; i++) {
1662 keyword = kwlist[i];
1663 if (*format == '|') {
Larry Hastings83a9f482012-03-20 20:06:16 +00001664 if (min != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001665 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001666 "Invalid format string (| specified twice)");
1667 return cleanreturn(0, &freelist);
1668 }
1669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 min = i;
1671 format++;
Larry Hastings83a9f482012-03-20 20:06:16 +00001672
1673 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001674 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001675 "Invalid format string ($ before |)");
1676 return cleanreturn(0, &freelist);
1677 }
1678 }
1679 if (*format == '$') {
1680 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001681 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001682 "Invalid format string ($ specified twice)");
1683 return cleanreturn(0, &freelist);
1684 }
1685
1686 max = i;
1687 format++;
1688
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001689 if (max < pos) {
1690 PyErr_SetString(PyExc_SystemError,
1691 "Empty parameter name after $");
1692 return cleanreturn(0, &freelist);
1693 }
1694 if (skip) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001695 /* Now we know the minimal and the maximal numbers of
1696 * positional arguments and can raise an exception with
1697 * informative message (see below). */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001698 break;
1699 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001700 if (max < nargs) {
1701 PyErr_Format(PyExc_TypeError,
1702 "Function takes %s %d positional arguments"
1703 " (%d given)",
1704 (min != INT_MAX) ? "at most" : "exactly",
1705 max, nargs);
1706 return cleanreturn(0, &freelist);
1707 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 }
1709 if (IS_END_OF_FORMAT(*format)) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001710 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 "More keyword list entries (%d) than "
1712 "format specifiers (%d)", len, i);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001713 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001715 if (!skip) {
1716 current_arg = NULL;
1717 if (nkeywords && i >= pos) {
1718 current_arg = PyDict_GetItemString(keywords, keyword);
1719 if (!current_arg && PyErr_Occurred()) {
1720 return cleanreturn(0, &freelist);
1721 }
1722 }
1723 if (current_arg) {
1724 --nkeywords;
1725 if (i < nargs) {
1726 /* arg present in tuple and in dict */
1727 PyErr_Format(PyExc_TypeError,
1728 "Argument given by name ('%s') "
1729 "and position (%d)",
1730 keyword, i+1);
1731 return cleanreturn(0, &freelist);
1732 }
1733 }
1734 else if (i < nargs)
1735 current_arg = PyTuple_GET_ITEM(args, i);
1736
1737 if (current_arg) {
1738 msg = convertitem(current_arg, &format, p_va, flags,
1739 levels, msgbuf, sizeof(msgbuf), &freelist);
1740 if (msg) {
1741 seterror(i+1, msg, levels, fname, custom_msg);
1742 return cleanreturn(0, &freelist);
1743 }
1744 continue;
1745 }
1746
1747 if (i < min) {
1748 if (i < pos) {
1749 assert (min == INT_MAX);
1750 assert (max == INT_MAX);
1751 skip = 1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001752 /* At that moment we still don't know the minimal and
1753 * the maximal numbers of positional arguments. Raising
1754 * an exception is deferred until we encounter | and $
1755 * or the end of the format. */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001756 }
1757 else {
1758 PyErr_Format(PyExc_TypeError, "Required argument "
1759 "'%s' (pos %d) not found",
1760 keyword, i+1);
1761 return cleanreturn(0, &freelist);
1762 }
1763 }
1764 /* current code reports success when all required args
1765 * fulfilled and no keyword args left, with no further
1766 * validation. XXX Maybe skip this in debug build ?
1767 */
1768 if (!nkeywords && !skip) {
1769 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 }
1771 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 /* We are into optional args, skip thru to any remaining
1774 * keyword args */
1775 msg = skipitem(&format, p_va, flags);
1776 if (msg) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001777 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001779 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 }
1781 }
Tim Petersb054be42001-10-27 05:07:41 +00001782
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001783 if (skip) {
1784 PyErr_Format(PyExc_TypeError,
1785 "Function takes %s %d positional arguments"
1786 " (%d given)",
1787 (Py_MIN(pos, min) < i) ? "at least" : "exactly",
1788 Py_MIN(pos, min), nargs);
1789 return cleanreturn(0, &freelist);
1790 }
1791
Larry Hastings83a9f482012-03-20 20:06:16 +00001792 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001793 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 "more argument specifiers than keyword list entries "
1795 "(remaining format:'%s')", format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001796 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 }
Tim Petersc2f01122001-10-27 07:25:06 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 /* make sure there are no extraneous keyword arguments */
1800 if (nkeywords > 0) {
1801 PyObject *key, *value;
1802 Py_ssize_t pos = 0;
1803 while (PyDict_Next(keywords, &pos, &key, &value)) {
1804 int match = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 if (!PyUnicode_Check(key)) {
1806 PyErr_SetString(PyExc_TypeError,
1807 "keywords must be strings");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001808 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 }
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001810 for (i = 0; i < len; i++) {
Serhiy Storchaka3b73ea12016-11-16 10:19:20 +02001811 if (*kwlist[i] && _PyUnicode_EqualToASCIIString(key, kwlist[i])) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001812 match = 1;
1813 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 }
1815 }
1816 if (!match) {
1817 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001818 "'%U' is an invalid keyword "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 "argument for this function",
Victor Stinner93b55132010-05-19 00:54:06 +00001820 key);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001821 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 }
1823 }
1824 }
1825
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001826 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001827}
1828
1829
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001830/* List of static parsers. */
1831static struct _PyArg_Parser *static_arg_parsers = NULL;
1832
1833static int
1834parser_init(struct _PyArg_Parser *parser)
1835{
1836 const char * const *keywords;
1837 const char *format, *msg;
1838 int i, len, min, max, nkw;
1839 PyObject *kwtuple;
1840
1841 assert(parser->format != NULL);
1842 assert(parser->keywords != NULL);
1843 if (parser->kwtuple != NULL) {
1844 return 1;
1845 }
1846
1847 /* grab the function name or custom error msg first (mutually exclusive) */
1848 parser->fname = strchr(parser->format, ':');
1849 if (parser->fname) {
1850 parser->fname++;
1851 parser->custom_msg = NULL;
1852 }
1853 else {
1854 parser->custom_msg = strchr(parser->format,';');
1855 if (parser->custom_msg)
1856 parser->custom_msg++;
1857 }
1858
1859 keywords = parser->keywords;
1860 /* scan keywords and count the number of positional-only parameters */
1861 for (i = 0; keywords[i] && !*keywords[i]; i++) {
1862 }
1863 parser->pos = i;
1864 /* scan keywords and get greatest possible nbr of args */
1865 for (; keywords[i]; i++) {
1866 if (!*keywords[i]) {
1867 PyErr_SetString(PyExc_SystemError,
1868 "Empty keyword parameter name");
1869 return 0;
1870 }
1871 }
1872 len = i;
1873
1874 min = max = INT_MAX;
1875 format = parser->format;
1876 for (i = 0; i < len; i++) {
1877 if (*format == '|') {
1878 if (min != INT_MAX) {
1879 PyErr_SetString(PyExc_SystemError,
1880 "Invalid format string (| specified twice)");
1881 return 0;
1882 }
1883 if (max != INT_MAX) {
1884 PyErr_SetString(PyExc_SystemError,
1885 "Invalid format string ($ before |)");
1886 return 0;
1887 }
1888 min = i;
1889 format++;
1890 }
1891 if (*format == '$') {
1892 if (max != INT_MAX) {
1893 PyErr_SetString(PyExc_SystemError,
1894 "Invalid format string ($ specified twice)");
1895 return 0;
1896 }
1897 if (i < parser->pos) {
1898 PyErr_SetString(PyExc_SystemError,
1899 "Empty parameter name after $");
1900 return 0;
1901 }
1902 max = i;
1903 format++;
1904 }
1905 if (IS_END_OF_FORMAT(*format)) {
1906 PyErr_Format(PyExc_SystemError,
1907 "More keyword list entries (%d) than "
1908 "format specifiers (%d)", len, i);
1909 return 0;
1910 }
1911
1912 msg = skipitem(&format, NULL, 0);
1913 if (msg) {
1914 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1915 format);
1916 return 0;
1917 }
1918 }
1919 parser->min = Py_MIN(min, len);
1920 parser->max = Py_MIN(max, len);
1921
1922 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1923 PyErr_Format(PyExc_SystemError,
1924 "more argument specifiers than keyword list entries "
1925 "(remaining format:'%s')", format);
1926 return 0;
1927 }
1928
1929 nkw = len - parser->pos;
1930 kwtuple = PyTuple_New(nkw);
1931 if (kwtuple == NULL) {
1932 return 0;
1933 }
1934 keywords = parser->keywords + parser->pos;
1935 for (i = 0; i < nkw; i++) {
1936 PyObject *str = PyUnicode_FromString(keywords[i]);
1937 if (str == NULL) {
1938 Py_DECREF(kwtuple);
1939 return 0;
1940 }
1941 PyUnicode_InternInPlace(&str);
1942 PyTuple_SET_ITEM(kwtuple, i, str);
1943 }
1944 parser->kwtuple = kwtuple;
1945
1946 assert(parser->next == NULL);
1947 parser->next = static_arg_parsers;
1948 static_arg_parsers = parser;
1949 return 1;
1950}
1951
1952static void
1953parser_clear(struct _PyArg_Parser *parser)
1954{
1955 Py_CLEAR(parser->kwtuple);
1956}
1957
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001958static PyObject*
1959find_keyword(PyObject *kwnames, PyObject **kwstack, PyObject *key)
1960{
1961 Py_ssize_t i, nkwargs;
1962
1963 nkwargs = PyTuple_GET_SIZE(kwnames);
1964 for (i=0; i < nkwargs; i++) {
1965 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
1966
1967 /* ptr==ptr should match in most cases since keyword keys
1968 should be interned strings */
1969 if (kwname == key) {
1970 return kwstack[i];
1971 }
1972 if (!PyUnicode_Check(kwname)) {
1973 /* ignore non-string keyword keys:
1974 an error will be raised above */
1975 continue;
1976 }
1977 if (_PyUnicode_EQ(kwname, key)) {
1978 return kwstack[i];
1979 }
1980 }
1981 return NULL;
1982}
1983
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001984static int
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001985vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
1986 PyObject *keywords, PyObject *kwnames,
1987 struct _PyArg_Parser *parser,
1988 va_list *p_va, int flags)
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001989{
1990 PyObject *kwtuple;
1991 char msgbuf[512];
1992 int levels[32];
1993 const char *format;
1994 const char *msg;
1995 PyObject *keyword;
1996 int i, pos, len;
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001997 Py_ssize_t nkeywords;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001998 PyObject *current_arg;
1999 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
2000 freelist_t freelist;
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002001 PyObject **kwstack = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002002
2003 freelist.entries = static_entries;
2004 freelist.first_available = 0;
2005 freelist.entries_malloced = 0;
2006
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002007 assert(keywords == NULL || PyDict_Check(keywords));
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002008 assert(kwnames == NULL || PyTuple_Check(kwnames));
2009 assert((keywords != NULL || kwnames != NULL)
2010 || (keywords == NULL && kwnames == NULL));
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002011 assert(parser != NULL);
2012 assert(p_va != NULL);
2013
2014 if (!parser_init(parser)) {
2015 return 0;
2016 }
2017
2018 kwtuple = parser->kwtuple;
2019 pos = parser->pos;
2020 len = pos + PyTuple_GET_SIZE(kwtuple);
2021
2022 if (len > STATIC_FREELIST_ENTRIES) {
2023 freelist.entries = PyMem_NEW(freelistentry_t, len);
2024 if (freelist.entries == NULL) {
2025 PyErr_NoMemory();
2026 return 0;
2027 }
2028 freelist.entries_malloced = 1;
2029 }
2030
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002031 if (keywords != NULL) {
2032 nkeywords = PyDict_Size(keywords);
2033 }
2034 else if (kwnames != NULL) {
2035 nkeywords = PyTuple_GET_SIZE(kwnames);
2036 kwstack = args + nargs;
2037 }
2038 else {
2039 nkeywords = 0;
2040 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002041 if (nargs + nkeywords > len) {
2042 PyErr_Format(PyExc_TypeError,
2043 "%s%s takes at most %d argument%s (%zd given)",
2044 (parser->fname == NULL) ? "function" : parser->fname,
2045 (parser->fname == NULL) ? "" : "()",
2046 len,
2047 (len == 1) ? "" : "s",
2048 nargs + nkeywords);
2049 return cleanreturn(0, &freelist);
2050 }
2051 if (parser->max < nargs) {
2052 PyErr_Format(PyExc_TypeError,
2053 "Function takes %s %d positional arguments (%d given)",
2054 (parser->min != INT_MAX) ? "at most" : "exactly",
2055 parser->max, nargs);
2056 return cleanreturn(0, &freelist);
2057 }
2058
2059 format = parser->format;
2060 /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
2061 for (i = 0; i < len; i++) {
2062 keyword = (i >= pos) ? PyTuple_GET_ITEM(kwtuple, i - pos) : NULL;
2063 if (*format == '|') {
2064 format++;
2065 }
2066 if (*format == '$') {
2067 format++;
2068 }
2069 assert(!IS_END_OF_FORMAT(*format));
2070
2071 current_arg = NULL;
2072 if (nkeywords && i >= pos) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002073 if (keywords != NULL) {
2074 current_arg = PyDict_GetItem(keywords, keyword);
2075 if (!current_arg && PyErr_Occurred()) {
2076 return cleanreturn(0, &freelist);
2077 }
2078 }
2079 else {
2080 current_arg = find_keyword(kwnames, kwstack, keyword);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002081 }
2082 }
2083 if (current_arg) {
2084 --nkeywords;
2085 if (i < nargs) {
2086 /* arg present in tuple and in dict */
2087 PyErr_Format(PyExc_TypeError,
2088 "Argument given by name ('%U') "
2089 "and position (%d)",
2090 keyword, i+1);
2091 return cleanreturn(0, &freelist);
2092 }
2093 }
Victor Stinnera9efb2f2016-09-09 17:40:22 -07002094 else if (i < nargs) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002095 current_arg = args[i];
Victor Stinnera9efb2f2016-09-09 17:40:22 -07002096 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002097
2098 if (current_arg) {
2099 msg = convertitem(current_arg, &format, p_va, flags,
2100 levels, msgbuf, sizeof(msgbuf), &freelist);
2101 if (msg) {
2102 seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2103 return cleanreturn(0, &freelist);
2104 }
2105 continue;
2106 }
2107
2108 if (i < parser->min) {
2109 /* Less arguments than required */
2110 if (i < pos) {
2111 PyErr_Format(PyExc_TypeError,
2112 "Function takes %s %d positional arguments"
2113 " (%d given)",
2114 (Py_MIN(pos, parser->min) < parser->max) ? "at least" : "exactly",
2115 Py_MIN(pos, parser->min), nargs);
2116 }
2117 else {
2118 PyErr_Format(PyExc_TypeError, "Required argument "
2119 "'%U' (pos %d) not found",
2120 keyword, i+1);
2121 }
2122 return cleanreturn(0, &freelist);
2123 }
2124 /* current code reports success when all required args
2125 * fulfilled and no keyword args left, with no further
2126 * validation. XXX Maybe skip this in debug build ?
2127 */
2128 if (!nkeywords) {
2129 return cleanreturn(1, &freelist);
2130 }
2131
2132 /* We are into optional args, skip thru to any remaining
2133 * keyword args */
2134 msg = skipitem(&format, p_va, flags);
2135 assert(msg == NULL);
2136 }
2137
2138 assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2139
2140 /* make sure there are no extraneous keyword arguments */
2141 if (nkeywords > 0) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002142 if (keywords != NULL) {
2143 PyObject *key, *value;
2144 Py_ssize_t pos = 0;
2145 while (PyDict_Next(keywords, &pos, &key, &value)) {
2146 int match;
2147 if (!PyUnicode_Check(key)) {
2148 PyErr_SetString(PyExc_TypeError,
2149 "keywords must be strings");
2150 return cleanreturn(0, &freelist);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002151 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002152 match = PySequence_Contains(kwtuple, key);
2153 if (match <= 0) {
2154 if (!match) {
2155 PyErr_Format(PyExc_TypeError,
2156 "'%U' is an invalid keyword "
2157 "argument for this function",
2158 key);
2159 }
2160 return cleanreturn(0, &freelist);
2161 }
2162 }
2163 }
2164 else {
2165 Py_ssize_t j, nkwargs;
2166
2167 nkwargs = PyTuple_GET_SIZE(kwnames);
2168 for (j=0; j < nkwargs; j++) {
2169 PyObject *key = PyTuple_GET_ITEM(kwnames, j);
2170 int match;
2171
2172 if (!PyUnicode_Check(key)) {
2173 PyErr_SetString(PyExc_TypeError,
2174 "keywords must be strings");
2175 return cleanreturn(0, &freelist);
2176 }
2177
2178 match = PySequence_Contains(kwtuple, key);
2179 if (match <= 0) {
2180 if (!match) {
2181 PyErr_Format(PyExc_TypeError,
2182 "'%U' is an invalid keyword "
2183 "argument for this function",
2184 key);
2185 }
2186 return cleanreturn(0, &freelist);
2187 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002188 }
2189 }
2190 }
2191
2192 return cleanreturn(1, &freelist);
2193}
2194
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002195static int
2196vgetargskeywordsfast(PyObject *args, PyObject *keywords,
2197 struct _PyArg_Parser *parser, va_list *p_va, int flags)
2198{
2199 PyObject **stack;
2200 Py_ssize_t nargs;
2201
2202 assert(args != NULL && PyTuple_Check(args));
2203
2204 stack = &PyTuple_GET_ITEM(args, 0);
2205 nargs = PyTuple_GET_SIZE(args);
2206 return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
2207 parser, p_va, flags);
2208}
2209
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002210
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002211static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002212skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00002213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 const char *format = *p_format;
2215 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00002218
Larry Hastingsa3479012012-05-08 23:52:03 -07002219 /*
2220 * codes that take a single data pointer as an argument
2221 * (the type of the pointer is irrelevant)
2222 */
Georg Brandl6dd14612005-09-14 19:29:53 +00002223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 case 'b': /* byte -- very short int */
2225 case 'B': /* byte as bitfield */
2226 case 'h': /* short int */
2227 case 'H': /* short int as bitfield */
2228 case 'i': /* int */
2229 case 'I': /* int sized bitfield */
2230 case 'l': /* long int */
2231 case 'k': /* long int sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -07002232 case 'L': /* long long */
2233 case 'K': /* long long sized bitfield */
Larry Hastingsa3479012012-05-08 23:52:03 -07002234 case 'n': /* Py_ssize_t */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 case 'f': /* float */
2236 case 'd': /* double */
2237 case 'D': /* complex double */
2238 case 'c': /* char */
2239 case 'C': /* unicode char */
Larry Hastings10ba07a2012-05-07 02:44:50 -07002240 case 'p': /* boolean predicate */
Larry Hastingsa3479012012-05-08 23:52:03 -07002241 case 'S': /* string object */
2242 case 'Y': /* string object */
2243 case 'U': /* unicode string object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002245 if (p_va != NULL) {
2246 (void) va_arg(*p_va, void *);
2247 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 break;
2249 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 case 'e': /* string with encoding */
2254 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002255 if (p_va != NULL) {
2256 (void) va_arg(*p_va, const char *);
2257 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 if (!(*format == 's' || *format == 't'))
2259 /* after 'e', only 's' and 't' is allowed */
2260 goto err;
2261 format++;
2262 /* explicit fallthrough to string cases */
2263 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 case 's': /* string */
2266 case 'z': /* string or None */
2267 case 'y': /* bytes */
2268 case 'u': /* unicode string */
Larry Hastingsd9e4a412012-05-08 03:51:18 -07002269 case 'Z': /* unicode string or None */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 case 'w': /* buffer, read-write */
2271 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002272 if (p_va != NULL) {
2273 (void) va_arg(*p_va, char **);
2274 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 if (*format == '#') {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002276 if (p_va != NULL) {
2277 if (flags & FLAG_SIZE_T)
2278 (void) va_arg(*p_va, Py_ssize_t *);
2279 else
2280 (void) va_arg(*p_va, int *);
2281 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002282 format++;
2283 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
2284 format++;
2285 }
2286 break;
2287 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 case 'O': /* object */
2290 {
2291 if (*format == '!') {
2292 format++;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002293 if (p_va != NULL) {
2294 (void) va_arg(*p_va, PyTypeObject*);
2295 (void) va_arg(*p_va, PyObject **);
2296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 }
2298 else if (*format == '&') {
2299 typedef int (*converter)(PyObject *, void *);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002300 if (p_va != NULL) {
2301 (void) va_arg(*p_va, converter);
2302 (void) va_arg(*p_va, void *);
2303 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 format++;
2305 }
2306 else {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002307 if (p_va != NULL) {
2308 (void) va_arg(*p_va, PyObject **);
2309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 }
2311 break;
2312 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 case '(': /* bypass tuple, not handled at all previously */
2315 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002316 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 for (;;) {
2318 if (*format==')')
2319 break;
2320 if (IS_END_OF_FORMAT(*format))
2321 return "Unmatched left paren in format "
2322 "string";
2323 msg = skipitem(&format, p_va, flags);
2324 if (msg)
2325 return msg;
2326 }
2327 format++;
2328 break;
2329 }
Christian Heimes380f7f22008-02-28 11:19:05 +00002330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 case ')':
2332 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00002333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00002335err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00002337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 *p_format = format;
2341 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00002342}
Fred Drakee4616e62001-10-23 21:09:29 +00002343
2344
2345int
Martin v. Löwis76246742006-03-01 04:06:10 +00002346PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00002347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 Py_ssize_t i, l;
2349 PyObject **o;
2350 va_list vargs;
Fred Drakee4616e62001-10-23 21:09:29 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 assert(min >= 0);
2353 assert(min <= max);
2354 if (!PyTuple_Check(args)) {
2355 PyErr_SetString(PyExc_SystemError,
2356 "PyArg_UnpackTuple() argument list is not a tuple");
2357 return 0;
2358 }
2359 l = PyTuple_GET_SIZE(args);
2360 if (l < min) {
2361 if (name != NULL)
2362 PyErr_Format(
2363 PyExc_TypeError,
2364 "%s expected %s%zd arguments, got %zd",
2365 name, (min == max ? "" : "at least "), min, l);
2366 else
2367 PyErr_Format(
2368 PyExc_TypeError,
2369 "unpacked tuple should have %s%zd elements,"
2370 " but has %zd",
2371 (min == max ? "" : "at least "), min, l);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 return 0;
2373 }
Raymond Hettinger94230232016-03-26 03:02:48 -07002374 if (l == 0)
2375 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 if (l > max) {
2377 if (name != NULL)
2378 PyErr_Format(
2379 PyExc_TypeError,
2380 "%s expected %s%zd arguments, got %zd",
2381 name, (min == max ? "" : "at most "), max, l);
2382 else
2383 PyErr_Format(
2384 PyExc_TypeError,
2385 "unpacked tuple should have %s%zd elements,"
2386 " but has %zd",
2387 (min == max ? "" : "at most "), max, l);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 return 0;
2389 }
Raymond Hettinger94230232016-03-26 03:02:48 -07002390
2391#ifdef HAVE_STDARG_PROTOTYPES
2392 va_start(vargs, max);
2393#else
2394 va_start(vargs);
2395#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 for (i = 0; i < l; i++) {
2397 o = va_arg(vargs, PyObject **);
2398 *o = PyTuple_GET_ITEM(args, i);
2399 }
2400 va_end(vargs);
2401 return 1;
Fred Drakee4616e62001-10-23 21:09:29 +00002402}
Georg Brandl02c42872005-08-26 06:42:30 +00002403
2404
2405/* For type constructors that don't take keyword args
2406 *
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002407 * Sets a TypeError and returns 0 if the args/kwargs is
Thomas Wouters89f507f2006-12-13 04:49:30 +00002408 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00002409 */
2410int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002411_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00002412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 if (kw == NULL)
2414 return 1;
2415 if (!PyDict_CheckExact(kw)) {
2416 PyErr_BadInternalCall();
2417 return 0;
2418 }
2419 if (PyDict_Size(kw) == 0)
2420 return 1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
2423 funcname);
2424 return 0;
Georg Brandl02c42872005-08-26 06:42:30 +00002425}
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002426
2427
2428int
2429_PyArg_NoPositional(const char *funcname, PyObject *args)
2430{
2431 if (args == NULL)
2432 return 1;
2433 if (!PyTuple_CheckExact(args)) {
2434 PyErr_BadInternalCall();
2435 return 0;
2436 }
2437 if (PyTuple_GET_SIZE(args) == 0)
2438 return 1;
2439
2440 PyErr_Format(PyExc_TypeError, "%s does not take positional arguments",
2441 funcname);
2442 return 0;
2443}
2444
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002445void
2446_PyArg_Fini(void)
2447{
2448 struct _PyArg_Parser *tmp, *s = static_arg_parsers;
2449 while (s) {
2450 tmp = s->next;
2451 s->next = NULL;
2452 parser_clear(s);
2453 s = tmp;
2454 }
2455 static_arg_parsers = NULL;
2456}
2457
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002458#ifdef __cplusplus
2459};
2460#endif