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 | { |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame^] | 969 | Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **); |
| 970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | if (*format == '#') { /* any buffer-like object */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 972 | /* "s#" or "Z#" */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | FETCH_SIZE; |
Travis E. Oliphant | ddacf96 | 2007-10-13 21:03:27 +0000 | [diff] [blame] | 974 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 975 | if (c == 'Z' && arg == Py_None) { |
| 976 | *p = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | STORE_SIZE(0); |
| 978 | } |
| 979 | else if (PyUnicode_Check(arg)) { |
| 980 | *p = PyUnicode_AS_UNICODE(arg); |
| 981 | STORE_SIZE(PyUnicode_GET_SIZE(arg)); |
| 982 | } |
Victor Stinner | 5216e6d | 2010-06-08 21:45:51 +0000 | [diff] [blame] | 983 | else |
| 984 | return converterr("str or None", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 985 | format++; |
| 986 | } else { |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 987 | /* "s" or "Z" */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 988 | if (c == 'Z' && arg == Py_None) |
| 989 | *p = NULL; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 990 | else if (PyUnicode_Check(arg)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | *p = PyUnicode_AS_UNICODE(arg); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 992 | if (Py_UNICODE_strlen(*p) != PyUnicode_GET_SIZE(arg)) |
| 993 | return converterr( |
| 994 | "str without null character or None", |
| 995 | arg, msgbuf, bufsize); |
| 996 | } else |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 997 | return converterr(c == 'Z' ? "str or None" : "str", |
| 998 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | } |
| 1000 | break; |
| 1001 | } |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | case 'e': {/* encoded string */ |
| 1004 | char **buffer; |
| 1005 | const char *encoding; |
| 1006 | PyObject *s; |
| 1007 | int recode_strings; |
| 1008 | Py_ssize_t size; |
| 1009 | const char *ptr; |
Jeremy Hylton | 4819e97 | 2001-10-11 14:40:37 +0000 | [diff] [blame] | 1010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | /* Get 'e' parameter: the encoding name */ |
| 1012 | encoding = (const char *)va_arg(*p_va, const char *); |
| 1013 | if (encoding == NULL) |
| 1014 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | /* Get output buffer parameter: |
| 1017 | 's' (recode all objects via Unicode) or |
| 1018 | 't' (only recode non-string objects) |
| 1019 | */ |
| 1020 | if (*format == 's') |
| 1021 | recode_strings = 1; |
| 1022 | else if (*format == 't') |
| 1023 | recode_strings = 0; |
| 1024 | else |
| 1025 | return converterr( |
| 1026 | "(unknown parser marker combination)", |
| 1027 | arg, msgbuf, bufsize); |
| 1028 | buffer = (char **)va_arg(*p_va, char **); |
| 1029 | format++; |
| 1030 | if (buffer == NULL) |
| 1031 | return converterr("(buffer is NULL)", |
| 1032 | arg, msgbuf, bufsize); |
Benjamin Peterson | 9edd2bd | 2008-08-27 00:31:37 +0000 | [diff] [blame] | 1033 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | /* Encode object */ |
| 1035 | if (!recode_strings && |
| 1036 | (PyBytes_Check(arg) || PyByteArray_Check(arg))) { |
| 1037 | s = arg; |
| 1038 | Py_INCREF(s); |
| 1039 | if (PyObject_AsCharBuffer(s, &ptr, &size) < 0) |
| 1040 | return converterr("(AsCharBuffer failed)", |
| 1041 | arg, msgbuf, bufsize); |
| 1042 | } |
| 1043 | else { |
| 1044 | PyObject *u; |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | /* Convert object to Unicode */ |
| 1047 | u = PyUnicode_FromObject(arg); |
| 1048 | if (u == NULL) |
| 1049 | return converterr( |
| 1050 | "string or unicode or text buffer", |
| 1051 | arg, msgbuf, bufsize); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | /* Encode object; use default error handling */ |
| 1054 | s = PyUnicode_AsEncodedString(u, |
| 1055 | encoding, |
| 1056 | NULL); |
| 1057 | Py_DECREF(u); |
| 1058 | if (s == NULL) |
| 1059 | return converterr("(encoding failed)", |
| 1060 | arg, msgbuf, bufsize); |
| 1061 | if (!PyBytes_Check(s)) { |
| 1062 | Py_DECREF(s); |
| 1063 | return converterr( |
| 1064 | "(encoder failed to return bytes)", |
| 1065 | arg, msgbuf, bufsize); |
| 1066 | } |
| 1067 | size = PyBytes_GET_SIZE(s); |
| 1068 | ptr = PyBytes_AS_STRING(s); |
| 1069 | if (ptr == NULL) |
| 1070 | ptr = ""; |
| 1071 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1072 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | /* Write output; output is guaranteed to be 0-terminated */ |
| 1074 | if (*format == '#') { |
| 1075 | /* Using buffer length parameter '#': |
| 1076 | |
| 1077 | - if *buffer is NULL, a new buffer of the |
| 1078 | needed size is allocated and the data |
| 1079 | copied into it; *buffer is updated to point |
| 1080 | to the new buffer; the caller is |
| 1081 | responsible for PyMem_Free()ing it after |
| 1082 | usage |
| 1083 | |
| 1084 | - if *buffer is not NULL, the data is |
| 1085 | copied to *buffer; *buffer_len has to be |
| 1086 | set to the size of the buffer on input; |
| 1087 | buffer overflow is signalled with an error; |
| 1088 | buffer has to provide enough room for the |
| 1089 | encoded string plus the trailing 0-byte |
| 1090 | |
| 1091 | - in both cases, *buffer_len is updated to |
| 1092 | the size of the buffer /excluding/ the |
| 1093 | trailing 0-byte |
| 1094 | |
| 1095 | */ |
| 1096 | FETCH_SIZE; |
| 1097 | |
| 1098 | format++; |
| 1099 | if (q == NULL && q2 == NULL) { |
| 1100 | Py_DECREF(s); |
| 1101 | return converterr( |
| 1102 | "(buffer_len is NULL)", |
| 1103 | arg, msgbuf, bufsize); |
| 1104 | } |
| 1105 | if (*buffer == NULL) { |
| 1106 | *buffer = PyMem_NEW(char, size + 1); |
| 1107 | if (*buffer == NULL) { |
| 1108 | Py_DECREF(s); |
Victor Stinner | 2872e5b | 2010-06-06 20:38:02 +0000 | [diff] [blame] | 1109 | PyErr_NoMemory(); |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1110 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1111 | } |
Victor Stinner | a093d0d | 2011-01-04 11:16:48 +0000 | [diff] [blame] | 1112 | if (addcleanup(*buffer, freelist, 0)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | Py_DECREF(s); |
| 1114 | return converterr( |
| 1115 | "(cleanup problem)", |
| 1116 | arg, msgbuf, bufsize); |
| 1117 | } |
| 1118 | } else { |
| 1119 | if (size + 1 > BUFFER_LEN) { |
| 1120 | Py_DECREF(s); |
| 1121 | return converterr( |
| 1122 | "(buffer overflow)", |
| 1123 | arg, msgbuf, bufsize); |
| 1124 | } |
| 1125 | } |
| 1126 | memcpy(*buffer, ptr, size+1); |
| 1127 | STORE_SIZE(size); |
| 1128 | } else { |
| 1129 | /* Using a 0-terminated buffer: |
| 1130 | |
| 1131 | - the encoded string has to be 0-terminated |
| 1132 | for this variant to work; if it is not, an |
| 1133 | error raised |
| 1134 | |
| 1135 | - a new buffer of the needed size is |
| 1136 | allocated and the data copied into it; |
| 1137 | *buffer is updated to point to the new |
| 1138 | buffer; the caller is responsible for |
| 1139 | PyMem_Free()ing it after usage |
| 1140 | |
| 1141 | */ |
| 1142 | if ((Py_ssize_t)strlen(ptr) != size) { |
| 1143 | Py_DECREF(s); |
| 1144 | return converterr( |
| 1145 | "encoded string without NULL bytes", |
| 1146 | arg, msgbuf, bufsize); |
| 1147 | } |
| 1148 | *buffer = PyMem_NEW(char, size + 1); |
| 1149 | if (*buffer == NULL) { |
| 1150 | Py_DECREF(s); |
Victor Stinner | 2872e5b | 2010-06-06 20:38:02 +0000 | [diff] [blame] | 1151 | PyErr_NoMemory(); |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1152 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | } |
Victor Stinner | a093d0d | 2011-01-04 11:16:48 +0000 | [diff] [blame] | 1154 | if (addcleanup(*buffer, freelist, 0)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | Py_DECREF(s); |
| 1156 | return converterr("(cleanup problem)", |
| 1157 | arg, msgbuf, bufsize); |
| 1158 | } |
| 1159 | memcpy(*buffer, ptr, size+1); |
| 1160 | } |
| 1161 | Py_DECREF(s); |
| 1162 | break; |
| 1163 | } |
| 1164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | case 'S': { /* PyBytes object */ |
| 1166 | PyObject **p = va_arg(*p_va, PyObject **); |
| 1167 | if (PyBytes_Check(arg)) |
| 1168 | *p = arg; |
| 1169 | else |
| 1170 | return converterr("bytes", arg, msgbuf, bufsize); |
| 1171 | break; |
| 1172 | } |
| 1173 | |
| 1174 | case 'Y': { /* PyByteArray object */ |
| 1175 | PyObject **p = va_arg(*p_va, PyObject **); |
| 1176 | if (PyByteArray_Check(arg)) |
| 1177 | *p = arg; |
| 1178 | else |
Victor Stinner | 5216e6d | 2010-06-08 21:45:51 +0000 | [diff] [blame] | 1179 | return converterr("bytearray", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | case 'U': { /* PyUnicode object */ |
| 1184 | PyObject **p = va_arg(*p_va, PyObject **); |
| 1185 | if (PyUnicode_Check(arg)) |
| 1186 | *p = arg; |
| 1187 | else |
| 1188 | return converterr("str", arg, msgbuf, bufsize); |
| 1189 | break; |
| 1190 | } |
| 1191 | |
| 1192 | case 'O': { /* object */ |
| 1193 | PyTypeObject *type; |
| 1194 | PyObject **p; |
| 1195 | if (*format == '!') { |
| 1196 | type = va_arg(*p_va, PyTypeObject*); |
| 1197 | p = va_arg(*p_va, PyObject **); |
| 1198 | format++; |
| 1199 | if (PyType_IsSubtype(arg->ob_type, type)) |
| 1200 | *p = arg; |
| 1201 | else |
| 1202 | return converterr(type->tp_name, arg, msgbuf, bufsize); |
| 1203 | |
| 1204 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | else if (*format == '&') { |
| 1206 | typedef int (*converter)(PyObject *, void *); |
| 1207 | converter convert = va_arg(*p_va, converter); |
| 1208 | void *addr = va_arg(*p_va, void *); |
| 1209 | int res; |
| 1210 | format++; |
| 1211 | if (! (res = (*convert)(arg, addr))) |
| 1212 | return converterr("(unspecified)", |
| 1213 | arg, msgbuf, bufsize); |
| 1214 | if (res == Py_CLEANUP_SUPPORTED && |
| 1215 | addcleanup_convert(addr, freelist, convert) == -1) |
| 1216 | return converterr("(cleanup problem)", |
| 1217 | arg, msgbuf, bufsize); |
| 1218 | } |
| 1219 | else { |
| 1220 | p = va_arg(*p_va, PyObject **); |
| 1221 | *p = arg; |
| 1222 | } |
| 1223 | break; |
| 1224 | } |
| 1225 | |
| 1226 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1227 | case 'w': { /* "w*": memory buffer, read-write access */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1228 | void **p = va_arg(*p_va, void **); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1230 | if (*format != '*') |
| 1231 | return converterr( |
| 1232 | "invalid use of 'w' format character", |
| 1233 | arg, msgbuf, bufsize); |
| 1234 | format++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1236 | /* Caller is interested in Py_buffer, and the object |
| 1237 | supports it directly. */ |
| 1238 | if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) { |
| 1239 | PyErr_Clear(); |
| 1240 | return converterr("read-write buffer", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | } |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1242 | if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) { |
| 1243 | PyBuffer_Release((Py_buffer*)p); |
| 1244 | return converterr("contiguous buffer", arg, msgbuf, bufsize); |
| 1245 | } |
Victor Stinner | a093d0d | 2011-01-04 11:16:48 +0000 | [diff] [blame] | 1246 | if (addcleanup(p, freelist, 1)) { |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1247 | return converterr( |
| 1248 | "(cleanup problem)", |
| 1249 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | } |
| 1253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | default: |
| 1255 | return converterr("impossible<bad format char>", arg, msgbuf, bufsize); |
| 1256 | |
| 1257 | } |
| 1258 | |
| 1259 | *p_format = format; |
| 1260 | return NULL; |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1261 | |
| 1262 | #undef FETCH_SIZE |
| 1263 | #undef STORE_SIZE |
| 1264 | #undef BUFFER_LEN |
| 1265 | #undef RETURN_ERR_OCCURRED |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 1266 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1267 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1268 | static Py_ssize_t |
Fred Drake | 563dfc2 | 2001-10-23 14:41:08 +0000 | [diff] [blame] | 1269 | convertbuffer(PyObject *arg, void **p, char **errmsg) |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1270 | { |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1271 | PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | Py_ssize_t count; |
| 1273 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | *errmsg = NULL; |
| 1276 | *p = NULL; |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1277 | if (pb != NULL && pb->bf_releasebuffer != NULL) { |
| 1278 | *errmsg = "read-only pinned buffer"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | return -1; |
| 1280 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1281 | |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1282 | if (getbuffer(arg, &view, errmsg) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | count = view.len; |
| 1285 | *p = view.buf; |
| 1286 | PyBuffer_Release(&view); |
| 1287 | return count; |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1288 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1289 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1290 | static int |
Neal Norwitz | 2f99b24 | 2008-08-24 05:48:10 +0000 | [diff] [blame] | 1291 | getbuffer(PyObject *arg, Py_buffer *view, char **errmsg) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1292 | { |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1293 | if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | *errmsg = "bytes or buffer"; |
| 1295 | return -1; |
| 1296 | } |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1297 | if (!PyBuffer_IsContiguous(view, 'C')) { |
Victor Stinner | 21e0948 | 2010-06-24 22:57:10 +0000 | [diff] [blame] | 1298 | PyBuffer_Release(view); |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1299 | *errmsg = "contiguous buffer"; |
| 1300 | return -1; |
| 1301 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1302 | return 0; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1305 | /* Support for keyword arguments donated by |
| 1306 | Geoff Philbrick <philbric@delphi.hks.com> */ |
| 1307 | |
Tim Peters | f8cd3e8 | 2001-10-27 04:26:57 +0000 | [diff] [blame] | 1308 | /* Return false (0) for error, else true. */ |
Fred Drake | 563dfc2 | 2001-10-23 14:41:08 +0000 | [diff] [blame] | 1309 | int |
| 1310 | PyArg_ParseTupleAndKeywords(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | PyObject *keywords, |
| 1312 | const char *format, |
| 1313 | char **kwlist, ...) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | int retval; |
| 1316 | va_list va; |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1319 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1320 | format == NULL || |
| 1321 | kwlist == NULL) |
| 1322 | { |
| 1323 | PyErr_BadInternalCall(); |
| 1324 | return 0; |
| 1325 | } |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | va_start(va, kwlist); |
| 1328 | retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0); |
| 1329 | va_end(va); |
| 1330 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | int |
| 1334 | _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | PyObject *keywords, |
| 1336 | const char *format, |
| 1337 | char **kwlist, ...) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1338 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | int retval; |
| 1340 | va_list va; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1341 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1343 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1344 | format == NULL || |
| 1345 | kwlist == NULL) |
| 1346 | { |
| 1347 | PyErr_BadInternalCall(); |
| 1348 | return 0; |
| 1349 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | va_start(va, kwlist); |
| 1352 | retval = vgetargskeywords(args, keywords, format, |
| 1353 | kwlist, &va, FLAG_SIZE_T); |
| 1354 | va_end(va); |
| 1355 | return retval; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1359 | int |
| 1360 | PyArg_VaParseTupleAndKeywords(PyObject *args, |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1361 | PyObject *keywords, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1362 | const char *format, |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1363 | char **kwlist, va_list va) |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1364 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1365 | int retval; |
| 1366 | va_list lva; |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1369 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1370 | format == NULL || |
| 1371 | kwlist == NULL) |
| 1372 | { |
| 1373 | PyErr_BadInternalCall(); |
| 1374 | return 0; |
| 1375 | } |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1376 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 1377 | Py_VA_COPY(lva, va); |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0); |
| 1380 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | int |
| 1384 | _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | PyObject *keywords, |
| 1386 | const char *format, |
| 1387 | char **kwlist, va_list va) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | int retval; |
| 1390 | va_list lva; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1392 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1393 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1394 | format == NULL || |
| 1395 | kwlist == NULL) |
| 1396 | { |
| 1397 | PyErr_BadInternalCall(); |
| 1398 | return 0; |
| 1399 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1400 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 1401 | Py_VA_COPY(lva, va); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1403 | retval = vgetargskeywords(args, keywords, format, |
| 1404 | kwlist, &lva, FLAG_SIZE_T); |
| 1405 | return retval; |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 1408 | int |
| 1409 | PyArg_ValidateKeywordArguments(PyObject *kwargs) |
| 1410 | { |
Benjamin Peterson | f609654 | 2010-11-17 22:33:12 +0000 | [diff] [blame] | 1411 | if (!PyDict_Check(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | PyErr_BadInternalCall(); |
| 1413 | return 0; |
| 1414 | } |
| 1415 | if (!_PyDict_HasOnlyStringKeys(kwargs)) { |
| 1416 | PyErr_SetString(PyExc_TypeError, |
| 1417 | "keyword arguments must be strings"); |
| 1418 | return 0; |
| 1419 | } |
| 1420 | return 1; |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1423 | #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1424 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1425 | static int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1426 | vgetargskeywords(PyObject *args, PyObject *keywords, const char *format, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | char **kwlist, va_list *p_va, int flags) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | char msgbuf[512]; |
| 1430 | int levels[32]; |
| 1431 | const char *fname, *msg, *custom_msg, *keyword; |
| 1432 | int min = INT_MAX; |
| 1433 | int i, len, nargs, nkeywords; |
| 1434 | PyObject *freelist = NULL, *current_arg; |
Tim Peters | f4331c1 | 2001-10-27 00:17:34 +0000 | [diff] [blame] | 1435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | assert(args != NULL && PyTuple_Check(args)); |
| 1437 | assert(keywords == NULL || PyDict_Check(keywords)); |
| 1438 | assert(format != NULL); |
| 1439 | assert(kwlist != NULL); |
| 1440 | assert(p_va != NULL); |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | /* grab the function name or custom error msg first (mutually exclusive) */ |
| 1443 | fname = strchr(format, ':'); |
| 1444 | if (fname) { |
| 1445 | fname++; |
| 1446 | custom_msg = NULL; |
| 1447 | } |
| 1448 | else { |
| 1449 | custom_msg = strchr(format,';'); |
| 1450 | if (custom_msg) |
| 1451 | custom_msg++; |
| 1452 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | /* scan kwlist and get greatest possible nbr of args */ |
| 1455 | for (len=0; kwlist[len]; len++) |
| 1456 | continue; |
Tim Peters | f8cd3e8 | 2001-10-27 04:26:57 +0000 | [diff] [blame] | 1457 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | nargs = PyTuple_GET_SIZE(args); |
| 1459 | nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords); |
| 1460 | if (nargs + nkeywords > len) { |
| 1461 | PyErr_Format(PyExc_TypeError, "%s%s takes at most %d " |
| 1462 | "argument%s (%d given)", |
| 1463 | (fname == NULL) ? "function" : fname, |
| 1464 | (fname == NULL) ? "" : "()", |
| 1465 | len, |
| 1466 | (len == 1) ? "" : "s", |
| 1467 | nargs + nkeywords); |
| 1468 | return 0; |
| 1469 | } |
Tim Peters | c2f0112 | 2001-10-27 07:25:06 +0000 | [diff] [blame] | 1470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | /* convert tuple args and keyword args in same loop, using kwlist to drive process */ |
| 1472 | for (i = 0; i < len; i++) { |
| 1473 | keyword = kwlist[i]; |
| 1474 | if (*format == '|') { |
| 1475 | min = i; |
| 1476 | format++; |
| 1477 | } |
| 1478 | if (IS_END_OF_FORMAT(*format)) { |
| 1479 | PyErr_Format(PyExc_RuntimeError, |
| 1480 | "More keyword list entries (%d) than " |
| 1481 | "format specifiers (%d)", len, i); |
| 1482 | return cleanreturn(0, freelist); |
| 1483 | } |
| 1484 | current_arg = NULL; |
| 1485 | if (nkeywords) { |
| 1486 | current_arg = PyDict_GetItemString(keywords, keyword); |
| 1487 | } |
| 1488 | if (current_arg) { |
| 1489 | --nkeywords; |
| 1490 | if (i < nargs) { |
| 1491 | /* arg present in tuple and in dict */ |
| 1492 | PyErr_Format(PyExc_TypeError, |
| 1493 | "Argument given by name ('%s') " |
| 1494 | "and position (%d)", |
| 1495 | keyword, i+1); |
| 1496 | return cleanreturn(0, freelist); |
| 1497 | } |
| 1498 | } |
| 1499 | else if (nkeywords && PyErr_Occurred()) |
| 1500 | return cleanreturn(0, freelist); |
| 1501 | else if (i < nargs) |
| 1502 | current_arg = PyTuple_GET_ITEM(args, i); |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | if (current_arg) { |
| 1505 | msg = convertitem(current_arg, &format, p_va, flags, |
| 1506 | levels, msgbuf, sizeof(msgbuf), &freelist); |
| 1507 | if (msg) { |
| 1508 | seterror(i+1, msg, levels, fname, custom_msg); |
| 1509 | return cleanreturn(0, freelist); |
| 1510 | } |
| 1511 | continue; |
| 1512 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | if (i < min) { |
| 1515 | PyErr_Format(PyExc_TypeError, "Required argument " |
| 1516 | "'%s' (pos %d) not found", |
| 1517 | keyword, i+1); |
| 1518 | return cleanreturn(0, freelist); |
| 1519 | } |
| 1520 | /* current code reports success when all required args |
| 1521 | * fulfilled and no keyword args left, with no further |
| 1522 | * validation. XXX Maybe skip this in debug build ? |
| 1523 | */ |
| 1524 | if (!nkeywords) |
| 1525 | return cleanreturn(1, freelist); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | /* We are into optional args, skip thru to any remaining |
| 1528 | * keyword args */ |
| 1529 | msg = skipitem(&format, p_va, flags); |
| 1530 | if (msg) { |
| 1531 | PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg, |
| 1532 | format); |
| 1533 | return cleanreturn(0, freelist); |
| 1534 | } |
| 1535 | } |
Tim Peters | b054be4 | 2001-10-27 05:07:41 +0000 | [diff] [blame] | 1536 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | if (!IS_END_OF_FORMAT(*format) && *format != '|') { |
| 1538 | PyErr_Format(PyExc_RuntimeError, |
| 1539 | "more argument specifiers than keyword list entries " |
| 1540 | "(remaining format:'%s')", format); |
| 1541 | return cleanreturn(0, freelist); |
| 1542 | } |
Tim Peters | c2f0112 | 2001-10-27 07:25:06 +0000 | [diff] [blame] | 1543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | /* make sure there are no extraneous keyword arguments */ |
| 1545 | if (nkeywords > 0) { |
| 1546 | PyObject *key, *value; |
| 1547 | Py_ssize_t pos = 0; |
| 1548 | while (PyDict_Next(keywords, &pos, &key, &value)) { |
| 1549 | int match = 0; |
| 1550 | char *ks; |
| 1551 | if (!PyUnicode_Check(key)) { |
| 1552 | PyErr_SetString(PyExc_TypeError, |
| 1553 | "keywords must be strings"); |
| 1554 | return cleanreturn(0, freelist); |
| 1555 | } |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 1556 | /* check that _PyUnicode_AsString() result is not NULL */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | ks = _PyUnicode_AsString(key); |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 1558 | if (ks != NULL) { |
| 1559 | for (i = 0; i < len; i++) { |
| 1560 | if (!strcmp(ks, kwlist[i])) { |
| 1561 | match = 1; |
| 1562 | break; |
| 1563 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1564 | } |
| 1565 | } |
| 1566 | if (!match) { |
| 1567 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 1568 | "'%U' is an invalid keyword " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | "argument for this function", |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 1570 | key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | return cleanreturn(0, freelist); |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | return cleanreturn(1, freelist); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | |
| 1580 | static char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1581 | skipitem(const char **p_format, va_list *p_va, int flags) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1582 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | const char *format = *p_format; |
| 1584 | char c = *format++; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1586 | switch (c) { |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 1587 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1588 | /* simple codes |
| 1589 | * The individual types (second arg of va_arg) are irrelevant */ |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 1590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | case 'b': /* byte -- very short int */ |
| 1592 | case 'B': /* byte as bitfield */ |
| 1593 | case 'h': /* short int */ |
| 1594 | case 'H': /* short int as bitfield */ |
| 1595 | case 'i': /* int */ |
| 1596 | case 'I': /* int sized bitfield */ |
| 1597 | case 'l': /* long int */ |
| 1598 | case 'k': /* long int sized bitfield */ |
Guido van Rossum | 3dbba6e | 1999-01-25 21:48:56 +0000 | [diff] [blame] | 1599 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | case 'L': /* PY_LONG_LONG */ |
| 1601 | case 'K': /* PY_LONG_LONG sized bitfield */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1602 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | case 'f': /* float */ |
| 1604 | case 'd': /* double */ |
| 1605 | case 'D': /* complex double */ |
| 1606 | case 'c': /* char */ |
| 1607 | case 'C': /* unicode char */ |
| 1608 | { |
| 1609 | (void) va_arg(*p_va, void *); |
| 1610 | break; |
| 1611 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | case 'n': /* Py_ssize_t */ |
| 1614 | { |
| 1615 | (void) va_arg(*p_va, Py_ssize_t *); |
| 1616 | break; |
| 1617 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | /* string codes */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | case 'e': /* string with encoding */ |
| 1622 | { |
| 1623 | (void) va_arg(*p_va, const char *); |
| 1624 | if (!(*format == 's' || *format == 't')) |
| 1625 | /* after 'e', only 's' and 't' is allowed */ |
| 1626 | goto err; |
| 1627 | format++; |
| 1628 | /* explicit fallthrough to string cases */ |
| 1629 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1631 | case 's': /* string */ |
| 1632 | case 'z': /* string or None */ |
| 1633 | case 'y': /* bytes */ |
| 1634 | case 'u': /* unicode string */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1635 | case 'w': /* buffer, read-write */ |
| 1636 | { |
| 1637 | (void) va_arg(*p_va, char **); |
| 1638 | if (*format == '#') { |
| 1639 | if (flags & FLAG_SIZE_T) |
| 1640 | (void) va_arg(*p_va, Py_ssize_t *); |
| 1641 | else |
| 1642 | (void) va_arg(*p_va, int *); |
| 1643 | format++; |
| 1644 | } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') { |
| 1645 | format++; |
| 1646 | } |
| 1647 | break; |
| 1648 | } |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 1649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1650 | /* object codes */ |
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 | case 'S': /* string object */ |
| 1653 | case 'Y': /* string object */ |
| 1654 | case 'U': /* unicode string object */ |
| 1655 | { |
| 1656 | (void) va_arg(*p_va, PyObject **); |
| 1657 | break; |
| 1658 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | case 'O': /* object */ |
| 1661 | { |
| 1662 | if (*format == '!') { |
| 1663 | format++; |
| 1664 | (void) va_arg(*p_va, PyTypeObject*); |
| 1665 | (void) va_arg(*p_va, PyObject **); |
| 1666 | } |
| 1667 | else if (*format == '&') { |
| 1668 | typedef int (*converter)(PyObject *, void *); |
| 1669 | (void) va_arg(*p_va, converter); |
| 1670 | (void) va_arg(*p_va, void *); |
| 1671 | format++; |
| 1672 | } |
| 1673 | else { |
| 1674 | (void) va_arg(*p_va, PyObject **); |
| 1675 | } |
| 1676 | break; |
| 1677 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | case '(': /* bypass tuple, not handled at all previously */ |
| 1680 | { |
| 1681 | char *msg; |
| 1682 | for (;;) { |
| 1683 | if (*format==')') |
| 1684 | break; |
| 1685 | if (IS_END_OF_FORMAT(*format)) |
| 1686 | return "Unmatched left paren in format " |
| 1687 | "string"; |
| 1688 | msg = skipitem(&format, p_va, flags); |
| 1689 | if (msg) |
| 1690 | return msg; |
| 1691 | } |
| 1692 | format++; |
| 1693 | break; |
| 1694 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | case ')': |
| 1697 | return "Unmatched right paren in format string"; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1699 | default: |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 1700 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | return "impossible<bad format char>"; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1702 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | } |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 1704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 | *p_format = format; |
| 1706 | return NULL; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1707 | } |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1708 | |
| 1709 | |
| 1710 | int |
Martin v. Löwis | 7624674 | 2006-03-01 04:06:10 +0000 | [diff] [blame] | 1711 | 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] | 1712 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1713 | Py_ssize_t i, l; |
| 1714 | PyObject **o; |
| 1715 | va_list vargs; |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1716 | |
| 1717 | #ifdef HAVE_STDARG_PROTOTYPES |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1718 | va_start(vargs, max); |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1719 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | va_start(vargs); |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1721 | #endif |
| 1722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | assert(min >= 0); |
| 1724 | assert(min <= max); |
| 1725 | if (!PyTuple_Check(args)) { |
| 1726 | PyErr_SetString(PyExc_SystemError, |
| 1727 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 1728 | return 0; |
| 1729 | } |
| 1730 | l = PyTuple_GET_SIZE(args); |
| 1731 | if (l < min) { |
| 1732 | if (name != NULL) |
| 1733 | PyErr_Format( |
| 1734 | PyExc_TypeError, |
| 1735 | "%s expected %s%zd arguments, got %zd", |
| 1736 | name, (min == max ? "" : "at least "), min, l); |
| 1737 | else |
| 1738 | PyErr_Format( |
| 1739 | PyExc_TypeError, |
| 1740 | "unpacked tuple should have %s%zd elements," |
| 1741 | " but has %zd", |
| 1742 | (min == max ? "" : "at least "), min, l); |
| 1743 | va_end(vargs); |
| 1744 | return 0; |
| 1745 | } |
| 1746 | if (l > max) { |
| 1747 | if (name != NULL) |
| 1748 | PyErr_Format( |
| 1749 | PyExc_TypeError, |
| 1750 | "%s expected %s%zd arguments, got %zd", |
| 1751 | name, (min == max ? "" : "at most "), max, l); |
| 1752 | else |
| 1753 | PyErr_Format( |
| 1754 | PyExc_TypeError, |
| 1755 | "unpacked tuple should have %s%zd elements," |
| 1756 | " but has %zd", |
| 1757 | (min == max ? "" : "at most "), max, l); |
| 1758 | va_end(vargs); |
| 1759 | return 0; |
| 1760 | } |
| 1761 | for (i = 0; i < l; i++) { |
| 1762 | o = va_arg(vargs, PyObject **); |
| 1763 | *o = PyTuple_GET_ITEM(args, i); |
| 1764 | } |
| 1765 | va_end(vargs); |
| 1766 | return 1; |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1767 | } |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1768 | |
| 1769 | |
| 1770 | /* For type constructors that don't take keyword args |
| 1771 | * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1772 | * Sets a TypeError and returns 0 if the kwds dict is |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1773 | * not empty, returns 1 otherwise |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1774 | */ |
| 1775 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1776 | _PyArg_NoKeywords(const char *funcname, PyObject *kw) |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | if (kw == NULL) |
| 1779 | return 1; |
| 1780 | if (!PyDict_CheckExact(kw)) { |
| 1781 | PyErr_BadInternalCall(); |
| 1782 | return 0; |
| 1783 | } |
| 1784 | if (PyDict_Size(kw) == 0) |
| 1785 | return 1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments", |
| 1788 | funcname); |
| 1789 | return 0; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1790 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1791 | #ifdef __cplusplus |
| 1792 | }; |
| 1793 | #endif |