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