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