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