blob: 774242828e45e389d2049f8a6531e62789fd43f8 [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
Vinay Sajip0b60f642019-10-15 08:26:12 +0100109PyAPI_FUNC(int)
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
Vinay Sajip0b60f642019-10-15 08:26:12 +0100134PyAPI_FUNC(int)
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
Vinay Sajip0b60f642019-10-15 08:26:12 +0100159PyAPI_FUNC(int)
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
Vinay Sajip0b60f642019-10-15 08:26:12 +0100185PyAPI_FUNC(int)
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) {
Jordon Xu2ec70102019-09-11 00:04:08 +0800315 if (Py_ISALPHA(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,
Serhiy Storchakad53fe5f2019-03-13 22:59:55 +0200375 "%.150s%s takes %s %d argument%s (%zd given)",
Victor Stinner6ced7c42011-03-21 18:15:42 +0100376 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",
Serhiy Storchakad53fe5f2019-03-13 22:59:55 +0200382 nargs);
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
Jordon Xu2ec70102019-09-11 00:04:08 +0800400 if (*format != '\0' && !Py_ISALPHA(*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;
Jordon Xu2ec70102019-09-11 00:04:08 +0800524 else if (level == 0 && Py_ISALPHA(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,
Victor Stinnera102ed72020-02-07 02:24:48 +0100534 arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 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
RĂ©mi Lapeyre4901fe22019-08-29 16:49:08 +0200613/* Format an error message generated by convertsimple().
614 displayname must be UTF-8 encoded.
615*/
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000616
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200617void
RĂ©mi Lapeyre4901fe22019-08-29 16:49:08 +0200618_PyArg_BadArgument(const char *fname, const char *displayname,
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200619 const char *expected, PyObject *arg)
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200620{
RĂ©mi Lapeyre4901fe22019-08-29 16:49:08 +0200621 PyErr_Format(PyExc_TypeError,
622 "%.200s() %.200s must be %.50s, not %.50s",
623 fname, displayname, expected,
Victor Stinnera102ed72020-02-07 02:24:48 +0100624 arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200625}
626
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200627static const char *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000628converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 assert(expected != NULL);
631 assert(arg != NULL);
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200632 if (expected[0] == '(') {
633 PyOS_snprintf(msgbuf, bufsize,
634 "%.100s", expected);
635 }
636 else {
637 PyOS_snprintf(msgbuf, bufsize,
638 "must be %.50s, not %.50s", expected,
Victor Stinnera102ed72020-02-07 02:24:48 +0100639 arg == Py_None ? "None" : Py_TYPE(arg)->tp_name);
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200640 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 return msgbuf;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000642}
643
644#define CONV_UNICODE "(unicode conversion error)"
645
Guido van Rossum45aecf42006-03-15 04:58:47 +0000646/* Explicitly check for float arguments when integers are expected.
Serhiy Storchaka6a44f6e2019-02-25 17:57:58 +0200647 Return 1 for error, 0 if ok.
648 XXX Should be removed after the end of the deprecation period in
649 _PyLong_FromNbIndexOrNbInt. */
Neil Schemenauer5042da62003-02-04 20:59:40 +0000650static int
651float_argument_error(PyObject *arg)
652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (PyFloat_Check(arg)) {
654 PyErr_SetString(PyExc_TypeError,
655 "integer argument expected, got float" );
656 return 1;
657 }
658 else
659 return 0;
Neil Schemenauer5042da62003-02-04 20:59:40 +0000660}
661
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000662/* Convert a non-tuple argument. Return NULL if conversion went OK,
663 or a string with a message describing the failure. The message is
664 formatted as "must be <desired type>, not <actual type>".
665 When failing, an exception may or may not have been raised.
Georg Brandl6dd14612005-09-14 19:29:53 +0000666 Don't call if a tuple is expected.
667
668 When you add new format codes, please don't forget poor skipitem() below.
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000669*/
670
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200671static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000672convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400673 char *msgbuf, size_t bufsize, freelist_t *freelist)
Guido van Rossumfe3f1a21994-09-29 09:42:55 +0000674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 /* For # codes */
676#define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\
677 if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \
Inada Naokid3c72a22019-03-23 21:04:40 +0900678 else { \
679 if (PyErr_WarnEx(PyExc_DeprecationWarning, \
680 "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) { \
681 return NULL; \
682 } \
683 q=va_arg(*p_va, int*); \
684 }
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000685#define STORE_SIZE(s) \
686 if (flags & FLAG_SIZE_T) \
687 *q2=s; \
688 else { \
689 if (INT_MAX < s) { \
690 PyErr_SetString(PyExc_OverflowError, \
691 "size does not fit in an int"); \
692 return converterr("", arg, msgbuf, bufsize); \
693 } \
Victor Stinner9550ef32013-06-05 01:18:13 +0200694 *q = (int)s; \
Victor Stinnerb3c9e072011-01-04 02:07:34 +0000695 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000696#define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q)
Victor Stinner6ab8e822011-01-04 11:16:49 +0000697#define RETURN_ERR_OCCURRED return msgbuf
Martin v. Löwis18e16552006-02-15 17:27:45 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 const char *format = *p_format;
700 char c = *format++;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200701 const char *sarg;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 switch (c) {
Guido van Rossum98297ee2007-11-06 21:34:58 +0000704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 case 'b': { /* unsigned byte -- very short int */
706 char *p = va_arg(*p_va, char *);
707 long ival;
708 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000709 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 ival = PyLong_AsLong(arg);
711 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000712 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 else if (ival < 0) {
714 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000715 "unsigned byte integer is less than minimum");
716 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 }
718 else if (ival > UCHAR_MAX) {
719 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000720 "unsigned byte integer is greater than maximum");
721 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 }
723 else
724 *p = (unsigned char) ival;
725 break;
726 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 case 'B': {/* byte sized bitfield - both signed and unsigned
729 values allowed */
730 char *p = va_arg(*p_va, char *);
731 long ival;
732 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000733 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 ival = PyLong_AsUnsignedLongMask(arg);
735 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000736 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 else
738 *p = (unsigned char) ival;
739 break;
740 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 case 'h': {/* signed short int */
743 short *p = va_arg(*p_va, short *);
744 long ival;
745 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000746 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 ival = PyLong_AsLong(arg);
748 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000749 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 else if (ival < SHRT_MIN) {
751 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000752 "signed short integer is less than minimum");
753 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 }
755 else if (ival > SHRT_MAX) {
756 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000757 "signed short integer is greater than maximum");
758 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 }
760 else
761 *p = (short) ival;
762 break;
763 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 case 'H': { /* short int sized bitfield, both signed and
766 unsigned allowed */
767 unsigned short *p = va_arg(*p_va, unsigned short *);
768 long ival;
769 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000770 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 ival = PyLong_AsUnsignedLongMask(arg);
772 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000773 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 else
775 *p = (unsigned short) ival;
776 break;
777 }
Martin v. Löwis18e16552006-02-15 17:27:45 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 case 'i': {/* signed int */
780 int *p = va_arg(*p_va, int *);
781 long ival;
782 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000783 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 ival = PyLong_AsLong(arg);
785 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000786 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 else if (ival > INT_MAX) {
788 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000789 "signed integer is greater than maximum");
790 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 }
792 else if (ival < INT_MIN) {
793 PyErr_SetString(PyExc_OverflowError,
Victor Stinner6ab8e822011-01-04 11:16:49 +0000794 "signed integer is less than minimum");
795 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 }
797 else
798 *p = ival;
799 break;
800 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 case 'I': { /* int sized bitfield, both signed and
803 unsigned allowed */
804 unsigned int *p = va_arg(*p_va, unsigned int *);
805 unsigned int ival;
806 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000807 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 ival = (unsigned int)PyLong_AsUnsignedLongMask(arg);
809 if (ival == (unsigned int)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000810 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 else
812 *p = ival;
813 break;
814 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 case 'n': /* Py_ssize_t */
817 {
818 PyObject *iobj;
819 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *);
820 Py_ssize_t ival = -1;
821 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000822 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 iobj = PyNumber_Index(arg);
824 if (iobj != NULL) {
825 ival = PyLong_AsSsize_t(iobj);
826 Py_DECREF(iobj);
827 }
828 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000829 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 *p = ival;
831 break;
832 }
833 case 'l': {/* long int */
834 long *p = va_arg(*p_va, long *);
835 long ival;
836 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000837 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 ival = PyLong_AsLong(arg);
839 if (ival == -1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000840 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 else
842 *p = ival;
843 break;
844 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 case 'k': { /* long sized bitfield */
847 unsigned long *p = va_arg(*p_va, unsigned long *);
848 unsigned long ival;
849 if (PyLong_Check(arg))
850 ival = PyLong_AsUnsignedLongMask(arg);
851 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200852 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 *p = ival;
854 break;
855 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000856
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700857 case 'L': {/* long long */
858 long long *p = va_arg( *p_va, long long * );
859 long long ival;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000860 if (float_argument_error(arg))
Victor Stinner6ab8e822011-01-04 11:16:49 +0000861 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 ival = PyLong_AsLongLong(arg);
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700863 if (ival == (long long)-1 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000864 RETURN_ERR_OCCURRED;
Mark Dickinsonc7301312010-06-10 16:05:10 +0000865 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 *p = ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 break;
868 }
Thomas Hellera4ea6032003-04-17 18:55:45 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 case 'K': { /* long long sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -0700871 unsigned long long *p = va_arg(*p_va, unsigned long long *);
872 unsigned long long ival;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (PyLong_Check(arg))
874 ival = PyLong_AsUnsignedLongLongMask(arg);
875 else
Serhiy Storchakac4b813d2016-02-08 01:06:11 +0200876 return converterr("int", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 *p = ival;
878 break;
879 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 case 'f': {/* float */
882 float *p = va_arg(*p_va, float *);
883 double dval = PyFloat_AsDouble(arg);
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700884 if (dval == -1.0 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000885 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 else
887 *p = (float) dval;
888 break;
889 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 case 'd': {/* double */
892 double *p = va_arg(*p_va, double *);
893 double dval = PyFloat_AsDouble(arg);
Raymond Hettingeraef9ad82019-08-24 19:10:39 -0700894 if (dval == -1.0 && PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000895 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 else
897 *p = dval;
898 break;
899 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 case 'D': {/* complex double */
902 Py_complex *p = va_arg(*p_va, Py_complex *);
903 Py_complex cval;
904 cval = PyComplex_AsCComplex(arg);
905 if (PyErr_Occurred())
Victor Stinner6ab8e822011-01-04 11:16:49 +0000906 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 else
908 *p = cval;
909 break;
910 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 case 'c': {/* char */
913 char *p = va_arg(*p_va, char *);
914 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1)
915 *p = PyBytes_AS_STRING(arg)[0];
Eli Bendersky906b88f2011-07-29 07:05:08 +0300916 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1)
917 *p = PyByteArray_AS_STRING(arg)[0];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 else
919 return converterr("a byte string of length 1", arg, msgbuf, bufsize);
920 break;
921 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 case 'C': {/* unicode char */
924 int *p = va_arg(*p_va, int *);
Victor Stinnere1335c72011-10-04 20:53:03 +0200925 int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +0300926 const void *data;
Victor Stinnere1335c72011-10-04 20:53:03 +0200927
928 if (!PyUnicode_Check(arg))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return converterr("a unicode character", arg, msgbuf, bufsize);
Victor Stinnere1335c72011-10-04 20:53:03 +0200930
931 if (PyUnicode_READY(arg))
932 RETURN_ERR_OCCURRED;
933
934 if (PyUnicode_GET_LENGTH(arg) != 1)
935 return converterr("a unicode character", arg, msgbuf, bufsize);
936
937 kind = PyUnicode_KIND(arg);
938 data = PyUnicode_DATA(arg);
939 *p = PyUnicode_READ(kind, data, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 break;
941 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000942
Larry Hastingsfaf91e72012-05-05 16:54:29 -0700943 case 'p': {/* boolean *p*redicate */
944 int *p = va_arg(*p_va, int *);
945 int val = PyObject_IsTrue(arg);
946 if (val > 0)
947 *p = 1;
948 else if (val == 0)
949 *p = 0;
950 else
951 RETURN_ERR_OCCURRED;
952 break;
953 }
954
Victor Stinner3dcb5ac2010-06-08 22:54:19 +0000955 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 need to be cleaned up! */
Guido van Rossum98297ee2007-11-06 21:34:58 +0000957
Serhiy Storchakab757c832014-12-05 22:25:22 +0200958 case 'y': {/* any bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 void **p = (void **)va_arg(*p_va, char **);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200960 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 Py_ssize_t count;
962 if (*format == '*') {
963 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0)
964 return converterr(buf, arg, msgbuf, bufsize);
965 format++;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -0400966 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 return converterr(
968 "(cleanup problem)",
969 arg, msgbuf, bufsize);
970 }
971 break;
972 }
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200973 count = convertbuffer(arg, (const void **)p, &buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 if (count < 0)
975 return converterr(buf, arg, msgbuf, bufsize);
Victor Stinner06e49dd2010-06-13 18:21:50 +0000976 if (*format == '#') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 FETCH_SIZE;
978 STORE_SIZE(count);
979 format++;
Victor Stinner06e49dd2010-06-13 18:21:50 +0000980 } else {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300981 if (strlen(*p) != (size_t)count) {
982 PyErr_SetString(PyExc_ValueError, "embedded null byte");
983 RETURN_ERR_OCCURRED;
984 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 }
986 break;
987 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000988
Serhiy Storchakab757c832014-12-05 22:25:22 +0200989 case 's': /* text string or bytes-like object */
990 case 'z': /* text string, bytes-like object or None */
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000991 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 if (*format == '*') {
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000993 /* "s*" or "z*" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *);
Martin v. Löwis423be952008-08-13 15:53:07 +0000995
Victor Stinner3c9e6e92010-06-24 22:31:12 +0000996 if (c == 'z' && arg == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0);
998 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200999 Py_ssize_t len;
1000 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1001 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 return converterr(CONV_UNICODE,
1003 arg, msgbuf, bufsize);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001004 PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 }
Serhiy Storchakab757c832014-12-05 22:25:22 +02001006 else { /* any bytes-like object */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001007 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 if (getbuffer(arg, p, &buf) < 0)
1009 return converterr(buf, arg, msgbuf, bufsize);
1010 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001011 if (addcleanup(p, freelist, cleanup_buffer)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 return converterr(
1013 "(cleanup problem)",
1014 arg, msgbuf, bufsize);
1015 }
1016 format++;
Serhiy Storchakab757c832014-12-05 22:25:22 +02001017 } else if (*format == '#') { /* a string or read-only bytes-like object */
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001018 /* "s#" or "z#" */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001019 const void **p = (const void **)va_arg(*p_va, const char **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 FETCH_SIZE;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001021
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001022 if (c == 'z' && arg == Py_None) {
1023 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 STORE_SIZE(0);
1025 }
1026 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001027 Py_ssize_t len;
1028 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1029 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 return converterr(CONV_UNICODE,
1031 arg, msgbuf, bufsize);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001032 *p = sarg;
1033 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 }
Serhiy Storchakab757c832014-12-05 22:25:22 +02001035 else { /* read-only bytes-like object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 /* XXX Really? */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001037 const char *buf;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 Py_ssize_t count = convertbuffer(arg, p, &buf);
1039 if (count < 0)
1040 return converterr(buf, arg, msgbuf, bufsize);
1041 STORE_SIZE(count);
1042 }
1043 format++;
1044 } else {
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001045 /* "s" or "z" */
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001046 const char **p = va_arg(*p_va, const char **);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001047 Py_ssize_t len;
1048 sarg = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +00001049
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001050 if (c == 'z' && arg == Py_None)
1051 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 else if (PyUnicode_Check(arg)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001053 sarg = PyUnicode_AsUTF8AndSize(arg, &len);
1054 if (sarg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 return converterr(CONV_UNICODE,
1056 arg, msgbuf, bufsize);
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001057 if (strlen(sarg) != (size_t)len) {
1058 PyErr_SetString(PyExc_ValueError, "embedded null character");
1059 RETURN_ERR_OCCURRED;
1060 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001061 *p = sarg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 }
1063 else
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001064 return converterr(c == 'z' ? "str or None" : "str",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 }
1067 break;
1068 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001069
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001070 case 'u': /* raw unicode buffer (Py_UNICODE *) */
1071 case 'Z': /* raw unicode buffer or None */
1072 {
Brett Cannonb94767f2011-02-22 20:15:44 +00001073 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
1074
Serhiy Storchakab757c832014-12-05 22:25:22 +02001075 if (*format == '#') {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001076 /* "u#" or "Z#" */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 FETCH_SIZE;
Travis E. Oliphantddacf962007-10-13 21:03:27 +00001078
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001079 if (c == 'Z' && arg == Py_None) {
1080 *p = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 STORE_SIZE(0);
1082 }
1083 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001084 Py_ssize_t len;
1085 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001086 if (*p == NULL)
1087 RETURN_ERR_OCCURRED;
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001088 STORE_SIZE(len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 }
Victor Stinner5216e6d2010-06-08 21:45:51 +00001090 else
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001091 return converterr(c == 'Z' ? "str or None" : "str",
1092 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 format++;
1094 } else {
Serhiy Storchakad6e53da2015-04-19 21:11:30 +03001095 /* "u" or "Z" */
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001096 if (c == 'Z' && arg == Py_None)
1097 *p = NULL;
Victor Stinner06e49dd2010-06-13 18:21:50 +00001098 else if (PyUnicode_Check(arg)) {
Victor Stinnerbeac78b2011-10-11 21:55:01 +02001099 Py_ssize_t len;
1100 *p = PyUnicode_AsUnicodeAndSize(arg, &len);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001101 if (*p == NULL)
1102 RETURN_ERR_OCCURRED;
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001103 if (wcslen(*p) != (size_t)len) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001104 PyErr_SetString(PyExc_ValueError, "embedded null character");
1105 RETURN_ERR_OCCURRED;
1106 }
Victor Stinner06e49dd2010-06-13 18:21:50 +00001107 } else
Victor Stinner3c9e6e92010-06-24 22:31:12 +00001108 return converterr(c == 'Z' ? "str or None" : "str",
1109 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 }
1111 break;
1112 }
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 case 'e': {/* encoded string */
1115 char **buffer;
1116 const char *encoding;
1117 PyObject *s;
1118 int recode_strings;
1119 Py_ssize_t size;
1120 const char *ptr;
Jeremy Hylton4819e972001-10-11 14:40:37 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 /* Get 'e' parameter: the encoding name */
1123 encoding = (const char *)va_arg(*p_va, const char *);
1124 if (encoding == NULL)
1125 encoding = PyUnicode_GetDefaultEncoding();
Martin v. Löwis423be952008-08-13 15:53:07 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 /* Get output buffer parameter:
1128 's' (recode all objects via Unicode) or
1129 't' (only recode non-string objects)
1130 */
1131 if (*format == 's')
1132 recode_strings = 1;
1133 else if (*format == 't')
1134 recode_strings = 0;
1135 else
1136 return converterr(
1137 "(unknown parser marker combination)",
1138 arg, msgbuf, bufsize);
1139 buffer = (char **)va_arg(*p_va, char **);
1140 format++;
1141 if (buffer == NULL)
1142 return converterr("(buffer is NULL)",
1143 arg, msgbuf, bufsize);
Benjamin Peterson9edd2bd2008-08-27 00:31:37 +00001144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 /* Encode object */
1146 if (!recode_strings &&
1147 (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
1148 s = arg;
1149 Py_INCREF(s);
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +02001150 if (PyBytes_Check(arg)) {
1151 size = PyBytes_GET_SIZE(s);
1152 ptr = PyBytes_AS_STRING(s);
1153 }
1154 else {
1155 size = PyByteArray_GET_SIZE(s);
1156 ptr = PyByteArray_AS_STRING(s);
1157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001159 else if (PyUnicode_Check(arg)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 /* Encode object; use default error handling */
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001161 s = PyUnicode_AsEncodedString(arg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 encoding,
1163 NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 if (s == NULL)
1165 return converterr("(encoding failed)",
1166 arg, msgbuf, bufsize);
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001167 assert(PyBytes_Check(s));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 size = PyBytes_GET_SIZE(s);
1169 ptr = PyBytes_AS_STRING(s);
1170 if (ptr == NULL)
1171 ptr = "";
1172 }
Serhiy Storchaka21a663e2016-04-13 15:37:23 +03001173 else {
1174 return converterr(
1175 recode_strings ? "str" : "str, bytes or bytearray",
1176 arg, msgbuf, bufsize);
1177 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 /* Write output; output is guaranteed to be 0-terminated */
1180 if (*format == '#') {
1181 /* Using buffer length parameter '#':
1182
1183 - if *buffer is NULL, a new buffer of the
1184 needed size is allocated and the data
1185 copied into it; *buffer is updated to point
1186 to the new buffer; the caller is
1187 responsible for PyMem_Free()ing it after
1188 usage
1189
1190 - if *buffer is not NULL, the data is
1191 copied to *buffer; *buffer_len has to be
1192 set to the size of the buffer on input;
1193 buffer overflow is signalled with an error;
1194 buffer has to provide enough room for the
1195 encoded string plus the trailing 0-byte
1196
1197 - in both cases, *buffer_len is updated to
1198 the size of the buffer /excluding/ the
1199 trailing 0-byte
1200
1201 */
Serhiy Storchaka5bc6a7c2019-10-21 11:35:07 +03001202 int *q = NULL; Py_ssize_t *q2 = NULL;
1203 if (flags & FLAG_SIZE_T) {
1204 q2 = va_arg(*p_va, Py_ssize_t*);
1205 }
1206 else {
1207 if (PyErr_WarnEx(PyExc_DeprecationWarning,
1208 "PY_SSIZE_T_CLEAN will be required for '#' formats", 1))
1209 {
1210 Py_DECREF(s);
1211 return NULL;
1212 }
1213 q = va_arg(*p_va, int*);
1214 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215
1216 format++;
1217 if (q == NULL && q2 == NULL) {
1218 Py_DECREF(s);
1219 return converterr(
1220 "(buffer_len is NULL)",
1221 arg, msgbuf, bufsize);
1222 }
1223 if (*buffer == NULL) {
1224 *buffer = PyMem_NEW(char, size + 1);
1225 if (*buffer == NULL) {
1226 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001227 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001228 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001230 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 Py_DECREF(s);
1232 return converterr(
1233 "(cleanup problem)",
1234 arg, msgbuf, bufsize);
1235 }
1236 } else {
1237 if (size + 1 > BUFFER_LEN) {
1238 Py_DECREF(s);
Serhiy Storchaka4cd63ef2016-02-08 01:22:47 +02001239 PyErr_Format(PyExc_ValueError,
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001240 "encoded string too long "
1241 "(%zd, maximum length %zd)",
1242 (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));
1243 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 }
1245 }
1246 memcpy(*buffer, ptr, size+1);
Serhiy Storchaka5bc6a7c2019-10-21 11:35:07 +03001247
1248 if (flags & FLAG_SIZE_T) {
1249 *q2 = size;
1250 }
1251 else {
1252 if (INT_MAX < size) {
1253 Py_DECREF(s);
1254 PyErr_SetString(PyExc_OverflowError,
1255 "size does not fit in an int");
1256 return converterr("", arg, msgbuf, bufsize);
1257 }
1258 *q = (int)size;
1259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 } else {
1261 /* Using a 0-terminated buffer:
1262
1263 - the encoded string has to be 0-terminated
1264 for this variant to work; if it is not, an
1265 error raised
1266
1267 - a new buffer of the needed size is
1268 allocated and the data copied into it;
1269 *buffer is updated to point to the new
1270 buffer; the caller is responsible for
1271 PyMem_Free()ing it after usage
1272
1273 */
1274 if ((Py_ssize_t)strlen(ptr) != size) {
1275 Py_DECREF(s);
1276 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001277 "encoded string without null bytes",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 arg, msgbuf, bufsize);
1279 }
1280 *buffer = PyMem_NEW(char, size + 1);
1281 if (*buffer == NULL) {
1282 Py_DECREF(s);
Victor Stinner2872e5b2010-06-06 20:38:02 +00001283 PyErr_NoMemory();
Victor Stinner6ab8e822011-01-04 11:16:49 +00001284 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001286 if (addcleanup(*buffer, freelist, cleanup_ptr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 Py_DECREF(s);
1288 return converterr("(cleanup problem)",
1289 arg, msgbuf, bufsize);
1290 }
1291 memcpy(*buffer, ptr, size+1);
1292 }
1293 Py_DECREF(s);
1294 break;
1295 }
1296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 case 'S': { /* PyBytes object */
1298 PyObject **p = va_arg(*p_va, PyObject **);
1299 if (PyBytes_Check(arg))
1300 *p = arg;
1301 else
1302 return converterr("bytes", arg, msgbuf, bufsize);
1303 break;
1304 }
1305
1306 case 'Y': { /* PyByteArray object */
1307 PyObject **p = va_arg(*p_va, PyObject **);
1308 if (PyByteArray_Check(arg))
1309 *p = arg;
1310 else
Victor Stinner5216e6d2010-06-08 21:45:51 +00001311 return converterr("bytearray", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 break;
1313 }
1314
1315 case 'U': { /* PyUnicode object */
1316 PyObject **p = va_arg(*p_va, PyObject **);
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001317 if (PyUnicode_Check(arg)) {
1318 if (PyUnicode_READY(arg) == -1)
1319 RETURN_ERR_OCCURRED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 *p = arg;
Victor Stinnera1b0c9f2012-05-29 12:30:29 +02001321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 else
1323 return converterr("str", arg, msgbuf, bufsize);
1324 break;
1325 }
1326
1327 case 'O': { /* object */
1328 PyTypeObject *type;
1329 PyObject **p;
1330 if (*format == '!') {
1331 type = va_arg(*p_va, PyTypeObject*);
1332 p = va_arg(*p_va, PyObject **);
1333 format++;
Victor Stinnera102ed72020-02-07 02:24:48 +01001334 if (PyType_IsSubtype(Py_TYPE(arg), type))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 *p = arg;
1336 else
1337 return converterr(type->tp_name, arg, msgbuf, bufsize);
1338
1339 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 else if (*format == '&') {
1341 typedef int (*converter)(PyObject *, void *);
1342 converter convert = va_arg(*p_va, converter);
1343 void *addr = va_arg(*p_va, void *);
1344 int res;
1345 format++;
1346 if (! (res = (*convert)(arg, addr)))
1347 return converterr("(unspecified)",
1348 arg, msgbuf, bufsize);
1349 if (res == Py_CLEANUP_SUPPORTED &&
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001350 addcleanup(addr, freelist, convert) == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 return converterr("(cleanup problem)",
1352 arg, msgbuf, bufsize);
1353 }
1354 else {
1355 p = va_arg(*p_va, PyObject **);
1356 *p = arg;
1357 }
1358 break;
1359 }
1360
1361
Victor Stinner25e8ec42010-06-25 00:02:38 +00001362 case 'w': { /* "w*": memory buffer, read-write access */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 void **p = va_arg(*p_va, void **);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364
Victor Stinner25e8ec42010-06-25 00:02:38 +00001365 if (*format != '*')
1366 return converterr(
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001367 "(invalid use of 'w' format character)",
Victor Stinner25e8ec42010-06-25 00:02:38 +00001368 arg, msgbuf, bufsize);
1369 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370
Victor Stinner25e8ec42010-06-25 00:02:38 +00001371 /* Caller is interested in Py_buffer, and the object
1372 supports it directly. */
1373 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) {
1374 PyErr_Clear();
R David Murray861470c2014-10-05 11:47:01 -04001375 return converterr("read-write bytes-like object",
1376 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 }
Victor Stinner8182b712010-07-28 00:40:58 +00001378 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) {
1379 PyBuffer_Release((Py_buffer*)p);
1380 return converterr("contiguous buffer", arg, msgbuf, bufsize);
1381 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001382 if (addcleanup(p, freelist, cleanup_buffer)) {
Victor Stinner25e8ec42010-06-25 00:02:38 +00001383 return converterr(
1384 "(cleanup problem)",
1385 arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 break;
1388 }
1389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 default:
Serhiy Storchakac4b813d2016-02-08 01:06:11 +02001391 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392
1393 }
1394
1395 *p_format = format;
1396 return NULL;
Victor Stinner6ab8e822011-01-04 11:16:49 +00001397
1398#undef FETCH_SIZE
1399#undef STORE_SIZE
1400#undef BUFFER_LEN
1401#undef RETURN_ERR_OCCURRED
Guido van Rossumfe3f1a21994-09-29 09:42:55 +00001402}
Guido van Rossumaa354651996-08-19 19:32:04 +00001403
Martin v. Löwis18e16552006-02-15 17:27:45 +00001404static Py_ssize_t
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001405convertbuffer(PyObject *arg, const void **p, const char **errmsg)
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001406{
Victor Stinner5cb62392010-06-06 20:27:51 +00001407 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 Py_ssize_t count;
1409 Py_buffer view;
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 *errmsg = NULL;
1412 *p = NULL;
Victor Stinner8182b712010-07-28 00:40:58 +00001413 if (pb != NULL && pb->bf_releasebuffer != NULL) {
R David Murray861470c2014-10-05 11:47:01 -04001414 *errmsg = "read-only bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 return -1;
1416 }
Travis E. Oliphantb99f7622007-08-18 11:21:56 +00001417
Victor Stinner8182b712010-07-28 00:40:58 +00001418 if (getbuffer(arg, &view, errmsg) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 count = view.len;
1421 *p = view.buf;
1422 PyBuffer_Release(&view);
1423 return count;
Jeremy Hylton1cb7aa32001-05-29 17:37:05 +00001424}
Guido van Rossumaa354651996-08-19 19:32:04 +00001425
Martin v. Löwis423be952008-08-13 15:53:07 +00001426static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001427getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg)
Martin v. Löwis423be952008-08-13 15:53:07 +00001428{
Victor Stinner8182b712010-07-28 00:40:58 +00001429 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) {
R David Murray861470c2014-10-05 11:47:01 -04001430 *errmsg = "bytes-like object";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 return -1;
1432 }
Victor Stinner5cb62392010-06-06 20:27:51 +00001433 if (!PyBuffer_IsContiguous(view, 'C')) {
Victor Stinner21e09482010-06-24 22:57:10 +00001434 PyBuffer_Release(view);
Victor Stinner5cb62392010-06-06 20:27:51 +00001435 *errmsg = "contiguous buffer";
1436 return -1;
1437 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 return 0;
Martin v. Löwis423be952008-08-13 15:53:07 +00001439}
1440
Guido van Rossumaa354651996-08-19 19:32:04 +00001441/* Support for keyword arguments donated by
1442 Geoff Philbrick <philbric@delphi.hks.com> */
1443
Tim Petersf8cd3e82001-10-27 04:26:57 +00001444/* Return false (0) for error, else true. */
Fred Drake563dfc22001-10-23 14:41:08 +00001445int
1446PyArg_ParseTupleAndKeywords(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 PyObject *keywords,
1448 const char *format,
1449 char **kwlist, ...)
Guido van Rossumaa354651996-08-19 19:32:04 +00001450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 int retval;
1452 va_list va;
Tim Peters45772cd2001-10-27 03:58:40 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 if ((args == NULL || !PyTuple_Check(args)) ||
1455 (keywords != NULL && !PyDict_Check(keywords)) ||
1456 format == NULL ||
1457 kwlist == NULL)
1458 {
1459 PyErr_BadInternalCall();
1460 return 0;
1461 }
Tim Peters45772cd2001-10-27 03:58:40 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 va_start(va, kwlist);
1464 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0);
1465 va_end(va);
1466 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001467}
1468
Vinay Sajip0b60f642019-10-15 08:26:12 +01001469PyAPI_FUNC(int)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001470_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 PyObject *keywords,
1472 const char *format,
1473 char **kwlist, ...)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 int retval;
1476 va_list va;
Martin v. Löwis18e16552006-02-15 17:27:45 +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 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 va_start(va, kwlist);
1488 retval = vgetargskeywords(args, keywords, format,
1489 kwlist, &va, FLAG_SIZE_T);
1490 va_end(va);
1491 return retval;
Guido van Rossumaa354651996-08-19 19:32:04 +00001492}
1493
1494
Brett Cannon711e7d92004-07-10 22:20:32 +00001495int
1496PyArg_VaParseTupleAndKeywords(PyObject *args,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00001497 PyObject *keywords,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001498 const char *format,
Martin v. Löwis15e62742006-02-27 16:46:16 +00001499 char **kwlist, va_list va)
Brett Cannon711e7d92004-07-10 22:20:32 +00001500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 int retval;
1502 va_list lva;
Brett Cannon711e7d92004-07-10 22:20:32 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 if ((args == NULL || !PyTuple_Check(args)) ||
1505 (keywords != NULL && !PyDict_Check(keywords)) ||
1506 format == NULL ||
1507 kwlist == NULL)
1508 {
1509 PyErr_BadInternalCall();
1510 return 0;
1511 }
Brett Cannon711e7d92004-07-10 22:20:32 +00001512
Benjamin Peterson0c212142016-09-20 20:39:33 -07001513 va_copy(lva, va);
Brett Cannon711e7d92004-07-10 22:20:32 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001516 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 return retval;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001518}
1519
Vinay Sajip0b60f642019-10-15 08:26:12 +01001520PyAPI_FUNC(int)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001521_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 PyObject *keywords,
1523 const char *format,
1524 char **kwlist, va_list va)
Martin v. Löwis18e16552006-02-15 17:27:45 +00001525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 int retval;
1527 va_list lva;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 if ((args == NULL || !PyTuple_Check(args)) ||
1530 (keywords != NULL && !PyDict_Check(keywords)) ||
1531 format == NULL ||
1532 kwlist == NULL)
1533 {
1534 PyErr_BadInternalCall();
1535 return 0;
1536 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00001537
Benjamin Peterson0c212142016-09-20 20:39:33 -07001538 va_copy(lva, va);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 retval = vgetargskeywords(args, keywords, format,
1541 kwlist, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001542 va_end(lva);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 return retval;
Brett Cannon711e7d92004-07-10 22:20:32 +00001544}
1545
Vinay Sajip0b60f642019-10-15 08:26:12 +01001546PyAPI_FUNC(int)
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001547_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1548 struct _PyArg_Parser *parser, ...)
1549{
1550 int retval;
1551 va_list va;
1552
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001553 va_start(va, parser);
1554 retval = vgetargskeywordsfast(args, keywords, parser, &va, 0);
1555 va_end(va);
1556 return retval;
1557}
1558
Vinay Sajip0b60f642019-10-15 08:26:12 +01001559PyAPI_FUNC(int)
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001560_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1561 struct _PyArg_Parser *parser, ...)
1562{
1563 int retval;
1564 va_list va;
1565
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001566 va_start(va, parser);
1567 retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T);
1568 va_end(va);
1569 return retval;
1570}
1571
Vinay Sajip0b60f642019-10-15 08:26:12 +01001572PyAPI_FUNC(int)
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001573_PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001574 struct _PyArg_Parser *parser, ...)
1575{
1576 int retval;
1577 va_list va;
1578
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001579 va_start(va, parser);
1580 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0);
1581 va_end(va);
1582 return retval;
1583}
1584
Vinay Sajip0b60f642019-10-15 08:26:12 +01001585PyAPI_FUNC(int)
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001586_PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001587 struct _PyArg_Parser *parser, ...)
1588{
1589 int retval;
1590 va_list va;
1591
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07001592 va_start(va, parser);
1593 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T);
1594 va_end(va);
1595 return retval;
1596}
1597
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001598
Vinay Sajip0b60f642019-10-15 08:26:12 +01001599PyAPI_FUNC(int)
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001600_PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords,
1601 struct _PyArg_Parser *parser, va_list va)
1602{
1603 int retval;
1604 va_list lva;
1605
Benjamin Peterson0c212142016-09-20 20:39:33 -07001606 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001607
1608 retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001609 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001610 return retval;
1611}
1612
Vinay Sajip0b60f642019-10-15 08:26:12 +01001613PyAPI_FUNC(int)
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001614_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords,
1615 struct _PyArg_Parser *parser, va_list va)
1616{
1617 int retval;
1618 va_list lva;
1619
Benjamin Peterson0c212142016-09-20 20:39:33 -07001620 va_copy(lva, va);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001621
1622 retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T);
Christian Heimes2f2fee12016-09-21 11:37:27 +02001623 va_end(lva);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001624 return retval;
1625}
1626
1627int
Benjamin Petersonfb886362010-04-24 18:21:17 +00001628PyArg_ValidateKeywordArguments(PyObject *kwargs)
1629{
Benjamin Petersonf6096542010-11-17 22:33:12 +00001630 if (!PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 PyErr_BadInternalCall();
1632 return 0;
1633 }
1634 if (!_PyDict_HasOnlyStringKeys(kwargs)) {
1635 PyErr_SetString(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02001636 "keywords must be strings");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 return 0;
1638 }
1639 return 1;
Benjamin Petersonfb886362010-04-24 18:21:17 +00001640}
1641
Christian Heimes380f7f22008-02-28 11:19:05 +00001642#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':')
Brett Cannon711e7d92004-07-10 22:20:32 +00001643
Guido van Rossumaa354651996-08-19 19:32:04 +00001644static int
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001645vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 char **kwlist, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00001647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 char msgbuf[512];
1649 int levels[32];
Serhiy Storchaka17414412017-01-17 10:07:25 +02001650 const char *fname, *msg, *custom_msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 int min = INT_MAX;
Larry Hastings83a9f482012-03-20 20:06:16 +00001652 int max = INT_MAX;
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001653 int i, pos, len;
1654 int skip = 0;
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001655 Py_ssize_t nargs, nkwargs;
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001656 PyObject *current_arg;
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001657 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
Benjamin Peterson40be9e52014-02-11 10:09:27 -05001658 freelist_t freelist;
1659
1660 freelist.entries = static_entries;
1661 freelist.first_available = 0;
1662 freelist.entries_malloced = 0;
Tim Petersf4331c12001-10-27 00:17:34 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 assert(args != NULL && PyTuple_Check(args));
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001665 assert(kwargs == NULL || PyDict_Check(kwargs));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 assert(format != NULL);
1667 assert(kwlist != NULL);
1668 assert(p_va != NULL);
Tim Peters45772cd2001-10-27 03:58:40 +00001669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 /* grab the function name or custom error msg first (mutually exclusive) */
1671 fname = strchr(format, ':');
1672 if (fname) {
1673 fname++;
1674 custom_msg = NULL;
1675 }
1676 else {
1677 custom_msg = strchr(format,';');
1678 if (custom_msg)
1679 custom_msg++;
1680 }
Christian Heimes380f7f22008-02-28 11:19:05 +00001681
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001682 /* scan kwlist and count the number of positional-only parameters */
1683 for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) {
1684 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 /* scan kwlist and get greatest possible nbr of args */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001686 for (len = pos; kwlist[len]; len++) {
1687 if (!*kwlist[len]) {
1688 PyErr_SetString(PyExc_SystemError,
1689 "Empty keyword parameter name");
1690 return cleanreturn(0, &freelist);
1691 }
1692 }
Tim Petersf8cd3e82001-10-27 04:26:57 +00001693
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001694 if (len > STATIC_FREELIST_ENTRIES) {
1695 freelist.entries = PyMem_NEW(freelistentry_t, len);
1696 if (freelist.entries == NULL) {
1697 PyErr_NoMemory();
1698 return 0;
1699 }
1700 freelist.entries_malloced = 1;
Benjamin Peterson7ed67272012-03-16 12:21:02 -05001701 }
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 nargs = PyTuple_GET_SIZE(args);
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001704 nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs);
1705 if (nargs + nkwargs > len) {
Oren Milmanbf9075a2017-08-23 21:16:48 +03001706 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
1707 messages in some special cases (see bpo-31229). */
Victor Stinner6ced7c42011-03-21 18:15:42 +01001708 PyErr_Format(PyExc_TypeError,
Oren Milmanbf9075a2017-08-23 21:16:48 +03001709 "%.200s%s takes at most %d %sargument%s (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 (fname == NULL) ? "function" : fname,
1711 (fname == NULL) ? "" : "()",
1712 len,
Oren Milmanbf9075a2017-08-23 21:16:48 +03001713 (nargs == 0) ? "keyword " : "",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 (len == 1) ? "" : "s",
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001715 nargs + nkwargs);
Benjamin Peterson01feaec2012-03-16 13:25:58 -05001716 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 }
Tim Petersc2f01122001-10-27 07:25:06 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 /* convert tuple args and keyword args in same loop, using kwlist to drive process */
1720 for (i = 0; i < len; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 if (*format == '|') {
Larry Hastings83a9f482012-03-20 20:06:16 +00001722 if (min != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001723 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001724 "Invalid format string (| specified twice)");
1725 return cleanreturn(0, &freelist);
1726 }
1727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 min = i;
1729 format++;
Larry Hastings83a9f482012-03-20 20:06:16 +00001730
1731 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001732 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001733 "Invalid format string ($ before |)");
1734 return cleanreturn(0, &freelist);
1735 }
1736 }
1737 if (*format == '$') {
1738 if (max != INT_MAX) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001739 PyErr_SetString(PyExc_SystemError,
Larry Hastings83a9f482012-03-20 20:06:16 +00001740 "Invalid format string ($ specified twice)");
1741 return cleanreturn(0, &freelist);
1742 }
1743
1744 max = i;
1745 format++;
1746
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001747 if (max < pos) {
1748 PyErr_SetString(PyExc_SystemError,
1749 "Empty parameter name after $");
1750 return cleanreturn(0, &freelist);
1751 }
1752 if (skip) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001753 /* Now we know the minimal and the maximal numbers of
1754 * positional arguments and can raise an exception with
1755 * informative message (see below). */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001756 break;
1757 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001758 if (max < nargs) {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03001759 if (max == 0) {
1760 PyErr_Format(PyExc_TypeError,
1761 "%.200s%s takes no positional arguments",
1762 (fname == NULL) ? "function" : fname,
1763 (fname == NULL) ? "" : "()");
1764 }
1765 else {
1766 PyErr_Format(PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05301767 "%.200s%s takes %s %d positional argument%s"
Serhiy Storchakad53fe5f2019-03-13 22:59:55 +02001768 " (%zd given)",
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03001769 (fname == NULL) ? "function" : fname,
1770 (fname == NULL) ? "" : "()",
1771 (min != INT_MAX) ? "at most" : "exactly",
Xtreak63262782018-12-21 20:15:13 +05301772 max,
1773 max == 1 ? "" : "s",
1774 nargs);
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03001775 }
Larry Hastings83a9f482012-03-20 20:06:16 +00001776 return cleanreturn(0, &freelist);
1777 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 }
1779 if (IS_END_OF_FORMAT(*format)) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001780 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 "More keyword list entries (%d) than "
1782 "format specifiers (%d)", len, i);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001783 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001785 if (!skip) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02001786 if (i < nargs) {
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001787 current_arg = PyTuple_GET_ITEM(args, i);
Serhiy Storchaka17414412017-01-17 10:07:25 +02001788 }
1789 else if (nkwargs && i >= pos) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001790 current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
1791 if (current_arg) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02001792 --nkwargs;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001793 }
1794 else if (PyErr_Occurred()) {
1795 return cleanreturn(0, &freelist);
1796 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02001797 }
1798 else {
1799 current_arg = NULL;
1800 }
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001801
1802 if (current_arg) {
1803 msg = convertitem(current_arg, &format, p_va, flags,
1804 levels, msgbuf, sizeof(msgbuf), &freelist);
1805 if (msg) {
1806 seterror(i+1, msg, levels, fname, custom_msg);
1807 return cleanreturn(0, &freelist);
1808 }
1809 continue;
1810 }
1811
1812 if (i < min) {
1813 if (i < pos) {
1814 assert (min == INT_MAX);
1815 assert (max == INT_MAX);
1816 skip = 1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001817 /* At that moment we still don't know the minimal and
1818 * the maximal numbers of positional arguments. Raising
1819 * an exception is deferred until we encounter | and $
1820 * or the end of the format. */
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001821 }
1822 else {
Michael Seifert64c8f702017-04-09 09:47:12 +02001823 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
1824 "argument '%s' (pos %d)",
1825 (fname == NULL) ? "function" : fname,
1826 (fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02001827 kwlist[i], i+1);
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001828 return cleanreturn(0, &freelist);
1829 }
1830 }
1831 /* current code reports success when all required args
1832 * fulfilled and no keyword args left, with no further
1833 * validation. XXX Maybe skip this in debug build ?
1834 */
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001835 if (!nkwargs && !skip) {
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001836 return cleanreturn(1, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 }
1838 }
Guido van Rossumaa354651996-08-19 19:32:04 +00001839
Leo Ariasc3d95082018-02-03 18:36:10 -06001840 /* We are into optional args, skip through to any remaining
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 * keyword args */
1842 msg = skipitem(&format, p_va, flags);
1843 if (msg) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001844 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001846 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 }
1848 }
Tim Petersb054be42001-10-27 05:07:41 +00001849
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001850 if (skip) {
1851 PyErr_Format(PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05301852 "%.200s%s takes %s %d positional argument%s"
Serhiy Storchakad53fe5f2019-03-13 22:59:55 +02001853 " (%zd given)",
Michael Seifert64c8f702017-04-09 09:47:12 +02001854 (fname == NULL) ? "function" : fname,
1855 (fname == NULL) ? "" : "()",
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001856 (Py_MIN(pos, min) < i) ? "at least" : "exactly",
Xtreak63262782018-12-21 20:15:13 +05301857 Py_MIN(pos, min),
1858 Py_MIN(pos, min) == 1 ? "" : "s",
1859 nargs);
Serhiy Storchakaf41b82f2016-06-09 16:30:29 +03001860 return cleanreturn(0, &freelist);
1861 }
1862
Larry Hastings83a9f482012-03-20 20:06:16 +00001863 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
Serhiy Storchakaa9725f82016-02-11 12:41:40 +02001864 PyErr_Format(PyExc_SystemError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 "more argument specifiers than keyword list entries "
1866 "(remaining format:'%s')", format);
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001867 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 }
Tim Petersc2f01122001-10-27 07:25:06 +00001869
Victor Stinnerc0083fc2017-01-17 00:37:42 +01001870 if (nkwargs > 0) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02001871 PyObject *key;
1872 Py_ssize_t j;
1873 /* make sure there are no arguments given by name and position */
1874 for (i = pos; i < nargs; i++) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001875 current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]);
Serhiy Storchaka17414412017-01-17 10:07:25 +02001876 if (current_arg) {
1877 /* arg present in tuple and in dict */
1878 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02001879 "argument for %.200s%s given by name ('%s') "
Serhiy Storchaka17414412017-01-17 10:07:25 +02001880 "and position (%d)",
Michael Seifert64c8f702017-04-09 09:47:12 +02001881 (fname == NULL) ? "function" : fname,
1882 (fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02001883 kwlist[i], i+1);
1884 return cleanreturn(0, &freelist);
1885 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001886 else if (PyErr_Occurred()) {
1887 return cleanreturn(0, &freelist);
1888 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02001889 }
1890 /* make sure there are no extraneous keyword arguments */
1891 j = 0;
1892 while (PyDict_Next(kwargs, &j, &key, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 int match = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 if (!PyUnicode_Check(key)) {
1895 PyErr_SetString(PyExc_TypeError,
1896 "keywords must be strings");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001897 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02001899 for (i = pos; i < len; i++) {
1900 if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) {
Antoine Pitrou7056cb22013-02-17 01:04:57 +01001901 match = 1;
1902 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 }
1904 }
1905 if (!match) {
1906 PyErr_Format(PyExc_TypeError,
Victor Stinner93b55132010-05-19 00:54:06 +00001907 "'%U' is an invalid keyword "
Michael Seifert64c8f702017-04-09 09:47:12 +02001908 "argument for %.200s%s",
1909 key,
1910 (fname == NULL) ? "this function" : fname,
1911 (fname == NULL) ? "" : "()");
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001912 return cleanreturn(0, &freelist);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 }
1914 }
1915 }
1916
Jean-Paul Calderonec961b4a2012-03-16 08:51:42 -04001917 return cleanreturn(1, &freelist);
Guido van Rossumaa354651996-08-19 19:32:04 +00001918}
1919
1920
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001921/* List of static parsers. */
1922static struct _PyArg_Parser *static_arg_parsers = NULL;
1923
1924static int
1925parser_init(struct _PyArg_Parser *parser)
1926{
1927 const char * const *keywords;
1928 const char *format, *msg;
1929 int i, len, min, max, nkw;
1930 PyObject *kwtuple;
1931
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001932 assert(parser->keywords != NULL);
1933 if (parser->kwtuple != NULL) {
1934 return 1;
1935 }
1936
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001937 keywords = parser->keywords;
1938 /* scan keywords and count the number of positional-only parameters */
1939 for (i = 0; keywords[i] && !*keywords[i]; i++) {
1940 }
1941 parser->pos = i;
1942 /* scan keywords and get greatest possible nbr of args */
1943 for (; keywords[i]; i++) {
1944 if (!*keywords[i]) {
1945 PyErr_SetString(PyExc_SystemError,
1946 "Empty keyword parameter name");
1947 return 0;
1948 }
1949 }
1950 len = i;
1951
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001952 format = parser->format;
Serhiy Storchaka31913912019-03-14 10:32:22 +02001953 if (format) {
1954 /* grab the function name or custom error msg first (mutually exclusive) */
1955 parser->fname = strchr(parser->format, ':');
1956 if (parser->fname) {
1957 parser->fname++;
1958 parser->custom_msg = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001959 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001960 else {
1961 parser->custom_msg = strchr(parser->format,';');
1962 if (parser->custom_msg)
1963 parser->custom_msg++;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03001964 }
Serhiy Storchaka31913912019-03-14 10:32:22 +02001965
1966 min = max = INT_MAX;
1967 for (i = 0; i < len; i++) {
1968 if (*format == '|') {
1969 if (min != INT_MAX) {
1970 PyErr_SetString(PyExc_SystemError,
1971 "Invalid format string (| specified twice)");
1972 return 0;
1973 }
1974 if (max != INT_MAX) {
1975 PyErr_SetString(PyExc_SystemError,
1976 "Invalid format string ($ before |)");
1977 return 0;
1978 }
1979 min = i;
1980 format++;
1981 }
1982 if (*format == '$') {
1983 if (max != INT_MAX) {
1984 PyErr_SetString(PyExc_SystemError,
1985 "Invalid format string ($ specified twice)");
1986 return 0;
1987 }
1988 if (i < parser->pos) {
1989 PyErr_SetString(PyExc_SystemError,
1990 "Empty parameter name after $");
1991 return 0;
1992 }
1993 max = i;
1994 format++;
1995 }
1996 if (IS_END_OF_FORMAT(*format)) {
1997 PyErr_Format(PyExc_SystemError,
1998 "More keyword list entries (%d) than "
1999 "format specifiers (%d)", len, i);
2000 return 0;
2001 }
2002
2003 msg = skipitem(&format, NULL, 0);
2004 if (msg) {
2005 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg,
2006 format);
2007 return 0;
2008 }
2009 }
2010 parser->min = Py_MIN(min, len);
2011 parser->max = Py_MIN(max, len);
2012
2013 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002014 PyErr_Format(PyExc_SystemError,
Serhiy Storchaka31913912019-03-14 10:32:22 +02002015 "more argument specifiers than keyword list entries "
2016 "(remaining format:'%s')", format);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002017 return 0;
2018 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002019 }
2020
2021 nkw = len - parser->pos;
2022 kwtuple = PyTuple_New(nkw);
2023 if (kwtuple == NULL) {
2024 return 0;
2025 }
2026 keywords = parser->keywords + parser->pos;
2027 for (i = 0; i < nkw; i++) {
2028 PyObject *str = PyUnicode_FromString(keywords[i]);
2029 if (str == NULL) {
2030 Py_DECREF(kwtuple);
2031 return 0;
2032 }
2033 PyUnicode_InternInPlace(&str);
2034 PyTuple_SET_ITEM(kwtuple, i, str);
2035 }
2036 parser->kwtuple = kwtuple;
2037
2038 assert(parser->next == NULL);
2039 parser->next = static_arg_parsers;
2040 static_arg_parsers = parser;
2041 return 1;
2042}
2043
2044static void
2045parser_clear(struct _PyArg_Parser *parser)
2046{
2047 Py_CLEAR(parser->kwtuple);
2048}
2049
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002050static PyObject*
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002051find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key)
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002052{
2053 Py_ssize_t i, nkwargs;
2054
2055 nkwargs = PyTuple_GET_SIZE(kwnames);
Sebastian Berg75bb07e2019-12-18 00:51:22 -06002056 for (i = 0; i < nkwargs; i++) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002057 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
2058
Sebastian Berg75bb07e2019-12-18 00:51:22 -06002059 /* kwname == key will normally find a match in since keyword keys
2060 should be interned strings; if not retry below in a new loop. */
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002061 if (kwname == key) {
2062 return kwstack[i];
2063 }
Sebastian Berg75bb07e2019-12-18 00:51:22 -06002064 }
2065
2066 for (i = 0; i < nkwargs; i++) {
2067 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i);
Jeroen Demeyer05677862019-08-16 12:41:27 +02002068 assert(PyUnicode_Check(kwname));
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002069 if (_PyUnicode_EQ(kwname, key)) {
2070 return kwstack[i];
2071 }
2072 }
2073 return NULL;
2074}
2075
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002076static int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002077vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs,
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002078 PyObject *kwargs, PyObject *kwnames,
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002079 struct _PyArg_Parser *parser,
2080 va_list *p_va, int flags)
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002081{
2082 PyObject *kwtuple;
2083 char msgbuf[512];
2084 int levels[32];
2085 const char *format;
2086 const char *msg;
2087 PyObject *keyword;
2088 int i, pos, len;
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002089 Py_ssize_t nkwargs;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002090 PyObject *current_arg;
2091 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES];
2092 freelist_t freelist;
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002093 PyObject *const *kwstack = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002094
2095 freelist.entries = static_entries;
2096 freelist.first_available = 0;
2097 freelist.entries_malloced = 0;
2098
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002099 assert(kwargs == NULL || PyDict_Check(kwargs));
Serhiy Storchaka17414412017-01-17 10:07:25 +02002100 assert(kwargs == NULL || kwnames == NULL);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002101 assert(p_va != NULL);
2102
Victor Stinner80ab22f2017-01-16 23:16:47 +01002103 if (parser == NULL) {
2104 PyErr_BadInternalCall();
2105 return 0;
2106 }
2107
2108 if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2109 PyErr_BadInternalCall();
2110 return 0;
2111 }
2112
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002113 if (!parser_init(parser)) {
2114 return 0;
2115 }
2116
2117 kwtuple = parser->kwtuple;
2118 pos = parser->pos;
Segev Finere7bc7aa2017-07-26 18:58:25 +03002119 len = pos + (int)PyTuple_GET_SIZE(kwtuple);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002120
2121 if (len > STATIC_FREELIST_ENTRIES) {
2122 freelist.entries = PyMem_NEW(freelistentry_t, len);
2123 if (freelist.entries == NULL) {
2124 PyErr_NoMemory();
2125 return 0;
2126 }
2127 freelist.entries_malloced = 1;
2128 }
2129
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002130 if (kwargs != NULL) {
2131 nkwargs = PyDict_GET_SIZE(kwargs);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002132 }
2133 else if (kwnames != NULL) {
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002134 nkwargs = PyTuple_GET_SIZE(kwnames);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002135 kwstack = args + nargs;
2136 }
2137 else {
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002138 nkwargs = 0;
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002139 }
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002140 if (nargs + nkwargs > len) {
Oren Milmanbf9075a2017-08-23 21:16:48 +03002141 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2142 messages in some special cases (see bpo-31229). */
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002143 PyErr_Format(PyExc_TypeError,
Oren Milmanbf9075a2017-08-23 21:16:48 +03002144 "%.200s%s takes at most %d %sargument%s (%zd given)",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002145 (parser->fname == NULL) ? "function" : parser->fname,
2146 (parser->fname == NULL) ? "" : "()",
2147 len,
Oren Milmanbf9075a2017-08-23 21:16:48 +03002148 (nargs == 0) ? "keyword " : "",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002149 (len == 1) ? "" : "s",
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002150 nargs + nkwargs);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002151 return cleanreturn(0, &freelist);
2152 }
2153 if (parser->max < nargs) {
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002154 if (parser->max == 0) {
2155 PyErr_Format(PyExc_TypeError,
Serhiy Storchakaf9f1cca2017-06-09 19:27:06 +03002156 "%.200s%s takes no positional arguments",
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002157 (parser->fname == NULL) ? "function" : parser->fname,
2158 (parser->fname == NULL) ? "" : "()");
2159 }
2160 else {
2161 PyErr_Format(PyExc_TypeError,
Serhiy Storchaka2c0d3f42019-03-14 10:06:05 +02002162 "%.200s%s takes %s %d positional argument%s (%zd given)",
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002163 (parser->fname == NULL) ? "function" : parser->fname,
2164 (parser->fname == NULL) ? "" : "()",
Serhiy Storchakaf2f55e72019-03-13 23:03:22 +02002165 (parser->min < parser->max) ? "at most" : "exactly",
Xtreak63262782018-12-21 20:15:13 +05302166 parser->max,
2167 parser->max == 1 ? "" : "s",
2168 nargs);
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002169 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002170 return cleanreturn(0, &freelist);
2171 }
2172
2173 format = parser->format;
2174 /* convert tuple args and keyword args in same loop, using kwtuple to drive process */
2175 for (i = 0; i < len; i++) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002176 if (*format == '|') {
2177 format++;
2178 }
2179 if (*format == '$') {
2180 format++;
2181 }
2182 assert(!IS_END_OF_FORMAT(*format));
2183
Serhiy Storchaka17414412017-01-17 10:07:25 +02002184 if (i < nargs) {
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002185 current_arg = args[i];
Victor Stinnera9efb2f2016-09-09 17:40:22 -07002186 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002187 else if (nkwargs && i >= pos) {
2188 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002189 if (kwargs != NULL) {
2190 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2191 if (!current_arg && PyErr_Occurred()) {
2192 return cleanreturn(0, &freelist);
2193 }
2194 }
2195 else {
2196 current_arg = find_keyword(kwnames, kwstack, keyword);
2197 }
2198 if (current_arg) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002199 --nkwargs;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002200 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002201 }
2202 else {
2203 current_arg = NULL;
2204 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002205
2206 if (current_arg) {
2207 msg = convertitem(current_arg, &format, p_va, flags,
2208 levels, msgbuf, sizeof(msgbuf), &freelist);
2209 if (msg) {
2210 seterror(i+1, msg, levels, parser->fname, parser->custom_msg);
2211 return cleanreturn(0, &freelist);
2212 }
2213 continue;
2214 }
2215
2216 if (i < parser->min) {
2217 /* Less arguments than required */
2218 if (i < pos) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002219 Py_ssize_t min = Py_MIN(pos, parser->min);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002220 PyErr_Format(PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05302221 "%.200s%s takes %s %d positional argument%s"
Serhiy Storchakad53fe5f2019-03-13 22:59:55 +02002222 " (%zd given)",
Michael Seifert64c8f702017-04-09 09:47:12 +02002223 (parser->fname == NULL) ? "function" : parser->fname,
2224 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02002225 min < parser->max ? "at least" : "exactly",
Xtreak63262782018-12-21 20:15:13 +05302226 min,
2227 min == 1 ? "" : "s",
2228 nargs);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002229 }
2230 else {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002231 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
Michael Seifert64c8f702017-04-09 09:47:12 +02002232 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
2233 "argument '%U' (pos %d)",
2234 (parser->fname == NULL) ? "function" : parser->fname,
2235 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002236 keyword, i+1);
2237 }
2238 return cleanreturn(0, &freelist);
2239 }
2240 /* current code reports success when all required args
2241 * fulfilled and no keyword args left, with no further
2242 * validation. XXX Maybe skip this in debug build ?
2243 */
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002244 if (!nkwargs) {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002245 return cleanreturn(1, &freelist);
2246 }
2247
Leo Ariasc3d95082018-02-03 18:36:10 -06002248 /* We are into optional args, skip through to any remaining
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002249 * keyword args */
2250 msg = skipitem(&format, p_va, flags);
2251 assert(msg == NULL);
2252 }
2253
2254 assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$'));
2255
Victor Stinnerc0083fc2017-01-17 00:37:42 +01002256 if (nkwargs > 0) {
Serhiy Storchaka17414412017-01-17 10:07:25 +02002257 Py_ssize_t j;
2258 /* make sure there are no arguments given by name and position */
2259 for (i = pos; i < nargs; i++) {
2260 keyword = PyTuple_GET_ITEM(kwtuple, i - pos);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002261 if (kwargs != NULL) {
2262 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2263 if (!current_arg && PyErr_Occurred()) {
2264 return cleanreturn(0, &freelist);
2265 }
2266 }
2267 else {
2268 current_arg = find_keyword(kwnames, kwstack, keyword);
2269 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002270 if (current_arg) {
2271 /* arg present in tuple and in dict */
2272 PyErr_Format(PyExc_TypeError,
Michael Seifert64c8f702017-04-09 09:47:12 +02002273 "argument for %.200s%s given by name ('%U') "
Serhiy Storchaka17414412017-01-17 10:07:25 +02002274 "and position (%d)",
Michael Seifert64c8f702017-04-09 09:47:12 +02002275 (parser->fname == NULL) ? "function" : parser->fname,
2276 (parser->fname == NULL) ? "" : "()",
Serhiy Storchaka17414412017-01-17 10:07:25 +02002277 keyword, i+1);
2278 return cleanreturn(0, &freelist);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002279 }
2280 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002281 /* make sure there are no extraneous keyword arguments */
2282 j = 0;
2283 while (1) {
2284 int match;
2285 if (kwargs != NULL) {
2286 if (!PyDict_Next(kwargs, &j, &keyword, NULL))
2287 break;
2288 }
2289 else {
2290 if (j >= PyTuple_GET_SIZE(kwnames))
2291 break;
2292 keyword = PyTuple_GET_ITEM(kwnames, j);
2293 j++;
2294 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002295
Serhiy Storchaka17414412017-01-17 10:07:25 +02002296 match = PySequence_Contains(kwtuple, keyword);
2297 if (match <= 0) {
2298 if (!match) {
2299 PyErr_Format(PyExc_TypeError,
Jeroen Demeyer05677862019-08-16 12:41:27 +02002300 "'%S' is an invalid keyword "
Michael Seifert64c8f702017-04-09 09:47:12 +02002301 "argument for %.200s%s",
2302 keyword,
2303 (parser->fname == NULL) ? "this function" : parser->fname,
2304 (parser->fname == NULL) ? "" : "()");
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002305 }
Serhiy Storchaka17414412017-01-17 10:07:25 +02002306 return cleanreturn(0, &freelist);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002307 }
2308 }
2309 }
2310
2311 return cleanreturn(1, &freelist);
2312}
2313
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002314static int
2315vgetargskeywordsfast(PyObject *args, PyObject *keywords,
2316 struct _PyArg_Parser *parser, va_list *p_va, int flags)
2317{
2318 PyObject **stack;
2319 Py_ssize_t nargs;
2320
Victor Stinner80ab22f2017-01-16 23:16:47 +01002321 if (args == NULL
2322 || !PyTuple_Check(args)
2323 || (keywords != NULL && !PyDict_Check(keywords)))
2324 {
2325 PyErr_BadInternalCall();
2326 return 0;
2327 }
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002328
Victor Stinnerd17a6932018-11-09 16:56:48 +01002329 stack = _PyTuple_ITEMS(args);
Victor Stinnerf0ccbbb2016-09-09 17:40:38 -07002330 nargs = PyTuple_GET_SIZE(args);
2331 return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL,
2332 parser, p_va, flags);
2333}
2334
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002335
Serhiy Storchaka31913912019-03-14 10:32:22 +02002336#undef _PyArg_UnpackKeywords
2337
2338PyObject * const *
2339_PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs,
2340 PyObject *kwargs, PyObject *kwnames,
2341 struct _PyArg_Parser *parser,
2342 int minpos, int maxpos, int minkw,
2343 PyObject **buf)
2344{
2345 PyObject *kwtuple;
2346 PyObject *keyword;
2347 int i, posonly, minposonly, maxargs;
2348 int reqlimit = minkw ? maxpos + minkw : minpos;
2349 Py_ssize_t nkwargs;
2350 PyObject *current_arg;
2351 PyObject * const *kwstack = NULL;
2352
2353 assert(kwargs == NULL || PyDict_Check(kwargs));
2354 assert(kwargs == NULL || kwnames == NULL);
2355
2356 if (parser == NULL) {
2357 PyErr_BadInternalCall();
2358 return NULL;
2359 }
2360
2361 if (kwnames != NULL && !PyTuple_Check(kwnames)) {
2362 PyErr_BadInternalCall();
2363 return NULL;
2364 }
2365
2366 if (args == NULL && nargs == 0) {
2367 args = buf;
2368 }
2369
2370 if (!parser_init(parser)) {
2371 return NULL;
2372 }
2373
2374 kwtuple = parser->kwtuple;
2375 posonly = parser->pos;
2376 minposonly = Py_MIN(posonly, minpos);
2377 maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple);
2378
2379 if (kwargs != NULL) {
2380 nkwargs = PyDict_GET_SIZE(kwargs);
2381 }
2382 else if (kwnames != NULL) {
2383 nkwargs = PyTuple_GET_SIZE(kwnames);
2384 kwstack = args + nargs;
2385 }
2386 else {
2387 nkwargs = 0;
2388 }
2389 if (nkwargs == 0 && minkw == 0 && minpos <= nargs && nargs <= maxpos) {
2390 /* Fast path. */
2391 return args;
2392 }
2393 if (nargs + nkwargs > maxargs) {
2394 /* Adding "keyword" (when nargs == 0) prevents producing wrong error
2395 messages in some special cases (see bpo-31229). */
2396 PyErr_Format(PyExc_TypeError,
2397 "%.200s%s takes at most %d %sargument%s (%zd given)",
2398 (parser->fname == NULL) ? "function" : parser->fname,
2399 (parser->fname == NULL) ? "" : "()",
2400 maxargs,
2401 (nargs == 0) ? "keyword " : "",
2402 (maxargs == 1) ? "" : "s",
2403 nargs + nkwargs);
2404 return NULL;
2405 }
2406 if (nargs > maxpos) {
2407 if (maxpos == 0) {
2408 PyErr_Format(PyExc_TypeError,
2409 "%.200s%s takes no positional arguments",
2410 (parser->fname == NULL) ? "function" : parser->fname,
2411 (parser->fname == NULL) ? "" : "()");
2412 }
2413 else {
2414 PyErr_Format(PyExc_TypeError,
2415 "%.200s%s takes %s %d positional argument%s (%zd given)",
2416 (parser->fname == NULL) ? "function" : parser->fname,
2417 (parser->fname == NULL) ? "" : "()",
2418 (minpos < maxpos) ? "at most" : "exactly",
2419 maxpos,
2420 (maxpos == 1) ? "" : "s",
2421 nargs);
2422 }
2423 return NULL;
2424 }
2425 if (nargs < minposonly) {
2426 PyErr_Format(PyExc_TypeError,
2427 "%.200s%s takes %s %d positional argument%s"
2428 " (%zd given)",
2429 (parser->fname == NULL) ? "function" : parser->fname,
2430 (parser->fname == NULL) ? "" : "()",
2431 minposonly < maxpos ? "at least" : "exactly",
2432 minposonly,
2433 minposonly == 1 ? "" : "s",
2434 nargs);
2435 return NULL;
2436 }
2437
2438 /* copy tuple args */
2439 for (i = 0; i < nargs; i++) {
2440 buf[i] = args[i];
2441 }
2442
2443 /* copy keyword args using kwtuple to drive process */
Serhiy Storchaka1b0393d2019-03-16 19:45:00 +02002444 for (i = Py_MAX((int)nargs, posonly); i < maxargs; i++) {
Serhiy Storchaka31913912019-03-14 10:32:22 +02002445 if (nkwargs) {
2446 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2447 if (kwargs != NULL) {
2448 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2449 if (!current_arg && PyErr_Occurred()) {
2450 return NULL;
2451 }
2452 }
2453 else {
2454 current_arg = find_keyword(kwnames, kwstack, keyword);
2455 }
2456 }
2457 else if (i >= reqlimit) {
2458 break;
2459 }
2460 else {
2461 current_arg = NULL;
2462 }
2463
2464 buf[i] = current_arg;
2465
2466 if (current_arg) {
2467 --nkwargs;
2468 }
2469 else if (i < minpos || (maxpos <= i && i < reqlimit)) {
2470 /* Less arguments than required */
2471 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2472 PyErr_Format(PyExc_TypeError, "%.200s%s missing required "
2473 "argument '%U' (pos %d)",
2474 (parser->fname == NULL) ? "function" : parser->fname,
2475 (parser->fname == NULL) ? "" : "()",
2476 keyword, i+1);
2477 return NULL;
2478 }
2479 }
2480
2481 if (nkwargs > 0) {
2482 Py_ssize_t j;
2483 /* make sure there are no arguments given by name and position */
2484 for (i = posonly; i < nargs; i++) {
2485 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly);
2486 if (kwargs != NULL) {
2487 current_arg = PyDict_GetItemWithError(kwargs, keyword);
2488 if (!current_arg && PyErr_Occurred()) {
2489 return NULL;
2490 }
2491 }
2492 else {
2493 current_arg = find_keyword(kwnames, kwstack, keyword);
2494 }
2495 if (current_arg) {
2496 /* arg present in tuple and in dict */
2497 PyErr_Format(PyExc_TypeError,
2498 "argument for %.200s%s given by name ('%U') "
2499 "and position (%d)",
2500 (parser->fname == NULL) ? "function" : parser->fname,
2501 (parser->fname == NULL) ? "" : "()",
2502 keyword, i+1);
2503 return NULL;
2504 }
2505 }
2506 /* make sure there are no extraneous keyword arguments */
2507 j = 0;
2508 while (1) {
2509 int match;
2510 if (kwargs != NULL) {
2511 if (!PyDict_Next(kwargs, &j, &keyword, NULL))
2512 break;
2513 }
2514 else {
2515 if (j >= PyTuple_GET_SIZE(kwnames))
2516 break;
2517 keyword = PyTuple_GET_ITEM(kwnames, j);
2518 j++;
2519 }
2520
Serhiy Storchaka31913912019-03-14 10:32:22 +02002521 match = PySequence_Contains(kwtuple, keyword);
2522 if (match <= 0) {
2523 if (!match) {
2524 PyErr_Format(PyExc_TypeError,
Jeroen Demeyer05677862019-08-16 12:41:27 +02002525 "'%S' is an invalid keyword "
Serhiy Storchaka31913912019-03-14 10:32:22 +02002526 "argument for %.200s%s",
2527 keyword,
2528 (parser->fname == NULL) ? "this function" : parser->fname,
2529 (parser->fname == NULL) ? "" : "()");
2530 }
2531 return NULL;
2532 }
2533 }
2534 }
2535
2536 return buf;
2537}
2538
2539
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002540static const char *
Martin v. Löwis18e16552006-02-15 17:27:45 +00002541skipitem(const char **p_format, va_list *p_va, int flags)
Guido van Rossumaa354651996-08-19 19:32:04 +00002542{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 const char *format = *p_format;
2544 char c = *format++;
Guido van Rossum98297ee2007-11-06 21:34:58 +00002545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 switch (c) {
Georg Brandl6dd14612005-09-14 19:29:53 +00002547
Larry Hastingsa3479012012-05-08 23:52:03 -07002548 /*
2549 * codes that take a single data pointer as an argument
2550 * (the type of the pointer is irrelevant)
2551 */
Georg Brandl6dd14612005-09-14 19:29:53 +00002552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 case 'b': /* byte -- very short int */
2554 case 'B': /* byte as bitfield */
2555 case 'h': /* short int */
2556 case 'H': /* short int as bitfield */
2557 case 'i': /* int */
2558 case 'I': /* int sized bitfield */
2559 case 'l': /* long int */
2560 case 'k': /* long int sized bitfield */
Benjamin Petersonaf580df2016-09-06 10:46:49 -07002561 case 'L': /* long long */
2562 case 'K': /* long long sized bitfield */
Larry Hastingsa3479012012-05-08 23:52:03 -07002563 case 'n': /* Py_ssize_t */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 case 'f': /* float */
2565 case 'd': /* double */
2566 case 'D': /* complex double */
2567 case 'c': /* char */
2568 case 'C': /* unicode char */
Larry Hastings10ba07a2012-05-07 02:44:50 -07002569 case 'p': /* boolean predicate */
Larry Hastingsa3479012012-05-08 23:52:03 -07002570 case 'S': /* string object */
2571 case 'Y': /* string object */
2572 case 'U': /* unicode string object */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002574 if (p_va != NULL) {
2575 (void) va_arg(*p_va, void *);
2576 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 break;
2578 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 /* string codes */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 case 'e': /* string with encoding */
2583 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002584 if (p_va != NULL) {
2585 (void) va_arg(*p_va, const char *);
2586 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 if (!(*format == 's' || *format == 't'))
2588 /* after 'e', only 's' and 't' is allowed */
2589 goto err;
2590 format++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 }
Stefan Krahf432a322017-08-21 13:09:59 +02002592 /* fall through */
Guido van Rossum98297ee2007-11-06 21:34:58 +00002593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 case 's': /* string */
2595 case 'z': /* string or None */
2596 case 'y': /* bytes */
2597 case 'u': /* unicode string */
Larry Hastingsd9e4a412012-05-08 03:51:18 -07002598 case 'Z': /* unicode string or None */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 case 'w': /* buffer, read-write */
2600 {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002601 if (p_va != NULL) {
2602 (void) va_arg(*p_va, char **);
2603 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 if (*format == '#') {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002605 if (p_va != NULL) {
2606 if (flags & FLAG_SIZE_T)
2607 (void) va_arg(*p_va, Py_ssize_t *);
Inada Naokid3c72a22019-03-23 21:04:40 +09002608 else {
2609 if (PyErr_WarnEx(PyExc_DeprecationWarning,
2610 "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
2611 return NULL;
2612 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002613 (void) va_arg(*p_va, int *);
Inada Naokid3c72a22019-03-23 21:04:40 +09002614 }
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002615 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 format++;
Serhiy Storchaka504373c2018-07-11 17:41:43 +03002617 } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w')
2618 && *format == '*')
2619 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 format++;
2621 }
2622 break;
2623 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 case 'O': /* object */
2626 {
2627 if (*format == '!') {
2628 format++;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002629 if (p_va != NULL) {
2630 (void) va_arg(*p_va, PyTypeObject*);
2631 (void) va_arg(*p_va, PyObject **);
2632 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 }
2634 else if (*format == '&') {
2635 typedef int (*converter)(PyObject *, void *);
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002636 if (p_va != NULL) {
2637 (void) va_arg(*p_va, converter);
2638 (void) va_arg(*p_va, void *);
2639 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 format++;
2641 }
2642 else {
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002643 if (p_va != NULL) {
2644 (void) va_arg(*p_va, PyObject **);
2645 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 }
2647 break;
2648 }
Guido van Rossum98297ee2007-11-06 21:34:58 +00002649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 case '(': /* bypass tuple, not handled at all previously */
2651 {
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002652 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 for (;;) {
2654 if (*format==')')
2655 break;
2656 if (IS_END_OF_FORMAT(*format))
2657 return "Unmatched left paren in format "
2658 "string";
2659 msg = skipitem(&format, p_va, flags);
2660 if (msg)
2661 return msg;
2662 }
2663 format++;
2664 break;
2665 }
Christian Heimes380f7f22008-02-28 11:19:05 +00002666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 case ')':
2668 return "Unmatched right paren in format string";
Christian Heimes380f7f22008-02-28 11:19:05 +00002669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 default:
Georg Brandl6dd14612005-09-14 19:29:53 +00002671err:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 return "impossible<bad format char>";
Guido van Rossum98297ee2007-11-06 21:34:58 +00002673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 }
Georg Brandl6dd14612005-09-14 19:29:53 +00002675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 *p_format = format;
2677 return NULL;
Guido van Rossumaa354651996-08-19 19:32:04 +00002678}
Fred Drakee4616e62001-10-23 21:09:29 +00002679
2680
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002681#undef _PyArg_CheckPositional
Fred Drakee4616e62001-10-23 21:09:29 +00002682
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002683int
2684_PyArg_CheckPositional(const char *name, Py_ssize_t nargs,
2685 Py_ssize_t min, Py_ssize_t max)
2686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 assert(min >= 0);
2688 assert(min <= max);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002689
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002690 if (nargs < min) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 if (name != NULL)
2692 PyErr_Format(
2693 PyExc_TypeError,
Xtreak1426daa2018-07-23 01:43:26 +05302694 "%.200s expected %s%zd argument%s, got %zd",
2695 name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 else
2697 PyErr_Format(
2698 PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05302699 "unpacked tuple should have %s%zd element%s,"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 " but has %zd",
Xtreak63262782018-12-21 20:15:13 +05302701 (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 return 0;
2703 }
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002704
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002705 if (nargs == 0) {
Raymond Hettinger94230232016-03-26 03:02:48 -07002706 return 1;
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002707 }
2708
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002709 if (nargs > max) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 if (name != NULL)
2711 PyErr_Format(
2712 PyExc_TypeError,
Xtreak1426daa2018-07-23 01:43:26 +05302713 "%.200s expected %s%zd argument%s, got %zd",
2714 name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 else
2716 PyErr_Format(
2717 PyExc_TypeError,
Xtreak63262782018-12-21 20:15:13 +05302718 "unpacked tuple should have %s%zd element%s,"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 " but has %zd",
Xtreak63262782018-12-21 20:15:13 +05302720 (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 return 0;
2722 }
Raymond Hettinger94230232016-03-26 03:02:48 -07002723
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002724 return 1;
2725}
2726
2727static int
2728unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name,
2729 Py_ssize_t min, Py_ssize_t max, va_list vargs)
2730{
2731 Py_ssize_t i;
2732 PyObject **o;
2733
2734 if (!_PyArg_CheckPositional(name, nargs, min, max)) {
2735 return 0;
2736 }
2737
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002738 for (i = 0; i < nargs; i++) {
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002739 o = va_arg(vargs, PyObject **);
2740 *o = args[i];
2741 }
2742 return 1;
2743}
2744
2745int
2746PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...)
2747{
2748 PyObject **stack;
2749 Py_ssize_t nargs;
2750 int retval;
2751 va_list vargs;
2752
2753 if (!PyTuple_Check(args)) {
2754 PyErr_SetString(PyExc_SystemError,
2755 "PyArg_UnpackTuple() argument list is not a tuple");
2756 return 0;
2757 }
Victor Stinnerd17a6932018-11-09 16:56:48 +01002758 stack = _PyTuple_ITEMS(args);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002759 nargs = PyTuple_GET_SIZE(args);
2760
Raymond Hettinger94230232016-03-26 03:02:48 -07002761#ifdef HAVE_STDARG_PROTOTYPES
2762 va_start(vargs, max);
2763#else
2764 va_start(vargs);
2765#endif
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002766 retval = unpack_stack(stack, nargs, name, min, max, vargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 va_end(vargs);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002768 return retval;
2769}
2770
2771int
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002772_PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002773 Py_ssize_t min, Py_ssize_t max, ...)
2774{
2775 int retval;
2776 va_list vargs;
2777
2778#ifdef HAVE_STDARG_PROTOTYPES
2779 va_start(vargs, max);
2780#else
2781 va_start(vargs);
2782#endif
Victor Stinnerfdbc3972017-02-01 17:42:18 +01002783 retval = unpack_stack(args, nargs, name, min, max, vargs);
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002784 va_end(vargs);
2785 return retval;
Fred Drakee4616e62001-10-23 21:09:29 +00002786}
Georg Brandl02c42872005-08-26 06:42:30 +00002787
2788
Serhiy Storchaka68a001d2017-02-06 10:41:46 +02002789#undef _PyArg_NoKeywords
Dong-hee Na87ec86c2020-03-16 23:06:20 +09002790#undef _PyArg_NoKwnames
Serhiy Storchaka68a001d2017-02-06 10:41:46 +02002791#undef _PyArg_NoPositional
2792
Georg Brandl02c42872005-08-26 06:42:30 +00002793/* For type constructors that don't take keyword args
2794 *
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002795 * Sets a TypeError and returns 0 if the args/kwargs is
Thomas Wouters89f507f2006-12-13 04:49:30 +00002796 * not empty, returns 1 otherwise
Georg Brandl02c42872005-08-26 06:42:30 +00002797 */
2798int
Victor Stinner29d39cc2017-01-17 01:40:01 +01002799_PyArg_NoKeywords(const char *funcname, PyObject *kwargs)
Georg Brandl02c42872005-08-26 06:42:30 +00002800{
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002801 if (kwargs == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 return 1;
Victor Stinnerfe54dd82017-01-17 02:33:55 +01002803 }
Victor Stinner29d39cc2017-01-17 01:40:01 +01002804 if (!PyDict_CheckExact(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 PyErr_BadInternalCall();
2806 return 0;
2807 }
Victor Stinner29d39cc2017-01-17 01:40:01 +01002808 if (PyDict_GET_SIZE(kwargs) == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 return 1;
Victor Stinner29d39cc2017-01-17 01:40:01 +01002810 }
2811
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002812 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments",
Victor Stinner29d39cc2017-01-17 01:40:01 +01002813 funcname);
2814 return 0;
2815}
2816
Victor Stinner29d39cc2017-01-17 01:40:01 +01002817int
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002818_PyArg_NoPositional(const char *funcname, PyObject *args)
2819{
2820 if (args == NULL)
2821 return 1;
2822 if (!PyTuple_CheckExact(args)) {
2823 PyErr_BadInternalCall();
2824 return 0;
2825 }
2826 if (PyTuple_GET_SIZE(args) == 0)
2827 return 1;
2828
Serhiy Storchaka5eb788b2017-06-06 18:45:22 +03002829 PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
Larry Hastingsb7ccb202014-01-18 23:50:21 -08002830 funcname);
2831 return 0;
2832}
2833
Dong-hee Na87ec86c2020-03-16 23:06:20 +09002834int
2835_PyArg_NoKwnames(const char *funcname, PyObject *kwnames)
2836{
2837 if (kwnames == NULL) {
2838 return 1;
2839 }
2840
2841 assert(PyTuple_CheckExact(kwnames));
2842
2843 if (PyTuple_GET_SIZE(kwnames) == 0) {
2844 return 1;
2845 }
2846
2847 PyErr_Format(PyExc_TypeError, "%s() takes no keyword arguments", funcname);
2848 return 0;
2849}
2850
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +03002851void
2852_PyArg_Fini(void)
2853{
2854 struct _PyArg_Parser *tmp, *s = static_arg_parsers;
2855 while (s) {
2856 tmp = s->next;
2857 s->next = NULL;
2858 parser_clear(s);
2859 s = tmp;
2860 }
2861 static_arg_parsers = NULL;
2862}
2863
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002864#ifdef __cplusplus
2865};
2866#endif