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