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 | |
| 260 | if (*format != '\0' && !isalpha(*format) && |
| 261 | *format != '|' && *format != ':' && *format != ';') { |
| 262 | PyErr_Format(PyExc_SystemError, |
| 263 | "bad format string: %s", formatsave); |
| 264 | return 0; |
| 265 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 266 | |
| 267 | return 1; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | |
| 272 | static void |
| 273 | seterror(iarg, msg, levels, fname, message) |
| 274 | int iarg; |
| 275 | char *msg; |
| 276 | int *levels; |
| 277 | char *fname; |
| 278 | char *message; |
| 279 | { |
| 280 | char buf[256]; |
| 281 | int i; |
| 282 | char *p = buf; |
| 283 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 284 | if (PyErr_Occurred()) |
Guido van Rossum | 64fc649 | 1995-01-21 14:09:37 +0000 | [diff] [blame] | 285 | return; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 286 | if (iarg == 0 && message == NULL) |
| 287 | message = msg; |
| 288 | else if (message == NULL) { |
| 289 | if (fname != NULL) { |
| 290 | sprintf(p, "%s, ", fname); |
| 291 | p += strlen(p); |
| 292 | } |
| 293 | sprintf(p, "argument %d", iarg); |
| 294 | i = 0; |
| 295 | p += strlen(p); |
| 296 | while (levels[i] > 0) { |
| 297 | sprintf(p, ", item %d", levels[i]-1); |
| 298 | p += strlen(p); |
| 299 | i++; |
| 300 | } |
| 301 | sprintf(p, ": expected %s found", msg); |
| 302 | message = buf; |
| 303 | } |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 304 | PyErr_SetString(PyExc_TypeError, message); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | |
| 308 | /* Convert a tuple argument. |
| 309 | On entry, *p_format points to the character _after_ the opening '('. |
| 310 | On successful exit, *p_format points to the closing ')'. |
| 311 | If successful: |
| 312 | *p_format and *p_va are updated, |
| 313 | *levels and *msgbuf are untouched, |
| 314 | and NULL is returned. |
| 315 | If the argument is invalid: |
| 316 | *p_format is unchanged, |
| 317 | *p_va is undefined, |
| 318 | *levels is a 0-terminated list of item numbers, |
| 319 | *msgbuf contains an error message, whose format is: |
| 320 | "<typename1>, <typename2>", where: |
| 321 | <typename1> is the name of the expected type, and |
| 322 | <typename2> is the name of the actual type, |
| 323 | (so you can surround it by "expected ... found"), |
| 324 | and msgbuf is returned. |
| 325 | */ |
| 326 | |
| 327 | static char * |
| 328 | converttuple(arg, p_format, p_va, levels, msgbuf, toplevel) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 329 | PyObject *arg; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 330 | char **p_format; |
| 331 | va_list *p_va; |
| 332 | int *levels; |
| 333 | char *msgbuf; |
| 334 | int toplevel; |
| 335 | { |
| 336 | int level = 0; |
| 337 | int n = 0; |
| 338 | char *format = *p_format; |
| 339 | int i; |
| 340 | |
| 341 | for (;;) { |
| 342 | int c = *format++; |
| 343 | if (c == '(') { |
| 344 | if (level == 0) |
| 345 | n++; |
| 346 | level++; |
| 347 | } |
| 348 | else if (c == ')') { |
| 349 | if (level == 0) |
| 350 | break; |
| 351 | level--; |
| 352 | } |
| 353 | else if (c == ':' || c == ';' || c == '\0') |
| 354 | break; |
| 355 | else if (level == 0 && isalpha(c)) |
| 356 | n++; |
| 357 | } |
| 358 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 359 | if (!PyTuple_Check(arg)) { |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 360 | levels[0] = 0; |
| 361 | sprintf(msgbuf, |
| 362 | toplevel ? "%d arguments, %s" : "%d-tuple, %s", |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 363 | n, arg == Py_None ? "None" : arg->ob_type->tp_name); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 364 | return msgbuf; |
| 365 | } |
| 366 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 367 | if ((i = PyTuple_Size(arg)) != n) { |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 368 | levels[0] = 0; |
| 369 | sprintf(msgbuf, |
| 370 | toplevel ? "%d arguments, %d" : "%d-tuple, %d-tuple", |
| 371 | n, i); |
| 372 | return msgbuf; |
| 373 | } |
| 374 | |
| 375 | format = *p_format; |
| 376 | for (i = 0; i < n; i++) { |
| 377 | char *msg; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 378 | msg = convertitem(PyTuple_GetItem(arg, i), &format, p_va, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 379 | levels+1, msgbuf); |
| 380 | if (msg != NULL) { |
| 381 | levels[0] = i+1; |
| 382 | return msg; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | *p_format = format; |
| 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /* Convert a single item. */ |
| 392 | |
| 393 | static char * |
| 394 | convertitem(arg, p_format, p_va, levels, msgbuf) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 395 | PyObject *arg; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 396 | char **p_format; |
| 397 | va_list *p_va; |
| 398 | int *levels; |
| 399 | char *msgbuf; |
| 400 | { |
| 401 | char *msg; |
| 402 | char *format = *p_format; |
| 403 | |
| 404 | if (*format == '(' /* ')' */) { |
| 405 | format++; |
| 406 | msg = converttuple(arg, &format, p_va, levels, msgbuf, 0); |
| 407 | if (msg == NULL) |
| 408 | format++; |
| 409 | } |
| 410 | else { |
| 411 | msg = convertsimple(arg, &format, p_va, msgbuf); |
| 412 | if (msg != NULL) |
| 413 | levels[0] = 0; |
| 414 | } |
| 415 | if (msg == NULL) |
| 416 | *p_format = format; |
| 417 | return msg; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | /* Convert a non-tuple argument. Adds to convertsimple1 functionality |
| 422 | by appending ", <actual argument type>" to error message. */ |
| 423 | |
| 424 | static char * |
| 425 | convertsimple(arg, p_format, p_va, msgbuf) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 426 | PyObject *arg; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 427 | char **p_format; |
| 428 | va_list *p_va; |
| 429 | char *msgbuf; |
| 430 | { |
| 431 | char *msg = convertsimple1(arg, p_format, p_va); |
| 432 | if (msg != NULL) { |
| 433 | sprintf(msgbuf, "%.50s, %.50s", msg, |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 434 | arg == Py_None ? "None" : arg->ob_type->tp_name); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 435 | msg = msgbuf; |
| 436 | } |
| 437 | return msg; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | /* Convert a non-tuple argument. Return NULL if conversion went OK, |
| 442 | or a string representing the expected type if the conversion failed. |
| 443 | When failing, an exception may or may not have been raised. |
| 444 | Don't call if a tuple is expected. */ |
| 445 | |
| 446 | static char * |
| 447 | convertsimple1(arg, p_format, p_va) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 448 | PyObject *arg; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 449 | char **p_format; |
| 450 | va_list *p_va; |
| 451 | { |
| 452 | char *format = *p_format; |
| 453 | char c = *format++; |
| 454 | |
| 455 | switch (c) { |
| 456 | |
| 457 | case 'b': /* byte -- very short int */ |
| 458 | { |
| 459 | char *p = va_arg(*p_va, char *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 460 | long ival = PyInt_AsLong(arg); |
| 461 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 462 | return "integer<b>"; |
| 463 | else |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 464 | *p = (char) ival; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 465 | break; |
| 466 | } |
| 467 | |
| 468 | case 'h': /* short int */ |
| 469 | { |
| 470 | short *p = va_arg(*p_va, short *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 471 | long ival = PyInt_AsLong(arg); |
| 472 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 473 | return "integer<h>"; |
| 474 | else |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 475 | *p = (short) ival; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | |
| 479 | case 'i': /* int */ |
| 480 | { |
| 481 | int *p = va_arg(*p_va, int *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 482 | long ival = PyInt_AsLong(arg); |
| 483 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 484 | return "integer<i>"; |
| 485 | else |
| 486 | *p = ival; |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | case 'l': /* long int */ |
| 491 | { |
| 492 | long *p = va_arg(*p_va, long *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 493 | long ival = PyInt_AsLong(arg); |
| 494 | if (ival == -1 && PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 495 | return "integer<l>"; |
| 496 | else |
| 497 | *p = ival; |
| 498 | break; |
| 499 | } |
| 500 | |
| 501 | case 'f': /* float */ |
| 502 | { |
| 503 | float *p = va_arg(*p_va, float *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 504 | double dval = PyFloat_AsDouble(arg); |
| 505 | if (PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 506 | return "float<f>"; |
| 507 | else |
Guido van Rossum | 6bf62da | 1997-04-11 20:37:35 +0000 | [diff] [blame] | 508 | *p = (float) dval; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 509 | break; |
| 510 | } |
| 511 | |
| 512 | case 'd': /* double */ |
| 513 | { |
| 514 | double *p = va_arg(*p_va, double *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 515 | double dval = PyFloat_AsDouble(arg); |
| 516 | if (PyErr_Occurred()) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 517 | return "float<d>"; |
| 518 | else |
| 519 | *p = dval; |
| 520 | break; |
| 521 | } |
| 522 | |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 523 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 524 | case 'D': /* complex double */ |
| 525 | { |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 526 | Py_complex *p = va_arg(*p_va, Py_complex *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 527 | Py_complex cval; |
| 528 | cval = PyComplex_AsCComplex(arg); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 529 | if (PyErr_Occurred()) |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 530 | return "complex<D>"; |
| 531 | else |
| 532 | *p = cval; |
| 533 | break; |
| 534 | } |
Guido van Rossum | 530956d | 1996-07-21 02:27:43 +0000 | [diff] [blame] | 535 | #endif /* WITHOUT_COMPLEX */ |
Guido van Rossum | 8a5c5d2 | 1996-01-12 01:09:56 +0000 | [diff] [blame] | 536 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 537 | case 'c': /* char */ |
| 538 | { |
| 539 | char *p = va_arg(*p_va, char *); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 540 | if (PyString_Check(arg) && PyString_Size(arg) == 1) |
| 541 | *p = PyString_AsString(arg)[0]; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 542 | else |
| 543 | return "char"; |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | case 's': /* string */ |
| 548 | { |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 549 | if (*format == '#') { /* any buffer-like object */ |
| 550 | void **p = (void **)va_arg(*p_va, char **); |
| 551 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 552 | int *q = va_arg(*p_va, int *); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 553 | int count; |
| 554 | |
| 555 | if ( pb == NULL || |
| 556 | pb->bf_getreadbuffer == NULL || |
| 557 | pb->bf_getsegcount == NULL ) |
| 558 | return "read-only buffer"; |
| 559 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
| 560 | return "single-segment read-only buffer"; |
| 561 | if ( (count = |
| 562 | (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 ) |
| 563 | return "(unspecified)"; |
| 564 | *q = count; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 565 | format++; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 566 | } else { |
| 567 | char **p = va_arg(*p_va, char **); |
| 568 | |
| 569 | if (PyString_Check(arg)) |
| 570 | *p = PyString_AsString(arg); |
| 571 | else |
| 572 | return "string"; |
| 573 | if ((int)strlen(*p) != PyString_Size(arg)) |
| 574 | return "string without null bytes"; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 575 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 576 | break; |
| 577 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 578 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 579 | case 'z': /* string, may be NULL (None) */ |
| 580 | { |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 581 | if (*format == '#') { /* any buffer-like object */ |
| 582 | void **p = (void **)va_arg(*p_va, char **); |
| 583 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 584 | int *q = va_arg(*p_va, int *); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 585 | int count; |
| 586 | |
| 587 | if (arg == Py_None) { |
| 588 | *p = 0; |
| 589 | *q = 0; |
| 590 | } else { |
| 591 | if ( pb == NULL || |
| 592 | pb->bf_getreadbuffer == NULL || |
| 593 | pb->bf_getsegcount == NULL ) |
| 594 | return "read-only buffer"; |
| 595 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
| 596 | return "single-segment read-only buffer"; |
| 597 | if ( (count = (*pb->bf_getreadbuffer) |
| 598 | (arg, 0, p)) < 0 ) |
| 599 | return "(unspecified)"; |
| 600 | *q = count; |
| 601 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 602 | format++; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 603 | } else { |
| 604 | char **p = va_arg(*p_va, char **); |
| 605 | |
| 606 | if (arg == Py_None) |
| 607 | *p = 0; |
| 608 | else if (PyString_Check(arg)) |
| 609 | *p = PyString_AsString(arg); |
| 610 | else |
| 611 | return "None or string"; |
| 612 | if (*format == '#') { |
| 613 | int *q = va_arg(*p_va, int *); |
| 614 | if (arg == Py_None) |
| 615 | *q = 0; |
| 616 | else |
| 617 | *q = PyString_Size(arg); |
| 618 | format++; |
| 619 | } |
| 620 | else if (*p != NULL && |
| 621 | (int)strlen(*p) != PyString_Size(arg)) |
| 622 | return "None or string without null bytes"; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 623 | } |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 624 | break; |
| 625 | } |
| 626 | |
| 627 | case 'S': /* string object */ |
| 628 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 629 | PyObject **p = va_arg(*p_va, PyObject **); |
| 630 | if (PyString_Check(arg)) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 631 | *p = arg; |
| 632 | else |
| 633 | return "string"; |
| 634 | break; |
| 635 | } |
| 636 | |
| 637 | case 'O': /* object */ |
| 638 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 639 | PyTypeObject *type; |
| 640 | PyObject **p; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 641 | if (*format == '!') { |
| 642 | format++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 643 | type = va_arg(*p_va, PyTypeObject*); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 644 | if (arg->ob_type != type) |
| 645 | return type->tp_name; |
| 646 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 647 | p = va_arg(*p_va, PyObject **); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 648 | *p = arg; |
| 649 | } |
| 650 | } |
| 651 | else if (*format == '?') { |
| 652 | inquiry pred = va_arg(*p_va, inquiry); |
| 653 | format++; |
| 654 | if ((*pred)(arg)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 655 | p = va_arg(*p_va, PyObject **); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 656 | *p = arg; |
| 657 | } |
| 658 | } |
| 659 | else if (*format == '&') { |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 660 | typedef int (*converter) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 661 | Py_PROTO((PyObject *, void *)); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 662 | converter convert = va_arg(*p_va, converter); |
| 663 | void *addr = va_arg(*p_va, void *); |
| 664 | format++; |
| 665 | if (! (*convert)(arg, addr)) |
Guido van Rossum | 64fc649 | 1995-01-21 14:09:37 +0000 | [diff] [blame] | 666 | return "(unspecified)"; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 667 | } |
| 668 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 669 | p = va_arg(*p_va, PyObject **); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 670 | *p = arg; |
| 671 | } |
| 672 | break; |
| 673 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 674 | |
| 675 | |
| 676 | case 'w': /* memory buffer, read-write access */ |
| 677 | { |
| 678 | void **p = va_arg(*p_va, void **); |
| 679 | PyBufferProcs *pb = arg->ob_type->tp_as_buffer; |
| 680 | int count; |
| 681 | |
| 682 | if ( pb == NULL || pb->bf_getwritebuffer == NULL || |
| 683 | pb->bf_getsegcount == NULL ) |
| 684 | return "read-write buffer"; |
| 685 | if ( (*pb->bf_getsegcount)(arg, NULL) != 1 ) |
| 686 | return "single-segment read-write buffer"; |
| 687 | if ( (count = pb->bf_getwritebuffer(arg, 0, p)) < 0 ) |
| 688 | return "(unspecified)"; |
| 689 | if (*format == '#') { |
| 690 | int *q = va_arg(*p_va, int *); |
| 691 | |
| 692 | *q = count; |
| 693 | format++; |
| 694 | } |
| 695 | break; |
| 696 | } |
| 697 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 698 | |
| 699 | default: |
| 700 | return "impossible<bad format char>"; |
| 701 | |
| 702 | } |
| 703 | |
| 704 | *p_format = format; |
| 705 | return NULL; |
| 706 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 707 | |
| 708 | |
| 709 | /* Support for keyword arguments donated by |
| 710 | Geoff Philbrick <philbric@delphi.hks.com> */ |
| 711 | |
| 712 | #ifdef HAVE_STDARG_PROTOTYPES |
| 713 | /* VARARGS2 */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 714 | int PyArg_ParseTupleAndKeywords(PyObject *args, |
| 715 | PyObject *keywords, |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 716 | char *format, |
| 717 | char **kwlist, ...) |
| 718 | #else |
| 719 | /* VARARGS */ |
| 720 | int PyArg_ParseTupleAndKeywords(va_alist) va_dcl |
| 721 | #endif |
| 722 | { |
| 723 | int retval; |
| 724 | va_list va; |
| 725 | #ifdef HAVE_STDARG_PROTOTYPES |
| 726 | |
| 727 | va_start(va, kwlist); |
| 728 | #else |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 729 | PyObject *args; |
| 730 | PyObject *keywords; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 731 | char *format; |
| 732 | char **kwlist; |
| 733 | |
| 734 | va_start(va); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 735 | args = va_arg(va, PyObject *); |
| 736 | keywords = va_arg(va, PyObject *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 737 | format = va_arg(va, char *); |
| 738 | kwlist = va_arg(va, char **); |
| 739 | #endif |
| 740 | retval = vgetargskeywords(args, keywords, format, kwlist, &va); |
| 741 | va_end(va); |
| 742 | return retval; |
| 743 | } |
| 744 | |
| 745 | |
| 746 | static int |
| 747 | vgetargskeywords(args, keywords, format, kwlist, p_va) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 748 | PyObject *args; |
| 749 | PyObject *keywords; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 750 | char *format; |
| 751 | char **kwlist; |
| 752 | va_list *p_va; |
| 753 | { |
| 754 | char msgbuf[256]; |
| 755 | int levels[32]; |
| 756 | char *fname = NULL; |
| 757 | char *message = NULL; |
| 758 | int min = -1; |
| 759 | int max = 0; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 760 | char *formatsave = format; |
| 761 | int i, len, tplen, kwlen; |
| 762 | char *msg, *ks, **p; |
| 763 | int nkwds, pos, match, converted; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 764 | PyObject *key, *value; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 765 | |
| 766 | /* nested tuples cannot be parsed when using keyword arguments */ |
| 767 | |
| 768 | for (;;) { |
| 769 | int c = *format++; |
| 770 | if (c == '(') { |
| 771 | PyErr_SetString(PyExc_SystemError, |
| 772 | "tuple found in format when using keyword arguments"); |
| 773 | return 0; |
| 774 | } |
| 775 | else if (c == '\0') |
| 776 | break; |
| 777 | else if (c == ':') { |
| 778 | fname = format; |
| 779 | break; |
| 780 | } |
| 781 | else if (c == ';') { |
| 782 | message = format; |
| 783 | break; |
| 784 | } |
| 785 | else if (isalpha(c)) |
| 786 | max++; |
| 787 | else if (c == '|') |
| 788 | min = max; |
| 789 | } |
| 790 | |
| 791 | if (min < 0) |
| 792 | min = max; |
| 793 | |
| 794 | format = formatsave; |
| 795 | |
| 796 | if (!PyTuple_Check(args)) { |
| 797 | PyErr_SetString(PyExc_SystemError, |
| 798 | "new style getargs format but argument is not a tuple"); |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | tplen = PyTuple_Size(args); |
| 803 | |
| 804 | /* do a cursory check of the keywords just to see how many we got */ |
| 805 | |
| 806 | if (keywords) { |
| 807 | if (!PyDict_Check(keywords)) { |
| 808 | PyErr_SetString(PyExc_SystemError, |
| 809 | "non-dictionary object received when keyword dictionary expected"); |
| 810 | return 0; |
| 811 | } |
| 812 | kwlen = PyDict_Size(keywords); |
| 813 | } |
| 814 | else { |
| 815 | kwlen = 0; |
| 816 | } |
| 817 | |
| 818 | /* make sure there are no duplicate values for an argument; |
| 819 | its not clear when to use the term "keyword argument vs. |
| 820 | keyword parameter in messages */ |
| 821 | |
| 822 | if (keywords) { |
| 823 | for (i = 0; i < tplen; i++) { |
| 824 | if (PyMapping_HasKeyString(keywords, kwlist[i])) { |
| 825 | sprintf(msgbuf, |
| 826 | "keyword parameter %s redefined", |
| 827 | kwlist[i]); |
| 828 | PyErr_SetString(PyExc_TypeError, msgbuf); |
| 829 | return 0; |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | PyErr_Clear(); /* I'm not which Py functions set the error string */ |
| 834 | |
| 835 | /* required arguments missing from args can be supplied by keyword |
| 836 | arguments */ |
| 837 | |
| 838 | len = tplen; |
| 839 | if (keywords && tplen < min) { |
| 840 | for (i = tplen; i < min; i++) { |
| 841 | if (PyMapping_HasKeyString(keywords, kwlist[i])) { |
| 842 | len++; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | PyErr_Clear(); |
| 847 | |
| 848 | /* make sure we got an acceptable number of arguments; the message |
| 849 | is a little confusing with keywords since keyword arguments |
| 850 | which are supplied, but don't match the required arguments |
| 851 | are not included in the "%d given" part of the message */ |
| 852 | |
| 853 | if (len < min || max < len) { |
| 854 | if (message == NULL) { |
| 855 | sprintf(msgbuf, |
| 856 | "%s requires %s %d argument%s; %d given", |
| 857 | fname==NULL ? "function" : fname, |
| 858 | min==max ? "exactly" |
| 859 | : len < min ? "at least" : "at most", |
| 860 | len < min ? min : max, |
| 861 | (len < min ? min : max) == 1 ? "" : "s", |
| 862 | len); |
| 863 | message = msgbuf; |
| 864 | } |
| 865 | PyErr_SetString(PyExc_TypeError, message); |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | for (i = 0; i < tplen; i++) { |
| 870 | if (*format == '|') |
| 871 | format++; |
| 872 | msg = convertitem(PyTuple_GetItem(args, i), &format, p_va, |
| 873 | levels, msgbuf); |
| 874 | if (msg) { |
| 875 | seterror(i+1, msg, levels, fname, message); |
| 876 | return 0; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /* handle no keyword parameters in call */ |
| 881 | |
| 882 | if (!keywords) return 1; |
| 883 | |
| 884 | /* make sure the number of keywords in the keyword list matches the |
| 885 | number of items in the format string */ |
| 886 | |
| 887 | nkwds = 0; |
| 888 | p = kwlist; |
| 889 | for (;;) { |
| 890 | if (!*(p++)) break; |
| 891 | nkwds++; |
| 892 | } |
| 893 | |
| 894 | if (nkwds != max) { |
| 895 | PyErr_SetString(PyExc_SystemError, |
| 896 | "number of items in format string and keyword list do not match"); |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | /* convert the keyword arguments; this uses the format |
| 901 | string where it was left after processing args */ |
| 902 | |
| 903 | converted = 0; |
| 904 | for (i = tplen; i < nkwds; i++) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 905 | PyObject *item; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 906 | if (*format == '|') |
| 907 | format++; |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 908 | item = PyMapping_GetItemString(keywords, kwlist[i]); |
| 909 | if (item != NULL) { |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 910 | msg = convertitem(item, &format, p_va, levels, msgbuf); |
| 911 | if (msg) { |
| 912 | seterror(i+1, msg, levels, fname, message); |
| 913 | return 0; |
| 914 | } |
| 915 | converted++; |
| 916 | } |
| 917 | else { |
| 918 | PyErr_Clear(); |
| 919 | msg = skipitem(&format, p_va); |
| 920 | if (msg) { |
| 921 | seterror(i+1, msg, levels, fname, message); |
| 922 | return 0; |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | /* make sure there are no extraneous keyword arguments */ |
| 928 | |
| 929 | pos = 0; |
| 930 | if (converted < kwlen) { |
| 931 | while (PyDict_Next(keywords, &pos, &key, &value)) { |
| 932 | match = 0; |
| 933 | ks = PyString_AsString(key); |
| 934 | for (i = 0; i < nkwds; i++) { |
| 935 | if (!strcmp(ks, kwlist[i])) { |
| 936 | match = 1; |
| 937 | break; |
| 938 | } |
| 939 | } |
| 940 | if (!match) { |
| 941 | sprintf(msgbuf, |
| 942 | "%s is an invalid keyword argument for this function", |
| 943 | ks); |
| 944 | PyErr_SetString(PyExc_TypeError, msgbuf); |
| 945 | return 0; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | return 1; |
| 951 | } |
| 952 | |
| 953 | |
| 954 | static char * |
| 955 | skipitem(p_format, p_va) |
| 956 | char **p_format; |
| 957 | va_list *p_va; |
| 958 | { |
| 959 | char *format = *p_format; |
| 960 | char c = *format++; |
| 961 | |
| 962 | switch (c) { |
| 963 | |
| 964 | case 'b': /* byte -- very short int */ |
| 965 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 966 | (void) va_arg(*p_va, char *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 967 | break; |
| 968 | } |
| 969 | |
| 970 | case 'h': /* short int */ |
| 971 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 972 | (void) va_arg(*p_va, short *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 973 | break; |
| 974 | } |
| 975 | |
| 976 | case 'i': /* int */ |
| 977 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 978 | (void) va_arg(*p_va, int *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 979 | break; |
| 980 | } |
| 981 | |
| 982 | case 'l': /* long int */ |
| 983 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 984 | (void) va_arg(*p_va, long *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 985 | break; |
| 986 | } |
| 987 | |
| 988 | case 'f': /* float */ |
| 989 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 990 | (void) va_arg(*p_va, float *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 991 | break; |
| 992 | } |
| 993 | |
| 994 | case 'd': /* double */ |
| 995 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 996 | (void) va_arg(*p_va, double *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 997 | break; |
| 998 | } |
| 999 | |
| 1000 | #ifndef WITHOUT_COMPLEX |
| 1001 | case 'D': /* complex double */ |
| 1002 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1003 | (void) va_arg(*p_va, Py_complex *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1004 | break; |
| 1005 | } |
| 1006 | #endif /* WITHOUT_COMPLEX */ |
| 1007 | |
| 1008 | case 'c': /* char */ |
| 1009 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1010 | (void) va_arg(*p_va, char *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1011 | break; |
| 1012 | } |
| 1013 | |
| 1014 | case 's': /* string */ |
| 1015 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1016 | (void) va_arg(*p_va, char **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1017 | if (*format == '#') { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1018 | (void) va_arg(*p_va, int *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1019 | format++; |
| 1020 | } |
| 1021 | break; |
| 1022 | } |
| 1023 | |
| 1024 | case 'z': /* string */ |
| 1025 | { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1026 | (void) va_arg(*p_va, char **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1027 | if (*format == '#') { |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1028 | (void) va_arg(*p_va, int *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1029 | format++; |
| 1030 | } |
| 1031 | break; |
| 1032 | } |
| 1033 | |
| 1034 | case 'S': /* string object */ |
| 1035 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1036 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1037 | break; |
| 1038 | } |
| 1039 | |
| 1040 | case 'O': /* object */ |
| 1041 | { |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1042 | if (*format == '!') { |
| 1043 | format++; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1044 | (void) va_arg(*p_va, PyTypeObject*); |
| 1045 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1046 | } |
| 1047 | #if 0 |
| 1048 | /* I don't know what this is for */ |
| 1049 | else if (*format == '?') { |
| 1050 | inquiry pred = va_arg(*p_va, inquiry); |
| 1051 | format++; |
| 1052 | if ((*pred)(arg)) { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1053 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | #endif |
| 1057 | else if (*format == '&') { |
| 1058 | typedef int (*converter) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1059 | Py_PROTO((PyObject *, void *)); |
Guido van Rossum | 80bb965 | 1996-12-05 23:27:02 +0000 | [diff] [blame] | 1060 | (void) va_arg(*p_va, converter); |
| 1061 | (void) va_arg(*p_va, void *); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1062 | format++; |
| 1063 | } |
| 1064 | else { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 1065 | (void) va_arg(*p_va, PyObject **); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1066 | } |
| 1067 | break; |
| 1068 | } |
| 1069 | |
| 1070 | default: |
| 1071 | return "impossible<bad format char>"; |
| 1072 | |
| 1073 | } |
| 1074 | |
| 1075 | *p_format = format; |
| 1076 | return NULL; |
| 1077 | } |