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