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