blob: 98823f22ed4e01363a852ed8d74cfa426d565ce2 [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 *, ...);
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020030PyAPI_FUNC(int) _PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs,
Victor Stinner6518a932017-01-17 01:29:49 +010031 const char *format, ...);
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020032PyAPI_FUNC(int) _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs,
33 PyObject *kwnames,
Benjamin Peterson819a46f2016-09-09 20:45:06 -070034 struct _PyArg_Parser *parser, ...);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020035PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
37 const char *, char **, ...);
38PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020039PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
41 const char *, char **, va_list);
Benjamin Peterson4eef5052016-09-10 17:04:36 -070042
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030043PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
44 struct _PyArg_Parser *, ...);
45PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
46 struct _PyArg_Parser *, va_list);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000047#endif
48
Martin v. Löwis18e16552006-02-15 17:27:45 +000049#define FLAG_COMPAT 1
50#define FLAG_SIZE_T 2
51
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040052typedef int (*destr_t)(PyObject *, void *);
53
54
55/* Keep track of "objects" that have been allocated or initialized and
56 which will need to be deallocated or cleaned up somehow if overall
57 parsing fails.
58*/
59typedef struct {
60 void *item;
61 destr_t destructor;
62} freelistentry_t;
63
64typedef struct {
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040065 freelistentry_t *entries;
Antoine Pitrou7056cb22013-02-17 01:04:57 +010066 int first_available;
67 int entries_malloced;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040068} freelist_t;
69
Antoine Pitrou7056cb22013-02-17 01:04:57 +010070#define STATIC_FREELIST_ENTRIES 8
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000071
72/* Forward */
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020073static int vgetargs1_impl(PyObject *args, PyObject *const *stack, Py_ssize_t nargs,
Victor Stinner6518a932017-01-17 01:29:49 +010074 const char *format, va_list *p_va, int flags);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000075static int vgetargs1(PyObject *, const char *, va_list *, int);
Victor Stinner84bb1cf2013-05-17 00:12:04 +020076static void seterror(Py_ssize_t, const char *, int *, const char *, const char *);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020077static const char *convertitem(PyObject *, const char **, va_list *, int, int *,
78 char *, size_t, freelist_t *);
79static const char *converttuple(PyObject *, const char **, va_list *, int,
80 int *, char *, size_t, int, freelist_t *);
81static const char *convertsimple(PyObject *, const char **, va_list *, int,
82 char *, size_t, freelist_t *);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +020083static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020084static int getbuffer(PyObject *, Py_buffer *, const char**);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000085
Tim Petersdbd9ba62000-07-09 03:09:57 +000086static int vgetargskeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 const char *, char **, va_list *, int);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030088static int vgetargskeywordsfast(PyObject *, PyObject *,
89 struct _PyArg_Parser *, va_list *, int);
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020090static int vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -070091 PyObject *keywords, PyObject *kwnames,
92 struct _PyArg_Parser *parser,
93 va_list *p_va, int flags);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020094static const char *skipitem(const char **, va_list *, int);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000095
Fred Drake563dfc22001-10-23 14:41:08 +000096int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000097PyArg_Parse(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 int retval;
100 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 va_start(va, format);
103 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
104 va_end(va);
105 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000106}
107
108int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200109_PyArg_Parse_SizeT(PyObject *args, const char *format, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 int retval;
112 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 va_start(va, format);
115 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
116 va_end(va);
117 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000118}
119
120
Fred Drake563dfc22001-10-23 14:41:08 +0000121int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000122PyArg_ParseTuple(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 int retval;
125 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 va_start(va, format);
128 retval = vgetargs1(args, format, &va, 0);
129 va_end(va);
130 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000131}
132
Martin v. Löwis18e16552006-02-15 17:27:45 +0000133int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200134_PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 int retval;
137 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 va_start(va, format);
140 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
141 va_end(va);
142 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000143}
144
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000145
146int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200147_PyArg_ParseStack(PyObject *const *args, Py_ssize_t nargs, const char *format, ...)
Victor Stinner6518a932017-01-17 01:29:49 +0100148{
149 int retval;
150 va_list va;
151
152 va_start(va, format);
153 retval = vgetargs1_impl(NULL, args, nargs, format, &va, 0);
154 va_end(va);
155 return retval;
156}
157
158int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200159_PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs, const char *format, ...)
Victor Stinner6518a932017-01-17 01:29:49 +0100160{
161 int retval;
162 va_list va;
163
164 va_start(va, format);
165 retval = vgetargs1_impl(NULL, args, nargs, format, &va, FLAG_SIZE_T);
166 va_end(va);
167 return retval;
168}
169
170
171int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000172PyArg_VaParse(PyObject *args, const char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 va_list lva;
Christian Heimes2f2fee12016-09-21 11:37:27 +0200175 int retval;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000176
Benjamin Peterson0c212142016-09-20 20:39:33 -0700177 va_copy(lva, va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000178
Christian Heimes2f2fee12016-09-21 11:37:27 +0200179 retval = vgetargs1(args, format, &lva, 0);
180 va_end(lva);
181 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000182}
183
Martin v. Löwis18e16552006-02-15 17:27:45 +0000184int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200185_PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 va_list lva;
Christian Heimes2f2fee12016-09-21 11:37:27 +0200188 int retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000189
Benjamin Peterson0c212142016-09-20 20:39:33 -0700190 va_copy(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000191
Christian Heimes2f2fee12016-09-21 11:37:27 +0200192 retval = vgetargs1(args, format, &lva, FLAG_SIZE_T);
193 va_end(lva);
194 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000195}
196
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000197
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000198/* Handle cleanup of allocated memory in case of exception */
199
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400200static int
201cleanup_ptr(PyObject *self, void *ptr)
Antoine Pitrouf71995782008-08-29 18:37:05 +0000202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 if (ptr) {
204 PyMem_FREE(ptr);
205 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000207}
208
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000209static int
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400210cleanup_buffer(PyObject *self, void *ptr)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000211{
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400212 Py_buffer *buf = (Py_buffer *)ptr;
213 if (buf) {
214 PyBuffer_Release(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400216 return 0;
217}
218
219static int
220addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
221{
222 int index;
223
224 index = freelist->first_available;
225 freelist->first_available += 1;
226
227 freelist->entries[index].item = ptr;
228 freelist->entries[index].destructor = destructor;
229
230 return 0;
231}
232
233static int
234cleanreturn(int retval, freelist_t *freelist)
235{
236 int index;
237
238 if (retval == 0) {
239 /* A failure occurred, therefore execute all of the cleanup
240 functions.
241 */
242 for (index = 0; index < freelist->first_available; ++index) {
243 freelist->entries[index].destructor(NULL,
244 freelist->entries[index].item);
245 }
246 }
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100247 if (freelist->entries_malloced)
248 PyMem_FREE(freelist->entries);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 return retval;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000250}
251
252
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000253static int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200254vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs, const char *format,
Victor Stinner6518a932017-01-17 01:29:49 +0100255 va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 char msgbuf[256];
258 int levels[32];
259 const char *fname = NULL;
260 const char *message = NULL;
261 int min = -1;
262 int max = 0;
263 int level = 0;
264 int endfmt = 0;
265 const char *formatsave = format;
Victor Stinner6518a932017-01-17 01:29:49 +0100266 Py_ssize_t i;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200267 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 int compat = flags & FLAG_COMPAT;
Benjamin Peterson40be9e52014-02-11 10:09:27 -0500269 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
270 freelist_t freelist;
271
Victor Stinner6518a932017-01-17 01:29:49 +0100272 assert(nargs == 0 || stack != NULL);
273
Benjamin Peterson40be9e52014-02-11 10:09:27 -0500274 freelist.entries = static_entries;
275 freelist.first_available = 0;
276 freelist.entries_malloced = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 while (endfmt == 0) {
281 int c = *format++;
282 switch (c) {
283 case '(':
284 if (level == 0)
285 max++;
286 level++;
287 if (level >= 30)
288 Py_FatalError("too many tuple nesting levels "
289 "in argument format string");
290 break;
291 case ')':
292 if (level == 0)
293 Py_FatalError("excess ')' in getargs format");
294 else
295 level--;
296 break;
297 case '\0':
298 endfmt = 1;
299 break;
300 case ':':
301 fname = format;
302 endfmt = 1;
303 break;
304 case ';':
305 message = format;
306 endfmt = 1;
307 break;
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100308 case '|':
309 if (level == 0)
310 min = max;
311 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 default:
313 if (level == 0) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100314 if (Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 if (c != 'e') /* skip encoded */
316 max++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 }
318 break;
319 }
320 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 if (level != 0)
323 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (min < 0)
326 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000329
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100330 if (max > STATIC_FREELIST_ENTRIES) {
331 freelist.entries = PyMem_NEW(freelistentry_t, max);
332 if (freelist.entries == NULL) {
333 PyErr_NoMemory();
334 return 0;
335 }
336 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -0500337 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 if (compat) {
340 if (max == 0) {
Victor Stinner6518a932017-01-17 01:29:49 +0100341 if (compat_args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 return 1;
Victor Stinner6ced7c42011-03-21 18:15:42 +0100343 PyErr_Format(PyExc_TypeError,
344 "%.200s%s takes no arguments",
345 fname==NULL ? "function" : fname,
346 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400347 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 }
349 else if (min == 1 && max == 1) {
Victor Stinner6518a932017-01-17 01:29:49 +0100350 if (compat_args == NULL) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100351 PyErr_Format(PyExc_TypeError,
352 "%.200s%s takes at least one argument",
353 fname==NULL ? "function" : fname,
354 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400355 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 }
Victor Stinner6518a932017-01-17 01:29:49 +0100357 msg = convertitem(compat_args, &format, p_va, flags, levels,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 msgbuf, sizeof(msgbuf), &freelist);
359 if (msg == NULL)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400360 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 seterror(levels[0], msg, levels+1, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400362 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 }
364 else {
365 PyErr_SetString(PyExc_SystemError,
366 "old style getargs format uses new features");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400367 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 }
369 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000370
Victor Stinner6518a932017-01-17 01:29:49 +0100371 if (nargs < min || max < nargs) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100372 if (message == NULL)
373 PyErr_Format(PyExc_TypeError,
374 "%.150s%s takes %s %d argument%s (%ld given)",
375 fname==NULL ? "function" : fname,
376 fname==NULL ? "" : "()",
377 min==max ? "exactly"
Victor Stinner6518a932017-01-17 01:29:49 +0100378 : nargs < min ? "at least" : "at most",
379 nargs < min ? min : max,
380 (nargs < min ? min : max) == 1 ? "" : "s",
381 Py_SAFE_DOWNCAST(nargs, Py_ssize_t, long));
Victor Stinner6ced7c42011-03-21 18:15:42 +0100382 else
383 PyErr_SetString(PyExc_TypeError, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400384 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000386
Victor Stinner6518a932017-01-17 01:29:49 +0100387 for (i = 0; i < nargs; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (*format == '|')
389 format++;
Victor Stinner6518a932017-01-17 01:29:49 +0100390 msg = convertitem(stack[i], &format, p_va,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 flags, levels, msgbuf,
392 sizeof(msgbuf), &freelist);
393 if (msg) {
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200394 seterror(i+1, msg, levels, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400395 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 }
397 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000398
Antoine Pitrou4de74572013-02-09 23:11:27 +0100399 if (*format != '\0' && !Py_ISALPHA(Py_CHARMASK(*format)) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 *format != '(' &&
401 *format != '|' && *format != ':' && *format != ';') {
402 PyErr_Format(PyExc_SystemError,
403 "bad format string: %.200s", formatsave);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400404 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000406
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400407 return cleanreturn(1, &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000408}
409
Victor Stinner6518a932017-01-17 01:29:49 +0100410static int
411vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
412{
413 PyObject **stack;
414 Py_ssize_t nargs;
415
416 if (!(flags & FLAG_COMPAT)) {
417 assert(args != NULL);
418
419 if (!PyTuple_Check(args)) {
420 PyErr_SetString(PyExc_SystemError,
421 "new style getargs format but argument is not a tuple");
422 return 0;
423 }
424
425 stack = &PyTuple_GET_ITEM(args, 0);
426 nargs = PyTuple_GET_SIZE(args);
427 }
428 else {
429 stack = NULL;
430 nargs = 0;
431 }
432
433 return vgetargs1_impl(args, stack, nargs, format, p_va, flags);
434}
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000435
436
437static void
Victor Stinner84bb1cf2013-05-17 00:12:04 +0200438seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000439 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 char buf[512];
442 int i;
443 char *p = buf;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 if (PyErr_Occurred())
446 return;
447 else if (message == NULL) {
448 if (fname != NULL) {
449 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
450 p += strlen(p);
451 }
452 if (iarg != 0) {
453 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Richard Oudkerk25296ce2013-05-18 17:35:19 +0100454 "argument %" PY_FORMAT_SIZE_T "d", iarg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 i = 0;
456 p += strlen(p);
Georg Brandl142ad662013-10-14 07:01:11 +0200457 while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 PyOS_snprintf(p, sizeof(buf) - (p - buf),
459 ", item %d", levels[i]-1);
460 p += strlen(p);
461 i++;
462 }
463 }
464 else {
465 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
466 p += strlen(p);
467 }
468 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
469 message = buf;
470 }
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +0200471 if (msg[0] == '(') {
472 PyErr_SetString(PyExc_SystemError, message);
473 }
474 else {
475 PyErr_SetString(PyExc_TypeError, message);
476 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000477}
478
479
480/* Convert a tuple argument.
481 On entry, *p_format points to the character _after_ the opening '('.
482 On successful exit, *p_format points to the closing ')'.
483 If successful:
484 *p_format and *p_va are updated,
485 *levels and *msgbuf are untouched,
486 and NULL is returned.
487 If the argument is invalid:
488 *p_format is unchanged,
489 *p_va is undefined,
490 *levels is a 0-terminated list of item numbers,
491 *msgbuf contains an error message, whose format is:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 "must be <typename1>, not <typename2>", where:
493 <typename1> is the name of the expected type, and
494 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000495 and msgbuf is returned.
496*/
497
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200498static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000499converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000500 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400501 freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 int level = 0;
504 int n = 0;
505 const char *format = *p_format;
506 int i;
Victor Stinner74387f52013-11-18 01:21:12 +0100507 Py_ssize_t len;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 for (;;) {
510 int c = *format++;
511 if (c == '(') {
512 if (level == 0)
513 n++;
514 level++;
515 }
516 else if (c == ')') {
517 if (level == 0)
518 break;
519 level--;
520 }
521 else if (c == ':' || c == ';' || c == '\0')
522 break;
Antoine Pitrou4de74572013-02-09 23:11:27 +0100523 else if (level == 0 && Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 n++;
525 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
528 levels[0] = 0;
529 PyOS_snprintf(msgbuf, bufsize,
530 toplevel ? "expected %d arguments, not %.50s" :
531 "must be %d-item sequence, not %.50s",
532 n,
533 arg == Py_None ? "None" : arg->ob_type->tp_name);
534 return msgbuf;
535 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000536
Victor Stinner74387f52013-11-18 01:21:12 +0100537 len = PySequence_Size(arg);
538 if (len != n) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 levels[0] = 0;
Victor Stinner74387f52013-11-18 01:21:12 +0100540 if (toplevel) {
541 PyOS_snprintf(msgbuf, bufsize,
542 "expected %d arguments, not %" PY_FORMAT_SIZE_T "d",
543 n, len);
544 }
545 else {
546 PyOS_snprintf(msgbuf, bufsize,
547 "must be sequence of length %d, "
548 "not %" PY_FORMAT_SIZE_T "d",
549 n, len);
550 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 return msgbuf;
552 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 format = *p_format;
555 for (i = 0; i < n; i++) {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200556 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 PyObject *item;
558 item = PySequence_GetItem(arg, i);
559 if (item == NULL) {
560 PyErr_Clear();
561 levels[0] = i+1;
562 levels[1] = 0;
563 strncpy(msgbuf, "is not retrievable", bufsize);
564 return msgbuf;
565 }
566 msg = convertitem(item, &format, p_va, flags, levels+1,
567 msgbuf, bufsize, freelist);
568 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
569 Py_XDECREF(item);
570 if (msg != NULL) {
571 levels[0] = i+1;
572 return msg;
573 }
574 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 *p_format = format;
577 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000578}
579
580
581/* Convert a single item. */
582
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200583static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000584convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400585 int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000586{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200587 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 if (*format == '(' /* ')' */) {
591 format++;
592 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
593 bufsize, 0, freelist);
594 if (msg == NULL)
595 format++;
596 }
597 else {
598 msg = convertsimple(arg, &format, p_va, flags,
599 msgbuf, bufsize, freelist);
600 if (msg != NULL)
601 levels[0] = 0;
602 }
603 if (msg == NULL)
604 *p_format = format;
605 return msg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000606}
607
608
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000609
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000610/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000611
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200612static const char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000613converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 assert(expected != NULL);
616 assert(arg != NULL);
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200617 if (expected[0] == '(') {
618 PyOS_snprintf(msgbuf, bufsize,
619 "%.100s", expected);
620 }
621 else {
622 PyOS_snprintf(msgbuf, bufsize,
623 "must be %.50s, not %.50s", expected,
624 arg == Py_None ? "None" : arg->ob_type->tp_name);
625 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000627}
628
629#define CONV_UNICODE "(unicode conversion error)"
630
Guido van Rossum45aecf42006-03-15 04:58:47 +0000631/* Explicitly check for float arguments when integers are expected.
632 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000633static int
634float_argument_error(PyObject *arg)
635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 if (PyFloat_Check(arg)) {
637 PyErr_SetString(PyExc_TypeError,
638 "integer argument expected, got float" );
639 return 1;
640 }
641 else
642 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000643}
644
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000645/* Convert a non-tuple argument. Return NULL if conversion went OK,
646 or a string with a message describing the failure. The message is
647 formatted as "must be <desired type>, not <actual type>".
648 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000649 Don't call if a tuple is expected.
650
651 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000652*/
653
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200654static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000655convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400656 char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 /* For # codes */
659#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
660 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
661 else q=va_arg(*p_va, int*);
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000662#define STORE_SIZE(s) \
663 if (flags & FLAG_SIZE_T) \
664 *q2=s; \
665 else { \
666 if (INT_MAX < s) { \
667 PyErr_SetString(PyExc_OverflowError, \
668 "size does not fit in an int"); \
669 return converterr("", arg, msgbuf, bufsize); \
670 } \
Victor Stinner9550ef32013-06-05 01:18:13 +0200671 *q = (int)s; \
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000672 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000673#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
Victor Stinner6ab8e822011-01-04 11:16:49 +0000674#define RETURN_ERR_OCCURRED return msgbuf
Martin v. Löwis18e16552006-02-15 17:27:45 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 const char *format = *p_format;
677 char c = *format++;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200678 const char *sarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 case 'b': { /* unsigned byte -- very short int */
683 char *p = va_arg(*p_va, char *);
684 long ival;
685 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000686 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000687 ival = PyLong_AsLong(arg);
688 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000689 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 else if (ival < 0) {
691 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000692 "unsigned byte integer is less than minimum");
693 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 }
695 else if (ival > UCHAR_MAX) {
696 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000697 "unsigned byte integer is greater than maximum");
698 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 }
700 else
701 *p = (unsigned char) ival;
702 break;
703 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 case 'B': {/* byte sized bitfield - both signed and unsigned
706 values allowed */
707 char *p = va_arg(*p_va, char *);
708 long ival;
709 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000710 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 ival = PyLong_AsUnsignedLongMask(arg);
712 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000713 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 else
715 *p = (unsigned char) ival;
716 break;
717 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 case 'h': {/* signed short int */
720 short *p = va_arg(*p_va, short *);
721 long ival;
722 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000723 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 ival = PyLong_AsLong(arg);
725 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000726 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 else if (ival < SHRT_MIN) {
728 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000729 "signed short integer is less than minimum");
730 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 }
732 else if (ival > SHRT_MAX) {
733 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000734 "signed short integer is greater than maximum");
735 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 }
737 else
738 *p = (short) ival;
739 break;
740 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 case 'H': { /* short int sized bitfield, both signed and
743 unsigned allowed */
744 unsigned short *p = va_arg(*p_va, unsigned short *);
745 long ival;
746 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000747 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 ival = PyLong_AsUnsignedLongMask(arg);
749 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000750 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 else
752 *p = (unsigned short) ival;
753 break;
754 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 case 'i': {/* signed int */
757 int *p = va_arg(*p_va, int *);
758 long ival;
759 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000760 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 ival = PyLong_AsLong(arg);
762 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000763 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 else if (ival > INT_MAX) {
765 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000766 "signed integer is greater than maximum");
767 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 }
769 else if (ival < INT_MIN) {
770 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000771 "signed integer is less than minimum");
772 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 }
774 else
775 *p = ival;
776 break;
777 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 case 'I': { /* int sized bitfield, both signed and
780 unsigned allowed */
781 unsigned int *p = va_arg(*p_va, unsigned int *);
782 unsigned int ival;
783 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000784 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
786 if (ival == (unsigned int)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000787 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 else
789 *p = ival;
790 break;
791 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 case 'n': /* Py_ssize_t */
794 {
795 PyObject *iobj;
796 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
797 Py_ssize_t ival = -1;
798 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000799 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 iobj = PyNumber_Index(arg);
801 if (iobj != NULL) {
802 ival = PyLong_AsSsize_t(iobj);
803 Py_DECREF(iobj);
804 }
805 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000806 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 *p = ival;
808 break;
809 }
810 case 'l': {/* long int */
811 long *p = va_arg(*p_va, long *);
812 long ival;
813 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000814 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 ival = PyLong_AsLong(arg);
816 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000817 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 else
819 *p = ival;
820 break;
821 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 case 'k': { /* long sized bitfield */
824 unsigned long *p = va_arg(*p_va, unsigned long *);
825 unsigned long ival;
826 if (PyLong_Check(arg))
827 ival = PyLong_AsUnsignedLongMask(arg);
828 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200829 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 *p = ival;
831 break;
832 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000833
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700834 case 'L': {/* long long */
835 long long *p = va_arg( *p_va, long long * );
836 long long ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000837 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000838 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 ival = PyLong_AsLongLong(arg);
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700840 if (ival == (long long)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000841 RETURN_ERR_OCCURRED;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000842 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 break;
845 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 case 'K': { /* long long sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700848 unsigned long long *p = va_arg(*p_va, unsigned long long *);
849 unsigned long long ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 if (PyLong_Check(arg))
851 ival = PyLong_AsUnsignedLongLongMask(arg);
852 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200853 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 *p = ival;
855 break;
856 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 case 'f': {/* float */
859 float *p = va_arg(*p_va, float *);
860 double dval = PyFloat_AsDouble(arg);
861 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000862 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 else
864 *p = (float) dval;
865 break;
866 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 case 'd': {/* double */
869 double *p = va_arg(*p_va, double *);
870 double dval = PyFloat_AsDouble(arg);
871 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000872 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 else
874 *p = dval;
875 break;
876 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 case 'D': {/* complex double */
879 Py_complex *p = va_arg(*p_va, Py_complex *);
880 Py_complex cval;
881 cval = PyComplex_AsCComplex(arg);
882 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000883 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 else
885 *p = cval;
886 break;
887 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 case 'c': {/* char */
890 char *p = va_arg(*p_va, char *);
891 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
892 *p = PyBytes_AS_STRING(arg)[0];
Eli Bendersky906b88f2011-07-29 07:05:08 +0300893 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
894 *p = PyByteArray_AS_STRING(arg)[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 else
896 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
897 break;
898 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 case 'C': {/* unicode char */
901 int *p = va_arg(*p_va, int *);
Victor Stinnere1335c72011-10-04 20:53:03 +0200902 int kind;
903 void *data;
904
905 if (!PyUnicode_Check(arg))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 return converterr("a unicode character", arg, msgbuf, bufsize);
Victor Stinnere1335c72011-10-04 20:53:03 +0200907
908 if (PyUnicode_READY(arg))
909 RETURN_ERR_OCCURRED;
910
911 if (PyUnicode_GET_LENGTH(arg) != 1)
912 return converterr("a unicode character", arg, msgbuf, bufsize);
913
914 kind = PyUnicode_KIND(arg);
915 data = PyUnicode_DATA(arg);
916 *p = PyUnicode_READ(kind, data, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 break;
918 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000919
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700920 case 'p': {/* boolean *p*redicate */
921 int *p = va_arg(*p_va, int *);
922 int val = PyObject_IsTrue(arg);
923 if (val > 0)
924 *p = 1;
925 else if (val == 0)
926 *p = 0;
927 else
928 RETURN_ERR_OCCURRED;
929 break;
930 }
931
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000932 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000934
Serhiy Storchakab757c832014-12-05 22:25:22 +0200935 case 'y': {/* any bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 void **p = (void **)va_arg(*p_va, char **);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200937 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 Py_ssize_t count;
939 if (*format == '*') {
940 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
941 return converterr(buf, arg, msgbuf, bufsize);
942 format++;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400943 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 return converterr(
945 "(cleanup problem)",
946 arg, msgbuf, bufsize);
947 }
948 break;
949 }
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200950 count = convertbuffer(arg, (const void **)p, &buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 if (count < 0)
952 return converterr(buf, arg, msgbuf, bufsize);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000953 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 FETCH_SIZE;
955 STORE_SIZE(count);
956 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000957 } else {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300958 if (strlen(*p) != (size_t)count) {
959 PyErr_SetString(PyExc_ValueError, "embedded null byte");
960 RETURN_ERR_OCCURRED;
961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 }
963 break;
964 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000965
Serhiy Storchakab757c832014-12-05 22:25:22 +0200966 case 's': /* text string or bytes-like object */
967 case 'z': /* text string, bytes-like object or None */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000968 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000970 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000972
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000973 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
975 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200976 Py_ssize_t len;
977 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
978 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 return converterr(CONV_UNICODE,
980 arg, msgbuf, bufsize);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200981 PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 }
Serhiy Storchakab757c832014-12-05 22:25:22 +0200983 else { /* any bytes-like object */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200984 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 if (getbuffer(arg, p, &buf) < 0)
986 return converterr(buf, arg, msgbuf, bufsize);
987 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400988 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 return converterr(
990 "(cleanup problem)",
991 arg, msgbuf, bufsize);
992 }
993 format++;
Serhiy Storchakab757c832014-12-05 22:25:22 +0200994 } else if (*format == '#') { /* a string or read-only bytes-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000995 /* "s#" or "z#" */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200996 const void **p = (const void **)va_arg(*p_va, const char **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000998
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000999 if (c == 'z' && arg == Py_None) {
1000 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 STORE_SIZE(0);
1002 }
1003 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001004 Py_ssize_t len;
1005 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1006 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 return converterr(CONV_UNICODE,
1008 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001009 *p = sarg;
1010 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 }
Serhiy Storchakab757c832014-12-05 22:25:22 +02001012 else { /* read-only bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 /* XXX Really? */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001014 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 Py_ssize_t count = convertbuffer(arg, p, &buf);
1016 if (count < 0)
1017 return converterr(buf, arg, msgbuf, bufsize);
1018 STORE_SIZE(count);
1019 }
1020 format++;
1021 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001022 /* "s" or "z" */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001023 const char **p = va_arg(*p_va, const char **);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001024 Py_ssize_t len;
1025 sarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001026
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001027 if (c == 'z' && arg == Py_None)
1028 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001030 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1031 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 return converterr(CONV_UNICODE,
1033 arg, msgbuf, bufsize);
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001034 if (strlen(sarg) != (size_t)len) {
1035 PyErr_SetString(PyExc_ValueError, "embedded null character");
1036 RETURN_ERR_OCCURRED;
1037 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001038 *p = sarg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 }
1040 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001041 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 }
1044 break;
1045 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001046
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001047 case 'u': /* raw unicode buffer (Py_UNICODE *) */
1048 case 'Z': /* raw unicode buffer or None */
1049 {
Brett Cannonb94767f2011-02-22 20:15:44 +00001050 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1051
Serhiy Storchakab757c832014-12-05 22:25:22 +02001052 if (*format == '#') {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001053 /* "u#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001055
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001056 if (c == 'Z' && arg == Py_None) {
1057 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 STORE_SIZE(0);
1059 }
1060 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001061 Py_ssize_t len;
1062 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001063 if (*p == NULL)
1064 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001065 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 }
Victor Stinner5216e6d2010-06-08 21:45:51 +00001067 else
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001068 return converterr(c == 'Z' ? "str or None" : "str",
1069 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 format++;
1071 } else {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001072 /* "u" or "Z" */
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001073 if (c == 'Z' && arg == Py_None)
1074 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +00001075 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001076 Py_ssize_t len;
1077 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001078 if (*p == NULL)
1079 RETURN_ERR_OCCURRED;
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001080 if (wcslen(*p) != (size_t)len) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001081 PyErr_SetString(PyExc_ValueError, "embedded null character");
1082 RETURN_ERR_OCCURRED;
1083 }
Victor Stinner06e49dd2010-06-13 18:21:50 +00001084 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001085 return converterr(c == 'Z' ? "str or None" : "str",
1086 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 }
1088 break;
1089 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 case 'e': {/* encoded string */
1092 char **buffer;
1093 const char *encoding;
1094 PyObject *s;
1095 int recode_strings;
1096 Py_ssize_t size;
1097 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 /* Get 'e' parameter: the encoding name */
1100 encoding = (const char *)va_arg(*p_va, const char *);
1101 if (encoding == NULL)
1102 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +00001103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 /* Get output buffer parameter:
1105 's' (recode all objects via Unicode) or
1106 't' (only recode non-string objects)
1107 */
1108 if (*format == 's')
1109 recode_strings = 1;
1110 else if (*format == 't')
1111 recode_strings = 0;
1112 else
1113 return converterr(
1114 "(unknown parser marker combination)",
1115 arg, msgbuf, bufsize);
1116 buffer = (char **)va_arg(*p_va, char **);
1117 format++;
1118 if (buffer == NULL)
1119 return converterr("(buffer is NULL)",
1120 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 /* Encode object */
1123 if (!recode_strings &&
1124 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1125 s = arg;
1126 Py_INCREF(s);
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001127 if (PyBytes_Check(arg)) {
1128 size = PyBytes_GET_SIZE(s);
1129 ptr = PyBytes_AS_STRING(s);
1130 }
1131 else {
1132 size = PyByteArray_GET_SIZE(s);
1133 ptr = PyByteArray_AS_STRING(s);
1134 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001136 else if (PyUnicode_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 /* Encode object; use default error handling */
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001138 s = PyUnicode_AsEncodedString(arg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 encoding,
1140 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 if (s == NULL)
1142 return converterr("(encoding failed)",
1143 arg, msgbuf, bufsize);
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001144 assert(PyBytes_Check(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 size = PyBytes_GET_SIZE(s);
1146 ptr = PyBytes_AS_STRING(s);
1147 if (ptr == NULL)
1148 ptr = "";
1149 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001150 else {
1151 return converterr(
1152 recode_strings ? "str" : "str, bytes or bytearray",
1153 arg, msgbuf, bufsize);
1154 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 /* Write output; output is guaranteed to be 0-terminated */
1157 if (*format == '#') {
1158 /* Using buffer length parameter '#':
1159
1160 - if *buffer is NULL, a new buffer of the
1161 needed size is allocated and the data
1162 copied into it; *buffer is updated to point
1163 to the new buffer; the caller is
1164 responsible for PyMem_Free()ing it after
1165 usage
1166
1167 - if *buffer is not NULL, the data is
1168 copied to *buffer; *buffer_len has to be
1169 set to the size of the buffer on input;
1170 buffer overflow is signalled with an error;
1171 buffer has to provide enough room for the
1172 encoded string plus the trailing 0-byte
1173
1174 - in both cases, *buffer_len is updated to
1175 the size of the buffer /excluding/ the
1176 trailing 0-byte
1177
1178 */
1179 FETCH_SIZE;
1180
1181 format++;
1182 if (q == NULL && q2 == NULL) {
1183 Py_DECREF(s);
1184 return converterr(
1185 "(buffer_len is NULL)",
1186 arg, msgbuf, bufsize);
1187 }
1188 if (*buffer == NULL) {
1189 *buffer = PyMem_NEW(char, size + 1);
1190 if (*buffer == NULL) {
1191 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001192 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001193 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001195 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 Py_DECREF(s);
1197 return converterr(
1198 "(cleanup problem)",
1199 arg, msgbuf, bufsize);
1200 }
1201 } else {
1202 if (size + 1 > BUFFER_LEN) {
1203 Py_DECREF(s);
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +02001204 PyErr_Format(PyExc_ValueError,
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001205 "encoded string too long "
1206 "(%zd, maximum length %zd)",
1207 (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1208 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 }
1210 }
1211 memcpy(*buffer, ptr, size+1);
1212 STORE_SIZE(size);
1213 } else {
1214 /* Using a 0-terminated buffer:
1215
1216 - the encoded string has to be 0-terminated
1217 for this variant to work; if it is not, an
1218 error raised
1219
1220 - a new buffer of the needed size is
1221 allocated and the data copied into it;
1222 *buffer is updated to point to the new
1223 buffer; the caller is responsible for
1224 PyMem_Free()ing it after usage
1225
1226 */
1227 if ((Py_ssize_t)strlen(ptr) != size) {
1228 Py_DECREF(s);
1229 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001230 "encoded string without null bytes",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 arg, msgbuf, bufsize);
1232 }
1233 *buffer = PyMem_NEW(char, size + 1);
1234 if (*buffer == NULL) {
1235 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001236 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001237 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001239 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 Py_DECREF(s);
1241 return converterr("(cleanup problem)",
1242 arg, msgbuf, bufsize);
1243 }
1244 memcpy(*buffer, ptr, size+1);
1245 }
1246 Py_DECREF(s);
1247 break;
1248 }
1249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 case 'S': { /* PyBytes object */
1251 PyObject **p = va_arg(*p_va, PyObject **);
1252 if (PyBytes_Check(arg))
1253 *p = arg;
1254 else
1255 return converterr("bytes", arg, msgbuf, bufsize);
1256 break;
1257 }
1258
1259 case 'Y': { /* PyByteArray object */
1260 PyObject **p = va_arg(*p_va, PyObject **);
1261 if (PyByteArray_Check(arg))
1262 *p = arg;
1263 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001264 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 break;
1266 }
1267
1268 case 'U': { /* PyUnicode object */
1269 PyObject **p = va_arg(*p_va, PyObject **);
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001270 if (PyUnicode_Check(arg)) {
1271 if (PyUnicode_READY(arg) == -1)
1272 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 *p = arg;
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001274 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 else
1276 return converterr("str", arg, msgbuf, bufsize);
1277 break;
1278 }
1279
1280 case 'O': { /* object */
1281 PyTypeObject *type;
1282 PyObject **p;
1283 if (*format == '!') {
1284 type = va_arg(*p_va, PyTypeObject*);
1285 p = va_arg(*p_va, PyObject **);
1286 format++;
1287 if (PyType_IsSubtype(arg->ob_type, type))
1288 *p = arg;
1289 else
1290 return converterr(type->tp_name, arg, msgbuf, bufsize);
1291
1292 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 else if (*format == '&') {
1294 typedef int (*converter)(PyObject *, void *);
1295 converter convert = va_arg(*p_va, converter);
1296 void *addr = va_arg(*p_va, void *);
1297 int res;
1298 format++;
1299 if (! (res = (*convert)(arg, addr)))
1300 return converterr("(unspecified)",
1301 arg, msgbuf, bufsize);
1302 if (res == Py_CLEANUP_SUPPORTED &&
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001303 addcleanup(addr, freelist, convert) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 return converterr("(cleanup problem)",
1305 arg, msgbuf, bufsize);
1306 }
1307 else {
1308 p = va_arg(*p_va, PyObject **);
1309 *p = arg;
1310 }
1311 break;
1312 }
1313
1314
Victor Stinner25e8ec42010-06-25 00:02:38 +00001315 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317
Victor Stinner25e8ec42010-06-25 00:02:38 +00001318 if (*format != '*')
1319 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001320 "(invalid use of 'w' format character)",
Victor Stinner25e8ec42010-06-25 00:02:38 +00001321 arg, msgbuf, bufsize);
1322 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323
Victor Stinner25e8ec42010-06-25 00:02:38 +00001324 /* Caller is interested in Py_buffer, and the object
1325 supports it directly. */
1326 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1327 PyErr_Clear();
R David Murray861470c2014-10-05 11:47:01 -04001328 return converterr("read-write bytes-like object",
1329 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 }
Victor Stinner8182b712010-07-28 00:40:58 +00001331 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1332 PyBuffer_Release((Py_buffer*)p);
1333 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1334 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001335 if (addcleanup(p, freelist, cleanup_buffer)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001336 return converterr(
1337 "(cleanup problem)",
1338 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 break;
1341 }
1342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 default:
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001344 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345
1346 }
1347
1348 *p_format = format;
1349 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001350
1351#undef FETCH_SIZE
1352#undef STORE_SIZE
1353#undef BUFFER_LEN
1354#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001355}
Guido van Rossumaa354651996-08-19 19:32:04 +00001356
Martin v. Löwis18e16552006-02-15 17:27:45 +00001357static Py_ssize_t
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001358convertbuffer(PyObject *arg, const void **p, const char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001359{
Victor Stinner5cb62392010-06-06 20:27:51 +00001360 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 Py_ssize_t count;
1362 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 *errmsg = NULL;
1365 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001366 if (pb != NULL && pb->bf_releasebuffer != NULL) {
R David Murray861470c2014-10-05 11:47:01 -04001367 *errmsg = "read-only bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 return -1;
1369 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001370
Victor Stinner8182b712010-07-28 00:40:58 +00001371 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 count = view.len;
1374 *p = view.buf;
1375 PyBuffer_Release(&view);
1376 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001377}
Guido van Rossumaa354651996-08-19 19:32:04 +00001378
Martin v. Löwis423be952008-08-13 15:53:07 +00001379static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001380getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001381{
Victor Stinner8182b712010-07-28 00:40:58 +00001382 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
R David Murray861470c2014-10-05 11:47:01 -04001383 *errmsg = "bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 return -1;
1385 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001386 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001387 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001388 *errmsg = "contiguous buffer";
1389 return -1;
1390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001392}
1393
Guido van Rossumaa354651996-08-19 19:32:04 +00001394/* Support for keyword arguments donated by
1395 Geoff Philbrick <philbric@delphi.hks.com> */
1396
Tim Petersf8cd3e82001-10-27 04:26:57 +00001397/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001398int
1399PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 PyObject *keywords,
1401 const char *format,
1402 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 int retval;
1405 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 if ((args == NULL || !PyTuple_Check(args)) ||
1408 (keywords != NULL && !PyDict_Check(keywords)) ||
1409 format == NULL ||
1410 kwlist == NULL)
1411 {
1412 PyErr_BadInternalCall();
1413 return 0;
1414 }
Tim Peters45772cd2001-10-27 03:58:40 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 va_start(va, kwlist);
1417 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1418 va_end(va);
1419 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001420}
1421
1422int
1423_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 PyObject *keywords,
1425 const char *format,
1426 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 int retval;
1429 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 if ((args == NULL || !PyTuple_Check(args)) ||
1432 (keywords != NULL && !PyDict_Check(keywords)) ||
1433 format == NULL ||
1434 kwlist == NULL)
1435 {
1436 PyErr_BadInternalCall();
1437 return 0;
1438 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 va_start(va, kwlist);
1441 retval = vgetargskeywords(args, keywords, format,
1442 kwlist, &va, FLAG_SIZE_T);
1443 va_end(va);
1444 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001445}
1446
1447
Brett Cannon711e7d92004-07-10 22:20:32 +00001448int
1449PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001450 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001451 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001452 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 int retval;
1455 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 if ((args == NULL || !PyTuple_Check(args)) ||
1458 (keywords != NULL && !PyDict_Check(keywords)) ||
1459 format == NULL ||
1460 kwlist == NULL)
1461 {
1462 PyErr_BadInternalCall();
1463 return 0;
1464 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001465
Benjamin Peterson0c212142016-09-20 20:39:33 -07001466 va_copy(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001469 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001471}
1472
1473int
1474_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 PyObject *keywords,
1476 const char *format,
1477 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 int retval;
1480 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 if ((args == NULL || !PyTuple_Check(args)) ||
1483 (keywords != NULL && !PyDict_Check(keywords)) ||
1484 format == NULL ||
1485 kwlist == NULL)
1486 {
1487 PyErr_BadInternalCall();
1488 return 0;
1489 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001490
Benjamin Peterson0c212142016-09-20 20:39:33 -07001491 va_copy(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 retval = vgetargskeywords(args, keywords, format,
1494 kwlist, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001495 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001497}
1498
Benjamin Petersonfb886362010-04-24 18:21:17 +00001499int
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001500_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1501 struct _PyArg_Parser *parser, ...)
1502{
1503 int retval;
1504 va_list va;
1505
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001506 va_start(va, parser);
1507 retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1508 va_end(va);
1509 return retval;
1510}
1511
1512int
1513_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1514 struct _PyArg_Parser *parser, ...)
1515{
1516 int retval;
1517 va_list va;
1518
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001519 va_start(va, parser);
1520 retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
1521 va_end(va);
1522 return retval;
1523}
1524
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001525int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001526_PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001527 struct _PyArg_Parser *parser, ...)
1528{
1529 int retval;
1530 va_list va;
1531
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001532 va_start(va, parser);
1533 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1534 va_end(va);
1535 return retval;
1536}
1537
1538int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001539_PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001540 struct _PyArg_Parser *parser, ...)
1541{
1542 int retval;
1543 va_list va;
1544
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001545 va_start(va, parser);
1546 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T);
1547 va_end(va);
1548 return retval;
1549}
1550
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001551
1552int
1553_PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1554 struct _PyArg_Parser *parser, va_list va)
1555{
1556 int retval;
1557 va_list lva;
1558
Benjamin Peterson0c212142016-09-20 20:39:33 -07001559 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001560
1561 retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001562 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001563 return retval;
1564}
1565
1566int
1567_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1568 struct _PyArg_Parser *parser, va_list va)
1569{
1570 int retval;
1571 va_list lva;
1572
Benjamin Peterson0c212142016-09-20 20:39:33 -07001573 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001574
1575 retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001576 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001577 return retval;
1578}
1579
1580int
Benjamin Petersonfb886362010-04-24 18:21:17 +00001581PyArg_ValidateKeywordArguments(PyObject *kwargs)
1582{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001583 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 PyErr_BadInternalCall();
1585 return 0;
1586 }
1587 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1588 PyErr_SetString(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02001589 "keywords must be strings");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 return 0;
1591 }
1592 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001593}
1594
Christian Heimes380f7f22008-02-28 11:19:05 +00001595#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001596
Guido van Rossumaa354651996-08-19 19:32:04 +00001597static int
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001598vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 char msgbuf[512];
1602 int levels[32];
Serhiy Storchaka17414412017-01-17 10:07:25 +02001603 const char *fname, *msg, *custom_msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 int min = INT_MAX;
Larry Hastings83a9f482012-03-20 20:06:16 +00001605 int max = INT_MAX;
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001606 int i, pos, len;
1607 int skip = 0;
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001608 Py_ssize_t nargs, nkwargs;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001609 PyObject *current_arg;
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001610 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
Benjamin Peterson40be9e52014-02-11 10:09:27 -05001611 freelist_t freelist;
1612
1613 freelist.entries = static_entries;
1614 freelist.first_available = 0;
1615 freelist.entries_malloced = 0;
Tim Petersf4331c12001-10-27 00:17:34 +00001616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 assert(args != NULL && PyTuple_Check(args));
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001618 assert(kwargs == NULL || PyDict_Check(kwargs));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 assert(format != NULL);
1620 assert(kwlist != NULL);
1621 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 /* grab the function name or custom error msg first (mutually exclusive) */
1624 fname = strchr(format, ':');
1625 if (fname) {
1626 fname++;
1627 custom_msg = NULL;
1628 }
1629 else {
1630 custom_msg = strchr(format,';');
1631 if (custom_msg)
1632 custom_msg++;
1633 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001634
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001635 /* scan kwlist and count the number of positional-only parameters */
1636 for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 /* scan kwlist and get greatest possible nbr of args */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001639 for (len = pos; kwlist[len]; len++) {
1640 if (!*kwlist[len]) {
1641 PyErr_SetString(PyExc_SystemError,
1642 "Empty keyword parameter name");
1643 return cleanreturn(0, &freelist);
1644 }
1645 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001646
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001647 if (len > STATIC_FREELIST_ENTRIES) {
1648 freelist.entries = PyMem_NEW(freelistentry_t, len);
1649 if (freelist.entries == NULL) {
1650 PyErr_NoMemory();
1651 return 0;
1652 }
1653 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -05001654 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 nargs = PyTuple_GET_SIZE(args);
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001657 nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
1658 if (nargs + nkwargs > len) {
Oren Milmanbf9075a2017-08-23 21:16:48 +03001659 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
1660 messages in some special cases (see bpo-31229). */
Victor Stinner6ced7c42011-03-21 18:15:42 +01001661 PyErr_Format(PyExc_TypeError,
Oren Milmanbf9075a2017-08-23 21:16:48 +03001662 "%.200s%s takes at most %d %sargument%s (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 (fname == NULL) ? "function" : fname,
1664 (fname == NULL) ? "" : "()",
1665 len,
Oren Milmanbf9075a2017-08-23 21:16:48 +03001666 (nargs == 0) ? "keyword " : "",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 (len == 1) ? "" : "s",
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001668 nargs + nkwargs);
Benjamin Peterson01feaec2012-03-16 13:25:58 -05001669 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 }
Tim Petersc2f01122001-10-27 07:25:06 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1673 for (i = 0; i < len; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 if (*format == '|') {
Larry Hastings83a9f482012-03-20 20:06:16 +00001675 if (min != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001676 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001677 "Invalid format string (| specified twice)");
1678 return cleanreturn(0, &freelist);
1679 }
1680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 min = i;
1682 format++;
Larry Hastings83a9f482012-03-20 20:06:16 +00001683
1684 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001685 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001686 "Invalid format string ($ before |)");
1687 return cleanreturn(0, &freelist);
1688 }
1689 }
1690 if (*format == '$') {
1691 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001692 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001693 "Invalid format string ($ specified twice)");
1694 return cleanreturn(0, &freelist);
1695 }
1696
1697 max = i;
1698 format++;
1699
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001700 if (max < pos) {
1701 PyErr_SetString(PyExc_SystemError,
1702 "Empty parameter name after $");
1703 return cleanreturn(0, &freelist);
1704 }
1705 if (skip) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001706 /* Now we know the minimal and the maximal numbers of
1707 * positional arguments and can raise an exception with
1708 * informative message (see below). */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001709 break;
1710 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001711 if (max < nargs) {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03001712 if (max == 0) {
1713 PyErr_Format(PyExc_TypeError,
1714 "%.200s%s takes no positional arguments",
1715 (fname == NULL) ? "function" : fname,
1716 (fname == NULL) ? "" : "()");
1717 }
1718 else {
1719 PyErr_Format(PyExc_TypeError,
1720 "%.200s%s takes %s %d positional arguments"
1721 " (%d given)",
1722 (fname == NULL) ? "function" : fname,
1723 (fname == NULL) ? "" : "()",
1724 (min != INT_MAX) ? "at most" : "exactly",
1725 max, nargs);
1726 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001727 return cleanreturn(0, &freelist);
1728 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 }
1730 if (IS_END_OF_FORMAT(*format)) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001731 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 "More keyword list entries (%d) than "
1733 "format specifiers (%d)", len, i);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001734 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001736 if (!skip) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02001737 if (i < nargs) {
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001738 current_arg = PyTuple_GET_ITEM(args, i);
Serhiy Storchaka17414412017-01-17 10:07:25 +02001739 }
1740 else if (nkwargs && i >= pos) {
1741 current_arg = PyDict_GetItemString(kwargs, kwlist[i]);
1742 if (current_arg)
1743 --nkwargs;
1744 }
1745 else {
1746 current_arg = NULL;
1747 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001748
1749 if (current_arg) {
1750 msg = convertitem(current_arg, &format, p_va, flags,
1751 levels, msgbuf, sizeof(msgbuf), &freelist);
1752 if (msg) {
1753 seterror(i+1, msg, levels, fname, custom_msg);
1754 return cleanreturn(0, &freelist);
1755 }
1756 continue;
1757 }
1758
1759 if (i < min) {
1760 if (i < pos) {
1761 assert (min == INT_MAX);
1762 assert (max == INT_MAX);
1763 skip = 1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001764 /* At that moment we still don't know the minimal and
1765 * the maximal numbers of positional arguments. Raising
1766 * an exception is deferred until we encounter | and $
1767 * or the end of the format. */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001768 }
1769 else {
Michael Seifert64c8f702017-04-09 09:47:12 +02001770 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
1771 "argument '%s' (pos %d)",
1772 (fname == NULL) ? "function" : fname,
1773 (fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02001774 kwlist[i], i+1);
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001775 return cleanreturn(0, &freelist);
1776 }
1777 }
1778 /* current code reports success when all required args
1779 * fulfilled and no keyword args left, with no further
1780 * validation. XXX Maybe skip this in debug build ?
1781 */
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001782 if (!nkwargs && !skip) {
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001783 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 }
1785 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001786
Leo Ariasc3d95082018-02-03 18:36:10 -06001787 /* We are into optional args, skip through to any remaining
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 * keyword args */
1789 msg = skipitem(&format, p_va, flags);
1790 if (msg) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001791 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001793 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 }
1795 }
Tim Petersb054be42001-10-27 05:07:41 +00001796
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001797 if (skip) {
1798 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02001799 "%.200s%s takes %s %d positional arguments"
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001800 " (%d given)",
Michael Seifert64c8f702017-04-09 09:47:12 +02001801 (fname == NULL) ? "function" : fname,
1802 (fname == NULL) ? "" : "()",
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001803 (Py_MIN(pos, min) < i) ? "at least" : "exactly",
1804 Py_MIN(pos, min), nargs);
1805 return cleanreturn(0, &freelist);
1806 }
1807
Larry Hastings83a9f482012-03-20 20:06:16 +00001808 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001809 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 "more argument specifiers than keyword list entries "
1811 "(remaining format:'%s')", format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001812 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 }
Tim Petersc2f01122001-10-27 07:25:06 +00001814
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001815 if (nkwargs > 0) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02001816 PyObject *key;
1817 Py_ssize_t j;
1818 /* make sure there are no arguments given by name and position */
1819 for (i = pos; i < nargs; i++) {
1820 current_arg = PyDict_GetItemString(kwargs, kwlist[i]);
1821 if (current_arg) {
1822 /* arg present in tuple and in dict */
1823 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02001824 "argument for %.200s%s given by name ('%s') "
Serhiy Storchaka17414412017-01-17 10:07:25 +02001825 "and position (%d)",
Michael Seifert64c8f702017-04-09 09:47:12 +02001826 (fname == NULL) ? "function" : fname,
1827 (fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02001828 kwlist[i], i+1);
1829 return cleanreturn(0, &freelist);
1830 }
1831 }
1832 /* make sure there are no extraneous keyword arguments */
1833 j = 0;
1834 while (PyDict_Next(kwargs, &j, &key, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 int match = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 if (!PyUnicode_Check(key)) {
1837 PyErr_SetString(PyExc_TypeError,
1838 "keywords must be strings");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001839 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02001841 for (i = pos; i < len; i++) {
1842 if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001843 match = 1;
1844 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 }
1846 }
1847 if (!match) {
1848 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001849 "'%U' is an invalid keyword "
Michael Seifert64c8f702017-04-09 09:47:12 +02001850 "argument for %.200s%s",
1851 key,
1852 (fname == NULL) ? "this function" : fname,
1853 (fname == NULL) ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001854 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 }
1856 }
1857 }
1858
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001859 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001860}
1861
1862
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001863/* List of static parsers. */
1864static struct _PyArg_Parser *static_arg_parsers = NULL;
1865
1866static int
1867parser_init(struct _PyArg_Parser *parser)
1868{
1869 const char * const *keywords;
1870 const char *format, *msg;
1871 int i, len, min, max, nkw;
1872 PyObject *kwtuple;
1873
1874 assert(parser->format != NULL);
1875 assert(parser->keywords != NULL);
1876 if (parser->kwtuple != NULL) {
1877 return 1;
1878 }
1879
1880 /* grab the function name or custom error msg first (mutually exclusive) */
1881 parser->fname = strchr(parser->format, ':');
1882 if (parser->fname) {
1883 parser->fname++;
1884 parser->custom_msg = NULL;
1885 }
1886 else {
1887 parser->custom_msg = strchr(parser->format,';');
1888 if (parser->custom_msg)
1889 parser->custom_msg++;
1890 }
1891
1892 keywords = parser->keywords;
1893 /* scan keywords and count the number of positional-only parameters */
1894 for (i = 0; keywords[i] && !*keywords[i]; i++) {
1895 }
1896 parser->pos = i;
1897 /* scan keywords and get greatest possible nbr of args */
1898 for (; keywords[i]; i++) {
1899 if (!*keywords[i]) {
1900 PyErr_SetString(PyExc_SystemError,
1901 "Empty keyword parameter name");
1902 return 0;
1903 }
1904 }
1905 len = i;
1906
1907 min = max = INT_MAX;
1908 format = parser->format;
1909 for (i = 0; i < len; i++) {
1910 if (*format == '|') {
1911 if (min != INT_MAX) {
1912 PyErr_SetString(PyExc_SystemError,
1913 "Invalid format string (| specified twice)");
1914 return 0;
1915 }
1916 if (max != INT_MAX) {
1917 PyErr_SetString(PyExc_SystemError,
1918 "Invalid format string ($ before |)");
1919 return 0;
1920 }
1921 min = i;
1922 format++;
1923 }
1924 if (*format == '$') {
1925 if (max != INT_MAX) {
1926 PyErr_SetString(PyExc_SystemError,
1927 "Invalid format string ($ specified twice)");
1928 return 0;
1929 }
1930 if (i < parser->pos) {
1931 PyErr_SetString(PyExc_SystemError,
1932 "Empty parameter name after $");
1933 return 0;
1934 }
1935 max = i;
1936 format++;
1937 }
1938 if (IS_END_OF_FORMAT(*format)) {
1939 PyErr_Format(PyExc_SystemError,
1940 "More keyword list entries (%d) than "
1941 "format specifiers (%d)", len, i);
1942 return 0;
1943 }
1944
1945 msg = skipitem(&format, NULL, 0);
1946 if (msg) {
1947 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1948 format);
1949 return 0;
1950 }
1951 }
1952 parser->min = Py_MIN(min, len);
1953 parser->max = Py_MIN(max, len);
1954
1955 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1956 PyErr_Format(PyExc_SystemError,
1957 "more argument specifiers than keyword list entries "
1958 "(remaining format:'%s')", format);
1959 return 0;
1960 }
1961
1962 nkw = len - parser->pos;
1963 kwtuple = PyTuple_New(nkw);
1964 if (kwtuple == NULL) {
1965 return 0;
1966 }
1967 keywords = parser->keywords + parser->pos;
1968 for (i = 0; i < nkw; i++) {
1969 PyObject *str = PyUnicode_FromString(keywords[i]);
1970 if (str == NULL) {
1971 Py_DECREF(kwtuple);
1972 return 0;
1973 }
1974 PyUnicode_InternInPlace(&str);
1975 PyTuple_SET_ITEM(kwtuple, i, str);
1976 }
1977 parser->kwtuple = kwtuple;
1978
1979 assert(parser->next == NULL);
1980 parser->next = static_arg_parsers;
1981 static_arg_parsers = parser;
1982 return 1;
1983}
1984
1985static void
1986parser_clear(struct _PyArg_Parser *parser)
1987{
1988 Py_CLEAR(parser->kwtuple);
1989}
1990
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001991static PyObject*
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001992find_keyword(PyObject *kwargs, PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001993{
1994 Py_ssize_t i, nkwargs;
1995
Serhiy Storchaka17414412017-01-17 10:07:25 +02001996 if (kwargs != NULL) {
1997 return PyDict_GetItem(kwargs, key);
1998 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001999 nkwargs = PyTuple_GET_SIZE(kwnames);
2000 for (i=0; i < nkwargs; i++) {
2001 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
2002
2003 /* ptr==ptr should match in most cases since keyword keys
2004 should be interned strings */
2005 if (kwname == key) {
2006 return kwstack[i];
2007 }
2008 if (!PyUnicode_Check(kwname)) {
2009 /* ignore non-string keyword keys:
Serhiy Storchaka17414412017-01-17 10:07:25 +02002010 an error will be raised below */
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002011 continue;
2012 }
2013 if (_PyUnicode_EQ(kwname, key)) {
2014 return kwstack[i];
2015 }
2016 }
2017 return NULL;
2018}
2019
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002020static int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002021vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002022 PyObject *kwargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002023 struct _PyArg_Parser *parser,
2024 va_list *p_va, int flags)
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002025{
2026 PyObject *kwtuple;
2027 char msgbuf[512];
2028 int levels[32];
2029 const char *format;
2030 const char *msg;
2031 PyObject *keyword;
2032 int i, pos, len;
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002033 Py_ssize_t nkwargs;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002034 PyObject *current_arg;
2035 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
2036 freelist_t freelist;
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002037 PyObject *const *kwstack = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002038
2039 freelist.entries = static_entries;
2040 freelist.first_available = 0;
2041 freelist.entries_malloced = 0;
2042
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002043 assert(kwargs == NULL || PyDict_Check(kwargs));
Serhiy Storchaka17414412017-01-17 10:07:25 +02002044 assert(kwargs == NULL || kwnames == NULL);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002045 assert(p_va != NULL);
2046
Victor Stinner80ab22f2017-01-16 23:16:47 +01002047 if (parser == NULL) {
2048 PyErr_BadInternalCall();
2049 return 0;
2050 }
2051
2052 if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2053 PyErr_BadInternalCall();
2054 return 0;
2055 }
2056
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002057 if (!parser_init(parser)) {
2058 return 0;
2059 }
2060
2061 kwtuple = parser->kwtuple;
2062 pos = parser->pos;
Segev Finere7bc7aa2017-07-26 18:58:25 +03002063 len = pos + (int)PyTuple_GET_SIZE(kwtuple);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002064
2065 if (len > STATIC_FREELIST_ENTRIES) {
2066 freelist.entries = PyMem_NEW(freelistentry_t, len);
2067 if (freelist.entries == NULL) {
2068 PyErr_NoMemory();
2069 return 0;
2070 }
2071 freelist.entries_malloced = 1;
2072 }
2073
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002074 if (kwargs != NULL) {
2075 nkwargs = PyDict_GET_SIZE(kwargs);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002076 }
2077 else if (kwnames != NULL) {
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002078 nkwargs = PyTuple_GET_SIZE(kwnames);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002079 kwstack = args + nargs;
2080 }
2081 else {
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002082 nkwargs = 0;
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002083 }
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002084 if (nargs + nkwargs > len) {
Oren Milmanbf9075a2017-08-23 21:16:48 +03002085 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2086 messages in some special cases (see bpo-31229). */
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002087 PyErr_Format(PyExc_TypeError,
Oren Milmanbf9075a2017-08-23 21:16:48 +03002088 "%.200s%s takes at most %d %sargument%s (%zd given)",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002089 (parser->fname == NULL) ? "function" : parser->fname,
2090 (parser->fname == NULL) ? "" : "()",
2091 len,
Oren Milmanbf9075a2017-08-23 21:16:48 +03002092 (nargs == 0) ? "keyword " : "",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002093 (len == 1) ? "" : "s",
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002094 nargs + nkwargs);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002095 return cleanreturn(0, &freelist);
2096 }
2097 if (parser->max < nargs) {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002098 if (parser->max == 0) {
2099 PyErr_Format(PyExc_TypeError,
Serhiy Storchakaf9f1cca2017-06-09 19:27:06 +03002100 "%.200s%s takes no positional arguments",
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002101 (parser->fname == NULL) ? "function" : parser->fname,
2102 (parser->fname == NULL) ? "" : "()");
2103 }
2104 else {
2105 PyErr_Format(PyExc_TypeError,
Serhiy Storchakaf9f1cca2017-06-09 19:27:06 +03002106 "%.200s%s takes %s %d positional arguments (%d given)",
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002107 (parser->fname == NULL) ? "function" : parser->fname,
2108 (parser->fname == NULL) ? "" : "()",
2109 (parser->min != INT_MAX) ? "at most" : "exactly",
2110 parser->max, nargs);
2111 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002112 return cleanreturn(0, &freelist);
2113 }
2114
2115 format = parser->format;
2116 /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
2117 for (i = 0; i < len; i++) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002118 if (*format == '|') {
2119 format++;
2120 }
2121 if (*format == '$') {
2122 format++;
2123 }
2124 assert(!IS_END_OF_FORMAT(*format));
2125
Serhiy Storchaka17414412017-01-17 10:07:25 +02002126 if (i < nargs) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002127 current_arg = args[i];
Victor Stinnera9efb2f2016-09-09 17:40:22 -07002128 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002129 else if (nkwargs && i >= pos) {
2130 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2131 current_arg = find_keyword(kwargs, kwnames, kwstack, keyword);
2132 if (current_arg)
2133 --nkwargs;
2134 }
2135 else {
2136 current_arg = NULL;
2137 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002138
2139 if (current_arg) {
2140 msg = convertitem(current_arg, &format, p_va, flags,
2141 levels, msgbuf, sizeof(msgbuf), &freelist);
2142 if (msg) {
2143 seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2144 return cleanreturn(0, &freelist);
2145 }
2146 continue;
2147 }
2148
2149 if (i < parser->min) {
2150 /* Less arguments than required */
2151 if (i < pos) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002152 Py_ssize_t min = Py_MIN(pos, parser->min);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002153 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02002154 "%.200s%s takes %s %d positional arguments"
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002155 " (%d given)",
Michael Seifert64c8f702017-04-09 09:47:12 +02002156 (parser->fname == NULL) ? "function" : parser->fname,
2157 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02002158 min < parser->max ? "at least" : "exactly",
2159 min, nargs);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002160 }
2161 else {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002162 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
Michael Seifert64c8f702017-04-09 09:47:12 +02002163 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
2164 "argument '%U' (pos %d)",
2165 (parser->fname == NULL) ? "function" : parser->fname,
2166 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002167 keyword, i+1);
2168 }
2169 return cleanreturn(0, &freelist);
2170 }
2171 /* current code reports success when all required args
2172 * fulfilled and no keyword args left, with no further
2173 * validation. XXX Maybe skip this in debug build ?
2174 */
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002175 if (!nkwargs) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002176 return cleanreturn(1, &freelist);
2177 }
2178
Leo Ariasc3d95082018-02-03 18:36:10 -06002179 /* We are into optional args, skip through to any remaining
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002180 * keyword args */
2181 msg = skipitem(&format, p_va, flags);
2182 assert(msg == NULL);
2183 }
2184
2185 assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2186
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002187 if (nkwargs > 0) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002188 Py_ssize_t j;
2189 /* make sure there are no arguments given by name and position */
2190 for (i = pos; i < nargs; i++) {
2191 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2192 current_arg = find_keyword(kwargs, kwnames, kwstack, keyword);
2193 if (current_arg) {
2194 /* arg present in tuple and in dict */
2195 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02002196 "argument for %.200s%s given by name ('%U') "
Serhiy Storchaka17414412017-01-17 10:07:25 +02002197 "and position (%d)",
Michael Seifert64c8f702017-04-09 09:47:12 +02002198 (parser->fname == NULL) ? "function" : parser->fname,
2199 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02002200 keyword, i+1);
2201 return cleanreturn(0, &freelist);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002202 }
2203 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002204 /* make sure there are no extraneous keyword arguments */
2205 j = 0;
2206 while (1) {
2207 int match;
2208 if (kwargs != NULL) {
2209 if (!PyDict_Next(kwargs, &j, &keyword, NULL))
2210 break;
2211 }
2212 else {
2213 if (j >= PyTuple_GET_SIZE(kwnames))
2214 break;
2215 keyword = PyTuple_GET_ITEM(kwnames, j);
2216 j++;
2217 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002218
Serhiy Storchaka17414412017-01-17 10:07:25 +02002219 if (!PyUnicode_Check(keyword)) {
2220 PyErr_SetString(PyExc_TypeError,
2221 "keywords must be strings");
2222 return cleanreturn(0, &freelist);
2223 }
2224 match = PySequence_Contains(kwtuple, keyword);
2225 if (match <= 0) {
2226 if (!match) {
2227 PyErr_Format(PyExc_TypeError,
2228 "'%U' is an invalid keyword "
Michael Seifert64c8f702017-04-09 09:47:12 +02002229 "argument for %.200s%s",
2230 keyword,
2231 (parser->fname == NULL) ? "this function" : parser->fname,
2232 (parser->fname == NULL) ? "" : "()");
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002233 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002234 return cleanreturn(0, &freelist);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002235 }
2236 }
2237 }
2238
2239 return cleanreturn(1, &freelist);
2240}
2241
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002242static int
2243vgetargskeywordsfast(PyObject *args, PyObject *keywords,
2244 struct _PyArg_Parser *parser, va_list *p_va, int flags)
2245{
2246 PyObject **stack;
2247 Py_ssize_t nargs;
2248
Victor Stinner80ab22f2017-01-16 23:16:47 +01002249 if (args == NULL
2250 || !PyTuple_Check(args)
2251 || (keywords != NULL && !PyDict_Check(keywords)))
2252 {
2253 PyErr_BadInternalCall();
2254 return 0;
2255 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002256
2257 stack = &PyTuple_GET_ITEM(args, 0);
2258 nargs = PyTuple_GET_SIZE(args);
2259 return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
2260 parser, p_va, flags);
2261}
2262
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002263
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002264static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002265skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00002266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 const char *format = *p_format;
2268 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00002271
Larry Hastingsa3479012012-05-08 23:52:03 -07002272 /*
2273 * codes that take a single data pointer as an argument
2274 * (the type of the pointer is irrelevant)
2275 */
Georg Brandl6dd14612005-09-14 19:29:53 +00002276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 case 'b': /* byte -- very short int */
2278 case 'B': /* byte as bitfield */
2279 case 'h': /* short int */
2280 case 'H': /* short int as bitfield */
2281 case 'i': /* int */
2282 case 'I': /* int sized bitfield */
2283 case 'l': /* long int */
2284 case 'k': /* long int sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -07002285 case 'L': /* long long */
2286 case 'K': /* long long sized bitfield */
Larry Hastingsa3479012012-05-08 23:52:03 -07002287 case 'n': /* Py_ssize_t */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 case 'f': /* float */
2289 case 'd': /* double */
2290 case 'D': /* complex double */
2291 case 'c': /* char */
2292 case 'C': /* unicode char */
Larry Hastings10ba07a2012-05-07 02:44:50 -07002293 case 'p': /* boolean predicate */
Larry Hastingsa3479012012-05-08 23:52:03 -07002294 case 'S': /* string object */
2295 case 'Y': /* string object */
2296 case 'U': /* unicode string object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002298 if (p_va != NULL) {
2299 (void) va_arg(*p_va, void *);
2300 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002301 break;
2302 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 case 'e': /* string with encoding */
2307 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002308 if (p_va != NULL) {
2309 (void) va_arg(*p_va, const char *);
2310 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 if (!(*format == 's' || *format == 't'))
2312 /* after 'e', only 's' and 't' is allowed */
2313 goto err;
2314 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 }
Stefan Krahf432a322017-08-21 13:09:59 +02002316 /* fall through */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 case 's': /* string */
2319 case 'z': /* string or None */
2320 case 'y': /* bytes */
2321 case 'u': /* unicode string */
Larry Hastingsd9e4a412012-05-08 03:51:18 -07002322 case 'Z': /* unicode string or None */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 case 'w': /* buffer, read-write */
2324 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002325 if (p_va != NULL) {
2326 (void) va_arg(*p_va, char **);
2327 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 if (*format == '#') {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002329 if (p_va != NULL) {
2330 if (flags & FLAG_SIZE_T)
2331 (void) va_arg(*p_va, Py_ssize_t *);
2332 else
2333 (void) va_arg(*p_va, int *);
2334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 format++;
Serhiy Storchaka504373c2018-07-11 17:41:43 +03002336 } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w')
2337 && *format == '*')
2338 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 format++;
2340 }
2341 break;
2342 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 case 'O': /* object */
2345 {
2346 if (*format == '!') {
2347 format++;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002348 if (p_va != NULL) {
2349 (void) va_arg(*p_va, PyTypeObject*);
2350 (void) va_arg(*p_va, PyObject **);
2351 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 }
2353 else if (*format == '&') {
2354 typedef int (*converter)(PyObject *, void *);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002355 if (p_va != NULL) {
2356 (void) va_arg(*p_va, converter);
2357 (void) va_arg(*p_va, void *);
2358 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 format++;
2360 }
2361 else {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002362 if (p_va != NULL) {
2363 (void) va_arg(*p_va, PyObject **);
2364 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 }
2366 break;
2367 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 case '(': /* bypass tuple, not handled at all previously */
2370 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002371 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 for (;;) {
2373 if (*format==')')
2374 break;
2375 if (IS_END_OF_FORMAT(*format))
2376 return "Unmatched left paren in format "
2377 "string";
2378 msg = skipitem(&format, p_va, flags);
2379 if (msg)
2380 return msg;
2381 }
2382 format++;
2383 break;
2384 }
Christian Heimes380f7f22008-02-28 11:19:05 +00002385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 case ')':
2387 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00002388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002389 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00002390err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00002392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 *p_format = format;
2396 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00002397}
Fred Drakee4616e62001-10-23 21:09:29 +00002398
2399
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002400static int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002401unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name,
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002402 Py_ssize_t min, Py_ssize_t max, va_list vargs)
Fred Drakee4616e62001-10-23 21:09:29 +00002403{
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002404 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 PyObject **o;
Fred Drakee4616e62001-10-23 21:09:29 +00002406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 assert(min >= 0);
2408 assert(min <= max);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002409
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002410 if (nargs < min) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 if (name != NULL)
2412 PyErr_Format(
2413 PyExc_TypeError,
Xtreak1426daa2018-07-23 01:43:26 +05302414 "%.200s expected %s%zd argument%s, got %zd",
2415 name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 else
2417 PyErr_Format(
2418 PyExc_TypeError,
2419 "unpacked tuple should have %s%zd elements,"
2420 " but has %zd",
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002421 (min == max ? "" : "at least "), min, nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 return 0;
2423 }
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002424
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002425 if (nargs == 0) {
Raymond Hettinger94230232016-03-26 03:02:48 -07002426 return 1;
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002427 }
2428
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002429 if (nargs > max) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002430 if (name != NULL)
2431 PyErr_Format(
2432 PyExc_TypeError,
Xtreak1426daa2018-07-23 01:43:26 +05302433 "%.200s expected %s%zd argument%s, got %zd",
2434 name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 else
2436 PyErr_Format(
2437 PyExc_TypeError,
2438 "unpacked tuple should have %s%zd elements,"
2439 " but has %zd",
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002440 (min == max ? "" : "at most "), max, nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 return 0;
2442 }
Raymond Hettinger94230232016-03-26 03:02:48 -07002443
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002444 for (i = 0; i < nargs; i++) {
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002445 o = va_arg(vargs, PyObject **);
2446 *o = args[i];
2447 }
2448 return 1;
2449}
2450
2451int
2452PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
2453{
2454 PyObject **stack;
2455 Py_ssize_t nargs;
2456 int retval;
2457 va_list vargs;
2458
2459 if (!PyTuple_Check(args)) {
2460 PyErr_SetString(PyExc_SystemError,
2461 "PyArg_UnpackTuple() argument list is not a tuple");
2462 return 0;
2463 }
2464 stack = &PyTuple_GET_ITEM(args, 0);
2465 nargs = PyTuple_GET_SIZE(args);
2466
Raymond Hettinger94230232016-03-26 03:02:48 -07002467#ifdef HAVE_STDARG_PROTOTYPES
2468 va_start(vargs, max);
2469#else
2470 va_start(vargs);
2471#endif
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002472 retval = unpack_stack(stack, nargs, name, min, max, vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 va_end(vargs);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002474 return retval;
2475}
2476
2477int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002478_PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002479 Py_ssize_t min, Py_ssize_t max, ...)
2480{
2481 int retval;
2482 va_list vargs;
2483
2484#ifdef HAVE_STDARG_PROTOTYPES
2485 va_start(vargs, max);
2486#else
2487 va_start(vargs);
2488#endif
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002489 retval = unpack_stack(args, nargs, name, min, max, vargs);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002490 va_end(vargs);
2491 return retval;
Fred Drakee4616e62001-10-23 21:09:29 +00002492}
Georg Brandl02c42872005-08-26 06:42:30 +00002493
2494
Serhiy Storchaka68a001d2017-02-06 10:41:46 +02002495#undef _PyArg_NoKeywords
Serhiy Storchaka68a001d2017-02-06 10:41:46 +02002496#undef _PyArg_NoPositional
2497
Georg Brandl02c42872005-08-26 06:42:30 +00002498/* For type constructors that don't take keyword args
2499 *
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002500 * Sets a TypeError and returns 0 if the args/kwargs is
Thomas Wouters89f507f2006-12-13 04:49:30 +00002501 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00002502 */
2503int
Victor Stinner29d39cc2017-01-17 01:40:01 +01002504_PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
Georg Brandl02c42872005-08-26 06:42:30 +00002505{
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002506 if (kwargs == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 return 1;
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002508 }
Victor Stinner29d39cc2017-01-17 01:40:01 +01002509 if (!PyDict_CheckExact(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 PyErr_BadInternalCall();
2511 return 0;
2512 }
Victor Stinner29d39cc2017-01-17 01:40:01 +01002513 if (PyDict_GET_SIZE(kwargs) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 return 1;
Victor Stinner29d39cc2017-01-17 01:40:01 +01002515 }
2516
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002517 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
Victor Stinner29d39cc2017-01-17 01:40:01 +01002518 funcname);
2519 return 0;
2520}
2521
2522
2523int
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002524_PyArg_NoPositional(const char *funcname, PyObject *args)
2525{
2526 if (args == NULL)
2527 return 1;
2528 if (!PyTuple_CheckExact(args)) {
2529 PyErr_BadInternalCall();
2530 return 0;
2531 }
2532 if (PyTuple_GET_SIZE(args) == 0)
2533 return 1;
2534
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002535 PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002536 funcname);
2537 return 0;
2538}
2539
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002540void
2541_PyArg_Fini(void)
2542{
2543 struct _PyArg_Parser *tmp, *s = static_arg_parsers;
2544 while (s) {
2545 tmp = s->next;
2546 s->next = NULL;
2547 parser_clear(s);
2548 s = tmp;
2549 }
2550 static_arg_parsers = NULL;
2551}
2552
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002553#ifdef __cplusplus
2554};
2555#endif