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