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