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