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