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