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 | |
| 646 | /* Convert a non-tuple argument. Return NULL if conversion went OK, |
| 647 | or a string with a message describing the failure. The message is |
| 648 | formatted as "must be <desired type>, not <actual type>". |
| 649 | When failing, an exception may or may not have been raised. |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 650 | Don't call if a tuple is expected. |
| 651 | |
| 652 | 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] | 653 | */ |
| 654 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 655 | static const char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 656 | 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] | 657 | char *msgbuf, size_t bufsize, freelist_t *freelist) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 658 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | /* For # codes */ |
| 660 | #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\ |
| 661 | 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] | 662 | else { \ |
| 663 | if (PyErr_WarnEx(PyExc_DeprecationWarning, \ |
| 664 | "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) { \ |
| 665 | return NULL; \ |
| 666 | } \ |
| 667 | q=va_arg(*p_va, int*); \ |
| 668 | } |
Victor Stinner | b3c9e07 | 2011-01-04 02:07:34 +0000 | [diff] [blame] | 669 | #define STORE_SIZE(s) \ |
| 670 | if (flags & FLAG_SIZE_T) \ |
| 671 | *q2=s; \ |
| 672 | else { \ |
| 673 | if (INT_MAX < s) { \ |
| 674 | PyErr_SetString(PyExc_OverflowError, \ |
| 675 | "size does not fit in an int"); \ |
| 676 | return converterr("", arg, msgbuf, bufsize); \ |
| 677 | } \ |
Victor Stinner | 9550ef3 | 2013-06-05 01:18:13 +0200 | [diff] [blame] | 678 | *q = (int)s; \ |
Victor Stinner | b3c9e07 | 2011-01-04 02:07:34 +0000 | [diff] [blame] | 679 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 680 | #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 681 | #define RETURN_ERR_OCCURRED return msgbuf |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | const char *format = *p_format; |
| 684 | char c = *format++; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 685 | const char *sarg; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | switch (c) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | case 'b': { /* unsigned byte -- very short int */ |
| 690 | char *p = va_arg(*p_va, char *); |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 691 | long ival = PyLong_AsLong(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 693 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | else if (ival < 0) { |
| 695 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 696 | "unsigned byte integer is less than minimum"); |
| 697 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 698 | } |
| 699 | else if (ival > UCHAR_MAX) { |
| 700 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 701 | "unsigned byte integer is greater than maximum"); |
| 702 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 703 | } |
| 704 | else |
| 705 | *p = (unsigned char) ival; |
| 706 | break; |
| 707 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 709 | case 'B': {/* byte sized bitfield - both signed and unsigned |
| 710 | values allowed */ |
| 711 | char *p = va_arg(*p_va, char *); |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 712 | unsigned long ival = PyLong_AsUnsignedLongMask(arg); |
| 713 | if (ival == (unsigned long)-1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 714 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 715 | else |
| 716 | *p = (unsigned char) ival; |
| 717 | break; |
| 718 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 720 | case 'h': {/* signed short int */ |
| 721 | short *p = va_arg(*p_va, short *); |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 722 | long ival = PyLong_AsLong(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 723 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 724 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 725 | else if (ival < SHRT_MIN) { |
| 726 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 727 | "signed short integer is less than minimum"); |
| 728 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | } |
| 730 | else if (ival > SHRT_MAX) { |
| 731 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 732 | "signed short integer is greater than maximum"); |
| 733 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 734 | } |
| 735 | else |
| 736 | *p = (short) ival; |
| 737 | break; |
| 738 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | case 'H': { /* short int sized bitfield, both signed and |
| 741 | unsigned allowed */ |
| 742 | unsigned short *p = va_arg(*p_va, unsigned short *); |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 743 | unsigned long ival = PyLong_AsUnsignedLongMask(arg); |
| 744 | if (ival == (unsigned long)-1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 745 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 746 | else |
| 747 | *p = (unsigned short) ival; |
| 748 | break; |
| 749 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | case 'i': {/* signed int */ |
| 752 | int *p = va_arg(*p_va, int *); |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 753 | long ival = PyLong_AsLong(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 755 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | else if (ival > INT_MAX) { |
| 757 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 758 | "signed integer is greater than maximum"); |
| 759 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | } |
| 761 | else if (ival < INT_MIN) { |
| 762 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 763 | "signed integer is less than minimum"); |
| 764 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | } |
| 766 | else |
| 767 | *p = ival; |
| 768 | break; |
| 769 | } |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | case 'I': { /* int sized bitfield, both signed and |
| 772 | unsigned allowed */ |
| 773 | unsigned int *p = va_arg(*p_va, unsigned int *); |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 774 | unsigned long ival = PyLong_AsUnsignedLongMask(arg); |
| 775 | if (ival == (unsigned long)-1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 776 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | else |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 778 | *p = (unsigned int) ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | break; |
| 780 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 782 | case 'n': /* Py_ssize_t */ |
| 783 | { |
| 784 | PyObject *iobj; |
| 785 | Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *); |
| 786 | Py_ssize_t ival = -1; |
Serhiy Storchaka | 5f4b229d | 2020-05-28 10:33:45 +0300 | [diff] [blame^] | 787 | iobj = _PyNumber_Index(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | if (iobj != NULL) { |
| 789 | ival = PyLong_AsSsize_t(iobj); |
| 790 | Py_DECREF(iobj); |
| 791 | } |
| 792 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 793 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 794 | *p = ival; |
| 795 | break; |
| 796 | } |
| 797 | case 'l': {/* long int */ |
| 798 | long *p = va_arg(*p_va, long *); |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 799 | long ival = PyLong_AsLong(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 801 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 802 | else |
| 803 | *p = ival; |
| 804 | break; |
| 805 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | case 'k': { /* long sized bitfield */ |
| 808 | unsigned long *p = va_arg(*p_va, unsigned long *); |
| 809 | unsigned long ival; |
| 810 | if (PyLong_Check(arg)) |
| 811 | ival = PyLong_AsUnsignedLongMask(arg); |
| 812 | else |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 813 | return converterr("int", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | *p = ival; |
| 815 | break; |
| 816 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 817 | |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 818 | case 'L': {/* long long */ |
| 819 | long long *p = va_arg( *p_va, long long * ); |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 820 | long long ival = PyLong_AsLongLong(arg); |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 821 | if (ival == (long long)-1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 822 | RETURN_ERR_OCCURRED; |
Mark Dickinson | c730131 | 2010-06-10 16:05:10 +0000 | [diff] [blame] | 823 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | *p = ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | break; |
| 826 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | case 'K': { /* long long sized bitfield */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 829 | unsigned long long *p = va_arg(*p_va, unsigned long long *); |
| 830 | unsigned long long ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 831 | if (PyLong_Check(arg)) |
| 832 | ival = PyLong_AsUnsignedLongLongMask(arg); |
| 833 | else |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 834 | return converterr("int", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | *p = ival; |
| 836 | break; |
| 837 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | case 'f': {/* float */ |
| 840 | float *p = va_arg(*p_va, float *); |
| 841 | double dval = PyFloat_AsDouble(arg); |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 842 | if (dval == -1.0 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 843 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | else |
| 845 | *p = (float) dval; |
| 846 | break; |
| 847 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 849 | case 'd': {/* double */ |
| 850 | double *p = va_arg(*p_va, double *); |
| 851 | double dval = PyFloat_AsDouble(arg); |
Raymond Hettinger | aef9ad8 | 2019-08-24 19:10:39 -0700 | [diff] [blame] | 852 | if (dval == -1.0 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 853 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | else |
| 855 | *p = dval; |
| 856 | break; |
| 857 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 858 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | case 'D': {/* complex double */ |
| 860 | Py_complex *p = va_arg(*p_va, Py_complex *); |
| 861 | Py_complex cval; |
| 862 | cval = PyComplex_AsCComplex(arg); |
| 863 | if (PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 864 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | else |
| 866 | *p = cval; |
| 867 | break; |
| 868 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | case 'c': {/* char */ |
| 871 | char *p = va_arg(*p_va, char *); |
| 872 | if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1) |
| 873 | *p = PyBytes_AS_STRING(arg)[0]; |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 874 | else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1) |
| 875 | *p = PyByteArray_AS_STRING(arg)[0]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | else |
| 877 | return converterr("a byte string of length 1", arg, msgbuf, bufsize); |
| 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 'C': {/* unicode char */ |
| 882 | int *p = va_arg(*p_va, int *); |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 883 | int kind; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 884 | const void *data; |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 885 | |
| 886 | if (!PyUnicode_Check(arg)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | return converterr("a unicode character", arg, msgbuf, bufsize); |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 888 | |
| 889 | if (PyUnicode_READY(arg)) |
| 890 | RETURN_ERR_OCCURRED; |
| 891 | |
| 892 | if (PyUnicode_GET_LENGTH(arg) != 1) |
| 893 | return converterr("a unicode character", arg, msgbuf, bufsize); |
| 894 | |
| 895 | kind = PyUnicode_KIND(arg); |
| 896 | data = PyUnicode_DATA(arg); |
| 897 | *p = PyUnicode_READ(kind, data, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | break; |
| 899 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 900 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 901 | case 'p': {/* boolean *p*redicate */ |
| 902 | int *p = va_arg(*p_va, int *); |
| 903 | int val = PyObject_IsTrue(arg); |
| 904 | if (val > 0) |
| 905 | *p = 1; |
| 906 | else if (val == 0) |
| 907 | *p = 0; |
| 908 | else |
| 909 | RETURN_ERR_OCCURRED; |
| 910 | break; |
| 911 | } |
| 912 | |
Victor Stinner | 3dcb5ac | 2010-06-08 22:54:19 +0000 | [diff] [blame] | 913 | /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | need to be cleaned up! */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 915 | |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 916 | case 'y': {/* any bytes-like object */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | void **p = (void **)va_arg(*p_va, char **); |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 918 | const char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | Py_ssize_t count; |
| 920 | if (*format == '*') { |
| 921 | if (getbuffer(arg, (Py_buffer*)p, &buf) < 0) |
| 922 | return converterr(buf, arg, msgbuf, bufsize); |
| 923 | format++; |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 924 | if (addcleanup(p, freelist, cleanup_buffer)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | return converterr( |
| 926 | "(cleanup problem)", |
| 927 | arg, msgbuf, bufsize); |
| 928 | } |
| 929 | break; |
| 930 | } |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 931 | count = convertbuffer(arg, (const void **)p, &buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | if (count < 0) |
| 933 | return converterr(buf, arg, msgbuf, bufsize); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 934 | if (*format == '#') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | FETCH_SIZE; |
| 936 | STORE_SIZE(count); |
| 937 | format++; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 938 | } else { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 939 | if (strlen(*p) != (size_t)count) { |
| 940 | PyErr_SetString(PyExc_ValueError, "embedded null byte"); |
| 941 | RETURN_ERR_OCCURRED; |
| 942 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | } |
| 944 | break; |
| 945 | } |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 946 | |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 947 | case 's': /* text string or bytes-like object */ |
| 948 | case 'z': /* text string, bytes-like object or None */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 949 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | if (*format == '*') { |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 951 | /* "s*" or "z*" */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | 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] | 953 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 954 | if (c == 'z' && arg == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0); |
| 956 | else if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 957 | Py_ssize_t len; |
| 958 | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 959 | if (sarg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | return converterr(CONV_UNICODE, |
| 961 | arg, msgbuf, bufsize); |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 962 | PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | } |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 964 | else { /* any bytes-like object */ |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 965 | const char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | if (getbuffer(arg, p, &buf) < 0) |
| 967 | return converterr(buf, arg, msgbuf, bufsize); |
| 968 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 969 | if (addcleanup(p, freelist, cleanup_buffer)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 970 | return converterr( |
| 971 | "(cleanup problem)", |
| 972 | arg, msgbuf, bufsize); |
| 973 | } |
| 974 | format++; |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 975 | } else if (*format == '#') { /* a string or read-only bytes-like object */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 976 | /* "s#" or "z#" */ |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 977 | const void **p = (const void **)va_arg(*p_va, const char **); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | FETCH_SIZE; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 979 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 980 | if (c == 'z' && arg == Py_None) { |
| 981 | *p = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | STORE_SIZE(0); |
| 983 | } |
| 984 | else if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 985 | Py_ssize_t len; |
| 986 | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 987 | if (sarg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | return converterr(CONV_UNICODE, |
| 989 | arg, msgbuf, bufsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 990 | *p = sarg; |
| 991 | STORE_SIZE(len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | } |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 993 | else { /* read-only bytes-like object */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | /* XXX Really? */ |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 995 | const char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | Py_ssize_t count = convertbuffer(arg, p, &buf); |
| 997 | if (count < 0) |
| 998 | return converterr(buf, arg, msgbuf, bufsize); |
| 999 | STORE_SIZE(count); |
| 1000 | } |
| 1001 | format++; |
| 1002 | } else { |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1003 | /* "s" or "z" */ |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1004 | const char **p = va_arg(*p_va, const char **); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1005 | Py_ssize_t len; |
| 1006 | sarg = NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1007 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1008 | if (c == 'z' && arg == Py_None) |
| 1009 | *p = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | else if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1011 | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 1012 | if (sarg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | return converterr(CONV_UNICODE, |
| 1014 | arg, msgbuf, bufsize); |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1015 | if (strlen(sarg) != (size_t)len) { |
| 1016 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1017 | RETURN_ERR_OCCURRED; |
| 1018 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1019 | *p = sarg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | } |
| 1021 | else |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1022 | return converterr(c == 'z' ? "str or None" : "str", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1023 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | } |
| 1025 | break; |
| 1026 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1027 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1028 | case 'u': /* raw unicode buffer (Py_UNICODE *) */ |
| 1029 | case 'Z': /* raw unicode buffer or None */ |
| 1030 | { |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 1031 | Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **); |
| 1032 | |
Serhiy Storchaka | b757c83 | 2014-12-05 22:25:22 +0200 | [diff] [blame] | 1033 | if (*format == '#') { |
Serhiy Storchaka | d6e53da | 2015-04-19 21:11:30 +0300 | [diff] [blame] | 1034 | /* "u#" or "Z#" */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | FETCH_SIZE; |
Travis E. Oliphant | ddacf96 | 2007-10-13 21:03:27 +0000 | [diff] [blame] | 1036 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1037 | if (c == 'Z' && arg == Py_None) { |
| 1038 | *p = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | STORE_SIZE(0); |
| 1040 | } |
| 1041 | else if (PyUnicode_Check(arg)) { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 1042 | Py_ssize_t len; |
| 1043 | *p = PyUnicode_AsUnicodeAndSize(arg, &len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1044 | if (*p == NULL) |
| 1045 | RETURN_ERR_OCCURRED; |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 1046 | STORE_SIZE(len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | } |
Victor Stinner | 5216e6d | 2010-06-08 21:45:51 +0000 | [diff] [blame] | 1048 | else |
Serhiy Storchaka | d6e53da | 2015-04-19 21:11:30 +0300 | [diff] [blame] | 1049 | return converterr(c == 'Z' ? "str or None" : "str", |
| 1050 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | format++; |
| 1052 | } else { |
Serhiy Storchaka | d6e53da | 2015-04-19 21:11:30 +0300 | [diff] [blame] | 1053 | /* "u" or "Z" */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1054 | if (c == 'Z' && arg == Py_None) |
| 1055 | *p = NULL; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1056 | else if (PyUnicode_Check(arg)) { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 1057 | Py_ssize_t len; |
| 1058 | *p = PyUnicode_AsUnicodeAndSize(arg, &len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1059 | if (*p == NULL) |
| 1060 | RETURN_ERR_OCCURRED; |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1061 | if (wcslen(*p) != (size_t)len) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1062 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1063 | RETURN_ERR_OCCURRED; |
| 1064 | } |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 1065 | } else |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 1066 | return converterr(c == 'Z' ? "str or None" : "str", |
| 1067 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | } |
| 1069 | break; |
| 1070 | } |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1071 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | case 'e': {/* encoded string */ |
| 1073 | char **buffer; |
| 1074 | const char *encoding; |
| 1075 | PyObject *s; |
| 1076 | int recode_strings; |
| 1077 | Py_ssize_t size; |
| 1078 | const char *ptr; |
Jeremy Hylton | 4819e97 | 2001-10-11 14:40:37 +0000 | [diff] [blame] | 1079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | /* Get 'e' parameter: the encoding name */ |
| 1081 | encoding = (const char *)va_arg(*p_va, const char *); |
| 1082 | if (encoding == NULL) |
| 1083 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1084 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | /* Get output buffer parameter: |
| 1086 | 's' (recode all objects via Unicode) or |
| 1087 | 't' (only recode non-string objects) |
| 1088 | */ |
| 1089 | if (*format == 's') |
| 1090 | recode_strings = 1; |
| 1091 | else if (*format == 't') |
| 1092 | recode_strings = 0; |
| 1093 | else |
| 1094 | return converterr( |
| 1095 | "(unknown parser marker combination)", |
| 1096 | arg, msgbuf, bufsize); |
| 1097 | buffer = (char **)va_arg(*p_va, char **); |
| 1098 | format++; |
| 1099 | if (buffer == NULL) |
| 1100 | return converterr("(buffer is NULL)", |
| 1101 | arg, msgbuf, bufsize); |
Benjamin Peterson | 9edd2bd | 2008-08-27 00:31:37 +0000 | [diff] [blame] | 1102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | /* Encode object */ |
| 1104 | if (!recode_strings && |
| 1105 | (PyBytes_Check(arg) || PyByteArray_Check(arg))) { |
| 1106 | s = arg; |
| 1107 | Py_INCREF(s); |
Serhiy Storchaka | 460bd0d | 2016-11-20 12:16:46 +0200 | [diff] [blame] | 1108 | if (PyBytes_Check(arg)) { |
| 1109 | size = PyBytes_GET_SIZE(s); |
| 1110 | ptr = PyBytes_AS_STRING(s); |
| 1111 | } |
| 1112 | else { |
| 1113 | size = PyByteArray_GET_SIZE(s); |
| 1114 | ptr = PyByteArray_AS_STRING(s); |
| 1115 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1117 | else if (PyUnicode_Check(arg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | /* Encode object; use default error handling */ |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1119 | s = PyUnicode_AsEncodedString(arg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | encoding, |
| 1121 | NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | if (s == NULL) |
| 1123 | return converterr("(encoding failed)", |
| 1124 | arg, msgbuf, bufsize); |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1125 | assert(PyBytes_Check(s)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | size = PyBytes_GET_SIZE(s); |
| 1127 | ptr = PyBytes_AS_STRING(s); |
| 1128 | if (ptr == NULL) |
| 1129 | ptr = ""; |
| 1130 | } |
Serhiy Storchaka | 21a663e | 2016-04-13 15:37:23 +0300 | [diff] [blame] | 1131 | else { |
| 1132 | return converterr( |
| 1133 | recode_strings ? "str" : "str, bytes or bytearray", |
| 1134 | arg, msgbuf, bufsize); |
| 1135 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1137 | /* Write output; output is guaranteed to be 0-terminated */ |
| 1138 | if (*format == '#') { |
| 1139 | /* Using buffer length parameter '#': |
| 1140 | |
| 1141 | - if *buffer is NULL, a new buffer of the |
| 1142 | needed size is allocated and the data |
| 1143 | copied into it; *buffer is updated to point |
| 1144 | to the new buffer; the caller is |
| 1145 | responsible for PyMem_Free()ing it after |
| 1146 | usage |
| 1147 | |
| 1148 | - if *buffer is not NULL, the data is |
| 1149 | copied to *buffer; *buffer_len has to be |
| 1150 | set to the size of the buffer on input; |
| 1151 | buffer overflow is signalled with an error; |
| 1152 | buffer has to provide enough room for the |
| 1153 | encoded string plus the trailing 0-byte |
| 1154 | |
| 1155 | - in both cases, *buffer_len is updated to |
| 1156 | the size of the buffer /excluding/ the |
| 1157 | trailing 0-byte |
| 1158 | |
| 1159 | */ |
Serhiy Storchaka | 5bc6a7c | 2019-10-21 11:35:07 +0300 | [diff] [blame] | 1160 | int *q = NULL; Py_ssize_t *q2 = NULL; |
| 1161 | if (flags & FLAG_SIZE_T) { |
| 1162 | q2 = va_arg(*p_va, Py_ssize_t*); |
| 1163 | } |
| 1164 | else { |
| 1165 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1166 | "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) |
| 1167 | { |
| 1168 | Py_DECREF(s); |
| 1169 | return NULL; |
| 1170 | } |
| 1171 | q = va_arg(*p_va, int*); |
| 1172 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | |
| 1174 | format++; |
| 1175 | if (q == NULL && q2 == NULL) { |
| 1176 | Py_DECREF(s); |
| 1177 | return converterr( |
| 1178 | "(buffer_len is NULL)", |
| 1179 | arg, msgbuf, bufsize); |
| 1180 | } |
| 1181 | if (*buffer == NULL) { |
| 1182 | *buffer = PyMem_NEW(char, size + 1); |
| 1183 | if (*buffer == NULL) { |
| 1184 | Py_DECREF(s); |
Victor Stinner | 2872e5b | 2010-06-06 20:38:02 +0000 | [diff] [blame] | 1185 | PyErr_NoMemory(); |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1186 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1188 | if (addcleanup(*buffer, freelist, cleanup_ptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | Py_DECREF(s); |
| 1190 | return converterr( |
| 1191 | "(cleanup problem)", |
| 1192 | arg, msgbuf, bufsize); |
| 1193 | } |
| 1194 | } else { |
| 1195 | if (size + 1 > BUFFER_LEN) { |
| 1196 | Py_DECREF(s); |
Serhiy Storchaka | 4cd63ef | 2016-02-08 01:22:47 +0200 | [diff] [blame] | 1197 | PyErr_Format(PyExc_ValueError, |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 1198 | "encoded string too long " |
| 1199 | "(%zd, maximum length %zd)", |
| 1200 | (Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1)); |
| 1201 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | memcpy(*buffer, ptr, size+1); |
Serhiy Storchaka | 5bc6a7c | 2019-10-21 11:35:07 +0300 | [diff] [blame] | 1205 | |
| 1206 | if (flags & FLAG_SIZE_T) { |
| 1207 | *q2 = size; |
| 1208 | } |
| 1209 | else { |
| 1210 | if (INT_MAX < size) { |
| 1211 | Py_DECREF(s); |
| 1212 | PyErr_SetString(PyExc_OverflowError, |
| 1213 | "size does not fit in an int"); |
| 1214 | return converterr("", arg, msgbuf, bufsize); |
| 1215 | } |
| 1216 | *q = (int)size; |
| 1217 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | } else { |
| 1219 | /* Using a 0-terminated buffer: |
| 1220 | |
| 1221 | - the encoded string has to be 0-terminated |
| 1222 | for this variant to work; if it is not, an |
| 1223 | error raised |
| 1224 | |
| 1225 | - a new buffer of the needed size is |
| 1226 | allocated and the data copied into it; |
| 1227 | *buffer is updated to point to the new |
| 1228 | buffer; the caller is responsible for |
| 1229 | PyMem_Free()ing it after usage |
| 1230 | |
| 1231 | */ |
| 1232 | if ((Py_ssize_t)strlen(ptr) != size) { |
| 1233 | Py_DECREF(s); |
| 1234 | return converterr( |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 1235 | "encoded string without null bytes", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | arg, msgbuf, bufsize); |
| 1237 | } |
| 1238 | *buffer = PyMem_NEW(char, size + 1); |
| 1239 | if (*buffer == NULL) { |
| 1240 | Py_DECREF(s); |
Victor Stinner | 2872e5b | 2010-06-06 20:38:02 +0000 | [diff] [blame] | 1241 | PyErr_NoMemory(); |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1242 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1243 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1244 | if (addcleanup(*buffer, freelist, cleanup_ptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | Py_DECREF(s); |
| 1246 | return converterr("(cleanup problem)", |
| 1247 | arg, msgbuf, bufsize); |
| 1248 | } |
| 1249 | memcpy(*buffer, ptr, size+1); |
| 1250 | } |
| 1251 | Py_DECREF(s); |
| 1252 | break; |
| 1253 | } |
| 1254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | case 'S': { /* PyBytes object */ |
| 1256 | PyObject **p = va_arg(*p_va, PyObject **); |
| 1257 | if (PyBytes_Check(arg)) |
| 1258 | *p = arg; |
| 1259 | else |
| 1260 | return converterr("bytes", arg, msgbuf, bufsize); |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | case 'Y': { /* PyByteArray object */ |
| 1265 | PyObject **p = va_arg(*p_va, PyObject **); |
| 1266 | if (PyByteArray_Check(arg)) |
| 1267 | *p = arg; |
| 1268 | else |
Victor Stinner | 5216e6d | 2010-06-08 21:45:51 +0000 | [diff] [blame] | 1269 | return converterr("bytearray", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1270 | break; |
| 1271 | } |
| 1272 | |
| 1273 | case 'U': { /* PyUnicode object */ |
| 1274 | PyObject **p = va_arg(*p_va, PyObject **); |
Victor Stinner | a1b0c9f | 2012-05-29 12:30:29 +0200 | [diff] [blame] | 1275 | if (PyUnicode_Check(arg)) { |
| 1276 | if (PyUnicode_READY(arg) == -1) |
| 1277 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1278 | *p = arg; |
Victor Stinner | a1b0c9f | 2012-05-29 12:30:29 +0200 | [diff] [blame] | 1279 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | else |
| 1281 | return converterr("str", arg, msgbuf, bufsize); |
| 1282 | break; |
| 1283 | } |
| 1284 | |
| 1285 | case 'O': { /* object */ |
| 1286 | PyTypeObject *type; |
| 1287 | PyObject **p; |
| 1288 | if (*format == '!') { |
| 1289 | type = va_arg(*p_va, PyTypeObject*); |
| 1290 | p = va_arg(*p_va, PyObject **); |
| 1291 | format++; |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 1292 | if (PyType_IsSubtype(Py_TYPE(arg), type)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | *p = arg; |
| 1294 | else |
| 1295 | return converterr(type->tp_name, arg, msgbuf, bufsize); |
| 1296 | |
| 1297 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1298 | else if (*format == '&') { |
| 1299 | typedef int (*converter)(PyObject *, void *); |
| 1300 | converter convert = va_arg(*p_va, converter); |
| 1301 | void *addr = va_arg(*p_va, void *); |
| 1302 | int res; |
| 1303 | format++; |
| 1304 | if (! (res = (*convert)(arg, addr))) |
| 1305 | return converterr("(unspecified)", |
| 1306 | arg, msgbuf, bufsize); |
| 1307 | if (res == Py_CLEANUP_SUPPORTED && |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1308 | addcleanup(addr, freelist, convert) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | return converterr("(cleanup problem)", |
| 1310 | arg, msgbuf, bufsize); |
| 1311 | } |
| 1312 | else { |
| 1313 | p = va_arg(*p_va, PyObject **); |
| 1314 | *p = arg; |
| 1315 | } |
| 1316 | break; |
| 1317 | } |
| 1318 | |
| 1319 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1320 | case 'w': { /* "w*": memory buffer, read-write access */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | void **p = va_arg(*p_va, void **); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1323 | if (*format != '*') |
| 1324 | return converterr( |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 1325 | "(invalid use of 'w' format character)", |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1326 | arg, msgbuf, bufsize); |
| 1327 | format++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1329 | /* Caller is interested in Py_buffer, and the object |
| 1330 | supports it directly. */ |
| 1331 | if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) { |
| 1332 | PyErr_Clear(); |
R David Murray | 861470c | 2014-10-05 11:47:01 -0400 | [diff] [blame] | 1333 | return converterr("read-write bytes-like object", |
| 1334 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | } |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1336 | if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) { |
| 1337 | PyBuffer_Release((Py_buffer*)p); |
| 1338 | return converterr("contiguous buffer", arg, msgbuf, bufsize); |
| 1339 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1340 | if (addcleanup(p, freelist, cleanup_buffer)) { |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1341 | return converterr( |
| 1342 | "(cleanup problem)", |
| 1343 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1344 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | break; |
| 1346 | } |
| 1347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | default: |
Serhiy Storchaka | c4b813d | 2016-02-08 01:06:11 +0200 | [diff] [blame] | 1349 | return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | |
| 1351 | } |
| 1352 | |
| 1353 | *p_format = format; |
| 1354 | return NULL; |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1355 | |
| 1356 | #undef FETCH_SIZE |
| 1357 | #undef STORE_SIZE |
| 1358 | #undef BUFFER_LEN |
| 1359 | #undef RETURN_ERR_OCCURRED |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 1360 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1361 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1362 | static Py_ssize_t |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 1363 | convertbuffer(PyObject *arg, const void **p, const char **errmsg) |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1364 | { |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1365 | PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | Py_ssize_t count; |
| 1367 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | *errmsg = NULL; |
| 1370 | *p = NULL; |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1371 | if (pb != NULL && pb->bf_releasebuffer != NULL) { |
R David Murray | 861470c | 2014-10-05 11:47:01 -0400 | [diff] [blame] | 1372 | *errmsg = "read-only bytes-like object"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | return -1; |
| 1374 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1375 | |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1376 | if (getbuffer(arg, &view, errmsg) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | count = view.len; |
| 1379 | *p = view.buf; |
| 1380 | PyBuffer_Release(&view); |
| 1381 | return count; |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1382 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1383 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1384 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1385 | getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1386 | { |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1387 | if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) { |
R David Murray | 861470c | 2014-10-05 11:47:01 -0400 | [diff] [blame] | 1388 | *errmsg = "bytes-like object"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | return -1; |
| 1390 | } |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1391 | if (!PyBuffer_IsContiguous(view, 'C')) { |
Victor Stinner | 21e0948 | 2010-06-24 22:57:10 +0000 | [diff] [blame] | 1392 | PyBuffer_Release(view); |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1393 | *errmsg = "contiguous buffer"; |
| 1394 | return -1; |
| 1395 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | return 0; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1399 | /* Support for keyword arguments donated by |
| 1400 | Geoff Philbrick <philbric@delphi.hks.com> */ |
| 1401 | |
Tim Peters | f8cd3e8 | 2001-10-27 04:26:57 +0000 | [diff] [blame] | 1402 | /* Return false (0) for error, else true. */ |
Fred Drake | 563dfc2 | 2001-10-23 14:41:08 +0000 | [diff] [blame] | 1403 | int |
| 1404 | PyArg_ParseTupleAndKeywords(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | PyObject *keywords, |
| 1406 | const char *format, |
| 1407 | char **kwlist, ...) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1409 | int retval; |
| 1410 | va_list va; |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1413 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1414 | format == NULL || |
| 1415 | kwlist == NULL) |
| 1416 | { |
| 1417 | PyErr_BadInternalCall(); |
| 1418 | return 0; |
| 1419 | } |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | va_start(va, kwlist); |
| 1422 | retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0); |
| 1423 | va_end(va); |
| 1424 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Vinay Sajip | 0b60f64 | 2019-10-15 08:26:12 +0100 | [diff] [blame] | 1427 | PyAPI_FUNC(int) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1428 | _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | PyObject *keywords, |
| 1430 | const char *format, |
| 1431 | char **kwlist, ...) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1433 | int retval; |
| 1434 | va_list va; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1437 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1438 | format == NULL || |
| 1439 | kwlist == NULL) |
| 1440 | { |
| 1441 | PyErr_BadInternalCall(); |
| 1442 | return 0; |
| 1443 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | va_start(va, kwlist); |
| 1446 | retval = vgetargskeywords(args, keywords, format, |
| 1447 | kwlist, &va, FLAG_SIZE_T); |
| 1448 | va_end(va); |
| 1449 | return retval; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1453 | int |
| 1454 | PyArg_VaParseTupleAndKeywords(PyObject *args, |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1455 | PyObject *keywords, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1456 | const char *format, |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1457 | char **kwlist, va_list va) |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1459 | int retval; |
| 1460 | va_list lva; |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1463 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1464 | format == NULL || |
| 1465 | kwlist == NULL) |
| 1466 | { |
| 1467 | PyErr_BadInternalCall(); |
| 1468 | return 0; |
| 1469 | } |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1470 | |
Benjamin Peterson | 0c21214 | 2016-09-20 20:39:33 -0700 | [diff] [blame] | 1471 | va_copy(lva, va); |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0); |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 1474 | va_end(lva); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Vinay Sajip | 0b60f64 | 2019-10-15 08:26:12 +0100 | [diff] [blame] | 1478 | PyAPI_FUNC(int) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1479 | _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | PyObject *keywords, |
| 1481 | const char *format, |
| 1482 | char **kwlist, va_list va) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1483 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | int retval; |
| 1485 | va_list lva; |
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 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1488 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1489 | format == NULL || |
| 1490 | kwlist == NULL) |
| 1491 | { |
| 1492 | PyErr_BadInternalCall(); |
| 1493 | return 0; |
| 1494 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1495 | |
Benjamin Peterson | 0c21214 | 2016-09-20 20:39:33 -0700 | [diff] [blame] | 1496 | va_copy(lva, va); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | retval = vgetargskeywords(args, keywords, format, |
| 1499 | kwlist, &lva, FLAG_SIZE_T); |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 1500 | va_end(lva); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | return retval; |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Vinay Sajip | 0b60f64 | 2019-10-15 08:26:12 +0100 | [diff] [blame] | 1504 | PyAPI_FUNC(int) |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1505 | _PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords, |
| 1506 | struct _PyArg_Parser *parser, ...) |
| 1507 | { |
| 1508 | int retval; |
| 1509 | va_list va; |
| 1510 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1511 | va_start(va, parser); |
| 1512 | retval = vgetargskeywordsfast(args, keywords, parser, &va, 0); |
| 1513 | va_end(va); |
| 1514 | return retval; |
| 1515 | } |
| 1516 | |
Vinay Sajip | 0b60f64 | 2019-10-15 08:26:12 +0100 | [diff] [blame] | 1517 | PyAPI_FUNC(int) |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1518 | _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords, |
| 1519 | struct _PyArg_Parser *parser, ...) |
| 1520 | { |
| 1521 | int retval; |
| 1522 | va_list va; |
| 1523 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1524 | va_start(va, parser); |
| 1525 | retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T); |
| 1526 | va_end(va); |
| 1527 | return retval; |
| 1528 | } |
| 1529 | |
Vinay Sajip | 0b60f64 | 2019-10-15 08:26:12 +0100 | [diff] [blame] | 1530 | PyAPI_FUNC(int) |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1531 | _PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 1532 | struct _PyArg_Parser *parser, ...) |
| 1533 | { |
| 1534 | int retval; |
| 1535 | va_list va; |
| 1536 | |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 1537 | va_start(va, parser); |
| 1538 | retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0); |
| 1539 | va_end(va); |
| 1540 | return retval; |
| 1541 | } |
| 1542 | |
Vinay Sajip | 0b60f64 | 2019-10-15 08:26:12 +0100 | [diff] [blame] | 1543 | PyAPI_FUNC(int) |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1544 | _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 1545 | struct _PyArg_Parser *parser, ...) |
| 1546 | { |
| 1547 | int retval; |
| 1548 | va_list va; |
| 1549 | |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 1550 | va_start(va, parser); |
| 1551 | retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T); |
| 1552 | va_end(va); |
| 1553 | return retval; |
| 1554 | } |
| 1555 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1556 | |
Vinay Sajip | 0b60f64 | 2019-10-15 08:26:12 +0100 | [diff] [blame] | 1557 | PyAPI_FUNC(int) |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1558 | _PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords, |
| 1559 | struct _PyArg_Parser *parser, va_list va) |
| 1560 | { |
| 1561 | int retval; |
| 1562 | va_list lva; |
| 1563 | |
Benjamin Peterson | 0c21214 | 2016-09-20 20:39:33 -0700 | [diff] [blame] | 1564 | va_copy(lva, va); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1565 | |
| 1566 | retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0); |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 1567 | va_end(lva); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1568 | return retval; |
| 1569 | } |
| 1570 | |
Vinay Sajip | 0b60f64 | 2019-10-15 08:26:12 +0100 | [diff] [blame] | 1571 | PyAPI_FUNC(int) |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1572 | _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords, |
| 1573 | struct _PyArg_Parser *parser, va_list va) |
| 1574 | { |
| 1575 | int retval; |
| 1576 | va_list lva; |
| 1577 | |
Benjamin Peterson | 0c21214 | 2016-09-20 20:39:33 -0700 | [diff] [blame] | 1578 | va_copy(lva, va); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1579 | |
| 1580 | retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T); |
Christian Heimes | 2f2fee1 | 2016-09-21 11:37:27 +0200 | [diff] [blame] | 1581 | va_end(lva); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1582 | return retval; |
| 1583 | } |
| 1584 | |
| 1585 | int |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 1586 | PyArg_ValidateKeywordArguments(PyObject *kwargs) |
| 1587 | { |
Benjamin Peterson | f609654 | 2010-11-17 22:33:12 +0000 | [diff] [blame] | 1588 | if (!PyDict_Check(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1589 | PyErr_BadInternalCall(); |
| 1590 | return 0; |
| 1591 | } |
| 1592 | if (!_PyDict_HasOnlyStringKeys(kwargs)) { |
| 1593 | PyErr_SetString(PyExc_TypeError, |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1594 | "keywords must be strings"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1595 | return 0; |
| 1596 | } |
| 1597 | return 1; |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1600 | #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1601 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1602 | static int |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1603 | vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1604 | char **kwlist, va_list *p_va, int flags) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1605 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1606 | char msgbuf[512]; |
| 1607 | int levels[32]; |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1608 | const char *fname, *msg, *custom_msg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1609 | int min = INT_MAX; |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1610 | int max = INT_MAX; |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1611 | int i, pos, len; |
| 1612 | int skip = 0; |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1613 | Py_ssize_t nargs, nkwargs; |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1614 | PyObject *current_arg; |
Antoine Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 1615 | freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
Benjamin Peterson | 40be9e5 | 2014-02-11 10:09:27 -0500 | [diff] [blame] | 1616 | freelist_t freelist; |
| 1617 | |
| 1618 | freelist.entries = static_entries; |
| 1619 | freelist.first_available = 0; |
| 1620 | freelist.entries_malloced = 0; |
Tim Peters | f4331c1 | 2001-10-27 00:17:34 +0000 | [diff] [blame] | 1621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | assert(args != NULL && PyTuple_Check(args)); |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1623 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | assert(format != NULL); |
| 1625 | assert(kwlist != NULL); |
| 1626 | assert(p_va != NULL); |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | /* grab the function name or custom error msg first (mutually exclusive) */ |
| 1629 | fname = strchr(format, ':'); |
| 1630 | if (fname) { |
| 1631 | fname++; |
| 1632 | custom_msg = NULL; |
| 1633 | } |
| 1634 | else { |
| 1635 | custom_msg = strchr(format,';'); |
| 1636 | if (custom_msg) |
| 1637 | custom_msg++; |
| 1638 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1639 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1640 | /* scan kwlist and count the number of positional-only parameters */ |
| 1641 | for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) { |
| 1642 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1643 | /* scan kwlist and get greatest possible nbr of args */ |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1644 | for (len = pos; kwlist[len]; len++) { |
| 1645 | if (!*kwlist[len]) { |
| 1646 | PyErr_SetString(PyExc_SystemError, |
| 1647 | "Empty keyword parameter name"); |
| 1648 | return cleanreturn(0, &freelist); |
| 1649 | } |
| 1650 | } |
Tim Peters | f8cd3e8 | 2001-10-27 04:26:57 +0000 | [diff] [blame] | 1651 | |
Antoine Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 1652 | if (len > STATIC_FREELIST_ENTRIES) { |
| 1653 | freelist.entries = PyMem_NEW(freelistentry_t, len); |
| 1654 | if (freelist.entries == NULL) { |
| 1655 | PyErr_NoMemory(); |
| 1656 | return 0; |
| 1657 | } |
| 1658 | freelist.entries_malloced = 1; |
Benjamin Peterson | 7ed6727 | 2012-03-16 12:21:02 -0500 | [diff] [blame] | 1659 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1660 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1661 | nargs = PyTuple_GET_SIZE(args); |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1662 | nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs); |
| 1663 | if (nargs + nkwargs > len) { |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 1664 | /* Adding "keyword" (when nargs == 0) prevents producing wrong error |
| 1665 | messages in some special cases (see bpo-31229). */ |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1666 | PyErr_Format(PyExc_TypeError, |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 1667 | "%.200s%s takes at most %d %sargument%s (%zd given)", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1668 | (fname == NULL) ? "function" : fname, |
| 1669 | (fname == NULL) ? "" : "()", |
| 1670 | len, |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 1671 | (nargs == 0) ? "keyword " : "", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1672 | (len == 1) ? "" : "s", |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1673 | nargs + nkwargs); |
Benjamin Peterson | 01feaec | 2012-03-16 13:25:58 -0500 | [diff] [blame] | 1674 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | } |
Tim Peters | c2f0112 | 2001-10-27 07:25:06 +0000 | [diff] [blame] | 1676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1677 | /* convert tuple args and keyword args in same loop, using kwlist to drive process */ |
| 1678 | for (i = 0; i < len; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | if (*format == '|') { |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1680 | if (min != INT_MAX) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1681 | PyErr_SetString(PyExc_SystemError, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1682 | "Invalid format string (| specified twice)"); |
| 1683 | return cleanreturn(0, &freelist); |
| 1684 | } |
| 1685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | min = i; |
| 1687 | format++; |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1688 | |
| 1689 | if (max != INT_MAX) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1690 | PyErr_SetString(PyExc_SystemError, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1691 | "Invalid format string ($ before |)"); |
| 1692 | return cleanreturn(0, &freelist); |
| 1693 | } |
| 1694 | } |
| 1695 | if (*format == '$') { |
| 1696 | if (max != INT_MAX) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1697 | PyErr_SetString(PyExc_SystemError, |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1698 | "Invalid format string ($ specified twice)"); |
| 1699 | return cleanreturn(0, &freelist); |
| 1700 | } |
| 1701 | |
| 1702 | max = i; |
| 1703 | format++; |
| 1704 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1705 | if (max < pos) { |
| 1706 | PyErr_SetString(PyExc_SystemError, |
| 1707 | "Empty parameter name after $"); |
| 1708 | return cleanreturn(0, &freelist); |
| 1709 | } |
| 1710 | if (skip) { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1711 | /* Now we know the minimal and the maximal numbers of |
| 1712 | * positional arguments and can raise an exception with |
| 1713 | * informative message (see below). */ |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1714 | break; |
| 1715 | } |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1716 | if (max < nargs) { |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 1717 | if (max == 0) { |
| 1718 | PyErr_Format(PyExc_TypeError, |
| 1719 | "%.200s%s takes no positional arguments", |
| 1720 | (fname == NULL) ? "function" : fname, |
| 1721 | (fname == NULL) ? "" : "()"); |
| 1722 | } |
| 1723 | else { |
| 1724 | PyErr_Format(PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 1725 | "%.200s%s takes %s %d positional argument%s" |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 1726 | " (%zd given)", |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 1727 | (fname == NULL) ? "function" : fname, |
| 1728 | (fname == NULL) ? "" : "()", |
| 1729 | (min != INT_MAX) ? "at most" : "exactly", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 1730 | max, |
| 1731 | max == 1 ? "" : "s", |
| 1732 | nargs); |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 1733 | } |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1734 | return cleanreturn(0, &freelist); |
| 1735 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | } |
| 1737 | if (IS_END_OF_FORMAT(*format)) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1738 | PyErr_Format(PyExc_SystemError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | "More keyword list entries (%d) than " |
| 1740 | "format specifiers (%d)", len, i); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1741 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | } |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1743 | if (!skip) { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1744 | if (i < nargs) { |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1745 | current_arg = PyTuple_GET_ITEM(args, i); |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1746 | } |
| 1747 | else if (nkwargs && i >= pos) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1748 | current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]); |
| 1749 | if (current_arg) { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1750 | --nkwargs; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1751 | } |
| 1752 | else if (PyErr_Occurred()) { |
| 1753 | return cleanreturn(0, &freelist); |
| 1754 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1755 | } |
| 1756 | else { |
| 1757 | current_arg = NULL; |
| 1758 | } |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1759 | |
| 1760 | if (current_arg) { |
| 1761 | msg = convertitem(current_arg, &format, p_va, flags, |
| 1762 | levels, msgbuf, sizeof(msgbuf), &freelist); |
| 1763 | if (msg) { |
| 1764 | seterror(i+1, msg, levels, fname, custom_msg); |
| 1765 | return cleanreturn(0, &freelist); |
| 1766 | } |
| 1767 | continue; |
| 1768 | } |
| 1769 | |
| 1770 | if (i < min) { |
| 1771 | if (i < pos) { |
| 1772 | assert (min == INT_MAX); |
| 1773 | assert (max == INT_MAX); |
| 1774 | skip = 1; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1775 | /* At that moment we still don't know the minimal and |
| 1776 | * the maximal numbers of positional arguments. Raising |
| 1777 | * an exception is deferred until we encounter | and $ |
| 1778 | * or the end of the format. */ |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1779 | } |
| 1780 | else { |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1781 | PyErr_Format(PyExc_TypeError, "%.200s%s missing required " |
| 1782 | "argument '%s' (pos %d)", |
| 1783 | (fname == NULL) ? "function" : fname, |
| 1784 | (fname == NULL) ? "" : "()", |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1785 | kwlist[i], i+1); |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1786 | return cleanreturn(0, &freelist); |
| 1787 | } |
| 1788 | } |
| 1789 | /* current code reports success when all required args |
| 1790 | * fulfilled and no keyword args left, with no further |
| 1791 | * validation. XXX Maybe skip this in debug build ? |
| 1792 | */ |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1793 | if (!nkwargs && !skip) { |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1794 | return cleanreturn(1, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | } |
| 1796 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1797 | |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 1798 | /* We are into optional args, skip through to any remaining |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1799 | * keyword args */ |
| 1800 | msg = skipitem(&format, p_va, flags); |
| 1801 | if (msg) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1802 | PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | format); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1804 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | } |
| 1806 | } |
Tim Peters | b054be4 | 2001-10-27 05:07:41 +0000 | [diff] [blame] | 1807 | |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1808 | if (skip) { |
| 1809 | PyErr_Format(PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 1810 | "%.200s%s takes %s %d positional argument%s" |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 1811 | " (%zd given)", |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1812 | (fname == NULL) ? "function" : fname, |
| 1813 | (fname == NULL) ? "" : "()", |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1814 | (Py_MIN(pos, min) < i) ? "at least" : "exactly", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 1815 | Py_MIN(pos, min), |
| 1816 | Py_MIN(pos, min) == 1 ? "" : "s", |
| 1817 | nargs); |
Serhiy Storchaka | f41b82f | 2016-06-09 16:30:29 +0300 | [diff] [blame] | 1818 | return cleanreturn(0, &freelist); |
| 1819 | } |
| 1820 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1821 | if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { |
Serhiy Storchaka | a9725f8 | 2016-02-11 12:41:40 +0200 | [diff] [blame] | 1822 | PyErr_Format(PyExc_SystemError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1823 | "more argument specifiers than keyword list entries " |
| 1824 | "(remaining format:'%s')", format); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1825 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | } |
Tim Peters | c2f0112 | 2001-10-27 07:25:06 +0000 | [diff] [blame] | 1827 | |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 1828 | if (nkwargs > 0) { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1829 | PyObject *key; |
| 1830 | Py_ssize_t j; |
| 1831 | /* make sure there are no arguments given by name and position */ |
| 1832 | for (i = pos; i < nargs; i++) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1833 | current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]); |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1834 | if (current_arg) { |
| 1835 | /* arg present in tuple and in dict */ |
| 1836 | PyErr_Format(PyExc_TypeError, |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1837 | "argument for %.200s%s given by name ('%s') " |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1838 | "and position (%d)", |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1839 | (fname == NULL) ? "function" : fname, |
| 1840 | (fname == NULL) ? "" : "()", |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1841 | kwlist[i], i+1); |
| 1842 | return cleanreturn(0, &freelist); |
| 1843 | } |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1844 | else if (PyErr_Occurred()) { |
| 1845 | return cleanreturn(0, &freelist); |
| 1846 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1847 | } |
| 1848 | /* make sure there are no extraneous keyword arguments */ |
| 1849 | j = 0; |
| 1850 | while (PyDict_Next(kwargs, &j, &key, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1851 | int match = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | if (!PyUnicode_Check(key)) { |
| 1853 | PyErr_SetString(PyExc_TypeError, |
| 1854 | "keywords must be strings"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1855 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 1857 | for (i = pos; i < len; i++) { |
| 1858 | if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) { |
Antoine Pitrou | 7056cb2 | 2013-02-17 01:04:57 +0100 | [diff] [blame] | 1859 | match = 1; |
| 1860 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | } |
| 1862 | } |
| 1863 | if (!match) { |
| 1864 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 1865 | "'%U' is an invalid keyword " |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 1866 | "argument for %.200s%s", |
| 1867 | key, |
| 1868 | (fname == NULL) ? "this function" : fname, |
| 1869 | (fname == NULL) ? "" : "()"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1870 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | } |
| 1872 | } |
| 1873 | } |
| 1874 | |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1875 | return cleanreturn(1, &freelist); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1879 | /* List of static parsers. */ |
| 1880 | static struct _PyArg_Parser *static_arg_parsers = NULL; |
| 1881 | |
| 1882 | static int |
| 1883 | parser_init(struct _PyArg_Parser *parser) |
| 1884 | { |
| 1885 | const char * const *keywords; |
| 1886 | const char *format, *msg; |
| 1887 | int i, len, min, max, nkw; |
| 1888 | PyObject *kwtuple; |
| 1889 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1890 | assert(parser->keywords != NULL); |
| 1891 | if (parser->kwtuple != NULL) { |
| 1892 | return 1; |
| 1893 | } |
| 1894 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1895 | keywords = parser->keywords; |
| 1896 | /* scan keywords and count the number of positional-only parameters */ |
| 1897 | for (i = 0; keywords[i] && !*keywords[i]; i++) { |
| 1898 | } |
| 1899 | parser->pos = i; |
| 1900 | /* scan keywords and get greatest possible nbr of args */ |
| 1901 | for (; keywords[i]; i++) { |
| 1902 | if (!*keywords[i]) { |
| 1903 | PyErr_SetString(PyExc_SystemError, |
| 1904 | "Empty keyword parameter name"); |
| 1905 | return 0; |
| 1906 | } |
| 1907 | } |
| 1908 | len = i; |
| 1909 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1910 | format = parser->format; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 1911 | if (format) { |
| 1912 | /* grab the function name or custom error msg first (mutually exclusive) */ |
| 1913 | parser->fname = strchr(parser->format, ':'); |
| 1914 | if (parser->fname) { |
| 1915 | parser->fname++; |
| 1916 | parser->custom_msg = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1917 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 1918 | else { |
| 1919 | parser->custom_msg = strchr(parser->format,';'); |
| 1920 | if (parser->custom_msg) |
| 1921 | parser->custom_msg++; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1922 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 1923 | |
| 1924 | min = max = INT_MAX; |
| 1925 | for (i = 0; i < len; i++) { |
| 1926 | if (*format == '|') { |
| 1927 | if (min != INT_MAX) { |
| 1928 | PyErr_SetString(PyExc_SystemError, |
| 1929 | "Invalid format string (| specified twice)"); |
| 1930 | return 0; |
| 1931 | } |
| 1932 | if (max != INT_MAX) { |
| 1933 | PyErr_SetString(PyExc_SystemError, |
| 1934 | "Invalid format string ($ before |)"); |
| 1935 | return 0; |
| 1936 | } |
| 1937 | min = i; |
| 1938 | format++; |
| 1939 | } |
| 1940 | if (*format == '$') { |
| 1941 | if (max != INT_MAX) { |
| 1942 | PyErr_SetString(PyExc_SystemError, |
| 1943 | "Invalid format string ($ specified twice)"); |
| 1944 | return 0; |
| 1945 | } |
| 1946 | if (i < parser->pos) { |
| 1947 | PyErr_SetString(PyExc_SystemError, |
| 1948 | "Empty parameter name after $"); |
| 1949 | return 0; |
| 1950 | } |
| 1951 | max = i; |
| 1952 | format++; |
| 1953 | } |
| 1954 | if (IS_END_OF_FORMAT(*format)) { |
| 1955 | PyErr_Format(PyExc_SystemError, |
| 1956 | "More keyword list entries (%d) than " |
| 1957 | "format specifiers (%d)", len, i); |
| 1958 | return 0; |
| 1959 | } |
| 1960 | |
| 1961 | msg = skipitem(&format, NULL, 0); |
| 1962 | if (msg) { |
| 1963 | PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, |
| 1964 | format); |
| 1965 | return 0; |
| 1966 | } |
| 1967 | } |
| 1968 | parser->min = Py_MIN(min, len); |
| 1969 | parser->max = Py_MIN(max, len); |
| 1970 | |
| 1971 | if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1972 | PyErr_Format(PyExc_SystemError, |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 1973 | "more argument specifiers than keyword list entries " |
| 1974 | "(remaining format:'%s')", format); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1975 | return 0; |
| 1976 | } |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | nkw = len - parser->pos; |
| 1980 | kwtuple = PyTuple_New(nkw); |
| 1981 | if (kwtuple == NULL) { |
| 1982 | return 0; |
| 1983 | } |
| 1984 | keywords = parser->keywords + parser->pos; |
| 1985 | for (i = 0; i < nkw; i++) { |
| 1986 | PyObject *str = PyUnicode_FromString(keywords[i]); |
| 1987 | if (str == NULL) { |
| 1988 | Py_DECREF(kwtuple); |
| 1989 | return 0; |
| 1990 | } |
| 1991 | PyUnicode_InternInPlace(&str); |
| 1992 | PyTuple_SET_ITEM(kwtuple, i, str); |
| 1993 | } |
| 1994 | parser->kwtuple = kwtuple; |
| 1995 | |
| 1996 | assert(parser->next == NULL); |
| 1997 | parser->next = static_arg_parsers; |
| 1998 | static_arg_parsers = parser; |
| 1999 | return 1; |
| 2000 | } |
| 2001 | |
| 2002 | static void |
| 2003 | parser_clear(struct _PyArg_Parser *parser) |
| 2004 | { |
| 2005 | Py_CLEAR(parser->kwtuple); |
| 2006 | } |
| 2007 | |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2008 | static PyObject* |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2009 | find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key) |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2010 | { |
| 2011 | Py_ssize_t i, nkwargs; |
| 2012 | |
| 2013 | nkwargs = PyTuple_GET_SIZE(kwnames); |
Sebastian Berg | 75bb07e | 2019-12-18 00:51:22 -0600 | [diff] [blame] | 2014 | for (i = 0; i < nkwargs; i++) { |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2015 | PyObject *kwname = PyTuple_GET_ITEM(kwnames, i); |
| 2016 | |
Sebastian Berg | 75bb07e | 2019-12-18 00:51:22 -0600 | [diff] [blame] | 2017 | /* kwname == key will normally find a match in since keyword keys |
| 2018 | should be interned strings; if not retry below in a new loop. */ |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2019 | if (kwname == key) { |
| 2020 | return kwstack[i]; |
| 2021 | } |
Sebastian Berg | 75bb07e | 2019-12-18 00:51:22 -0600 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | for (i = 0; i < nkwargs; i++) { |
| 2025 | PyObject *kwname = PyTuple_GET_ITEM(kwnames, i); |
Jeroen Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 2026 | assert(PyUnicode_Check(kwname)); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2027 | if (_PyUnicode_EQ(kwname, key)) { |
| 2028 | return kwstack[i]; |
| 2029 | } |
| 2030 | } |
| 2031 | return NULL; |
| 2032 | } |
| 2033 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2034 | static int |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2035 | vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2036 | PyObject *kwargs, PyObject *kwnames, |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2037 | struct _PyArg_Parser *parser, |
| 2038 | va_list *p_va, int flags) |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2039 | { |
| 2040 | PyObject *kwtuple; |
| 2041 | char msgbuf[512]; |
| 2042 | int levels[32]; |
| 2043 | const char *format; |
| 2044 | const char *msg; |
| 2045 | PyObject *keyword; |
| 2046 | int i, pos, len; |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2047 | Py_ssize_t nkwargs; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2048 | PyObject *current_arg; |
| 2049 | freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; |
| 2050 | freelist_t freelist; |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2051 | PyObject *const *kwstack = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2052 | |
| 2053 | freelist.entries = static_entries; |
| 2054 | freelist.first_available = 0; |
| 2055 | freelist.entries_malloced = 0; |
| 2056 | |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2057 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2058 | assert(kwargs == NULL || kwnames == NULL); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2059 | assert(p_va != NULL); |
| 2060 | |
Victor Stinner | 80ab22f | 2017-01-16 23:16:47 +0100 | [diff] [blame] | 2061 | if (parser == NULL) { |
| 2062 | PyErr_BadInternalCall(); |
| 2063 | return 0; |
| 2064 | } |
| 2065 | |
| 2066 | if (kwnames != NULL && !PyTuple_Check(kwnames)) { |
| 2067 | PyErr_BadInternalCall(); |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2071 | if (!parser_init(parser)) { |
| 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | kwtuple = parser->kwtuple; |
| 2076 | pos = parser->pos; |
Segev Finer | e7bc7aa | 2017-07-26 18:58:25 +0300 | [diff] [blame] | 2077 | len = pos + (int)PyTuple_GET_SIZE(kwtuple); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2078 | |
| 2079 | if (len > STATIC_FREELIST_ENTRIES) { |
| 2080 | freelist.entries = PyMem_NEW(freelistentry_t, len); |
| 2081 | if (freelist.entries == NULL) { |
| 2082 | PyErr_NoMemory(); |
| 2083 | return 0; |
| 2084 | } |
| 2085 | freelist.entries_malloced = 1; |
| 2086 | } |
| 2087 | |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2088 | if (kwargs != NULL) { |
| 2089 | nkwargs = PyDict_GET_SIZE(kwargs); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2090 | } |
| 2091 | else if (kwnames != NULL) { |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2092 | nkwargs = PyTuple_GET_SIZE(kwnames); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2093 | kwstack = args + nargs; |
| 2094 | } |
| 2095 | else { |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2096 | nkwargs = 0; |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2097 | } |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2098 | if (nargs + nkwargs > len) { |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 2099 | /* Adding "keyword" (when nargs == 0) prevents producing wrong error |
| 2100 | messages in some special cases (see bpo-31229). */ |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2101 | PyErr_Format(PyExc_TypeError, |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 2102 | "%.200s%s takes at most %d %sargument%s (%zd given)", |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2103 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2104 | (parser->fname == NULL) ? "" : "()", |
| 2105 | len, |
Oren Milman | bf9075a | 2017-08-23 21:16:48 +0300 | [diff] [blame] | 2106 | (nargs == 0) ? "keyword " : "", |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2107 | (len == 1) ? "" : "s", |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2108 | nargs + nkwargs); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2109 | return cleanreturn(0, &freelist); |
| 2110 | } |
| 2111 | if (parser->max < nargs) { |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2112 | if (parser->max == 0) { |
| 2113 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | f9f1cca | 2017-06-09 19:27:06 +0300 | [diff] [blame] | 2114 | "%.200s%s takes no positional arguments", |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2115 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2116 | (parser->fname == NULL) ? "" : "()"); |
| 2117 | } |
| 2118 | else { |
| 2119 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 2c0d3f4 | 2019-03-14 10:06:05 +0200 | [diff] [blame] | 2120 | "%.200s%s takes %s %d positional argument%s (%zd given)", |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2121 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2122 | (parser->fname == NULL) ? "" : "()", |
Serhiy Storchaka | f2f55e7 | 2019-03-13 23:03:22 +0200 | [diff] [blame] | 2123 | (parser->min < parser->max) ? "at most" : "exactly", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2124 | parser->max, |
| 2125 | parser->max == 1 ? "" : "s", |
| 2126 | nargs); |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2127 | } |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2128 | return cleanreturn(0, &freelist); |
| 2129 | } |
| 2130 | |
| 2131 | format = parser->format; |
| 2132 | /* convert tuple args and keyword args in same loop, using kwtuple to drive process */ |
| 2133 | for (i = 0; i < len; i++) { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2134 | if (*format == '|') { |
| 2135 | format++; |
| 2136 | } |
| 2137 | if (*format == '$') { |
| 2138 | format++; |
| 2139 | } |
| 2140 | assert(!IS_END_OF_FORMAT(*format)); |
| 2141 | |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2142 | if (i < nargs) { |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2143 | current_arg = args[i]; |
Victor Stinner | a9efb2f | 2016-09-09 17:40:22 -0700 | [diff] [blame] | 2144 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2145 | else if (nkwargs && i >= pos) { |
| 2146 | keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2147 | if (kwargs != NULL) { |
| 2148 | current_arg = PyDict_GetItemWithError(kwargs, keyword); |
| 2149 | if (!current_arg && PyErr_Occurred()) { |
| 2150 | return cleanreturn(0, &freelist); |
| 2151 | } |
| 2152 | } |
| 2153 | else { |
| 2154 | current_arg = find_keyword(kwnames, kwstack, keyword); |
| 2155 | } |
| 2156 | if (current_arg) { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2157 | --nkwargs; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2158 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2159 | } |
| 2160 | else { |
| 2161 | current_arg = NULL; |
| 2162 | } |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2163 | |
| 2164 | if (current_arg) { |
| 2165 | msg = convertitem(current_arg, &format, p_va, flags, |
| 2166 | levels, msgbuf, sizeof(msgbuf), &freelist); |
| 2167 | if (msg) { |
| 2168 | seterror(i+1, msg, levels, parser->fname, parser->custom_msg); |
| 2169 | return cleanreturn(0, &freelist); |
| 2170 | } |
| 2171 | continue; |
| 2172 | } |
| 2173 | |
| 2174 | if (i < parser->min) { |
| 2175 | /* Less arguments than required */ |
| 2176 | if (i < pos) { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2177 | Py_ssize_t min = Py_MIN(pos, parser->min); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2178 | PyErr_Format(PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2179 | "%.200s%s takes %s %d positional argument%s" |
Serhiy Storchaka | d53fe5f | 2019-03-13 22:59:55 +0200 | [diff] [blame] | 2180 | " (%zd given)", |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 2181 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2182 | (parser->fname == NULL) ? "" : "()", |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2183 | min < parser->max ? "at least" : "exactly", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2184 | min, |
| 2185 | min == 1 ? "" : "s", |
| 2186 | nargs); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2187 | } |
| 2188 | else { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2189 | keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 2190 | PyErr_Format(PyExc_TypeError, "%.200s%s missing required " |
| 2191 | "argument '%U' (pos %d)", |
| 2192 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2193 | (parser->fname == NULL) ? "" : "()", |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2194 | keyword, i+1); |
| 2195 | } |
| 2196 | return cleanreturn(0, &freelist); |
| 2197 | } |
| 2198 | /* current code reports success when all required args |
| 2199 | * fulfilled and no keyword args left, with no further |
| 2200 | * validation. XXX Maybe skip this in debug build ? |
| 2201 | */ |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2202 | if (!nkwargs) { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2203 | return cleanreturn(1, &freelist); |
| 2204 | } |
| 2205 | |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 2206 | /* We are into optional args, skip through to any remaining |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2207 | * keyword args */ |
| 2208 | msg = skipitem(&format, p_va, flags); |
| 2209 | assert(msg == NULL); |
| 2210 | } |
| 2211 | |
| 2212 | assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$')); |
| 2213 | |
Victor Stinner | c0083fc | 2017-01-17 00:37:42 +0100 | [diff] [blame] | 2214 | if (nkwargs > 0) { |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2215 | Py_ssize_t j; |
| 2216 | /* make sure there are no arguments given by name and position */ |
| 2217 | for (i = pos; i < nargs; i++) { |
| 2218 | keyword = PyTuple_GET_ITEM(kwtuple, i - pos); |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 2219 | if (kwargs != NULL) { |
| 2220 | current_arg = PyDict_GetItemWithError(kwargs, keyword); |
| 2221 | if (!current_arg && PyErr_Occurred()) { |
| 2222 | return cleanreturn(0, &freelist); |
| 2223 | } |
| 2224 | } |
| 2225 | else { |
| 2226 | current_arg = find_keyword(kwnames, kwstack, keyword); |
| 2227 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2228 | if (current_arg) { |
| 2229 | /* arg present in tuple and in dict */ |
| 2230 | PyErr_Format(PyExc_TypeError, |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 2231 | "argument for %.200s%s given by name ('%U') " |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2232 | "and position (%d)", |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 2233 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2234 | (parser->fname == NULL) ? "" : "()", |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2235 | keyword, i+1); |
| 2236 | return cleanreturn(0, &freelist); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2237 | } |
| 2238 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2239 | /* make sure there are no extraneous keyword arguments */ |
| 2240 | j = 0; |
| 2241 | while (1) { |
| 2242 | int match; |
| 2243 | if (kwargs != NULL) { |
| 2244 | if (!PyDict_Next(kwargs, &j, &keyword, NULL)) |
| 2245 | break; |
| 2246 | } |
| 2247 | else { |
| 2248 | if (j >= PyTuple_GET_SIZE(kwnames)) |
| 2249 | break; |
| 2250 | keyword = PyTuple_GET_ITEM(kwnames, j); |
| 2251 | j++; |
| 2252 | } |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2253 | |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2254 | match = PySequence_Contains(kwtuple, keyword); |
| 2255 | if (match <= 0) { |
| 2256 | if (!match) { |
| 2257 | PyErr_Format(PyExc_TypeError, |
Jeroen Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 2258 | "'%S' is an invalid keyword " |
Michael Seifert | 64c8f70 | 2017-04-09 09:47:12 +0200 | [diff] [blame] | 2259 | "argument for %.200s%s", |
| 2260 | keyword, |
| 2261 | (parser->fname == NULL) ? "this function" : parser->fname, |
| 2262 | (parser->fname == NULL) ? "" : "()"); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2263 | } |
Serhiy Storchaka | 1741441 | 2017-01-17 10:07:25 +0200 | [diff] [blame] | 2264 | return cleanreturn(0, &freelist); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | return cleanreturn(1, &freelist); |
| 2270 | } |
| 2271 | |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2272 | static int |
| 2273 | vgetargskeywordsfast(PyObject *args, PyObject *keywords, |
| 2274 | struct _PyArg_Parser *parser, va_list *p_va, int flags) |
| 2275 | { |
| 2276 | PyObject **stack; |
| 2277 | Py_ssize_t nargs; |
| 2278 | |
Victor Stinner | 80ab22f | 2017-01-16 23:16:47 +0100 | [diff] [blame] | 2279 | if (args == NULL |
| 2280 | || !PyTuple_Check(args) |
| 2281 | || (keywords != NULL && !PyDict_Check(keywords))) |
| 2282 | { |
| 2283 | PyErr_BadInternalCall(); |
| 2284 | return 0; |
| 2285 | } |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2286 | |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 2287 | stack = _PyTuple_ITEMS(args); |
Victor Stinner | f0ccbbb | 2016-09-09 17:40:38 -0700 | [diff] [blame] | 2288 | nargs = PyTuple_GET_SIZE(args); |
| 2289 | return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL, |
| 2290 | parser, p_va, flags); |
| 2291 | } |
| 2292 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2293 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 2294 | #undef _PyArg_UnpackKeywords |
| 2295 | |
| 2296 | PyObject * const * |
| 2297 | _PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs, |
| 2298 | PyObject *kwargs, PyObject *kwnames, |
| 2299 | struct _PyArg_Parser *parser, |
| 2300 | int minpos, int maxpos, int minkw, |
| 2301 | PyObject **buf) |
| 2302 | { |
| 2303 | PyObject *kwtuple; |
| 2304 | PyObject *keyword; |
| 2305 | int i, posonly, minposonly, maxargs; |
| 2306 | int reqlimit = minkw ? maxpos + minkw : minpos; |
| 2307 | Py_ssize_t nkwargs; |
| 2308 | PyObject *current_arg; |
| 2309 | PyObject * const *kwstack = NULL; |
| 2310 | |
| 2311 | assert(kwargs == NULL || PyDict_Check(kwargs)); |
| 2312 | assert(kwargs == NULL || kwnames == NULL); |
| 2313 | |
| 2314 | if (parser == NULL) { |
| 2315 | PyErr_BadInternalCall(); |
| 2316 | return NULL; |
| 2317 | } |
| 2318 | |
| 2319 | if (kwnames != NULL && !PyTuple_Check(kwnames)) { |
| 2320 | PyErr_BadInternalCall(); |
| 2321 | return NULL; |
| 2322 | } |
| 2323 | |
| 2324 | if (args == NULL && nargs == 0) { |
| 2325 | args = buf; |
| 2326 | } |
| 2327 | |
| 2328 | if (!parser_init(parser)) { |
| 2329 | return NULL; |
| 2330 | } |
| 2331 | |
| 2332 | kwtuple = parser->kwtuple; |
| 2333 | posonly = parser->pos; |
| 2334 | minposonly = Py_MIN(posonly, minpos); |
| 2335 | maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple); |
| 2336 | |
| 2337 | if (kwargs != NULL) { |
| 2338 | nkwargs = PyDict_GET_SIZE(kwargs); |
| 2339 | } |
| 2340 | else if (kwnames != NULL) { |
| 2341 | nkwargs = PyTuple_GET_SIZE(kwnames); |
| 2342 | kwstack = args + nargs; |
| 2343 | } |
| 2344 | else { |
| 2345 | nkwargs = 0; |
| 2346 | } |
| 2347 | if (nkwargs == 0 && minkw == 0 && minpos <= nargs && nargs <= maxpos) { |
| 2348 | /* Fast path. */ |
| 2349 | return args; |
| 2350 | } |
| 2351 | if (nargs + nkwargs > maxargs) { |
| 2352 | /* Adding "keyword" (when nargs == 0) prevents producing wrong error |
| 2353 | messages in some special cases (see bpo-31229). */ |
| 2354 | PyErr_Format(PyExc_TypeError, |
| 2355 | "%.200s%s takes at most %d %sargument%s (%zd given)", |
| 2356 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2357 | (parser->fname == NULL) ? "" : "()", |
| 2358 | maxargs, |
| 2359 | (nargs == 0) ? "keyword " : "", |
| 2360 | (maxargs == 1) ? "" : "s", |
| 2361 | nargs + nkwargs); |
| 2362 | return NULL; |
| 2363 | } |
| 2364 | if (nargs > maxpos) { |
| 2365 | if (maxpos == 0) { |
| 2366 | PyErr_Format(PyExc_TypeError, |
| 2367 | "%.200s%s takes no positional arguments", |
| 2368 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2369 | (parser->fname == NULL) ? "" : "()"); |
| 2370 | } |
| 2371 | else { |
| 2372 | PyErr_Format(PyExc_TypeError, |
| 2373 | "%.200s%s takes %s %d positional argument%s (%zd given)", |
| 2374 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2375 | (parser->fname == NULL) ? "" : "()", |
| 2376 | (minpos < maxpos) ? "at most" : "exactly", |
| 2377 | maxpos, |
| 2378 | (maxpos == 1) ? "" : "s", |
| 2379 | nargs); |
| 2380 | } |
| 2381 | return NULL; |
| 2382 | } |
| 2383 | if (nargs < minposonly) { |
| 2384 | PyErr_Format(PyExc_TypeError, |
| 2385 | "%.200s%s takes %s %d positional argument%s" |
| 2386 | " (%zd given)", |
| 2387 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2388 | (parser->fname == NULL) ? "" : "()", |
| 2389 | minposonly < maxpos ? "at least" : "exactly", |
| 2390 | minposonly, |
| 2391 | minposonly == 1 ? "" : "s", |
| 2392 | nargs); |
| 2393 | return NULL; |
| 2394 | } |
| 2395 | |
| 2396 | /* copy tuple args */ |
| 2397 | for (i = 0; i < nargs; i++) { |
| 2398 | buf[i] = args[i]; |
| 2399 | } |
| 2400 | |
| 2401 | /* copy keyword args using kwtuple to drive process */ |
Serhiy Storchaka | 1b0393d | 2019-03-16 19:45:00 +0200 | [diff] [blame] | 2402 | for (i = Py_MAX((int)nargs, posonly); i < maxargs; i++) { |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 2403 | if (nkwargs) { |
| 2404 | keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); |
| 2405 | if (kwargs != NULL) { |
| 2406 | current_arg = PyDict_GetItemWithError(kwargs, keyword); |
| 2407 | if (!current_arg && PyErr_Occurred()) { |
| 2408 | return NULL; |
| 2409 | } |
| 2410 | } |
| 2411 | else { |
| 2412 | current_arg = find_keyword(kwnames, kwstack, keyword); |
| 2413 | } |
| 2414 | } |
| 2415 | else if (i >= reqlimit) { |
| 2416 | break; |
| 2417 | } |
| 2418 | else { |
| 2419 | current_arg = NULL; |
| 2420 | } |
| 2421 | |
| 2422 | buf[i] = current_arg; |
| 2423 | |
| 2424 | if (current_arg) { |
| 2425 | --nkwargs; |
| 2426 | } |
| 2427 | else if (i < minpos || (maxpos <= i && i < reqlimit)) { |
| 2428 | /* Less arguments than required */ |
| 2429 | keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); |
| 2430 | PyErr_Format(PyExc_TypeError, "%.200s%s missing required " |
| 2431 | "argument '%U' (pos %d)", |
| 2432 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2433 | (parser->fname == NULL) ? "" : "()", |
| 2434 | keyword, i+1); |
| 2435 | return NULL; |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | if (nkwargs > 0) { |
| 2440 | Py_ssize_t j; |
| 2441 | /* make sure there are no arguments given by name and position */ |
| 2442 | for (i = posonly; i < nargs; i++) { |
| 2443 | keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); |
| 2444 | if (kwargs != NULL) { |
| 2445 | current_arg = PyDict_GetItemWithError(kwargs, keyword); |
| 2446 | if (!current_arg && PyErr_Occurred()) { |
| 2447 | return NULL; |
| 2448 | } |
| 2449 | } |
| 2450 | else { |
| 2451 | current_arg = find_keyword(kwnames, kwstack, keyword); |
| 2452 | } |
| 2453 | if (current_arg) { |
| 2454 | /* arg present in tuple and in dict */ |
| 2455 | PyErr_Format(PyExc_TypeError, |
| 2456 | "argument for %.200s%s given by name ('%U') " |
| 2457 | "and position (%d)", |
| 2458 | (parser->fname == NULL) ? "function" : parser->fname, |
| 2459 | (parser->fname == NULL) ? "" : "()", |
| 2460 | keyword, i+1); |
| 2461 | return NULL; |
| 2462 | } |
| 2463 | } |
| 2464 | /* make sure there are no extraneous keyword arguments */ |
| 2465 | j = 0; |
| 2466 | while (1) { |
| 2467 | int match; |
| 2468 | if (kwargs != NULL) { |
| 2469 | if (!PyDict_Next(kwargs, &j, &keyword, NULL)) |
| 2470 | break; |
| 2471 | } |
| 2472 | else { |
| 2473 | if (j >= PyTuple_GET_SIZE(kwnames)) |
| 2474 | break; |
| 2475 | keyword = PyTuple_GET_ITEM(kwnames, j); |
| 2476 | j++; |
| 2477 | } |
| 2478 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 2479 | match = PySequence_Contains(kwtuple, keyword); |
| 2480 | if (match <= 0) { |
| 2481 | if (!match) { |
| 2482 | PyErr_Format(PyExc_TypeError, |
Jeroen Demeyer | 0567786 | 2019-08-16 12:41:27 +0200 | [diff] [blame] | 2483 | "'%S' is an invalid keyword " |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 2484 | "argument for %.200s%s", |
| 2485 | keyword, |
| 2486 | (parser->fname == NULL) ? "this function" : parser->fname, |
| 2487 | (parser->fname == NULL) ? "" : "()"); |
| 2488 | } |
| 2489 | return NULL; |
| 2490 | } |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | return buf; |
| 2495 | } |
| 2496 | |
| 2497 | |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2498 | static const char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2499 | skipitem(const char **p_format, va_list *p_va, int flags) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 2500 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2501 | const char *format = *p_format; |
| 2502 | char c = *format++; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2504 | switch (c) { |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 2505 | |
Larry Hastings | a347901 | 2012-05-08 23:52:03 -0700 | [diff] [blame] | 2506 | /* |
| 2507 | * codes that take a single data pointer as an argument |
| 2508 | * (the type of the pointer is irrelevant) |
| 2509 | */ |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 2510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2511 | case 'b': /* byte -- very short int */ |
| 2512 | case 'B': /* byte as bitfield */ |
| 2513 | case 'h': /* short int */ |
| 2514 | case 'H': /* short int as bitfield */ |
| 2515 | case 'i': /* int */ |
| 2516 | case 'I': /* int sized bitfield */ |
| 2517 | case 'l': /* long int */ |
| 2518 | case 'k': /* long int sized bitfield */ |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 2519 | case 'L': /* long long */ |
| 2520 | case 'K': /* long long sized bitfield */ |
Larry Hastings | a347901 | 2012-05-08 23:52:03 -0700 | [diff] [blame] | 2521 | case 'n': /* Py_ssize_t */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | case 'f': /* float */ |
| 2523 | case 'd': /* double */ |
| 2524 | case 'D': /* complex double */ |
| 2525 | case 'c': /* char */ |
| 2526 | case 'C': /* unicode char */ |
Larry Hastings | 10ba07a | 2012-05-07 02:44:50 -0700 | [diff] [blame] | 2527 | case 'p': /* boolean predicate */ |
Larry Hastings | a347901 | 2012-05-08 23:52:03 -0700 | [diff] [blame] | 2528 | case 'S': /* string object */ |
| 2529 | case 'Y': /* string object */ |
| 2530 | case 'U': /* unicode string object */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2531 | { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2532 | if (p_va != NULL) { |
| 2533 | (void) va_arg(*p_va, void *); |
| 2534 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2535 | break; |
| 2536 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | /* string codes */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2540 | case 'e': /* string with encoding */ |
| 2541 | { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2542 | if (p_va != NULL) { |
| 2543 | (void) va_arg(*p_va, const char *); |
| 2544 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | if (!(*format == 's' || *format == 't')) |
| 2546 | /* after 'e', only 's' and 't' is allowed */ |
| 2547 | goto err; |
| 2548 | format++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | } |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 2550 | /* fall through */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2551 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2552 | case 's': /* string */ |
| 2553 | case 'z': /* string or None */ |
| 2554 | case 'y': /* bytes */ |
| 2555 | case 'u': /* unicode string */ |
Larry Hastings | d9e4a41 | 2012-05-08 03:51:18 -0700 | [diff] [blame] | 2556 | case 'Z': /* unicode string or None */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2557 | case 'w': /* buffer, read-write */ |
| 2558 | { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2559 | if (p_va != NULL) { |
| 2560 | (void) va_arg(*p_va, char **); |
| 2561 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2562 | if (*format == '#') { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2563 | if (p_va != NULL) { |
| 2564 | if (flags & FLAG_SIZE_T) |
| 2565 | (void) va_arg(*p_va, Py_ssize_t *); |
Inada Naoki | d3c72a2 | 2019-03-23 21:04:40 +0900 | [diff] [blame] | 2566 | else { |
| 2567 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 2568 | "PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) { |
| 2569 | return NULL; |
| 2570 | } |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2571 | (void) va_arg(*p_va, int *); |
Inada Naoki | d3c72a2 | 2019-03-23 21:04:40 +0900 | [diff] [blame] | 2572 | } |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2573 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2574 | format++; |
Serhiy Storchaka | 504373c | 2018-07-11 17:41:43 +0300 | [diff] [blame] | 2575 | } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w') |
| 2576 | && *format == '*') |
| 2577 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2578 | format++; |
| 2579 | } |
| 2580 | break; |
| 2581 | } |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 2582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2583 | case 'O': /* object */ |
| 2584 | { |
| 2585 | if (*format == '!') { |
| 2586 | format++; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2587 | if (p_va != NULL) { |
| 2588 | (void) va_arg(*p_va, PyTypeObject*); |
| 2589 | (void) va_arg(*p_va, PyObject **); |
| 2590 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | } |
| 2592 | else if (*format == '&') { |
| 2593 | typedef int (*converter)(PyObject *, void *); |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2594 | if (p_va != NULL) { |
| 2595 | (void) va_arg(*p_va, converter); |
| 2596 | (void) va_arg(*p_va, void *); |
| 2597 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2598 | format++; |
| 2599 | } |
| 2600 | else { |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2601 | if (p_va != NULL) { |
| 2602 | (void) va_arg(*p_va, PyObject **); |
| 2603 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 | } |
| 2605 | break; |
| 2606 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2607 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2608 | case '(': /* bypass tuple, not handled at all previously */ |
| 2609 | { |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2610 | const char *msg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2611 | for (;;) { |
| 2612 | if (*format==')') |
| 2613 | break; |
| 2614 | if (IS_END_OF_FORMAT(*format)) |
| 2615 | return "Unmatched left paren in format " |
| 2616 | "string"; |
| 2617 | msg = skipitem(&format, p_va, flags); |
| 2618 | if (msg) |
| 2619 | return msg; |
| 2620 | } |
| 2621 | format++; |
| 2622 | break; |
| 2623 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2625 | case ')': |
| 2626 | return "Unmatched right paren in format string"; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 2627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2628 | default: |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 2629 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2630 | return "impossible<bad format char>"; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | } |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 2633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | *p_format = format; |
| 2635 | return NULL; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 2636 | } |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 2637 | |
| 2638 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2639 | #undef _PyArg_CheckPositional |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 2640 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2641 | int |
| 2642 | _PyArg_CheckPositional(const char *name, Py_ssize_t nargs, |
| 2643 | Py_ssize_t min, Py_ssize_t max) |
| 2644 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | assert(min >= 0); |
| 2646 | assert(min <= max); |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2647 | |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2648 | if (nargs < min) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2649 | if (name != NULL) |
| 2650 | PyErr_Format( |
| 2651 | PyExc_TypeError, |
Xtreak | 1426daa | 2018-07-23 01:43:26 +0530 | [diff] [blame] | 2652 | "%.200s expected %s%zd argument%s, got %zd", |
| 2653 | name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2654 | else |
| 2655 | PyErr_Format( |
| 2656 | PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2657 | "unpacked tuple should have %s%zd element%s," |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2658 | " but has %zd", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2659 | (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2660 | return 0; |
| 2661 | } |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2662 | |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2663 | if (nargs == 0) { |
Raymond Hettinger | 9423023 | 2016-03-26 03:02:48 -0700 | [diff] [blame] | 2664 | return 1; |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2665 | } |
| 2666 | |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2667 | if (nargs > max) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2668 | if (name != NULL) |
| 2669 | PyErr_Format( |
| 2670 | PyExc_TypeError, |
Xtreak | 1426daa | 2018-07-23 01:43:26 +0530 | [diff] [blame] | 2671 | "%.200s expected %s%zd argument%s, got %zd", |
| 2672 | name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | else |
| 2674 | PyErr_Format( |
| 2675 | PyExc_TypeError, |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2676 | "unpacked tuple should have %s%zd element%s," |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | " but has %zd", |
Xtreak | 6326278 | 2018-12-21 20:15:13 +0530 | [diff] [blame] | 2678 | (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | return 0; |
| 2680 | } |
Raymond Hettinger | 9423023 | 2016-03-26 03:02:48 -0700 | [diff] [blame] | 2681 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2682 | return 1; |
| 2683 | } |
| 2684 | |
| 2685 | static int |
| 2686 | unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name, |
| 2687 | Py_ssize_t min, Py_ssize_t max, va_list vargs) |
| 2688 | { |
| 2689 | Py_ssize_t i; |
| 2690 | PyObject **o; |
| 2691 | |
| 2692 | if (!_PyArg_CheckPositional(name, nargs, min, max)) { |
| 2693 | return 0; |
| 2694 | } |
| 2695 | |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2696 | for (i = 0; i < nargs; i++) { |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2697 | o = va_arg(vargs, PyObject **); |
| 2698 | *o = args[i]; |
| 2699 | } |
| 2700 | return 1; |
| 2701 | } |
| 2702 | |
| 2703 | int |
| 2704 | PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) |
| 2705 | { |
| 2706 | PyObject **stack; |
| 2707 | Py_ssize_t nargs; |
| 2708 | int retval; |
| 2709 | va_list vargs; |
| 2710 | |
| 2711 | if (!PyTuple_Check(args)) { |
| 2712 | PyErr_SetString(PyExc_SystemError, |
| 2713 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 2714 | return 0; |
| 2715 | } |
Victor Stinner | d17a693 | 2018-11-09 16:56:48 +0100 | [diff] [blame] | 2716 | stack = _PyTuple_ITEMS(args); |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2717 | nargs = PyTuple_GET_SIZE(args); |
| 2718 | |
Raymond Hettinger | 9423023 | 2016-03-26 03:02:48 -0700 | [diff] [blame] | 2719 | #ifdef HAVE_STDARG_PROTOTYPES |
| 2720 | va_start(vargs, max); |
| 2721 | #else |
| 2722 | va_start(vargs); |
| 2723 | #endif |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2724 | retval = unpack_stack(stack, nargs, name, min, max, vargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2725 | va_end(vargs); |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2726 | return retval; |
| 2727 | } |
| 2728 | |
| 2729 | int |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2730 | _PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name, |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2731 | Py_ssize_t min, Py_ssize_t max, ...) |
| 2732 | { |
| 2733 | int retval; |
| 2734 | va_list vargs; |
| 2735 | |
| 2736 | #ifdef HAVE_STDARG_PROTOTYPES |
| 2737 | va_start(vargs, max); |
| 2738 | #else |
| 2739 | va_start(vargs); |
| 2740 | #endif |
Victor Stinner | fdbc397 | 2017-02-01 17:42:18 +0100 | [diff] [blame] | 2741 | retval = unpack_stack(args, nargs, name, min, max, vargs); |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2742 | va_end(vargs); |
| 2743 | return retval; |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 2744 | } |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2745 | |
| 2746 | |
Serhiy Storchaka | 68a001d | 2017-02-06 10:41:46 +0200 | [diff] [blame] | 2747 | #undef _PyArg_NoKeywords |
Dong-hee Na | 87ec86c | 2020-03-16 23:06:20 +0900 | [diff] [blame] | 2748 | #undef _PyArg_NoKwnames |
Serhiy Storchaka | 68a001d | 2017-02-06 10:41:46 +0200 | [diff] [blame] | 2749 | #undef _PyArg_NoPositional |
| 2750 | |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2751 | /* For type constructors that don't take keyword args |
| 2752 | * |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2753 | * Sets a TypeError and returns 0 if the args/kwargs is |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2754 | * not empty, returns 1 otherwise |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2755 | */ |
| 2756 | int |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2757 | _PyArg_NoKeywords(const char *funcname, PyObject *kwargs) |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 2758 | { |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2759 | if (kwargs == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | return 1; |
Victor Stinner | fe54dd8 | 2017-01-17 02:33:55 +0100 | [diff] [blame] | 2761 | } |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2762 | if (!PyDict_CheckExact(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | PyErr_BadInternalCall(); |
| 2764 | return 0; |
| 2765 | } |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2766 | if (PyDict_GET_SIZE(kwargs) == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | return 1; |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2768 | } |
| 2769 | |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2770 | PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2771 | funcname); |
| 2772 | return 0; |
| 2773 | } |
| 2774 | |
Victor Stinner | 29d39cc | 2017-01-17 01:40:01 +0100 | [diff] [blame] | 2775 | int |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2776 | _PyArg_NoPositional(const char *funcname, PyObject *args) |
| 2777 | { |
| 2778 | if (args == NULL) |
| 2779 | return 1; |
| 2780 | if (!PyTuple_CheckExact(args)) { |
| 2781 | PyErr_BadInternalCall(); |
| 2782 | return 0; |
| 2783 | } |
| 2784 | if (PyTuple_GET_SIZE(args) == 0) |
| 2785 | return 1; |
| 2786 | |
Serhiy Storchaka | 5eb788b | 2017-06-06 18:45:22 +0300 | [diff] [blame] | 2787 | PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments", |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 2788 | funcname); |
| 2789 | return 0; |
| 2790 | } |
| 2791 | |
Dong-hee Na | 87ec86c | 2020-03-16 23:06:20 +0900 | [diff] [blame] | 2792 | int |
| 2793 | _PyArg_NoKwnames(const char *funcname, PyObject *kwnames) |
| 2794 | { |
| 2795 | if (kwnames == NULL) { |
| 2796 | return 1; |
| 2797 | } |
| 2798 | |
| 2799 | assert(PyTuple_CheckExact(kwnames)); |
| 2800 | |
| 2801 | if (PyTuple_GET_SIZE(kwnames) == 0) { |
| 2802 | return 1; |
| 2803 | } |
| 2804 | |
| 2805 | PyErr_Format(PyExc_TypeError, "%s() takes no keyword arguments", funcname); |
| 2806 | return 0; |
| 2807 | } |
| 2808 | |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 2809 | void |
| 2810 | _PyArg_Fini(void) |
| 2811 | { |
| 2812 | struct _PyArg_Parser *tmp, *s = static_arg_parsers; |
| 2813 | while (s) { |
| 2814 | tmp = s->next; |
| 2815 | s->next = NULL; |
| 2816 | parser_clear(s); |
| 2817 | s = tmp; |
| 2818 | } |
| 2819 | static_arg_parsers = NULL; |
| 2820 | } |
| 2821 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2822 | #ifdef __cplusplus |
| 2823 | }; |
| 2824 | #endif |