Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* New getargs implementation */ |
| 33 | |
| 34 | /* XXX There are several unchecked sprintf or strcat calls in this file. |
| 35 | XXX The only way these can become a danger is if some C code in the |
| 36 | XXX Python source (or in an extension) uses ridiculously long names |
| 37 | XXX or riduculously deep nesting in format strings. */ |
| 38 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 39 | #include "Python.h" |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | c1d5053 | 1996-08-21 23:38:24 +0000 | [diff] [blame] | 41 | #include <ctype.h> |
| 42 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 44 | int PyArg_Parse Py_PROTO((PyObject *, char *, ...)); |
| 45 | int PyArg_ParseTuple Py_PROTO((PyObject *, char *, ...)); |
Guido van Rossum | 13454c3 | 1997-05-05 21:57:29 +0000 | [diff] [blame] | 46 | int PyArg_VaParse Py_PROTO((PyObject *, char *, va_list)); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 48 | int PyArg_ParseTupleAndKeywords Py_PROTO((PyObject *, PyObject *, |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 49 | char *, char **, ...)); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 50 | |
| 51 | /* Forward */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 52 | static int vgetargs1 Py_PROTO((PyObject *, char *, va_list *, int)); |
| 53 | static void seterror Py_PROTO((int, char *, int *, char *, char *)); |
| 54 | static char *convertitem Py_PROTO((PyObject *, char **, va_list *, |
| 55 | int *, char *)); |
| 56 | static char *converttuple Py_PROTO((PyObject *, char **, va_list *, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 57 | int *, char *, int)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 58 | static char *convertsimple Py_PROTO((PyObject *, char **, va_list *, char *)); |
| 59 | static char *convertsimple1 Py_PROTO((PyObject *, char **, va_list *)); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 61 | static int vgetargskeywords Py_PROTO((PyObject *, PyObject *, |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 62 | char *, char **, va_list *)); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 63 | static char *skipitem Py_PROTO((char **, va_list *)); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 64 | |
| 65 | #ifdef HAVE_STDARG_PROTOTYPES |
| 66 | /* VARARGS2 */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 67 | int PyArg_Parse(PyObject *args, char *format, ...) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 68 | #else |
| 69 | /* VARARGS */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 70 | int PyArg_Parse(va_alist) va_dcl |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 71 | #endif |
| 72 | { |
| 73 | int retval; |
| 74 | va_list va; |
| 75 | #ifdef HAVE_STDARG_PROTOTYPES |
| 76 | |
| 77 | va_start(va, format); |
| 78 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 79 | PyObject *args; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 80 | char *format; |
| 81 | |
| 82 | va_start(va); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 83 | args = va_arg(va, PyObject *); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 84 | format = va_arg(va, char *); |
| 85 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 86 | retval = vgetargs1(args, format, &va, 1); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 87 | va_end(va); |
| 88 | return retval; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | #ifdef HAVE_STDARG_PROTOTYPES |
| 93 | /* VARARGS2 */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 94 | int PyArg_ParseTuple(PyObject *args, char *format, ...) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 95 | #else |
| 96 | /* VARARGS */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 97 | int PyArg_ParseTuple(va_alist) va_dcl |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 98 | #endif |
| 99 | { |
| 100 | int retval; |
| 101 | va_list va; |
| 102 | #ifdef HAVE_STDARG_PROTOTYPES |
| 103 | |
| 104 | va_start(va, format); |
| 105 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 106 | PyObject *args; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 107 | char *format; |
| 108 | |
| 109 | va_start(va); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 110 | args = va_arg(va, PyObject *); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 111 | format = va_arg(va, char *); |
| 112 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 113 | retval = vgetargs1(args, format, &va, 0); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 114 | va_end(va); |
| 115 | return retval; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | int |
Guido van Rossum | 13454c3 | 1997-05-05 21:57:29 +0000 | [diff] [blame] | 120 | PyArg_VaParse(args, format, va) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 121 | PyObject *args; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 122 | char *format; |
| 123 | va_list va; |
| 124 | { |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 125 | va_list lva; |
| 126 | |
| 127 | #ifdef VA_LIST_IS_ARRAY |
| 128 | memcpy(lva, va, sizeof(va_list)); |
| 129 | #else |
| 130 | lva = va; |
| 131 | #endif |
| 132 | |
| 133 | return vgetargs1(args, format, &lva, 0); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
| 137 | static int |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 138 | vgetargs1(args, format, p_va, compat) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 139 | PyObject *args; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 140 | char *format; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 141 | va_list *p_va; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 142 | int compat; |
| 143 | { |
| 144 | char msgbuf[256]; |
| 145 | int levels[32]; |
| 146 | char *fname = NULL; |
| 147 | char *message = NULL; |
| 148 | int min = -1; |
| 149 | int max = 0; |
| 150 | int level = 0; |
| 151 | char *formatsave = format; |
| 152 | int i, len; |
| 153 | char *msg; |
| 154 | |
| 155 | for (;;) { |
| 156 | int c = *format++; |
| 157 | if (c == '(' /* ')' */) { |
| 158 | if (level == 0) |
| 159 | max++; |
| 160 | level++; |
| 161 | } |
| 162 | else if (/* '(' */ c == ')') { |
| 163 | if (level == 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 164 | Py_FatalError(/* '(' */ |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 165 | "excess ')' in getargs format"); |
| 166 | else |
| 167 | level--; |
| 168 | } |
| 169 | else if (c == '\0') |
| 170 | break; |
| 171 | else if (c == ':') { |
| 172 | fname = format; |
| 173 | break; |
| 174 | } |
| 175 | else if (c == ';') { |
| 176 | message = format; |
| 177 | break; |
| 178 | } |
| 179 | else if (level != 0) |
| 180 | ; /* Pass */ |
| 181 | else if (isalpha(c)) |
| 182 | max++; |
| 183 | else if (c == '|') |
| 184 | min = max; |
| 185 | } |
| 186 | |
| 187 | if (level != 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 188 | Py_FatalError(/* '(' */ "missing ')' in getargs format"); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 189 | |
| 190 | if (min < 0) |
| 191 | min = max; |
| 192 | |
| 193 | format = formatsave; |
| 194 | |
| 195 | if (compat) { |
| 196 | if (max == 0) { |
| 197 | if (args == NULL) |
| 198 | return 1; |
| 199 | sprintf(msgbuf, "%s requires no arguments", |
| 200 | fname==NULL ? "function" : fname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 201 | PyErr_SetString(PyExc_TypeError, msgbuf); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | else if (min == 1 && max == 1) { |
Guido van Rossum | 13d0ed1 | 1994-11-10 22:35:48 +0000 | [diff] [blame] | 205 | if (args == NULL) { |
| 206 | sprintf(msgbuf, |
| 207 | "%s requires at least one argument", |
| 208 | fname==NULL ? "function" : fname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 209 | PyErr_SetString(PyExc_TypeError, msgbuf); |
Guido van Rossum | 13d0ed1 | 1994-11-10 22:35:48 +0000 | [diff] [blame] | 210 | return 0; |
| 211 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 212 | msg = convertitem(args, &format, p_va, levels, msgbuf); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 213 | if (msg == NULL) |
| 214 | return 1; |
| 215 | seterror(levels[0], msg, levels+1, fname, message); |
| 216 | return 0; |
| 217 | } |
| 218 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 219 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 220 | "old style getargs format uses new features"); |
| 221 | return 0; |
| 222 | } |
| 223 | } |
| 224 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 225 | if (!PyTuple_Check(args)) { |
| 226 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 227 | "new style getargs format but argument is not a tuple"); |
| 228 | return 0; |
| 229 | } |
| 230 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 231 | len = PyTuple_Size(args); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 232 | |
| 233 | if (len < min || max < len) { |
| 234 | if (message == NULL) { |
| 235 | sprintf(msgbuf, |
| 236 | "%s requires %s %d argument%s; %d given", |
| 237 | fname==NULL ? "function" : fname, |
| 238 | min==max ? "exactly" |
| 239 | : len < min ? "at least" : "at most", |
| 240 | len < min ? min : max, |
| 241 | (len < min ? min : max) == 1 ? "" : "s", |
| 242 | len); |
| 243 | message = msgbuf; |
| 244 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 245 | PyErr_SetString(PyExc_TypeError, message); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | for (i = 0; i < len; i++) { |
| 250 | if (*format == '|') |
| 251 | format++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 252 | msg = convertitem(PyTuple_GetItem(args, i), &format, p_va, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 253 | levels, msgbuf); |
| 254 | if (msg) { |
| 255 | seterror(i+1, msg, levels, fname, message); |
| 256 | return 0; |
| 257 | } |
| 258 | } |
Guido van Rossum | 231a41e | 1997-12-09 20:36:39 +0000 | [diff] [blame] | 259 | |
Guido van Rossum | 730806d | 1998-04-10 22:27:42 +0000 | [diff] [blame] | 260 | if (*format != '\0' && !isalpha((int)(*format)) && |
Guido van Rossum | 7d4f68c | 1997-12-19 04:25:23 +0000 | [diff] [blame] | 261 | *format != '(' && |
Guido van Rossum | 231a41e | 1997-12-09 20:36:39 +0000 | [diff] [blame] | 262 | *format != '|' && *format != ':' && *format != ';') { |
| 263 | PyErr_Format(PyExc_SystemError, |
Guido van Rossum | 0d6b49e | 1998-01-19 22:22:44 +0000 | [diff] [blame] | 264 | "bad format string: %.200s", formatsave); |
Guido van Rossum | 231a41e | 1997-12-09 20:36:39 +0000 | [diff] [blame] | 265 | return 0; |
| 266 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 267 | |
| 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | static void |
| 274 | seterror(iarg, msg, levels, fname, message) |
| 275 | int iarg; |
| 276 | char *msg; |
| 277 | int *levels; |
| 278 | char *fname; |
| 279 | char *message; |
| 280 | { |
| 281 | char buf[256]; |
| 282 | int i; |
| 283 | char *p = buf; |
| 284 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 285 | if (PyErr_Occurred()) |
Guido van Rossum | 64fc649 | 1995-01-21 14:09:37 +0000 | [diff] [blame] | 286 | return; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 287 | if (iarg == 0 && message == NULL) |
| 288 | message = msg; |
| 289 | else if (message == NULL) { |
| 290 | if (fname != NULL) { |
| 291 | sprintf(p, "%s, ", fname); |
| 292 | p += strlen(p); |
| 293 | } |
| 294 | sprintf(p, "argument %d", iarg); |
| 295 | i = 0; |
| 296 | p += strlen(p); |
| 297 | while (levels[i] > 0) { |
| 298 | sprintf(p, ", item %d", levels[i]-1); |
| 299 | p += strlen(p); |
| 300 | i++; |
| 301 | } |
| 302 | sprintf(p, ": expected %s found", msg); |
| 303 | message = buf; |
| 304 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 305 | PyErr_SetString(PyExc_TypeError, message); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
| 309 | /* Convert a tuple argument. |
| 310 | On entry, *p_format points to the character _after_ the opening '('. |
| 311 | On successful exit, *p_format points to the closing ')'. |
| 312 | If successful: |
| 313 | *p_format and *p_va are updated, |
| 314 | *levels and *msgbuf are untouched, |
| 315 | and NULL is returned. |
| 316 | If the argument is invalid: |
| 317 | *p_format is unchanged, |
| 318 | *p_va is undefined, |
| 319 | *levels is a 0-terminated list of item numbers, |
| 320 | *msgbuf contains an error message, whose format is: |
| 321 | "<typename1>, <typename2>", where: |
| 322 | <typename1> is the name of the expected type, and |
| 323 | <typename2> is the name of the actual type, |
| 324 | (so you can surround it by "expected ... found"), |
| 325 | and msgbuf is returned. |
| 326 | */ |
| 327 | |
| 328 | static char * |
| 329 | converttuple(arg, p_format, p_va, levels, msgbuf, toplevel) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 330 | PyObject *arg; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 331 | char **p_format; |
| 332 | va_list *p_va; |
| 333 | int *levels; |
| 334 | char *msgbuf; |
| 335 | int toplevel; |
| 336 | { |
| 337 | int level = 0; |
| 338 | int n = 0; |
| 339 | char *format = *p_format; |
| 340 | int i; |
| 341 | |
| 342 | for (;;) { |
| 343 | int c = *format++; |
| 344 | if (c == '(') { |
| 345 | if (level == 0) |
| 346 | n++; |
| 347 | level++; |
| 348 | } |
| 349 | else if (c == ')') { |
| 350 | if (level == 0) |
| 351 | break; |
| 352 | level--; |
| 353 | } |
| 354 | else if (c == ':' || c == ';' || c == '\0') |
| 355 | break; |
| 356 | else if (level == 0 && isalpha(c)) |
| 357 | n++; |
| 358 | } |
| 359 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 360 | if (!PyTuple_Check(arg)) { |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 361 | levels[0] = 0; |
| 362 | sprintf(msgbuf, |
| 363 | toplevel ? "%d arguments, %s" : "%d-tuple, %s", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 364 | n, arg == Py_None ? "None" : arg->ob_type->tp_name); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 365 | return msgbuf; |
| 366 | } |
| 367 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 368 | if ((i = PyTuple_Size(arg)) != n) { |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 369 | levels[0] = 0; |
| 370 | sprintf(msgbuf, |
| 371 | toplevel ? "%d arguments, %d" : "%d-tuple, %d-tuple", |
| 372 | n, i); |
| 373 | return msgbuf; |
| 374 | } |
| 375 | |
| 376 | format = *p_format; |
| 377 | for (i = 0; i < n; i++) { |
| 378 | char *msg; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 379 | msg = convertitem(PyTuple_GetItem(arg, i), &format, p_va, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 380 | levels+1, msgbuf); |
| 381 | if (msg != NULL) { |
| 382 | levels[0] = i+1; |
| 383 | return msg; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | *p_format = format; |
| 388 | return NULL; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /* Convert a single item. */ |
| 393 | |
| 394 | static char * |
| 395 | convertitem(arg, p_format, p_va, levels, msgbuf) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 396 | PyObject *arg; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 397 | char **p_format; |
| 398 | va_list *p_va; |
| 399 | int *levels; |
| 400 | char *msgbuf; |
| 401 | { |
| 402 | char *msg; |
| 403 | char *format = *p_format; |
| 404 | |
| 405 | if (*format == '(' /* ')' */) { |
| 406 | format++; |
| 407 | msg = converttuple(arg, &format, p_va, levels, msgbuf, 0); |
| 408 | if (msg == NULL) |
| 409 | format++; |
| 410 | } |
| 411 | else { |
| 412 | msg = convertsimple(arg, &format, p_va, msgbuf); |
| 413 | if (msg != NULL) |
| 414 | levels[0] = 0; |
| 415 | } |
| 416 | if (msg == NULL) |
| 417 | *p_format = format; |
| 418 | return msg; |
| 419 | } |
| 420 | |
| 421 | |
| 422 | /* Convert a non-tuple argument. Adds to convertsimple1 functionality |
| 423 | by appending ", <actual argument type>" to error message. */ |
| 424 | |
| 425 | static char * |
| 426 | convertsimple(arg, p_format, p_va, msgbuf) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 427 | PyObject *arg; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 428 | char **p_format; |
| 429 | va_list *p_va; |
| 430 | char *msgbuf; |
| 431 | { |
| 432 | char *msg = convertsimple1(arg, p_format, p_va); |
| 433 | if (msg != NULL) { |
| 434 | sprintf(msgbuf, "%.50s, %.50s", msg, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 435 | arg == Py_None ? "None" : arg->ob_type->tp_name); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 436 | msg = msgbuf; |
| 437 | } |
| 438 | return msg; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | /* Convert a non-tuple argument. Return NULL if conversion went OK, |
| 443 | or a string representing the expected type if the conversion failed. |
| 444 | When failing, an exception may or may not have been raised. |
| 445 | Don't call if a tuple is expected. */ |
| 446 | |
| 447 | static char * |
| 448 | convertsimple1(arg, p_format, p_va) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 449 | PyObject *arg; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 450 | char **p_format; |
| 451 | va_list *p_va; |
| 452 | { |
| 453 | char *format = *p_format; |
| 454 | char c = *format++; |
| 455 | |
| 456 | switch (c) { |
| 457 | |
| 458 | case 'b': /* byte -- very short int */ |
| 459 | { |
| 460 | char *p = va_arg(*p_va, char *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 461 | long ival = PyInt_AsLong(arg); |
| 462 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 463 | return "integer<b>"; |
| 464 | else |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 465 | *p = (char) ival; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 466 | break; |
| 467 | } |
| 468 | |
| 469 | case 'h': /* short int */ |
| 470 | { |
| 471 | short *p = va_arg(*p_va, short *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 472 | long ival = PyInt_AsLong(arg); |
| 473 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 474 | return "integer<h>"; |
| 475 | else |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 476 | *p = (short) ival; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 477 | break; |
| 478 | } |
| 479 | |
| 480 | case 'i': /* int */ |
| 481 | { |
| 482 | int *p = va_arg(*p_va, int *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 483 | long ival = PyInt_AsLong(arg); |
| 484 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 485 | return "integer<i>"; |
| 486 | else |
| 487 | *p = ival; |
| 488 | break; |
| 489 | } |
| 490 | |
| 491 | case 'l': /* long int */ |
| 492 | { |
| 493 | long *p = va_arg(*p_va, long *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 494 | long ival = PyInt_AsLong(arg); |
| 495 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 496 | return "integer<l>"; |
| 497 | else |
| 498 | *p = ival; |
| 499 | break; |
| 500 | } |
| 501 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 502 | #if HAVE_LONG_LONG |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 503 | case 'L': /* LONG_LONG */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 504 | { |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 505 | LONG_LONG *p = va_arg( *p_va, LONG_LONG * ); |
| 506 | LONG_LONG ival = PyLong_AsLongLong( arg ); |
| 507 | if( ival == (LONG_LONG)-1 && PyErr_Occurred() ) { |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 508 | return "long<L>"; |
| 509 | } else { |
| 510 | *p = ival; |
| 511 | } |
| 512 | break; |
| 513 | } |
| 514 | #endif |
| 515 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 516 | case 'f': /* float */ |
| 517 | { |
| 518 | float *p = va_arg(*p_va, float *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 519 | double dval = PyFloat_AsDouble(arg); |
| 520 | if (PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 521 | return "float<f>"; |
| 522 | else |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 523 | *p = (float) dval; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 524 | break; |
| 525 | } |
| 526 | |
| 527 | case 'd': /* double */ |
| 528 | { |
| 529 | double *p = va_arg(*p_va, double *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 530 | double dval = PyFloat_AsDouble(arg); |
| 531 | if (PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 532 | return "float<d>"; |
| 533 | else |
| 534 | *p = dval; |
| 535 | break; |
| 536 | } |
| 537 | |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 538 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 539 | case 'D': /* complex double */ |
| 540 | { |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 541 | Py_complex *p = va_arg(*p_va, Py_complex *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 542 | Py_complex cval; |
| 543 | cval = PyComplex_AsCComplex(arg); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 544 | if (PyErr_Occurred()) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 545 | return "complex<D>"; |
| 546 | else |
| 547 | *p = cval; |
| 548 | break; |
| 549 | } |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 550 | #endif /* WITHOUT_COMPLEX */ |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 551 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 552 | case 'c': /* char */ |
| 553 | { |
| 554 | char *p = va_arg(*p_va, char *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 555 | if (PyString_Check(arg) && PyString_Size(arg) == 1) |
| 556 | *p = PyString_AsString(arg)[0]; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 557 | else |
| 558 | return "char"; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | case 's': /* string */ |
| 563 | { |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 564 | if (*format == '#') { /* any buffer-like object */ |
| 565 | void **p = (void **)va_arg(*p_va, char **); |
| 566 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 567 | int *q = va_arg(*p_va, int *); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 568 | int count; |
| 569 | |
| 570 | if ( pb == NULL || |
| 571 | pb->bf_getreadbuffer == NULL || |
| 572 | pb->bf_getsegcount == NULL ) |
| 573 | return "read-only buffer"; |
| 574 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
| 575 | return "single-segment read-only buffer"; |
| 576 | if ( (count = |
| 577 | (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 ) |
| 578 | return "(unspecified)"; |
| 579 | *q = count; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 580 | format++; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 581 | } else { |
| 582 | char **p = va_arg(*p_va, char **); |
| 583 | |
| 584 | if (PyString_Check(arg)) |
| 585 | *p = PyString_AsString(arg); |
| 586 | else |
| 587 | return "string"; |
| 588 | if ((int)strlen(*p) != PyString_Size(arg)) |
| 589 | return "string without null bytes"; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 590 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 591 | break; |
| 592 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 593 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 594 | case 'z': /* string, may be NULL (None) */ |
| 595 | { |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 596 | if (*format == '#') { /* any buffer-like object */ |
| 597 | void **p = (void **)va_arg(*p_va, char **); |
| 598 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 599 | int *q = va_arg(*p_va, int *); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 600 | int count; |
| 601 | |
| 602 | if (arg == Py_None) { |
| 603 | *p = 0; |
| 604 | *q = 0; |
| 605 | } else { |
| 606 | if ( pb == NULL || |
| 607 | pb->bf_getreadbuffer == NULL || |
| 608 | pb->bf_getsegcount == NULL ) |
| 609 | return "read-only buffer"; |
| 610 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
| 611 | return "single-segment read-only buffer"; |
| 612 | if ( (count = (*pb->bf_getreadbuffer) |
| 613 | (arg, 0, p)) < 0 ) |
| 614 | return "(unspecified)"; |
| 615 | *q = count; |
| 616 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 617 | format++; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 618 | } else { |
| 619 | char **p = va_arg(*p_va, char **); |
| 620 | |
| 621 | if (arg == Py_None) |
| 622 | *p = 0; |
| 623 | else if (PyString_Check(arg)) |
| 624 | *p = PyString_AsString(arg); |
| 625 | else |
| 626 | return "None or string"; |
| 627 | if (*format == '#') { |
| 628 | int *q = va_arg(*p_va, int *); |
| 629 | if (arg == Py_None) |
| 630 | *q = 0; |
| 631 | else |
| 632 | *q = PyString_Size(arg); |
| 633 | format++; |
| 634 | } |
| 635 | else if (*p != NULL && |
| 636 | (int)strlen(*p) != PyString_Size(arg)) |
| 637 | return "None or string without null bytes"; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 638 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 639 | break; |
| 640 | } |
| 641 | |
| 642 | case 'S': /* string object */ |
| 643 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 644 | PyObject **p = va_arg(*p_va, PyObject **); |
| 645 | if (PyString_Check(arg)) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 646 | *p = arg; |
| 647 | else |
| 648 | return "string"; |
| 649 | break; |
| 650 | } |
| 651 | |
| 652 | case 'O': /* object */ |
| 653 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 654 | PyTypeObject *type; |
| 655 | PyObject **p; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 656 | if (*format == '!') { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 657 | type = va_arg(*p_va, PyTypeObject*); |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 658 | p = va_arg(*p_va, PyObject **); |
| 659 | format++; |
| 660 | if (arg->ob_type == type) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 661 | *p = arg; |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 662 | else |
| 663 | return type->tp_name; |
| 664 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 665 | } |
| 666 | else if (*format == '?') { |
| 667 | inquiry pred = va_arg(*p_va, inquiry); |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 668 | p = va_arg(*p_va, PyObject **); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 669 | format++; |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 670 | if ((*pred)(arg)) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 671 | *p = arg; |
Guido van Rossum | fccfe89 | 1998-05-15 22:04:07 +0000 | [diff] [blame] | 672 | else |
| 673 | return "(unspecified)"; |
| 674 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 675 | } |
| 676 | else if (*format == '&') { |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 677 | typedef int (*converter) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 678 | Py_PROTO((PyObject *, void *)); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 679 | converter convert = va_arg(*p_va, converter); |
| 680 | void *addr = va_arg(*p_va, void *); |
| 681 | format++; |
| 682 | if (! (*convert)(arg, addr)) |
Guido van Rossum | 64fc649 | 1995-01-21 14:09:37 +0000 | [diff] [blame] | 683 | return "(unspecified)"; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 684 | } |
| 685 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 686 | p = va_arg(*p_va, PyObject **); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 687 | *p = arg; |
| 688 | } |
| 689 | break; |
| 690 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 691 | |
| 692 | |
| 693 | case 'w': /* memory buffer, read-write access */ |
| 694 | { |
| 695 | void **p = va_arg(*p_va, void **); |
| 696 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
| 697 | int count; |
| 698 | |
| 699 | if ( pb == NULL || pb->bf_getwritebuffer == NULL || |
| 700 | pb->bf_getsegcount == NULL ) |
| 701 | return "read-write buffer"; |
| 702 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
| 703 | return "single-segment read-write buffer"; |
| 704 | if ( (count = pb->bf_getwritebuffer(arg, 0, p)) < 0 ) |
| 705 | return "(unspecified)"; |
| 706 | if (*format == '#') { |
| 707 | int *q = va_arg(*p_va, int *); |
| 708 | |
| 709 | *q = count; |
| 710 | format++; |
| 711 | } |
| 712 | break; |
| 713 | } |
| 714 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 715 | |
| 716 | default: |
| 717 | return "impossible<bad format char>"; |
| 718 | |
| 719 | } |
| 720 | |
| 721 | *p_format = format; |
| 722 | return NULL; |
| 723 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 724 | |
| 725 | |
| 726 | /* Support for keyword arguments donated by |
| 727 | Geoff Philbrick <philbric@delphi.hks.com> */ |
| 728 | |
| 729 | #ifdef HAVE_STDARG_PROTOTYPES |
| 730 | /* VARARGS2 */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 731 | int PyArg_ParseTupleAndKeywords(PyObject *args, |
| 732 | PyObject *keywords, |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 733 | char *format, |
| 734 | char **kwlist, ...) |
| 735 | #else |
| 736 | /* VARARGS */ |
| 737 | int PyArg_ParseTupleAndKeywords(va_alist) va_dcl |
| 738 | #endif |
| 739 | { |
| 740 | int retval; |
| 741 | va_list va; |
| 742 | #ifdef HAVE_STDARG_PROTOTYPES |
| 743 | |
| 744 | va_start(va, kwlist); |
| 745 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 746 | PyObject *args; |
| 747 | PyObject *keywords; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 748 | char *format; |
| 749 | char **kwlist; |
| 750 | |
| 751 | va_start(va); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 752 | args = va_arg(va, PyObject *); |
| 753 | keywords = va_arg(va, PyObject *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 754 | format = va_arg(va, char *); |
| 755 | kwlist = va_arg(va, char **); |
| 756 | #endif |
| 757 | retval = vgetargskeywords(args, keywords, format, kwlist, &va); |
| 758 | va_end(va); |
| 759 | return retval; |
| 760 | } |
| 761 | |
| 762 | |
| 763 | static int |
| 764 | vgetargskeywords(args, keywords, format, kwlist, p_va) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 765 | PyObject *args; |
| 766 | PyObject *keywords; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 767 | char *format; |
| 768 | char **kwlist; |
| 769 | va_list *p_va; |
| 770 | { |
| 771 | char msgbuf[256]; |
| 772 | int levels[32]; |
| 773 | char *fname = NULL; |
| 774 | char *message = NULL; |
| 775 | int min = -1; |
| 776 | int max = 0; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 777 | char *formatsave = format; |
| 778 | int i, len, tplen, kwlen; |
| 779 | char *msg, *ks, **p; |
| 780 | int nkwds, pos, match, converted; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 781 | PyObject *key, *value; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 782 | |
| 783 | /* nested tuples cannot be parsed when using keyword arguments */ |
| 784 | |
| 785 | for (;;) { |
| 786 | int c = *format++; |
| 787 | if (c == '(') { |
| 788 | PyErr_SetString(PyExc_SystemError, |
| 789 | "tuple found in format when using keyword arguments"); |
| 790 | return 0; |
| 791 | } |
| 792 | else if (c == '\0') |
| 793 | break; |
| 794 | else if (c == ':') { |
| 795 | fname = format; |
| 796 | break; |
| 797 | } |
| 798 | else if (c == ';') { |
| 799 | message = format; |
| 800 | break; |
| 801 | } |
| 802 | else if (isalpha(c)) |
| 803 | max++; |
| 804 | else if (c == '|') |
| 805 | min = max; |
| 806 | } |
| 807 | |
| 808 | if (min < 0) |
| 809 | min = max; |
| 810 | |
| 811 | format = formatsave; |
| 812 | |
| 813 | if (!PyTuple_Check(args)) { |
| 814 | PyErr_SetString(PyExc_SystemError, |
| 815 | "new style getargs format but argument is not a tuple"); |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | tplen = PyTuple_Size(args); |
| 820 | |
| 821 | /* do a cursory check of the keywords just to see how many we got */ |
| 822 | |
| 823 | if (keywords) { |
| 824 | if (!PyDict_Check(keywords)) { |
| 825 | PyErr_SetString(PyExc_SystemError, |
| 826 | "non-dictionary object received when keyword dictionary expected"); |
| 827 | return 0; |
| 828 | } |
| 829 | kwlen = PyDict_Size(keywords); |
| 830 | } |
| 831 | else { |
| 832 | kwlen = 0; |
| 833 | } |
| 834 | |
| 835 | /* make sure there are no duplicate values for an argument; |
| 836 | its not clear when to use the term "keyword argument vs. |
| 837 | keyword parameter in messages */ |
| 838 | |
| 839 | if (keywords) { |
| 840 | for (i = 0; i < tplen; i++) { |
| 841 | if (PyMapping_HasKeyString(keywords, kwlist[i])) { |
| 842 | sprintf(msgbuf, |
| 843 | "keyword parameter %s redefined", |
| 844 | kwlist[i]); |
| 845 | PyErr_SetString(PyExc_TypeError, msgbuf); |
| 846 | return 0; |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | PyErr_Clear(); /* I'm not which Py functions set the error string */ |
| 851 | |
| 852 | /* required arguments missing from args can be supplied by keyword |
| 853 | arguments */ |
| 854 | |
| 855 | len = tplen; |
| 856 | if (keywords && tplen < min) { |
| 857 | for (i = tplen; i < min; i++) { |
| 858 | if (PyMapping_HasKeyString(keywords, kwlist[i])) { |
| 859 | len++; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | PyErr_Clear(); |
| 864 | |
| 865 | /* make sure we got an acceptable number of arguments; the message |
| 866 | is a little confusing with keywords since keyword arguments |
| 867 | which are supplied, but don't match the required arguments |
| 868 | are not included in the "%d given" part of the message */ |
| 869 | |
| 870 | if (len < min || max < len) { |
| 871 | if (message == NULL) { |
| 872 | sprintf(msgbuf, |
| 873 | "%s requires %s %d argument%s; %d given", |
| 874 | fname==NULL ? "function" : fname, |
| 875 | min==max ? "exactly" |
| 876 | : len < min ? "at least" : "at most", |
| 877 | len < min ? min : max, |
| 878 | (len < min ? min : max) == 1 ? "" : "s", |
| 879 | len); |
| 880 | message = msgbuf; |
| 881 | } |
| 882 | PyErr_SetString(PyExc_TypeError, message); |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | for (i = 0; i < tplen; i++) { |
| 887 | if (*format == '|') |
| 888 | format++; |
| 889 | msg = convertitem(PyTuple_GetItem(args, i), &format, p_va, |
| 890 | levels, msgbuf); |
| 891 | if (msg) { |
| 892 | seterror(i+1, msg, levels, fname, message); |
| 893 | return 0; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /* handle no keyword parameters in call */ |
| 898 | |
| 899 | if (!keywords) return 1; |
| 900 | |
| 901 | /* make sure the number of keywords in the keyword list matches the |
| 902 | number of items in the format string */ |
| 903 | |
| 904 | nkwds = 0; |
| 905 | p = kwlist; |
| 906 | for (;;) { |
| 907 | if (!*(p++)) break; |
| 908 | nkwds++; |
| 909 | } |
| 910 | |
| 911 | if (nkwds != max) { |
| 912 | PyErr_SetString(PyExc_SystemError, |
| 913 | "number of items in format string and keyword list do not match"); |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | /* convert the keyword arguments; this uses the format |
| 918 | string where it was left after processing args */ |
| 919 | |
| 920 | converted = 0; |
| 921 | for (i = tplen; i < nkwds; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 922 | PyObject *item; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 923 | if (*format == '|') |
| 924 | format++; |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 925 | item = PyMapping_GetItemString(keywords, kwlist[i]); |
| 926 | if (item != NULL) { |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 927 | msg = convertitem(item, &format, p_va, levels, msgbuf); |
| 928 | if (msg) { |
| 929 | seterror(i+1, msg, levels, fname, message); |
| 930 | return 0; |
| 931 | } |
| 932 | converted++; |
| 933 | } |
| 934 | else { |
| 935 | PyErr_Clear(); |
| 936 | msg = skipitem(&format, p_va); |
| 937 | if (msg) { |
| 938 | seterror(i+1, msg, levels, fname, message); |
| 939 | return 0; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | /* make sure there are no extraneous keyword arguments */ |
| 945 | |
| 946 | pos = 0; |
| 947 | if (converted < kwlen) { |
| 948 | while (PyDict_Next(keywords, &pos, &key, &value)) { |
| 949 | match = 0; |
| 950 | ks = PyString_AsString(key); |
| 951 | for (i = 0; i < nkwds; i++) { |
| 952 | if (!strcmp(ks, kwlist[i])) { |
| 953 | match = 1; |
| 954 | break; |
| 955 | } |
| 956 | } |
| 957 | if (!match) { |
| 958 | sprintf(msgbuf, |
| 959 | "%s is an invalid keyword argument for this function", |
| 960 | ks); |
| 961 | PyErr_SetString(PyExc_TypeError, msgbuf); |
| 962 | return 0; |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | return 1; |
| 968 | } |
| 969 | |
| 970 | |
| 971 | static char * |
| 972 | skipitem(p_format, p_va) |
| 973 | char **p_format; |
| 974 | va_list *p_va; |
| 975 | { |
| 976 | char *format = *p_format; |
| 977 | char c = *format++; |
| 978 | |
| 979 | switch (c) { |
| 980 | |
| 981 | case 'b': /* byte -- very short int */ |
| 982 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 983 | (void) va_arg(*p_va, char *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 984 | break; |
| 985 | } |
| 986 | |
| 987 | case 'h': /* short int */ |
| 988 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 989 | (void) va_arg(*p_va, short *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 990 | break; |
| 991 | } |
| 992 | |
| 993 | case 'i': /* int */ |
| 994 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 995 | (void) va_arg(*p_va, int *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 996 | break; |
| 997 | } |
| 998 | |
| 999 | case 'l': /* long int */ |
| 1000 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1001 | (void) va_arg(*p_va, long *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1002 | break; |
| 1003 | } |
| 1004 | |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1005 | #if HAVE_LONG_LONG |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 1006 | case 'L': /* LONG_LONG int */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1007 | { |
Guido van Rossum | 3293b07 | 1998-08-25 16:07:15 +0000 | [diff] [blame] | 1008 | (void) va_arg(*p_va, LONG_LONG *); |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1009 | break; |
| 1010 | } |
| 1011 | #endif |
| 1012 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1013 | case 'f': /* float */ |
| 1014 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1015 | (void) va_arg(*p_va, float *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1016 | break; |
| 1017 | } |
| 1018 | |
| 1019 | case 'd': /* double */ |
| 1020 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1021 | (void) va_arg(*p_va, double *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1022 | break; |
| 1023 | } |
| 1024 | |
| 1025 | #ifndef WITHOUT_COMPLEX |
| 1026 | case 'D': /* complex double */ |
| 1027 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1028 | (void) va_arg(*p_va, Py_complex *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1029 | break; |
| 1030 | } |
| 1031 | #endif /* WITHOUT_COMPLEX */ |
| 1032 | |
| 1033 | case 'c': /* char */ |
| 1034 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1035 | (void) va_arg(*p_va, char *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1036 | break; |
| 1037 | } |
| 1038 | |
| 1039 | case 's': /* string */ |
| 1040 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1041 | (void) va_arg(*p_va, char **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1042 | if (*format == '#') { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1043 | (void) va_arg(*p_va, int *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1044 | format++; |
| 1045 | } |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | case 'z': /* string */ |
| 1050 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1051 | (void) va_arg(*p_va, char **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1052 | if (*format == '#') { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1053 | (void) va_arg(*p_va, int *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1054 | format++; |
| 1055 | } |
| 1056 | break; |
| 1057 | } |
| 1058 | |
| 1059 | case 'S': /* string object */ |
| 1060 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1061 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1062 | break; |
| 1063 | } |
| 1064 | |
| 1065 | case 'O': /* object */ |
| 1066 | { |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1067 | if (*format == '!') { |
| 1068 | format++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1069 | (void) va_arg(*p_va, PyTypeObject*); |
| 1070 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1071 | } |
| 1072 | #if 0 |
| 1073 | /* I don't know what this is for */ |
| 1074 | else if (*format == '?') { |
| 1075 | inquiry pred = va_arg(*p_va, inquiry); |
| 1076 | format++; |
| 1077 | if ((*pred)(arg)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1078 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1079 | } |
| 1080 | } |
| 1081 | #endif |
| 1082 | else if (*format == '&') { |
| 1083 | typedef int (*converter) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1084 | Py_PROTO((PyObject *, void *)); |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1085 | (void) va_arg(*p_va, converter); |
| 1086 | (void) va_arg(*p_va, void *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1087 | format++; |
| 1088 | } |
| 1089 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1090 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1091 | } |
| 1092 | break; |
| 1093 | } |
| 1094 | |
| 1095 | default: |
| 1096 | return "impossible<bad format char>"; |
| 1097 | |
| 1098 | } |
| 1099 | |
| 1100 | *p_format = format; |
| 1101 | return NULL; |
| 1102 | } |