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