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