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