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