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