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