blob: c491169abe5793d2eaec85f3094be0181e6daf6a [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"
Victor Stinnerec13b932018-11-25 23:56:17 +01005#include "pycore_tupleobject.h"
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00006
Guido van Rossumc1d50531996-08-21 23:38:24 +00007#include <ctype.h>
Benjamin Petersona853a8b2017-09-07 11:13:59 -07008#include <float.h>
Guido van Rossumc1d50531996-08-21 23:38:24 +00009
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000010
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011#ifdef __cplusplus
Guido van Rossum98297ee2007-11-06 21:34:58 +000012extern "C" {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013#endif
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000014int PyArg_Parse(PyObject *, const char *, ...);
15int PyArg_ParseTuple(PyObject *, const char *, ...);
16int PyArg_VaParse(PyObject *, const char *, va_list);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000017
Tim Petersdbd9ba62000-07-09 03:09:57 +000018int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 const char *, char **, ...);
Brett Cannon711e7d92004-07-10 22:20:32 +000020int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000021 const char *, char **, va_list);
Brett Cannon711e7d92004-07-10 22:20:32 +000022
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030023int _PyArg_ParseTupleAndKeywordsFast(PyObject *, PyObject *,
24 struct _PyArg_Parser *, ...);
25int _PyArg_VaParseTupleAndKeywordsFast(PyObject *, PyObject *,
26 struct _PyArg_Parser *, va_list);
27
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000028#ifdef HAVE_DECLSPEC_DLL
29/* Export functions */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020030PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...);
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020031PyAPI_FUNC(int) _PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs,
Victor Stinner6518a932017-01-17 01:29:49 +010032 const char *format, ...);
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020033PyAPI_FUNC(int) _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs,
34 PyObject *kwnames,
Benjamin Peterson819a46f2016-09-09 20:45:06 -070035 struct _PyArg_Parser *parser, ...);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020036PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
38 const char *, char **, ...);
39PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020040PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *,
42 const char *, char **, va_list);
Benjamin Peterson4eef5052016-09-10 17:04:36 -070043
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030044PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
45 struct _PyArg_Parser *, ...);
46PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *,
47 struct _PyArg_Parser *, va_list);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000048#endif
49
Martin v. Löwis18e16552006-02-15 17:27:45 +000050#define FLAG_COMPAT 1
51#define FLAG_SIZE_T 2
52
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040053typedef int (*destr_t)(PyObject *, void *);
54
55
56/* Keep track of "objects" that have been allocated or initialized and
57 which will need to be deallocated or cleaned up somehow if overall
58 parsing fails.
59*/
60typedef struct {
61 void *item;
62 destr_t destructor;
63} freelistentry_t;
64
65typedef struct {
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040066 freelistentry_t *entries;
Antoine Pitrou7056cb22013-02-17 01:04:57 +010067 int first_available;
68 int entries_malloced;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -040069} freelist_t;
70
Antoine Pitrou7056cb22013-02-17 01:04:57 +010071#define STATIC_FREELIST_ENTRIES 8
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000072
73/* Forward */
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020074static int vgetargs1_impl(PyObject *args, PyObject *const *stack, Py_ssize_t nargs,
Victor Stinner6518a932017-01-17 01:29:49 +010075 const char *format, va_list *p_va, int flags);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000076static int vgetargs1(PyObject *, const char *, va_list *, int);
Victor Stinner84bb1cf2013-05-17 00:12:04 +020077static void seterror(Py_ssize_t, const char *, int *, const char *, const char *);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020078static const char *convertitem(PyObject *, const char **, va_list *, int, int *,
79 char *, size_t, freelist_t *);
80static const char *converttuple(PyObject *, const char **, va_list *, int,
81 int *, char *, size_t, int, freelist_t *);
82static const char *convertsimple(PyObject *, const char **, va_list *, int,
83 char *, size_t, freelist_t *);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +020084static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020085static int getbuffer(PyObject *, Py_buffer *, const char**);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000086
Tim Petersdbd9ba62000-07-09 03:09:57 +000087static int vgetargskeywords(PyObject *, PyObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 const char *, char **, va_list *, int);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030089static int vgetargskeywordsfast(PyObject *, PyObject *,
90 struct _PyArg_Parser *, va_list *, int);
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020091static int vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -070092 PyObject *keywords, PyObject *kwnames,
93 struct _PyArg_Parser *parser,
94 va_list *p_va, int flags);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020095static const char *skipitem(const char **, va_list *, int);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000096
Fred Drake563dfc22001-10-23 14:41:08 +000097int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000098PyArg_Parse(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +000099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 int retval;
101 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 va_start(va, format);
104 retval = vgetargs1(args, format, &va, FLAG_COMPAT);
105 va_end(va);
106 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000107}
108
109int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200110_PyArg_Parse_SizeT(PyObject *args, const char *format, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 int retval;
113 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 va_start(va, format);
116 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T);
117 va_end(va);
118 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000119}
120
121
Fred Drake563dfc22001-10-23 14:41:08 +0000122int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000123PyArg_ParseTuple(PyObject *args, const char *format, ...)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 int retval;
126 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 va_start(va, format);
129 retval = vgetargs1(args, format, &va, 0);
130 va_end(va);
131 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000132}
133
Martin v. Löwis18e16552006-02-15 17:27:45 +0000134int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200135_PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 int retval;
138 va_list va;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 va_start(va, format);
141 retval = vgetargs1(args, format, &va, FLAG_SIZE_T);
142 va_end(va);
143 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000144}
145
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000146
147int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200148_PyArg_ParseStack(PyObject *const *args, Py_ssize_t nargs, const char *format, ...)
Victor Stinner6518a932017-01-17 01:29:49 +0100149{
150 int retval;
151 va_list va;
152
153 va_start(va, format);
154 retval = vgetargs1_impl(NULL, args, nargs, format, &va, 0);
155 va_end(va);
156 return retval;
157}
158
159int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200160_PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs, const char *format, ...)
Victor Stinner6518a932017-01-17 01:29:49 +0100161{
162 int retval;
163 va_list va;
164
165 va_start(va, format);
166 retval = vgetargs1_impl(NULL, args, nargs, format, &va, FLAG_SIZE_T);
167 va_end(va);
168 return retval;
169}
170
171
172int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000173PyArg_VaParse(PyObject *args, const char *format, va_list va)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 va_list lva;
Christian Heimes2f2fee12016-09-21 11:37:27 +0200176 int retval;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000177
Benjamin Peterson0c212142016-09-20 20:39:33 -0700178 va_copy(lva, va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000179
Christian Heimes2f2fee12016-09-21 11:37:27 +0200180 retval = vgetargs1(args, format, &lva, 0);
181 va_end(lva);
182 return retval;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000183}
184
Martin v. Löwis18e16552006-02-15 17:27:45 +0000185int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200186_PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +0000187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 va_list lva;
Christian Heimes2f2fee12016-09-21 11:37:27 +0200189 int retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000190
Benjamin Peterson0c212142016-09-20 20:39:33 -0700191 va_copy(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000192
Christian Heimes2f2fee12016-09-21 11:37:27 +0200193 retval = vgetargs1(args, format, &lva, FLAG_SIZE_T);
194 va_end(lva);
195 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000196}
197
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000198
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000199/* Handle cleanup of allocated memory in case of exception */
200
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400201static int
202cleanup_ptr(PyObject *self, void *ptr)
Antoine Pitrouf71995782008-08-29 18:37:05 +0000203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (ptr) {
205 PyMem_FREE(ptr);
206 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 return 0;
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000208}
209
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000210static int
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400211cleanup_buffer(PyObject *self, void *ptr)
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000212{
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400213 Py_buffer *buf = (Py_buffer *)ptr;
214 if (buf) {
215 PyBuffer_Release(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400217 return 0;
218}
219
220static int
221addcleanup(void *ptr, freelist_t *freelist, destr_t destructor)
222{
223 int index;
224
225 index = freelist->first_available;
226 freelist->first_available += 1;
227
228 freelist->entries[index].item = ptr;
229 freelist->entries[index].destructor = destructor;
230
231 return 0;
232}
233
234static int
235cleanreturn(int retval, freelist_t *freelist)
236{
237 int index;
238
239 if (retval == 0) {
240 /* A failure occurred, therefore execute all of the cleanup
241 functions.
242 */
243 for (index = 0; index < freelist->first_available; ++index) {
244 freelist->entries[index].destructor(NULL,
245 freelist->entries[index].item);
246 }
247 }
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100248 if (freelist->entries_malloced)
249 PyMem_FREE(freelist->entries);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 return retval;
Martin v. Löwise6bbb4d2003-05-03 10:00:22 +0000251}
252
253
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000254static int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200255vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs, const char *format,
Victor Stinner6518a932017-01-17 01:29:49 +0100256 va_list *p_va, int flags)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000257{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 char msgbuf[256];
259 int levels[32];
260 const char *fname = NULL;
261 const char *message = NULL;
262 int min = -1;
263 int max = 0;
264 int level = 0;
265 int endfmt = 0;
266 const char *formatsave = format;
Victor Stinner6518a932017-01-17 01:29:49 +0100267 Py_ssize_t i;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200268 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 int compat = flags & FLAG_COMPAT;
Benjamin Peterson40be9e52014-02-11 10:09:27 -0500270 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
271 freelist_t freelist;
272
Victor Stinner6518a932017-01-17 01:29:49 +0100273 assert(nargs == 0 || stack != NULL);
274
Benjamin Peterson40be9e52014-02-11 10:09:27 -0500275 freelist.entries = static_entries;
276 freelist.first_available = 0;
277 freelist.entries_malloced = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 flags = flags & ~FLAG_COMPAT;
Tim Peters5c4d5bf2001-02-12 22:13:26 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 while (endfmt == 0) {
282 int c = *format++;
283 switch (c) {
284 case '(':
285 if (level == 0)
286 max++;
287 level++;
288 if (level >= 30)
289 Py_FatalError("too many tuple nesting levels "
290 "in argument format string");
291 break;
292 case ')':
293 if (level == 0)
294 Py_FatalError("excess ')' in getargs format");
295 else
296 level--;
297 break;
298 case '\0':
299 endfmt = 1;
300 break;
301 case ':':
302 fname = format;
303 endfmt = 1;
304 break;
305 case ';':
306 message = format;
307 endfmt = 1;
308 break;
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100309 case '|':
310 if (level == 0)
311 min = max;
312 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 default:
314 if (level == 0) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100315 if (Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 if (c != 'e') /* skip encoded */
317 max++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 }
319 break;
320 }
321 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 if (level != 0)
324 Py_FatalError(/* '(' */ "missing ')' in getargs format");
Guido van Rossum98297ee2007-11-06 21:34:58 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 if (min < 0)
327 min = max;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 format = formatsave;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000330
Antoine Pitrou7056cb22013-02-17 01:04:57 +0100331 if (max > STATIC_FREELIST_ENTRIES) {
332 freelist.entries = PyMem_NEW(freelistentry_t, max);
333 if (freelist.entries == NULL) {
334 PyErr_NoMemory();
335 return 0;
336 }
337 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -0500338 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 if (compat) {
341 if (max == 0) {
Victor Stinner6518a932017-01-17 01:29:49 +0100342 if (compat_args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 return 1;
Victor Stinner6ced7c42011-03-21 18:15:42 +0100344 PyErr_Format(PyExc_TypeError,
345 "%.200s%s takes no arguments",
346 fname==NULL ? "function" : fname,
347 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400348 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 }
350 else if (min == 1 && max == 1) {
Victor Stinner6518a932017-01-17 01:29:49 +0100351 if (compat_args == NULL) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100352 PyErr_Format(PyExc_TypeError,
353 "%.200s%s takes at least one argument",
354 fname==NULL ? "function" : fname,
355 fname==NULL ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400356 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 }
Victor Stinner6518a932017-01-17 01:29:49 +0100358 msg = convertitem(compat_args, &format, p_va, flags, levels,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 msgbuf, sizeof(msgbuf), &freelist);
360 if (msg == NULL)
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400361 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 seterror(levels[0], msg, levels+1, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400363 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 }
365 else {
366 PyErr_SetString(PyExc_SystemError,
367 "old style getargs format uses new features");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400368 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 }
370 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000371
Victor Stinner6518a932017-01-17 01:29:49 +0100372 if (nargs < min || max < nargs) {
Victor Stinner6ced7c42011-03-21 18:15:42 +0100373 if (message == NULL)
374 PyErr_Format(PyExc_TypeError,
375 "%.150s%s takes %s %d argument%s (%ld given)",
376 fname==NULL ? "function" : fname,
377 fname==NULL ? "" : "()",
378 min==max ? "exactly"
Victor Stinner6518a932017-01-17 01:29:49 +0100379 : nargs < min ? "at least" : "at most",
380 nargs < min ? min : max,
381 (nargs < min ? min : max) == 1 ? "" : "s",
382 Py_SAFE_DOWNCAST(nargs, Py_ssize_t, long));
Victor Stinner6ced7c42011-03-21 18:15:42 +0100383 else
384 PyErr_SetString(PyExc_TypeError, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400385 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000387
Victor Stinner6518a932017-01-17 01:29:49 +0100388 for (i = 0; i < nargs; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (*format == '|')
390 format++;
Victor Stinner6518a932017-01-17 01:29:49 +0100391 msg = convertitem(stack[i], &format, p_va,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 flags, levels, msgbuf,
393 sizeof(msgbuf), &freelist);
394 if (msg) {
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200395 seterror(i+1, msg, levels, fname, message);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400396 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 }
398 }
Guido van Rossum231a41e1997-12-09 20:36:39 +0000399
Antoine Pitrou4de74572013-02-09 23:11:27 +0100400 if (*format != '\0' && !Py_ISALPHA(Py_CHARMASK(*format)) &&
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 *format != '(' &&
402 *format != '|' && *format != ':' && *format != ';') {
403 PyErr_Format(PyExc_SystemError,
404 "bad format string: %.200s", formatsave);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400405 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000407
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400408 return cleanreturn(1, &freelist);
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000409}
410
Victor Stinner6518a932017-01-17 01:29:49 +0100411static int
412vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags)
413{
414 PyObject **stack;
415 Py_ssize_t nargs;
416
417 if (!(flags & FLAG_COMPAT)) {
418 assert(args != NULL);
419
420 if (!PyTuple_Check(args)) {
421 PyErr_SetString(PyExc_SystemError,
422 "new style getargs format but argument is not a tuple");
423 return 0;
424 }
425
Victor Stinnerd17a6932018-11-09 16:56:48 +0100426 stack = _PyTuple_ITEMS(args);
Victor Stinner6518a932017-01-17 01:29:49 +0100427 nargs = PyTuple_GET_SIZE(args);
428 }
429 else {
430 stack = NULL;
431 nargs = 0;
432 }
433
434 return vgetargs1_impl(args, stack, nargs, format, p_va, flags);
435}
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000436
437
438static void
Victor Stinner84bb1cf2013-05-17 00:12:04 +0200439seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000440 const char *message)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 char buf[512];
443 int i;
444 char *p = buf;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (PyErr_Occurred())
447 return;
448 else if (message == NULL) {
449 if (fname != NULL) {
450 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname);
451 p += strlen(p);
452 }
453 if (iarg != 0) {
454 PyOS_snprintf(p, sizeof(buf) - (p - buf),
Richard Oudkerk25296ce2013-05-18 17:35:19 +0100455 "argument %" PY_FORMAT_SIZE_T "d", iarg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 i = 0;
457 p += strlen(p);
Georg Brandl142ad662013-10-14 07:01:11 +0200458 while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 PyOS_snprintf(p, sizeof(buf) - (p - buf),
460 ", item %d", levels[i]-1);
461 p += strlen(p);
462 i++;
463 }
464 }
465 else {
466 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument");
467 p += strlen(p);
468 }
469 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
470 message = buf;
471 }
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +0200472 if (msg[0] == '(') {
473 PyErr_SetString(PyExc_SystemError, message);
474 }
475 else {
476 PyErr_SetString(PyExc_TypeError, message);
477 }
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000478}
479
480
481/* Convert a tuple argument.
482 On entry, *p_format points to the character _after_ the opening '('.
483 On successful exit, *p_format points to the closing ')'.
484 If successful:
485 *p_format and *p_va are updated,
486 *levels and *msgbuf are untouched,
487 and NULL is returned.
488 If the argument is invalid:
489 *p_format is unchanged,
490 *p_va is undefined,
491 *levels is a 0-terminated list of item numbers,
492 *msgbuf contains an error message, whose format is:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 "must be <typename1>, not <typename2>", where:
494 <typename1> is the name of the expected type, and
495 <typename2> is the name of the actual type,
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000496 and msgbuf is returned.
497*/
498
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200499static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000500converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Guido van Rossum98297ee2007-11-06 21:34:58 +0000501 int *levels, char *msgbuf, size_t bufsize, int toplevel,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400502 freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 int level = 0;
505 int n = 0;
506 const char *format = *p_format;
507 int i;
Victor Stinner74387f52013-11-18 01:21:12 +0100508 Py_ssize_t len;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 for (;;) {
511 int c = *format++;
512 if (c == '(') {
513 if (level == 0)
514 n++;
515 level++;
516 }
517 else if (c == ')') {
518 if (level == 0)
519 break;
520 level--;
521 }
522 else if (c == ':' || c == ';' || c == '\0')
523 break;
Antoine Pitrou4de74572013-02-09 23:11:27 +0100524 else if (level == 0 && Py_ISALPHA(Py_CHARMASK(c)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 n++;
526 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (!PySequence_Check(arg) || PyBytes_Check(arg)) {
529 levels[0] = 0;
530 PyOS_snprintf(msgbuf, bufsize,
531 toplevel ? "expected %d arguments, not %.50s" :
532 "must be %d-item sequence, not %.50s",
533 n,
534 arg == Py_None ? "None" : arg->ob_type->tp_name);
535 return msgbuf;
536 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000537
Victor Stinner74387f52013-11-18 01:21:12 +0100538 len = PySequence_Size(arg);
539 if (len != n) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 levels[0] = 0;
Victor Stinner74387f52013-11-18 01:21:12 +0100541 if (toplevel) {
542 PyOS_snprintf(msgbuf, bufsize,
Xtreak63262782018-12-21 20:15:13 +0530543 "expected %d argument%s, not %" PY_FORMAT_SIZE_T "d",
544 n,
545 n == 1 ? "" : "s",
546 len);
Victor Stinner74387f52013-11-18 01:21:12 +0100547 }
548 else {
549 PyOS_snprintf(msgbuf, bufsize,
550 "must be sequence of length %d, "
551 "not %" PY_FORMAT_SIZE_T "d",
552 n, len);
553 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 return msgbuf;
555 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 format = *p_format;
558 for (i = 0; i < n; i++) {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200559 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 PyObject *item;
561 item = PySequence_GetItem(arg, i);
562 if (item == NULL) {
563 PyErr_Clear();
564 levels[0] = i+1;
565 levels[1] = 0;
566 strncpy(msgbuf, "is not retrievable", bufsize);
567 return msgbuf;
568 }
569 msg = convertitem(item, &format, p_va, flags, levels+1,
570 msgbuf, bufsize, freelist);
571 /* PySequence_GetItem calls tp->sq_item, which INCREFs */
572 Py_XDECREF(item);
573 if (msg != NULL) {
574 levels[0] = i+1;
575 return msg;
576 }
577 }
Ka-Ping Yee20579702001-01-15 22:14:16 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 *p_format = format;
580 return NULL;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000581}
582
583
584/* Convert a single item. */
585
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200586static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000587convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400588 int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000589{
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200590 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 const char *format = *p_format;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 if (*format == '(' /* ')' */) {
594 format++;
595 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf,
596 bufsize, 0, freelist);
597 if (msg == NULL)
598 format++;
599 }
600 else {
601 msg = convertsimple(arg, &format, p_va, flags,
602 msgbuf, bufsize, freelist);
603 if (msg != NULL)
604 levels[0] = 0;
605 }
606 if (msg == NULL)
607 *p_format = format;
608 return msg;
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000609}
610
611
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000612
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000613/* Format an error message generated by convertsimple(). */
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000614
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200615void
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200616_PyArg_BadArgument(const char *fname, int iarg,
617 const char *expected, PyObject *arg)
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200618{
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200619 if (iarg) {
620 PyErr_Format(PyExc_TypeError,
621 "%.200s() argument %d must be %.50s, not %.50s",
622 fname, iarg, expected,
623 arg == Py_None ? "None" : arg->ob_type->tp_name);
624 }
625 else {
626 PyErr_Format(PyExc_TypeError,
627 "%.200s() argument must be %.50s, not %.50s",
628 fname, expected,
629 arg == Py_None ? "None" : arg->ob_type->tp_name);
630 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200631}
632
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200633static const char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000634converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 assert(expected != NULL);
637 assert(arg != NULL);
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200638 if (expected[0] == '(') {
639 PyOS_snprintf(msgbuf, bufsize,
640 "%.100s", expected);
641 }
642 else {
643 PyOS_snprintf(msgbuf, bufsize,
644 "must be %.50s, not %.50s", expected,
645 arg == Py_None ? "None" : arg->ob_type->tp_name);
646 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000648}
649
650#define CONV_UNICODE "(unicode conversion error)"
651
Guido van Rossum45aecf42006-03-15 04:58:47 +0000652/* Explicitly check for float arguments when integers are expected.
653 Return 1 for error, 0 if ok. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000654static int
655float_argument_error(PyObject *arg)
656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 if (PyFloat_Check(arg)) {
658 PyErr_SetString(PyExc_TypeError,
659 "integer argument expected, got float" );
660 return 1;
661 }
662 else
663 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000664}
665
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000666/* Convert a non-tuple argument. Return NULL if conversion went OK,
667 or a string with a message describing the failure. The message is
668 formatted as "must be <desired type>, not <actual type>".
669 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000670 Don't call if a tuple is expected.
671
672 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000673*/
674
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200675static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000676convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400677 char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 /* For # codes */
680#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
681 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
682 else q=va_arg(*p_va, int*);
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000683#define STORE_SIZE(s) \
684 if (flags & FLAG_SIZE_T) \
685 *q2=s; \
686 else { \
687 if (INT_MAX < s) { \
688 PyErr_SetString(PyExc_OverflowError, \
689 "size does not fit in an int"); \
690 return converterr("", arg, msgbuf, bufsize); \
691 } \
Victor Stinner9550ef32013-06-05 01:18:13 +0200692 *q = (int)s; \
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000693 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000694#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
Victor Stinner6ab8e822011-01-04 11:16:49 +0000695#define RETURN_ERR_OCCURRED return msgbuf
Martin v. Löwis18e16552006-02-15 17:27:45 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 const char *format = *p_format;
698 char c = *format++;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200699 const char *sarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 case 'b': { /* unsigned byte -- very short int */
704 char *p = va_arg(*p_va, char *);
705 long ival;
706 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000707 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 ival = PyLong_AsLong(arg);
709 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000710 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 else if (ival < 0) {
712 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000713 "unsigned byte integer is less than minimum");
714 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 }
716 else if (ival > UCHAR_MAX) {
717 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000718 "unsigned byte integer is greater than maximum");
719 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 }
721 else
722 *p = (unsigned char) ival;
723 break;
724 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 case 'B': {/* byte sized bitfield - both signed and unsigned
727 values allowed */
728 char *p = va_arg(*p_va, char *);
729 long ival;
730 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000731 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 ival = PyLong_AsUnsignedLongMask(arg);
733 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000734 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 else
736 *p = (unsigned char) ival;
737 break;
738 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 case 'h': {/* signed short int */
741 short *p = va_arg(*p_va, short *);
742 long ival;
743 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000744 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 ival = PyLong_AsLong(arg);
746 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000747 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 else if (ival < SHRT_MIN) {
749 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000750 "signed short integer is less than minimum");
751 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 }
753 else if (ival > SHRT_MAX) {
754 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000755 "signed short integer is greater than maximum");
756 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 }
758 else
759 *p = (short) ival;
760 break;
761 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 case 'H': { /* short int sized bitfield, both signed and
764 unsigned allowed */
765 unsigned short *p = va_arg(*p_va, unsigned short *);
766 long ival;
767 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000768 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 ival = PyLong_AsUnsignedLongMask(arg);
770 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000771 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 else
773 *p = (unsigned short) ival;
774 break;
775 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 case 'i': {/* signed int */
778 int *p = va_arg(*p_va, int *);
779 long ival;
780 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000781 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 ival = PyLong_AsLong(arg);
783 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000784 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 else if (ival > INT_MAX) {
786 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000787 "signed integer is greater than maximum");
788 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 }
790 else if (ival < INT_MIN) {
791 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000792 "signed integer is less than minimum");
793 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 }
795 else
796 *p = ival;
797 break;
798 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 case 'I': { /* int sized bitfield, both signed and
801 unsigned allowed */
802 unsigned int *p = va_arg(*p_va, unsigned int *);
803 unsigned int ival;
804 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000805 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
807 if (ival == (unsigned int)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000808 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 else
810 *p = ival;
811 break;
812 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 case 'n': /* Py_ssize_t */
815 {
816 PyObject *iobj;
817 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
818 Py_ssize_t ival = -1;
819 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000820 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 iobj = PyNumber_Index(arg);
822 if (iobj != NULL) {
823 ival = PyLong_AsSsize_t(iobj);
824 Py_DECREF(iobj);
825 }
826 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000827 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 *p = ival;
829 break;
830 }
831 case 'l': {/* long int */
832 long *p = va_arg(*p_va, long *);
833 long ival;
834 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000835 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 ival = PyLong_AsLong(arg);
837 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000838 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 else
840 *p = ival;
841 break;
842 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 case 'k': { /* long sized bitfield */
845 unsigned long *p = va_arg(*p_va, unsigned long *);
846 unsigned long ival;
847 if (PyLong_Check(arg))
848 ival = PyLong_AsUnsignedLongMask(arg);
849 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200850 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 *p = ival;
852 break;
853 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000854
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700855 case 'L': {/* long long */
856 long long *p = va_arg( *p_va, long long * );
857 long long ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000858 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000859 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 ival = PyLong_AsLongLong(arg);
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700861 if (ival == (long long)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000862 RETURN_ERR_OCCURRED;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000863 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 break;
866 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 case 'K': { /* long long sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700869 unsigned long long *p = va_arg(*p_va, unsigned long long *);
870 unsigned long long ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 if (PyLong_Check(arg))
872 ival = PyLong_AsUnsignedLongLongMask(arg);
873 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200874 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 *p = ival;
876 break;
877 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 case 'f': {/* float */
880 float *p = va_arg(*p_va, float *);
881 double dval = PyFloat_AsDouble(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 = (float) dval;
886 break;
887 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 case 'd': {/* double */
890 double *p = va_arg(*p_va, double *);
891 double dval = PyFloat_AsDouble(arg);
892 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000893 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 else
895 *p = dval;
896 break;
897 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 case 'D': {/* complex double */
900 Py_complex *p = va_arg(*p_va, Py_complex *);
901 Py_complex cval;
902 cval = PyComplex_AsCComplex(arg);
903 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000904 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 else
906 *p = cval;
907 break;
908 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 case 'c': {/* char */
911 char *p = va_arg(*p_va, char *);
912 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
913 *p = PyBytes_AS_STRING(arg)[0];
Eli Bendersky906b88f2011-07-29 07:05:08 +0300914 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
915 *p = PyByteArray_AS_STRING(arg)[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 else
917 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
918 break;
919 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 case 'C': {/* unicode char */
922 int *p = va_arg(*p_va, int *);
Victor Stinnere1335c72011-10-04 20:53:03 +0200923 int kind;
924 void *data;
925
926 if (!PyUnicode_Check(arg))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 return converterr("a unicode character", arg, msgbuf, bufsize);
Victor Stinnere1335c72011-10-04 20:53:03 +0200928
929 if (PyUnicode_READY(arg))
930 RETURN_ERR_OCCURRED;
931
932 if (PyUnicode_GET_LENGTH(arg) != 1)
933 return converterr("a unicode character", arg, msgbuf, bufsize);
934
935 kind = PyUnicode_KIND(arg);
936 data = PyUnicode_DATA(arg);
937 *p = PyUnicode_READ(kind, data, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 break;
939 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000940
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700941 case 'p': {/* boolean *p*redicate */
942 int *p = va_arg(*p_va, int *);
943 int val = PyObject_IsTrue(arg);
944 if (val > 0)
945 *p = 1;
946 else if (val == 0)
947 *p = 0;
948 else
949 RETURN_ERR_OCCURRED;
950 break;
951 }
952
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000953 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000955
Serhiy Storchakab757c832014-12-05 22:25:22 +0200956 case 'y': {/* any bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 void **p = (void **)va_arg(*p_va, char **);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200958 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 Py_ssize_t count;
960 if (*format == '*') {
961 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
962 return converterr(buf, arg, msgbuf, bufsize);
963 format++;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400964 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 return converterr(
966 "(cleanup problem)",
967 arg, msgbuf, bufsize);
968 }
969 break;
970 }
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200971 count = convertbuffer(arg, (const void **)p, &buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 if (count < 0)
973 return converterr(buf, arg, msgbuf, bufsize);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000974 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 FETCH_SIZE;
976 STORE_SIZE(count);
977 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000978 } else {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300979 if (strlen(*p) != (size_t)count) {
980 PyErr_SetString(PyExc_ValueError, "embedded null byte");
981 RETURN_ERR_OCCURRED;
982 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 }
984 break;
985 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000986
Serhiy Storchakab757c832014-12-05 22:25:22 +0200987 case 's': /* text string or bytes-like object */
988 case 'z': /* text string, bytes-like object or None */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000989 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000991 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000993
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000994 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
996 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200997 Py_ssize_t len;
998 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
999 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 return converterr(CONV_UNICODE,
1001 arg, msgbuf, bufsize);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001002 PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 }
Serhiy Storchakab757c832014-12-05 22:25:22 +02001004 else { /* any bytes-like object */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001005 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 if (getbuffer(arg, p, &buf) < 0)
1007 return converterr(buf, arg, msgbuf, bufsize);
1008 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001009 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 return converterr(
1011 "(cleanup problem)",
1012 arg, msgbuf, bufsize);
1013 }
1014 format++;
Serhiy Storchakab757c832014-12-05 22:25:22 +02001015 } else if (*format == '#') { /* a string or read-only bytes-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001016 /* "s#" or "z#" */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001017 const void **p = (const void **)va_arg(*p_va, const char **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001019
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001020 if (c == 'z' && arg == Py_None) {
1021 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 STORE_SIZE(0);
1023 }
1024 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001025 Py_ssize_t len;
1026 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1027 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 return converterr(CONV_UNICODE,
1029 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001030 *p = sarg;
1031 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 }
Serhiy Storchakab757c832014-12-05 22:25:22 +02001033 else { /* read-only bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 /* XXX Really? */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001035 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 Py_ssize_t count = convertbuffer(arg, p, &buf);
1037 if (count < 0)
1038 return converterr(buf, arg, msgbuf, bufsize);
1039 STORE_SIZE(count);
1040 }
1041 format++;
1042 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001043 /* "s" or "z" */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001044 const char **p = va_arg(*p_va, const char **);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001045 Py_ssize_t len;
1046 sarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001047
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001048 if (c == 'z' && arg == Py_None)
1049 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001051 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1052 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 return converterr(CONV_UNICODE,
1054 arg, msgbuf, bufsize);
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001055 if (strlen(sarg) != (size_t)len) {
1056 PyErr_SetString(PyExc_ValueError, "embedded null character");
1057 RETURN_ERR_OCCURRED;
1058 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001059 *p = sarg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 }
1061 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001062 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 }
1065 break;
1066 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001067
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001068 case 'u': /* raw unicode buffer (Py_UNICODE *) */
1069 case 'Z': /* raw unicode buffer or None */
1070 {
Brett Cannonb94767f2011-02-22 20:15:44 +00001071 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1072
Serhiy Storchakab757c832014-12-05 22:25:22 +02001073 if (*format == '#') {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001074 /* "u#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001076
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001077 if (c == 'Z' && arg == Py_None) {
1078 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 STORE_SIZE(0);
1080 }
1081 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001082 Py_ssize_t len;
1083 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001084 if (*p == NULL)
1085 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001086 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 }
Victor Stinner5216e6d2010-06-08 21:45:51 +00001088 else
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001089 return converterr(c == 'Z' ? "str or None" : "str",
1090 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 format++;
1092 } else {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001093 /* "u" or "Z" */
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001094 if (c == 'Z' && arg == Py_None)
1095 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +00001096 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001097 Py_ssize_t len;
1098 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001099 if (*p == NULL)
1100 RETURN_ERR_OCCURRED;
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001101 if (wcslen(*p) != (size_t)len) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001102 PyErr_SetString(PyExc_ValueError, "embedded null character");
1103 RETURN_ERR_OCCURRED;
1104 }
Victor Stinner06e49dd2010-06-13 18:21:50 +00001105 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001106 return converterr(c == 'Z' ? "str or None" : "str",
1107 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 }
1109 break;
1110 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 case 'e': {/* encoded string */
1113 char **buffer;
1114 const char *encoding;
1115 PyObject *s;
1116 int recode_strings;
1117 Py_ssize_t size;
1118 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 /* Get 'e' parameter: the encoding name */
1121 encoding = (const char *)va_arg(*p_va, const char *);
1122 if (encoding == NULL)
1123 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* Get output buffer parameter:
1126 's' (recode all objects via Unicode) or
1127 't' (only recode non-string objects)
1128 */
1129 if (*format == 's')
1130 recode_strings = 1;
1131 else if (*format == 't')
1132 recode_strings = 0;
1133 else
1134 return converterr(
1135 "(unknown parser marker combination)",
1136 arg, msgbuf, bufsize);
1137 buffer = (char **)va_arg(*p_va, char **);
1138 format++;
1139 if (buffer == NULL)
1140 return converterr("(buffer is NULL)",
1141 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 /* Encode object */
1144 if (!recode_strings &&
1145 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1146 s = arg;
1147 Py_INCREF(s);
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001148 if (PyBytes_Check(arg)) {
1149 size = PyBytes_GET_SIZE(s);
1150 ptr = PyBytes_AS_STRING(s);
1151 }
1152 else {
1153 size = PyByteArray_GET_SIZE(s);
1154 ptr = PyByteArray_AS_STRING(s);
1155 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001157 else if (PyUnicode_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 /* Encode object; use default error handling */
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001159 s = PyUnicode_AsEncodedString(arg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 encoding,
1161 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (s == NULL)
1163 return converterr("(encoding failed)",
1164 arg, msgbuf, bufsize);
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001165 assert(PyBytes_Check(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 size = PyBytes_GET_SIZE(s);
1167 ptr = PyBytes_AS_STRING(s);
1168 if (ptr == NULL)
1169 ptr = "";
1170 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001171 else {
1172 return converterr(
1173 recode_strings ? "str" : "str, bytes or bytearray",
1174 arg, msgbuf, bufsize);
1175 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 /* Write output; output is guaranteed to be 0-terminated */
1178 if (*format == '#') {
1179 /* Using buffer length parameter '#':
1180
1181 - if *buffer is NULL, a new buffer of the
1182 needed size is allocated and the data
1183 copied into it; *buffer is updated to point
1184 to the new buffer; the caller is
1185 responsible for PyMem_Free()ing it after
1186 usage
1187
1188 - if *buffer is not NULL, the data is
1189 copied to *buffer; *buffer_len has to be
1190 set to the size of the buffer on input;
1191 buffer overflow is signalled with an error;
1192 buffer has to provide enough room for the
1193 encoded string plus the trailing 0-byte
1194
1195 - in both cases, *buffer_len is updated to
1196 the size of the buffer /excluding/ the
1197 trailing 0-byte
1198
1199 */
1200 FETCH_SIZE;
1201
1202 format++;
1203 if (q == NULL && q2 == NULL) {
1204 Py_DECREF(s);
1205 return converterr(
1206 "(buffer_len is NULL)",
1207 arg, msgbuf, bufsize);
1208 }
1209 if (*buffer == NULL) {
1210 *buffer = PyMem_NEW(char, size + 1);
1211 if (*buffer == NULL) {
1212 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001213 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001214 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001216 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 Py_DECREF(s);
1218 return converterr(
1219 "(cleanup problem)",
1220 arg, msgbuf, bufsize);
1221 }
1222 } else {
1223 if (size + 1 > BUFFER_LEN) {
1224 Py_DECREF(s);
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +02001225 PyErr_Format(PyExc_ValueError,
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001226 "encoded string too long "
1227 "(%zd, maximum length %zd)",
1228 (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1229 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 }
1231 }
1232 memcpy(*buffer, ptr, size+1);
1233 STORE_SIZE(size);
1234 } else {
1235 /* Using a 0-terminated buffer:
1236
1237 - the encoded string has to be 0-terminated
1238 for this variant to work; if it is not, an
1239 error raised
1240
1241 - a new buffer of the needed size is
1242 allocated and the data copied into it;
1243 *buffer is updated to point to the new
1244 buffer; the caller is responsible for
1245 PyMem_Free()ing it after usage
1246
1247 */
1248 if ((Py_ssize_t)strlen(ptr) != size) {
1249 Py_DECREF(s);
1250 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001251 "encoded string without null bytes",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 arg, msgbuf, bufsize);
1253 }
1254 *buffer = PyMem_NEW(char, size + 1);
1255 if (*buffer == NULL) {
1256 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001257 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001258 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001260 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 Py_DECREF(s);
1262 return converterr("(cleanup problem)",
1263 arg, msgbuf, bufsize);
1264 }
1265 memcpy(*buffer, ptr, size+1);
1266 }
1267 Py_DECREF(s);
1268 break;
1269 }
1270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 case 'S': { /* PyBytes object */
1272 PyObject **p = va_arg(*p_va, PyObject **);
1273 if (PyBytes_Check(arg))
1274 *p = arg;
1275 else
1276 return converterr("bytes", arg, msgbuf, bufsize);
1277 break;
1278 }
1279
1280 case 'Y': { /* PyByteArray object */
1281 PyObject **p = va_arg(*p_va, PyObject **);
1282 if (PyByteArray_Check(arg))
1283 *p = arg;
1284 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001285 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 break;
1287 }
1288
1289 case 'U': { /* PyUnicode object */
1290 PyObject **p = va_arg(*p_va, PyObject **);
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001291 if (PyUnicode_Check(arg)) {
1292 if (PyUnicode_READY(arg) == -1)
1293 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 *p = arg;
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001295 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 else
1297 return converterr("str", arg, msgbuf, bufsize);
1298 break;
1299 }
1300
1301 case 'O': { /* object */
1302 PyTypeObject *type;
1303 PyObject **p;
1304 if (*format == '!') {
1305 type = va_arg(*p_va, PyTypeObject*);
1306 p = va_arg(*p_va, PyObject **);
1307 format++;
1308 if (PyType_IsSubtype(arg->ob_type, type))
1309 *p = arg;
1310 else
1311 return converterr(type->tp_name, arg, msgbuf, bufsize);
1312
1313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 else if (*format == '&') {
1315 typedef int (*converter)(PyObject *, void *);
1316 converter convert = va_arg(*p_va, converter);
1317 void *addr = va_arg(*p_va, void *);
1318 int res;
1319 format++;
1320 if (! (res = (*convert)(arg, addr)))
1321 return converterr("(unspecified)",
1322 arg, msgbuf, bufsize);
1323 if (res == Py_CLEANUP_SUPPORTED &&
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001324 addcleanup(addr, freelist, convert) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 return converterr("(cleanup problem)",
1326 arg, msgbuf, bufsize);
1327 }
1328 else {
1329 p = va_arg(*p_va, PyObject **);
1330 *p = arg;
1331 }
1332 break;
1333 }
1334
1335
Victor Stinner25e8ec42010-06-25 00:02:38 +00001336 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338
Victor Stinner25e8ec42010-06-25 00:02:38 +00001339 if (*format != '*')
1340 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001341 "(invalid use of 'w' format character)",
Victor Stinner25e8ec42010-06-25 00:02:38 +00001342 arg, msgbuf, bufsize);
1343 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344
Victor Stinner25e8ec42010-06-25 00:02:38 +00001345 /* Caller is interested in Py_buffer, and the object
1346 supports it directly. */
1347 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1348 PyErr_Clear();
R David Murray861470c2014-10-05 11:47:01 -04001349 return converterr("read-write bytes-like object",
1350 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 }
Victor Stinner8182b712010-07-28 00:40:58 +00001352 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1353 PyBuffer_Release((Py_buffer*)p);
1354 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1355 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001356 if (addcleanup(p, freelist, cleanup_buffer)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001357 return converterr(
1358 "(cleanup problem)",
1359 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 break;
1362 }
1363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 default:
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001365 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366
1367 }
1368
1369 *p_format = format;
1370 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001371
1372#undef FETCH_SIZE
1373#undef STORE_SIZE
1374#undef BUFFER_LEN
1375#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001376}
Guido van Rossumaa354651996-08-19 19:32:04 +00001377
Martin v. Löwis18e16552006-02-15 17:27:45 +00001378static Py_ssize_t
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001379convertbuffer(PyObject *arg, const void **p, const char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001380{
Victor Stinner5cb62392010-06-06 20:27:51 +00001381 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 Py_ssize_t count;
1383 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 *errmsg = NULL;
1386 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001387 if (pb != NULL && pb->bf_releasebuffer != NULL) {
R David Murray861470c2014-10-05 11:47:01 -04001388 *errmsg = "read-only bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 return -1;
1390 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001391
Victor Stinner8182b712010-07-28 00:40:58 +00001392 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 count = view.len;
1395 *p = view.buf;
1396 PyBuffer_Release(&view);
1397 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001398}
Guido van Rossumaa354651996-08-19 19:32:04 +00001399
Martin v. Löwis423be952008-08-13 15:53:07 +00001400static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001401getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001402{
Victor Stinner8182b712010-07-28 00:40:58 +00001403 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
R David Murray861470c2014-10-05 11:47:01 -04001404 *errmsg = "bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 return -1;
1406 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001407 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001408 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001409 *errmsg = "contiguous buffer";
1410 return -1;
1411 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001413}
1414
Guido van Rossumaa354651996-08-19 19:32:04 +00001415/* Support for keyword arguments donated by
1416 Geoff Philbrick <philbric@delphi.hks.com> */
1417
Tim Petersf8cd3e82001-10-27 04:26:57 +00001418/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001419int
1420PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 PyObject *keywords,
1422 const char *format,
1423 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 int retval;
1426 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 if ((args == NULL || !PyTuple_Check(args)) ||
1429 (keywords != NULL && !PyDict_Check(keywords)) ||
1430 format == NULL ||
1431 kwlist == NULL)
1432 {
1433 PyErr_BadInternalCall();
1434 return 0;
1435 }
Tim Peters45772cd2001-10-27 03:58:40 +00001436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 va_start(va, kwlist);
1438 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1439 va_end(va);
1440 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001441}
1442
1443int
1444_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 PyObject *keywords,
1446 const char *format,
1447 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 int retval;
1450 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if ((args == NULL || !PyTuple_Check(args)) ||
1453 (keywords != NULL && !PyDict_Check(keywords)) ||
1454 format == NULL ||
1455 kwlist == NULL)
1456 {
1457 PyErr_BadInternalCall();
1458 return 0;
1459 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 va_start(va, kwlist);
1462 retval = vgetargskeywords(args, keywords, format,
1463 kwlist, &va, FLAG_SIZE_T);
1464 va_end(va);
1465 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001466}
1467
1468
Brett Cannon711e7d92004-07-10 22:20:32 +00001469int
1470PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001471 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001472 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001473 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 int retval;
1476 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 if ((args == NULL || !PyTuple_Check(args)) ||
1479 (keywords != NULL && !PyDict_Check(keywords)) ||
1480 format == NULL ||
1481 kwlist == NULL)
1482 {
1483 PyErr_BadInternalCall();
1484 return 0;
1485 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001486
Benjamin Peterson0c212142016-09-20 20:39:33 -07001487 va_copy(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001490 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001492}
1493
1494int
1495_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 PyObject *keywords,
1497 const char *format,
1498 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 int retval;
1501 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 if ((args == NULL || !PyTuple_Check(args)) ||
1504 (keywords != NULL && !PyDict_Check(keywords)) ||
1505 format == NULL ||
1506 kwlist == NULL)
1507 {
1508 PyErr_BadInternalCall();
1509 return 0;
1510 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001511
Benjamin Peterson0c212142016-09-20 20:39:33 -07001512 va_copy(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 retval = vgetargskeywords(args, keywords, format,
1515 kwlist, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001516 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001518}
1519
Benjamin Petersonfb886362010-04-24 18:21:17 +00001520int
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001521_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1522 struct _PyArg_Parser *parser, ...)
1523{
1524 int retval;
1525 va_list va;
1526
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001527 va_start(va, parser);
1528 retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1529 va_end(va);
1530 return retval;
1531}
1532
1533int
1534_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1535 struct _PyArg_Parser *parser, ...)
1536{
1537 int retval;
1538 va_list va;
1539
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001540 va_start(va, parser);
1541 retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
1542 va_end(va);
1543 return retval;
1544}
1545
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001546int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001547_PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001548 struct _PyArg_Parser *parser, ...)
1549{
1550 int retval;
1551 va_list va;
1552
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001553 va_start(va, parser);
1554 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1555 va_end(va);
1556 return retval;
1557}
1558
1559int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001560_PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001561 struct _PyArg_Parser *parser, ...)
1562{
1563 int retval;
1564 va_list va;
1565
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001566 va_start(va, parser);
1567 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T);
1568 va_end(va);
1569 return retval;
1570}
1571
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001572
1573int
1574_PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1575 struct _PyArg_Parser *parser, va_list va)
1576{
1577 int retval;
1578 va_list lva;
1579
Benjamin Peterson0c212142016-09-20 20:39:33 -07001580 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001581
1582 retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001583 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001584 return retval;
1585}
1586
1587int
1588_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1589 struct _PyArg_Parser *parser, va_list va)
1590{
1591 int retval;
1592 va_list lva;
1593
Benjamin Peterson0c212142016-09-20 20:39:33 -07001594 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001595
1596 retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001597 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001598 return retval;
1599}
1600
1601int
Benjamin Petersonfb886362010-04-24 18:21:17 +00001602PyArg_ValidateKeywordArguments(PyObject *kwargs)
1603{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001604 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 PyErr_BadInternalCall();
1606 return 0;
1607 }
1608 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1609 PyErr_SetString(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02001610 "keywords must be strings");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 return 0;
1612 }
1613 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001614}
1615
Christian Heimes380f7f22008-02-28 11:19:05 +00001616#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001617
Guido van Rossumaa354651996-08-19 19:32:04 +00001618static int
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001619vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 char msgbuf[512];
1623 int levels[32];
Serhiy Storchaka17414412017-01-17 10:07:25 +02001624 const char *fname, *msg, *custom_msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 int min = INT_MAX;
Larry Hastings83a9f482012-03-20 20:06:16 +00001626 int max = INT_MAX;
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001627 int i, pos, len;
1628 int skip = 0;
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001629 Py_ssize_t nargs, nkwargs;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001630 PyObject *current_arg;
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001631 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
Benjamin Peterson40be9e52014-02-11 10:09:27 -05001632 freelist_t freelist;
1633
1634 freelist.entries = static_entries;
1635 freelist.first_available = 0;
1636 freelist.entries_malloced = 0;
Tim Petersf4331c12001-10-27 00:17:34 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 assert(args != NULL && PyTuple_Check(args));
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001639 assert(kwargs == NULL || PyDict_Check(kwargs));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 assert(format != NULL);
1641 assert(kwlist != NULL);
1642 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 /* grab the function name or custom error msg first (mutually exclusive) */
1645 fname = strchr(format, ':');
1646 if (fname) {
1647 fname++;
1648 custom_msg = NULL;
1649 }
1650 else {
1651 custom_msg = strchr(format,';');
1652 if (custom_msg)
1653 custom_msg++;
1654 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001655
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001656 /* scan kwlist and count the number of positional-only parameters */
1657 for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1658 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 /* scan kwlist and get greatest possible nbr of args */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001660 for (len = pos; kwlist[len]; len++) {
1661 if (!*kwlist[len]) {
1662 PyErr_SetString(PyExc_SystemError,
1663 "Empty keyword parameter name");
1664 return cleanreturn(0, &freelist);
1665 }
1666 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001667
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001668 if (len > STATIC_FREELIST_ENTRIES) {
1669 freelist.entries = PyMem_NEW(freelistentry_t, len);
1670 if (freelist.entries == NULL) {
1671 PyErr_NoMemory();
1672 return 0;
1673 }
1674 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -05001675 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 nargs = PyTuple_GET_SIZE(args);
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001678 nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
1679 if (nargs + nkwargs > len) {
Oren Milmanbf9075a2017-08-23 21:16:48 +03001680 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
1681 messages in some special cases (see bpo-31229). */
Victor Stinner6ced7c42011-03-21 18:15:42 +01001682 PyErr_Format(PyExc_TypeError,
Oren Milmanbf9075a2017-08-23 21:16:48 +03001683 "%.200s%s takes at most %d %sargument%s (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 (fname == NULL) ? "function" : fname,
1685 (fname == NULL) ? "" : "()",
1686 len,
Oren Milmanbf9075a2017-08-23 21:16:48 +03001687 (nargs == 0) ? "keyword " : "",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 (len == 1) ? "" : "s",
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001689 nargs + nkwargs);
Benjamin Peterson01feaec2012-03-16 13:25:58 -05001690 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 }
Tim Petersc2f01122001-10-27 07:25:06 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1694 for (i = 0; i < len; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 if (*format == '|') {
Larry Hastings83a9f482012-03-20 20:06:16 +00001696 if (min != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001697 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001698 "Invalid format string (| specified twice)");
1699 return cleanreturn(0, &freelist);
1700 }
1701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 min = i;
1703 format++;
Larry Hastings83a9f482012-03-20 20:06:16 +00001704
1705 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001706 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001707 "Invalid format string ($ before |)");
1708 return cleanreturn(0, &freelist);
1709 }
1710 }
1711 if (*format == '$') {
1712 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001713 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001714 "Invalid format string ($ specified twice)");
1715 return cleanreturn(0, &freelist);
1716 }
1717
1718 max = i;
1719 format++;
1720
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001721 if (max < pos) {
1722 PyErr_SetString(PyExc_SystemError,
1723 "Empty parameter name after $");
1724 return cleanreturn(0, &freelist);
1725 }
1726 if (skip) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001727 /* Now we know the minimal and the maximal numbers of
1728 * positional arguments and can raise an exception with
1729 * informative message (see below). */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001730 break;
1731 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001732 if (max < nargs) {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03001733 if (max == 0) {
1734 PyErr_Format(PyExc_TypeError,
1735 "%.200s%s takes no positional arguments",
1736 (fname == NULL) ? "function" : fname,
1737 (fname == NULL) ? "" : "()");
1738 }
1739 else {
1740 PyErr_Format(PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05301741 "%.200s%s takes %s %d positional argument%s"
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03001742 " (%d given)",
1743 (fname == NULL) ? "function" : fname,
1744 (fname == NULL) ? "" : "()",
1745 (min != INT_MAX) ? "at most" : "exactly",
Xtreak63262782018-12-21 20:15:13 +05301746 max,
1747 max == 1 ? "" : "s",
1748 nargs);
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03001749 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001750 return cleanreturn(0, &freelist);
1751 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 }
1753 if (IS_END_OF_FORMAT(*format)) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001754 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 "More keyword list entries (%d) than "
1756 "format specifiers (%d)", len, i);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001757 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001759 if (!skip) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02001760 if (i < nargs) {
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001761 current_arg = PyTuple_GET_ITEM(args, i);
Serhiy Storchaka17414412017-01-17 10:07:25 +02001762 }
1763 else if (nkwargs && i >= pos) {
1764 current_arg = PyDict_GetItemString(kwargs, kwlist[i]);
1765 if (current_arg)
1766 --nkwargs;
1767 }
1768 else {
1769 current_arg = NULL;
1770 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001771
1772 if (current_arg) {
1773 msg = convertitem(current_arg, &format, p_va, flags,
1774 levels, msgbuf, sizeof(msgbuf), &freelist);
1775 if (msg) {
1776 seterror(i+1, msg, levels, fname, custom_msg);
1777 return cleanreturn(0, &freelist);
1778 }
1779 continue;
1780 }
1781
1782 if (i < min) {
1783 if (i < pos) {
1784 assert (min == INT_MAX);
1785 assert (max == INT_MAX);
1786 skip = 1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001787 /* At that moment we still don't know the minimal and
1788 * the maximal numbers of positional arguments. Raising
1789 * an exception is deferred until we encounter | and $
1790 * or the end of the format. */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001791 }
1792 else {
Michael Seifert64c8f702017-04-09 09:47:12 +02001793 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
1794 "argument '%s' (pos %d)",
1795 (fname == NULL) ? "function" : fname,
1796 (fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02001797 kwlist[i], i+1);
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001798 return cleanreturn(0, &freelist);
1799 }
1800 }
1801 /* current code reports success when all required args
1802 * fulfilled and no keyword args left, with no further
1803 * validation. XXX Maybe skip this in debug build ?
1804 */
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001805 if (!nkwargs && !skip) {
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001806 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 }
1808 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001809
Leo Ariasc3d95082018-02-03 18:36:10 -06001810 /* We are into optional args, skip through to any remaining
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 * keyword args */
1812 msg = skipitem(&format, p_va, flags);
1813 if (msg) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001814 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001816 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 }
1818 }
Tim Petersb054be42001-10-27 05:07:41 +00001819
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001820 if (skip) {
1821 PyErr_Format(PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05301822 "%.200s%s takes %s %d positional argument%s"
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001823 " (%d given)",
Michael Seifert64c8f702017-04-09 09:47:12 +02001824 (fname == NULL) ? "function" : fname,
1825 (fname == NULL) ? "" : "()",
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001826 (Py_MIN(pos, min) < i) ? "at least" : "exactly",
Xtreak63262782018-12-21 20:15:13 +05301827 Py_MIN(pos, min),
1828 Py_MIN(pos, min) == 1 ? "" : "s",
1829 nargs);
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001830 return cleanreturn(0, &freelist);
1831 }
1832
Larry Hastings83a9f482012-03-20 20:06:16 +00001833 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001834 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 "more argument specifiers than keyword list entries "
1836 "(remaining format:'%s')", format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001837 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 }
Tim Petersc2f01122001-10-27 07:25:06 +00001839
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001840 if (nkwargs > 0) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02001841 PyObject *key;
1842 Py_ssize_t j;
1843 /* make sure there are no arguments given by name and position */
1844 for (i = pos; i < nargs; i++) {
1845 current_arg = PyDict_GetItemString(kwargs, kwlist[i]);
1846 if (current_arg) {
1847 /* arg present in tuple and in dict */
1848 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02001849 "argument for %.200s%s given by name ('%s') "
Serhiy Storchaka17414412017-01-17 10:07:25 +02001850 "and position (%d)",
Michael Seifert64c8f702017-04-09 09:47:12 +02001851 (fname == NULL) ? "function" : fname,
1852 (fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02001853 kwlist[i], i+1);
1854 return cleanreturn(0, &freelist);
1855 }
1856 }
1857 /* make sure there are no extraneous keyword arguments */
1858 j = 0;
1859 while (PyDict_Next(kwargs, &j, &key, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 int match = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 if (!PyUnicode_Check(key)) {
1862 PyErr_SetString(PyExc_TypeError,
1863 "keywords must be strings");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001864 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02001866 for (i = pos; i < len; i++) {
1867 if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001868 match = 1;
1869 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 }
1871 }
1872 if (!match) {
1873 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001874 "'%U' is an invalid keyword "
Michael Seifert64c8f702017-04-09 09:47:12 +02001875 "argument for %.200s%s",
1876 key,
1877 (fname == NULL) ? "this function" : fname,
1878 (fname == NULL) ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001879 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 }
1881 }
1882 }
1883
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001884 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001885}
1886
1887
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001888/* List of static parsers. */
1889static struct _PyArg_Parser *static_arg_parsers = NULL;
1890
1891static int
1892parser_init(struct _PyArg_Parser *parser)
1893{
1894 const char * const *keywords;
1895 const char *format, *msg;
1896 int i, len, min, max, nkw;
1897 PyObject *kwtuple;
1898
1899 assert(parser->format != NULL);
1900 assert(parser->keywords != NULL);
1901 if (parser->kwtuple != NULL) {
1902 return 1;
1903 }
1904
1905 /* grab the function name or custom error msg first (mutually exclusive) */
1906 parser->fname = strchr(parser->format, ':');
1907 if (parser->fname) {
1908 parser->fname++;
1909 parser->custom_msg = NULL;
1910 }
1911 else {
1912 parser->custom_msg = strchr(parser->format,';');
1913 if (parser->custom_msg)
1914 parser->custom_msg++;
1915 }
1916
1917 keywords = parser->keywords;
1918 /* scan keywords and count the number of positional-only parameters */
1919 for (i = 0; keywords[i] && !*keywords[i]; i++) {
1920 }
1921 parser->pos = i;
1922 /* scan keywords and get greatest possible nbr of args */
1923 for (; keywords[i]; i++) {
1924 if (!*keywords[i]) {
1925 PyErr_SetString(PyExc_SystemError,
1926 "Empty keyword parameter name");
1927 return 0;
1928 }
1929 }
1930 len = i;
1931
1932 min = max = INT_MAX;
1933 format = parser->format;
1934 for (i = 0; i < len; i++) {
1935 if (*format == '|') {
1936 if (min != INT_MAX) {
1937 PyErr_SetString(PyExc_SystemError,
1938 "Invalid format string (| specified twice)");
1939 return 0;
1940 }
1941 if (max != INT_MAX) {
1942 PyErr_SetString(PyExc_SystemError,
1943 "Invalid format string ($ before |)");
1944 return 0;
1945 }
1946 min = i;
1947 format++;
1948 }
1949 if (*format == '$') {
1950 if (max != INT_MAX) {
1951 PyErr_SetString(PyExc_SystemError,
1952 "Invalid format string ($ specified twice)");
1953 return 0;
1954 }
1955 if (i < parser->pos) {
1956 PyErr_SetString(PyExc_SystemError,
1957 "Empty parameter name after $");
1958 return 0;
1959 }
1960 max = i;
1961 format++;
1962 }
1963 if (IS_END_OF_FORMAT(*format)) {
1964 PyErr_Format(PyExc_SystemError,
1965 "More keyword list entries (%d) than "
1966 "format specifiers (%d)", len, i);
1967 return 0;
1968 }
1969
1970 msg = skipitem(&format, NULL, 0);
1971 if (msg) {
1972 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
1973 format);
1974 return 0;
1975 }
1976 }
1977 parser->min = Py_MIN(min, len);
1978 parser->max = Py_MIN(max, len);
1979
1980 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
1981 PyErr_Format(PyExc_SystemError,
1982 "more argument specifiers than keyword list entries "
1983 "(remaining format:'%s')", format);
1984 return 0;
1985 }
1986
1987 nkw = len - parser->pos;
1988 kwtuple = PyTuple_New(nkw);
1989 if (kwtuple == NULL) {
1990 return 0;
1991 }
1992 keywords = parser->keywords + parser->pos;
1993 for (i = 0; i < nkw; i++) {
1994 PyObject *str = PyUnicode_FromString(keywords[i]);
1995 if (str == NULL) {
1996 Py_DECREF(kwtuple);
1997 return 0;
1998 }
1999 PyUnicode_InternInPlace(&str);
2000 PyTuple_SET_ITEM(kwtuple, i, str);
2001 }
2002 parser->kwtuple = kwtuple;
2003
2004 assert(parser->next == NULL);
2005 parser->next = static_arg_parsers;
2006 static_arg_parsers = parser;
2007 return 1;
2008}
2009
2010static void
2011parser_clear(struct _PyArg_Parser *parser)
2012{
2013 Py_CLEAR(parser->kwtuple);
2014}
2015
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002016static PyObject*
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002017find_keyword(PyObject *kwargs, PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002018{
2019 Py_ssize_t i, nkwargs;
2020
Serhiy Storchaka17414412017-01-17 10:07:25 +02002021 if (kwargs != NULL) {
2022 return PyDict_GetItem(kwargs, key);
2023 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002024 nkwargs = PyTuple_GET_SIZE(kwnames);
2025 for (i=0; i < nkwargs; i++) {
2026 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
2027
2028 /* ptr==ptr should match in most cases since keyword keys
2029 should be interned strings */
2030 if (kwname == key) {
2031 return kwstack[i];
2032 }
2033 if (!PyUnicode_Check(kwname)) {
2034 /* ignore non-string keyword keys:
Serhiy Storchaka17414412017-01-17 10:07:25 +02002035 an error will be raised below */
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002036 continue;
2037 }
2038 if (_PyUnicode_EQ(kwname, key)) {
2039 return kwstack[i];
2040 }
2041 }
2042 return NULL;
2043}
2044
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002045static int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002046vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002047 PyObject *kwargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002048 struct _PyArg_Parser *parser,
2049 va_list *p_va, int flags)
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002050{
2051 PyObject *kwtuple;
2052 char msgbuf[512];
2053 int levels[32];
2054 const char *format;
2055 const char *msg;
2056 PyObject *keyword;
2057 int i, pos, len;
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002058 Py_ssize_t nkwargs;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002059 PyObject *current_arg;
2060 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
2061 freelist_t freelist;
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002062 PyObject *const *kwstack = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002063
2064 freelist.entries = static_entries;
2065 freelist.first_available = 0;
2066 freelist.entries_malloced = 0;
2067
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002068 assert(kwargs == NULL || PyDict_Check(kwargs));
Serhiy Storchaka17414412017-01-17 10:07:25 +02002069 assert(kwargs == NULL || kwnames == NULL);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002070 assert(p_va != NULL);
2071
Victor Stinner80ab22f2017-01-16 23:16:47 +01002072 if (parser == NULL) {
2073 PyErr_BadInternalCall();
2074 return 0;
2075 }
2076
2077 if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2078 PyErr_BadInternalCall();
2079 return 0;
2080 }
2081
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002082 if (!parser_init(parser)) {
2083 return 0;
2084 }
2085
2086 kwtuple = parser->kwtuple;
2087 pos = parser->pos;
Segev Finere7bc7aa2017-07-26 18:58:25 +03002088 len = pos + (int)PyTuple_GET_SIZE(kwtuple);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002089
2090 if (len > STATIC_FREELIST_ENTRIES) {
2091 freelist.entries = PyMem_NEW(freelistentry_t, len);
2092 if (freelist.entries == NULL) {
2093 PyErr_NoMemory();
2094 return 0;
2095 }
2096 freelist.entries_malloced = 1;
2097 }
2098
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002099 if (kwargs != NULL) {
2100 nkwargs = PyDict_GET_SIZE(kwargs);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002101 }
2102 else if (kwnames != NULL) {
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002103 nkwargs = PyTuple_GET_SIZE(kwnames);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002104 kwstack = args + nargs;
2105 }
2106 else {
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002107 nkwargs = 0;
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002108 }
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002109 if (nargs + nkwargs > len) {
Oren Milmanbf9075a2017-08-23 21:16:48 +03002110 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2111 messages in some special cases (see bpo-31229). */
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002112 PyErr_Format(PyExc_TypeError,
Oren Milmanbf9075a2017-08-23 21:16:48 +03002113 "%.200s%s takes at most %d %sargument%s (%zd given)",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002114 (parser->fname == NULL) ? "function" : parser->fname,
2115 (parser->fname == NULL) ? "" : "()",
2116 len,
Oren Milmanbf9075a2017-08-23 21:16:48 +03002117 (nargs == 0) ? "keyword " : "",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002118 (len == 1) ? "" : "s",
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002119 nargs + nkwargs);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002120 return cleanreturn(0, &freelist);
2121 }
2122 if (parser->max < nargs) {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002123 if (parser->max == 0) {
2124 PyErr_Format(PyExc_TypeError,
Serhiy Storchakaf9f1cca2017-06-09 19:27:06 +03002125 "%.200s%s takes no positional arguments",
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002126 (parser->fname == NULL) ? "function" : parser->fname,
2127 (parser->fname == NULL) ? "" : "()");
2128 }
2129 else {
2130 PyErr_Format(PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05302131 "%.200s%s takes %s %d positional argument%s (%d given)",
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002132 (parser->fname == NULL) ? "function" : parser->fname,
2133 (parser->fname == NULL) ? "" : "()",
2134 (parser->min != INT_MAX) ? "at most" : "exactly",
Xtreak63262782018-12-21 20:15:13 +05302135 parser->max,
2136 parser->max == 1 ? "" : "s",
2137 nargs);
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002138 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002139 return cleanreturn(0, &freelist);
2140 }
2141
2142 format = parser->format;
2143 /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
2144 for (i = 0; i < len; i++) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002145 if (*format == '|') {
2146 format++;
2147 }
2148 if (*format == '$') {
2149 format++;
2150 }
2151 assert(!IS_END_OF_FORMAT(*format));
2152
Serhiy Storchaka17414412017-01-17 10:07:25 +02002153 if (i < nargs) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002154 current_arg = args[i];
Victor Stinnera9efb2f2016-09-09 17:40:22 -07002155 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002156 else if (nkwargs && i >= pos) {
2157 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2158 current_arg = find_keyword(kwargs, kwnames, kwstack, keyword);
2159 if (current_arg)
2160 --nkwargs;
2161 }
2162 else {
2163 current_arg = NULL;
2164 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002165
2166 if (current_arg) {
2167 msg = convertitem(current_arg, &format, p_va, flags,
2168 levels, msgbuf, sizeof(msgbuf), &freelist);
2169 if (msg) {
2170 seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2171 return cleanreturn(0, &freelist);
2172 }
2173 continue;
2174 }
2175
2176 if (i < parser->min) {
2177 /* Less arguments than required */
2178 if (i < pos) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002179 Py_ssize_t min = Py_MIN(pos, parser->min);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002180 PyErr_Format(PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05302181 "%.200s%s takes %s %d positional argument%s"
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002182 " (%d given)",
Michael Seifert64c8f702017-04-09 09:47:12 +02002183 (parser->fname == NULL) ? "function" : parser->fname,
2184 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02002185 min < parser->max ? "at least" : "exactly",
Xtreak63262782018-12-21 20:15:13 +05302186 min,
2187 min == 1 ? "" : "s",
2188 nargs);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002189 }
2190 else {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002191 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
Michael Seifert64c8f702017-04-09 09:47:12 +02002192 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
2193 "argument '%U' (pos %d)",
2194 (parser->fname == NULL) ? "function" : parser->fname,
2195 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002196 keyword, i+1);
2197 }
2198 return cleanreturn(0, &freelist);
2199 }
2200 /* current code reports success when all required args
2201 * fulfilled and no keyword args left, with no further
2202 * validation. XXX Maybe skip this in debug build ?
2203 */
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002204 if (!nkwargs) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002205 return cleanreturn(1, &freelist);
2206 }
2207
Leo Ariasc3d95082018-02-03 18:36:10 -06002208 /* We are into optional args, skip through to any remaining
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002209 * keyword args */
2210 msg = skipitem(&format, p_va, flags);
2211 assert(msg == NULL);
2212 }
2213
2214 assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2215
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002216 if (nkwargs > 0) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002217 Py_ssize_t j;
2218 /* make sure there are no arguments given by name and position */
2219 for (i = pos; i < nargs; i++) {
2220 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
2221 current_arg = find_keyword(kwargs, kwnames, kwstack, keyword);
2222 if (current_arg) {
2223 /* arg present in tuple and in dict */
2224 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02002225 "argument for %.200s%s given by name ('%U') "
Serhiy Storchaka17414412017-01-17 10:07:25 +02002226 "and position (%d)",
Michael Seifert64c8f702017-04-09 09:47:12 +02002227 (parser->fname == NULL) ? "function" : parser->fname,
2228 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02002229 keyword, i+1);
2230 return cleanreturn(0, &freelist);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002231 }
2232 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002233 /* make sure there are no extraneous keyword arguments */
2234 j = 0;
2235 while (1) {
2236 int match;
2237 if (kwargs != NULL) {
2238 if (!PyDict_Next(kwargs, &j, &keyword, NULL))
2239 break;
2240 }
2241 else {
2242 if (j >= PyTuple_GET_SIZE(kwnames))
2243 break;
2244 keyword = PyTuple_GET_ITEM(kwnames, j);
2245 j++;
2246 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002247
Serhiy Storchaka17414412017-01-17 10:07:25 +02002248 if (!PyUnicode_Check(keyword)) {
2249 PyErr_SetString(PyExc_TypeError,
2250 "keywords must be strings");
2251 return cleanreturn(0, &freelist);
2252 }
2253 match = PySequence_Contains(kwtuple, keyword);
2254 if (match <= 0) {
2255 if (!match) {
2256 PyErr_Format(PyExc_TypeError,
2257 "'%U' is an invalid keyword "
Michael Seifert64c8f702017-04-09 09:47:12 +02002258 "argument for %.200s%s",
2259 keyword,
2260 (parser->fname == NULL) ? "this function" : parser->fname,
2261 (parser->fname == NULL) ? "" : "()");
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002262 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002263 return cleanreturn(0, &freelist);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002264 }
2265 }
2266 }
2267
2268 return cleanreturn(1, &freelist);
2269}
2270
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002271static int
2272vgetargskeywordsfast(PyObject *args, PyObject *keywords,
2273 struct _PyArg_Parser *parser, va_list *p_va, int flags)
2274{
2275 PyObject **stack;
2276 Py_ssize_t nargs;
2277
Victor Stinner80ab22f2017-01-16 23:16:47 +01002278 if (args == NULL
2279 || !PyTuple_Check(args)
2280 || (keywords != NULL && !PyDict_Check(keywords)))
2281 {
2282 PyErr_BadInternalCall();
2283 return 0;
2284 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002285
Victor Stinnerd17a6932018-11-09 16:56:48 +01002286 stack = _PyTuple_ITEMS(args);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002287 nargs = PyTuple_GET_SIZE(args);
2288 return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
2289 parser, p_va, flags);
2290}
2291
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002292
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002293static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002294skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00002295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 const char *format = *p_format;
2297 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00002300
Larry Hastingsa3479012012-05-08 23:52:03 -07002301 /*
2302 * codes that take a single data pointer as an argument
2303 * (the type of the pointer is irrelevant)
2304 */
Georg Brandl6dd14612005-09-14 19:29:53 +00002305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 case 'b': /* byte -- very short int */
2307 case 'B': /* byte as bitfield */
2308 case 'h': /* short int */
2309 case 'H': /* short int as bitfield */
2310 case 'i': /* int */
2311 case 'I': /* int sized bitfield */
2312 case 'l': /* long int */
2313 case 'k': /* long int sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -07002314 case 'L': /* long long */
2315 case 'K': /* long long sized bitfield */
Larry Hastingsa3479012012-05-08 23:52:03 -07002316 case 'n': /* Py_ssize_t */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 case 'f': /* float */
2318 case 'd': /* double */
2319 case 'D': /* complex double */
2320 case 'c': /* char */
2321 case 'C': /* unicode char */
Larry Hastings10ba07a2012-05-07 02:44:50 -07002322 case 'p': /* boolean predicate */
Larry Hastingsa3479012012-05-08 23:52:03 -07002323 case 'S': /* string object */
2324 case 'Y': /* string object */
2325 case 'U': /* unicode string object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002327 if (p_va != NULL) {
2328 (void) va_arg(*p_va, void *);
2329 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 break;
2331 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 case 'e': /* string with encoding */
2336 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002337 if (p_va != NULL) {
2338 (void) va_arg(*p_va, const char *);
2339 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 if (!(*format == 's' || *format == 't'))
2341 /* after 'e', only 's' and 't' is allowed */
2342 goto err;
2343 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 }
Stefan Krahf432a322017-08-21 13:09:59 +02002345 /* fall through */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 case 's': /* string */
2348 case 'z': /* string or None */
2349 case 'y': /* bytes */
2350 case 'u': /* unicode string */
Larry Hastingsd9e4a412012-05-08 03:51:18 -07002351 case 'Z': /* unicode string or None */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 case 'w': /* buffer, read-write */
2353 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002354 if (p_va != NULL) {
2355 (void) va_arg(*p_va, char **);
2356 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 if (*format == '#') {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002358 if (p_va != NULL) {
2359 if (flags & FLAG_SIZE_T)
2360 (void) va_arg(*p_va, Py_ssize_t *);
2361 else
2362 (void) va_arg(*p_va, int *);
2363 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 format++;
Serhiy Storchaka504373c2018-07-11 17:41:43 +03002365 } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w')
2366 && *format == '*')
2367 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 format++;
2369 }
2370 break;
2371 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 case 'O': /* object */
2374 {
2375 if (*format == '!') {
2376 format++;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002377 if (p_va != NULL) {
2378 (void) va_arg(*p_va, PyTypeObject*);
2379 (void) va_arg(*p_va, PyObject **);
2380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 }
2382 else if (*format == '&') {
2383 typedef int (*converter)(PyObject *, void *);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002384 if (p_va != NULL) {
2385 (void) va_arg(*p_va, converter);
2386 (void) va_arg(*p_va, void *);
2387 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 format++;
2389 }
2390 else {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002391 if (p_va != NULL) {
2392 (void) va_arg(*p_va, PyObject **);
2393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 }
2395 break;
2396 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 case '(': /* bypass tuple, not handled at all previously */
2399 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002400 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 for (;;) {
2402 if (*format==')')
2403 break;
2404 if (IS_END_OF_FORMAT(*format))
2405 return "Unmatched left paren in format "
2406 "string";
2407 msg = skipitem(&format, p_va, flags);
2408 if (msg)
2409 return msg;
2410 }
2411 format++;
2412 break;
2413 }
Christian Heimes380f7f22008-02-28 11:19:05 +00002414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 case ')':
2416 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00002419err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00002421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 *p_format = format;
2425 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00002426}
Fred Drakee4616e62001-10-23 21:09:29 +00002427
2428
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002429#undef _PyArg_CheckPositional
Fred Drakee4616e62001-10-23 21:09:29 +00002430
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002431int
2432_PyArg_CheckPositional(const char *name, Py_ssize_t nargs,
2433 Py_ssize_t min, Py_ssize_t max)
2434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 assert(min >= 0);
2436 assert(min <= max);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002437
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002438 if (nargs < min) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 if (name != NULL)
2440 PyErr_Format(
2441 PyExc_TypeError,
Xtreak1426daa2018-07-23 01:43:26 +05302442 "%.200s expected %s%zd argument%s, got %zd",
2443 name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 else
2445 PyErr_Format(
2446 PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05302447 "unpacked tuple should have %s%zd element%s,"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 " but has %zd",
Xtreak63262782018-12-21 20:15:13 +05302449 (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 return 0;
2451 }
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002452
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002453 if (nargs == 0) {
Raymond Hettinger94230232016-03-26 03:02:48 -07002454 return 1;
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002455 }
2456
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002457 if (nargs > max) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 if (name != NULL)
2459 PyErr_Format(
2460 PyExc_TypeError,
Xtreak1426daa2018-07-23 01:43:26 +05302461 "%.200s expected %s%zd argument%s, got %zd",
2462 name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 else
2464 PyErr_Format(
2465 PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05302466 "unpacked tuple should have %s%zd element%s,"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 " but has %zd",
Xtreak63262782018-12-21 20:15:13 +05302468 (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 return 0;
2470 }
Raymond Hettinger94230232016-03-26 03:02:48 -07002471
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002472 return 1;
2473}
2474
2475static int
2476unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name,
2477 Py_ssize_t min, Py_ssize_t max, va_list vargs)
2478{
2479 Py_ssize_t i;
2480 PyObject **o;
2481
2482 if (!_PyArg_CheckPositional(name, nargs, min, max)) {
2483 return 0;
2484 }
2485
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002486 for (i = 0; i < nargs; i++) {
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002487 o = va_arg(vargs, PyObject **);
2488 *o = args[i];
2489 }
2490 return 1;
2491}
2492
2493int
2494PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
2495{
2496 PyObject **stack;
2497 Py_ssize_t nargs;
2498 int retval;
2499 va_list vargs;
2500
2501 if (!PyTuple_Check(args)) {
2502 PyErr_SetString(PyExc_SystemError,
2503 "PyArg_UnpackTuple() argument list is not a tuple");
2504 return 0;
2505 }
Victor Stinnerd17a6932018-11-09 16:56:48 +01002506 stack = _PyTuple_ITEMS(args);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002507 nargs = PyTuple_GET_SIZE(args);
2508
Raymond Hettinger94230232016-03-26 03:02:48 -07002509#ifdef HAVE_STDARG_PROTOTYPES
2510 va_start(vargs, max);
2511#else
2512 va_start(vargs);
2513#endif
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002514 retval = unpack_stack(stack, nargs, name, min, max, vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 va_end(vargs);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002516 return retval;
2517}
2518
2519int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002520_PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002521 Py_ssize_t min, Py_ssize_t max, ...)
2522{
2523 int retval;
2524 va_list vargs;
2525
2526#ifdef HAVE_STDARG_PROTOTYPES
2527 va_start(vargs, max);
2528#else
2529 va_start(vargs);
2530#endif
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002531 retval = unpack_stack(args, nargs, name, min, max, vargs);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002532 va_end(vargs);
2533 return retval;
Fred Drakee4616e62001-10-23 21:09:29 +00002534}
Georg Brandl02c42872005-08-26 06:42:30 +00002535
2536
Serhiy Storchaka68a001d2017-02-06 10:41:46 +02002537#undef _PyArg_NoKeywords
Serhiy Storchaka68a001d2017-02-06 10:41:46 +02002538#undef _PyArg_NoPositional
2539
Georg Brandl02c42872005-08-26 06:42:30 +00002540/* For type constructors that don't take keyword args
2541 *
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002542 * Sets a TypeError and returns 0 if the args/kwargs is
Thomas Wouters89f507f2006-12-13 04:49:30 +00002543 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00002544 */
2545int
Victor Stinner29d39cc2017-01-17 01:40:01 +01002546_PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
Georg Brandl02c42872005-08-26 06:42:30 +00002547{
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002548 if (kwargs == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 return 1;
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002550 }
Victor Stinner29d39cc2017-01-17 01:40:01 +01002551 if (!PyDict_CheckExact(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 PyErr_BadInternalCall();
2553 return 0;
2554 }
Victor Stinner29d39cc2017-01-17 01:40:01 +01002555 if (PyDict_GET_SIZE(kwargs) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 return 1;
Victor Stinner29d39cc2017-01-17 01:40:01 +01002557 }
2558
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002559 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
Victor Stinner29d39cc2017-01-17 01:40:01 +01002560 funcname);
2561 return 0;
2562}
2563
2564
2565int
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002566_PyArg_NoPositional(const char *funcname, PyObject *args)
2567{
2568 if (args == NULL)
2569 return 1;
2570 if (!PyTuple_CheckExact(args)) {
2571 PyErr_BadInternalCall();
2572 return 0;
2573 }
2574 if (PyTuple_GET_SIZE(args) == 0)
2575 return 1;
2576
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002577 PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002578 funcname);
2579 return 0;
2580}
2581
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002582void
2583_PyArg_Fini(void)
2584{
2585 struct _PyArg_Parser *tmp, *s = static_arg_parsers;
2586 while (s) {
2587 tmp = s->next;
2588 s->next = NULL;
2589 parser_clear(s);
2590 s = tmp;
2591 }
2592 static_arg_parsers = NULL;
2593}
2594
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002595#ifdef __cplusplus
2596};
2597#endif