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