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