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