Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 1 | |
| 2 | /* New getargs implementation */ |
| 3 | |
| 4 | /* XXX There are several unchecked sprintf or strcat calls in this file. |
| 5 | XXX The only way these can become a danger is if some C code in the |
| 6 | XXX Python source (or in an extension) uses ridiculously long names |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 7 | XXX or ridiculously deep nesting in format strings. */ |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 9 | #include "Python.h" |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | c1d5053 | 1996-08-21 23:38:24 +0000 | [diff] [blame] | 11 | #include <ctype.h> |
| 12 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 13 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 14 | int PyArg_Parse(PyObject *, char *, ...); |
| 15 | int PyArg_ParseTuple(PyObject *, char *, ...); |
| 16 | int PyArg_VaParse(PyObject *, char *, va_list); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 17 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 18 | int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, |
| 19 | char *, char **, ...); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 20 | |
| 21 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 22 | static int vgetargs1(PyObject *, char *, va_list *, int); |
| 23 | static void seterror(int, char *, int *, char *, char *); |
| 24 | static char *convertitem(PyObject *, char **, va_list *, int *, char *); |
| 25 | static char *converttuple(PyObject *, char **, va_list *, |
| 26 | int *, char *, int); |
| 27 | static char *convertsimple(PyObject *, char **, va_list *, char *); |
| 28 | static char *convertsimple1(PyObject *, char **, va_list *); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 29 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 30 | static int vgetargskeywords(PyObject *, PyObject *, |
| 31 | char *, char **, va_list *); |
| 32 | static char *skipitem(char **, va_list *); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 34 | int PyArg_Parse(PyObject *args, char *format, ...) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 35 | { |
| 36 | int retval; |
| 37 | va_list va; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 38 | |
| 39 | va_start(va, format); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 40 | retval = vgetargs1(args, format, &va, 1); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 41 | va_end(va); |
| 42 | return retval; |
| 43 | } |
| 44 | |
| 45 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 46 | int PyArg_ParseTuple(PyObject *args, char *format, ...) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 47 | { |
| 48 | int retval; |
| 49 | va_list va; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 50 | |
| 51 | va_start(va, format); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 52 | retval = vgetargs1(args, format, &va, 0); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 53 | va_end(va); |
| 54 | return retval; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 59 | PyArg_VaParse(PyObject *args, char *format, va_list va) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 60 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 61 | va_list lva; |
| 62 | |
| 63 | #ifdef VA_LIST_IS_ARRAY |
| 64 | memcpy(lva, va, sizeof(va_list)); |
| 65 | #else |
| 66 | lva = va; |
| 67 | #endif |
| 68 | |
| 69 | return vgetargs1(args, format, &lva, 0); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| 73 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 74 | vgetargs1(PyObject *args, char *format, va_list *p_va, int compat) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 75 | { |
| 76 | char msgbuf[256]; |
| 77 | int levels[32]; |
| 78 | char *fname = NULL; |
| 79 | char *message = NULL; |
| 80 | int min = -1; |
| 81 | int max = 0; |
| 82 | int level = 0; |
| 83 | char *formatsave = format; |
| 84 | int i, len; |
| 85 | char *msg; |
| 86 | |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 87 | assert(compat || (args != (PyObject*)NULL)); |
| 88 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 89 | for (;;) { |
| 90 | int c = *format++; |
| 91 | if (c == '(' /* ')' */) { |
| 92 | if (level == 0) |
| 93 | max++; |
| 94 | level++; |
| 95 | } |
| 96 | else if (/* '(' */ c == ')') { |
| 97 | if (level == 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 98 | Py_FatalError(/* '(' */ |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 99 | "excess ')' in getargs format"); |
| 100 | else |
| 101 | level--; |
| 102 | } |
| 103 | else if (c == '\0') |
| 104 | break; |
| 105 | else if (c == ':') { |
| 106 | fname = format; |
| 107 | break; |
| 108 | } |
| 109 | else if (c == ';') { |
| 110 | message = format; |
| 111 | break; |
| 112 | } |
| 113 | else if (level != 0) |
| 114 | ; /* Pass */ |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 115 | else if (c == 'e') |
| 116 | ; /* Pass */ |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 117 | else if (isalpha(c)) |
| 118 | max++; |
| 119 | else if (c == '|') |
| 120 | min = max; |
| 121 | } |
| 122 | |
| 123 | if (level != 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 124 | Py_FatalError(/* '(' */ "missing ')' in getargs format"); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 125 | |
| 126 | if (min < 0) |
| 127 | min = max; |
| 128 | |
| 129 | format = formatsave; |
| 130 | |
| 131 | if (compat) { |
| 132 | if (max == 0) { |
| 133 | if (args == NULL) |
| 134 | return 1; |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 135 | sprintf(msgbuf, "%s%s takes no arguments", |
| 136 | fname==NULL ? "function" : fname, |
| 137 | fname==NULL ? "" : "()"); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 138 | PyErr_SetString(PyExc_TypeError, msgbuf); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | else if (min == 1 && max == 1) { |
Guido van Rossum | 13d0ed1 | 1994-11-10 22:35:48 +0000 | [diff] [blame] | 142 | if (args == NULL) { |
| 143 | sprintf(msgbuf, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 144 | "%s%s takes at least one argument", |
| 145 | fname==NULL ? "function" : fname, |
| 146 | fname==NULL ? "" : "()"); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 147 | PyErr_SetString(PyExc_TypeError, msgbuf); |
Guido van Rossum | 13d0ed1 | 1994-11-10 22:35:48 +0000 | [diff] [blame] | 148 | return 0; |
| 149 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 150 | msg = convertitem(args, &format, p_va, levels, msgbuf); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 151 | if (msg == NULL) |
| 152 | return 1; |
| 153 | seterror(levels[0], msg, levels+1, fname, message); |
| 154 | return 0; |
| 155 | } |
| 156 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 157 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 158 | "old style getargs format uses new features"); |
| 159 | return 0; |
| 160 | } |
| 161 | } |
| 162 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 163 | if (!PyTuple_Check(args)) { |
| 164 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 165 | "new style getargs format but argument is not a tuple"); |
| 166 | return 0; |
| 167 | } |
| 168 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 169 | len = PyTuple_Size(args); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 170 | |
| 171 | if (len < min || max < len) { |
| 172 | if (message == NULL) { |
| 173 | sprintf(msgbuf, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 174 | "%s%s takes %s %d argument%s (%d given)", |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 175 | fname==NULL ? "function" : fname, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 176 | fname==NULL ? "" : "()", |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 177 | min==max ? "exactly" |
| 178 | : len < min ? "at least" : "at most", |
| 179 | len < min ? min : max, |
| 180 | (len < min ? min : max) == 1 ? "" : "s", |
| 181 | len); |
| 182 | message = msgbuf; |
| 183 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 184 | PyErr_SetString(PyExc_TypeError, message); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | for (i = 0; i < len; i++) { |
| 189 | if (*format == '|') |
| 190 | format++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 191 | msg = convertitem(PyTuple_GetItem(args, i), &format, p_va, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 192 | levels, msgbuf); |
| 193 | if (msg) { |
| 194 | seterror(i+1, msg, levels, fname, message); |
| 195 | return 0; |
| 196 | } |
| 197 | } |
Guido van Rossum | 231a41e | 1997-12-09 20:36:39 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 199 | if (*format != '\0' && !isalpha((int)(*format)) && |
Guido van Rossum | 7d4f68c | 1997-12-19 04:25:23 +0000 | [diff] [blame] | 200 | *format != '(' && |
Guido van Rossum | 231a41e | 1997-12-09 20:36:39 +0000 | [diff] [blame] | 201 | *format != '|' && *format != ':' && *format != ';') { |
| 202 | PyErr_Format(PyExc_SystemError, |
Guido van Rossum | 0d6b49e | 1998-01-19 22:22:44 +0000 | [diff] [blame] | 203 | "bad format string: %.200s", formatsave); |
Guido van Rossum | 231a41e | 1997-12-09 20:36:39 +0000 | [diff] [blame] | 204 | return 0; |
| 205 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 206 | |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | static void |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 213 | seterror(int iarg, char *msg, int *levels, char *fname, char *message) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 214 | { |
| 215 | char buf[256]; |
| 216 | int i; |
| 217 | char *p = buf; |
| 218 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 219 | if (PyErr_Occurred()) |
Guido van Rossum | 64fc649 | 1995-01-21 14:09:37 +0000 | [diff] [blame] | 220 | return; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 221 | else if (message == NULL) { |
| 222 | if (fname != NULL) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 223 | sprintf(p, "%s() ", fname); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 224 | p += strlen(p); |
| 225 | } |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 226 | if (iarg != 0) { |
| 227 | sprintf(p, "argument %d", iarg); |
| 228 | i = 0; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 229 | p += strlen(p); |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 230 | while (levels[i] > 0) { |
| 231 | sprintf(p, ", item %d", levels[i]-1); |
| 232 | p += strlen(p); |
| 233 | i++; |
| 234 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 235 | } |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 236 | else { |
| 237 | sprintf(p, "argument"); |
| 238 | p += strlen(p); |
| 239 | } |
| 240 | sprintf(p, " %s", msg); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 241 | message = buf; |
| 242 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 243 | PyErr_SetString(PyExc_TypeError, message); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | |
| 247 | /* Convert a tuple argument. |
| 248 | On entry, *p_format points to the character _after_ the opening '('. |
| 249 | On successful exit, *p_format points to the closing ')'. |
| 250 | If successful: |
| 251 | *p_format and *p_va are updated, |
| 252 | *levels and *msgbuf are untouched, |
| 253 | and NULL is returned. |
| 254 | If the argument is invalid: |
| 255 | *p_format is unchanged, |
| 256 | *p_va is undefined, |
| 257 | *levels is a 0-terminated list of item numbers, |
| 258 | *msgbuf contains an error message, whose format is: |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 259 | "must be <typename1>, not <typename2>", where: |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 260 | <typename1> is the name of the expected type, and |
| 261 | <typename2> is the name of the actual type, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 262 | and msgbuf is returned. |
| 263 | */ |
| 264 | |
| 265 | static char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 266 | converttuple(PyObject *arg, char **p_format, va_list *p_va, int *levels, |
| 267 | char *msgbuf, int toplevel) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 268 | { |
| 269 | int level = 0; |
| 270 | int n = 0; |
| 271 | char *format = *p_format; |
| 272 | int i; |
| 273 | |
| 274 | for (;;) { |
| 275 | int c = *format++; |
| 276 | if (c == '(') { |
| 277 | if (level == 0) |
| 278 | n++; |
| 279 | level++; |
| 280 | } |
| 281 | else if (c == ')') { |
| 282 | if (level == 0) |
| 283 | break; |
| 284 | level--; |
| 285 | } |
| 286 | else if (c == ':' || c == ';' || c == '\0') |
| 287 | break; |
| 288 | else if (level == 0 && isalpha(c)) |
| 289 | n++; |
| 290 | } |
| 291 | |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 292 | if (!PySequence_Check(arg) || PyString_Check(arg)) { |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 293 | levels[0] = 0; |
| 294 | sprintf(msgbuf, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 295 | toplevel ? "expected %d arguments, not %s" : |
| 296 | "must be %d-item sequence, not %s", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 297 | n, arg == Py_None ? "None" : arg->ob_type->tp_name); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 298 | return msgbuf; |
| 299 | } |
| 300 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 301 | if ((i = PySequence_Size(arg)) != n) { |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 302 | levels[0] = 0; |
| 303 | sprintf(msgbuf, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 304 | toplevel ? "expected %d arguments, not %d" : |
| 305 | "must be sequence of length %d, not %d", |
| 306 | n, i); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 307 | return msgbuf; |
| 308 | } |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 309 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 310 | format = *p_format; |
| 311 | for (i = 0; i < n; i++) { |
| 312 | char *msg; |
Guido van Rossum | 66368cc | 1999-02-17 23:16:43 +0000 | [diff] [blame] | 313 | PyObject *item; |
| 314 | item = PySequence_GetItem(arg, i); |
| 315 | msg = convertitem(item, &format, p_va, levels+1, msgbuf); |
| 316 | /* PySequence_GetItem calls tp->sq_item, which INCREFs */ |
| 317 | Py_XDECREF(item); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 318 | if (msg != NULL) { |
| 319 | levels[0] = i+1; |
| 320 | return msg; |
| 321 | } |
| 322 | } |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 323 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 324 | *p_format = format; |
| 325 | return NULL; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | /* Convert a single item. */ |
| 330 | |
| 331 | static char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 332 | convertitem(PyObject *arg, char **p_format, va_list *p_va, int *levels, |
| 333 | char *msgbuf) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 334 | { |
| 335 | char *msg; |
| 336 | char *format = *p_format; |
| 337 | |
| 338 | if (*format == '(' /* ')' */) { |
| 339 | format++; |
| 340 | msg = converttuple(arg, &format, p_va, levels, msgbuf, 0); |
| 341 | if (msg == NULL) |
| 342 | format++; |
| 343 | } |
| 344 | else { |
| 345 | msg = convertsimple(arg, &format, p_va, msgbuf); |
| 346 | if (msg != NULL) |
| 347 | levels[0] = 0; |
| 348 | } |
| 349 | if (msg == NULL) |
| 350 | *p_format = format; |
| 351 | return msg; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | /* Convert a non-tuple argument. Adds to convertsimple1 functionality |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 356 | by formatting messages as "must be <desired type>, not <actual type>". */ |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 357 | |
| 358 | static char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 359 | convertsimple(PyObject *arg, char **p_format, va_list *p_va, char *msgbuf) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 360 | { |
| 361 | char *msg = convertsimple1(arg, p_format, p_va); |
| 362 | if (msg != NULL) { |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 363 | sprintf(msgbuf, "must be %.50s, not %.50s", msg, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 364 | arg == Py_None ? "None" : arg->ob_type->tp_name); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 365 | msg = msgbuf; |
| 366 | } |
| 367 | return msg; |
| 368 | } |
| 369 | |
| 370 | |
Guido van Rossum | 700c6ff | 2000-04-27 20:13:18 +0000 | [diff] [blame] | 371 | /* Internal API needed by convertsimple1(): */ |
| 372 | extern |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 373 | PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
Guido van Rossum | 700c6ff | 2000-04-27 20:13:18 +0000 | [diff] [blame] | 374 | const char *errors); |
| 375 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 376 | /* Convert a non-tuple argument. Return NULL if conversion went OK, |
| 377 | or a string representing the expected type if the conversion failed. |
| 378 | When failing, an exception may or may not have been raised. |
| 379 | Don't call if a tuple is expected. */ |
| 380 | |
| 381 | static char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 382 | convertsimple1(PyObject *arg, char **p_format, va_list *p_va) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 383 | { |
| 384 | char *format = *p_format; |
| 385 | char c = *format++; |
| 386 | |
| 387 | switch (c) { |
| 388 | |
Fred Drake | 230cae7 | 2000-05-09 21:50:00 +0000 | [diff] [blame] | 389 | case 'b': /* unsigned byte -- very short int */ |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 390 | { |
| 391 | char *p = va_arg(*p_va, char *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 392 | long ival = PyInt_AsLong(arg); |
| 393 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 394 | return "integer<b>"; |
Fred Drake | 230cae7 | 2000-05-09 21:50:00 +0000 | [diff] [blame] | 395 | else if (ival < 0) { |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 396 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 5e08cb8 | 2000-06-28 23:53:56 +0000 | [diff] [blame] | 397 | "unsigned byte integer is less than minimum"); |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 398 | return "integer<b>"; |
| 399 | } |
Fred Drake | 230cae7 | 2000-05-09 21:50:00 +0000 | [diff] [blame] | 400 | else if (ival > UCHAR_MAX) { |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 401 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 5e08cb8 | 2000-06-28 23:53:56 +0000 | [diff] [blame] | 402 | "unsigned byte integer is greater than maximum"); |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 403 | return "integer<b>"; |
| 404 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 405 | else |
Fred Drake | 230cae7 | 2000-05-09 21:50:00 +0000 | [diff] [blame] | 406 | *p = (unsigned char) ival; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 407 | break; |
| 408 | } |
| 409 | |
Jack Jansen | cc22fbe | 2000-08-05 21:29:58 +0000 | [diff] [blame] | 410 | case 'B': /* byte sized bitfield - both signed and unsigned values allowed */ |
| 411 | { |
| 412 | char *p = va_arg(*p_va, char *); |
| 413 | long ival = PyInt_AsLong(arg); |
| 414 | if (ival == -1 && PyErr_Occurred()) |
| 415 | return "integer<b>"; |
| 416 | else if (ival < SCHAR_MIN) { |
| 417 | PyErr_SetString(PyExc_OverflowError, |
| 418 | "byte-sized integer bitfield is less than minimum"); |
| 419 | return "integer<B>"; |
| 420 | } |
Jack Jansen | a454ebd | 2000-09-15 12:52:19 +0000 | [diff] [blame] | 421 | else if (ival > (int)UCHAR_MAX) { |
Jack Jansen | cc22fbe | 2000-08-05 21:29:58 +0000 | [diff] [blame] | 422 | PyErr_SetString(PyExc_OverflowError, |
| 423 | "byte-sized integer bitfield is greater than maximum"); |
| 424 | return "integer<B>"; |
| 425 | } |
| 426 | else |
| 427 | *p = (unsigned char) ival; |
| 428 | break; |
| 429 | } |
| 430 | |
Fred Drake | 230cae7 | 2000-05-09 21:50:00 +0000 | [diff] [blame] | 431 | case 'h': /* signed short int */ |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 432 | { |
| 433 | short *p = va_arg(*p_va, short *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 434 | long ival = PyInt_AsLong(arg); |
| 435 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 436 | return "integer<h>"; |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 437 | else if (ival < SHRT_MIN) { |
| 438 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 5e08cb8 | 2000-06-28 23:53:56 +0000 | [diff] [blame] | 439 | "signed short integer is less than minimum"); |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 440 | return "integer<h>"; |
| 441 | } |
| 442 | else if (ival > SHRT_MAX) { |
| 443 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 5e08cb8 | 2000-06-28 23:53:56 +0000 | [diff] [blame] | 444 | "signed short integer is greater than maximum"); |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 445 | return "integer<h>"; |
| 446 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 447 | else |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 448 | *p = (short) ival; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 449 | break; |
| 450 | } |
| 451 | |
Jack Jansen | cc22fbe | 2000-08-05 21:29:58 +0000 | [diff] [blame] | 452 | case 'H': /* short int sized bitfield, both signed and unsigned allowed */ |
Jack Jansen | d50338f | 2000-07-06 12:22:00 +0000 | [diff] [blame] | 453 | { |
| 454 | unsigned short *p = va_arg(*p_va, unsigned short *); |
| 455 | long ival = PyInt_AsLong(arg); |
| 456 | if (ival == -1 && PyErr_Occurred()) |
| 457 | return "integer<H>"; |
Jack Jansen | cc22fbe | 2000-08-05 21:29:58 +0000 | [diff] [blame] | 458 | else if (ival < SHRT_MIN) { |
Jack Jansen | d50338f | 2000-07-06 12:22:00 +0000 | [diff] [blame] | 459 | PyErr_SetString(PyExc_OverflowError, |
Jack Jansen | cc22fbe | 2000-08-05 21:29:58 +0000 | [diff] [blame] | 460 | "short integer bitfield is less than minimum"); |
Jack Jansen | d50338f | 2000-07-06 12:22:00 +0000 | [diff] [blame] | 461 | return "integer<H>"; |
| 462 | } |
| 463 | else if (ival > USHRT_MAX) { |
| 464 | PyErr_SetString(PyExc_OverflowError, |
Jack Jansen | cc22fbe | 2000-08-05 21:29:58 +0000 | [diff] [blame] | 465 | "short integer bitfield is greater than maximum"); |
Jack Jansen | d50338f | 2000-07-06 12:22:00 +0000 | [diff] [blame] | 466 | return "integer<H>"; |
| 467 | } |
| 468 | else |
| 469 | *p = (unsigned short) ival; |
| 470 | break; |
| 471 | } |
| 472 | |
Fred Drake | 230cae7 | 2000-05-09 21:50:00 +0000 | [diff] [blame] | 473 | case 'i': /* signed int */ |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 474 | { |
| 475 | int *p = va_arg(*p_va, int *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 476 | long ival = PyInt_AsLong(arg); |
| 477 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 478 | return "integer<i>"; |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 479 | else if (ival > INT_MAX) { |
| 480 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 5e08cb8 | 2000-06-28 23:53:56 +0000 | [diff] [blame] | 481 | "signed integer is greater than maximum"); |
| 482 | return "integer<i>"; |
| 483 | } |
| 484 | else if (ival < INT_MIN) { |
| 485 | PyErr_SetString(PyExc_OverflowError, |
| 486 | "signed integer is less than minimum"); |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 487 | return "integer<i>"; |
| 488 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 489 | else |
| 490 | *p = ival; |
| 491 | break; |
| 492 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 493 | case 'l': /* long int */ |
| 494 | { |
| 495 | long *p = va_arg(*p_va, long *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 496 | long ival = PyInt_AsLong(arg); |
| 497 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 498 | return "integer<l>"; |
| 499 | else |
| 500 | *p = ival; |
| 501 | break; |
| 502 | } |
| 503 | |
Guido van Rossum | 3dbba6e | 1999-01-25 21:48:56 +0000 | [diff] [blame] | 504 | #ifdef HAVE_LONG_LONG |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 505 | case 'L': /* LONG_LONG */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 506 | { |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 507 | LONG_LONG *p = va_arg( *p_va, LONG_LONG * ); |
| 508 | LONG_LONG ival = PyLong_AsLongLong( arg ); |
| 509 | if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) { |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 510 | return "long<L>"; |
| 511 | } else { |
| 512 | *p = ival; |
| 513 | } |
| 514 | break; |
| 515 | } |
| 516 | #endif |
| 517 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 518 | case 'f': /* float */ |
| 519 | { |
| 520 | float *p = va_arg(*p_va, float *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 521 | double dval = PyFloat_AsDouble(arg); |
| 522 | if (PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 523 | return "float<f>"; |
| 524 | else |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 525 | *p = (float) dval; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 526 | break; |
| 527 | } |
| 528 | |
| 529 | case 'd': /* double */ |
| 530 | { |
| 531 | double *p = va_arg(*p_va, double *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 532 | double dval = PyFloat_AsDouble(arg); |
| 533 | if (PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 534 | return "float<d>"; |
| 535 | else |
| 536 | *p = dval; |
| 537 | break; |
| 538 | } |
| 539 | |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 540 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 541 | case 'D': /* complex double */ |
| 542 | { |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 543 | Py_complex *p = va_arg(*p_va, Py_complex *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 544 | Py_complex cval; |
| 545 | cval = PyComplex_AsCComplex(arg); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 546 | if (PyErr_Occurred()) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 547 | return "complex<D>"; |
| 548 | else |
| 549 | *p = cval; |
| 550 | break; |
| 551 | } |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 552 | #endif /* WITHOUT_COMPLEX */ |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 553 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 554 | case 'c': /* char */ |
| 555 | { |
| 556 | char *p = va_arg(*p_va, char *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 557 | if (PyString_Check(arg) && PyString_Size(arg) == 1) |
| 558 | *p = PyString_AsString(arg)[0]; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 559 | else |
| 560 | return "char"; |
| 561 | break; |
| 562 | } |
| 563 | |
| 564 | case 's': /* string */ |
| 565 | { |
Marc-André Lemburg | 0afff38 | 2000-09-21 21:08:30 +0000 | [diff] [blame] | 566 | if (*format == '#') { |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 567 | void **p = (void **)va_arg(*p_va, char **); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 568 | int *q = va_arg(*p_va, int *); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 569 | |
Marc-André Lemburg | 0afff38 | 2000-09-21 21:08:30 +0000 | [diff] [blame] | 570 | if (PyString_Check(arg)) { |
| 571 | *p = PyString_AS_STRING(arg); |
| 572 | *q = PyString_GET_SIZE(arg); |
| 573 | } |
| 574 | else if (PyUnicode_Check(arg)) { |
| 575 | arg = _PyUnicode_AsDefaultEncodedString( |
| 576 | arg, NULL); |
| 577 | if (arg == NULL) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 578 | return "(unicode conversion error)"; |
Marc-André Lemburg | 0afff38 | 2000-09-21 21:08:30 +0000 | [diff] [blame] | 579 | *p = PyString_AS_STRING(arg); |
| 580 | *q = PyString_GET_SIZE(arg); |
| 581 | } |
| 582 | else { /* any buffer-like object */ |
| 583 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
| 584 | int count; |
| 585 | if ( pb == NULL || |
| 586 | pb->bf_getreadbuffer == NULL || |
| 587 | pb->bf_getsegcount == NULL ) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 588 | return "string or read-only buffer"; |
Marc-André Lemburg | 0afff38 | 2000-09-21 21:08:30 +0000 | [diff] [blame] | 589 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 590 | return "string or single-segment read-only buffer"; |
Marc-André Lemburg | 0afff38 | 2000-09-21 21:08:30 +0000 | [diff] [blame] | 591 | if ( (count = |
| 592 | (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 ) |
| 593 | return "(unspecified)"; |
| 594 | *q = count; |
| 595 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 596 | format++; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 597 | } else { |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 598 | char **p = va_arg(*p_va, char **); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 599 | |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 600 | if (PyString_Check(arg)) |
Guido van Rossum | e826ef0 | 2000-03-10 23:02:17 +0000 | [diff] [blame] | 601 | *p = PyString_AS_STRING(arg); |
| 602 | else if (PyUnicode_Check(arg)) { |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 603 | arg = _PyUnicode_AsDefaultEncodedString( |
| 604 | arg, NULL); |
Guido van Rossum | e826ef0 | 2000-03-10 23:02:17 +0000 | [diff] [blame] | 605 | if (arg == NULL) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 606 | return "(unicode conversion error)"; |
Guido van Rossum | e826ef0 | 2000-03-10 23:02:17 +0000 | [diff] [blame] | 607 | *p = PyString_AS_STRING(arg); |
| 608 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 609 | else |
| 610 | return "string"; |
| 611 | if ((int)strlen(*p) != PyString_Size(arg)) |
| 612 | return "string without null bytes"; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 613 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 614 | break; |
| 615 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 616 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 617 | case 'z': /* string, may be NULL (None) */ |
| 618 | { |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 619 | if (*format == '#') { /* any buffer-like object */ |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 620 | void **p = (void **)va_arg(*p_va, char **); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 621 | int *q = va_arg(*p_va, int *); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 622 | |
| 623 | if (arg == Py_None) { |
| 624 | *p = 0; |
| 625 | *q = 0; |
Marc-André Lemburg | 0afff38 | 2000-09-21 21:08:30 +0000 | [diff] [blame] | 626 | } |
| 627 | else if (PyString_Check(arg)) { |
| 628 | *p = PyString_AS_STRING(arg); |
| 629 | *q = PyString_GET_SIZE(arg); |
| 630 | } |
| 631 | else if (PyUnicode_Check(arg)) { |
| 632 | arg = _PyUnicode_AsDefaultEncodedString( |
| 633 | arg, NULL); |
| 634 | if (arg == NULL) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 635 | return "(unicode conversion error)"; |
Marc-André Lemburg | 0afff38 | 2000-09-21 21:08:30 +0000 | [diff] [blame] | 636 | *p = PyString_AS_STRING(arg); |
| 637 | *q = PyString_GET_SIZE(arg); |
| 638 | } |
| 639 | else { /* any buffer-like object */ |
| 640 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
| 641 | int count; |
| 642 | if ( pb == NULL || |
| 643 | pb->bf_getreadbuffer == NULL || |
| 644 | pb->bf_getsegcount == NULL ) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 645 | return "string or read-only buffer"; |
Marc-André Lemburg | 0afff38 | 2000-09-21 21:08:30 +0000 | [diff] [blame] | 646 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 647 | return "string or single-segment read-only buffer"; |
Marc-André Lemburg | 0afff38 | 2000-09-21 21:08:30 +0000 | [diff] [blame] | 648 | if ( (count = |
| 649 | (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 ) |
| 650 | return "(unspecified)"; |
| 651 | *q = count; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 652 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 653 | format++; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 654 | } else { |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 655 | char **p = va_arg(*p_va, char **); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 656 | |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 657 | if (arg == Py_None) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 658 | *p = 0; |
| 659 | else if (PyString_Check(arg)) |
| 660 | *p = PyString_AsString(arg); |
Guido van Rossum | e826ef0 | 2000-03-10 23:02:17 +0000 | [diff] [blame] | 661 | else if (PyUnicode_Check(arg)) { |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 662 | arg = _PyUnicode_AsDefaultEncodedString( |
| 663 | arg, NULL); |
Guido van Rossum | e826ef0 | 2000-03-10 23:02:17 +0000 | [diff] [blame] | 664 | if (arg == NULL) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 665 | return "(unicode conversion error)"; |
Guido van Rossum | e826ef0 | 2000-03-10 23:02:17 +0000 | [diff] [blame] | 666 | *p = PyString_AS_STRING(arg); |
| 667 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 668 | else |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 669 | return "string or None"; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 670 | if (*format == '#') { |
| 671 | int *q = va_arg(*p_va, int *); |
| 672 | if (arg == Py_None) |
| 673 | *q = 0; |
| 674 | else |
| 675 | *q = PyString_Size(arg); |
| 676 | format++; |
| 677 | } |
| 678 | else if (*p != NULL && |
| 679 | (int)strlen(*p) != PyString_Size(arg)) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 680 | return "string without null bytes or None"; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 681 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 682 | break; |
| 683 | } |
| 684 | |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 685 | case 'e': /* encoded string */ |
| 686 | { |
| 687 | char **buffer; |
| 688 | const char *encoding; |
| 689 | PyObject *u, *s; |
| 690 | int size; |
| 691 | |
| 692 | /* Get 'e' parameter: the encoding name */ |
| 693 | encoding = (const char *)va_arg(*p_va, const char *); |
| 694 | if (encoding == NULL) |
Marc-André Lemburg | bff879c | 2000-08-03 18:46:08 +0000 | [diff] [blame] | 695 | encoding = PyUnicode_GetDefaultEncoding(); |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 696 | |
| 697 | /* Get 's' parameter: the output buffer to use */ |
| 698 | if (*format != 's') |
Guido van Rossum | 50fbb15 | 2000-03-28 02:00:29 +0000 | [diff] [blame] | 699 | return "(unknown parser marker combination)"; |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 700 | buffer = (char **)va_arg(*p_va, char **); |
| 701 | format++; |
| 702 | if (buffer == NULL) |
| 703 | return "(buffer is NULL)"; |
| 704 | |
| 705 | /* Convert object to Unicode */ |
| 706 | u = PyUnicode_FromObject(arg); |
| 707 | if (u == NULL) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 708 | return "string or unicode or text buffer"; |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 709 | |
| 710 | /* Encode object; use default error handling */ |
| 711 | s = PyUnicode_AsEncodedString(u, |
| 712 | encoding, |
| 713 | NULL); |
| 714 | Py_DECREF(u); |
| 715 | if (s == NULL) |
| 716 | return "(encoding failed)"; |
| 717 | if (!PyString_Check(s)) { |
| 718 | Py_DECREF(s); |
| 719 | return "(encoder failed to return a string)"; |
| 720 | } |
| 721 | size = PyString_GET_SIZE(s); |
| 722 | |
| 723 | /* Write output; output is guaranteed to be |
| 724 | 0-terminated */ |
| 725 | if (*format == '#') { |
| 726 | /* Using buffer length parameter '#': |
| 727 | |
| 728 | - if *buffer is NULL, a new buffer |
| 729 | of the needed size is allocated and |
| 730 | the data copied into it; *buffer is |
| 731 | updated to point to the new buffer; |
| 732 | the caller is responsible for |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 733 | PyMem_Free()ing it after usage |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 734 | |
| 735 | - if *buffer is not NULL, the data |
| 736 | is copied to *buffer; *buffer_len |
| 737 | has to be set to the size of the |
| 738 | buffer on input; buffer overflow is |
| 739 | signalled with an error; buffer has |
| 740 | to provide enough room for the |
| 741 | encoded string plus the trailing |
| 742 | 0-byte |
| 743 | |
| 744 | - in both cases, *buffer_len is |
| 745 | updated to the size of the buffer |
| 746 | /excluding/ the trailing 0-byte |
| 747 | |
| 748 | */ |
| 749 | int *buffer_len = va_arg(*p_va, int *); |
| 750 | |
| 751 | format++; |
| 752 | if (buffer_len == NULL) |
| 753 | return "(buffer_len is NULL)"; |
| 754 | if (*buffer == NULL) { |
| 755 | *buffer = PyMem_NEW(char, size + 1); |
| 756 | if (*buffer == NULL) { |
| 757 | Py_DECREF(s); |
| 758 | return "(memory error)"; |
| 759 | } |
| 760 | } else { |
| 761 | if (size + 1 > *buffer_len) { |
| 762 | Py_DECREF(s); |
| 763 | return "(buffer overflow)"; |
| 764 | } |
| 765 | } |
| 766 | memcpy(*buffer, |
| 767 | PyString_AS_STRING(s), |
| 768 | size + 1); |
| 769 | *buffer_len = size; |
| 770 | } else { |
| 771 | /* Using a 0-terminated buffer: |
| 772 | |
| 773 | - the encoded string has to be |
| 774 | 0-terminated for this variant to |
| 775 | work; if it is not, an error raised |
| 776 | |
| 777 | - a new buffer of the needed size |
| 778 | is allocated and the data copied |
| 779 | into it; *buffer is updated to |
| 780 | point to the new buffer; the caller |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 781 | is responsible for PyMem_Free()ing it |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 782 | after usage |
| 783 | |
| 784 | */ |
Guido van Rossum | 50fbb15 | 2000-03-28 02:00:29 +0000 | [diff] [blame] | 785 | if ((int)strlen(PyString_AS_STRING(s)) != size) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 786 | return "(encoded string without NULL bytes)"; |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 787 | *buffer = PyMem_NEW(char, size + 1); |
| 788 | if (*buffer == NULL) { |
| 789 | Py_DECREF(s); |
| 790 | return "(memory error)"; |
| 791 | } |
| 792 | memcpy(*buffer, |
| 793 | PyString_AS_STRING(s), |
| 794 | size + 1); |
| 795 | } |
| 796 | Py_DECREF(s); |
| 797 | break; |
| 798 | } |
| 799 | |
Fred Drake | 25871c0 | 2000-05-03 15:17:02 +0000 | [diff] [blame] | 800 | case 'u': /* raw unicode buffer (Py_UNICODE *) */ |
| 801 | { |
| 802 | if (*format == '#') { /* any buffer-like object */ |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 803 | void **p = (void **)va_arg(*p_va, char **); |
| 804 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
Fred Drake | 25871c0 | 2000-05-03 15:17:02 +0000 | [diff] [blame] | 805 | int *q = va_arg(*p_va, int *); |
| 806 | int count; |
| 807 | |
| 808 | if ( pb == NULL || |
| 809 | pb->bf_getreadbuffer == NULL || |
| 810 | pb->bf_getsegcount == NULL ) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 811 | return "unicode or read-only buffer"; |
Fred Drake | 25871c0 | 2000-05-03 15:17:02 +0000 | [diff] [blame] | 812 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 813 | return "unicode or single-segment read-only buffer"; |
Fred Drake | 25871c0 | 2000-05-03 15:17:02 +0000 | [diff] [blame] | 814 | if ( (count = |
| 815 | (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 ) |
| 816 | return "(unspecified)"; |
| 817 | /* buffer interface returns bytes, we want |
| 818 | length in characters */ |
| 819 | *q = count/(sizeof(Py_UNICODE)); |
| 820 | format++; |
| 821 | } else { |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 822 | Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **); |
Fred Drake | 25871c0 | 2000-05-03 15:17:02 +0000 | [diff] [blame] | 823 | |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 824 | if (PyUnicode_Check(arg)) |
Fred Drake | 25871c0 | 2000-05-03 15:17:02 +0000 | [diff] [blame] | 825 | *p = PyUnicode_AS_UNICODE(arg); |
| 826 | else |
| 827 | return "unicode"; |
| 828 | } |
| 829 | break; |
| 830 | } |
| 831 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 832 | case 'S': /* string object */ |
| 833 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 834 | PyObject **p = va_arg(*p_va, PyObject **); |
| 835 | if (PyString_Check(arg)) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 836 | *p = arg; |
| 837 | else |
| 838 | return "string"; |
| 839 | break; |
| 840 | } |
| 841 | |
Guido van Rossum | e826ef0 | 2000-03-10 23:02:17 +0000 | [diff] [blame] | 842 | case 'U': /* Unicode object */ |
| 843 | { |
| 844 | PyObject **p = va_arg(*p_va, PyObject **); |
| 845 | if (PyUnicode_Check(arg)) |
| 846 | *p = arg; |
| 847 | else |
| 848 | return "unicode"; |
| 849 | break; |
| 850 | } |
| 851 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 852 | case 'O': /* object */ |
| 853 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 854 | PyTypeObject *type; |
| 855 | PyObject **p; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 856 | if (*format == '!') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 857 | type = va_arg(*p_va, PyTypeObject*); |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 858 | p = va_arg(*p_va, PyObject **); |
| 859 | format++; |
| 860 | if (arg->ob_type == type) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 861 | *p = arg; |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 862 | else |
| 863 | return type->tp_name; |
| 864 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 865 | } |
| 866 | else if (*format == '?') { |
| 867 | inquiry pred = va_arg(*p_va, inquiry); |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 868 | p = va_arg(*p_va, PyObject **); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 869 | format++; |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 870 | if ((*pred)(arg)) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 871 | *p = arg; |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 872 | else |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 873 | return "(unspecified)"; |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 874 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 875 | } |
| 876 | else if (*format == '&') { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 877 | typedef int (*converter)(PyObject *, void *); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 878 | converter convert = va_arg(*p_va, converter); |
| 879 | void *addr = va_arg(*p_va, void *); |
| 880 | format++; |
| 881 | if (! (*convert)(arg, addr)) |
Guido van Rossum | 64fc649 | 1995-01-21 14:09:37 +0000 | [diff] [blame] | 882 | return "(unspecified)"; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 883 | } |
| 884 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 885 | p = va_arg(*p_va, PyObject **); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 886 | *p = arg; |
| 887 | } |
| 888 | break; |
| 889 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 890 | |
| 891 | |
| 892 | case 'w': /* memory buffer, read-write access */ |
| 893 | { |
| 894 | void **p = va_arg(*p_va, void **); |
| 895 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
| 896 | int count; |
| 897 | |
| 898 | if ( pb == NULL || pb->bf_getwritebuffer == NULL || |
| 899 | pb->bf_getsegcount == NULL ) |
| 900 | return "read-write buffer"; |
| 901 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
| 902 | return "single-segment read-write buffer"; |
| 903 | if ( (count = pb->bf_getwritebuffer(arg, 0, p)) < 0 ) |
| 904 | return "(unspecified)"; |
| 905 | if (*format == '#') { |
| 906 | int *q = va_arg(*p_va, int *); |
| 907 | |
| 908 | *q = count; |
| 909 | format++; |
| 910 | } |
| 911 | break; |
| 912 | } |
| 913 | |
Guido van Rossum | b317f8a | 1998-10-08 02:21:21 +0000 | [diff] [blame] | 914 | case 't': /* 8-bit character buffer, read-only access */ |
| 915 | { |
| 916 | const char **p = va_arg(*p_va, const char **); |
| 917 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
| 918 | int count; |
| 919 | |
| 920 | if ( *format++ != '#' ) |
| 921 | return "invalid use of 't' format character"; |
| 922 | if ( !PyType_HasFeature( |
| 923 | arg->ob_type, |
| 924 | Py_TPFLAGS_HAVE_GETCHARBUFFER) || |
| 925 | pb == NULL || |
| 926 | pb->bf_getcharbuffer == NULL || |
| 927 | pb->bf_getsegcount == NULL ) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 928 | return "string or read-only character buffer"; |
Guido van Rossum | b317f8a | 1998-10-08 02:21:21 +0000 | [diff] [blame] | 929 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
Guido van Rossum | 60a1e7f | 2000-12-01 12:59:05 +0000 | [diff] [blame] | 930 | return "string or single-segment read-only buffer"; |
Guido van Rossum | b317f8a | 1998-10-08 02:21:21 +0000 | [diff] [blame] | 931 | if ( (count = pb->bf_getcharbuffer(arg, 0, p)) < 0 ) |
| 932 | return "(unspecified)"; |
| 933 | |
| 934 | *va_arg(*p_va, int *) = count; |
| 935 | |
| 936 | break; |
| 937 | } |
| 938 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 939 | |
| 940 | default: |
| 941 | return "impossible<bad format char>"; |
| 942 | |
| 943 | } |
| 944 | |
| 945 | *p_format = format; |
| 946 | return NULL; |
| 947 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 948 | |
| 949 | |
| 950 | /* Support for keyword arguments donated by |
| 951 | Geoff Philbrick <philbric@delphi.hks.com> */ |
| 952 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 953 | int PyArg_ParseTupleAndKeywords(PyObject *args, |
| 954 | PyObject *keywords, |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 955 | char *format, |
| 956 | char **kwlist, ...) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 957 | { |
| 958 | int retval; |
| 959 | va_list va; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 960 | |
| 961 | va_start(va, kwlist); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 962 | retval = vgetargskeywords(args, keywords, format, kwlist, &va); |
| 963 | va_end(va); |
| 964 | return retval; |
| 965 | } |
| 966 | |
| 967 | |
| 968 | static int |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 969 | vgetargskeywords(PyObject *args, PyObject *keywords, char *format, |
| 970 | char **kwlist, va_list *p_va) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 971 | { |
| 972 | char msgbuf[256]; |
| 973 | int levels[32]; |
| 974 | char *fname = NULL; |
| 975 | char *message = NULL; |
| 976 | int min = -1; |
| 977 | int max = 0; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 978 | char *formatsave = format; |
| 979 | int i, len, tplen, kwlen; |
| 980 | char *msg, *ks, **p; |
| 981 | int nkwds, pos, match, converted; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 982 | PyObject *key, *value; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 983 | |
| 984 | /* nested tuples cannot be parsed when using keyword arguments */ |
| 985 | |
| 986 | for (;;) { |
| 987 | int c = *format++; |
| 988 | if (c == '(') { |
| 989 | PyErr_SetString(PyExc_SystemError, |
| 990 | "tuple found in format when using keyword arguments"); |
| 991 | return 0; |
| 992 | } |
| 993 | else if (c == '\0') |
| 994 | break; |
| 995 | else if (c == ':') { |
| 996 | fname = format; |
| 997 | break; |
| 998 | } |
| 999 | else if (c == ';') { |
| 1000 | message = format; |
| 1001 | break; |
| 1002 | } |
Marc-André Lemburg | bbcf2a7 | 2000-09-08 11:49:37 +0000 | [diff] [blame] | 1003 | else if (c == 'e') |
| 1004 | ; /* Pass */ |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1005 | else if (isalpha(c)) |
| 1006 | max++; |
| 1007 | else if (c == '|') |
| 1008 | min = max; |
| 1009 | } |
| 1010 | |
| 1011 | if (min < 0) |
| 1012 | min = max; |
| 1013 | |
| 1014 | format = formatsave; |
| 1015 | |
| 1016 | if (!PyTuple_Check(args)) { |
| 1017 | PyErr_SetString(PyExc_SystemError, |
| 1018 | "new style getargs format but argument is not a tuple"); |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | tplen = PyTuple_Size(args); |
| 1023 | |
| 1024 | /* do a cursory check of the keywords just to see how many we got */ |
| 1025 | |
| 1026 | if (keywords) { |
| 1027 | if (!PyDict_Check(keywords)) { |
Jeremy Hylton | a0ac40c | 2001-01-25 20:13:10 +0000 | [diff] [blame] | 1028 | if (keywords == NULL) |
| 1029 | PyErr_SetString(PyExc_SystemError, |
| 1030 | "NULL received when keyword dictionary expected"); |
| 1031 | else |
| 1032 | PyErr_Format(PyExc_SystemError, |
| 1033 | "%s received when keyword dictionary expected", |
| 1034 | keywords->ob_type->tp_name); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1035 | return 0; |
| 1036 | } |
| 1037 | kwlen = PyDict_Size(keywords); |
| 1038 | } |
| 1039 | else { |
| 1040 | kwlen = 0; |
| 1041 | } |
| 1042 | |
| 1043 | /* make sure there are no duplicate values for an argument; |
| 1044 | its not clear when to use the term "keyword argument vs. |
| 1045 | keyword parameter in messages */ |
| 1046 | |
| 1047 | if (keywords) { |
| 1048 | for (i = 0; i < tplen; i++) { |
| 1049 | if (PyMapping_HasKeyString(keywords, kwlist[i])) { |
| 1050 | sprintf(msgbuf, |
| 1051 | "keyword parameter %s redefined", |
| 1052 | kwlist[i]); |
| 1053 | PyErr_SetString(PyExc_TypeError, msgbuf); |
| 1054 | return 0; |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | PyErr_Clear(); /* I'm not which Py functions set the error string */ |
| 1059 | |
| 1060 | /* required arguments missing from args can be supplied by keyword |
| 1061 | arguments */ |
| 1062 | |
| 1063 | len = tplen; |
| 1064 | if (keywords && tplen < min) { |
| 1065 | for (i = tplen; i < min; i++) { |
| 1066 | if (PyMapping_HasKeyString(keywords, kwlist[i])) { |
| 1067 | len++; |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | PyErr_Clear(); |
| 1072 | |
| 1073 | /* make sure we got an acceptable number of arguments; the message |
| 1074 | is a little confusing with keywords since keyword arguments |
| 1075 | which are supplied, but don't match the required arguments |
| 1076 | are not included in the "%d given" part of the message */ |
| 1077 | |
| 1078 | if (len < min || max < len) { |
| 1079 | if (message == NULL) { |
| 1080 | sprintf(msgbuf, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 1081 | "%s%s takes %s %d argument%s (%d given)", |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1082 | fname==NULL ? "function" : fname, |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 1083 | fname==NULL ? "" : "()", |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1084 | min==max ? "exactly" |
| 1085 | : len < min ? "at least" : "at most", |
| 1086 | len < min ? min : max, |
| 1087 | (len < min ? min : max) == 1 ? "" : "s", |
| 1088 | len); |
| 1089 | message = msgbuf; |
| 1090 | } |
| 1091 | PyErr_SetString(PyExc_TypeError, message); |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | for (i = 0; i < tplen; i++) { |
| 1096 | if (*format == '|') |
| 1097 | format++; |
| 1098 | msg = convertitem(PyTuple_GetItem(args, i), &format, p_va, |
| 1099 | levels, msgbuf); |
| 1100 | if (msg) { |
| 1101 | seterror(i+1, msg, levels, fname, message); |
| 1102 | return 0; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | /* handle no keyword parameters in call */ |
| 1107 | |
| 1108 | if (!keywords) return 1; |
| 1109 | |
| 1110 | /* make sure the number of keywords in the keyword list matches the |
| 1111 | number of items in the format string */ |
| 1112 | |
| 1113 | nkwds = 0; |
| 1114 | p = kwlist; |
| 1115 | for (;;) { |
| 1116 | if (!*(p++)) break; |
| 1117 | nkwds++; |
| 1118 | } |
| 1119 | |
| 1120 | if (nkwds != max) { |
| 1121 | PyErr_SetString(PyExc_SystemError, |
| 1122 | "number of items in format string and keyword list do not match"); |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | /* convert the keyword arguments; this uses the format |
| 1127 | string where it was left after processing args */ |
| 1128 | |
| 1129 | converted = 0; |
| 1130 | for (i = tplen; i < nkwds; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1131 | PyObject *item; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1132 | if (*format == '|') |
| 1133 | format++; |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1134 | item = PyMapping_GetItemString(keywords, kwlist[i]); |
| 1135 | if (item != NULL) { |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1136 | msg = convertitem(item, &format, p_va, levels, msgbuf); |
| 1137 | if (msg) { |
| 1138 | seterror(i+1, msg, levels, fname, message); |
| 1139 | return 0; |
| 1140 | } |
| 1141 | converted++; |
Barry Warsaw | 0705028 | 2000-12-11 20:01:55 +0000 | [diff] [blame] | 1142 | Py_DECREF(item); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1143 | } |
| 1144 | else { |
| 1145 | PyErr_Clear(); |
| 1146 | msg = skipitem(&format, p_va); |
| 1147 | if (msg) { |
| 1148 | seterror(i+1, msg, levels, fname, message); |
| 1149 | return 0; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | /* make sure there are no extraneous keyword arguments */ |
| 1155 | |
| 1156 | pos = 0; |
| 1157 | if (converted < kwlen) { |
| 1158 | while (PyDict_Next(keywords, &pos, &key, &value)) { |
| 1159 | match = 0; |
| 1160 | ks = PyString_AsString(key); |
| 1161 | for (i = 0; i < nkwds; i++) { |
| 1162 | if (!strcmp(ks, kwlist[i])) { |
| 1163 | match = 1; |
| 1164 | break; |
| 1165 | } |
| 1166 | } |
| 1167 | if (!match) { |
| 1168 | sprintf(msgbuf, |
Guido van Rossum | 80dc16b | 2000-05-08 14:02:41 +0000 | [diff] [blame] | 1169 | "%s is an invalid keyword argument for this function", |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1170 | ks); |
| 1171 | PyErr_SetString(PyExc_TypeError, msgbuf); |
| 1172 | return 0; |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | return 1; |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | static char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 1182 | skipitem(char **p_format, va_list *p_va) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1183 | { |
| 1184 | char *format = *p_format; |
| 1185 | char c = *format++; |
| 1186 | |
| 1187 | switch (c) { |
| 1188 | |
| 1189 | case 'b': /* byte -- very short int */ |
Jack Jansen | cc22fbe | 2000-08-05 21:29:58 +0000 | [diff] [blame] | 1190 | case 'B': /* byte as bitfield */ |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1191 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1192 | (void) va_arg(*p_va, char *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1193 | break; |
| 1194 | } |
| 1195 | |
| 1196 | case 'h': /* short int */ |
| 1197 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1198 | (void) va_arg(*p_va, short *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1199 | break; |
| 1200 | } |
| 1201 | |
Jack Jansen | cc22fbe | 2000-08-05 21:29:58 +0000 | [diff] [blame] | 1202 | case 'H': /* short int as bitfield */ |
Jack Jansen | d50338f | 2000-07-06 12:22:00 +0000 | [diff] [blame] | 1203 | { |
| 1204 | (void) va_arg(*p_va, unsigned short *); |
| 1205 | break; |
| 1206 | } |
| 1207 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1208 | case 'i': /* int */ |
| 1209 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1210 | (void) va_arg(*p_va, int *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1211 | break; |
| 1212 | } |
| 1213 | |
| 1214 | case 'l': /* long int */ |
| 1215 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1216 | (void) va_arg(*p_va, long *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1217 | break; |
| 1218 | } |
| 1219 | |
Guido van Rossum | 3dbba6e | 1999-01-25 21:48:56 +0000 | [diff] [blame] | 1220 | #ifdef HAVE_LONG_LONG |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 1221 | case 'L': /* LONG_LONG int */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1222 | { |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 1223 | (void) va_arg(*p_va, LONG_LONG *); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1224 | break; |
| 1225 | } |
| 1226 | #endif |
| 1227 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1228 | case 'f': /* float */ |
| 1229 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1230 | (void) va_arg(*p_va, float *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1231 | break; |
| 1232 | } |
| 1233 | |
| 1234 | case 'd': /* double */ |
| 1235 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1236 | (void) va_arg(*p_va, double *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1237 | break; |
| 1238 | } |
| 1239 | |
| 1240 | #ifndef WITHOUT_COMPLEX |
| 1241 | case 'D': /* complex double */ |
| 1242 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1243 | (void) va_arg(*p_va, Py_complex *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1244 | break; |
| 1245 | } |
| 1246 | #endif /* WITHOUT_COMPLEX */ |
| 1247 | |
| 1248 | case 'c': /* char */ |
| 1249 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1250 | (void) va_arg(*p_va, char *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | } |
| 1253 | |
| 1254 | case 's': /* string */ |
| 1255 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1256 | (void) va_arg(*p_va, char **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1257 | if (*format == '#') { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1258 | (void) va_arg(*p_va, int *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1259 | format++; |
| 1260 | } |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | case 'z': /* string */ |
| 1265 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1266 | (void) va_arg(*p_va, char **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1267 | if (*format == '#') { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1268 | (void) va_arg(*p_va, int *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1269 | format++; |
| 1270 | } |
| 1271 | break; |
| 1272 | } |
| 1273 | |
| 1274 | case 'S': /* string object */ |
| 1275 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1276 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1277 | break; |
| 1278 | } |
| 1279 | |
| 1280 | case 'O': /* object */ |
| 1281 | { |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1282 | if (*format == '!') { |
| 1283 | format++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1284 | (void) va_arg(*p_va, PyTypeObject*); |
| 1285 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1286 | } |
| 1287 | #if 0 |
| 1288 | /* I don't know what this is for */ |
| 1289 | else if (*format == '?') { |
| 1290 | inquiry pred = va_arg(*p_va, inquiry); |
| 1291 | format++; |
| 1292 | if ((*pred)(arg)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1293 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | #endif |
| 1297 | else if (*format == '&') { |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 1298 | typedef int (*converter)(PyObject *, void *); |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1299 | (void) va_arg(*p_va, converter); |
| 1300 | (void) va_arg(*p_va, void *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1301 | format++; |
| 1302 | } |
| 1303 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1304 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1305 | } |
| 1306 | break; |
| 1307 | } |
| 1308 | |
| 1309 | default: |
| 1310 | return "impossible<bad format char>"; |
| 1311 | |
| 1312 | } |
| 1313 | |
| 1314 | *p_format = format; |
| 1315 | return NULL; |
| 1316 | } |