Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 1 | |
| 2 | /* New getargs implementation */ |
| 3 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Victor Stinner | ec13b93 | 2018-11-25 23:56:17 +0100 | [diff] [blame] | 5 | #include "pycore_tupleobject.h" |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | c1d5053 | 1996-08-21 23:38:24 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
Benjamin Peterson | a853a8b | 2017-09-07 11:13:59 -0700 | [diff] [blame] | 8 | #include <float.h> |
Guido van Rossum | c1d5053 | 1996-08-21 23:38:24 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11 | #ifdef __cplusplus |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 12 | extern "C" { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13 | #endif |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 14 | int PyArg_Parse(PyObject *, const char *, ...); |
| 15 | int PyArg_ParseTuple(PyObject *, const char *, ...); |
| 16 | int PyArg_VaParse(PyObject *, const char *, va_list); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 17 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 18 | int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | const char *, char **, ...); |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 20 | int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 21 | const char *, char **, va_list); |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 22 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 23 | int _PyArg_ParseTupleAndKeywordsFast(PyObject *, PyObject *, |
| 24 | struct _PyArg_Parser *, ...); |
| 25 | int _PyArg_VaParseTupleAndKeywordsFast(PyObject *, PyObject *, |
| 26 | struct _PyArg_Parser *, va_list); |
| 27 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 28 | #ifdef HAVE_DECLSPEC_DLL |
| 29 | /* Export functions */ |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 30 | PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...); |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 31 | PyAPI_FUNC(int) _PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs, |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 32 | const char *format, ...); |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 33 | PyAPI_FUNC(int) _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, |
| 34 | PyObject *kwnames, |
Benjamin Peterson | 819a46f | 2016-09-09 20:45:06 -0700 | [diff] [blame] | 35 | struct _PyArg_Parser *parser, ...); |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 36 | PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *, |
| 38 | const char *, char **, ...); |
| 39 | PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...); |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 40 | PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 41 | PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *, |
| 42 | const char *, char **, va_list); |
Benjamin Peterson | 4eef505 | 2016-09-10 17:04:36 -0700 | [diff] [blame] | 43 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 44 | PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *, |
| 45 | struct _PyArg_Parser *, ...); |
| 46 | PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *, |
| 47 | struct _PyArg_Parser *, va_list); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 50 | #define FLAG_COMPAT 1 |
| 51 | #define FLAG_SIZE_T 2 |
| 52 | |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 53 | typedef 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 | */ |
| 60 | typedef struct { |
| 61 | void *item; |
| 62 | destr_t destructor; |
| 63 | } freelistentry_t; |
| 64 | |
| 65 | typedef struct { |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 66 | freelistentry_t *entries; |
Antoine Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 67 | int first_available; |
| 68 | int entries_malloced; |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 69 | } freelist_t; |
| 70 | |
Antoine Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 71 | #define STATIC_FREELIST_ENTRIES 8 |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 72 | |
| 73 | /* Forward */ |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 74 | static int vgetargs1_impl(PyObject *args, PyObject *const *stack, Py_ssize_t nargs, |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 75 | const char *format, va_list *p_va, int flags); |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 76 | static int vgetargs1(PyObject *, const char *, va_list *, int); |
Victor Stinner | 84bb1cf | 2013-05-17 00:12:04 +0200 | [diff] [blame] | 77 | static void seterror(Py_ssize_t, const char *, int *, const char *, const char *); |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 78 | static const char *convertitem(PyObject *, const char **, va_list *, int, int *, |
| 79 | char *, size_t, freelist_t *); |
| 80 | static const char *converttuple(PyObject *, const char **, va_list *, int, |
| 81 | int *, char *, size_t, int, freelist_t *); |
| 82 | static const char *convertsimple(PyObject *, const char **, va_list *, int, |
| 83 | char *, size_t, freelist_t *); |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 84 | static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **); |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 85 | static int getbuffer(PyObject *, Py_buffer *, const char**); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 86 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 87 | static int vgetargskeywords(PyObject *, PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | const char *, char **, va_list *, int); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 89 | static int vgetargskeywordsfast(PyObject *, PyObject *, |
| 90 | struct _PyArg_Parser *, va_list *, int); |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 91 | static int vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 92 | PyObject *keywords, PyObject *kwnames, |
| 93 | struct _PyArg_Parser *parser, |
| 94 | va_list *p_va, int flags); |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 95 | static const char *skipitem(const char **, va_list *, int); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 96 | |
Fred Drake | 563dfc2 | 2001-10-23 14:41:08 +0000 | [diff] [blame] | 97 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 98 | PyArg_Parse(PyObject *args, const char *format, ...) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 99 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | int retval; |
| 101 | va_list va; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | va_start(va, format); |
| 104 | retval = vgetargs1(args, format, &va, FLAG_COMPAT); |
| 105 | va_end(va); |
| 106 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 110 | _PyArg_Parse_SizeT(PyObject *args, const char *format, ...) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | int retval; |
| 113 | va_list va; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | va_start(va, format); |
| 116 | retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T); |
| 117 | va_end(va); |
| 118 | return retval; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Fred Drake | 563dfc2 | 2001-10-23 14:41:08 +0000 | [diff] [blame] | 122 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 123 | PyArg_ParseTuple(PyObject *args, const char *format, ...) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 125 | int retval; |
| 126 | va_list va; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | va_start(va, format); |
| 129 | retval = vgetargs1(args, format, &va, 0); |
| 130 | va_end(va); |
| 131 | return retval; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 134 | int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 135 | _PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 136 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | int retval; |
| 138 | va_list va; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | va_start(va, format); |
| 141 | retval = vgetargs1(args, format, &va, FLAG_SIZE_T); |
| 142 | va_end(va); |
| 143 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 146 | |
| 147 | int |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 148 | _PyArg_ParseStack(PyObject *const *args, Py_ssize_t nargs, const char *format, ...) |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 149 | { |
| 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 | |
| 159 | int |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 160 | _PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs, const char *format, ...) |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 161 | { |
| 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 | |
| 172 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 173 | PyArg_VaParse(PyObject *args, const char *format, va_list va) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | va_list lva; |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 176 | int retval; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 177 | |
Benjamin Peterson | 0c21214 | 2016-09-20 20:39:33 -0700 | [diff] [blame] | 178 | va_copy(lva, va); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 179 | |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 180 | retval = vgetargs1(args, format, &lva, 0); |
| 181 | va_end(lva); |
| 182 | return retval; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 185 | int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 186 | _PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 187 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | va_list lva; |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 189 | int retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 190 | |
Benjamin Peterson | 0c21214 | 2016-09-20 20:39:33 -0700 | [diff] [blame] | 191 | va_copy(lva, va); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 192 | |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 193 | retval = vgetargs1(args, format, &lva, FLAG_SIZE_T); |
| 194 | va_end(lva); |
| 195 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 198 | |
Martin v. Löwis | e6bbb4d | 2003-05-03 10:00:22 +0000 | [diff] [blame] | 199 | /* Handle cleanup of allocated memory in case of exception */ |
| 200 | |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 201 | static int |
| 202 | cleanup_ptr(PyObject *self, void *ptr) |
Antoine Pitrou | f7199578 | 2008-08-29 18:37:05 +0000 | [diff] [blame] | 203 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | if (ptr) { |
| 205 | PyMem_FREE(ptr); |
| 206 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 207 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Martin v. Löwis | e6bbb4d | 2003-05-03 10:00:22 +0000 | [diff] [blame] | 210 | static int |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 211 | cleanup_buffer(PyObject *self, void *ptr) |
Martin v. Löwis | e6bbb4d | 2003-05-03 10:00:22 +0000 | [diff] [blame] | 212 | { |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 213 | Py_buffer *buf = (Py_buffer *)ptr; |
| 214 | if (buf) { |
| 215 | PyBuffer_Release(buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 216 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int |
| 221 | addcleanup(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 | |
| 234 | static int |
| 235 | cleanreturn(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 Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 248 | if (freelist->entries_malloced) |
| 249 | PyMem_FREE(freelist->entries); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | return retval; |
Martin v. Löwis | e6bbb4d | 2003-05-03 10:00:22 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 254 | static int |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 255 | vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs, const char *format, |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 256 | va_list *p_va, int flags) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | 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 Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 267 | Py_ssize_t i; |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 268 | const char *msg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | int compat = flags & FLAG_COMPAT; |
Benjamin Peterson | 40be9e5 | 2014-02-11 10:09:27 -0500 | [diff] [blame] | 270 | freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
| 271 | freelist_t freelist; |
| 272 | |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 273 | assert(nargs == 0 || stack != NULL); |
| 274 | |
Benjamin Peterson | 40be9e5 | 2014-02-11 10:09:27 -0500 | [diff] [blame] | 275 | freelist.entries = static_entries; |
| 276 | freelist.first_available = 0; |
| 277 | freelist.entries_malloced = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 279 | flags = flags & ~FLAG_COMPAT; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | 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 Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 309 | case '|': |
| 310 | if (level == 0) |
| 311 | min = max; |
| 312 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | default: |
| 314 | if (level == 0) { |
Antoine Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 315 | if (Py_ISALPHA(Py_CHARMASK(c))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | if (c != 'e') /* skip encoded */ |
| 317 | max++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | } |
| 319 | break; |
| 320 | } |
| 321 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | if (level != 0) |
| 324 | Py_FatalError(/* '(' */ "missing ')' in getargs format"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | if (min < 0) |
| 327 | min = max; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | format = formatsave; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 330 | |
Antoine Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 331 | 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 Peterson | 7ed6727 | 2012-03-16 12:21:02 -0500 | [diff] [blame] | 338 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | if (compat) { |
| 341 | if (max == 0) { |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 342 | if (compat_args == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | return 1; |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 344 | PyErr_Format(PyExc_TypeError, |
| 345 | "%.200s%s takes no arguments", |
| 346 | fname==NULL ? "function" : fname, |
| 347 | fname==NULL ? "" : "()"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 348 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | } |
| 350 | else if (min == 1 && max == 1) { |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 351 | if (compat_args == NULL) { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 352 | PyErr_Format(PyExc_TypeError, |
| 353 | "%.200s%s takes at least one argument", |
| 354 | fname==NULL ? "function" : fname, |
| 355 | fname==NULL ? "" : "()"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 356 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | } |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 358 | msg = convertitem(compat_args, &format, p_va, flags, levels, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 359 | msgbuf, sizeof(msgbuf), &freelist); |
| 360 | if (msg == NULL) |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 361 | return cleanreturn(1, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | seterror(levels[0], msg, levels+1, fname, message); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 363 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | } |
| 365 | else { |
| 366 | PyErr_SetString(PyExc_SystemError, |
| 367 | "old style getargs format uses new features"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 368 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 371 | |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 372 | if (nargs < min || max < nargs) { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 373 | if (message == NULL) |
| 374 | PyErr_Format(PyExc_TypeError, |
| 375 | "%.150s%s takes %s %d argument%s (%ld given)", |
| 376 | fname==NULL ? "function" : fname, |
| 377 | fname==NULL ? "" : "()", |
| 378 | min==max ? "exactly" |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 379 | : nargs < min ? "at least" : "at most", |
| 380 | nargs < min ? min : max, |
| 381 | (nargs < min ? min : max) == 1 ? "" : "s", |
| 382 | Py_SAFE_DOWNCAST(nargs, Py_ssize_t, long)); |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 383 | else |
| 384 | PyErr_SetString(PyExc_TypeError, message); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 385 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 387 | |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 388 | for (i = 0; i < nargs; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | if (*format == '|') |
| 390 | format++; |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 391 | msg = convertitem(stack[i], &format, p_va, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | flags, levels, msgbuf, |
| 393 | sizeof(msgbuf), &freelist); |
| 394 | if (msg) { |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 395 | seterror(i+1, msg, levels, fname, message); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 396 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
Guido van Rossum | 231a41e | 1997-12-09 20:36:39 +0000 | [diff] [blame] | 399 | |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 400 | if (*format != '\0' && !Py_ISALPHA(Py_CHARMASK(*format)) && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | *format != '(' && |
| 402 | *format != '|' && *format != ':' && *format != ';') { |
| 403 | PyErr_Format(PyExc_SystemError, |
| 404 | "bad format string: %.200s", formatsave); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 405 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 407 | |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 408 | return cleanreturn(1, &freelist); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 411 | static int |
| 412 | vgetargs1(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 Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 426 | stack = _PyTuple_ITEMS(args); |
Victor Stinner | 6518a93 | 2017-01-17 01:29:49 +0100 | [diff] [blame] | 427 | 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 Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 436 | |
| 437 | |
| 438 | static void |
Victor Stinner | 84bb1cf | 2013-05-17 00:12:04 +0200 | [diff] [blame] | 439 | seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname, |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 440 | const char *message) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 441 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | char buf[512]; |
| 443 | int i; |
| 444 | char *p = buf; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | 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 Oudkerk | 25296ce | 2013-05-18 17:35:19 +0100 | [diff] [blame] | 455 | "argument %" PY_FORMAT_SIZE_T "d", iarg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | i = 0; |
| 457 | p += strlen(p); |
Georg Brandl | 142ad66 | 2013-10-14 07:01:11 +0200 | [diff] [blame] | 458 | while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | 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 Storchaka | 4cd63ef | 2016-02-08 01:22:47 +0200 | [diff] [blame] | 472 | if (msg[0] == '(') { |
| 473 | PyErr_SetString(PyExc_SystemError, message); |
| 474 | } |
| 475 | else { |
| 476 | PyErr_SetString(PyExc_TypeError, message); |
| 477 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 478 | } |
| 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | "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 Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 496 | and msgbuf is returned. |
| 497 | */ |
| 498 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 499 | static const char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 500 | converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 501 | int *levels, char *msgbuf, size_t bufsize, int toplevel, |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 502 | freelist_t *freelist) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 503 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | int level = 0; |
| 505 | int n = 0; |
| 506 | const char *format = *p_format; |
| 507 | int i; |
Victor Stinner | 74387f5 | 2013-11-18 01:21:12 +0100 | [diff] [blame] | 508 | Py_ssize_t len; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | for (;;) { |
| 511 | int c = *format++; |
| 512 | if (c == '(') { |
| 513 | if (level == 0) |
| 514 | n++; |
| 515 | level++; |
| 516 | } |
| 517 | else if (c == ')') { |
| 518 | if (level == 0) |
| 519 | break; |
| 520 | level--; |
| 521 | } |
| 522 | else if (c == ':' || c == ';' || c == '\0') |
| 523 | break; |
Antoine Pitrou | 4de7457 | 2013-02-09 23:11:27 +0100 | [diff] [blame] | 524 | else if (level == 0 && Py_ISALPHA(Py_CHARMASK(c))) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | n++; |
| 526 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | if (!PySequence_Check(arg) || PyBytes_Check(arg)) { |
| 529 | levels[0] = 0; |
| 530 | PyOS_snprintf(msgbuf, bufsize, |
| 531 | toplevel ? "expected %d arguments, not %.50s" : |
| 532 | "must be %d-item sequence, not %.50s", |
| 533 | n, |
| 534 | arg == Py_None ? "None" : arg->ob_type->tp_name); |
| 535 | return msgbuf; |
| 536 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 537 | |
Victor Stinner | 74387f5 | 2013-11-18 01:21:12 +0100 | [diff] [blame] | 538 | len = PySequence_Size(arg); |
| 539 | if (len != n) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 540 | levels[0] = 0; |
Victor Stinner | 74387f5 | 2013-11-18 01:21:12 +0100 | [diff] [blame] | 541 | if (toplevel) { |
| 542 | PyOS_snprintf(msgbuf, bufsize, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 543 | "expected %d argument%s, not %" PY_FORMAT_SIZE_T "d", |
| 544 | n, |
| 545 | n == 1 ? "" : "s", |
| 546 | len); |
Victor Stinner | 74387f5 | 2013-11-18 01:21:12 +0100 | [diff] [blame] | 547 | } |
| 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | return msgbuf; |
| 555 | } |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | format = *p_format; |
| 558 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 559 | const char *msg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | 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 Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | *p_format = format; |
| 580 | return NULL; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | |
| 584 | /* Convert a single item. */ |
| 585 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 586 | static const char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 587 | convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 588 | int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 589 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 590 | const char *msg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | const char *format = *p_format; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 593 | 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 Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 612 | |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 613 | /* Format an error message generated by convertsimple(). */ |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 614 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 615 | void |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 616 | _PyArg_BadArgument(const char *fname, int iarg, |
| 617 | const char *expected, PyObject *arg) |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 618 | { |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 619 | if (iarg) { |
| 620 | PyErr_Format(PyExc_TypeError, |
| 621 | "%.200s() argument %d must be %.50s, not %.50s", |
| 622 | fname, iarg, expected, |
| 623 | arg == Py_None ? "None" : arg->ob_type->tp_name); |
| 624 | } |
| 625 | else { |
| 626 | PyErr_Format(PyExc_TypeError, |
| 627 | "%.200s() argument must be %.50s, not %.50s", |
| 628 | fname, expected, |
| 629 | arg == Py_None ? "None" : arg->ob_type->tp_name); |
| 630 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 633 | static const char * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 634 | converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize) |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 635 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | assert(expected != NULL); |
| 637 | assert(arg != NULL); |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 638 | if (expected[0] == '(') { |
| 639 | PyOS_snprintf(msgbuf, bufsize, |
| 640 | "%.100s", expected); |
| 641 | } |
| 642 | else { |
| 643 | PyOS_snprintf(msgbuf, bufsize, |
| 644 | "must be %.50s, not %.50s", expected, |
| 645 | arg == Py_None ? "None" : arg->ob_type->tp_name); |
| 646 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | return msgbuf; |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | #define CONV_UNICODE "(unicode conversion error)" |
| 651 | |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 652 | /* Explicitly check for float arguments when integers are expected. |
| 653 | Return 1 for error, 0 if ok. */ |
Neil Schemenauer | 5042da6 | 2003-02-04 20:59:40 +0000 | [diff] [blame] | 654 | static int |
| 655 | float_argument_error(PyObject *arg) |
| 656 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | if (PyFloat_Check(arg)) { |
| 658 | PyErr_SetString(PyExc_TypeError, |
| 659 | "integer argument expected, got float" ); |
| 660 | return 1; |
| 661 | } |
| 662 | else |
| 663 | return 0; |
Neil Schemenauer | 5042da6 | 2003-02-04 20:59:40 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 666 | /* Convert a non-tuple argument. Return NULL if conversion went OK, |
| 667 | or a string with a message describing the failure. The message is |
| 668 | formatted as "must be <desired type>, not <actual type>". |
| 669 | When failing, an exception may or may not have been raised. |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 670 | Don't call if a tuple is expected. |
| 671 | |
| 672 | When you add new format codes, please don't forget poor skipitem() below. |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 673 | */ |
| 674 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 675 | static const char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 676 | convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 677 | char *msgbuf, size_t bufsize, freelist_t *freelist) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 678 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | /* For # codes */ |
| 680 | #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\ |
| 681 | if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \ |
| 682 | else q=va_arg(*p_va, int*); |
Victor Stinner | b3c9e07 | 2011-01-04 02:07:34 +0000 | [diff] [blame] | 683 | #define STORE_SIZE(s) \ |
| 684 | if (flags & FLAG_SIZE_T) \ |
| 685 | *q2=s; \ |
| 686 | else { \ |
| 687 | if (INT_MAX < s) { \ |
| 688 | PyErr_SetString(PyExc_OverflowError, \ |
| 689 | "size does not fit in an int"); \ |
| 690 | return converterr("", arg, msgbuf, bufsize); \ |
| 691 | } \ |
Victor Stinner | 9550ef3 | 2013-06-05 01:18:13 +0200 | [diff] [blame] | 692 | *q = (int)s; \ |
Victor Stinner | b3c9e07 | 2011-01-04 02:07:34 +0000 | [diff] [blame] | 693 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 694 | #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 695 | #define RETURN_ERR_OCCURRED return msgbuf |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 697 | const char *format = *p_format; |
| 698 | char c = *format++; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 699 | const char *sarg; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | switch (c) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | case 'b': { /* unsigned byte -- very short int */ |
| 704 | char *p = va_arg(*p_va, char *); |
| 705 | long ival; |
| 706 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 707 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 708 | ival = PyLong_AsLong(arg); |
| 709 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 710 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 711 | else if (ival < 0) { |
| 712 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 713 | "unsigned byte integer is less than minimum"); |
| 714 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | } |
| 716 | else if (ival > UCHAR_MAX) { |
| 717 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 718 | "unsigned byte integer is greater than maximum"); |
| 719 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | } |
| 721 | else |
| 722 | *p = (unsigned char) ival; |
| 723 | break; |
| 724 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 725 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | case 'B': {/* byte sized bitfield - both signed and unsigned |
| 727 | values allowed */ |
| 728 | char *p = va_arg(*p_va, char *); |
| 729 | long ival; |
| 730 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 731 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | ival = PyLong_AsUnsignedLongMask(arg); |
| 733 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 734 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | else |
| 736 | *p = (unsigned char) ival; |
| 737 | break; |
| 738 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | case 'h': {/* signed short int */ |
| 741 | short *p = va_arg(*p_va, short *); |
| 742 | long ival; |
| 743 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 744 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 745 | ival = PyLong_AsLong(arg); |
| 746 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 747 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 748 | else if (ival < SHRT_MIN) { |
| 749 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 750 | "signed short integer is less than minimum"); |
| 751 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | } |
| 753 | else if (ival > SHRT_MAX) { |
| 754 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 755 | "signed short integer is greater than maximum"); |
| 756 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 757 | } |
| 758 | else |
| 759 | *p = (short) ival; |
| 760 | break; |
| 761 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 763 | case 'H': { /* short int sized bitfield, both signed and |
| 764 | unsigned allowed */ |
| 765 | unsigned short *p = va_arg(*p_va, unsigned short *); |
| 766 | long ival; |
| 767 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 768 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | ival = PyLong_AsUnsignedLongMask(arg); |
| 770 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 771 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | else |
| 773 | *p = (unsigned short) ival; |
| 774 | break; |
| 775 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | case 'i': {/* signed int */ |
| 778 | int *p = va_arg(*p_va, int *); |
| 779 | long ival; |
| 780 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 781 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | ival = PyLong_AsLong(arg); |
| 783 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 784 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | else if (ival > INT_MAX) { |
| 786 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 787 | "signed integer is greater than maximum"); |
| 788 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | } |
| 790 | else if (ival < INT_MIN) { |
| 791 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 792 | "signed integer is less than minimum"); |
| 793 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | } |
| 795 | else |
| 796 | *p = ival; |
| 797 | break; |
| 798 | } |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | case 'I': { /* int sized bitfield, both signed and |
| 801 | unsigned allowed */ |
| 802 | unsigned int *p = va_arg(*p_va, unsigned int *); |
| 803 | unsigned int ival; |
| 804 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 805 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 806 | ival = (unsigned int)PyLong_AsUnsignedLongMask(arg); |
| 807 | if (ival == (unsigned int)-1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 808 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | else |
| 810 | *p = ival; |
| 811 | break; |
| 812 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | case 'n': /* Py_ssize_t */ |
| 815 | { |
| 816 | PyObject *iobj; |
| 817 | Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *); |
| 818 | Py_ssize_t ival = -1; |
| 819 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 820 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 821 | iobj = PyNumber_Index(arg); |
| 822 | if (iobj != NULL) { |
| 823 | ival = PyLong_AsSsize_t(iobj); |
| 824 | Py_DECREF(iobj); |
| 825 | } |
| 826 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 827 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | *p = ival; |
| 829 | break; |
| 830 | } |
| 831 | case 'l': {/* long int */ |
| 832 | long *p = va_arg(*p_va, long *); |
| 833 | long ival; |
| 834 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 835 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | ival = PyLong_AsLong(arg); |
| 837 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 838 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | else |
| 840 | *p = ival; |
| 841 | break; |
| 842 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | case 'k': { /* long sized bitfield */ |
| 845 | unsigned long *p = va_arg(*p_va, unsigned long *); |
| 846 | unsigned long ival; |
| 847 | if (PyLong_Check(arg)) |
| 848 | ival = PyLong_AsUnsignedLongMask(arg); |
| 849 | else |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 850 | return converterr("int", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | *p = ival; |
| 852 | break; |
| 853 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 854 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 855 | case 'L': {/* long long */ |
| 856 | long long *p = va_arg( *p_va, long long * ); |
| 857 | long long ival; |
Mark Dickinson | c730131 | 2010-06-10 16:05:10 +0000 | [diff] [blame] | 858 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 859 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | ival = PyLong_AsLongLong(arg); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 861 | if (ival == (long long)-1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 862 | RETURN_ERR_OCCURRED; |
Mark Dickinson | c730131 | 2010-06-10 16:05:10 +0000 | [diff] [blame] | 863 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 864 | *p = ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | break; |
| 866 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | case 'K': { /* long long sized bitfield */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 869 | unsigned long long *p = va_arg(*p_va, unsigned long long *); |
| 870 | unsigned long long ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | if (PyLong_Check(arg)) |
| 872 | ival = PyLong_AsUnsignedLongLongMask(arg); |
| 873 | else |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 874 | return converterr("int", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | *p = ival; |
| 876 | break; |
| 877 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | case 'f': {/* float */ |
| 880 | float *p = va_arg(*p_va, float *); |
| 881 | double dval = PyFloat_AsDouble(arg); |
| 882 | if (PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 883 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | else |
| 885 | *p = (float) dval; |
| 886 | break; |
| 887 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | case 'd': {/* double */ |
| 890 | double *p = va_arg(*p_va, double *); |
| 891 | double dval = PyFloat_AsDouble(arg); |
| 892 | if (PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 893 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | else |
| 895 | *p = dval; |
| 896 | break; |
| 897 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | case 'D': {/* complex double */ |
| 900 | Py_complex *p = va_arg(*p_va, Py_complex *); |
| 901 | Py_complex cval; |
| 902 | cval = PyComplex_AsCComplex(arg); |
| 903 | if (PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 904 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | else |
| 906 | *p = cval; |
| 907 | break; |
| 908 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | case 'c': {/* char */ |
| 911 | char *p = va_arg(*p_va, char *); |
| 912 | if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1) |
| 913 | *p = PyBytes_AS_STRING(arg)[0]; |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 914 | else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1) |
| 915 | *p = PyByteArray_AS_STRING(arg)[0]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 916 | else |
| 917 | return converterr("a byte string of length 1", arg, msgbuf, bufsize); |
| 918 | break; |
| 919 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | case 'C': {/* unicode char */ |
| 922 | int *p = va_arg(*p_va, int *); |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 923 | int kind; |
| 924 | void *data; |
| 925 | |
| 926 | if (!PyUnicode_Check(arg)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | return converterr("a unicode character", arg, msgbuf, bufsize); |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 928 | |
| 929 | if (PyUnicode_READY(arg)) |
| 930 | RETURN_ERR_OCCURRED; |
| 931 | |
| 932 | if (PyUnicode_GET_LENGTH(arg) != 1) |
| 933 | return converterr("a unicode character", arg, msgbuf, bufsize); |
| 934 | |
| 935 | kind = PyUnicode_KIND(arg); |
| 936 | data = PyUnicode_DATA(arg); |
| 937 | *p = PyUnicode_READ(kind, data, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 938 | break; |
| 939 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 940 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 941 | case 'p': {/* boolean *p*redicate */ |
| 942 | int *p = va_arg(*p_va, int *); |
| 943 | int val = PyObject_IsTrue(arg); |
| 944 | if (val > 0) |
| 945 | *p = 1; |
| 946 | else if (val == 0) |
| 947 | *p = 0; |
| 948 | else |
| 949 | RETURN_ERR_OCCURRED; |
| 950 | break; |
| 951 | } |
| 952 | |
Victor Stinner | 3dcb5ac | 2010-06-08 22:54:19 +0000 | [diff] [blame] | 953 | /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | need to be cleaned up! */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 955 | |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 956 | case 'y': {/* any bytes-like object */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | void **p = (void **)va_arg(*p_va, char **); |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 958 | const char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | Py_ssize_t count; |
| 960 | if (*format == '*') { |
| 961 | if (getbuffer(arg, (Py_buffer*)p, &buf) < 0) |
| 962 | return converterr(buf, arg, msgbuf, bufsize); |
| 963 | format++; |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 964 | if (addcleanup(p, freelist, cleanup_buffer)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | return converterr( |
| 966 | "(cleanup problem)", |
| 967 | arg, msgbuf, bufsize); |
| 968 | } |
| 969 | break; |
| 970 | } |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 971 | count = convertbuffer(arg, (const void **)p, &buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | if (count < 0) |
| 973 | return converterr(buf, arg, msgbuf, bufsize); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 974 | if (*format == '#') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | FETCH_SIZE; |
| 976 | STORE_SIZE(count); |
| 977 | format++; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 978 | } else { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 979 | if (strlen(*p) != (size_t)count) { |
| 980 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
| 981 | RETURN_ERR_OCCURRED; |
| 982 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | } |
| 984 | break; |
| 985 | } |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 986 | |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 987 | case 's': /* text string or bytes-like object */ |
| 988 | case 'z': /* text string, bytes-like object or None */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 989 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | if (*format == '*') { |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 991 | /* "s*" or "z*" */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 993 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 994 | if (c == 'z' && arg == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 995 | PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0); |
| 996 | else if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 997 | Py_ssize_t len; |
| 998 | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 999 | if (sarg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | return converterr(CONV_UNICODE, |
| 1001 | arg, msgbuf, bufsize); |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1002 | PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | } |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 1004 | else { /* any bytes-like object */ |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1005 | const char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | if (getbuffer(arg, p, &buf) < 0) |
| 1007 | return converterr(buf, arg, msgbuf, bufsize); |
| 1008 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1009 | if (addcleanup(p, freelist, cleanup_buffer)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | return converterr( |
| 1011 | "(cleanup problem)", |
| 1012 | arg, msgbuf, bufsize); |
| 1013 | } |
| 1014 | format++; |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 1015 | } else if (*format == '#') { /* a string or read-only bytes-like object */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1016 | /* "s#" or "z#" */ |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1017 | const void **p = (const void **)va_arg(*p_va, const char **); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | FETCH_SIZE; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1019 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1020 | if (c == 'z' && arg == Py_None) { |
| 1021 | *p = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | STORE_SIZE(0); |
| 1023 | } |
| 1024 | else if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1025 | Py_ssize_t len; |
| 1026 | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 1027 | if (sarg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | return converterr(CONV_UNICODE, |
| 1029 | arg, msgbuf, bufsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1030 | *p = sarg; |
| 1031 | STORE_SIZE(len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | } |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 1033 | else { /* read-only bytes-like object */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | /* XXX Really? */ |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1035 | const char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | Py_ssize_t count = convertbuffer(arg, p, &buf); |
| 1037 | if (count < 0) |
| 1038 | return converterr(buf, arg, msgbuf, bufsize); |
| 1039 | STORE_SIZE(count); |
| 1040 | } |
| 1041 | format++; |
| 1042 | } else { |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1043 | /* "s" or "z" */ |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1044 | const char **p = va_arg(*p_va, const char **); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1045 | Py_ssize_t len; |
| 1046 | sarg = NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1047 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1048 | if (c == 'z' && arg == Py_None) |
| 1049 | *p = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | else if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1051 | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 1052 | if (sarg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | return converterr(CONV_UNICODE, |
| 1054 | arg, msgbuf, bufsize); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1055 | if (strlen(sarg) != (size_t)len) { |
| 1056 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1057 | RETURN_ERR_OCCURRED; |
| 1058 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1059 | *p = sarg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | } |
| 1061 | else |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1062 | return converterr(c == 'z' ? "str or None" : "str", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | } |
| 1065 | break; |
| 1066 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1067 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1068 | case 'u': /* raw unicode buffer (Py_UNICODE *) */ |
| 1069 | case 'Z': /* raw unicode buffer or None */ |
| 1070 | { |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1071 | Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **); |
| 1072 | |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 1073 | if (*format == '#') { |
Serhiy Storchaka | d6e53da | 2015-04-19 21:11:30 +0300 | [diff] [blame] | 1074 | /* "u#" or "Z#" */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | FETCH_SIZE; |
Travis E. Oliphant | ddacf96 | 2007-10-13 21:03:27 +0000 | [diff] [blame] | 1076 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1077 | if (c == 'Z' && arg == Py_None) { |
| 1078 | *p = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | STORE_SIZE(0); |
| 1080 | } |
| 1081 | else if (PyUnicode_Check(arg)) { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 1082 | Py_ssize_t len; |
| 1083 | *p = PyUnicode_AsUnicodeAndSize(arg, &len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1084 | if (*p == NULL) |
| 1085 | RETURN_ERR_OCCURRED; |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 1086 | STORE_SIZE(len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1087 | } |
Victor Stinner | 5216e6d | 2010-06-08 21:45:51 +0000 | [diff] [blame] | 1088 | else |
Serhiy Storchaka | d6e53da | 2015-04-19 21:11:30 +0300 | [diff] [blame] | 1089 | return converterr(c == 'Z' ? "str or None" : "str", |
| 1090 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | format++; |
| 1092 | } else { |
Serhiy Storchaka | d6e53da | 2015-04-19 21:11:30 +0300 | [diff] [blame] | 1093 | /* "u" or "Z" */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1094 | if (c == 'Z' && arg == Py_None) |
| 1095 | *p = NULL; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1096 | else if (PyUnicode_Check(arg)) { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 1097 | Py_ssize_t len; |
| 1098 | *p = PyUnicode_AsUnicodeAndSize(arg, &len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1099 | if (*p == NULL) |
| 1100 | RETURN_ERR_OCCURRED; |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1101 | if (wcslen(*p) != (size_t)len) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1102 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1103 | RETURN_ERR_OCCURRED; |
| 1104 | } |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1105 | } else |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1106 | return converterr(c == 'Z' ? "str or None" : "str", |
| 1107 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | } |
| 1109 | break; |
| 1110 | } |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | case 'e': {/* encoded string */ |
| 1113 | char **buffer; |
| 1114 | const char *encoding; |
| 1115 | PyObject *s; |
| 1116 | int recode_strings; |
| 1117 | Py_ssize_t size; |
| 1118 | const char *ptr; |
Jeremy Hylton | 4819e97 | 2001-10-11 14:40:37 +0000 | [diff] [blame] | 1119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | /* Get 'e' parameter: the encoding name */ |
| 1121 | encoding = (const char *)va_arg(*p_va, const char *); |
| 1122 | if (encoding == NULL) |
| 1123 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1124 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | /* Get output buffer parameter: |
| 1126 | 's' (recode all objects via Unicode) or |
| 1127 | 't' (only recode non-string objects) |
| 1128 | */ |
| 1129 | if (*format == 's') |
| 1130 | recode_strings = 1; |
| 1131 | else if (*format == 't') |
| 1132 | recode_strings = 0; |
| 1133 | else |
| 1134 | return converterr( |
| 1135 | "(unknown parser marker combination)", |
| 1136 | arg, msgbuf, bufsize); |
| 1137 | buffer = (char **)va_arg(*p_va, char **); |
| 1138 | format++; |
| 1139 | if (buffer == NULL) |
| 1140 | return converterr("(buffer is NULL)", |
| 1141 | arg, msgbuf, bufsize); |
Benjamin Peterson | 9edd2bd | 2008-08-27 00:31:37 +0000 | [diff] [blame] | 1142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1143 | /* Encode object */ |
| 1144 | if (!recode_strings && |
| 1145 | (PyBytes_Check(arg) || PyByteArray_Check(arg))) { |
| 1146 | s = arg; |
| 1147 | Py_INCREF(s); |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1148 | if (PyBytes_Check(arg)) { |
| 1149 | size = PyBytes_GET_SIZE(s); |
| 1150 | ptr = PyBytes_AS_STRING(s); |
| 1151 | } |
| 1152 | else { |
| 1153 | size = PyByteArray_GET_SIZE(s); |
| 1154 | ptr = PyByteArray_AS_STRING(s); |
| 1155 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1156 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1157 | else if (PyUnicode_Check(arg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | /* Encode object; use default error handling */ |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1159 | s = PyUnicode_AsEncodedString(arg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | encoding, |
| 1161 | NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | if (s == NULL) |
| 1163 | return converterr("(encoding failed)", |
| 1164 | arg, msgbuf, bufsize); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1165 | assert(PyBytes_Check(s)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | size = PyBytes_GET_SIZE(s); |
| 1167 | ptr = PyBytes_AS_STRING(s); |
| 1168 | if (ptr == NULL) |
| 1169 | ptr = ""; |
| 1170 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1171 | else { |
| 1172 | return converterr( |
| 1173 | recode_strings ? "str" : "str, bytes or bytearray", |
| 1174 | arg, msgbuf, bufsize); |
| 1175 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | /* Write output; output is guaranteed to be 0-terminated */ |
| 1178 | if (*format == '#') { |
| 1179 | /* Using buffer length parameter '#': |
| 1180 | |
| 1181 | - if *buffer is NULL, a new buffer of the |
| 1182 | needed size is allocated and the data |
| 1183 | copied into it; *buffer is updated to point |
| 1184 | to the new buffer; the caller is |
| 1185 | responsible for PyMem_Free()ing it after |
| 1186 | usage |
| 1187 | |
| 1188 | - if *buffer is not NULL, the data is |
| 1189 | copied to *buffer; *buffer_len has to be |
| 1190 | set to the size of the buffer on input; |
| 1191 | buffer overflow is signalled with an error; |
| 1192 | buffer has to provide enough room for the |
| 1193 | encoded string plus the trailing 0-byte |
| 1194 | |
| 1195 | - in both cases, *buffer_len is updated to |
| 1196 | the size of the buffer /excluding/ the |
| 1197 | trailing 0-byte |
| 1198 | |
| 1199 | */ |
| 1200 | FETCH_SIZE; |
| 1201 | |
| 1202 | format++; |
| 1203 | if (q == NULL && q2 == NULL) { |
| 1204 | Py_DECREF(s); |
| 1205 | return converterr( |
| 1206 | "(buffer_len is NULL)", |
| 1207 | arg, msgbuf, bufsize); |
| 1208 | } |
| 1209 | if (*buffer == NULL) { |
| 1210 | *buffer = PyMem_NEW(char, size + 1); |
| 1211 | if (*buffer == NULL) { |
| 1212 | Py_DECREF(s); |
Victor Stinner | 2872e5b | 2010-06-06 20:38:02 +0000 | [diff] [blame] | 1213 | PyErr_NoMemory(); |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1214 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1216 | if (addcleanup(*buffer, freelist, cleanup_ptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | Py_DECREF(s); |
| 1218 | return converterr( |
| 1219 | "(cleanup problem)", |
| 1220 | arg, msgbuf, bufsize); |
| 1221 | } |
| 1222 | } else { |
| 1223 | if (size + 1 > BUFFER_LEN) { |
| 1224 | Py_DECREF(s); |
Serhiy Storchaka | 4cd63ef | 2016-02-08 01:22:47 +0200 | [diff] [blame] | 1225 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 1226 | "encoded string too long " |
| 1227 | "(%zd, maximum length %zd)", |
| 1228 | (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1)); |
| 1229 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | } |
| 1231 | } |
| 1232 | memcpy(*buffer, ptr, size+1); |
| 1233 | STORE_SIZE(size); |
| 1234 | } else { |
| 1235 | /* Using a 0-terminated buffer: |
| 1236 | |
| 1237 | - the encoded string has to be 0-terminated |
| 1238 | for this variant to work; if it is not, an |
| 1239 | error raised |
| 1240 | |
| 1241 | - a new buffer of the needed size is |
| 1242 | allocated and the data copied into it; |
| 1243 | *buffer is updated to point to the new |
| 1244 | buffer; the caller is responsible for |
| 1245 | PyMem_Free()ing it after usage |
| 1246 | |
| 1247 | */ |
| 1248 | if ((Py_ssize_t)strlen(ptr) != size) { |
| 1249 | Py_DECREF(s); |
| 1250 | return converterr( |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 1251 | "encoded string without null bytes", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1252 | arg, msgbuf, bufsize); |
| 1253 | } |
| 1254 | *buffer = PyMem_NEW(char, size + 1); |
| 1255 | if (*buffer == NULL) { |
| 1256 | Py_DECREF(s); |
Victor Stinner | 2872e5b | 2010-06-06 20:38:02 +0000 | [diff] [blame] | 1257 | PyErr_NoMemory(); |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1258 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1260 | if (addcleanup(*buffer, freelist, cleanup_ptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | Py_DECREF(s); |
| 1262 | return converterr("(cleanup problem)", |
| 1263 | arg, msgbuf, bufsize); |
| 1264 | } |
| 1265 | memcpy(*buffer, ptr, size+1); |
| 1266 | } |
| 1267 | Py_DECREF(s); |
| 1268 | break; |
| 1269 | } |
| 1270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | case 'S': { /* PyBytes object */ |
| 1272 | PyObject **p = va_arg(*p_va, PyObject **); |
| 1273 | if (PyBytes_Check(arg)) |
| 1274 | *p = arg; |
| 1275 | else |
| 1276 | return converterr("bytes", arg, msgbuf, bufsize); |
| 1277 | break; |
| 1278 | } |
| 1279 | |
| 1280 | case 'Y': { /* PyByteArray object */ |
| 1281 | PyObject **p = va_arg(*p_va, PyObject **); |
| 1282 | if (PyByteArray_Check(arg)) |
| 1283 | *p = arg; |
| 1284 | else |
Victor Stinner | 5216e6d | 2010-06-08 21:45:51 +0000 | [diff] [blame] | 1285 | return converterr("bytearray", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | break; |
| 1287 | } |
| 1288 | |
| 1289 | case 'U': { /* PyUnicode object */ |
| 1290 | PyObject **p = va_arg(*p_va, PyObject **); |
Victor Stinner | a1b0c9f | 2012-05-29 12:30:29 +0200 | [diff] [blame] | 1291 | if (PyUnicode_Check(arg)) { |
| 1292 | if (PyUnicode_READY(arg) == -1) |
| 1293 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | *p = arg; |
Victor Stinner | a1b0c9f | 2012-05-29 12:30:29 +0200 | [diff] [blame] | 1295 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | else |
| 1297 | return converterr("str", arg, msgbuf, bufsize); |
| 1298 | break; |
| 1299 | } |
| 1300 | |
| 1301 | case 'O': { /* object */ |
| 1302 | PyTypeObject *type; |
| 1303 | PyObject **p; |
| 1304 | if (*format == '!') { |
| 1305 | type = va_arg(*p_va, PyTypeObject*); |
| 1306 | p = va_arg(*p_va, PyObject **); |
| 1307 | format++; |
| 1308 | if (PyType_IsSubtype(arg->ob_type, type)) |
| 1309 | *p = arg; |
| 1310 | else |
| 1311 | return converterr(type->tp_name, arg, msgbuf, bufsize); |
| 1312 | |
| 1313 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | else if (*format == '&') { |
| 1315 | typedef int (*converter)(PyObject *, void *); |
| 1316 | converter convert = va_arg(*p_va, converter); |
| 1317 | void *addr = va_arg(*p_va, void *); |
| 1318 | int res; |
| 1319 | format++; |
| 1320 | if (! (res = (*convert)(arg, addr))) |
| 1321 | return converterr("(unspecified)", |
| 1322 | arg, msgbuf, bufsize); |
| 1323 | if (res == Py_CLEANUP_SUPPORTED && |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1324 | addcleanup(addr, freelist, convert) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | return converterr("(cleanup problem)", |
| 1326 | arg, msgbuf, bufsize); |
| 1327 | } |
| 1328 | else { |
| 1329 | p = va_arg(*p_va, PyObject **); |
| 1330 | *p = arg; |
| 1331 | } |
| 1332 | break; |
| 1333 | } |
| 1334 | |
| 1335 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1336 | case 'w': { /* "w*": memory buffer, read-write access */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | void **p = va_arg(*p_va, void **); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1338 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1339 | if (*format != '*') |
| 1340 | return converterr( |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 1341 | "(invalid use of 'w' format character)", |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1342 | arg, msgbuf, bufsize); |
| 1343 | format++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1344 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1345 | /* Caller is interested in Py_buffer, and the object |
| 1346 | supports it directly. */ |
| 1347 | if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) { |
| 1348 | PyErr_Clear(); |
R David Murray | 861470c | 2014-10-05 11:47:01 -0400 | [diff] [blame] | 1349 | return converterr("read-write bytes-like object", |
| 1350 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | } |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1352 | if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) { |
| 1353 | PyBuffer_Release((Py_buffer*)p); |
| 1354 | return converterr("contiguous buffer", arg, msgbuf, bufsize); |
| 1355 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1356 | if (addcleanup(p, freelist, cleanup_buffer)) { |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1357 | return converterr( |
| 1358 | "(cleanup problem)", |
| 1359 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | break; |
| 1362 | } |
| 1363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1364 | default: |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 1365 | return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | |
| 1367 | } |
| 1368 | |
| 1369 | *p_format = format; |
| 1370 | return NULL; |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1371 | |
| 1372 | #undef FETCH_SIZE |
| 1373 | #undef STORE_SIZE |
| 1374 | #undef BUFFER_LEN |
| 1375 | #undef RETURN_ERR_OCCURRED |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 1376 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1377 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1378 | static Py_ssize_t |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1379 | convertbuffer(PyObject *arg, const void **p, const char **errmsg) |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1380 | { |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1381 | PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1382 | Py_ssize_t count; |
| 1383 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | *errmsg = NULL; |
| 1386 | *p = NULL; |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1387 | if (pb != NULL && pb->bf_releasebuffer != NULL) { |
R David Murray | 861470c | 2014-10-05 11:47:01 -0400 | [diff] [blame] | 1388 | *errmsg = "read-only bytes-like object"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | return -1; |
| 1390 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1391 | |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1392 | if (getbuffer(arg, &view, errmsg) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | count = view.len; |
| 1395 | *p = view.buf; |
| 1396 | PyBuffer_Release(&view); |
| 1397 | return count; |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1398 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1399 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1400 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1401 | getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1402 | { |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1403 | if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) { |
R David Murray | 861470c | 2014-10-05 11:47:01 -0400 | [diff] [blame] | 1404 | *errmsg = "bytes-like object"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | return -1; |
| 1406 | } |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1407 | if (!PyBuffer_IsContiguous(view, 'C')) { |
Victor Stinner | 21e0948 | 2010-06-24 22:57:10 +0000 | [diff] [blame] | 1408 | PyBuffer_Release(view); |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1409 | *errmsg = "contiguous buffer"; |
| 1410 | return -1; |
| 1411 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | return 0; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1415 | /* Support for keyword arguments donated by |
| 1416 | Geoff Philbrick <philbric@delphi.hks.com> */ |
| 1417 | |
Tim Peters | f8cd3e8 | 2001-10-27 04:26:57 +0000 | [diff] [blame] | 1418 | /* Return false (0) for error, else true. */ |
Fred Drake | 563dfc2 | 2001-10-23 14:41:08 +0000 | [diff] [blame] | 1419 | int |
| 1420 | PyArg_ParseTupleAndKeywords(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | PyObject *keywords, |
| 1422 | const char *format, |
| 1423 | char **kwlist, ...) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1425 | int retval; |
| 1426 | va_list va; |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1429 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1430 | format == NULL || |
| 1431 | kwlist == NULL) |
| 1432 | { |
| 1433 | PyErr_BadInternalCall(); |
| 1434 | return 0; |
| 1435 | } |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | va_start(va, kwlist); |
| 1438 | retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0); |
| 1439 | va_end(va); |
| 1440 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | int |
| 1444 | _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | PyObject *keywords, |
| 1446 | const char *format, |
| 1447 | char **kwlist, ...) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1448 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1449 | int retval; |
| 1450 | va_list va; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1453 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1454 | format == NULL || |
| 1455 | kwlist == NULL) |
| 1456 | { |
| 1457 | PyErr_BadInternalCall(); |
| 1458 | return 0; |
| 1459 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1461 | va_start(va, kwlist); |
| 1462 | retval = vgetargskeywords(args, keywords, format, |
| 1463 | kwlist, &va, FLAG_SIZE_T); |
| 1464 | va_end(va); |
| 1465 | return retval; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1469 | int |
| 1470 | PyArg_VaParseTupleAndKeywords(PyObject *args, |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1471 | PyObject *keywords, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1472 | const char *format, |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1473 | char **kwlist, va_list va) |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1474 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | int retval; |
| 1476 | va_list lva; |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1478 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1479 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1480 | format == NULL || |
| 1481 | kwlist == NULL) |
| 1482 | { |
| 1483 | PyErr_BadInternalCall(); |
| 1484 | return 0; |
| 1485 | } |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1486 | |
Benjamin Peterson | 0c21214 | 2016-09-20 20:39:33 -0700 | [diff] [blame] | 1487 | va_copy(lva, va); |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0); |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 1490 | va_end(lva); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | int |
| 1495 | _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | PyObject *keywords, |
| 1497 | const char *format, |
| 1498 | char **kwlist, va_list va) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1499 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | int retval; |
| 1501 | va_list lva; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1504 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1505 | format == NULL || |
| 1506 | kwlist == NULL) |
| 1507 | { |
| 1508 | PyErr_BadInternalCall(); |
| 1509 | return 0; |
| 1510 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1511 | |
Benjamin Peterson | 0c21214 | 2016-09-20 20:39:33 -0700 | [diff] [blame] | 1512 | va_copy(lva, va); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | retval = vgetargskeywords(args, keywords, format, |
| 1515 | kwlist, &lva, FLAG_SIZE_T); |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 1516 | va_end(lva); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | return retval; |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 1520 | int |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1521 | _PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords, |
| 1522 | struct _PyArg_Parser *parser, ...) |
| 1523 | { |
| 1524 | int retval; |
| 1525 | va_list va; |
| 1526 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1527 | va_start(va, parser); |
| 1528 | retval = vgetargskeywordsfast(args, keywords, parser, &va, 0); |
| 1529 | va_end(va); |
| 1530 | return retval; |
| 1531 | } |
| 1532 | |
| 1533 | int |
| 1534 | _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords, |
| 1535 | struct _PyArg_Parser *parser, ...) |
| 1536 | { |
| 1537 | int retval; |
| 1538 | va_list va; |
| 1539 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1540 | va_start(va, parser); |
| 1541 | retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T); |
| 1542 | va_end(va); |
| 1543 | return retval; |
| 1544 | } |
| 1545 | |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 1546 | int |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1547 | _PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 1548 | struct _PyArg_Parser *parser, ...) |
| 1549 | { |
| 1550 | int retval; |
| 1551 | va_list va; |
| 1552 | |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 1553 | va_start(va, parser); |
| 1554 | retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0); |
| 1555 | va_end(va); |
| 1556 | return retval; |
| 1557 | } |
| 1558 | |
| 1559 | int |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1560 | _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 1561 | struct _PyArg_Parser *parser, ...) |
| 1562 | { |
| 1563 | int retval; |
| 1564 | va_list va; |
| 1565 | |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 1566 | va_start(va, parser); |
| 1567 | retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T); |
| 1568 | va_end(va); |
| 1569 | return retval; |
| 1570 | } |
| 1571 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1572 | |
| 1573 | int |
| 1574 | _PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords, |
| 1575 | struct _PyArg_Parser *parser, va_list va) |
| 1576 | { |
| 1577 | int retval; |
| 1578 | va_list lva; |
| 1579 | |
Benjamin Peterson | 0c21214 | 2016-09-20 20:39:33 -0700 | [diff] [blame] | 1580 | va_copy(lva, va); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1581 | |
| 1582 | retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0); |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 1583 | va_end(lva); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1584 | return retval; |
| 1585 | } |
| 1586 | |
| 1587 | int |
| 1588 | _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords, |
| 1589 | struct _PyArg_Parser *parser, va_list va) |
| 1590 | { |
| 1591 | int retval; |
| 1592 | va_list lva; |
| 1593 | |
Benjamin Peterson | 0c21214 | 2016-09-20 20:39:33 -0700 | [diff] [blame] | 1594 | va_copy(lva, va); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1595 | |
| 1596 | retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T); |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 1597 | va_end(lva); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1598 | return retval; |
| 1599 | } |
| 1600 | |
| 1601 | int |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 1602 | PyArg_ValidateKeywordArguments(PyObject *kwargs) |
| 1603 | { |
Benjamin Peterson | f609654 | 2010-11-17 22:33:12 +0000 | [diff] [blame] | 1604 | if (!PyDict_Check(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | PyErr_BadInternalCall(); |
| 1606 | return 0; |
| 1607 | } |
| 1608 | if (!_PyDict_HasOnlyStringKeys(kwargs)) { |
| 1609 | PyErr_SetString(PyExc_TypeError, |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1610 | "keywords must be strings"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1611 | return 0; |
| 1612 | } |
| 1613 | return 1; |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1616 | #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1617 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1618 | static int |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1619 | vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | char **kwlist, va_list *p_va, int flags) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | char msgbuf[512]; |
| 1623 | int levels[32]; |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1624 | const char *fname, *msg, *custom_msg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | int min = INT_MAX; |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1626 | int max = INT_MAX; |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1627 | int i, pos, len; |
| 1628 | int skip = 0; |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1629 | Py_ssize_t nargs, nkwargs; |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1630 | PyObject *current_arg; |
Antoine Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 1631 | freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
Benjamin Peterson | 40be9e5 | 2014-02-11 10:09:27 -0500 | [diff] [blame] | 1632 | freelist_t freelist; |
| 1633 | |
| 1634 | freelist.entries = static_entries; |
| 1635 | freelist.first_available = 0; |
| 1636 | freelist.entries_malloced = 0; |
Tim Peters | f4331c1 | 2001-10-27 00:17:34 +0000 | [diff] [blame] | 1637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | assert(args != NULL && PyTuple_Check(args)); |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1639 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1640 | assert(format != NULL); |
| 1641 | assert(kwlist != NULL); |
| 1642 | assert(p_va != NULL); |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1644 | /* grab the function name or custom error msg first (mutually exclusive) */ |
| 1645 | fname = strchr(format, ':'); |
| 1646 | if (fname) { |
| 1647 | fname++; |
| 1648 | custom_msg = NULL; |
| 1649 | } |
| 1650 | else { |
| 1651 | custom_msg = strchr(format,';'); |
| 1652 | if (custom_msg) |
| 1653 | custom_msg++; |
| 1654 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1655 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1656 | /* scan kwlist and count the number of positional-only parameters */ |
| 1657 | for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) { |
| 1658 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | /* scan kwlist and get greatest possible nbr of args */ |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1660 | for (len = pos; kwlist[len]; len++) { |
| 1661 | if (!*kwlist[len]) { |
| 1662 | PyErr_SetString(PyExc_SystemError, |
| 1663 | "Empty keyword parameter name"); |
| 1664 | return cleanreturn(0, &freelist); |
| 1665 | } |
| 1666 | } |
Tim Peters | f8cd3e8 | 2001-10-27 04:26:57 +0000 | [diff] [blame] | 1667 | |
Antoine Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 1668 | if (len > STATIC_FREELIST_ENTRIES) { |
| 1669 | freelist.entries = PyMem_NEW(freelistentry_t, len); |
| 1670 | if (freelist.entries == NULL) { |
| 1671 | PyErr_NoMemory(); |
| 1672 | return 0; |
| 1673 | } |
| 1674 | freelist.entries_malloced = 1; |
Benjamin Peterson | 7ed6727 | 2012-03-16 12:21:02 -0500 | [diff] [blame] | 1675 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | nargs = PyTuple_GET_SIZE(args); |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1678 | nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs); |
| 1679 | if (nargs + nkwargs > len) { |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 1680 | /* Adding "keyword" (when nargs == 0) prevents producing wrong error |
| 1681 | messages in some special cases (see bpo-31229). */ |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1682 | PyErr_Format(PyExc_TypeError, |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 1683 | "%.200s%s takes at most %d %sargument%s (%zd given)", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1684 | (fname == NULL) ? "function" : fname, |
| 1685 | (fname == NULL) ? "" : "()", |
| 1686 | len, |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 1687 | (nargs == 0) ? "keyword " : "", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | (len == 1) ? "" : "s", |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1689 | nargs + nkwargs); |
Benjamin Peterson | 01feaec | 2012-03-16 13:25:58 -0500 | [diff] [blame] | 1690 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1691 | } |
Tim Peters | c2f0112 | 2001-10-27 07:25:06 +0000 | [diff] [blame] | 1692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | /* convert tuple args and keyword args in same loop, using kwlist to drive process */ |
| 1694 | for (i = 0; i < len; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1695 | if (*format == '|') { |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1696 | if (min != INT_MAX) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1697 | PyErr_SetString(PyExc_SystemError, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1698 | "Invalid format string (| specified twice)"); |
| 1699 | return cleanreturn(0, &freelist); |
| 1700 | } |
| 1701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | min = i; |
| 1703 | format++; |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1704 | |
| 1705 | if (max != INT_MAX) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1706 | PyErr_SetString(PyExc_SystemError, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1707 | "Invalid format string ($ before |)"); |
| 1708 | return cleanreturn(0, &freelist); |
| 1709 | } |
| 1710 | } |
| 1711 | if (*format == '$') { |
| 1712 | if (max != INT_MAX) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1713 | PyErr_SetString(PyExc_SystemError, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1714 | "Invalid format string ($ specified twice)"); |
| 1715 | return cleanreturn(0, &freelist); |
| 1716 | } |
| 1717 | |
| 1718 | max = i; |
| 1719 | format++; |
| 1720 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1721 | if (max < pos) { |
| 1722 | PyErr_SetString(PyExc_SystemError, |
| 1723 | "Empty parameter name after $"); |
| 1724 | return cleanreturn(0, &freelist); |
| 1725 | } |
| 1726 | if (skip) { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1727 | /* Now we know the minimal and the maximal numbers of |
| 1728 | * positional arguments and can raise an exception with |
| 1729 | * informative message (see below). */ |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1730 | break; |
| 1731 | } |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1732 | if (max < nargs) { |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 1733 | if (max == 0) { |
| 1734 | PyErr_Format(PyExc_TypeError, |
| 1735 | "%.200s%s takes no positional arguments", |
| 1736 | (fname == NULL) ? "function" : fname, |
| 1737 | (fname == NULL) ? "" : "()"); |
| 1738 | } |
| 1739 | else { |
| 1740 | PyErr_Format(PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 1741 | "%.200s%s takes %s %d positional argument%s" |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 1742 | " (%d given)", |
| 1743 | (fname == NULL) ? "function" : fname, |
| 1744 | (fname == NULL) ? "" : "()", |
| 1745 | (min != INT_MAX) ? "at most" : "exactly", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 1746 | max, |
| 1747 | max == 1 ? "" : "s", |
| 1748 | nargs); |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 1749 | } |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1750 | return cleanreturn(0, &freelist); |
| 1751 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | } |
| 1753 | if (IS_END_OF_FORMAT(*format)) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1754 | PyErr_Format(PyExc_SystemError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1755 | "More keyword list entries (%d) than " |
| 1756 | "format specifiers (%d)", len, i); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1757 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1758 | } |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1759 | if (!skip) { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1760 | if (i < nargs) { |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1761 | current_arg = PyTuple_GET_ITEM(args, i); |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1762 | } |
| 1763 | else if (nkwargs && i >= pos) { |
| 1764 | current_arg = PyDict_GetItemString(kwargs, kwlist[i]); |
| 1765 | if (current_arg) |
| 1766 | --nkwargs; |
| 1767 | } |
| 1768 | else { |
| 1769 | current_arg = NULL; |
| 1770 | } |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1771 | |
| 1772 | if (current_arg) { |
| 1773 | msg = convertitem(current_arg, &format, p_va, flags, |
| 1774 | levels, msgbuf, sizeof(msgbuf), &freelist); |
| 1775 | if (msg) { |
| 1776 | seterror(i+1, msg, levels, fname, custom_msg); |
| 1777 | return cleanreturn(0, &freelist); |
| 1778 | } |
| 1779 | continue; |
| 1780 | } |
| 1781 | |
| 1782 | if (i < min) { |
| 1783 | if (i < pos) { |
| 1784 | assert (min == INT_MAX); |
| 1785 | assert (max == INT_MAX); |
| 1786 | skip = 1; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1787 | /* At that moment we still don't know the minimal and |
| 1788 | * the maximal numbers of positional arguments. Raising |
| 1789 | * an exception is deferred until we encounter | and $ |
| 1790 | * or the end of the format. */ |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1791 | } |
| 1792 | else { |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1793 | PyErr_Format(PyExc_TypeError, "%.200s%s missing required " |
| 1794 | "argument '%s' (pos %d)", |
| 1795 | (fname == NULL) ? "function" : fname, |
| 1796 | (fname == NULL) ? "" : "()", |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1797 | kwlist[i], i+1); |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1798 | return cleanreturn(0, &freelist); |
| 1799 | } |
| 1800 | } |
| 1801 | /* current code reports success when all required args |
| 1802 | * fulfilled and no keyword args left, with no further |
| 1803 | * validation. XXX Maybe skip this in debug build ? |
| 1804 | */ |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1805 | if (!nkwargs && !skip) { |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1806 | return cleanreturn(1, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1807 | } |
| 1808 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1809 | |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 1810 | /* We are into optional args, skip through to any remaining |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | * keyword args */ |
| 1812 | msg = skipitem(&format, p_va, flags); |
| 1813 | if (msg) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1814 | PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1815 | format); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1816 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1817 | } |
| 1818 | } |
Tim Peters | b054be4 | 2001-10-27 05:07:41 +0000 | [diff] [blame] | 1819 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1820 | if (skip) { |
| 1821 | PyErr_Format(PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 1822 | "%.200s%s takes %s %d positional argument%s" |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1823 | " (%d given)", |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1824 | (fname == NULL) ? "function" : fname, |
| 1825 | (fname == NULL) ? "" : "()", |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1826 | (Py_MIN(pos, min) < i) ? "at least" : "exactly", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 1827 | Py_MIN(pos, min), |
| 1828 | Py_MIN(pos, min) == 1 ? "" : "s", |
| 1829 | nargs); |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1830 | return cleanreturn(0, &freelist); |
| 1831 | } |
| 1832 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1833 | if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1834 | PyErr_Format(PyExc_SystemError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | "more argument specifiers than keyword list entries " |
| 1836 | "(remaining format:'%s')", format); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1837 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | } |
Tim Peters | c2f0112 | 2001-10-27 07:25:06 +0000 | [diff] [blame] | 1839 | |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1840 | if (nkwargs > 0) { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1841 | PyObject *key; |
| 1842 | Py_ssize_t j; |
| 1843 | /* make sure there are no arguments given by name and position */ |
| 1844 | for (i = pos; i < nargs; i++) { |
| 1845 | current_arg = PyDict_GetItemString(kwargs, kwlist[i]); |
| 1846 | if (current_arg) { |
| 1847 | /* arg present in tuple and in dict */ |
| 1848 | PyErr_Format(PyExc_TypeError, |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1849 | "argument for %.200s%s given by name ('%s') " |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1850 | "and position (%d)", |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1851 | (fname == NULL) ? "function" : fname, |
| 1852 | (fname == NULL) ? "" : "()", |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1853 | kwlist[i], i+1); |
| 1854 | return cleanreturn(0, &freelist); |
| 1855 | } |
| 1856 | } |
| 1857 | /* make sure there are no extraneous keyword arguments */ |
| 1858 | j = 0; |
| 1859 | while (PyDict_Next(kwargs, &j, &key, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | int match = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | if (!PyUnicode_Check(key)) { |
| 1862 | PyErr_SetString(PyExc_TypeError, |
| 1863 | "keywords must be strings"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1864 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1866 | for (i = pos; i < len; i++) { |
| 1867 | if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) { |
Antoine Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 1868 | match = 1; |
| 1869 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | if (!match) { |
| 1873 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 1874 | "'%U' is an invalid keyword " |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1875 | "argument for %.200s%s", |
| 1876 | key, |
| 1877 | (fname == NULL) ? "this function" : fname, |
| 1878 | (fname == NULL) ? "" : "()"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1879 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1884 | return cleanreturn(1, &freelist); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1888 | /* List of static parsers. */ |
| 1889 | static struct _PyArg_Parser *static_arg_parsers = NULL; |
| 1890 | |
| 1891 | static int |
| 1892 | parser_init(struct _PyArg_Parser *parser) |
| 1893 | { |
| 1894 | const char * const *keywords; |
| 1895 | const char *format, *msg; |
| 1896 | int i, len, min, max, nkw; |
| 1897 | PyObject *kwtuple; |
| 1898 | |
| 1899 | assert(parser->format != NULL); |
| 1900 | assert(parser->keywords != NULL); |
| 1901 | if (parser->kwtuple != NULL) { |
| 1902 | return 1; |
| 1903 | } |
| 1904 | |
| 1905 | /* grab the function name or custom error msg first (mutually exclusive) */ |
| 1906 | parser->fname = strchr(parser->format, ':'); |
| 1907 | if (parser->fname) { |
| 1908 | parser->fname++; |
| 1909 | parser->custom_msg = NULL; |
| 1910 | } |
| 1911 | else { |
| 1912 | parser->custom_msg = strchr(parser->format,';'); |
| 1913 | if (parser->custom_msg) |
| 1914 | parser->custom_msg++; |
| 1915 | } |
| 1916 | |
| 1917 | keywords = parser->keywords; |
| 1918 | /* scan keywords and count the number of positional-only parameters */ |
| 1919 | for (i = 0; keywords[i] && !*keywords[i]; i++) { |
| 1920 | } |
| 1921 | parser->pos = i; |
| 1922 | /* scan keywords and get greatest possible nbr of args */ |
| 1923 | for (; keywords[i]; i++) { |
| 1924 | if (!*keywords[i]) { |
| 1925 | PyErr_SetString(PyExc_SystemError, |
| 1926 | "Empty keyword parameter name"); |
| 1927 | return 0; |
| 1928 | } |
| 1929 | } |
| 1930 | len = i; |
| 1931 | |
| 1932 | min = max = INT_MAX; |
| 1933 | format = parser->format; |
| 1934 | for (i = 0; i < len; i++) { |
| 1935 | if (*format == '|') { |
| 1936 | if (min != INT_MAX) { |
| 1937 | PyErr_SetString(PyExc_SystemError, |
| 1938 | "Invalid format string (| specified twice)"); |
| 1939 | return 0; |
| 1940 | } |
| 1941 | if (max != INT_MAX) { |
| 1942 | PyErr_SetString(PyExc_SystemError, |
| 1943 | "Invalid format string ($ before |)"); |
| 1944 | return 0; |
| 1945 | } |
| 1946 | min = i; |
| 1947 | format++; |
| 1948 | } |
| 1949 | if (*format == '$') { |
| 1950 | if (max != INT_MAX) { |
| 1951 | PyErr_SetString(PyExc_SystemError, |
| 1952 | "Invalid format string ($ specified twice)"); |
| 1953 | return 0; |
| 1954 | } |
| 1955 | if (i < parser->pos) { |
| 1956 | PyErr_SetString(PyExc_SystemError, |
| 1957 | "Empty parameter name after $"); |
| 1958 | return 0; |
| 1959 | } |
| 1960 | max = i; |
| 1961 | format++; |
| 1962 | } |
| 1963 | if (IS_END_OF_FORMAT(*format)) { |
| 1964 | PyErr_Format(PyExc_SystemError, |
| 1965 | "More keyword list entries (%d) than " |
| 1966 | "format specifiers (%d)", len, i); |
| 1967 | return 0; |
| 1968 | } |
| 1969 | |
| 1970 | msg = skipitem(&format, NULL, 0); |
| 1971 | if (msg) { |
| 1972 | PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, |
| 1973 | format); |
| 1974 | return 0; |
| 1975 | } |
| 1976 | } |
| 1977 | parser->min = Py_MIN(min, len); |
| 1978 | parser->max = Py_MIN(max, len); |
| 1979 | |
| 1980 | if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { |
| 1981 | PyErr_Format(PyExc_SystemError, |
| 1982 | "more argument specifiers than keyword list entries " |
| 1983 | "(remaining format:'%s')", format); |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
| 1987 | nkw = len - parser->pos; |
| 1988 | kwtuple = PyTuple_New(nkw); |
| 1989 | if (kwtuple == NULL) { |
| 1990 | return 0; |
| 1991 | } |
| 1992 | keywords = parser->keywords + parser->pos; |
| 1993 | for (i = 0; i < nkw; i++) { |
| 1994 | PyObject *str = PyUnicode_FromString(keywords[i]); |
| 1995 | if (str == NULL) { |
| 1996 | Py_DECREF(kwtuple); |
| 1997 | return 0; |
| 1998 | } |
| 1999 | PyUnicode_InternInPlace(&str); |
| 2000 | PyTuple_SET_ITEM(kwtuple, i, str); |
| 2001 | } |
| 2002 | parser->kwtuple = kwtuple; |
| 2003 | |
| 2004 | assert(parser->next == NULL); |
| 2005 | parser->next = static_arg_parsers; |
| 2006 | static_arg_parsers = parser; |
| 2007 | return 1; |
| 2008 | } |
| 2009 | |
| 2010 | static void |
| 2011 | parser_clear(struct _PyArg_Parser *parser) |
| 2012 | { |
| 2013 | Py_CLEAR(parser->kwtuple); |
| 2014 | } |
| 2015 | |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2016 | static PyObject* |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2017 | find_keyword(PyObject *kwargs, PyObject *kwnames, PyObject *const *kwstack, PyObject *key) |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2018 | { |
| 2019 | Py_ssize_t i, nkwargs; |
| 2020 | |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2021 | if (kwargs != NULL) { |
| 2022 | return PyDict_GetItem(kwargs, key); |
| 2023 | } |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2024 | nkwargs = PyTuple_GET_SIZE(kwnames); |
| 2025 | for (i=0; i < nkwargs; i++) { |
| 2026 | PyObject *kwname = PyTuple_GET_ITEM(kwnames, i); |
| 2027 | |
| 2028 | /* ptr==ptr should match in most cases since keyword keys |
| 2029 | should be interned strings */ |
| 2030 | if (kwname == key) { |
| 2031 | return kwstack[i]; |
| 2032 | } |
| 2033 | if (!PyUnicode_Check(kwname)) { |
| 2034 | /* ignore non-string keyword keys: |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2035 | an error will be raised below */ |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2036 | continue; |
| 2037 | } |
| 2038 | if (_PyUnicode_EQ(kwname, key)) { |
| 2039 | return kwstack[i]; |
| 2040 | } |
| 2041 | } |
| 2042 | return NULL; |
| 2043 | } |
| 2044 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2045 | static int |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2046 | vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2047 | PyObject *kwargs, PyObject *kwnames, |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2048 | struct _PyArg_Parser *parser, |
| 2049 | va_list *p_va, int flags) |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2050 | { |
| 2051 | PyObject *kwtuple; |
| 2052 | char msgbuf[512]; |
| 2053 | int levels[32]; |
| 2054 | const char *format; |
| 2055 | const char *msg; |
| 2056 | PyObject *keyword; |
| 2057 | int i, pos, len; |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2058 | Py_ssize_t nkwargs; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2059 | PyObject *current_arg; |
| 2060 | freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
| 2061 | freelist_t freelist; |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2062 | PyObject *const *kwstack = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2063 | |
| 2064 | freelist.entries = static_entries; |
| 2065 | freelist.first_available = 0; |
| 2066 | freelist.entries_malloced = 0; |
| 2067 | |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2068 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2069 | assert(kwargs == NULL || kwnames == NULL); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2070 | assert(p_va != NULL); |
| 2071 | |
Victor Stinner | 80ab22f | 2017-01-16 23:16:47 +0100 | [diff] [blame] | 2072 | if (parser == NULL) { |
| 2073 | PyErr_BadInternalCall(); |
| 2074 | return 0; |
| 2075 | } |
| 2076 | |
| 2077 | if (kwnames != NULL && !PyTuple_Check(kwnames)) { |
| 2078 | PyErr_BadInternalCall(); |
| 2079 | return 0; |
| 2080 | } |
| 2081 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2082 | if (!parser_init(parser)) { |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
| 2086 | kwtuple = parser->kwtuple; |
| 2087 | pos = parser->pos; |
Segev Finer | e7bc7aa | 2017-07-26 18:58:25 +0300 | [diff] [blame] | 2088 | len = pos + (int)PyTuple_GET_SIZE(kwtuple); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2089 | |
| 2090 | if (len > STATIC_FREELIST_ENTRIES) { |
| 2091 | freelist.entries = PyMem_NEW(freelistentry_t, len); |
| 2092 | if (freelist.entries == NULL) { |
| 2093 | PyErr_NoMemory(); |
| 2094 | return 0; |
| 2095 | } |
| 2096 | freelist.entries_malloced = 1; |
| 2097 | } |
| 2098 | |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2099 | if (kwargs != NULL) { |
| 2100 | nkwargs = PyDict_GET_SIZE(kwargs); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2101 | } |
| 2102 | else if (kwnames != NULL) { |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2103 | nkwargs = PyTuple_GET_SIZE(kwnames); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2104 | kwstack = args + nargs; |
| 2105 | } |
| 2106 | else { |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2107 | nkwargs = 0; |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2108 | } |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2109 | if (nargs + nkwargs > len) { |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 2110 | /* Adding "keyword" (when nargs == 0) prevents producing wrong error |
| 2111 | messages in some special cases (see bpo-31229). */ |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2112 | PyErr_Format(PyExc_TypeError, |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 2113 | "%.200s%s takes at most %d %sargument%s (%zd given)", |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2114 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2115 | (parser->fname == NULL) ? "" : "()", |
| 2116 | len, |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 2117 | (nargs == 0) ? "keyword " : "", |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2118 | (len == 1) ? "" : "s", |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2119 | nargs + nkwargs); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2120 | return cleanreturn(0, &freelist); |
| 2121 | } |
| 2122 | if (parser->max < nargs) { |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2123 | if (parser->max == 0) { |
| 2124 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | f9f1cca | 2017-06-09 19:27:06 +0300 | [diff] [blame] | 2125 | "%.200s%s takes no positional arguments", |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2126 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2127 | (parser->fname == NULL) ? "" : "()"); |
| 2128 | } |
| 2129 | else { |
| 2130 | PyErr_Format(PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2131 | "%.200s%s takes %s %d positional argument%s (%d given)", |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2132 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2133 | (parser->fname == NULL) ? "" : "()", |
| 2134 | (parser->min != INT_MAX) ? "at most" : "exactly", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2135 | parser->max, |
| 2136 | parser->max == 1 ? "" : "s", |
| 2137 | nargs); |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2138 | } |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2139 | return cleanreturn(0, &freelist); |
| 2140 | } |
| 2141 | |
| 2142 | format = parser->format; |
| 2143 | /* convert tuple args and keyword args in same loop, using kwtuple to drive process */ |
| 2144 | for (i = 0; i < len; i++) { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2145 | if (*format == '|') { |
| 2146 | format++; |
| 2147 | } |
| 2148 | if (*format == '$') { |
| 2149 | format++; |
| 2150 | } |
| 2151 | assert(!IS_END_OF_FORMAT(*format)); |
| 2152 | |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2153 | if (i < nargs) { |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2154 | current_arg = args[i]; |
Victor Stinner | a9efb2f | 2016-09-09 17:40:22 -0700 | [diff] [blame] | 2155 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2156 | else if (nkwargs && i >= pos) { |
| 2157 | keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
| 2158 | current_arg = find_keyword(kwargs, kwnames, kwstack, keyword); |
| 2159 | if (current_arg) |
| 2160 | --nkwargs; |
| 2161 | } |
| 2162 | else { |
| 2163 | current_arg = NULL; |
| 2164 | } |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2165 | |
| 2166 | if (current_arg) { |
| 2167 | msg = convertitem(current_arg, &format, p_va, flags, |
| 2168 | levels, msgbuf, sizeof(msgbuf), &freelist); |
| 2169 | if (msg) { |
| 2170 | seterror(i+1, msg, levels, parser->fname, parser->custom_msg); |
| 2171 | return cleanreturn(0, &freelist); |
| 2172 | } |
| 2173 | continue; |
| 2174 | } |
| 2175 | |
| 2176 | if (i < parser->min) { |
| 2177 | /* Less arguments than required */ |
| 2178 | if (i < pos) { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2179 | Py_ssize_t min = Py_MIN(pos, parser->min); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2180 | PyErr_Format(PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2181 | "%.200s%s takes %s %d positional argument%s" |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2182 | " (%d given)", |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 2183 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2184 | (parser->fname == NULL) ? "" : "()", |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2185 | min < parser->max ? "at least" : "exactly", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2186 | min, |
| 2187 | min == 1 ? "" : "s", |
| 2188 | nargs); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2189 | } |
| 2190 | else { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2191 | keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 2192 | PyErr_Format(PyExc_TypeError, "%.200s%s missing required " |
| 2193 | "argument '%U' (pos %d)", |
| 2194 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2195 | (parser->fname == NULL) ? "" : "()", |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2196 | keyword, i+1); |
| 2197 | } |
| 2198 | return cleanreturn(0, &freelist); |
| 2199 | } |
| 2200 | /* current code reports success when all required args |
| 2201 | * fulfilled and no keyword args left, with no further |
| 2202 | * validation. XXX Maybe skip this in debug build ? |
| 2203 | */ |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2204 | if (!nkwargs) { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2205 | return cleanreturn(1, &freelist); |
| 2206 | } |
| 2207 | |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 2208 | /* We are into optional args, skip through to any remaining |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2209 | * keyword args */ |
| 2210 | msg = skipitem(&format, p_va, flags); |
| 2211 | assert(msg == NULL); |
| 2212 | } |
| 2213 | |
| 2214 | assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$')); |
| 2215 | |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2216 | if (nkwargs > 0) { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2217 | Py_ssize_t j; |
| 2218 | /* make sure there are no arguments given by name and position */ |
| 2219 | for (i = pos; i < nargs; i++) { |
| 2220 | keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
| 2221 | current_arg = find_keyword(kwargs, kwnames, kwstack, keyword); |
| 2222 | if (current_arg) { |
| 2223 | /* arg present in tuple and in dict */ |
| 2224 | PyErr_Format(PyExc_TypeError, |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 2225 | "argument for %.200s%s given by name ('%U') " |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2226 | "and position (%d)", |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 2227 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2228 | (parser->fname == NULL) ? "" : "()", |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2229 | keyword, i+1); |
| 2230 | return cleanreturn(0, &freelist); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2231 | } |
| 2232 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2233 | /* make sure there are no extraneous keyword arguments */ |
| 2234 | j = 0; |
| 2235 | while (1) { |
| 2236 | int match; |
| 2237 | if (kwargs != NULL) { |
| 2238 | if (!PyDict_Next(kwargs, &j, &keyword, NULL)) |
| 2239 | break; |
| 2240 | } |
| 2241 | else { |
| 2242 | if (j >= PyTuple_GET_SIZE(kwnames)) |
| 2243 | break; |
| 2244 | keyword = PyTuple_GET_ITEM(kwnames, j); |
| 2245 | j++; |
| 2246 | } |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2247 | |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2248 | if (!PyUnicode_Check(keyword)) { |
| 2249 | PyErr_SetString(PyExc_TypeError, |
| 2250 | "keywords must be strings"); |
| 2251 | return cleanreturn(0, &freelist); |
| 2252 | } |
| 2253 | match = PySequence_Contains(kwtuple, keyword); |
| 2254 | if (match <= 0) { |
| 2255 | if (!match) { |
| 2256 | PyErr_Format(PyExc_TypeError, |
| 2257 | "'%U' is an invalid keyword " |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 2258 | "argument for %.200s%s", |
| 2259 | keyword, |
| 2260 | (parser->fname == NULL) ? "this function" : parser->fname, |
| 2261 | (parser->fname == NULL) ? "" : "()"); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2262 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2263 | return cleanreturn(0, &freelist); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | return cleanreturn(1, &freelist); |
| 2269 | } |
| 2270 | |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2271 | static int |
| 2272 | vgetargskeywordsfast(PyObject *args, PyObject *keywords, |
| 2273 | struct _PyArg_Parser *parser, va_list *p_va, int flags) |
| 2274 | { |
| 2275 | PyObject **stack; |
| 2276 | Py_ssize_t nargs; |
| 2277 | |
Victor Stinner | 80ab22f | 2017-01-16 23:16:47 +0100 | [diff] [blame] | 2278 | if (args == NULL |
| 2279 | || !PyTuple_Check(args) |
| 2280 | || (keywords != NULL && !PyDict_Check(keywords))) |
| 2281 | { |
| 2282 | PyErr_BadInternalCall(); |
| 2283 | return 0; |
| 2284 | } |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2285 | |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 2286 | stack = _PyTuple_ITEMS(args); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2287 | nargs = PyTuple_GET_SIZE(args); |
| 2288 | return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL, |
| 2289 | parser, p_va, flags); |
| 2290 | } |
| 2291 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2292 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2293 | static const char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2294 | skipitem(const char **p_format, va_list *p_va, int flags) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 2295 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | const char *format = *p_format; |
| 2297 | char c = *format++; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | switch (c) { |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 2300 | |
Larry Hastings | a347901 | 2012-05-08 23:52:03 -0700 | [diff] [blame] | 2301 | /* |
| 2302 | * codes that take a single data pointer as an argument |
| 2303 | * (the type of the pointer is irrelevant) |
| 2304 | */ |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 2305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | case 'b': /* byte -- very short int */ |
| 2307 | case 'B': /* byte as bitfield */ |
| 2308 | case 'h': /* short int */ |
| 2309 | case 'H': /* short int as bitfield */ |
| 2310 | case 'i': /* int */ |
| 2311 | case 'I': /* int sized bitfield */ |
| 2312 | case 'l': /* long int */ |
| 2313 | case 'k': /* long int sized bitfield */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 2314 | case 'L': /* long long */ |
| 2315 | case 'K': /* long long sized bitfield */ |
Larry Hastings | a347901 | 2012-05-08 23:52:03 -0700 | [diff] [blame] | 2316 | case 'n': /* Py_ssize_t */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | case 'f': /* float */ |
| 2318 | case 'd': /* double */ |
| 2319 | case 'D': /* complex double */ |
| 2320 | case 'c': /* char */ |
| 2321 | case 'C': /* unicode char */ |
Larry Hastings | 10ba07a | 2012-05-07 02:44:50 -0700 | [diff] [blame] | 2322 | case 'p': /* boolean predicate */ |
Larry Hastings | a347901 | 2012-05-08 23:52:03 -0700 | [diff] [blame] | 2323 | case 'S': /* string object */ |
| 2324 | case 'Y': /* string object */ |
| 2325 | case 'U': /* unicode string object */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2327 | if (p_va != NULL) { |
| 2328 | (void) va_arg(*p_va, void *); |
| 2329 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | break; |
| 2331 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2333 | /* string codes */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | case 'e': /* string with encoding */ |
| 2336 | { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2337 | if (p_va != NULL) { |
| 2338 | (void) va_arg(*p_va, const char *); |
| 2339 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | if (!(*format == 's' || *format == 't')) |
| 2341 | /* after 'e', only 's' and 't' is allowed */ |
| 2342 | goto err; |
| 2343 | format++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 2345 | /* fall through */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2347 | case 's': /* string */ |
| 2348 | case 'z': /* string or None */ |
| 2349 | case 'y': /* bytes */ |
| 2350 | case 'u': /* unicode string */ |
Larry Hastings | d9e4a41 | 2012-05-08 03:51:18 -0700 | [diff] [blame] | 2351 | case 'Z': /* unicode string or None */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2352 | case 'w': /* buffer, read-write */ |
| 2353 | { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2354 | if (p_va != NULL) { |
| 2355 | (void) va_arg(*p_va, char **); |
| 2356 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | if (*format == '#') { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2358 | if (p_va != NULL) { |
| 2359 | if (flags & FLAG_SIZE_T) |
| 2360 | (void) va_arg(*p_va, Py_ssize_t *); |
| 2361 | else |
| 2362 | (void) va_arg(*p_va, int *); |
| 2363 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2364 | format++; |
Serhiy Storchaka | 504373c | 2018-07-11 17:41:43 +0300 | [diff] [blame] | 2365 | } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w') |
| 2366 | && *format == '*') |
| 2367 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | format++; |
| 2369 | } |
| 2370 | break; |
| 2371 | } |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 2372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | case 'O': /* object */ |
| 2374 | { |
| 2375 | if (*format == '!') { |
| 2376 | format++; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2377 | if (p_va != NULL) { |
| 2378 | (void) va_arg(*p_va, PyTypeObject*); |
| 2379 | (void) va_arg(*p_va, PyObject **); |
| 2380 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2381 | } |
| 2382 | else if (*format == '&') { |
| 2383 | typedef int (*converter)(PyObject *, void *); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2384 | if (p_va != NULL) { |
| 2385 | (void) va_arg(*p_va, converter); |
| 2386 | (void) va_arg(*p_va, void *); |
| 2387 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | format++; |
| 2389 | } |
| 2390 | else { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2391 | if (p_va != NULL) { |
| 2392 | (void) va_arg(*p_va, PyObject **); |
| 2393 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2394 | } |
| 2395 | break; |
| 2396 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | case '(': /* bypass tuple, not handled at all previously */ |
| 2399 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2400 | const char *msg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2401 | for (;;) { |
| 2402 | if (*format==')') |
| 2403 | break; |
| 2404 | if (IS_END_OF_FORMAT(*format)) |
| 2405 | return "Unmatched left paren in format " |
| 2406 | "string"; |
| 2407 | msg = skipitem(&format, p_va, flags); |
| 2408 | if (msg) |
| 2409 | return msg; |
| 2410 | } |
| 2411 | format++; |
| 2412 | break; |
| 2413 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | case ')': |
| 2416 | return "Unmatched right paren in format string"; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2418 | default: |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 2419 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | return "impossible<bad format char>"; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2421 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2422 | } |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 2423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | *p_format = format; |
| 2425 | return NULL; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 2426 | } |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 2427 | |
| 2428 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2429 | #undef _PyArg_CheckPositional |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 2430 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2431 | int |
| 2432 | _PyArg_CheckPositional(const char *name, Py_ssize_t nargs, |
| 2433 | Py_ssize_t min, Py_ssize_t max) |
| 2434 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2435 | assert(min >= 0); |
| 2436 | assert(min <= max); |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2437 | |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2438 | if (nargs < min) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | if (name != NULL) |
| 2440 | PyErr_Format( |
| 2441 | PyExc_TypeError, |
Xtreak | 1426daa | 2018-07-23 01:43:26 +0530 | [diff] [blame] | 2442 | "%.200s expected %s%zd argument%s, got %zd", |
| 2443 | name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2444 | else |
| 2445 | PyErr_Format( |
| 2446 | PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2447 | "unpacked tuple should have %s%zd element%s," |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2448 | " but has %zd", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2449 | (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2450 | return 0; |
| 2451 | } |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2452 | |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2453 | if (nargs == 0) { |
Raymond Hettinger | 9423023 | 2016-03-26 03:02:48 -0700 | [diff] [blame] | 2454 | return 1; |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2455 | } |
| 2456 | |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2457 | if (nargs > max) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2458 | if (name != NULL) |
| 2459 | PyErr_Format( |
| 2460 | PyExc_TypeError, |
Xtreak | 1426daa | 2018-07-23 01:43:26 +0530 | [diff] [blame] | 2461 | "%.200s expected %s%zd argument%s, got %zd", |
| 2462 | name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2463 | else |
| 2464 | PyErr_Format( |
| 2465 | PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2466 | "unpacked tuple should have %s%zd element%s," |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2467 | " but has %zd", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2468 | (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2469 | return 0; |
| 2470 | } |
Raymond Hettinger | 9423023 | 2016-03-26 03:02:48 -0700 | [diff] [blame] | 2471 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2472 | return 1; |
| 2473 | } |
| 2474 | |
| 2475 | static int |
| 2476 | unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name, |
| 2477 | Py_ssize_t min, Py_ssize_t max, va_list vargs) |
| 2478 | { |
| 2479 | Py_ssize_t i; |
| 2480 | PyObject **o; |
| 2481 | |
| 2482 | if (!_PyArg_CheckPositional(name, nargs, min, max)) { |
| 2483 | return 0; |
| 2484 | } |
| 2485 | |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2486 | for (i = 0; i < nargs; i++) { |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2487 | o = va_arg(vargs, PyObject **); |
| 2488 | *o = args[i]; |
| 2489 | } |
| 2490 | return 1; |
| 2491 | } |
| 2492 | |
| 2493 | int |
| 2494 | PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) |
| 2495 | { |
| 2496 | PyObject **stack; |
| 2497 | Py_ssize_t nargs; |
| 2498 | int retval; |
| 2499 | va_list vargs; |
| 2500 | |
| 2501 | if (!PyTuple_Check(args)) { |
| 2502 | PyErr_SetString(PyExc_SystemError, |
| 2503 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 2504 | return 0; |
| 2505 | } |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 2506 | stack = _PyTuple_ITEMS(args); |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2507 | nargs = PyTuple_GET_SIZE(args); |
| 2508 | |
Raymond Hettinger | 9423023 | 2016-03-26 03:02:48 -0700 | [diff] [blame] | 2509 | #ifdef HAVE_STDARG_PROTOTYPES |
| 2510 | va_start(vargs, max); |
| 2511 | #else |
| 2512 | va_start(vargs); |
| 2513 | #endif |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2514 | retval = unpack_stack(stack, nargs, name, min, max, vargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2515 | va_end(vargs); |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2516 | return retval; |
| 2517 | } |
| 2518 | |
| 2519 | int |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2520 | _PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name, |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2521 | Py_ssize_t min, Py_ssize_t max, ...) |
| 2522 | { |
| 2523 | int retval; |
| 2524 | va_list vargs; |
| 2525 | |
| 2526 | #ifdef HAVE_STDARG_PROTOTYPES |
| 2527 | va_start(vargs, max); |
| 2528 | #else |
| 2529 | va_start(vargs); |
| 2530 | #endif |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2531 | retval = unpack_stack(args, nargs, name, min, max, vargs); |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2532 | va_end(vargs); |
| 2533 | return retval; |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 2534 | } |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2535 | |
| 2536 | |
Serhiy Storchaka | 68a001d | 2017-02-06 10:41:46 +0200 | [diff] [blame] | 2537 | #undef _PyArg_NoKeywords |
Serhiy Storchaka | 68a001d | 2017-02-06 10:41:46 +0200 | [diff] [blame] | 2538 | #undef _PyArg_NoPositional |
| 2539 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2540 | /* For type constructors that don't take keyword args |
| 2541 | * |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2542 | * Sets a TypeError and returns 0 if the args/kwargs is |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2543 | * not empty, returns 1 otherwise |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2544 | */ |
| 2545 | int |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2546 | _PyArg_NoKeywords(const char *funcname, PyObject *kwargs) |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2547 | { |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2548 | if (kwargs == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | return 1; |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2550 | } |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2551 | if (!PyDict_CheckExact(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2552 | PyErr_BadInternalCall(); |
| 2553 | return 0; |
| 2554 | } |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2555 | if (PyDict_GET_SIZE(kwargs) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2556 | return 1; |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2557 | } |
| 2558 | |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2559 | PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2560 | funcname); |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
| 2564 | |
| 2565 | int |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2566 | _PyArg_NoPositional(const char *funcname, PyObject *args) |
| 2567 | { |
| 2568 | if (args == NULL) |
| 2569 | return 1; |
| 2570 | if (!PyTuple_CheckExact(args)) { |
| 2571 | PyErr_BadInternalCall(); |
| 2572 | return 0; |
| 2573 | } |
| 2574 | if (PyTuple_GET_SIZE(args) == 0) |
| 2575 | return 1; |
| 2576 | |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2577 | PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments", |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2578 | funcname); |
| 2579 | return 0; |
| 2580 | } |
| 2581 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2582 | void |
| 2583 | _PyArg_Fini(void) |
| 2584 | { |
| 2585 | struct _PyArg_Parser *tmp, *s = static_arg_parsers; |
| 2586 | while (s) { |
| 2587 | tmp = s->next; |
| 2588 | s->next = NULL; |
| 2589 | parser_clear(s); |
| 2590 | s = tmp; |
| 2591 | } |
| 2592 | static_arg_parsers = NULL; |
| 2593 | } |
| 2594 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2595 | #ifdef __cplusplus |
| 2596 | }; |
| 2597 | #endif |