blob: 0b155a170f90855e2ee98fff42338fbfe80582ae [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>
Benjamin Petersona853a8b2017-09-07 11:13:59 -07007#include <float.h>
Guido van Rossumc1d50531996-08-21 23:38:24 +00008
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00009
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010#ifdef __cplusplus
Guido van Rossum98297ee2007-11-06 21:34:58 +000011extern "C" {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000012#endif
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000013int PyArg_Parse(PyObject *, const char *, ...);
14int PyArg_ParseTuple(PyObject *, const char *, ...);
15int PyArg_VaParse(PyObject *, const char *, va_list);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000016
Tim Petersdbd9ba62000-07-09 03:09:57 +000017int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 const char *, char **, ...);
Brett Cannon711e7d92004-07-10 22:20:32 +000019int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 const char *, char **, va_list);
Brett Cannon711e7d92004-07-10 22:20:32 +000021
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030022int _PyArg_ParseTupleAndKeywordsFast(PyObject *, PyObject *,
23 struct _PyArg_Parser *, ...);
24int _PyArg_VaParseTupleAndKeywordsFast(PyObject *, PyObject *,
25 struct _PyArg_Parser *, va_list);
26
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027#ifdef HAVE_DECLSPEC_DLL
28/* Export functions */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020029PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...);
Victor Stinner6518a932017-01-17 01:29:49 +010030PyAPI_FUNC(int) _PyArg_ParseStack_SizeT(PyObject **args, Py_ssize_t nargs,
31 const char *format, ...);
Victor Stinner3e1fad62017-01-17 01:29:01 +010032PyAPI_FUNC(int) _PyArg_ParseStackAndKeywords_SizeT(PyObject **args, Py_ssize_t nargs, PyObject *kwnames,
Benjamin Peterson819a46f2016-09-09 20:45:06 -070033 struct _PyArg_Parser *parser, ...);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020034PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
36 const char *, char **, ...);
37PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020038PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
40 const char *, char **, va_list);
Benjamin Peterson4eef5052016-09-10 17:04:36 -070041
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030042PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
43 struct _PyArg_Parser *, ...);
44PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
45 struct _PyArg_Parser *, va_list);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000046#endif
47
Martin v. Löwis18e16552006-02-15 17:27:45 +000048#define FLAG_COMPAT 1
49#define FLAG_SIZE_T 2
50
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040051typedef int (*destr_t)(PyObject *, void *);
52
53
54/* Keep track of "objects" that have been allocated or initialized and
55 which will need to be deallocated or cleaned up somehow if overall
56 parsing fails.
57*/
58typedef struct {
59 void *item;
60 destr_t destructor;
61} freelistentry_t;
62
63typedef struct {
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040064 freelistentry_t *entries;
Antoine Pitrou7056cb22013-02-17 01:04:57 +010065 int first_available;
66 int entries_malloced;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040067} freelist_t;
68
Antoine Pitrou7056cb22013-02-17 01:04:57 +010069#define STATIC_FREELIST_ENTRIES 8
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000070
71/* Forward */
Victor Stinner6518a932017-01-17 01:29:49 +010072static int vgetargs1_impl(PyObject *args, PyObject **stack, Py_ssize_t nargs,
73 const char *format, va_list *p_va, int flags);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000074static int vgetargs1(PyObject *, const char *, va_list *, int);
Victor Stinner84bb1cf2013-05-17 00:12:04 +020075static void seterror(Py_ssize_t, const char *, int *, const char *, const char *);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020076static const char *convertitem(PyObject *, const char **, va_list *, int, int *,
77 char *, size_t, freelist_t *);
78static const char *converttuple(PyObject *, const char **, va_list *, int,
79 int *, char *, size_t, int, freelist_t *);
80static const char *convertsimple(PyObject *, const char **, va_list *, int,
81 char *, size_t, freelist_t *);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +020082static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020083static int getbuffer(PyObject *, Py_buffer *, const char**);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000084
Tim Petersdbd9ba62000-07-09 03:09:57 +000085static int vgetargskeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 const char *, char **, va_list *, int);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030087static int vgetargskeywordsfast(PyObject *, PyObject *,
88 struct _PyArg_Parser *, va_list *, int);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -070089static int vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
90 PyObject *keywords, PyObject *kwnames,
91 struct _PyArg_Parser *parser,
92 va_list *p_va, int flags);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020093static const char *skipitem(const char **, va_list *, int);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000094
Fred Drake563dfc22001-10-23 14:41:08 +000095int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000096PyArg_Parse(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 int retval;
99 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 va_start(va, format);
102 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
103 va_end(va);
104 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000105}
106
107int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200108_PyArg_Parse_SizeT(PyObject *args, const char *format, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 int retval;
111 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 va_start(va, format);
114 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
115 va_end(va);
116 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000117}
118
119
Fred Drake563dfc22001-10-23 14:41:08 +0000120int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000121PyArg_ParseTuple(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 int retval;
124 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 va_start(va, format);
127 retval = vgetargs1(args, format, &va, 0);
128 va_end(va);
129 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000130}
131
Martin v. Löwis18e16552006-02-15 17:27:45 +0000132int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200133_PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000134{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 int retval;
136 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 va_start(va, format);
139 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
140 va_end(va);
141 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000142}
143
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000144
145int
Victor Stinner6518a932017-01-17 01:29:49 +0100146_PyArg_ParseStack(PyObject **args, Py_ssize_t nargs, const char *format, ...)
147{
148 int retval;
149 va_list va;
150
151 va_start(va, format);
152 retval = vgetargs1_impl(NULL, args, nargs, format, &va, 0);
153 va_end(va);
154 return retval;
155}
156
157int
158_PyArg_ParseStack_SizeT(PyObject **args, Py_ssize_t nargs, const char *format, ...)
159{
160 int retval;
161 va_list va;
162
163 va_start(va, format);
164 retval = vgetargs1_impl(NULL, args, nargs, format, &va, FLAG_SIZE_T);
165 va_end(va);
166 return retval;
167}
168
169
170int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000171PyArg_VaParse(PyObject *args, const char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 va_list lva;
Christian Heimes2f2fee12016-09-21 11:37:27 +0200174 int retval;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000175
Benjamin Peterson0c212142016-09-20 20:39:33 -0700176 va_copy(lva, va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000177
Christian Heimes2f2fee12016-09-21 11:37:27 +0200178 retval = vgetargs1(args, format, &lva, 0);
179 va_end(lva);
180 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000181}
182
Martin v. Löwis18e16552006-02-15 17:27:45 +0000183int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200184_PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 va_list lva;
Christian Heimes2f2fee12016-09-21 11:37:27 +0200187 int retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000188
Benjamin Peterson0c212142016-09-20 20:39:33 -0700189 va_copy(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000190
Christian Heimes2f2fee12016-09-21 11:37:27 +0200191 retval = vgetargs1(args, format, &lva, FLAG_SIZE_T);
192 va_end(lva);
193 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000194}
195
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000196
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000197/* Handle cleanup of allocated memory in case of exception */
198
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400199static int
200cleanup_ptr(PyObject *self, void *ptr)
Antoine Pitrouf71995782008-08-29 18:37:05 +0000201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 if (ptr) {
203 PyMem_FREE(ptr);
204 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000206}
207
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000208static int
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400209cleanup_buffer(PyObject *self, void *ptr)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000210{
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400211 Py_buffer *buf = (Py_buffer *)ptr;
212 if (buf) {
213 PyBuffer_Release(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400215 return 0;
216}
217
218static int
219addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
220{
221 int index;
222
223 index = freelist->first_available;
224 freelist->first_available += 1;
225
226 freelist->entries[index].item = ptr;
227 freelist->entries[index].destructor = destructor;
228
229 return 0;
230}
231
232static int
233cleanreturn(int retval, freelist_t *freelist)
234{
235 int index;
236
237 if (retval == 0) {
238 /* A failure occurred, therefore execute all of the cleanup
239 functions.
240 */
241 for (index = 0; index < freelist->first_available; ++index) {
242 freelist->entries[index].destructor(NULL,
243 freelist->entries[index].item);
244 }
245 }
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100246 if (freelist->entries_malloced)
247 PyMem_FREE(freelist->entries);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 return retval;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000249}
250
251
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000252static int
Victor Stinner6518a932017-01-17 01:29:49 +0100253vgetargs1_impl(PyObject *compat_args, PyObject **stack, Py_ssize_t nargs, const char *format,
254 va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 char msgbuf[256];
257 int levels[32];
258 const char *fname = NULL;
259 const char *message = NULL;
260 int min = -1;
261 int max = 0;
262 int level = 0;
263 int endfmt = 0;
264 const char *formatsave = format;
Victor Stinner6518a932017-01-17 01:29:49 +0100265 Py_ssize_t i;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200266 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 int compat = flags & FLAG_COMPAT;
Benjamin Peterson40be9e52014-02-11 10:09:27 -0500268 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
269 freelist_t freelist;
270
Victor Stinner6518a932017-01-17 01:29:49 +0100271 assert(nargs == 0 || stack != NULL);
272
Benjamin Peterson40be9e52014-02-11 10:09:27 -0500273 freelist.entries = static_entries;
274 freelist.first_available = 0;
275 freelist.entries_malloced = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 while (endfmt == 0) {
280 int c = *format++;
281 switch (c) {
282 case '(':
283 if (level == 0)
284 max++;
285 level++;
286 if (level >= 30)
287 Py_FatalError("too many tuple nesting levels "
288 "in argument format string");
289 break;
290 case ')':
291 if (level == 0)
292 Py_FatalError("excess ')' in getargs format");
293 else
294 level--;
295 break;
296 case '\0':
297 endfmt = 1;
298 break;
299 case ':':
300 fname = format;
301 endfmt = 1;
302 break;
303 case ';':
304 message = format;
305 endfmt = 1;
306 break;
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100307 case '|':
308 if (level == 0)
309 min = max;
310 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 default:
312 if (level == 0) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100313 if (Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 if (c != 'e') /* skip encoded */
315 max++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 }
317 break;
318 }
319 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 if (level != 0)
322 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 if (min < 0)
325 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000328
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100329 if (max > STATIC_FREELIST_ENTRIES) {
330 freelist.entries = PyMem_NEW(freelistentry_t, max);
331 if (freelist.entries == NULL) {
332 PyErr_NoMemory();
333 return 0;
334 }
335 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -0500336 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 if (compat) {
339 if (max == 0) {
Victor Stinner6518a932017-01-17 01:29:49 +0100340 if (compat_args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 return 1;
Victor Stinner6ced7c42011-03-21 18:15:42 +0100342 PyErr_Format(PyExc_TypeError,
343 "%.200s%s takes no arguments",
344 fname==NULL ? "function" : fname,
345 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400346 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 }
348 else if (min == 1 && max == 1) {
Victor Stinner6518a932017-01-17 01:29:49 +0100349 if (compat_args == NULL) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100350 PyErr_Format(PyExc_TypeError,
351 "%.200s%s takes at least one argument",
352 fname==NULL ? "function" : fname,
353 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400354 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 }
Victor Stinner6518a932017-01-17 01:29:49 +0100356 msg = convertitem(compat_args, &format, p_va, flags, levels,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 msgbuf, sizeof(msgbuf), &freelist);
358 if (msg == NULL)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400359 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 seterror(levels[0], msg, levels+1, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400361 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 }
363 else {
364 PyErr_SetString(PyExc_SystemError,
365 "old style getargs format uses new features");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400366 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 }
368 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000369
Victor Stinner6518a932017-01-17 01:29:49 +0100370 if (nargs < min || max < nargs) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100371 if (message == NULL)
372 PyErr_Format(PyExc_TypeError,
373 "%.150s%s takes %s %d argument%s (%ld given)",
374 fname==NULL ? "function" : fname,
375 fname==NULL ? "" : "()",
376 min==max ? "exactly"
Victor Stinner6518a932017-01-17 01:29:49 +0100377 : nargs < min ? "at least" : "at most",
378 nargs < min ? min : max,
379 (nargs < min ? min : max) == 1 ? "" : "s",
380 Py_SAFE_DOWNCAST(nargs, Py_ssize_t, long));
Victor Stinner6ced7c42011-03-21 18:15:42 +0100381 else
382 PyErr_SetString(PyExc_TypeError, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400383 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000385
Victor Stinner6518a932017-01-17 01:29:49 +0100386 for (i = 0; i < nargs; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 if (*format == '|')
388 format++;
Victor Stinner6518a932017-01-17 01:29:49 +0100389 msg = convertitem(stack[i], &format, p_va,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 flags, levels, msgbuf,
391 sizeof(msgbuf), &freelist);
392 if (msg) {
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200393 seterror(i+1, msg, levels, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400394 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 }
396 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000397
Antoine Pitrou4de74572013-02-09 23:11:27 +0100398 if (*format != '\0' && !Py_ISALPHA(Py_CHARMASK(*format)) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 *format != '(' &&
400 *format != '|' && *format != ':' && *format != ';') {
401 PyErr_Format(PyExc_SystemError,
402 "bad format string: %.200s", formatsave);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400403 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000405
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400406 return cleanreturn(1, &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000407}
408
Victor Stinner6518a932017-01-17 01:29:49 +0100409static int
410vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
411{
412 PyObject **stack;
413 Py_ssize_t nargs;
414
415 if (!(flags & FLAG_COMPAT)) {
416 assert(args != NULL);
417
418 if (!PyTuple_Check(args)) {
419 PyErr_SetString(PyExc_SystemError,
420 "new style getargs format but argument is not a tuple");
421 return 0;
422 }
423
424 stack = &PyTuple_GET_ITEM(args, 0);
425 nargs = PyTuple_GET_SIZE(args);
426 }
427 else {
428 stack = NULL;
429 nargs = 0;
430 }
431
432 return vgetargs1_impl(args, stack, nargs, format, p_va, flags);
433}
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000434
435
436static void
Victor Stinner84bb1cf2013-05-17 00:12:04 +0200437seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000438 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 char buf[512];
441 int i;
442 char *p = buf;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 if (PyErr_Occurred())
445 return;
446 else if (message == NULL) {
447 if (fname != NULL) {
448 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
449 p += strlen(p);
450 }
451 if (iarg != 0) {
452 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Richard Oudkerk25296ce2013-05-18 17:35:19 +0100453 "argument %" PY_FORMAT_SIZE_T "d", iarg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 i = 0;
455 p += strlen(p);
Georg Brandl142ad662013-10-14 07:01:11 +0200456 while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 PyOS_snprintf(p, sizeof(buf) - (p - buf),
458 ", item %d", levels[i]-1);
459 p += strlen(p);
460 i++;
461 }
462 }
463 else {
464 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
465 p += strlen(p);
466 }
467 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
468 message = buf;
469 }
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +0200470 if (msg[0] == '(') {
471 PyErr_SetString(PyExc_SystemError, message);
472 }
473 else {
474 PyErr_SetString(PyExc_TypeError, message);
475 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000476}
477
478
479/* Convert a tuple argument.
480 On entry, *p_format points to the character _after_ the opening '('.
481 On successful exit, *p_format points to the closing ')'.
482 If successful:
483 *p_format and *p_va are updated,
484 *levels and *msgbuf are untouched,
485 and NULL is returned.
486 If the argument is invalid:
487 *p_format is unchanged,
488 *p_va is undefined,
489 *levels is a 0-terminated list of item numbers,
490 *msgbuf contains an error message, whose format is:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 "must be <typename1>, not <typename2>", where:
492 <typename1> is the name of the expected type, and
493 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000494 and msgbuf is returned.
495*/
496
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200497static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000498converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000499 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400500 freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 int level = 0;
503 int n = 0;
504 const char *format = *p_format;
505 int i;
Victor Stinner74387f52013-11-18 01:21:12 +0100506 Py_ssize_t len;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 for (;;) {
509 int c = *format++;
510 if (c == '(') {
511 if (level == 0)
512 n++;
513 level++;
514 }
515 else if (c == ')') {
516 if (level == 0)
517 break;
518 level--;
519 }
520 else if (c == ':' || c == ';' || c == '\0')
521 break;
Antoine Pitrou4de74572013-02-09 23:11:27 +0100522 else if (level == 0 && Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 n++;
524 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
527 levels[0] = 0;
528 PyOS_snprintf(msgbuf, bufsize,
529 toplevel ? "expected %d arguments, not %.50s" :
530 "must be %d-item sequence, not %.50s",
531 n,
532 arg == Py_None ? "None" : arg->ob_type->tp_name);
533 return msgbuf;
534 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000535
Victor Stinner74387f52013-11-18 01:21:12 +0100536 len = PySequence_Size(arg);
537 if (len != n) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 levels[0] = 0;
Victor Stinner74387f52013-11-18 01:21:12 +0100539 if (toplevel) {
540 PyOS_snprintf(msgbuf, bufsize,
541 "expected %d arguments, not %" PY_FORMAT_SIZE_T "d",
542 n, len);
543 }
544 else {
545 PyOS_snprintf(msgbuf, bufsize,
546 "must be sequence of length %d, "
547 "not %" PY_FORMAT_SIZE_T "d",
548 n, len);
549 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 return msgbuf;
551 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 format = *p_format;
554 for (i = 0; i < n; i++) {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200555 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 PyObject *item;
557 item = PySequence_GetItem(arg, i);
558 if (item == NULL) {
559 PyErr_Clear();
560 levels[0] = i+1;
561 levels[1] = 0;
562 strncpy(msgbuf, "is not retrievable", bufsize);
563 return msgbuf;
564 }
565 msg = convertitem(item, &format, p_va, flags, levels+1,
566 msgbuf, bufsize, freelist);
567 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
568 Py_XDECREF(item);
569 if (msg != NULL) {
570 levels[0] = i+1;
571 return msg;
572 }
573 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 *p_format = format;
576 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000577}
578
579
580/* Convert a single item. */
581
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200582static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000583convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400584 int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000585{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200586 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 if (*format == '(' /* ')' */) {
590 format++;
591 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
592 bufsize, 0, freelist);
593 if (msg == NULL)
594 format++;
595 }
596 else {
597 msg = convertsimple(arg, &format, p_va, flags,
598 msgbuf, bufsize, freelist);
599 if (msg != NULL)
600 levels[0] = 0;
601 }
602 if (msg == NULL)
603 *p_format = format;
604 return msg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000605}
606
607
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000608
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000609/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000610
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200611static const char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000612converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 assert(expected != NULL);
615 assert(arg != NULL);
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200616 if (expected[0] == '(') {
617 PyOS_snprintf(msgbuf, bufsize,
618 "%.100s", expected);
619 }
620 else {
621 PyOS_snprintf(msgbuf, bufsize,
622 "must be %.50s, not %.50s", expected,
623 arg == Py_None ? "None" : arg->ob_type->tp_name);
624 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000626}
627
628#define CONV_UNICODE "(unicode conversion error)"
629
Guido van Rossum45aecf42006-03-15 04:58:47 +0000630/* Explicitly check for float arguments when integers are expected.
631 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000632static int
633float_argument_error(PyObject *arg)
634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (PyFloat_Check(arg)) {
636 PyErr_SetString(PyExc_TypeError,
637 "integer argument expected, got float" );
638 return 1;
639 }
640 else
641 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000642}
643
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000644/* Convert a non-tuple argument. Return NULL if conversion went OK,
645 or a string with a message describing the failure. The message is
646 formatted as "must be <desired type>, not <actual type>".
647 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000648 Don't call if a tuple is expected.
649
650 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000651*/
652
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200653static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000654convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400655 char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 /* For # codes */
658#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
659 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
660 else q=va_arg(*p_va, int*);
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000661#define STORE_SIZE(s) \
662 if (flags & FLAG_SIZE_T) \
663 *q2=s; \
664 else { \
665 if (INT_MAX < s) { \
666 PyErr_SetString(PyExc_OverflowError, \
667 "size does not fit in an int"); \
668 return converterr("", arg, msgbuf, bufsize); \
669 } \
Victor Stinner9550ef32013-06-05 01:18:13 +0200670 *q = (int)s; \
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000671 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000672#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
Victor Stinner6ab8e822011-01-04 11:16:49 +0000673#define RETURN_ERR_OCCURRED return msgbuf
Martin v. Löwis18e16552006-02-15 17:27:45 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 const char *format = *p_format;
676 char c = *format++;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200677 const char *sarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 case 'b': { /* unsigned byte -- very short int */
682 char *p = va_arg(*p_va, char *);
683 long ival;
684 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000685 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 ival = PyLong_AsLong(arg);
687 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000688 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 else if (ival < 0) {
690 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000691 "unsigned byte integer is less than minimum");
692 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 }
694 else if (ival > UCHAR_MAX) {
695 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000696 "unsigned byte integer is greater than maximum");
697 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 }
699 else
700 *p = (unsigned char) ival;
701 break;
702 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 case 'B': {/* byte sized bitfield - both signed and unsigned
705 values allowed */
706 char *p = va_arg(*p_va, char *);
707 long ival;
708 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000709 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 ival = PyLong_AsUnsignedLongMask(arg);
711 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000712 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 else
714 *p = (unsigned char) ival;
715 break;
716 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 case 'h': {/* signed short int */
719 short *p = va_arg(*p_va, short *);
720 long ival;
721 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000722 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 ival = PyLong_AsLong(arg);
724 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000725 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 else if (ival < SHRT_MIN) {
727 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000728 "signed short integer is less than minimum");
729 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 }
731 else if (ival > SHRT_MAX) {
732 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000733 "signed short integer is greater than maximum");
734 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 }
736 else
737 *p = (short) ival;
738 break;
739 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 case 'H': { /* short int sized bitfield, both signed and
742 unsigned allowed */
743 unsigned short *p = va_arg(*p_va, unsigned short *);
744 long ival;
745 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000746 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 ival = PyLong_AsUnsignedLongMask(arg);
748 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000749 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 else
751 *p = (unsigned short) ival;
752 break;
753 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 case 'i': {/* signed int */
756 int *p = va_arg(*p_va, int *);
757 long ival;
758 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000759 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 ival = PyLong_AsLong(arg);
761 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000762 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 else if (ival > INT_MAX) {
764 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000765 "signed integer is greater than maximum");
766 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 }
768 else if (ival < INT_MIN) {
769 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000770 "signed integer is less than minimum");
771 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 }
773 else
774 *p = ival;
775 break;
776 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 case 'I': { /* int sized bitfield, both signed and
779 unsigned allowed */
780 unsigned int *p = va_arg(*p_va, unsigned int *);
781 unsigned int ival;
782 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000783 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
785 if (ival == (unsigned int)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000786 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 else
788 *p = ival;
789 break;
790 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 case 'n': /* Py_ssize_t */
793 {
794 PyObject *iobj;
795 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
796 Py_ssize_t ival = -1;
797 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000798 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 iobj = PyNumber_Index(arg);
800 if (iobj != NULL) {
801 ival = PyLong_AsSsize_t(iobj);
802 Py_DECREF(iobj);
803 }
804 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000805 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 *p = ival;
807 break;
808 }
809 case 'l': {/* long int */
810 long *p = va_arg(*p_va, long *);
811 long ival;
812 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000813 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 ival = PyLong_AsLong(arg);
815 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000816 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 else
818 *p = ival;
819 break;
820 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 case 'k': { /* long sized bitfield */
823 unsigned long *p = va_arg(*p_va, unsigned long *);
824 unsigned long ival;
825 if (PyLong_Check(arg))
826 ival = PyLong_AsUnsignedLongMask(arg);
827 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200828 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 *p = ival;
830 break;
831 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000832
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700833 case 'L': {/* long long */
834 long long *p = va_arg( *p_va, long long * );
835 long long ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000836 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000837 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 ival = PyLong_AsLongLong(arg);
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700839 if (ival == (long long)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000840 RETURN_ERR_OCCURRED;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000841 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 break;
844 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 case 'K': { /* long long sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700847 unsigned long long *p = va_arg(*p_va, unsigned long long *);
848 unsigned long long ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 if (PyLong_Check(arg))
850 ival = PyLong_AsUnsignedLongLongMask(arg);
851 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200852 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 *p = ival;
854 break;
855 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 case 'f': {/* float */
858 float *p = va_arg(*p_va, float *);
859 double dval = PyFloat_AsDouble(arg);
860 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000861 RETURN_ERR_OCCURRED;
Benjamin Petersona853a8b2017-09-07 11:13:59 -0700862 else if (dval > FLT_MAX)
863 *p = (float)INFINITY;
864 else if (dval < -FLT_MAX)
865 *p = (float)-INFINITY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 else
867 *p = (float) dval;
868 break;
869 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 case 'd': {/* double */
872 double *p = va_arg(*p_va, double *);
873 double dval = PyFloat_AsDouble(arg);
874 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000875 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 else
877 *p = dval;
878 break;
879 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 case 'D': {/* complex double */
882 Py_complex *p = va_arg(*p_va, Py_complex *);
883 Py_complex cval;
884 cval = PyComplex_AsCComplex(arg);
885 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000886 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 else
888 *p = cval;
889 break;
890 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 case 'c': {/* char */
893 char *p = va_arg(*p_va, char *);
894 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
895 *p = PyBytes_AS_STRING(arg)[0];
Eli Bendersky906b88f2011-07-29 07:05:08 +0300896 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
897 *p = PyByteArray_AS_STRING(arg)[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 else
899 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
900 break;
901 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 case 'C': {/* unicode char */
904 int *p = va_arg(*p_va, int *);
Victor Stinnere1335c72011-10-04 20:53:03 +0200905 int kind;
906 void *data;
907
908 if (!PyUnicode_Check(arg))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 return converterr("a unicode character", arg, msgbuf, bufsize);
Victor Stinnere1335c72011-10-04 20:53:03 +0200910
911 if (PyUnicode_READY(arg))
912 RETURN_ERR_OCCURRED;
913
914 if (PyUnicode_GET_LENGTH(arg) != 1)
915 return converterr("a unicode character", arg, msgbuf, bufsize);
916
917 kind = PyUnicode_KIND(arg);
918 data = PyUnicode_DATA(arg);
919 *p = PyUnicode_READ(kind, data, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 break;
921 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000922
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700923 case 'p': {/* boolean *p*redicate */
924 int *p = va_arg(*p_va, int *);
925 int val = PyObject_IsTrue(arg);
926 if (val > 0)
927 *p = 1;
928 else if (val == 0)
929 *p = 0;
930 else
931 RETURN_ERR_OCCURRED;
932 break;
933 }
934
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000935 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000937
Serhiy Storchakab757c832014-12-05 22:25:22 +0200938 case 'y': {/* any bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 void **p = (void **)va_arg(*p_va, char **);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200940 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 Py_ssize_t count;
942 if (*format == '*') {
943 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
944 return converterr(buf, arg, msgbuf, bufsize);
945 format++;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400946 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 return converterr(
948 "(cleanup problem)",
949 arg, msgbuf, bufsize);
950 }
951 break;
952 }
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200953 count = convertbuffer(arg, (const void **)p, &buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 if (count < 0)
955 return converterr(buf, arg, msgbuf, bufsize);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000956 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 FETCH_SIZE;
958 STORE_SIZE(count);
959 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000960 } else {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300961 if (strlen(*p) != (size_t)count) {
962 PyErr_SetString(PyExc_ValueError, "embedded null byte");
963 RETURN_ERR_OCCURRED;
964 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 }
966 break;
967 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000968
Serhiy Storchakab757c832014-12-05 22:25:22 +0200969 case 's': /* text string or bytes-like object */
970 case 'z': /* text string, bytes-like object or None */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000971 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000973 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000975
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000976 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
978 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200979 Py_ssize_t len;
980 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 Storchaka85b0f5b2016-11-20 10:16:47 +0200984 PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 }
Serhiy Storchakab757c832014-12-05 22:25:22 +0200986 else { /* any bytes-like object */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200987 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 if (getbuffer(arg, p, &buf) < 0)
989 return converterr(buf, arg, msgbuf, bufsize);
990 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400991 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 return converterr(
993 "(cleanup problem)",
994 arg, msgbuf, bufsize);
995 }
996 format++;
Serhiy Storchakab757c832014-12-05 22:25:22 +0200997 } else if (*format == '#') { /* a string or read-only bytes-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000998 /* "s#" or "z#" */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200999 const void **p = (const void **)va_arg(*p_va, const char **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001001
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001002 if (c == 'z' && arg == Py_None) {
1003 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 STORE_SIZE(0);
1005 }
1006 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001007 Py_ssize_t len;
1008 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1009 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 return converterr(CONV_UNICODE,
1011 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001012 *p = sarg;
1013 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 }
Serhiy Storchakab757c832014-12-05 22:25:22 +02001015 else { /* read-only bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* XXX Really? */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001017 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 Py_ssize_t count = convertbuffer(arg, p, &buf);
1019 if (count < 0)
1020 return converterr(buf, arg, msgbuf, bufsize);
1021 STORE_SIZE(count);
1022 }
1023 format++;
1024 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001025 /* "s" or "z" */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001026 const char **p = va_arg(*p_va, const char **);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001027 Py_ssize_t len;
1028 sarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001029
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001030 if (c == 'z' && arg == Py_None)
1031 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001033 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1034 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 return converterr(CONV_UNICODE,
1036 arg, msgbuf, bufsize);
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001037 if (strlen(sarg) != (size_t)len) {
1038 PyErr_SetString(PyExc_ValueError, "embedded null character");
1039 RETURN_ERR_OCCURRED;
1040 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001041 *p = sarg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 }
1043 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001044 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 }
1047 break;
1048 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001049
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001050 case 'u': /* raw unicode buffer (Py_UNICODE *) */
1051 case 'Z': /* raw unicode buffer or None */
1052 {
Brett Cannonb94767f2011-02-22 20:15:44 +00001053 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1054
Serhiy Storchakab757c832014-12-05 22:25:22 +02001055 if (*format == '#') {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001056 /* "u#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001058
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001059 if (c == 'Z' && arg == Py_None) {
1060 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 STORE_SIZE(0);
1062 }
1063 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001064 Py_ssize_t len;
1065 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001066 if (*p == NULL)
1067 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001068 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 }
Victor Stinner5216e6d2010-06-08 21:45:51 +00001070 else
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001071 return converterr(c == 'Z' ? "str or None" : "str",
1072 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 format++;
1074 } else {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001075 /* "u" or "Z" */
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001076 if (c == 'Z' && arg == Py_None)
1077 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +00001078 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001079 Py_ssize_t len;
1080 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001081 if (*p == NULL)
1082 RETURN_ERR_OCCURRED;
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001083 if (wcslen(*p) != (size_t)len) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001084 PyErr_SetString(PyExc_ValueError, "embedded null character");
1085 RETURN_ERR_OCCURRED;
1086 }
Victor Stinner06e49dd2010-06-13 18:21:50 +00001087 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001088 return converterr(c == 'Z' ? "str or None" : "str",
1089 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 }
1091 break;
1092 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 case 'e': {/* encoded string */
1095 char **buffer;
1096 const char *encoding;
1097 PyObject *s;
1098 int recode_strings;
1099 Py_ssize_t size;
1100 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +00001101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 /* Get 'e' parameter: the encoding name */
1103 encoding = (const char *)va_arg(*p_va, const char *);
1104 if (encoding == NULL)
1105 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 /* Get output buffer parameter:
1108 's' (recode all objects via Unicode) or
1109 't' (only recode non-string objects)
1110 */
1111 if (*format == 's')
1112 recode_strings = 1;
1113 else if (*format == 't')
1114 recode_strings = 0;
1115 else
1116 return converterr(
1117 "(unknown parser marker combination)",
1118 arg, msgbuf, bufsize);
1119 buffer = (char **)va_arg(*p_va, char **);
1120 format++;
1121 if (buffer == NULL)
1122 return converterr("(buffer is NULL)",
1123 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* Encode object */
1126 if (!recode_strings &&
1127 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1128 s = arg;
1129 Py_INCREF(s);
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001130 if (PyBytes_Check(arg)) {
1131 size = PyBytes_GET_SIZE(s);
1132 ptr = PyBytes_AS_STRING(s);
1133 }
1134 else {
1135 size = PyByteArray_GET_SIZE(s);
1136 ptr = PyByteArray_AS_STRING(s);
1137 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001139 else if (PyUnicode_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 /* Encode object; use default error handling */
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001141 s = PyUnicode_AsEncodedString(arg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 encoding,
1143 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 if (s == NULL)
1145 return converterr("(encoding failed)",
1146 arg, msgbuf, bufsize);
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001147 assert(PyBytes_Check(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 size = PyBytes_GET_SIZE(s);
1149 ptr = PyBytes_AS_STRING(s);
1150 if (ptr == NULL)
1151 ptr = "";
1152 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001153 else {
1154 return converterr(
1155 recode_strings ? "str" : "str, bytes or bytearray",
1156 arg, msgbuf, bufsize);
1157 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 /* Write output; output is guaranteed to be 0-terminated */
1160 if (*format == '#') {
1161 /* Using buffer length parameter '#':
1162
1163 - if *buffer is NULL, a new buffer of the
1164 needed size is allocated and the data
1165 copied into it; *buffer is updated to point
1166 to the new buffer; the caller is
1167 responsible for PyMem_Free()ing it after
1168 usage
1169
1170 - if *buffer is not NULL, the data is
1171 copied to *buffer; *buffer_len has to be
1172 set to the size of the buffer on input;
1173 buffer overflow is signalled with an error;
1174 buffer has to provide enough room for the
1175 encoded string plus the trailing 0-byte
1176
1177 - in both cases, *buffer_len is updated to
1178 the size of the buffer /excluding/ the
1179 trailing 0-byte
1180
1181 */
1182 FETCH_SIZE;
1183
1184 format++;
1185 if (q == NULL && q2 == NULL) {
1186 Py_DECREF(s);
1187 return converterr(
1188 "(buffer_len is NULL)",
1189 arg, msgbuf, bufsize);
1190 }
1191 if (*buffer == NULL) {
1192 *buffer = PyMem_NEW(char, size + 1);
1193 if (*buffer == NULL) {
1194 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001195 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001196 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001198 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 Py_DECREF(s);
1200 return converterr(
1201 "(cleanup problem)",
1202 arg, msgbuf, bufsize);
1203 }
1204 } else {
1205 if (size + 1 > BUFFER_LEN) {
1206 Py_DECREF(s);
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +02001207 PyErr_Format(PyExc_ValueError,
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001208 "encoded string too long "
1209 "(%zd, maximum length %zd)",
1210 (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1211 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 }
1213 }
1214 memcpy(*buffer, ptr, size+1);
1215 STORE_SIZE(size);
1216 } else {
1217 /* Using a 0-terminated buffer:
1218
1219 - the encoded string has to be 0-terminated
1220 for this variant to work; if it is not, an
1221 error raised
1222
1223 - a new buffer of the needed size is
1224 allocated and the data copied into it;
1225 *buffer is updated to point to the new
1226 buffer; the caller is responsible for
1227 PyMem_Free()ing it after usage
1228
1229 */
1230 if ((Py_ssize_t)strlen(ptr) != size) {
1231 Py_DECREF(s);
1232 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001233 "encoded string without null bytes",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 arg, msgbuf, bufsize);
1235 }
1236 *buffer = PyMem_NEW(char, size + 1);
1237 if (*buffer == NULL) {
1238 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001239 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001240 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001242 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 Py_DECREF(s);
1244 return converterr("(cleanup problem)",
1245 arg, msgbuf, bufsize);
1246 }
1247 memcpy(*buffer, ptr, size+1);
1248 }
1249 Py_DECREF(s);
1250 break;
1251 }
1252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 case 'S': { /* PyBytes object */
1254 PyObject **p = va_arg(*p_va, PyObject **);
1255 if (PyBytes_Check(arg))
1256 *p = arg;
1257 else
1258 return converterr("bytes", arg, msgbuf, bufsize);
1259 break;
1260 }
1261
1262 case 'Y': { /* PyByteArray object */
1263 PyObject **p = va_arg(*p_va, PyObject **);
1264 if (PyByteArray_Check(arg))
1265 *p = arg;
1266 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001267 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 break;
1269 }
1270
1271 case 'U': { /* PyUnicode object */
1272 PyObject **p = va_arg(*p_va, PyObject **);
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001273 if (PyUnicode_Check(arg)) {
1274 if (PyUnicode_READY(arg) == -1)
1275 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 *p = arg;
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 else
1279 return converterr("str", arg, msgbuf, bufsize);
1280 break;
1281 }
1282
1283 case 'O': { /* object */
1284 PyTypeObject *type;
1285 PyObject **p;
1286 if (*format == '!') {
1287 type = va_arg(*p_va, PyTypeObject*);
1288 p = va_arg(*p_va, PyObject **);
1289 format++;
1290 if (PyType_IsSubtype(arg->ob_type, type))
1291 *p = arg;
1292 else
1293 return converterr(type->tp_name, arg, msgbuf, bufsize);
1294
1295 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 else if (*format == '&') {
1297 typedef int (*converter)(PyObject *, void *);
1298 converter convert = va_arg(*p_va, converter);
1299 void *addr = va_arg(*p_va, void *);
1300 int res;
1301 format++;
1302 if (! (res = (*convert)(arg, addr)))
1303 return converterr("(unspecified)",
1304 arg, msgbuf, bufsize);
1305 if (res == Py_CLEANUP_SUPPORTED &&
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001306 addcleanup(addr, freelist, convert) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 return converterr("(cleanup problem)",
1308 arg, msgbuf, bufsize);
1309 }
1310 else {
1311 p = va_arg(*p_va, PyObject **);
1312 *p = arg;
1313 }
1314 break;
1315 }
1316
1317
Victor Stinner25e8ec42010-06-25 00:02:38 +00001318 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320
Victor Stinner25e8ec42010-06-25 00:02:38 +00001321 if (*format != '*')
1322 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001323 "(invalid use of 'w' format character)",
Victor Stinner25e8ec42010-06-25 00:02:38 +00001324 arg, msgbuf, bufsize);
1325 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326
Victor Stinner25e8ec42010-06-25 00:02:38 +00001327 /* Caller is interested in Py_buffer, and the object
1328 supports it directly. */
1329 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1330 PyErr_Clear();
R David Murray861470c2014-10-05 11:47:01 -04001331 return converterr("read-write bytes-like object",
1332 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 }
Victor Stinner8182b712010-07-28 00:40:58 +00001334 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1335 PyBuffer_Release((Py_buffer*)p);
1336 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1337 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001338 if (addcleanup(p, freelist, cleanup_buffer)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001339 return converterr(
1340 "(cleanup problem)",
1341 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 break;
1344 }
1345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 default:
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001347 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348
1349 }
1350
1351 *p_format = format;
1352 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001353
1354#undef FETCH_SIZE
1355#undef STORE_SIZE
1356#undef BUFFER_LEN
1357#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001358}
Guido van Rossumaa354651996-08-19 19:32:04 +00001359
Martin v. Löwis18e16552006-02-15 17:27:45 +00001360static Py_ssize_t
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001361convertbuffer(PyObject *arg, const void **p, const char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001362{
Victor Stinner5cb62392010-06-06 20:27:51 +00001363 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 Py_ssize_t count;
1365 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 *errmsg = NULL;
1368 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001369 if (pb != NULL && pb->bf_releasebuffer != NULL) {
R David Murray861470c2014-10-05 11:47:01 -04001370 *errmsg = "read-only bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 return -1;
1372 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001373
Victor Stinner8182b712010-07-28 00:40:58 +00001374 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 count = view.len;
1377 *p = view.buf;
1378 PyBuffer_Release(&view);
1379 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001380}
Guido van Rossumaa354651996-08-19 19:32:04 +00001381
Martin v. Löwis423be952008-08-13 15:53:07 +00001382static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001383getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001384{
Victor Stinner8182b712010-07-28 00:40:58 +00001385 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
R David Murray861470c2014-10-05 11:47:01 -04001386 *errmsg = "bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 return -1;
1388 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001389 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001390 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001391 *errmsg = "contiguous buffer";
1392 return -1;
1393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001395}
1396
Guido van Rossumaa354651996-08-19 19:32:04 +00001397/* Support for keyword arguments donated by
1398 Geoff Philbrick <philbric@delphi.hks.com> */
1399
Tim Petersf8cd3e82001-10-27 04:26:57 +00001400/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001401int
1402PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 PyObject *keywords,
1404 const char *format,
1405 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 int retval;
1408 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 if ((args == NULL || !PyTuple_Check(args)) ||
1411 (keywords != NULL && !PyDict_Check(keywords)) ||
1412 format == NULL ||
1413 kwlist == NULL)
1414 {
1415 PyErr_BadInternalCall();
1416 return 0;
1417 }
Tim Peters45772cd2001-10-27 03:58:40 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 va_start(va, kwlist);
1420 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1421 va_end(va);
1422 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001423}
1424
1425int
1426_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 PyObject *keywords,
1428 const char *format,
1429 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 int retval;
1432 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 if ((args == NULL || !PyTuple_Check(args)) ||
1435 (keywords != NULL && !PyDict_Check(keywords)) ||
1436 format == NULL ||
1437 kwlist == NULL)
1438 {
1439 PyErr_BadInternalCall();
1440 return 0;
1441 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 va_start(va, kwlist);
1444 retval = vgetargskeywords(args, keywords, format,
1445 kwlist, &va, FLAG_SIZE_T);
1446 va_end(va);
1447 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001448}
1449
1450
Brett Cannon711e7d92004-07-10 22:20:32 +00001451int
1452PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001453 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001454 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001455 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 int retval;
1458 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 if ((args == NULL || !PyTuple_Check(args)) ||
1461 (keywords != NULL && !PyDict_Check(keywords)) ||
1462 format == NULL ||
1463 kwlist == NULL)
1464 {
1465 PyErr_BadInternalCall();
1466 return 0;
1467 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001468
Benjamin Peterson0c212142016-09-20 20:39:33 -07001469 va_copy(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001472 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001474}
1475
1476int
1477_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 PyObject *keywords,
1479 const char *format,
1480 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 int retval;
1483 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 if ((args == NULL || !PyTuple_Check(args)) ||
1486 (keywords != NULL && !PyDict_Check(keywords)) ||
1487 format == NULL ||
1488 kwlist == NULL)
1489 {
1490 PyErr_BadInternalCall();
1491 return 0;
1492 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001493
Benjamin Peterson0c212142016-09-20 20:39:33 -07001494 va_copy(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 retval = vgetargskeywords(args, keywords, format,
1497 kwlist, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001498 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001500}
1501
Benjamin Petersonfb886362010-04-24 18:21:17 +00001502int
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001503_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1504 struct _PyArg_Parser *parser, ...)
1505{
1506 int retval;
1507 va_list va;
1508
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001509 va_start(va, parser);
1510 retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1511 va_end(va);
1512 return retval;
1513}
1514
1515int
1516_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1517 struct _PyArg_Parser *parser, ...)
1518{
1519 int retval;
1520 va_list va;
1521
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001522 va_start(va, parser);
1523 retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
1524 va_end(va);
1525 return retval;
1526}
1527
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001528int
Victor Stinner3e1fad62017-01-17 01:29:01 +01001529_PyArg_ParseStackAndKeywords(PyObject **args, Py_ssize_t nargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001530 struct _PyArg_Parser *parser, ...)
1531{
1532 int retval;
1533 va_list va;
1534
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001535 va_start(va, parser);
1536 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1537 va_end(va);
1538 return retval;
1539}
1540
1541int
Victor Stinner3e1fad62017-01-17 01:29:01 +01001542_PyArg_ParseStackAndKeywords_SizeT(PyObject **args, Py_ssize_t nargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001543 struct _PyArg_Parser *parser, ...)
1544{
1545 int retval;
1546 va_list va;
1547
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001548 va_start(va, parser);
1549 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T);
1550 va_end(va);
1551 return retval;
1552}
1553
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001554
1555int
1556_PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1557 struct _PyArg_Parser *parser, va_list va)
1558{
1559 int retval;
1560 va_list lva;
1561
Benjamin Peterson0c212142016-09-20 20:39:33 -07001562 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001563
1564 retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001565 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001566 return retval;
1567}
1568
1569int
1570_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1571 struct _PyArg_Parser *parser, va_list va)
1572{
1573 int retval;
1574 va_list lva;
1575
Benjamin Peterson0c212142016-09-20 20:39:33 -07001576 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001577
1578 retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001579 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001580 return retval;
1581}
1582
1583int
Benjamin Petersonfb886362010-04-24 18:21:17 +00001584PyArg_ValidateKeywordArguments(PyObject *kwargs)
1585{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001586 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 PyErr_BadInternalCall();
1588 return 0;
1589 }
1590 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1591 PyErr_SetString(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02001592 "keywords must be strings");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 return 0;
1594 }
1595 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001596}
1597
Christian Heimes380f7f22008-02-28 11:19:05 +00001598#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001599
Guido van Rossumaa354651996-08-19 19:32:04 +00001600static int
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001601vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 char msgbuf[512];
1605 int levels[32];
Serhiy Storchaka17414412017-01-17 10:07:25 +02001606 const char *fname, *msg, *custom_msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 int min = INT_MAX;
Larry Hastings83a9f482012-03-20 20:06:16 +00001608 int max = INT_MAX;
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001609 int i, pos, len;
1610 int skip = 0;
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001611 Py_ssize_t nargs, nkwargs;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001612 PyObject *current_arg;
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001613 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
Benjamin Peterson40be9e52014-02-11 10:09:27 -05001614 freelist_t freelist;
1615
1616 freelist.entries = static_entries;
1617 freelist.first_available = 0;
1618 freelist.entries_malloced = 0;
Tim Petersf4331c12001-10-27 00:17:34 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 assert(args != NULL && PyTuple_Check(args));
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001621 assert(kwargs == NULL || PyDict_Check(kwargs));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 assert(format != NULL);
1623 assert(kwlist != NULL);
1624 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 /* grab the function name or custom error msg first (mutually exclusive) */
1627 fname = strchr(format, ':');
1628 if (fname) {
1629 fname++;
1630 custom_msg = NULL;
1631 }
1632 else {
1633 custom_msg = strchr(format,';');
1634 if (custom_msg)
1635 custom_msg++;
1636 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001637
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001638 /* scan kwlist and count the number of positional-only parameters */
1639 for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1640 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 /* scan kwlist and get greatest possible nbr of args */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001642 for (len = pos; kwlist[len]; len++) {
1643 if (!*kwlist[len]) {
1644 PyErr_SetString(PyExc_SystemError,
1645 "Empty keyword parameter name");
1646 return cleanreturn(0, &freelist);
1647 }
1648 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001649
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001650 if (len > STATIC_FREELIST_ENTRIES) {
1651 freelist.entries = PyMem_NEW(freelistentry_t, len);
1652 if (freelist.entries == NULL) {
1653 PyErr_NoMemory();
1654 return 0;
1655 }
1656 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -05001657 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 nargs = PyTuple_GET_SIZE(args);
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001660 nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
1661 if (nargs + nkwargs > len) {
Oren Milmanbf9075a2017-08-23 21:16:48 +03001662 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
1663 messages in some special cases (see bpo-31229). */
Victor Stinner6ced7c42011-03-21 18:15:42 +01001664 PyErr_Format(PyExc_TypeError,
Oren Milmanbf9075a2017-08-23 21:16:48 +03001665 "%.200s%s takes at most %d %sargument%s (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 (fname == NULL) ? "function" : fname,
1667 (fname == NULL) ? "" : "()",
1668 len,
Oren Milmanbf9075a2017-08-23 21:16:48 +03001669 (nargs == 0) ? "keyword " : "",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 (len == 1) ? "" : "s",
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001671 nargs + nkwargs);
Benjamin Peterson01feaec2012-03-16 13:25:58 -05001672 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 }
Tim Petersc2f01122001-10-27 07:25:06 +00001674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1676 for (i = 0; i < len; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 if (*format == '|') {
Larry Hastings83a9f482012-03-20 20:06:16 +00001678 if (min != 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 (| specified twice)");
1681 return cleanreturn(0, &freelist);
1682 }
1683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 min = i;
1685 format++;
Larry Hastings83a9f482012-03-20 20:06:16 +00001686
1687 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001688 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001689 "Invalid format string ($ before |)");
1690 return cleanreturn(0, &freelist);
1691 }
1692 }
1693 if (*format == '$') {
1694 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001695 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001696 "Invalid format string ($ specified twice)");
1697 return cleanreturn(0, &freelist);
1698 }
1699
1700 max = i;
1701 format++;
1702
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001703 if (max < pos) {
1704 PyErr_SetString(PyExc_SystemError,
1705 "Empty parameter name after $");
1706 return cleanreturn(0, &freelist);
1707 }
1708 if (skip) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001709 /* Now we know the minimal and the maximal numbers of
1710 * positional arguments and can raise an exception with
1711 * informative message (see below). */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001712 break;
1713 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001714 if (max < nargs) {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03001715 if (max == 0) {
1716 PyErr_Format(PyExc_TypeError,
1717 "%.200s%s takes no positional arguments",
1718 (fname == NULL) ? "function" : fname,
1719 (fname == NULL) ? "" : "()");
1720 }
1721 else {
1722 PyErr_Format(PyExc_TypeError,
1723 "%.200s%s takes %s %d positional arguments"
1724 " (%d given)",
1725 (fname == NULL) ? "function" : fname,
1726 (fname == NULL) ? "" : "()",
1727 (min != INT_MAX) ? "at most" : "exactly",
1728 max, nargs);
1729 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001730 return cleanreturn(0, &freelist);
1731 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 }
1733 if (IS_END_OF_FORMAT(*format)) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001734 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 "More keyword list entries (%d) than "
1736 "format specifiers (%d)", len, i);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001737 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001739 if (!skip) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02001740 if (i < nargs) {
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001741 current_arg = PyTuple_GET_ITEM(args, i);
Serhiy Storchaka17414412017-01-17 10:07:25 +02001742 }
1743 else if (nkwargs && i >= pos) {
1744 current_arg = PyDict_GetItemString(kwargs, kwlist[i]);
1745 if (current_arg)
1746 --nkwargs;
1747 }
1748 else {
1749 current_arg = NULL;
1750 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001751
1752 if (current_arg) {
1753 msg = convertitem(current_arg, &format, p_va, flags,
1754 levels, msgbuf, sizeof(msgbuf), &freelist);
1755 if (msg) {
1756 seterror(i+1, msg, levels, fname, custom_msg);
1757 return cleanreturn(0, &freelist);
1758 }
1759 continue;
1760 }
1761
1762 if (i < min) {
1763 if (i < pos) {
1764 assert (min == INT_MAX);
1765 assert (max == INT_MAX);
1766 skip = 1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001767 /* At that moment we still don't know the minimal and
1768 * the maximal numbers of positional arguments. Raising
1769 * an exception is deferred until we encounter | and $
1770 * or the end of the format. */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001771 }
1772 else {
Michael Seifert64c8f702017-04-09 09:47:12 +02001773 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
1774 "argument '%s' (pos %d)",
1775 (fname == NULL) ? "function" : fname,
1776 (fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02001777 kwlist[i], i+1);
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001778 return cleanreturn(0, &freelist);
1779 }
1780 }
1781 /* current code reports success when all required args
1782 * fulfilled and no keyword args left, with no further
1783 * validation. XXX Maybe skip this in debug build ?
1784 */
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001785 if (!nkwargs && !skip) {
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001786 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 }
1788 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 /* We are into optional args, skip thru to any remaining
1791 * keyword args */
1792 msg = skipitem(&format, p_va, flags);
1793 if (msg) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001794 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001796 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 }
1798 }
Tim Petersb054be42001-10-27 05:07:41 +00001799
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001800 if (skip) {
1801 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02001802 "%.200s%s takes %s %d positional arguments"
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001803 " (%d given)",
Michael Seifert64c8f702017-04-09 09:47:12 +02001804 (fname == NULL) ? "function" : fname,
1805 (fname == NULL) ? "" : "()",
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001806 (Py_MIN(pos, min) < i) ? "at least" : "exactly",
1807 Py_MIN(pos, min), nargs);
1808 return cleanreturn(0, &freelist);
1809 }
1810
Larry Hastings83a9f482012-03-20 20:06:16 +00001811 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001812 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 "more argument specifiers than keyword list entries "
1814 "(remaining format:'%s')", format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001815 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 }
Tim Petersc2f01122001-10-27 07:25:06 +00001817
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001818 if (nkwargs > 0) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02001819 PyObject *key;
1820 Py_ssize_t j;
1821 /* make sure there are no arguments given by name and position */
1822 for (i = pos; i < nargs; i++) {
1823 current_arg = PyDict_GetItemString(kwargs, kwlist[i]);
1824 if (current_arg) {
1825 /* arg present in tuple and in dict */
1826 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02001827 "argument for %.200s%s given by name ('%s') "
Serhiy Storchaka17414412017-01-17 10:07:25 +02001828 "and position (%d)",
Michael Seifert64c8f702017-04-09 09:47:12 +02001829 (fname == NULL) ? "function" : fname,
1830 (fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02001831 kwlist[i], i+1);
1832 return cleanreturn(0, &freelist);
1833 }
1834 }
1835 /* make sure there are no extraneous keyword arguments */
1836 j = 0;
1837 while (PyDict_Next(kwargs, &j, &key, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 int match = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 if (!PyUnicode_Check(key)) {
1840 PyErr_SetString(PyExc_TypeError,
1841 "keywords must be strings");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001842 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02001844 for (i = pos; i < len; i++) {
1845 if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001846 match = 1;
1847 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 }
1849 }
1850 if (!match) {
1851 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001852 "'%U' is an invalid keyword "
Michael Seifert64c8f702017-04-09 09:47:12 +02001853 "argument for %.200s%s",
1854 key,
1855 (fname == NULL) ? "this function" : fname,
1856 (fname == NULL) ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001857 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 }
1859 }
1860 }
1861
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001862 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001863}
1864
1865
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001866/* List of static parsers. */
1867static struct _PyArg_Parser *static_arg_parsers = NULL;
1868
1869static int
1870parser_init(struct _PyArg_Parser *parser)
1871{
1872 const char * const *keywords;
1873 const char *format, *msg;
1874 int i, len, min, max, nkw;
1875 PyObject *kwtuple;
1876
1877 assert(parser->format != NULL);
1878 assert(parser->keywords != NULL);
1879 if (parser->kwtuple != NULL) {
1880 return 1;
1881 }
1882
1883 /* grab the function name or custom error msg first (mutually exclusive) */
1884 parser->fname = strchr(parser->format, ':');
1885 if (parser->fname) {
1886 parser->fname++;
1887 parser->custom_msg = NULL;
1888 }
1889 else {
1890 parser->custom_msg = strchr(parser->format,';');
1891 if (parser->custom_msg)
1892 parser->custom_msg++;
1893 }
1894
1895 keywords = parser->keywords;
1896 /* scan keywords and count the number of positional-only parameters */
1897 for (i = 0; keywords[i] && !*keywords[i]; i++) {
1898 }
1899 parser->pos = i;
1900 /* scan keywords and get greatest possible nbr of args */
1901 for (; keywords[i]; i++) {
1902 if (!*keywords[i]) {
1903 PyErr_SetString(PyExc_SystemError,
1904 "Empty keyword parameter name");
1905 return 0;
1906 }
1907 }
1908 len = i;
1909
1910 min = max = INT_MAX;
1911 format = parser->format;
1912 for (i = 0; i < len; i++) {
1913 if (*format == '|') {
1914 if (min != INT_MAX) {
1915 PyErr_SetString(PyExc_SystemError,
1916 "Invalid format string (| specified twice)");
1917 return 0;
1918 }
1919 if (max != INT_MAX) {
1920 PyErr_SetString(PyExc_SystemError,
1921 "Invalid format string ($ before |)");
1922 return 0;
1923 }
1924 min = i;
1925 format++;
1926 }
1927 if (*format == '$') {
1928 if (max != INT_MAX) {
1929 PyErr_SetString(PyExc_SystemError,
1930 "Invalid format string ($ specified twice)");
1931 return 0;
1932 }
1933 if (i < parser->pos) {
1934 PyErr_SetString(PyExc_SystemError,
1935 "Empty parameter name after $");
1936 return 0;
1937 }
1938 max = i;
1939 format++;
1940 }
1941 if (IS_END_OF_FORMAT(*format)) {
1942 PyErr_Format(PyExc_SystemError,
1943 "More keyword list entries (%d) than "
1944 "format specifiers (%d)", len, i);
1945 return 0;
1946 }
1947
1948 msg = skipitem(&format, NULL, 0);
1949 if (msg) {
1950 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1951 format);
1952 return 0;
1953 }
1954 }
1955 parser->min = Py_MIN(min, len);
1956 parser->max = Py_MIN(max, len);
1957
1958 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1959 PyErr_Format(PyExc_SystemError,
1960 "more argument specifiers than keyword list entries "
1961 "(remaining format:'%s')", format);
1962 return 0;
1963 }
1964
1965 nkw = len - parser->pos;
1966 kwtuple = PyTuple_New(nkw);
1967 if (kwtuple == NULL) {
1968 return 0;
1969 }
1970 keywords = parser->keywords + parser->pos;
1971 for (i = 0; i < nkw; i++) {
1972 PyObject *str = PyUnicode_FromString(keywords[i]);
1973 if (str == NULL) {
1974 Py_DECREF(kwtuple);
1975 return 0;
1976 }
1977 PyUnicode_InternInPlace(&str);
1978 PyTuple_SET_ITEM(kwtuple, i, str);
1979 }
1980 parser->kwtuple = kwtuple;
1981
1982 assert(parser->next == NULL);
1983 parser->next = static_arg_parsers;
1984 static_arg_parsers = parser;
1985 return 1;
1986}
1987
1988static void
1989parser_clear(struct _PyArg_Parser *parser)
1990{
1991 Py_CLEAR(parser->kwtuple);
1992}
1993
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001994static PyObject*
Serhiy Storchaka17414412017-01-17 10:07:25 +02001995find_keyword(PyObject *kwargs, PyObject *kwnames, PyObject **kwstack, PyObject *key)
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001996{
1997 Py_ssize_t i, nkwargs;
1998
Serhiy Storchaka17414412017-01-17 10:07:25 +02001999 if (kwargs != NULL) {
2000 return PyDict_GetItem(kwargs, key);
2001 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002002 nkwargs = PyTuple_GET_SIZE(kwnames);
2003 for (i=0; i < nkwargs; i++) {
2004 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
2005
2006 /* ptr==ptr should match in most cases since keyword keys
2007 should be interned strings */
2008 if (kwname == key) {
2009 return kwstack[i];
2010 }
2011 if (!PyUnicode_Check(kwname)) {
2012 /* ignore non-string keyword keys:
Serhiy Storchaka17414412017-01-17 10:07:25 +02002013 an error will be raised below */
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002014 continue;
2015 }
2016 if (_PyUnicode_EQ(kwname, key)) {
2017 return kwstack[i];
2018 }
2019 }
2020 return NULL;
2021}
2022
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002023static int
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002024vgetargskeywordsfast_impl(PyObject **args, Py_ssize_t nargs,
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002025 PyObject *kwargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002026 struct _PyArg_Parser *parser,
2027 va_list *p_va, int flags)
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002028{
2029 PyObject *kwtuple;
2030 char msgbuf[512];
2031 int levels[32];
2032 const char *format;
2033 const char *msg;
2034 PyObject *keyword;
2035 int i, pos, len;
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002036 Py_ssize_t nkwargs;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002037 PyObject *current_arg;
2038 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
2039 freelist_t freelist;
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002040 PyObject **kwstack = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002041
2042 freelist.entries = static_entries;
2043 freelist.first_available = 0;
2044 freelist.entries_malloced = 0;
2045
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002046 assert(kwargs == NULL || PyDict_Check(kwargs));
Serhiy Storchaka17414412017-01-17 10:07:25 +02002047 assert(kwargs == NULL || kwnames == NULL);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002048 assert(p_va != NULL);
2049
Victor Stinner80ab22f2017-01-16 23:16:47 +01002050 if (parser == NULL) {
2051 PyErr_BadInternalCall();
2052 return 0;
2053 }
2054
2055 if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2056 PyErr_BadInternalCall();
2057 return 0;
2058 }
2059
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002060 if (!parser_init(parser)) {
2061 return 0;
2062 }
2063
2064 kwtuple = parser->kwtuple;
2065 pos = parser->pos;
Segev Finere7bc7aa2017-07-26 18:58:25 +03002066 len = pos + (int)PyTuple_GET_SIZE(kwtuple);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002067
2068 if (len > STATIC_FREELIST_ENTRIES) {
2069 freelist.entries = PyMem_NEW(freelistentry_t, len);
2070 if (freelist.entries == NULL) {
2071 PyErr_NoMemory();
2072 return 0;
2073 }
2074 freelist.entries_malloced = 1;
2075 }
2076
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002077 if (kwargs != NULL) {
2078 nkwargs = PyDict_GET_SIZE(kwargs);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002079 }
2080 else if (kwnames != NULL) {
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002081 nkwargs = PyTuple_GET_SIZE(kwnames);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002082 kwstack = args + nargs;
2083 }
2084 else {
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002085 nkwargs = 0;
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002086 }
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002087 if (nargs + nkwargs > len) {
Oren Milmanbf9075a2017-08-23 21:16:48 +03002088 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2089 messages in some special cases (see bpo-31229). */
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002090 PyErr_Format(PyExc_TypeError,
Oren Milmanbf9075a2017-08-23 21:16:48 +03002091 "%.200s%s takes at most %d %sargument%s (%zd given)",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002092 (parser->fname == NULL) ? "function" : parser->fname,
2093 (parser->fname == NULL) ? "" : "()",
2094 len,
Oren Milmanbf9075a2017-08-23 21:16:48 +03002095 (nargs == 0) ? "keyword " : "",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002096 (len == 1) ? "" : "s",
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002097 nargs + nkwargs);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002098 return cleanreturn(0, &freelist);
2099 }
2100 if (parser->max < nargs) {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002101 if (parser->max == 0) {
2102 PyErr_Format(PyExc_TypeError,
Serhiy Storchakaf9f1cca2017-06-09 19:27:06 +03002103 "%.200s%s takes no positional arguments",
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002104 (parser->fname == NULL) ? "function" : parser->fname,
2105 (parser->fname == NULL) ? "" : "()");
2106 }
2107 else {
2108 PyErr_Format(PyExc_TypeError,
Serhiy Storchakaf9f1cca2017-06-09 19:27:06 +03002109 "%.200s%s takes %s %d positional arguments (%d given)",
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002110 (parser->fname == NULL) ? "function" : parser->fname,
2111 (parser->fname == NULL) ? "" : "()",
2112 (parser->min != INT_MAX) ? "at most" : "exactly",
2113 parser->max, nargs);
2114 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002115 return cleanreturn(0, &freelist);
2116 }
2117
2118 format = parser->format;
2119 /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
2120 for (i = 0; i < len; i++) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002121 if (*format == '|') {
2122 format++;
2123 }
2124 if (*format == '$') {
2125 format++;
2126 }
2127 assert(!IS_END_OF_FORMAT(*format));
2128
Serhiy Storchaka17414412017-01-17 10:07:25 +02002129 if (i < nargs) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002130 current_arg = args[i];
Victor Stinnera9efb2f2016-09-09 17:40:22 -07002131 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002132 else if (nkwargs && i >= pos) {
2133 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2134 current_arg = find_keyword(kwargs, kwnames, kwstack, keyword);
2135 if (current_arg)
2136 --nkwargs;
2137 }
2138 else {
2139 current_arg = NULL;
2140 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002141
2142 if (current_arg) {
2143 msg = convertitem(current_arg, &format, p_va, flags,
2144 levels, msgbuf, sizeof(msgbuf), &freelist);
2145 if (msg) {
2146 seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2147 return cleanreturn(0, &freelist);
2148 }
2149 continue;
2150 }
2151
2152 if (i < parser->min) {
2153 /* Less arguments than required */
2154 if (i < pos) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002155 Py_ssize_t min = Py_MIN(pos, parser->min);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002156 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02002157 "%.200s%s takes %s %d positional arguments"
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002158 " (%d given)",
Michael Seifert64c8f702017-04-09 09:47:12 +02002159 (parser->fname == NULL) ? "function" : parser->fname,
2160 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02002161 min < parser->max ? "at least" : "exactly",
2162 min, nargs);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002163 }
2164 else {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002165 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
Michael Seifert64c8f702017-04-09 09:47:12 +02002166 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
2167 "argument '%U' (pos %d)",
2168 (parser->fname == NULL) ? "function" : parser->fname,
2169 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002170 keyword, i+1);
2171 }
2172 return cleanreturn(0, &freelist);
2173 }
2174 /* current code reports success when all required args
2175 * fulfilled and no keyword args left, with no further
2176 * validation. XXX Maybe skip this in debug build ?
2177 */
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002178 if (!nkwargs) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002179 return cleanreturn(1, &freelist);
2180 }
2181
2182 /* We are into optional args, skip thru to any remaining
2183 * keyword args */
2184 msg = skipitem(&format, p_va, flags);
2185 assert(msg == NULL);
2186 }
2187
2188 assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2189
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002190 if (nkwargs > 0) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002191 Py_ssize_t j;
2192 /* make sure there are no arguments given by name and position */
2193 for (i = pos; i < nargs; i++) {
2194 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2195 current_arg = find_keyword(kwargs, kwnames, kwstack, keyword);
2196 if (current_arg) {
2197 /* arg present in tuple and in dict */
2198 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02002199 "argument for %.200s%s given by name ('%U') "
Serhiy Storchaka17414412017-01-17 10:07:25 +02002200 "and position (%d)",
Michael Seifert64c8f702017-04-09 09:47:12 +02002201 (parser->fname == NULL) ? "function" : parser->fname,
2202 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02002203 keyword, i+1);
2204 return cleanreturn(0, &freelist);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002205 }
2206 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002207 /* make sure there are no extraneous keyword arguments */
2208 j = 0;
2209 while (1) {
2210 int match;
2211 if (kwargs != NULL) {
2212 if (!PyDict_Next(kwargs, &j, &keyword, NULL))
2213 break;
2214 }
2215 else {
2216 if (j >= PyTuple_GET_SIZE(kwnames))
2217 break;
2218 keyword = PyTuple_GET_ITEM(kwnames, j);
2219 j++;
2220 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002221
Serhiy Storchaka17414412017-01-17 10:07:25 +02002222 if (!PyUnicode_Check(keyword)) {
2223 PyErr_SetString(PyExc_TypeError,
2224 "keywords must be strings");
2225 return cleanreturn(0, &freelist);
2226 }
2227 match = PySequence_Contains(kwtuple, keyword);
2228 if (match <= 0) {
2229 if (!match) {
2230 PyErr_Format(PyExc_TypeError,
2231 "'%U' is an invalid keyword "
Michael Seifert64c8f702017-04-09 09:47:12 +02002232 "argument for %.200s%s",
2233 keyword,
2234 (parser->fname == NULL) ? "this function" : parser->fname,
2235 (parser->fname == NULL) ? "" : "()");
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002236 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002237 return cleanreturn(0, &freelist);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002238 }
2239 }
2240 }
2241
2242 return cleanreturn(1, &freelist);
2243}
2244
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002245static int
2246vgetargskeywordsfast(PyObject *args, PyObject *keywords,
2247 struct _PyArg_Parser *parser, va_list *p_va, int flags)
2248{
2249 PyObject **stack;
2250 Py_ssize_t nargs;
2251
Victor Stinner80ab22f2017-01-16 23:16:47 +01002252 if (args == NULL
2253 || !PyTuple_Check(args)
2254 || (keywords != NULL && !PyDict_Check(keywords)))
2255 {
2256 PyErr_BadInternalCall();
2257 return 0;
2258 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002259
2260 stack = &PyTuple_GET_ITEM(args, 0);
2261 nargs = PyTuple_GET_SIZE(args);
2262 return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
2263 parser, p_va, flags);
2264}
2265
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002266
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002267static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002268skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00002269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 const char *format = *p_format;
2271 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00002274
Larry Hastingsa3479012012-05-08 23:52:03 -07002275 /*
2276 * codes that take a single data pointer as an argument
2277 * (the type of the pointer is irrelevant)
2278 */
Georg Brandl6dd14612005-09-14 19:29:53 +00002279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 case 'b': /* byte -- very short int */
2281 case 'B': /* byte as bitfield */
2282 case 'h': /* short int */
2283 case 'H': /* short int as bitfield */
2284 case 'i': /* int */
2285 case 'I': /* int sized bitfield */
2286 case 'l': /* long int */
2287 case 'k': /* long int sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -07002288 case 'L': /* long long */
2289 case 'K': /* long long sized bitfield */
Larry Hastingsa3479012012-05-08 23:52:03 -07002290 case 'n': /* Py_ssize_t */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 case 'f': /* float */
2292 case 'd': /* double */
2293 case 'D': /* complex double */
2294 case 'c': /* char */
2295 case 'C': /* unicode char */
Larry Hastings10ba07a2012-05-07 02:44:50 -07002296 case 'p': /* boolean predicate */
Larry Hastingsa3479012012-05-08 23:52:03 -07002297 case 'S': /* string object */
2298 case 'Y': /* string object */
2299 case 'U': /* unicode string object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002301 if (p_va != NULL) {
2302 (void) va_arg(*p_va, void *);
2303 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 break;
2305 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 case 'e': /* string with encoding */
2310 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002311 if (p_va != NULL) {
2312 (void) va_arg(*p_va, const char *);
2313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002314 if (!(*format == 's' || *format == 't'))
2315 /* after 'e', only 's' and 't' is allowed */
2316 goto err;
2317 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 }
Stefan Krahf432a322017-08-21 13:09:59 +02002319 /* fall through */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 case 's': /* string */
2322 case 'z': /* string or None */
2323 case 'y': /* bytes */
2324 case 'u': /* unicode string */
Larry Hastingsd9e4a412012-05-08 03:51:18 -07002325 case 'Z': /* unicode string or None */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 case 'w': /* buffer, read-write */
2327 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002328 if (p_va != NULL) {
2329 (void) va_arg(*p_va, char **);
2330 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 if (*format == '#') {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002332 if (p_va != NULL) {
2333 if (flags & FLAG_SIZE_T)
2334 (void) va_arg(*p_va, Py_ssize_t *);
2335 else
2336 (void) va_arg(*p_va, int *);
2337 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 format++;
2339 } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') {
2340 format++;
2341 }
2342 break;
2343 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 case 'O': /* object */
2346 {
2347 if (*format == '!') {
2348 format++;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002349 if (p_va != NULL) {
2350 (void) va_arg(*p_va, PyTypeObject*);
2351 (void) va_arg(*p_va, PyObject **);
2352 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 }
2354 else if (*format == '&') {
2355 typedef int (*converter)(PyObject *, void *);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002356 if (p_va != NULL) {
2357 (void) va_arg(*p_va, converter);
2358 (void) va_arg(*p_va, void *);
2359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 format++;
2361 }
2362 else {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002363 if (p_va != NULL) {
2364 (void) va_arg(*p_va, PyObject **);
2365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 }
2367 break;
2368 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 case '(': /* bypass tuple, not handled at all previously */
2371 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002372 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 for (;;) {
2374 if (*format==')')
2375 break;
2376 if (IS_END_OF_FORMAT(*format))
2377 return "Unmatched left paren in format "
2378 "string";
2379 msg = skipitem(&format, p_va, flags);
2380 if (msg)
2381 return msg;
2382 }
2383 format++;
2384 break;
2385 }
Christian Heimes380f7f22008-02-28 11:19:05 +00002386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 case ')':
2388 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00002389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00002391err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 *p_format = format;
2397 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00002398}
Fred Drakee4616e62001-10-23 21:09:29 +00002399
2400
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002401static int
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002402unpack_stack(PyObject **args, Py_ssize_t nargs, const char *name,
2403 Py_ssize_t min, Py_ssize_t max, va_list vargs)
Fred Drakee4616e62001-10-23 21:09:29 +00002404{
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002405 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 PyObject **o;
Fred Drakee4616e62001-10-23 21:09:29 +00002407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002408 assert(min >= 0);
2409 assert(min <= max);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002410
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002411 if (nargs < min) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 if (name != NULL)
2413 PyErr_Format(
2414 PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02002415 "%.200s expected %s%zd arguments, got %zd",
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002416 name, (min == max ? "" : "at least "), min, nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002417 else
2418 PyErr_Format(
2419 PyExc_TypeError,
2420 "unpacked tuple should have %s%zd elements,"
2421 " but has %zd",
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002422 (min == max ? "" : "at least "), min, nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 return 0;
2424 }
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002425
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002426 if (nargs == 0) {
Raymond Hettinger94230232016-03-26 03:02:48 -07002427 return 1;
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002428 }
2429
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002430 if (nargs > max) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 if (name != NULL)
2432 PyErr_Format(
2433 PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02002434 "%.200s expected %s%zd arguments, got %zd",
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002435 name, (min == max ? "" : "at most "), max, nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 else
2437 PyErr_Format(
2438 PyExc_TypeError,
2439 "unpacked tuple should have %s%zd elements,"
2440 " but has %zd",
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002441 (min == max ? "" : "at most "), max, nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 return 0;
2443 }
Raymond Hettinger94230232016-03-26 03:02:48 -07002444
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002445 for (i = 0; i < nargs; i++) {
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002446 o = va_arg(vargs, PyObject **);
2447 *o = args[i];
2448 }
2449 return 1;
2450}
2451
2452int
2453PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
2454{
2455 PyObject **stack;
2456 Py_ssize_t nargs;
2457 int retval;
2458 va_list vargs;
2459
2460 if (!PyTuple_Check(args)) {
2461 PyErr_SetString(PyExc_SystemError,
2462 "PyArg_UnpackTuple() argument list is not a tuple");
2463 return 0;
2464 }
2465 stack = &PyTuple_GET_ITEM(args, 0);
2466 nargs = PyTuple_GET_SIZE(args);
2467
Raymond Hettinger94230232016-03-26 03:02:48 -07002468#ifdef HAVE_STDARG_PROTOTYPES
2469 va_start(vargs, max);
2470#else
2471 va_start(vargs);
2472#endif
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002473 retval = unpack_stack(stack, nargs, name, min, max, vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 va_end(vargs);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002475 return retval;
2476}
2477
2478int
2479_PyArg_UnpackStack(PyObject **args, Py_ssize_t nargs, const char *name,
2480 Py_ssize_t min, Py_ssize_t max, ...)
2481{
2482 int retval;
2483 va_list vargs;
2484
2485#ifdef HAVE_STDARG_PROTOTYPES
2486 va_start(vargs, max);
2487#else
2488 va_start(vargs);
2489#endif
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002490 retval = unpack_stack(args, nargs, name, min, max, vargs);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002491 va_end(vargs);
2492 return retval;
Fred Drakee4616e62001-10-23 21:09:29 +00002493}
Georg Brandl02c42872005-08-26 06:42:30 +00002494
2495
Serhiy Storchaka68a001d2017-02-06 10:41:46 +02002496#undef _PyArg_NoKeywords
Serhiy Storchaka68a001d2017-02-06 10:41:46 +02002497#undef _PyArg_NoPositional
2498
Georg Brandl02c42872005-08-26 06:42:30 +00002499/* For type constructors that don't take keyword args
2500 *
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002501 * Sets a TypeError and returns 0 if the args/kwargs is
Thomas Wouters89f507f2006-12-13 04:49:30 +00002502 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00002503 */
2504int
Victor Stinner29d39cc2017-01-17 01:40:01 +01002505_PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
Georg Brandl02c42872005-08-26 06:42:30 +00002506{
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002507 if (kwargs == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 return 1;
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002509 }
Victor Stinner29d39cc2017-01-17 01:40:01 +01002510 if (!PyDict_CheckExact(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 PyErr_BadInternalCall();
2512 return 0;
2513 }
Victor Stinner29d39cc2017-01-17 01:40:01 +01002514 if (PyDict_GET_SIZE(kwargs) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 return 1;
Victor Stinner29d39cc2017-01-17 01:40:01 +01002516 }
2517
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002518 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
Victor Stinner29d39cc2017-01-17 01:40:01 +01002519 funcname);
2520 return 0;
2521}
2522
2523
2524int
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002525_PyArg_NoPositional(const char *funcname, PyObject *args)
2526{
2527 if (args == NULL)
2528 return 1;
2529 if (!PyTuple_CheckExact(args)) {
2530 PyErr_BadInternalCall();
2531 return 0;
2532 }
2533 if (PyTuple_GET_SIZE(args) == 0)
2534 return 1;
2535
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002536 PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002537 funcname);
2538 return 0;
2539}
2540
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002541void
2542_PyArg_Fini(void)
2543{
2544 struct _PyArg_Parser *tmp, *s = static_arg_parsers;
2545 while (s) {
2546 tmp = s->next;
2547 s->next = NULL;
2548 parser_clear(s);
2549 s = tmp;
2550 }
2551 static_arg_parsers = NULL;
2552}
2553
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002554#ifdef __cplusplus
2555};
2556#endif