blob: c552d0fe27a2228b0c7dde44e785bc77d2e2df3b [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 *);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +020077static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020078static 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++;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200628 const 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 }
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200900 count = convertbuffer(arg, (const void **)p, &buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 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);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200931 PyBuffer_FillInfo(p, arg, (void *)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#" */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200946 const void **p = (const void **)va_arg(*p_va, const char **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 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" */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200973 const char **p = va_arg(*p_va, const 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 Storchaka460bd0d2016-11-20 12:16:46 +02001030 if (wcslen(*p) != (size_t)len) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001031 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);
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001077 if (PyBytes_Check(arg)) {
1078 size = PyBytes_GET_SIZE(s);
1079 ptr = PyBytes_AS_STRING(s);
1080 }
1081 else {
1082 size = PyByteArray_GET_SIZE(s);
1083 ptr = PyByteArray_AS_STRING(s);
1084 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001086 else if (PyUnicode_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 /* Encode object; use default error handling */
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001088 s = PyUnicode_AsEncodedString(arg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 encoding,
1090 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (s == NULL)
1092 return converterr("(encoding failed)",
1093 arg, msgbuf, bufsize);
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001094 assert(PyBytes_Check(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 size = PyBytes_GET_SIZE(s);
1096 ptr = PyBytes_AS_STRING(s);
1097 if (ptr == NULL)
1098 ptr = "";
1099 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001100 else {
1101 return converterr(
1102 recode_strings ? "str" : "str, bytes or bytearray",
1103 arg, msgbuf, bufsize);
1104 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 /* Write output; output is guaranteed to be 0-terminated */
1107 if (*format == '#') {
1108 /* Using buffer length parameter '#':
1109
1110 - if *buffer is NULL, a new buffer of the
1111 needed size is allocated and the data
1112 copied into it; *buffer is updated to point
1113 to the new buffer; the caller is
1114 responsible for PyMem_Free()ing it after
1115 usage
1116
1117 - if *buffer is not NULL, the data is
1118 copied to *buffer; *buffer_len has to be
1119 set to the size of the buffer on input;
1120 buffer overflow is signalled with an error;
1121 buffer has to provide enough room for the
1122 encoded string plus the trailing 0-byte
1123
1124 - in both cases, *buffer_len is updated to
1125 the size of the buffer /excluding/ the
1126 trailing 0-byte
1127
1128 */
1129 FETCH_SIZE;
1130
1131 format++;
1132 if (q == NULL && q2 == NULL) {
1133 Py_DECREF(s);
1134 return converterr(
1135 "(buffer_len is NULL)",
1136 arg, msgbuf, bufsize);
1137 }
1138 if (*buffer == NULL) {
1139 *buffer = PyMem_NEW(char, size + 1);
1140 if (*buffer == NULL) {
1141 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001142 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001143 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001145 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 Py_DECREF(s);
1147 return converterr(
1148 "(cleanup problem)",
1149 arg, msgbuf, bufsize);
1150 }
1151 } else {
1152 if (size + 1 > BUFFER_LEN) {
1153 Py_DECREF(s);
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +02001154 PyErr_Format(PyExc_ValueError,
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001155 "encoded string too long "
1156 "(%zd, maximum length %zd)",
1157 (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1158 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 }
1160 }
1161 memcpy(*buffer, ptr, size+1);
1162 STORE_SIZE(size);
1163 } else {
1164 /* Using a 0-terminated buffer:
1165
1166 - the encoded string has to be 0-terminated
1167 for this variant to work; if it is not, an
1168 error raised
1169
1170 - a new buffer of the needed size is
1171 allocated and the data copied into it;
1172 *buffer is updated to point to the new
1173 buffer; the caller is responsible for
1174 PyMem_Free()ing it after usage
1175
1176 */
1177 if ((Py_ssize_t)strlen(ptr) != size) {
1178 Py_DECREF(s);
1179 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001180 "encoded string without null bytes",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 arg, msgbuf, bufsize);
1182 }
1183 *buffer = PyMem_NEW(char, size + 1);
1184 if (*buffer == NULL) {
1185 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001186 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001187 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001189 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 Py_DECREF(s);
1191 return converterr("(cleanup problem)",
1192 arg, msgbuf, bufsize);
1193 }
1194 memcpy(*buffer, ptr, size+1);
1195 }
1196 Py_DECREF(s);
1197 break;
1198 }
1199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 case 'S': { /* PyBytes object */
1201 PyObject **p = va_arg(*p_va, PyObject **);
1202 if (PyBytes_Check(arg))
1203 *p = arg;
1204 else
1205 return converterr("bytes", arg, msgbuf, bufsize);
1206 break;
1207 }
1208
1209 case 'Y': { /* PyByteArray object */
1210 PyObject **p = va_arg(*p_va, PyObject **);
1211 if (PyByteArray_Check(arg))
1212 *p = arg;
1213 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001214 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 break;
1216 }
1217
1218 case 'U': { /* PyUnicode object */
1219 PyObject **p = va_arg(*p_va, PyObject **);
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001220 if (PyUnicode_Check(arg)) {
1221 if (PyUnicode_READY(arg) == -1)
1222 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 *p = arg;
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001224 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 else
1226 return converterr("str", arg, msgbuf, bufsize);
1227 break;
1228 }
1229
1230 case 'O': { /* object */
1231 PyTypeObject *type;
1232 PyObject **p;
1233 if (*format == '!') {
1234 type = va_arg(*p_va, PyTypeObject*);
1235 p = va_arg(*p_va, PyObject **);
1236 format++;
1237 if (PyType_IsSubtype(arg->ob_type, type))
1238 *p = arg;
1239 else
1240 return converterr(type->tp_name, arg, msgbuf, bufsize);
1241
1242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 else if (*format == '&') {
1244 typedef int (*converter)(PyObject *, void *);
1245 converter convert = va_arg(*p_va, converter);
1246 void *addr = va_arg(*p_va, void *);
1247 int res;
1248 format++;
1249 if (! (res = (*convert)(arg, addr)))
1250 return converterr("(unspecified)",
1251 arg, msgbuf, bufsize);
1252 if (res == Py_CLEANUP_SUPPORTED &&
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001253 addcleanup(addr, freelist, convert) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 return converterr("(cleanup problem)",
1255 arg, msgbuf, bufsize);
1256 }
1257 else {
1258 p = va_arg(*p_va, PyObject **);
1259 *p = arg;
1260 }
1261 break;
1262 }
1263
1264
Victor Stinner25e8ec42010-06-25 00:02:38 +00001265 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267
Victor Stinner25e8ec42010-06-25 00:02:38 +00001268 if (*format != '*')
1269 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001270 "(invalid use of 'w' format character)",
Victor Stinner25e8ec42010-06-25 00:02:38 +00001271 arg, msgbuf, bufsize);
1272 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273
Victor Stinner25e8ec42010-06-25 00:02:38 +00001274 /* Caller is interested in Py_buffer, and the object
1275 supports it directly. */
1276 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1277 PyErr_Clear();
R David Murray861470c2014-10-05 11:47:01 -04001278 return converterr("read-write bytes-like object",
1279 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 }
Victor Stinner8182b712010-07-28 00:40:58 +00001281 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1282 PyBuffer_Release((Py_buffer*)p);
1283 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1284 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001285 if (addcleanup(p, freelist, cleanup_buffer)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001286 return converterr(
1287 "(cleanup problem)",
1288 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 break;
1291 }
1292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 default:
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001294 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295
1296 }
1297
1298 *p_format = format;
1299 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001300
1301#undef FETCH_SIZE
1302#undef STORE_SIZE
1303#undef BUFFER_LEN
1304#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001305}
Guido van Rossumaa354651996-08-19 19:32:04 +00001306
Martin v. Löwis18e16552006-02-15 17:27:45 +00001307static Py_ssize_t
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001308convertbuffer(PyObject *arg, const void **p, const char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001309{
Victor Stinner5cb62392010-06-06 20:27:51 +00001310 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 Py_ssize_t count;
1312 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 *errmsg = NULL;
1315 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001316 if (pb != NULL && pb->bf_releasebuffer != NULL) {
R David Murray861470c2014-10-05 11:47:01 -04001317 *errmsg = "read-only bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 return -1;
1319 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001320
Victor Stinner8182b712010-07-28 00:40:58 +00001321 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 count = view.len;
1324 *p = view.buf;
1325 PyBuffer_Release(&view);
1326 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001327}
Guido van Rossumaa354651996-08-19 19:32:04 +00001328
Martin v. Löwis423be952008-08-13 15:53:07 +00001329static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001330getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001331{
Victor Stinner8182b712010-07-28 00:40:58 +00001332 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
R David Murray861470c2014-10-05 11:47:01 -04001333 *errmsg = "bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 return -1;
1335 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001336 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001337 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001338 *errmsg = "contiguous buffer";
1339 return -1;
1340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001342}
1343
Guido van Rossumaa354651996-08-19 19:32:04 +00001344/* Support for keyword arguments donated by
1345 Geoff Philbrick <philbric@delphi.hks.com> */
1346
Tim Petersf8cd3e82001-10-27 04:26:57 +00001347/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001348int
1349PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 PyObject *keywords,
1351 const char *format,
1352 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 int retval;
1355 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 if ((args == NULL || !PyTuple_Check(args)) ||
1358 (keywords != NULL && !PyDict_Check(keywords)) ||
1359 format == NULL ||
1360 kwlist == NULL)
1361 {
1362 PyErr_BadInternalCall();
1363 return 0;
1364 }
Tim Peters45772cd2001-10-27 03:58:40 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 va_start(va, kwlist);
1367 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1368 va_end(va);
1369 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001370}
1371
1372int
1373_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 PyObject *keywords,
1375 const char *format,
1376 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 int retval;
1379 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 if ((args == NULL || !PyTuple_Check(args)) ||
1382 (keywords != NULL && !PyDict_Check(keywords)) ||
1383 format == NULL ||
1384 kwlist == NULL)
1385 {
1386 PyErr_BadInternalCall();
1387 return 0;
1388 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 va_start(va, kwlist);
1391 retval = vgetargskeywords(args, keywords, format,
1392 kwlist, &va, FLAG_SIZE_T);
1393 va_end(va);
1394 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001395}
1396
1397
Brett Cannon711e7d92004-07-10 22:20:32 +00001398int
1399PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001400 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001401 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001402 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 int retval;
1405 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 if ((args == NULL || !PyTuple_Check(args)) ||
1408 (keywords != NULL && !PyDict_Check(keywords)) ||
1409 format == NULL ||
1410 kwlist == NULL)
1411 {
1412 PyErr_BadInternalCall();
1413 return 0;
1414 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001415
Benjamin Peterson0c212142016-09-20 20:39:33 -07001416 va_copy(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001419 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001421}
1422
1423int
1424_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 PyObject *keywords,
1426 const char *format,
1427 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 int retval;
1430 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 if ((args == NULL || !PyTuple_Check(args)) ||
1433 (keywords != NULL && !PyDict_Check(keywords)) ||
1434 format == NULL ||
1435 kwlist == NULL)
1436 {
1437 PyErr_BadInternalCall();
1438 return 0;
1439 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001440
Benjamin Peterson0c212142016-09-20 20:39:33 -07001441 va_copy(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 retval = vgetargskeywords(args, keywords, format,
1444 kwlist, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001445 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001447}
1448
Benjamin Petersonfb886362010-04-24 18:21:17 +00001449int
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001450_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1451 struct _PyArg_Parser *parser, ...)
1452{
1453 int retval;
1454 va_list va;
1455
1456 if ((args == NULL || !PyTuple_Check(args)) ||
1457 (keywords != NULL && !PyDict_Check(keywords)) ||
1458 parser == NULL)
1459 {
1460 PyErr_BadInternalCall();
1461 return 0;
1462 }
1463
1464 va_start(va, parser);
1465 retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1466 va_end(va);
1467 return retval;
1468}
1469
1470int
1471_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1472 struct _PyArg_Parser *parser, ...)
1473{
1474 int retval;
1475 va_list va;
1476
1477 if ((args == NULL || !PyTuple_Check(args)) ||
1478 (keywords != NULL && !PyDict_Check(keywords)) ||
1479 parser == NULL)
1480 {
1481 PyErr_BadInternalCall();
1482 return 0;
1483 }
1484
1485 va_start(va, parser);
1486 retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
1487 va_end(va);
1488 return retval;
1489}
1490
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001491int
1492_PyArg_ParseStack(PyObject **args, Py_ssize_t nargs, PyObject *kwnames,
1493 struct _PyArg_Parser *parser, ...)
1494{
1495 int retval;
1496 va_list va;
1497
1498 if ((kwnames != NULL && !PyTuple_Check(kwnames)) ||
1499 parser == NULL)
1500 {
1501 PyErr_BadInternalCall();
1502 return 0;
1503 }
1504
1505 va_start(va, parser);
1506 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1507 va_end(va);
1508 return retval;
1509}
1510
1511int
1512_PyArg_ParseStack_SizeT(PyObject **args, Py_ssize_t nargs, PyObject *kwnames,
1513 struct _PyArg_Parser *parser, ...)
1514{
1515 int retval;
1516 va_list va;
1517
1518 if ((kwnames != NULL && !PyTuple_Check(kwnames)) ||
1519 parser == NULL)
1520 {
1521 PyErr_BadInternalCall();
1522 return 0;
1523 }
1524
1525 va_start(va, parser);
1526 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T);
1527 va_end(va);
1528 return retval;
1529}
1530
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001531
1532int
1533_PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1534 struct _PyArg_Parser *parser, va_list va)
1535{
1536 int retval;
1537 va_list lva;
1538
1539 if ((args == NULL || !PyTuple_Check(args)) ||
1540 (keywords != NULL && !PyDict_Check(keywords)) ||
1541 parser == NULL)
1542 {
1543 PyErr_BadInternalCall();
1544 return 0;
1545 }
1546
Benjamin Peterson0c212142016-09-20 20:39:33 -07001547 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001548
1549 retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001550 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001551 return retval;
1552}
1553
1554int
1555_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1556 struct _PyArg_Parser *parser, va_list va)
1557{
1558 int retval;
1559 va_list lva;
1560
1561 if ((args == NULL || !PyTuple_Check(args)) ||
1562 (keywords != NULL && !PyDict_Check(keywords)) ||
1563 parser == NULL)
1564 {
1565 PyErr_BadInternalCall();
1566 return 0;
1567 }
1568
Benjamin Peterson0c212142016-09-20 20:39:33 -07001569 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001570
1571 retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001572 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001573 return retval;
1574}
1575
1576int
Benjamin Petersonfb886362010-04-24 18:21:17 +00001577PyArg_ValidateKeywordArguments(PyObject *kwargs)
1578{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001579 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 PyErr_BadInternalCall();
1581 return 0;
1582 }
1583 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1584 PyErr_SetString(PyExc_TypeError,
1585 "keyword arguments must be strings");
1586 return 0;
1587 }
1588 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001589}
1590
Christian Heimes380f7f22008-02-28 11:19:05 +00001591#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001592
Guido van Rossumaa354651996-08-19 19:32:04 +00001593static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001594vgetargskeywords(PyObject *args, PyObject *keywords, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 char msgbuf[512];
1598 int levels[32];
1599 const char *fname, *msg, *custom_msg, *keyword;
1600 int min = INT_MAX;
Larry Hastings83a9f482012-03-20 20:06:16 +00001601 int max = INT_MAX;
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001602 int i, pos, len;
1603 int skip = 0;
Victor Stinner74387f52013-11-18 01:21:12 +01001604 Py_ssize_t nargs, nkeywords;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001605 PyObject *current_arg;
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001606 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
Benjamin Peterson40be9e52014-02-11 10:09:27 -05001607 freelist_t freelist;
1608
1609 freelist.entries = static_entries;
1610 freelist.first_available = 0;
1611 freelist.entries_malloced = 0;
Tim Petersf4331c12001-10-27 00:17:34 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 assert(args != NULL && PyTuple_Check(args));
1614 assert(keywords == NULL || PyDict_Check(keywords));
1615 assert(format != NULL);
1616 assert(kwlist != NULL);
1617 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 /* grab the function name or custom error msg first (mutually exclusive) */
1620 fname = strchr(format, ':');
1621 if (fname) {
1622 fname++;
1623 custom_msg = NULL;
1624 }
1625 else {
1626 custom_msg = strchr(format,';');
1627 if (custom_msg)
1628 custom_msg++;
1629 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001630
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001631 /* scan kwlist and count the number of positional-only parameters */
1632 for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1633 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 /* scan kwlist and get greatest possible nbr of args */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001635 for (len = pos; kwlist[len]; len++) {
1636 if (!*kwlist[len]) {
1637 PyErr_SetString(PyExc_SystemError,
1638 "Empty keyword parameter name");
1639 return cleanreturn(0, &freelist);
1640 }
1641 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001642
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001643 if (len > STATIC_FREELIST_ENTRIES) {
1644 freelist.entries = PyMem_NEW(freelistentry_t, len);
1645 if (freelist.entries == NULL) {
1646 PyErr_NoMemory();
1647 return 0;
1648 }
1649 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -05001650 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 nargs = PyTuple_GET_SIZE(args);
1653 nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords);
1654 if (nargs + nkeywords > len) {
Victor Stinner6ced7c42011-03-21 18:15:42 +01001655 PyErr_Format(PyExc_TypeError,
Victor Stinnercb29ec52013-11-18 02:05:31 +01001656 "%s%s takes at most %d argument%s (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 (fname == NULL) ? "function" : fname,
1658 (fname == NULL) ? "" : "()",
1659 len,
1660 (len == 1) ? "" : "s",
1661 nargs + nkeywords);
Benjamin Peterson01feaec2012-03-16 13:25:58 -05001662 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 }
Tim Petersc2f01122001-10-27 07:25:06 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1666 for (i = 0; i < len; i++) {
1667 keyword = kwlist[i];
1668 if (*format == '|') {
Larry Hastings83a9f482012-03-20 20:06:16 +00001669 if (min != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001670 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001671 "Invalid format string (| specified twice)");
1672 return cleanreturn(0, &freelist);
1673 }
1674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 min = i;
1676 format++;
Larry Hastings83a9f482012-03-20 20:06:16 +00001677
1678 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001679 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001680 "Invalid format string ($ before |)");
1681 return cleanreturn(0, &freelist);
1682 }
1683 }
1684 if (*format == '$') {
1685 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001686 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001687 "Invalid format string ($ specified twice)");
1688 return cleanreturn(0, &freelist);
1689 }
1690
1691 max = i;
1692 format++;
1693
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001694 if (max < pos) {
1695 PyErr_SetString(PyExc_SystemError,
1696 "Empty parameter name after $");
1697 return cleanreturn(0, &freelist);
1698 }
1699 if (skip) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001700 /* Now we know the minimal and the maximal numbers of
1701 * positional arguments and can raise an exception with
1702 * informative message (see below). */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001703 break;
1704 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001705 if (max < nargs) {
1706 PyErr_Format(PyExc_TypeError,
1707 "Function takes %s %d positional arguments"
1708 " (%d given)",
1709 (min != INT_MAX) ? "at most" : "exactly",
1710 max, nargs);
1711 return cleanreturn(0, &freelist);
1712 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 }
1714 if (IS_END_OF_FORMAT(*format)) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001715 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 "More keyword list entries (%d) than "
1717 "format specifiers (%d)", len, i);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001718 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001720 if (!skip) {
1721 current_arg = NULL;
1722 if (nkeywords && i >= pos) {
1723 current_arg = PyDict_GetItemString(keywords, keyword);
1724 if (!current_arg && PyErr_Occurred()) {
1725 return cleanreturn(0, &freelist);
1726 }
1727 }
1728 if (current_arg) {
1729 --nkeywords;
1730 if (i < nargs) {
1731 /* arg present in tuple and in dict */
1732 PyErr_Format(PyExc_TypeError,
1733 "Argument given by name ('%s') "
1734 "and position (%d)",
1735 keyword, i+1);
1736 return cleanreturn(0, &freelist);
1737 }
1738 }
1739 else if (i < nargs)
1740 current_arg = PyTuple_GET_ITEM(args, i);
1741
1742 if (current_arg) {
1743 msg = convertitem(current_arg, &format, p_va, flags,
1744 levels, msgbuf, sizeof(msgbuf), &freelist);
1745 if (msg) {
1746 seterror(i+1, msg, levels, fname, custom_msg);
1747 return cleanreturn(0, &freelist);
1748 }
1749 continue;
1750 }
1751
1752 if (i < min) {
1753 if (i < pos) {
1754 assert (min == INT_MAX);
1755 assert (max == INT_MAX);
1756 skip = 1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001757 /* At that moment we still don't know the minimal and
1758 * the maximal numbers of positional arguments. Raising
1759 * an exception is deferred until we encounter | and $
1760 * or the end of the format. */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001761 }
1762 else {
1763 PyErr_Format(PyExc_TypeError, "Required argument "
1764 "'%s' (pos %d) not found",
1765 keyword, i+1);
1766 return cleanreturn(0, &freelist);
1767 }
1768 }
1769 /* current code reports success when all required args
1770 * fulfilled and no keyword args left, with no further
1771 * validation. XXX Maybe skip this in debug build ?
1772 */
1773 if (!nkeywords && !skip) {
1774 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 }
1776 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 /* We are into optional args, skip thru to any remaining
1779 * keyword args */
1780 msg = skipitem(&format, p_va, flags);
1781 if (msg) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001782 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001784 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 }
1786 }
Tim Petersb054be42001-10-27 05:07:41 +00001787
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001788 if (skip) {
1789 PyErr_Format(PyExc_TypeError,
1790 "Function takes %s %d positional arguments"
1791 " (%d given)",
1792 (Py_MIN(pos, min) < i) ? "at least" : "exactly",
1793 Py_MIN(pos, min), nargs);
1794 return cleanreturn(0, &freelist);
1795 }
1796
Larry Hastings83a9f482012-03-20 20:06:16 +00001797 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001798 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 "more argument specifiers than keyword list entries "
1800 "(remaining format:'%s')", format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001801 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 }
Tim Petersc2f01122001-10-27 07:25:06 +00001803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 /* make sure there are no extraneous keyword arguments */
1805 if (nkeywords > 0) {
1806 PyObject *key, *value;
1807 Py_ssize_t pos = 0;
1808 while (PyDict_Next(keywords, &pos, &key, &value)) {
1809 int match = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 if (!PyUnicode_Check(key)) {
1811 PyErr_SetString(PyExc_TypeError,
1812 "keywords must be strings");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001813 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 }
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001815 for (i = 0; i < len; i++) {
Serhiy Storchaka3b73ea12016-11-16 10:19:20 +02001816 if (*kwlist[i] && _PyUnicode_EqualToASCIIString(key, kwlist[i])) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001817 match = 1;
1818 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 }
1820 }
1821 if (!match) {
1822 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001823 "'%U' is an invalid keyword "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 "argument for this function",
Victor Stinner93b55132010-05-19 00:54:06 +00001825 key);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001826 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 }
1828 }
1829 }
1830
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001831 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001832}
1833
1834
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001835/* List of static parsers. */
1836static struct _PyArg_Parser *static_arg_parsers = NULL;
1837
1838static int
1839parser_init(struct _PyArg_Parser *parser)
1840{
1841 const char * const *keywords;
1842 const char *format, *msg;
1843 int i, len, min, max, nkw;
1844 PyObject *kwtuple;
1845
1846 assert(parser->format != NULL);
1847 assert(parser->keywords != NULL);
1848 if (parser->kwtuple != NULL) {
1849 return 1;
1850 }
1851
1852 /* grab the function name or custom error msg first (mutually exclusive) */
1853 parser->fname = strchr(parser->format, ':');
1854 if (parser->fname) {
1855 parser->fname++;
1856 parser->custom_msg = NULL;
1857 }
1858 else {
1859 parser->custom_msg = strchr(parser->format,';');
1860 if (parser->custom_msg)
1861 parser->custom_msg++;
1862 }
1863
1864 keywords = parser->keywords;
1865 /* scan keywords and count the number of positional-only parameters */
1866 for (i = 0; keywords[i] && !*keywords[i]; i++) {
1867 }
1868 parser->pos = i;
1869 /* scan keywords and get greatest possible nbr of args */
1870 for (; keywords[i]; i++) {
1871 if (!*keywords[i]) {
1872 PyErr_SetString(PyExc_SystemError,
1873 "Empty keyword parameter name");
1874 return 0;
1875 }
1876 }
1877 len = i;
1878
1879 min = max = INT_MAX;
1880 format = parser->format;
1881 for (i = 0; i < len; i++) {
1882 if (*format == '|') {
1883 if (min != INT_MAX) {
1884 PyErr_SetString(PyExc_SystemError,
1885 "Invalid format string (| specified twice)");
1886 return 0;
1887 }
1888 if (max != INT_MAX) {
1889 PyErr_SetString(PyExc_SystemError,
1890 "Invalid format string ($ before |)");
1891 return 0;
1892 }
1893 min = i;
1894 format++;
1895 }
1896 if (*format == '$') {
1897 if (max != INT_MAX) {
1898 PyErr_SetString(PyExc_SystemError,
1899 "Invalid format string ($ specified twice)");
1900 return 0;
1901 }
1902 if (i < parser->pos) {
1903 PyErr_SetString(PyExc_SystemError,
1904 "Empty parameter name after $");
1905 return 0;
1906 }
1907 max = i;
1908 format++;
1909 }
1910 if (IS_END_OF_FORMAT(*format)) {
1911 PyErr_Format(PyExc_SystemError,
1912 "More keyword list entries (%d) than "
1913 "format specifiers (%d)", len, i);
1914 return 0;
1915 }
1916
1917 msg = skipitem(&format, NULL, 0);
1918 if (msg) {
1919 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1920 format);
1921 return 0;
1922 }
1923 }
1924 parser->min = Py_MIN(min, len);
1925 parser->max = Py_MIN(max, len);
1926
1927 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1928 PyErr_Format(PyExc_SystemError,
1929 "more argument specifiers than keyword list entries "
1930 "(remaining format:'%s')", format);
1931 return 0;
1932 }
1933
1934 nkw = len - parser->pos;
1935 kwtuple = PyTuple_New(nkw);
1936 if (kwtuple == NULL) {
1937 return 0;
1938 }
1939 keywords = parser->keywords + parser->pos;
1940 for (i = 0; i < nkw; i++) {
1941 PyObject *str = PyUnicode_FromString(keywords[i]);
1942 if (str == NULL) {
1943 Py_DECREF(kwtuple);
1944 return 0;
1945 }
1946 PyUnicode_InternInPlace(&str);
1947 PyTuple_SET_ITEM(kwtuple, i, str);
1948 }
1949 parser->kwtuple = kwtuple;
1950
1951 assert(parser->next == NULL);
1952 parser->next = static_arg_parsers;
1953 static_arg_parsers = parser;
1954 return 1;
1955}
1956
1957static void
1958parser_clear(struct _PyArg_Parser *parser)
1959{
1960 Py_CLEAR(parser->kwtuple);
1961}
1962
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001963static PyObject*
1964find_keyword(PyObject *kwnames, PyObject **kwstack, PyObject *key)
1965{
1966 Py_ssize_t i, nkwargs;
1967
1968 nkwargs = PyTuple_GET_SIZE(kwnames);
1969 for (i=0; i < nkwargs; i++) {
1970 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
1971
1972 /* ptr==ptr should match in most cases since keyword keys
1973 should be interned strings */
1974 if (kwname == key) {
1975 return kwstack[i];
1976 }
1977 if (!PyUnicode_Check(kwname)) {
1978 /* ignore non-string keyword keys:
1979 an error will be raised above */
1980 continue;
1981 }
1982 if (_PyUnicode_EQ(kwname, key)) {
1983 return kwstack[i];
1984 }
1985 }
1986 return NULL;
1987}
1988
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001989static int
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001990vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
1991 PyObject *keywords, PyObject *kwnames,
1992 struct _PyArg_Parser *parser,
1993 va_list *p_va, int flags)
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001994{
1995 PyObject *kwtuple;
1996 char msgbuf[512];
1997 int levels[32];
1998 const char *format;
1999 const char *msg;
2000 PyObject *keyword;
2001 int i, pos, len;
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002002 Py_ssize_t nkeywords;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002003 PyObject *current_arg;
2004 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
2005 freelist_t freelist;
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002006 PyObject **kwstack = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002007
2008 freelist.entries = static_entries;
2009 freelist.first_available = 0;
2010 freelist.entries_malloced = 0;
2011
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002012 assert(keywords == NULL || PyDict_Check(keywords));
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002013 assert(kwnames == NULL || PyTuple_Check(kwnames));
2014 assert((keywords != NULL || kwnames != NULL)
2015 || (keywords == NULL && kwnames == NULL));
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002016 assert(parser != NULL);
2017 assert(p_va != NULL);
2018
2019 if (!parser_init(parser)) {
2020 return 0;
2021 }
2022
2023 kwtuple = parser->kwtuple;
2024 pos = parser->pos;
2025 len = pos + PyTuple_GET_SIZE(kwtuple);
2026
2027 if (len > STATIC_FREELIST_ENTRIES) {
2028 freelist.entries = PyMem_NEW(freelistentry_t, len);
2029 if (freelist.entries == NULL) {
2030 PyErr_NoMemory();
2031 return 0;
2032 }
2033 freelist.entries_malloced = 1;
2034 }
2035
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002036 if (keywords != NULL) {
2037 nkeywords = PyDict_Size(keywords);
2038 }
2039 else if (kwnames != NULL) {
2040 nkeywords = PyTuple_GET_SIZE(kwnames);
2041 kwstack = args + nargs;
2042 }
2043 else {
2044 nkeywords = 0;
2045 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002046 if (nargs + nkeywords > len) {
2047 PyErr_Format(PyExc_TypeError,
2048 "%s%s takes at most %d argument%s (%zd given)",
2049 (parser->fname == NULL) ? "function" : parser->fname,
2050 (parser->fname == NULL) ? "" : "()",
2051 len,
2052 (len == 1) ? "" : "s",
2053 nargs + nkeywords);
2054 return cleanreturn(0, &freelist);
2055 }
2056 if (parser->max < nargs) {
2057 PyErr_Format(PyExc_TypeError,
2058 "Function takes %s %d positional arguments (%d given)",
2059 (parser->min != INT_MAX) ? "at most" : "exactly",
2060 parser->max, nargs);
2061 return cleanreturn(0, &freelist);
2062 }
2063
2064 format = parser->format;
2065 /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
2066 for (i = 0; i < len; i++) {
2067 keyword = (i >= pos) ? PyTuple_GET_ITEM(kwtuple, i - pos) : NULL;
2068 if (*format == '|') {
2069 format++;
2070 }
2071 if (*format == '$') {
2072 format++;
2073 }
2074 assert(!IS_END_OF_FORMAT(*format));
2075
2076 current_arg = NULL;
2077 if (nkeywords && i >= pos) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002078 if (keywords != NULL) {
2079 current_arg = PyDict_GetItem(keywords, keyword);
2080 if (!current_arg && PyErr_Occurred()) {
2081 return cleanreturn(0, &freelist);
2082 }
2083 }
2084 else {
2085 current_arg = find_keyword(kwnames, kwstack, keyword);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002086 }
2087 }
2088 if (current_arg) {
2089 --nkeywords;
2090 if (i < nargs) {
2091 /* arg present in tuple and in dict */
2092 PyErr_Format(PyExc_TypeError,
2093 "Argument given by name ('%U') "
2094 "and position (%d)",
2095 keyword, i+1);
2096 return cleanreturn(0, &freelist);
2097 }
2098 }
Victor Stinnera9efb2f2016-09-09 17:40:22 -07002099 else if (i < nargs) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002100 current_arg = args[i];
Victor Stinnera9efb2f2016-09-09 17:40:22 -07002101 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002102
2103 if (current_arg) {
2104 msg = convertitem(current_arg, &format, p_va, flags,
2105 levels, msgbuf, sizeof(msgbuf), &freelist);
2106 if (msg) {
2107 seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2108 return cleanreturn(0, &freelist);
2109 }
2110 continue;
2111 }
2112
2113 if (i < parser->min) {
2114 /* Less arguments than required */
2115 if (i < pos) {
2116 PyErr_Format(PyExc_TypeError,
2117 "Function takes %s %d positional arguments"
2118 " (%d given)",
2119 (Py_MIN(pos, parser->min) < parser->max) ? "at least" : "exactly",
2120 Py_MIN(pos, parser->min), nargs);
2121 }
2122 else {
2123 PyErr_Format(PyExc_TypeError, "Required argument "
2124 "'%U' (pos %d) not found",
2125 keyword, i+1);
2126 }
2127 return cleanreturn(0, &freelist);
2128 }
2129 /* current code reports success when all required args
2130 * fulfilled and no keyword args left, with no further
2131 * validation. XXX Maybe skip this in debug build ?
2132 */
2133 if (!nkeywords) {
2134 return cleanreturn(1, &freelist);
2135 }
2136
2137 /* We are into optional args, skip thru to any remaining
2138 * keyword args */
2139 msg = skipitem(&format, p_va, flags);
2140 assert(msg == NULL);
2141 }
2142
2143 assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2144
2145 /* make sure there are no extraneous keyword arguments */
2146 if (nkeywords > 0) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002147 if (keywords != NULL) {
2148 PyObject *key, *value;
2149 Py_ssize_t pos = 0;
2150 while (PyDict_Next(keywords, &pos, &key, &value)) {
2151 int match;
2152 if (!PyUnicode_Check(key)) {
2153 PyErr_SetString(PyExc_TypeError,
2154 "keywords must be strings");
2155 return cleanreturn(0, &freelist);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002156 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002157 match = PySequence_Contains(kwtuple, key);
2158 if (match <= 0) {
2159 if (!match) {
2160 PyErr_Format(PyExc_TypeError,
2161 "'%U' is an invalid keyword "
2162 "argument for this function",
2163 key);
2164 }
2165 return cleanreturn(0, &freelist);
2166 }
2167 }
2168 }
2169 else {
2170 Py_ssize_t j, nkwargs;
2171
2172 nkwargs = PyTuple_GET_SIZE(kwnames);
2173 for (j=0; j < nkwargs; j++) {
2174 PyObject *key = PyTuple_GET_ITEM(kwnames, j);
2175 int match;
2176
2177 if (!PyUnicode_Check(key)) {
2178 PyErr_SetString(PyExc_TypeError,
2179 "keywords must be strings");
2180 return cleanreturn(0, &freelist);
2181 }
2182
2183 match = PySequence_Contains(kwtuple, key);
2184 if (match <= 0) {
2185 if (!match) {
2186 PyErr_Format(PyExc_TypeError,
2187 "'%U' is an invalid keyword "
2188 "argument for this function",
2189 key);
2190 }
2191 return cleanreturn(0, &freelist);
2192 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002193 }
2194 }
2195 }
2196
2197 return cleanreturn(1, &freelist);
2198}
2199
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002200static int
2201vgetargskeywordsfast(PyObject *args, PyObject *keywords,
2202 struct _PyArg_Parser *parser, va_list *p_va, int flags)
2203{
2204 PyObject **stack;
2205 Py_ssize_t nargs;
2206
2207 assert(args != NULL && PyTuple_Check(args));
2208
2209 stack = &PyTuple_GET_ITEM(args, 0);
2210 nargs = PyTuple_GET_SIZE(args);
2211 return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
2212 parser, p_va, flags);
2213}
2214
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002215
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002216static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002217skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00002218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 const char *format = *p_format;
2220 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00002223
Larry Hastingsa3479012012-05-08 23:52:03 -07002224 /*
2225 * codes that take a single data pointer as an argument
2226 * (the type of the pointer is irrelevant)
2227 */
Georg Brandl6dd14612005-09-14 19:29:53 +00002228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 case 'b': /* byte -- very short int */
2230 case 'B': /* byte as bitfield */
2231 case 'h': /* short int */
2232 case 'H': /* short int as bitfield */
2233 case 'i': /* int */
2234 case 'I': /* int sized bitfield */
2235 case 'l': /* long int */
2236 case 'k': /* long int sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -07002237 case 'L': /* long long */
2238 case 'K': /* long long sized bitfield */
Larry Hastingsa3479012012-05-08 23:52:03 -07002239 case 'n': /* Py_ssize_t */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002240 case 'f': /* float */
2241 case 'd': /* double */
2242 case 'D': /* complex double */
2243 case 'c': /* char */
2244 case 'C': /* unicode char */
Larry Hastings10ba07a2012-05-07 02:44:50 -07002245 case 'p': /* boolean predicate */
Larry Hastingsa3479012012-05-08 23:52:03 -07002246 case 'S': /* string object */
2247 case 'Y': /* string object */
2248 case 'U': /* unicode string object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002250 if (p_va != NULL) {
2251 (void) va_arg(*p_va, void *);
2252 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 break;
2254 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 case 'e': /* string with encoding */
2259 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002260 if (p_va != NULL) {
2261 (void) va_arg(*p_va, const char *);
2262 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 if (!(*format == 's' || *format == 't'))
2264 /* after 'e', only 's' and 't' is allowed */
2265 goto err;
2266 format++;
2267 /* explicit fallthrough to string cases */
2268 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 case 's': /* string */
2271 case 'z': /* string or None */
2272 case 'y': /* bytes */
2273 case 'u': /* unicode string */
Larry Hastingsd9e4a412012-05-08 03:51:18 -07002274 case 'Z': /* unicode string or None */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 case 'w': /* buffer, read-write */
2276 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002277 if (p_va != NULL) {
2278 (void) va_arg(*p_va, char **);
2279 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 if (*format == '#') {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002281 if (p_va != NULL) {
2282 if (flags & FLAG_SIZE_T)
2283 (void) va_arg(*p_va, Py_ssize_t *);
2284 else
2285 (void) va_arg(*p_va, int *);
2286 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 format++;
2288 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
2289 format++;
2290 }
2291 break;
2292 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 case 'O': /* object */
2295 {
2296 if (*format == '!') {
2297 format++;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002298 if (p_va != NULL) {
2299 (void) va_arg(*p_va, PyTypeObject*);
2300 (void) va_arg(*p_va, PyObject **);
2301 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 }
2303 else if (*format == '&') {
2304 typedef int (*converter)(PyObject *, void *);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002305 if (p_va != NULL) {
2306 (void) va_arg(*p_va, converter);
2307 (void) va_arg(*p_va, void *);
2308 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 format++;
2310 }
2311 else {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002312 if (p_va != NULL) {
2313 (void) va_arg(*p_va, PyObject **);
2314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 }
2316 break;
2317 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 case '(': /* bypass tuple, not handled at all previously */
2320 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002321 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 for (;;) {
2323 if (*format==')')
2324 break;
2325 if (IS_END_OF_FORMAT(*format))
2326 return "Unmatched left paren in format "
2327 "string";
2328 msg = skipitem(&format, p_va, flags);
2329 if (msg)
2330 return msg;
2331 }
2332 format++;
2333 break;
2334 }
Christian Heimes380f7f22008-02-28 11:19:05 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 case ')':
2337 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00002338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00002340err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 *p_format = format;
2346 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00002347}
Fred Drakee4616e62001-10-23 21:09:29 +00002348
2349
2350int
Martin v. Löwis76246742006-03-01 04:06:10 +00002351PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
Fred Drakee4616e62001-10-23 21:09:29 +00002352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 Py_ssize_t i, l;
2354 PyObject **o;
2355 va_list vargs;
Fred Drakee4616e62001-10-23 21:09:29 +00002356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 assert(min >= 0);
2358 assert(min <= max);
2359 if (!PyTuple_Check(args)) {
2360 PyErr_SetString(PyExc_SystemError,
2361 "PyArg_UnpackTuple() argument list is not a tuple");
2362 return 0;
2363 }
2364 l = PyTuple_GET_SIZE(args);
2365 if (l < min) {
2366 if (name != NULL)
2367 PyErr_Format(
2368 PyExc_TypeError,
2369 "%s expected %s%zd arguments, got %zd",
2370 name, (min == max ? "" : "at least "), min, l);
2371 else
2372 PyErr_Format(
2373 PyExc_TypeError,
2374 "unpacked tuple should have %s%zd elements,"
2375 " but has %zd",
2376 (min == max ? "" : "at least "), min, l);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002377 return 0;
2378 }
Raymond Hettinger94230232016-03-26 03:02:48 -07002379 if (l == 0)
2380 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 if (l > max) {
2382 if (name != NULL)
2383 PyErr_Format(
2384 PyExc_TypeError,
2385 "%s expected %s%zd arguments, got %zd",
2386 name, (min == max ? "" : "at most "), max, l);
2387 else
2388 PyErr_Format(
2389 PyExc_TypeError,
2390 "unpacked tuple should have %s%zd elements,"
2391 " but has %zd",
2392 (min == max ? "" : "at most "), max, l);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 return 0;
2394 }
Raymond Hettinger94230232016-03-26 03:02:48 -07002395
2396#ifdef HAVE_STDARG_PROTOTYPES
2397 va_start(vargs, max);
2398#else
2399 va_start(vargs);
2400#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 for (i = 0; i < l; i++) {
2402 o = va_arg(vargs, PyObject **);
2403 *o = PyTuple_GET_ITEM(args, i);
2404 }
2405 va_end(vargs);
2406 return 1;
Fred Drakee4616e62001-10-23 21:09:29 +00002407}
Georg Brandl02c42872005-08-26 06:42:30 +00002408
2409
2410/* For type constructors that don't take keyword args
2411 *
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002412 * Sets a TypeError and returns 0 if the args/kwargs is
Thomas Wouters89f507f2006-12-13 04:49:30 +00002413 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00002414 */
2415int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002416_PyArg_NoKeywords(const char *funcname, PyObject *kw)
Georg Brandl02c42872005-08-26 06:42:30 +00002417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 if (kw == NULL)
2419 return 1;
2420 if (!PyDict_CheckExact(kw)) {
2421 PyErr_BadInternalCall();
2422 return 0;
2423 }
2424 if (PyDict_Size(kw) == 0)
2425 return 1;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments",
2428 funcname);
2429 return 0;
Georg Brandl02c42872005-08-26 06:42:30 +00002430}
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002431
2432
2433int
2434_PyArg_NoPositional(const char *funcname, PyObject *args)
2435{
2436 if (args == NULL)
2437 return 1;
2438 if (!PyTuple_CheckExact(args)) {
2439 PyErr_BadInternalCall();
2440 return 0;
2441 }
2442 if (PyTuple_GET_SIZE(args) == 0)
2443 return 1;
2444
2445 PyErr_Format(PyExc_TypeError, "%s does not take positional arguments",
2446 funcname);
2447 return 0;
2448}
2449
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002450void
2451_PyArg_Fini(void)
2452{
2453 struct _PyArg_Parser *tmp, *s = static_arg_parsers;
2454 while (s) {
2455 tmp = s->next;
2456 s->next = NULL;
2457 parser_clear(s);
2458 s = tmp;
2459 }
2460 static_arg_parsers = NULL;
2461}
2462
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002463#ifdef __cplusplus
2464};
2465#endif