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 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9 | #ifdef __cplusplus |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 10 | extern "C" { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11 | #endif |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 12 | int PyArg_Parse(PyObject *, const char *, ...); |
| 13 | int PyArg_ParseTuple(PyObject *, const char *, ...); |
| 14 | int PyArg_VaParse(PyObject *, const char *, va_list); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 15 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 16 | int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | const char *, char **, ...); |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 18 | int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | const char *, char **, va_list); |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 21 | #ifdef HAVE_DECLSPEC_DLL |
| 22 | /* Export functions */ |
| 23 | PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, char *, ...); |
| 24 | PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, char *, ...); |
| 25 | PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *, |
| 26 | const char *, char **, ...); |
| 27 | PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...); |
| 28 | PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, char *, va_list); |
| 29 | PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *, |
| 30 | const char *, char **, va_list); |
| 31 | #endif |
| 32 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 33 | #define FLAG_COMPAT 1 |
| 34 | #define FLAG_SIZE_T 2 |
| 35 | |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 36 | typedef int (*destr_t)(PyObject *, void *); |
| 37 | |
| 38 | |
| 39 | /* Keep track of "objects" that have been allocated or initialized and |
| 40 | which will need to be deallocated or cleaned up somehow if overall |
| 41 | parsing fails. |
| 42 | */ |
| 43 | typedef struct { |
| 44 | void *item; |
| 45 | destr_t destructor; |
| 46 | } freelistentry_t; |
| 47 | |
| 48 | typedef struct { |
| 49 | int first_available; |
| 50 | freelistentry_t *entries; |
| 51 | } freelist_t; |
| 52 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 53 | |
| 54 | /* Forward */ |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 55 | static int vgetargs1(PyObject *, const char *, va_list *, int); |
| 56 | static void seterror(int, const char *, int *, const char *, const char *); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 57 | static char *convertitem(PyObject *, const char **, va_list *, int, int *, |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 58 | char *, size_t, freelist_t *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 59 | static char *converttuple(PyObject *, const char **, va_list *, int, |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 60 | int *, char *, size_t, int, freelist_t *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 61 | static char *convertsimple(PyObject *, const char **, va_list *, int, char *, |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 62 | size_t, freelist_t *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 63 | static Py_ssize_t convertbuffer(PyObject *, void **p, char **); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 64 | static int getbuffer(PyObject *, Py_buffer *, char**); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 65 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 66 | static int vgetargskeywords(PyObject *, PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | const char *, char **, va_list *, int); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 68 | static char *skipitem(const char **, va_list *, int); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 69 | |
Fred Drake | 563dfc2 | 2001-10-23 14:41:08 +0000 | [diff] [blame] | 70 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 71 | PyArg_Parse(PyObject *args, const char *format, ...) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 72 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | int retval; |
| 74 | va_list va; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 75 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | va_start(va, format); |
| 77 | retval = vgetargs1(args, format, &va, FLAG_COMPAT); |
| 78 | va_end(va); |
| 79 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | int |
| 83 | _PyArg_Parse_SizeT(PyObject *args, char *format, ...) |
| 84 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | int retval; |
| 86 | va_list va; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 87 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | va_start(va, format); |
| 89 | retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T); |
| 90 | va_end(va); |
| 91 | return retval; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
Fred Drake | 563dfc2 | 2001-10-23 14:41:08 +0000 | [diff] [blame] | 95 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 96 | PyArg_ParseTuple(PyObject *args, const char *format, ...) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 97 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | int retval; |
| 99 | va_list va; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | va_start(va, format); |
| 102 | retval = vgetargs1(args, format, &va, 0); |
| 103 | va_end(va); |
| 104 | return retval; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 107 | int |
| 108 | _PyArg_ParseTuple_SizeT(PyObject *args, char *format, ...) |
| 109 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | int retval; |
| 111 | va_list va; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | va_start(va, format); |
| 114 | retval = vgetargs1(args, format, &va, FLAG_SIZE_T); |
| 115 | va_end(va); |
| 116 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 119 | |
| 120 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 121 | PyArg_VaParse(PyObject *args, const char *format, va_list va) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 122 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | va_list lva; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 124 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 125 | Py_VA_COPY(lva, va); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | return vgetargs1(args, format, &lva, 0); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 130 | int |
| 131 | _PyArg_VaParse_SizeT(PyObject *args, char *format, va_list va) |
| 132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | va_list lva; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 134 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 135 | Py_VA_COPY(lva, va); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | return vgetargs1(args, format, &lva, FLAG_SIZE_T); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 140 | |
Martin v. Löwis | e6bbb4d | 2003-05-03 10:00:22 +0000 | [diff] [blame] | 141 | /* Handle cleanup of allocated memory in case of exception */ |
| 142 | |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 143 | static int |
| 144 | cleanup_ptr(PyObject *self, void *ptr) |
Antoine Pitrou | f7199578 | 2008-08-29 18:37:05 +0000 | [diff] [blame] | 145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | if (ptr) { |
| 147 | PyMem_FREE(ptr); |
| 148 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | return 0; |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Martin v. Löwis | e6bbb4d | 2003-05-03 10:00:22 +0000 | [diff] [blame] | 152 | static int |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 153 | cleanup_buffer(PyObject *self, void *ptr) |
Martin v. Löwis | e6bbb4d | 2003-05-03 10:00:22 +0000 | [diff] [blame] | 154 | { |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 155 | Py_buffer *buf = (Py_buffer *)ptr; |
| 156 | if (buf) { |
| 157 | PyBuffer_Release(buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int |
| 163 | addcleanup(void *ptr, freelist_t *freelist, destr_t destructor) |
| 164 | { |
| 165 | int index; |
| 166 | |
| 167 | index = freelist->first_available; |
| 168 | freelist->first_available += 1; |
| 169 | |
| 170 | freelist->entries[index].item = ptr; |
| 171 | freelist->entries[index].destructor = destructor; |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int |
| 177 | cleanreturn(int retval, freelist_t *freelist) |
| 178 | { |
| 179 | int index; |
| 180 | |
| 181 | if (retval == 0) { |
| 182 | /* A failure occurred, therefore execute all of the cleanup |
| 183 | functions. |
| 184 | */ |
| 185 | for (index = 0; index < freelist->first_available; ++index) { |
| 186 | freelist->entries[index].destructor(NULL, |
| 187 | freelist->entries[index].item); |
| 188 | } |
| 189 | } |
Benjamin Peterson | f7c1321 | 2012-03-16 12:23:39 -0500 | [diff] [blame] | 190 | PyMem_FREE(freelist->entries); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | return retval; |
Martin v. Löwis | e6bbb4d | 2003-05-03 10:00:22 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 195 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 196 | vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 197 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 198 | char msgbuf[256]; |
| 199 | int levels[32]; |
| 200 | const char *fname = NULL; |
| 201 | const char *message = NULL; |
| 202 | int min = -1; |
| 203 | int max = 0; |
| 204 | int level = 0; |
| 205 | int endfmt = 0; |
| 206 | const char *formatsave = format; |
| 207 | Py_ssize_t i, len; |
| 208 | char *msg; |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 209 | freelist_t freelist = {0, NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | int compat = flags & FLAG_COMPAT; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | assert(compat || (args != (PyObject*)NULL)); |
| 213 | flags = flags & ~FLAG_COMPAT; |
Tim Peters | 5c4d5bf | 2001-02-12 22:13:26 +0000 | [diff] [blame] | 214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | while (endfmt == 0) { |
| 216 | int c = *format++; |
| 217 | switch (c) { |
| 218 | case '(': |
| 219 | if (level == 0) |
| 220 | max++; |
| 221 | level++; |
| 222 | if (level >= 30) |
| 223 | Py_FatalError("too many tuple nesting levels " |
| 224 | "in argument format string"); |
| 225 | break; |
| 226 | case ')': |
| 227 | if (level == 0) |
| 228 | Py_FatalError("excess ')' in getargs format"); |
| 229 | else |
| 230 | level--; |
| 231 | break; |
| 232 | case '\0': |
| 233 | endfmt = 1; |
| 234 | break; |
| 235 | case ':': |
| 236 | fname = format; |
| 237 | endfmt = 1; |
| 238 | break; |
| 239 | case ';': |
| 240 | message = format; |
| 241 | endfmt = 1; |
| 242 | break; |
| 243 | default: |
| 244 | if (level == 0) { |
| 245 | if (c == 'O') |
| 246 | max++; |
| 247 | else if (isalpha(Py_CHARMASK(c))) { |
| 248 | if (c != 'e') /* skip encoded */ |
| 249 | max++; |
| 250 | } else if (c == '|') |
| 251 | min = max; |
| 252 | } |
| 253 | break; |
| 254 | } |
| 255 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | if (level != 0) |
| 258 | Py_FatalError(/* '(' */ "missing ')' in getargs format"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | if (min < 0) |
| 261 | min = max; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 262 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | format = formatsave; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 264 | |
Benjamin Peterson | f7c1321 | 2012-03-16 12:23:39 -0500 | [diff] [blame] | 265 | freelist.entries = PyMem_NEW(freelistentry_t, max); |
Benjamin Peterson | 7ed6727 | 2012-03-16 12:21:02 -0500 | [diff] [blame] | 266 | if (freelist.entries == NULL) { |
| 267 | PyErr_NoMemory(); |
| 268 | return 0; |
| 269 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 270 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | if (compat) { |
| 272 | if (max == 0) { |
| 273 | if (args == NULL) |
| 274 | return 1; |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 275 | PyErr_Format(PyExc_TypeError, |
| 276 | "%.200s%s takes no arguments", |
| 277 | fname==NULL ? "function" : fname, |
| 278 | fname==NULL ? "" : "()"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 279 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | } |
| 281 | else if (min == 1 && max == 1) { |
| 282 | if (args == NULL) { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 283 | PyErr_Format(PyExc_TypeError, |
| 284 | "%.200s%s takes at least one argument", |
| 285 | fname==NULL ? "function" : fname, |
| 286 | fname==NULL ? "" : "()"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 287 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | } |
| 289 | msg = convertitem(args, &format, p_va, flags, levels, |
| 290 | msgbuf, sizeof(msgbuf), &freelist); |
| 291 | if (msg == NULL) |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 292 | return cleanreturn(1, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | seterror(levels[0], msg, levels+1, fname, message); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 294 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | } |
| 296 | else { |
| 297 | PyErr_SetString(PyExc_SystemError, |
| 298 | "old style getargs format uses new features"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 299 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | } |
| 301 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | if (!PyTuple_Check(args)) { |
| 304 | PyErr_SetString(PyExc_SystemError, |
| 305 | "new style getargs format but argument is not a tuple"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 306 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 308 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | len = PyTuple_GET_SIZE(args); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 311 | if (len < min || max < len) { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 312 | if (message == NULL) |
| 313 | PyErr_Format(PyExc_TypeError, |
| 314 | "%.150s%s takes %s %d argument%s (%ld given)", |
| 315 | fname==NULL ? "function" : fname, |
| 316 | fname==NULL ? "" : "()", |
| 317 | min==max ? "exactly" |
| 318 | : len < min ? "at least" : "at most", |
| 319 | len < min ? min : max, |
| 320 | (len < min ? min : max) == 1 ? "" : "s", |
| 321 | Py_SAFE_DOWNCAST(len, Py_ssize_t, long)); |
| 322 | else |
| 323 | PyErr_SetString(PyExc_TypeError, message); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 324 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | for (i = 0; i < len; i++) { |
| 328 | if (*format == '|') |
| 329 | format++; |
| 330 | msg = convertitem(PyTuple_GET_ITEM(args, i), &format, p_va, |
| 331 | flags, levels, msgbuf, |
| 332 | sizeof(msgbuf), &freelist); |
| 333 | if (msg) { |
| 334 | seterror(i+1, msg, levels, fname, msg); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 335 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
Guido van Rossum | 231a41e | 1997-12-09 20:36:39 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | if (*format != '\0' && !isalpha(Py_CHARMASK(*format)) && |
| 340 | *format != '(' && |
| 341 | *format != '|' && *format != ':' && *format != ';') { |
| 342 | PyErr_Format(PyExc_SystemError, |
| 343 | "bad format string: %.200s", formatsave); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 344 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 346 | |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 347 | return cleanreturn(1, &freelist); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | |
| 351 | |
| 352 | static void |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 353 | seterror(int iarg, const char *msg, int *levels, const char *fname, |
| 354 | const char *message) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | char buf[512]; |
| 357 | int i; |
| 358 | char *p = buf; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | if (PyErr_Occurred()) |
| 361 | return; |
| 362 | else if (message == NULL) { |
| 363 | if (fname != NULL) { |
| 364 | PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname); |
| 365 | p += strlen(p); |
| 366 | } |
| 367 | if (iarg != 0) { |
| 368 | PyOS_snprintf(p, sizeof(buf) - (p - buf), |
| 369 | "argument %d", iarg); |
| 370 | i = 0; |
| 371 | p += strlen(p); |
| 372 | while (levels[i] > 0 && i < 32 && (int)(p-buf) < 220) { |
| 373 | PyOS_snprintf(p, sizeof(buf) - (p - buf), |
| 374 | ", item %d", levels[i]-1); |
| 375 | p += strlen(p); |
| 376 | i++; |
| 377 | } |
| 378 | } |
| 379 | else { |
| 380 | PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument"); |
| 381 | p += strlen(p); |
| 382 | } |
| 383 | PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg); |
| 384 | message = buf; |
| 385 | } |
| 386 | PyErr_SetString(PyExc_TypeError, message); |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | |
| 390 | /* Convert a tuple argument. |
| 391 | On entry, *p_format points to the character _after_ the opening '('. |
| 392 | On successful exit, *p_format points to the closing ')'. |
| 393 | If successful: |
| 394 | *p_format and *p_va are updated, |
| 395 | *levels and *msgbuf are untouched, |
| 396 | and NULL is returned. |
| 397 | If the argument is invalid: |
| 398 | *p_format is unchanged, |
| 399 | *p_va is undefined, |
| 400 | *levels is a 0-terminated list of item numbers, |
| 401 | *msgbuf contains an error message, whose format is: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | "must be <typename1>, not <typename2>", where: |
| 403 | <typename1> is the name of the expected type, and |
| 404 | <typename2> is the name of the actual type, |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 405 | and msgbuf is returned. |
| 406 | */ |
| 407 | |
| 408 | static char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 409 | converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 410 | int *levels, char *msgbuf, size_t bufsize, int toplevel, |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 411 | freelist_t *freelist) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 412 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | int level = 0; |
| 414 | int n = 0; |
| 415 | const char *format = *p_format; |
| 416 | int i; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | for (;;) { |
| 419 | int c = *format++; |
| 420 | if (c == '(') { |
| 421 | if (level == 0) |
| 422 | n++; |
| 423 | level++; |
| 424 | } |
| 425 | else if (c == ')') { |
| 426 | if (level == 0) |
| 427 | break; |
| 428 | level--; |
| 429 | } |
| 430 | else if (c == ':' || c == ';' || c == '\0') |
| 431 | break; |
| 432 | else if (level == 0 && isalpha(Py_CHARMASK(c))) |
| 433 | n++; |
| 434 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | if (!PySequence_Check(arg) || PyBytes_Check(arg)) { |
| 437 | levels[0] = 0; |
| 438 | PyOS_snprintf(msgbuf, bufsize, |
| 439 | toplevel ? "expected %d arguments, not %.50s" : |
| 440 | "must be %d-item sequence, not %.50s", |
| 441 | n, |
| 442 | arg == Py_None ? "None" : arg->ob_type->tp_name); |
| 443 | return msgbuf; |
| 444 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | if ((i = PySequence_Size(arg)) != n) { |
| 447 | levels[0] = 0; |
| 448 | PyOS_snprintf(msgbuf, bufsize, |
| 449 | toplevel ? "expected %d arguments, not %d" : |
| 450 | "must be sequence of length %d, not %d", |
| 451 | n, i); |
| 452 | return msgbuf; |
| 453 | } |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | format = *p_format; |
| 456 | for (i = 0; i < n; i++) { |
| 457 | char *msg; |
| 458 | PyObject *item; |
| 459 | item = PySequence_GetItem(arg, i); |
| 460 | if (item == NULL) { |
| 461 | PyErr_Clear(); |
| 462 | levels[0] = i+1; |
| 463 | levels[1] = 0; |
| 464 | strncpy(msgbuf, "is not retrievable", bufsize); |
| 465 | return msgbuf; |
| 466 | } |
| 467 | msg = convertitem(item, &format, p_va, flags, levels+1, |
| 468 | msgbuf, bufsize, freelist); |
| 469 | /* PySequence_GetItem calls tp->sq_item, which INCREFs */ |
| 470 | Py_XDECREF(item); |
| 471 | if (msg != NULL) { |
| 472 | levels[0] = i+1; |
| 473 | return msg; |
| 474 | } |
| 475 | } |
Ka-Ping Yee | 2057970 | 2001-01-15 22:14:16 +0000 | [diff] [blame] | 476 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | *p_format = format; |
| 478 | return NULL; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | |
| 482 | /* Convert a single item. */ |
| 483 | |
| 484 | static char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 485 | convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 486 | int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | char *msg; |
| 489 | const char *format = *p_format; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | if (*format == '(' /* ')' */) { |
| 492 | format++; |
| 493 | msg = converttuple(arg, &format, p_va, flags, levels, msgbuf, |
| 494 | bufsize, 0, freelist); |
| 495 | if (msg == NULL) |
| 496 | format++; |
| 497 | } |
| 498 | else { |
| 499 | msg = convertsimple(arg, &format, p_va, flags, |
| 500 | msgbuf, bufsize, freelist); |
| 501 | if (msg != NULL) |
| 502 | levels[0] = 0; |
| 503 | } |
| 504 | if (msg == NULL) |
| 505 | *p_format = format; |
| 506 | return msg; |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 510 | |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 511 | /* Format an error message generated by convertsimple(). */ |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 512 | |
| 513 | static char * |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 514 | converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize) |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 515 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | assert(expected != NULL); |
| 517 | assert(arg != NULL); |
| 518 | PyOS_snprintf(msgbuf, bufsize, |
| 519 | "must be %.50s, not %.50s", expected, |
| 520 | arg == Py_None ? "None" : arg->ob_type->tp_name); |
| 521 | return msgbuf; |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | #define CONV_UNICODE "(unicode conversion error)" |
| 525 | |
Guido van Rossum | 45aecf4 | 2006-03-15 04:58:47 +0000 | [diff] [blame] | 526 | /* Explicitly check for float arguments when integers are expected. |
| 527 | Return 1 for error, 0 if ok. */ |
Neil Schemenauer | 5042da6 | 2003-02-04 20:59:40 +0000 | [diff] [blame] | 528 | static int |
| 529 | float_argument_error(PyObject *arg) |
| 530 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | if (PyFloat_Check(arg)) { |
| 532 | PyErr_SetString(PyExc_TypeError, |
| 533 | "integer argument expected, got float" ); |
| 534 | return 1; |
| 535 | } |
| 536 | else |
| 537 | return 0; |
Neil Schemenauer | 5042da6 | 2003-02-04 20:59:40 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 540 | /* Convert a non-tuple argument. Return NULL if conversion went OK, |
| 541 | or a string with a message describing the failure. The message is |
| 542 | formatted as "must be <desired type>, not <actual type>". |
| 543 | When failing, an exception may or may not have been raised. |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 544 | Don't call if a tuple is expected. |
| 545 | |
| 546 | 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] | 547 | */ |
| 548 | |
| 549 | static char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 550 | convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 551 | char *msgbuf, size_t bufsize, freelist_t *freelist) |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 552 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | /* For # codes */ |
| 554 | #define FETCH_SIZE int *q=NULL;Py_ssize_t *q2=NULL;\ |
| 555 | if (flags & FLAG_SIZE_T) q2=va_arg(*p_va, Py_ssize_t*); \ |
| 556 | else q=va_arg(*p_va, int*); |
Victor Stinner | b3c9e07 | 2011-01-04 02:07:34 +0000 | [diff] [blame] | 557 | #define STORE_SIZE(s) \ |
| 558 | if (flags & FLAG_SIZE_T) \ |
| 559 | *q2=s; \ |
| 560 | else { \ |
| 561 | if (INT_MAX < s) { \ |
| 562 | PyErr_SetString(PyExc_OverflowError, \ |
| 563 | "size does not fit in an int"); \ |
| 564 | return converterr("", arg, msgbuf, bufsize); \ |
| 565 | } \ |
| 566 | *q=s; \ |
| 567 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 568 | #define BUFFER_LEN ((flags & FLAG_SIZE_T) ? *q2:*q) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 569 | #define RETURN_ERR_OCCURRED return msgbuf |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 570 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | const char *format = *p_format; |
| 572 | char c = *format++; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 573 | char *sarg; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | switch (c) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | case 'b': { /* unsigned byte -- very short int */ |
| 578 | char *p = va_arg(*p_va, char *); |
| 579 | long ival; |
| 580 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 581 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 582 | ival = PyLong_AsLong(arg); |
| 583 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 584 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | else if (ival < 0) { |
| 586 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 587 | "unsigned byte integer is less than minimum"); |
| 588 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | } |
| 590 | else if (ival > UCHAR_MAX) { |
| 591 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 592 | "unsigned byte integer is greater than maximum"); |
| 593 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | } |
| 595 | else |
| 596 | *p = (unsigned char) ival; |
| 597 | break; |
| 598 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 599 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | case 'B': {/* byte sized bitfield - both signed and unsigned |
| 601 | values allowed */ |
| 602 | char *p = va_arg(*p_va, char *); |
| 603 | long ival; |
| 604 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 605 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | ival = PyLong_AsUnsignedLongMask(arg); |
| 607 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 608 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | else |
| 610 | *p = (unsigned char) ival; |
| 611 | break; |
| 612 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | case 'h': {/* signed short int */ |
| 615 | short *p = va_arg(*p_va, short *); |
| 616 | long ival; |
| 617 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 618 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | ival = PyLong_AsLong(arg); |
| 620 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 621 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | else if (ival < SHRT_MIN) { |
| 623 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 624 | "signed short integer is less than minimum"); |
| 625 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | } |
| 627 | else if (ival > SHRT_MAX) { |
| 628 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 629 | "signed short integer is greater than maximum"); |
| 630 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | } |
| 632 | else |
| 633 | *p = (short) ival; |
| 634 | break; |
| 635 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | case 'H': { /* short int sized bitfield, both signed and |
| 638 | unsigned allowed */ |
| 639 | unsigned short *p = va_arg(*p_va, unsigned short *); |
| 640 | long ival; |
| 641 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 642 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | ival = PyLong_AsUnsignedLongMask(arg); |
| 644 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 645 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | else |
| 647 | *p = (unsigned short) ival; |
| 648 | break; |
| 649 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | case 'i': {/* signed int */ |
| 652 | int *p = va_arg(*p_va, int *); |
| 653 | long ival; |
| 654 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 655 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | ival = PyLong_AsLong(arg); |
| 657 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 658 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | else if (ival > INT_MAX) { |
| 660 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 661 | "signed integer is greater than maximum"); |
| 662 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 663 | } |
| 664 | else if (ival < INT_MIN) { |
| 665 | PyErr_SetString(PyExc_OverflowError, |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 666 | "signed integer is less than minimum"); |
| 667 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 668 | } |
| 669 | else |
| 670 | *p = ival; |
| 671 | break; |
| 672 | } |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | case 'I': { /* int sized bitfield, both signed and |
| 675 | unsigned allowed */ |
| 676 | unsigned int *p = va_arg(*p_va, unsigned int *); |
| 677 | unsigned int ival; |
| 678 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 679 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | ival = (unsigned int)PyLong_AsUnsignedLongMask(arg); |
| 681 | if (ival == (unsigned int)-1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 682 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | else |
| 684 | *p = ival; |
| 685 | break; |
| 686 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | case 'n': /* Py_ssize_t */ |
| 689 | { |
| 690 | PyObject *iobj; |
| 691 | Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *); |
| 692 | Py_ssize_t ival = -1; |
| 693 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 694 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 695 | iobj = PyNumber_Index(arg); |
| 696 | if (iobj != NULL) { |
| 697 | ival = PyLong_AsSsize_t(iobj); |
| 698 | Py_DECREF(iobj); |
| 699 | } |
| 700 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 701 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | *p = ival; |
| 703 | break; |
| 704 | } |
| 705 | case 'l': {/* long int */ |
| 706 | long *p = va_arg(*p_va, long *); |
| 707 | long ival; |
| 708 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 709 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 710 | ival = PyLong_AsLong(arg); |
| 711 | if (ival == -1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 712 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | else |
| 714 | *p = ival; |
| 715 | break; |
| 716 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 718 | case 'k': { /* long sized bitfield */ |
| 719 | unsigned long *p = va_arg(*p_va, unsigned long *); |
| 720 | unsigned long ival; |
| 721 | if (PyLong_Check(arg)) |
| 722 | ival = PyLong_AsUnsignedLongMask(arg); |
| 723 | else |
| 724 | return converterr("integer<k>", arg, msgbuf, bufsize); |
| 725 | *p = ival; |
| 726 | break; |
| 727 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 728 | |
Guido van Rossum | 3dbba6e | 1999-01-25 21:48:56 +0000 | [diff] [blame] | 729 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | case 'L': {/* PY_LONG_LONG */ |
| 731 | PY_LONG_LONG *p = va_arg( *p_va, PY_LONG_LONG * ); |
| 732 | PY_LONG_LONG ival; |
Mark Dickinson | c730131 | 2010-06-10 16:05:10 +0000 | [diff] [blame] | 733 | if (float_argument_error(arg)) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 734 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | ival = PyLong_AsLongLong(arg); |
Mark Dickinson | c730131 | 2010-06-10 16:05:10 +0000 | [diff] [blame] | 736 | if (ival == (PY_LONG_LONG)-1 && PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 737 | RETURN_ERR_OCCURRED; |
Mark Dickinson | c730131 | 2010-06-10 16:05:10 +0000 | [diff] [blame] | 738 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | *p = ival; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | break; |
| 741 | } |
Thomas Heller | a4ea603 | 2003-04-17 18:55:45 +0000 | [diff] [blame] | 742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | case 'K': { /* long long sized bitfield */ |
| 744 | unsigned PY_LONG_LONG *p = va_arg(*p_va, unsigned PY_LONG_LONG *); |
| 745 | unsigned PY_LONG_LONG ival; |
| 746 | if (PyLong_Check(arg)) |
| 747 | ival = PyLong_AsUnsignedLongLongMask(arg); |
| 748 | else |
| 749 | return converterr("integer<K>", arg, msgbuf, bufsize); |
| 750 | *p = ival; |
| 751 | break; |
| 752 | } |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 753 | #endif |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | case 'f': {/* float */ |
| 756 | float *p = va_arg(*p_va, float *); |
| 757 | double dval = PyFloat_AsDouble(arg); |
| 758 | if (PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 759 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | else |
| 761 | *p = (float) dval; |
| 762 | break; |
| 763 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | case 'd': {/* double */ |
| 766 | double *p = va_arg(*p_va, double *); |
| 767 | double dval = PyFloat_AsDouble(arg); |
| 768 | if (PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 769 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | else |
| 771 | *p = dval; |
| 772 | break; |
| 773 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | case 'D': {/* complex double */ |
| 776 | Py_complex *p = va_arg(*p_va, Py_complex *); |
| 777 | Py_complex cval; |
| 778 | cval = PyComplex_AsCComplex(arg); |
| 779 | if (PyErr_Occurred()) |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 780 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | else |
| 782 | *p = cval; |
| 783 | break; |
| 784 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | case 'c': {/* char */ |
| 787 | char *p = va_arg(*p_va, char *); |
| 788 | if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1) |
| 789 | *p = PyBytes_AS_STRING(arg)[0]; |
Eli Bendersky | 906b88f | 2011-07-29 07:05:08 +0300 | [diff] [blame] | 790 | else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1) |
| 791 | *p = PyByteArray_AS_STRING(arg)[0]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | else |
| 793 | return converterr("a byte string of length 1", arg, msgbuf, bufsize); |
| 794 | break; |
| 795 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | case 'C': {/* unicode char */ |
| 798 | int *p = va_arg(*p_va, int *); |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 799 | int kind; |
| 800 | void *data; |
| 801 | |
| 802 | if (!PyUnicode_Check(arg)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | return converterr("a unicode character", arg, msgbuf, bufsize); |
Victor Stinner | e1335c7 | 2011-10-04 20:53:03 +0200 | [diff] [blame] | 804 | |
| 805 | if (PyUnicode_READY(arg)) |
| 806 | RETURN_ERR_OCCURRED; |
| 807 | |
| 808 | if (PyUnicode_GET_LENGTH(arg) != 1) |
| 809 | return converterr("a unicode character", arg, msgbuf, bufsize); |
| 810 | |
| 811 | kind = PyUnicode_KIND(arg); |
| 812 | data = PyUnicode_DATA(arg); |
| 813 | *p = PyUnicode_READ(kind, data, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | break; |
| 815 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 816 | |
Larry Hastings | faf91e7 | 2012-05-05 16:54:29 -0700 | [diff] [blame] | 817 | case 'p': {/* boolean *p*redicate */ |
| 818 | int *p = va_arg(*p_va, int *); |
| 819 | int val = PyObject_IsTrue(arg); |
| 820 | if (val > 0) |
| 821 | *p = 1; |
| 822 | else if (val == 0) |
| 823 | *p = 0; |
| 824 | else |
| 825 | RETURN_ERR_OCCURRED; |
| 826 | break; |
| 827 | } |
| 828 | |
Victor Stinner | 3dcb5ac | 2010-06-08 22:54:19 +0000 | [diff] [blame] | 829 | /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | need to be cleaned up! */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | case 'y': {/* any buffer-like object, but not PyUnicode */ |
| 833 | void **p = (void **)va_arg(*p_va, char **); |
| 834 | char *buf; |
| 835 | Py_ssize_t count; |
| 836 | if (*format == '*') { |
| 837 | if (getbuffer(arg, (Py_buffer*)p, &buf) < 0) |
| 838 | return converterr(buf, arg, msgbuf, bufsize); |
| 839 | format++; |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 840 | if (addcleanup(p, freelist, cleanup_buffer)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | return converterr( |
| 842 | "(cleanup problem)", |
| 843 | arg, msgbuf, bufsize); |
| 844 | } |
| 845 | break; |
| 846 | } |
| 847 | count = convertbuffer(arg, p, &buf); |
| 848 | if (count < 0) |
| 849 | return converterr(buf, arg, msgbuf, bufsize); |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 850 | if (*format == '#') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | FETCH_SIZE; |
| 852 | STORE_SIZE(count); |
| 853 | format++; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 854 | } else { |
| 855 | if (strlen(*p) != count) |
| 856 | return converterr( |
| 857 | "bytes without null bytes", |
| 858 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | } |
| 860 | break; |
| 861 | } |
Walter Dörwald | 612344f | 2007-05-04 19:28:21 +0000 | [diff] [blame] | 862 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 863 | case 's': /* text string */ |
| 864 | case 'z': /* text string or None */ |
| 865 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | if (*format == '*') { |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 867 | /* "s*" or "z*" */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 868 | Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 869 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 870 | if (c == 'z' && arg == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0); |
| 872 | else if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 873 | Py_ssize_t len; |
| 874 | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 875 | if (sarg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | return converterr(CONV_UNICODE, |
| 877 | arg, msgbuf, bufsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 878 | PyBuffer_FillInfo(p, arg, sarg, len, 1, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | } |
| 880 | else { /* any buffer-like object */ |
| 881 | char *buf; |
| 882 | if (getbuffer(arg, p, &buf) < 0) |
| 883 | return converterr(buf, arg, msgbuf, bufsize); |
| 884 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 885 | if (addcleanup(p, freelist, cleanup_buffer)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | return converterr( |
| 887 | "(cleanup problem)", |
| 888 | arg, msgbuf, bufsize); |
| 889 | } |
| 890 | format++; |
| 891 | } else if (*format == '#') { /* any buffer-like object */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 892 | /* "s#" or "z#" */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | void **p = (void **)va_arg(*p_va, char **); |
| 894 | FETCH_SIZE; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 895 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 896 | if (c == 'z' && arg == Py_None) { |
| 897 | *p = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | STORE_SIZE(0); |
| 899 | } |
| 900 | else if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 901 | Py_ssize_t len; |
| 902 | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 903 | if (sarg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | return converterr(CONV_UNICODE, |
| 905 | arg, msgbuf, bufsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 906 | *p = sarg; |
| 907 | STORE_SIZE(len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | } |
| 909 | else { /* any buffer-like object */ |
| 910 | /* XXX Really? */ |
| 911 | char *buf; |
| 912 | Py_ssize_t count = convertbuffer(arg, p, &buf); |
| 913 | if (count < 0) |
| 914 | return converterr(buf, arg, msgbuf, bufsize); |
| 915 | STORE_SIZE(count); |
| 916 | } |
| 917 | format++; |
| 918 | } else { |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 919 | /* "s" or "z" */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 920 | char **p = va_arg(*p_va, char **); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 921 | Py_ssize_t len; |
| 922 | sarg = NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 923 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 924 | if (c == 'z' && arg == Py_None) |
| 925 | *p = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | else if (PyUnicode_Check(arg)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 927 | sarg = PyUnicode_AsUTF8AndSize(arg, &len); |
| 928 | if (sarg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | return converterr(CONV_UNICODE, |
| 930 | arg, msgbuf, bufsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 931 | *p = sarg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | } |
| 933 | else |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 934 | return converterr(c == 'z' ? "str or None" : "str", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | arg, msgbuf, bufsize); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 936 | if (*p != NULL && sarg != NULL && (Py_ssize_t) strlen(*p) != len) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | return converterr( |
Victor Stinner | 9d663d0 | 2011-10-13 23:25:03 +0200 | [diff] [blame] | 938 | c == 'z' ? "str without null characters or None" |
| 939 | : "str without null characters", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | arg, msgbuf, bufsize); |
| 941 | } |
| 942 | break; |
| 943 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 944 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 945 | case 'u': /* raw unicode buffer (Py_UNICODE *) */ |
| 946 | case 'Z': /* raw unicode buffer or None */ |
| 947 | { |
Brett Cannon | b94767f | 2011-02-22 20:15:44 +0000 | [diff] [blame] | 948 | Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **); |
| 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | if (*format == '#') { /* any buffer-like object */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 951 | /* "s#" or "Z#" */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | FETCH_SIZE; |
Travis E. Oliphant | ddacf96 | 2007-10-13 21:03:27 +0000 | [diff] [blame] | 953 | |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 954 | if (c == 'Z' && arg == Py_None) { |
| 955 | *p = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | STORE_SIZE(0); |
| 957 | } |
| 958 | else if (PyUnicode_Check(arg)) { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 959 | Py_ssize_t len; |
| 960 | *p = PyUnicode_AsUnicodeAndSize(arg, &len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 961 | if (*p == NULL) |
| 962 | RETURN_ERR_OCCURRED; |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 963 | STORE_SIZE(len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | } |
Victor Stinner | 5216e6d | 2010-06-08 21:45:51 +0000 | [diff] [blame] | 965 | else |
| 966 | return converterr("str or None", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | format++; |
| 968 | } else { |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 969 | /* "s" or "Z" */ |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 970 | if (c == 'Z' && arg == Py_None) |
| 971 | *p = NULL; |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 972 | else if (PyUnicode_Check(arg)) { |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 973 | Py_ssize_t len; |
| 974 | *p = PyUnicode_AsUnicodeAndSize(arg, &len); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 975 | if (*p == NULL) |
| 976 | RETURN_ERR_OCCURRED; |
Victor Stinner | beac78b | 2011-10-11 21:55:01 +0200 | [diff] [blame] | 977 | if (Py_UNICODE_strlen(*p) != len) |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 978 | return converterr( |
Victor Stinner | 9d663d0 | 2011-10-13 23:25:03 +0200 | [diff] [blame] | 979 | "str without null characters or None", |
Victor Stinner | 06e49dd | 2010-06-13 18:21:50 +0000 | [diff] [blame] | 980 | arg, msgbuf, bufsize); |
| 981 | } else |
Victor Stinner | 3c9e6e9 | 2010-06-24 22:31:12 +0000 | [diff] [blame] | 982 | return converterr(c == 'Z' ? "str or None" : "str", |
| 983 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | } |
| 985 | break; |
| 986 | } |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | case 'e': {/* encoded string */ |
| 989 | char **buffer; |
| 990 | const char *encoding; |
| 991 | PyObject *s; |
| 992 | int recode_strings; |
| 993 | Py_ssize_t size; |
| 994 | const char *ptr; |
Jeremy Hylton | 4819e97 | 2001-10-11 14:40:37 +0000 | [diff] [blame] | 995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | /* Get 'e' parameter: the encoding name */ |
| 997 | encoding = (const char *)va_arg(*p_va, const char *); |
| 998 | if (encoding == NULL) |
| 999 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | /* Get output buffer parameter: |
| 1002 | 's' (recode all objects via Unicode) or |
| 1003 | 't' (only recode non-string objects) |
| 1004 | */ |
| 1005 | if (*format == 's') |
| 1006 | recode_strings = 1; |
| 1007 | else if (*format == 't') |
| 1008 | recode_strings = 0; |
| 1009 | else |
| 1010 | return converterr( |
| 1011 | "(unknown parser marker combination)", |
| 1012 | arg, msgbuf, bufsize); |
| 1013 | buffer = (char **)va_arg(*p_va, char **); |
| 1014 | format++; |
| 1015 | if (buffer == NULL) |
| 1016 | return converterr("(buffer is NULL)", |
| 1017 | arg, msgbuf, bufsize); |
Benjamin Peterson | 9edd2bd | 2008-08-27 00:31:37 +0000 | [diff] [blame] | 1018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | /* Encode object */ |
| 1020 | if (!recode_strings && |
| 1021 | (PyBytes_Check(arg) || PyByteArray_Check(arg))) { |
| 1022 | s = arg; |
| 1023 | Py_INCREF(s); |
| 1024 | if (PyObject_AsCharBuffer(s, &ptr, &size) < 0) |
| 1025 | return converterr("(AsCharBuffer failed)", |
| 1026 | arg, msgbuf, bufsize); |
| 1027 | } |
| 1028 | else { |
| 1029 | PyObject *u; |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | /* Convert object to Unicode */ |
| 1032 | u = PyUnicode_FromObject(arg); |
| 1033 | if (u == NULL) |
| 1034 | return converterr( |
| 1035 | "string or unicode or text buffer", |
| 1036 | arg, msgbuf, bufsize); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | /* Encode object; use default error handling */ |
| 1039 | s = PyUnicode_AsEncodedString(u, |
| 1040 | encoding, |
| 1041 | NULL); |
| 1042 | Py_DECREF(u); |
| 1043 | if (s == NULL) |
| 1044 | return converterr("(encoding failed)", |
| 1045 | arg, msgbuf, bufsize); |
| 1046 | if (!PyBytes_Check(s)) { |
| 1047 | Py_DECREF(s); |
| 1048 | return converterr( |
| 1049 | "(encoder failed to return bytes)", |
| 1050 | arg, msgbuf, bufsize); |
| 1051 | } |
| 1052 | size = PyBytes_GET_SIZE(s); |
| 1053 | ptr = PyBytes_AS_STRING(s); |
| 1054 | if (ptr == NULL) |
| 1055 | ptr = ""; |
| 1056 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | /* Write output; output is guaranteed to be 0-terminated */ |
| 1059 | if (*format == '#') { |
| 1060 | /* Using buffer length parameter '#': |
| 1061 | |
| 1062 | - if *buffer is NULL, a new buffer of the |
| 1063 | needed size is allocated and the data |
| 1064 | copied into it; *buffer is updated to point |
| 1065 | to the new buffer; the caller is |
| 1066 | responsible for PyMem_Free()ing it after |
| 1067 | usage |
| 1068 | |
| 1069 | - if *buffer is not NULL, the data is |
| 1070 | copied to *buffer; *buffer_len has to be |
| 1071 | set to the size of the buffer on input; |
| 1072 | buffer overflow is signalled with an error; |
| 1073 | buffer has to provide enough room for the |
| 1074 | encoded string plus the trailing 0-byte |
| 1075 | |
| 1076 | - in both cases, *buffer_len is updated to |
| 1077 | the size of the buffer /excluding/ the |
| 1078 | trailing 0-byte |
| 1079 | |
| 1080 | */ |
| 1081 | FETCH_SIZE; |
| 1082 | |
| 1083 | format++; |
| 1084 | if (q == NULL && q2 == NULL) { |
| 1085 | Py_DECREF(s); |
| 1086 | return converterr( |
| 1087 | "(buffer_len is NULL)", |
| 1088 | arg, msgbuf, bufsize); |
| 1089 | } |
| 1090 | if (*buffer == NULL) { |
| 1091 | *buffer = PyMem_NEW(char, size + 1); |
| 1092 | if (*buffer == NULL) { |
| 1093 | Py_DECREF(s); |
Victor Stinner | 2872e5b | 2010-06-06 20:38:02 +0000 | [diff] [blame] | 1094 | PyErr_NoMemory(); |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1095 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1097 | if (addcleanup(*buffer, freelist, cleanup_ptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | Py_DECREF(s); |
| 1099 | return converterr( |
| 1100 | "(cleanup problem)", |
| 1101 | arg, msgbuf, bufsize); |
| 1102 | } |
| 1103 | } else { |
| 1104 | if (size + 1 > BUFFER_LEN) { |
| 1105 | Py_DECREF(s); |
| 1106 | return converterr( |
| 1107 | "(buffer overflow)", |
| 1108 | arg, msgbuf, bufsize); |
| 1109 | } |
| 1110 | } |
| 1111 | memcpy(*buffer, ptr, size+1); |
| 1112 | STORE_SIZE(size); |
| 1113 | } else { |
| 1114 | /* Using a 0-terminated buffer: |
| 1115 | |
| 1116 | - the encoded string has to be 0-terminated |
| 1117 | for this variant to work; if it is not, an |
| 1118 | error raised |
| 1119 | |
| 1120 | - a new buffer of the needed size is |
| 1121 | allocated and the data copied into it; |
| 1122 | *buffer is updated to point to the new |
| 1123 | buffer; the caller is responsible for |
| 1124 | PyMem_Free()ing it after usage |
| 1125 | |
| 1126 | */ |
| 1127 | if ((Py_ssize_t)strlen(ptr) != size) { |
| 1128 | Py_DECREF(s); |
| 1129 | return converterr( |
| 1130 | "encoded string without NULL bytes", |
| 1131 | arg, msgbuf, bufsize); |
| 1132 | } |
| 1133 | *buffer = PyMem_NEW(char, size + 1); |
| 1134 | if (*buffer == NULL) { |
| 1135 | Py_DECREF(s); |
Victor Stinner | 2872e5b | 2010-06-06 20:38:02 +0000 | [diff] [blame] | 1136 | PyErr_NoMemory(); |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1137 | RETURN_ERR_OCCURRED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1139 | if (addcleanup(*buffer, freelist, cleanup_ptr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | Py_DECREF(s); |
| 1141 | return converterr("(cleanup problem)", |
| 1142 | arg, msgbuf, bufsize); |
| 1143 | } |
| 1144 | memcpy(*buffer, ptr, size+1); |
| 1145 | } |
| 1146 | Py_DECREF(s); |
| 1147 | break; |
| 1148 | } |
| 1149 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | case 'S': { /* PyBytes object */ |
| 1151 | PyObject **p = va_arg(*p_va, PyObject **); |
| 1152 | if (PyBytes_Check(arg)) |
| 1153 | *p = arg; |
| 1154 | else |
| 1155 | return converterr("bytes", arg, msgbuf, bufsize); |
| 1156 | break; |
| 1157 | } |
| 1158 | |
| 1159 | case 'Y': { /* PyByteArray object */ |
| 1160 | PyObject **p = va_arg(*p_va, PyObject **); |
| 1161 | if (PyByteArray_Check(arg)) |
| 1162 | *p = arg; |
| 1163 | else |
Victor Stinner | 5216e6d | 2010-06-08 21:45:51 +0000 | [diff] [blame] | 1164 | return converterr("bytearray", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | break; |
| 1166 | } |
| 1167 | |
| 1168 | case 'U': { /* PyUnicode object */ |
| 1169 | PyObject **p = va_arg(*p_va, PyObject **); |
| 1170 | if (PyUnicode_Check(arg)) |
| 1171 | *p = arg; |
| 1172 | else |
| 1173 | return converterr("str", arg, msgbuf, bufsize); |
| 1174 | break; |
| 1175 | } |
| 1176 | |
| 1177 | case 'O': { /* object */ |
| 1178 | PyTypeObject *type; |
| 1179 | PyObject **p; |
| 1180 | if (*format == '!') { |
| 1181 | type = va_arg(*p_va, PyTypeObject*); |
| 1182 | p = va_arg(*p_va, PyObject **); |
| 1183 | format++; |
| 1184 | if (PyType_IsSubtype(arg->ob_type, type)) |
| 1185 | *p = arg; |
| 1186 | else |
| 1187 | return converterr(type->tp_name, arg, msgbuf, bufsize); |
| 1188 | |
| 1189 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | else if (*format == '&') { |
| 1191 | typedef int (*converter)(PyObject *, void *); |
| 1192 | converter convert = va_arg(*p_va, converter); |
| 1193 | void *addr = va_arg(*p_va, void *); |
| 1194 | int res; |
| 1195 | format++; |
| 1196 | if (! (res = (*convert)(arg, addr))) |
| 1197 | return converterr("(unspecified)", |
| 1198 | arg, msgbuf, bufsize); |
| 1199 | if (res == Py_CLEANUP_SUPPORTED && |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1200 | addcleanup(addr, freelist, convert) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | return converterr("(cleanup problem)", |
| 1202 | arg, msgbuf, bufsize); |
| 1203 | } |
| 1204 | else { |
| 1205 | p = va_arg(*p_va, PyObject **); |
| 1206 | *p = arg; |
| 1207 | } |
| 1208 | break; |
| 1209 | } |
| 1210 | |
| 1211 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1212 | case 'w': { /* "w*": memory buffer, read-write access */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | void **p = va_arg(*p_va, void **); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1215 | if (*format != '*') |
| 1216 | return converterr( |
| 1217 | "invalid use of 'w' format character", |
| 1218 | arg, msgbuf, bufsize); |
| 1219 | format++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1221 | /* Caller is interested in Py_buffer, and the object |
| 1222 | supports it directly. */ |
| 1223 | if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) { |
| 1224 | PyErr_Clear(); |
| 1225 | return converterr("read-write buffer", arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | } |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1227 | if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) { |
| 1228 | PyBuffer_Release((Py_buffer*)p); |
| 1229 | return converterr("contiguous buffer", arg, msgbuf, bufsize); |
| 1230 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1231 | if (addcleanup(p, freelist, cleanup_buffer)) { |
Victor Stinner | 25e8ec4 | 2010-06-25 00:02:38 +0000 | [diff] [blame] | 1232 | return converterr( |
| 1233 | "(cleanup problem)", |
| 1234 | arg, msgbuf, bufsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | break; |
| 1237 | } |
| 1238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | default: |
| 1240 | return converterr("impossible<bad format char>", arg, msgbuf, bufsize); |
| 1241 | |
| 1242 | } |
| 1243 | |
| 1244 | *p_format = format; |
| 1245 | return NULL; |
Victor Stinner | 6ab8e82 | 2011-01-04 11:16:49 +0000 | [diff] [blame] | 1246 | |
| 1247 | #undef FETCH_SIZE |
| 1248 | #undef STORE_SIZE |
| 1249 | #undef BUFFER_LEN |
| 1250 | #undef RETURN_ERR_OCCURRED |
Guido van Rossum | fe3f1a2 | 1994-09-29 09:42:55 +0000 | [diff] [blame] | 1251 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1252 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1253 | static Py_ssize_t |
Fred Drake | 563dfc2 | 2001-10-23 14:41:08 +0000 | [diff] [blame] | 1254 | convertbuffer(PyObject *arg, void **p, char **errmsg) |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1255 | { |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1256 | PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | Py_ssize_t count; |
| 1258 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | *errmsg = NULL; |
| 1261 | *p = NULL; |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1262 | if (pb != NULL && pb->bf_releasebuffer != NULL) { |
| 1263 | *errmsg = "read-only pinned buffer"; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | return -1; |
| 1265 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1266 | |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1267 | if (getbuffer(arg, &view, errmsg) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | count = view.len; |
| 1270 | *p = view.buf; |
| 1271 | PyBuffer_Release(&view); |
| 1272 | return count; |
Jeremy Hylton | 1cb7aa3 | 2001-05-29 17:37:05 +0000 | [diff] [blame] | 1273 | } |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1274 | |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1275 | static int |
Neal Norwitz | 2f99b24 | 2008-08-24 05:48:10 +0000 | [diff] [blame] | 1276 | getbuffer(PyObject *arg, Py_buffer *view, char **errmsg) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1277 | { |
Victor Stinner | 8182b71 | 2010-07-28 00:40:58 +0000 | [diff] [blame] | 1278 | if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | *errmsg = "bytes or buffer"; |
| 1280 | return -1; |
| 1281 | } |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1282 | if (!PyBuffer_IsContiguous(view, 'C')) { |
Victor Stinner | 21e0948 | 2010-06-24 22:57:10 +0000 | [diff] [blame] | 1283 | PyBuffer_Release(view); |
Victor Stinner | 5cb6239 | 2010-06-06 20:27:51 +0000 | [diff] [blame] | 1284 | *errmsg = "contiguous buffer"; |
| 1285 | return -1; |
| 1286 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | return 0; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1290 | /* Support for keyword arguments donated by |
| 1291 | Geoff Philbrick <philbric@delphi.hks.com> */ |
| 1292 | |
Tim Peters | f8cd3e8 | 2001-10-27 04:26:57 +0000 | [diff] [blame] | 1293 | /* Return false (0) for error, else true. */ |
Fred Drake | 563dfc2 | 2001-10-23 14:41:08 +0000 | [diff] [blame] | 1294 | int |
| 1295 | PyArg_ParseTupleAndKeywords(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | PyObject *keywords, |
| 1297 | const char *format, |
| 1298 | char **kwlist, ...) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1299 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | int retval; |
| 1301 | va_list va; |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1303 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1304 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1305 | format == NULL || |
| 1306 | kwlist == NULL) |
| 1307 | { |
| 1308 | PyErr_BadInternalCall(); |
| 1309 | return 0; |
| 1310 | } |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | va_start(va, kwlist); |
| 1313 | retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0); |
| 1314 | va_end(va); |
| 1315 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | int |
| 1319 | _PyArg_ParseTupleAndKeywords_SizeT(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1320 | PyObject *keywords, |
| 1321 | const char *format, |
| 1322 | char **kwlist, ...) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1323 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | int retval; |
| 1325 | va_list va; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1328 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1329 | format == NULL || |
| 1330 | kwlist == NULL) |
| 1331 | { |
| 1332 | PyErr_BadInternalCall(); |
| 1333 | return 0; |
| 1334 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | va_start(va, kwlist); |
| 1337 | retval = vgetargskeywords(args, keywords, format, |
| 1338 | kwlist, &va, FLAG_SIZE_T); |
| 1339 | va_end(va); |
| 1340 | return retval; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1344 | int |
| 1345 | PyArg_VaParseTupleAndKeywords(PyObject *args, |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1346 | PyObject *keywords, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1347 | const char *format, |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1348 | char **kwlist, va_list va) |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | int retval; |
| 1351 | va_list lva; |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1354 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1355 | format == NULL || |
| 1356 | kwlist == NULL) |
| 1357 | { |
| 1358 | PyErr_BadInternalCall(); |
| 1359 | return 0; |
| 1360 | } |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1361 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 1362 | Py_VA_COPY(lva, va); |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1364 | retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0); |
| 1365 | return retval; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | int |
| 1369 | _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | PyObject *keywords, |
| 1371 | const char *format, |
| 1372 | char **kwlist, va_list va) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1373 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | int retval; |
| 1375 | va_list lva; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | if ((args == NULL || !PyTuple_Check(args)) || |
| 1378 | (keywords != NULL && !PyDict_Check(keywords)) || |
| 1379 | format == NULL || |
| 1380 | kwlist == NULL) |
| 1381 | { |
| 1382 | PyErr_BadInternalCall(); |
| 1383 | return 0; |
| 1384 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1385 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 1386 | Py_VA_COPY(lva, va); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | retval = vgetargskeywords(args, keywords, format, |
| 1389 | kwlist, &lva, FLAG_SIZE_T); |
| 1390 | return retval; |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 1393 | int |
| 1394 | PyArg_ValidateKeywordArguments(PyObject *kwargs) |
| 1395 | { |
Benjamin Peterson | f609654 | 2010-11-17 22:33:12 +0000 | [diff] [blame] | 1396 | if (!PyDict_Check(kwargs)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | PyErr_BadInternalCall(); |
| 1398 | return 0; |
| 1399 | } |
| 1400 | if (!_PyDict_HasOnlyStringKeys(kwargs)) { |
| 1401 | PyErr_SetString(PyExc_TypeError, |
| 1402 | "keyword arguments must be strings"); |
| 1403 | return 0; |
| 1404 | } |
| 1405 | return 1; |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1408 | #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') |
Brett Cannon | 711e7d9 | 2004-07-10 22:20:32 +0000 | [diff] [blame] | 1409 | |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1410 | static int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1411 | vgetargskeywords(PyObject *args, PyObject *keywords, const char *format, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | char **kwlist, va_list *p_va, int flags) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | char msgbuf[512]; |
| 1415 | int levels[32]; |
| 1416 | const char *fname, *msg, *custom_msg, *keyword; |
| 1417 | int min = INT_MAX; |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1418 | int max = INT_MAX; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | int i, len, nargs, nkeywords; |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1420 | PyObject *current_arg; |
| 1421 | freelist_t freelist = {0, NULL}; |
Tim Peters | f4331c1 | 2001-10-27 00:17:34 +0000 | [diff] [blame] | 1422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | assert(args != NULL && PyTuple_Check(args)); |
| 1424 | assert(keywords == NULL || PyDict_Check(keywords)); |
| 1425 | assert(format != NULL); |
| 1426 | assert(kwlist != NULL); |
| 1427 | assert(p_va != NULL); |
Tim Peters | 45772cd | 2001-10-27 03:58:40 +0000 | [diff] [blame] | 1428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1429 | /* grab the function name or custom error msg first (mutually exclusive) */ |
| 1430 | fname = strchr(format, ':'); |
| 1431 | if (fname) { |
| 1432 | fname++; |
| 1433 | custom_msg = NULL; |
| 1434 | } |
| 1435 | else { |
| 1436 | custom_msg = strchr(format,';'); |
| 1437 | if (custom_msg) |
| 1438 | custom_msg++; |
| 1439 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1440 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1441 | /* scan kwlist and get greatest possible nbr of args */ |
| 1442 | for (len=0; kwlist[len]; len++) |
| 1443 | continue; |
Tim Peters | f8cd3e8 | 2001-10-27 04:26:57 +0000 | [diff] [blame] | 1444 | |
Benjamin Peterson | f7c1321 | 2012-03-16 12:23:39 -0500 | [diff] [blame] | 1445 | freelist.entries = PyMem_NEW(freelistentry_t, len); |
Benjamin Peterson | 7ed6727 | 2012-03-16 12:21:02 -0500 | [diff] [blame] | 1446 | if (freelist.entries == NULL) { |
| 1447 | PyErr_NoMemory(); |
| 1448 | return 0; |
| 1449 | } |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | nargs = PyTuple_GET_SIZE(args); |
| 1452 | nkeywords = (keywords == NULL) ? 0 : PyDict_Size(keywords); |
| 1453 | if (nargs + nkeywords > len) { |
Victor Stinner | 6ced7c4 | 2011-03-21 18:15:42 +0100 | [diff] [blame] | 1454 | PyErr_Format(PyExc_TypeError, |
| 1455 | "%s%s takes at most %d argument%s (%d given)", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | (fname == NULL) ? "function" : fname, |
| 1457 | (fname == NULL) ? "" : "()", |
| 1458 | len, |
| 1459 | (len == 1) ? "" : "s", |
| 1460 | nargs + nkeywords); |
Benjamin Peterson | 01feaec | 2012-03-16 13:25:58 -0500 | [diff] [blame] | 1461 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | } |
Tim Peters | c2f0112 | 2001-10-27 07:25:06 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | /* convert tuple args and keyword args in same loop, using kwlist to drive process */ |
| 1465 | for (i = 0; i < len; i++) { |
| 1466 | keyword = kwlist[i]; |
| 1467 | if (*format == '|') { |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1468 | if (min != INT_MAX) { |
| 1469 | PyErr_SetString(PyExc_RuntimeError, |
| 1470 | "Invalid format string (| specified twice)"); |
| 1471 | return cleanreturn(0, &freelist); |
| 1472 | } |
| 1473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1474 | min = i; |
| 1475 | format++; |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1476 | |
| 1477 | if (max != INT_MAX) { |
| 1478 | PyErr_SetString(PyExc_RuntimeError, |
| 1479 | "Invalid format string ($ before |)"); |
| 1480 | return cleanreturn(0, &freelist); |
| 1481 | } |
| 1482 | } |
| 1483 | if (*format == '$') { |
| 1484 | if (max != INT_MAX) { |
| 1485 | PyErr_SetString(PyExc_RuntimeError, |
| 1486 | "Invalid format string ($ specified twice)"); |
| 1487 | return cleanreturn(0, &freelist); |
| 1488 | } |
| 1489 | |
| 1490 | max = i; |
| 1491 | format++; |
| 1492 | |
| 1493 | if (max < nargs) { |
| 1494 | PyErr_Format(PyExc_TypeError, |
| 1495 | "Function takes %s %d positional arguments" |
| 1496 | " (%d given)", |
| 1497 | (min != INT_MAX) ? "at most" : "exactly", |
| 1498 | max, nargs); |
| 1499 | return cleanreturn(0, &freelist); |
| 1500 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | } |
| 1502 | if (IS_END_OF_FORMAT(*format)) { |
| 1503 | PyErr_Format(PyExc_RuntimeError, |
| 1504 | "More keyword list entries (%d) than " |
| 1505 | "format specifiers (%d)", len, i); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1506 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1507 | } |
| 1508 | current_arg = NULL; |
| 1509 | if (nkeywords) { |
| 1510 | current_arg = PyDict_GetItemString(keywords, keyword); |
| 1511 | } |
| 1512 | if (current_arg) { |
| 1513 | --nkeywords; |
| 1514 | if (i < nargs) { |
| 1515 | /* arg present in tuple and in dict */ |
| 1516 | PyErr_Format(PyExc_TypeError, |
| 1517 | "Argument given by name ('%s') " |
| 1518 | "and position (%d)", |
| 1519 | keyword, i+1); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1520 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | } |
| 1522 | } |
| 1523 | else if (nkeywords && PyErr_Occurred()) |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1524 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1525 | else if (i < nargs) |
| 1526 | current_arg = PyTuple_GET_ITEM(args, i); |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1528 | if (current_arg) { |
| 1529 | msg = convertitem(current_arg, &format, p_va, flags, |
| 1530 | levels, msgbuf, sizeof(msgbuf), &freelist); |
| 1531 | if (msg) { |
| 1532 | seterror(i+1, msg, levels, fname, custom_msg); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1533 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | } |
| 1535 | continue; |
| 1536 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1537 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | if (i < min) { |
| 1539 | PyErr_Format(PyExc_TypeError, "Required argument " |
| 1540 | "'%s' (pos %d) not found", |
| 1541 | keyword, i+1); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1542 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | } |
| 1544 | /* current code reports success when all required args |
| 1545 | * fulfilled and no keyword args left, with no further |
| 1546 | * validation. XXX Maybe skip this in debug build ? |
| 1547 | */ |
| 1548 | if (!nkeywords) |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1549 | return cleanreturn(1, &freelist); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1551 | /* We are into optional args, skip thru to any remaining |
| 1552 | * keyword args */ |
| 1553 | msg = skipitem(&format, p_va, flags); |
| 1554 | if (msg) { |
| 1555 | PyErr_Format(PyExc_RuntimeError, "%s: '%s'", msg, |
| 1556 | format); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1557 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | } |
| 1559 | } |
Tim Peters | b054be4 | 2001-10-27 05:07:41 +0000 | [diff] [blame] | 1560 | |
Larry Hastings | 83a9f48 | 2012-03-20 20:06:16 +0000 | [diff] [blame] | 1561 | if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | PyErr_Format(PyExc_RuntimeError, |
| 1563 | "more argument specifiers than keyword list entries " |
| 1564 | "(remaining format:'%s')", format); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1565 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | } |
Tim Peters | c2f0112 | 2001-10-27 07:25:06 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1568 | /* make sure there are no extraneous keyword arguments */ |
| 1569 | if (nkeywords > 0) { |
| 1570 | PyObject *key, *value; |
| 1571 | Py_ssize_t pos = 0; |
| 1572 | while (PyDict_Next(keywords, &pos, &key, &value)) { |
| 1573 | int match = 0; |
| 1574 | char *ks; |
| 1575 | if (!PyUnicode_Check(key)) { |
| 1576 | PyErr_SetString(PyExc_TypeError, |
| 1577 | "keywords must be strings"); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1578 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | } |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 1580 | /* check that _PyUnicode_AsString() result is not NULL */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1581 | ks = _PyUnicode_AsString(key); |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 1582 | if (ks != NULL) { |
| 1583 | for (i = 0; i < len; i++) { |
| 1584 | if (!strcmp(ks, kwlist[i])) { |
| 1585 | match = 1; |
| 1586 | break; |
| 1587 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | if (!match) { |
| 1591 | PyErr_Format(PyExc_TypeError, |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 1592 | "'%U' is an invalid keyword " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | "argument for this function", |
Victor Stinner | 93b5513 | 2010-05-19 00:54:06 +0000 | [diff] [blame] | 1594 | key); |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1595 | return cleanreturn(0, &freelist); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | |
Jean-Paul Calderone | c961b4a | 2012-03-16 08:51:42 -0400 | [diff] [blame] | 1600 | return cleanreturn(1, &freelist); |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | |
| 1604 | static char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1605 | skipitem(const char **p_format, va_list *p_va, int flags) |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1606 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1607 | const char *format = *p_format; |
| 1608 | char c = *format++; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | switch (c) { |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 1611 | |
Larry Hastings | a347901 | 2012-05-08 23:52:03 -0700 | [diff] [blame] | 1612 | /* |
| 1613 | * codes that take a single data pointer as an argument |
| 1614 | * (the type of the pointer is irrelevant) |
| 1615 | */ |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 1616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | case 'b': /* byte -- very short int */ |
| 1618 | case 'B': /* byte as bitfield */ |
| 1619 | case 'h': /* short int */ |
| 1620 | case 'H': /* short int as bitfield */ |
| 1621 | case 'i': /* int */ |
| 1622 | case 'I': /* int sized bitfield */ |
| 1623 | case 'l': /* long int */ |
| 1624 | case 'k': /* long int sized bitfield */ |
Guido van Rossum | 3dbba6e | 1999-01-25 21:48:56 +0000 | [diff] [blame] | 1625 | #ifdef HAVE_LONG_LONG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1626 | case 'L': /* PY_LONG_LONG */ |
| 1627 | case 'K': /* PY_LONG_LONG sized bitfield */ |
Guido van Rossum | 1a8791e | 1998-08-04 22:46:29 +0000 | [diff] [blame] | 1628 | #endif |
Larry Hastings | a347901 | 2012-05-08 23:52:03 -0700 | [diff] [blame] | 1629 | case 'n': /* Py_ssize_t */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1630 | case 'f': /* float */ |
| 1631 | case 'd': /* double */ |
| 1632 | case 'D': /* complex double */ |
| 1633 | case 'c': /* char */ |
| 1634 | case 'C': /* unicode char */ |
Larry Hastings | 10ba07a | 2012-05-07 02:44:50 -0700 | [diff] [blame] | 1635 | case 'p': /* boolean predicate */ |
Larry Hastings | a347901 | 2012-05-08 23:52:03 -0700 | [diff] [blame] | 1636 | case 'S': /* string object */ |
| 1637 | case 'Y': /* string object */ |
| 1638 | case 'U': /* unicode string object */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | { |
| 1640 | (void) va_arg(*p_va, void *); |
| 1641 | break; |
| 1642 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1644 | /* string codes */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1646 | case 'e': /* string with encoding */ |
| 1647 | { |
| 1648 | (void) va_arg(*p_va, const char *); |
| 1649 | if (!(*format == 's' || *format == 't')) |
| 1650 | /* after 'e', only 's' and 't' is allowed */ |
| 1651 | goto err; |
| 1652 | format++; |
| 1653 | /* explicit fallthrough to string cases */ |
| 1654 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | case 's': /* string */ |
| 1657 | case 'z': /* string or None */ |
| 1658 | case 'y': /* bytes */ |
| 1659 | case 'u': /* unicode string */ |
Larry Hastings | d9e4a41 | 2012-05-08 03:51:18 -0700 | [diff] [blame] | 1660 | case 'Z': /* unicode string or None */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1661 | case 'w': /* buffer, read-write */ |
| 1662 | { |
| 1663 | (void) va_arg(*p_va, char **); |
| 1664 | if (*format == '#') { |
| 1665 | if (flags & FLAG_SIZE_T) |
| 1666 | (void) va_arg(*p_va, Py_ssize_t *); |
| 1667 | else |
| 1668 | (void) va_arg(*p_va, int *); |
| 1669 | format++; |
| 1670 | } else if ((c == 's' || c == 'z' || c == 'y') && *format == '*') { |
| 1671 | format++; |
| 1672 | } |
| 1673 | break; |
| 1674 | } |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 1675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 | case 'O': /* object */ |
| 1677 | { |
| 1678 | if (*format == '!') { |
| 1679 | format++; |
| 1680 | (void) va_arg(*p_va, PyTypeObject*); |
| 1681 | (void) va_arg(*p_va, PyObject **); |
| 1682 | } |
| 1683 | else if (*format == '&') { |
| 1684 | typedef int (*converter)(PyObject *, void *); |
| 1685 | (void) va_arg(*p_va, converter); |
| 1686 | (void) va_arg(*p_va, void *); |
| 1687 | format++; |
| 1688 | } |
| 1689 | else { |
| 1690 | (void) va_arg(*p_va, PyObject **); |
| 1691 | } |
| 1692 | break; |
| 1693 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1695 | case '(': /* bypass tuple, not handled at all previously */ |
| 1696 | { |
| 1697 | char *msg; |
| 1698 | for (;;) { |
| 1699 | if (*format==')') |
| 1700 | break; |
| 1701 | if (IS_END_OF_FORMAT(*format)) |
| 1702 | return "Unmatched left paren in format " |
| 1703 | "string"; |
| 1704 | msg = skipitem(&format, p_va, flags); |
| 1705 | if (msg) |
| 1706 | return msg; |
| 1707 | } |
| 1708 | format++; |
| 1709 | break; |
| 1710 | } |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | case ')': |
| 1713 | return "Unmatched right paren in format string"; |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 1714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | default: |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 1716 | err: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | return "impossible<bad format char>"; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | } |
Georg Brandl | 6dd1461 | 2005-09-14 19:29:53 +0000 | [diff] [blame] | 1720 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | *p_format = format; |
| 1722 | return NULL; |
Guido van Rossum | aa35465 | 1996-08-19 19:32:04 +0000 | [diff] [blame] | 1723 | } |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1724 | |
| 1725 | |
| 1726 | int |
Martin v. Löwis | 7624674 | 2006-03-01 04:06:10 +0000 | [diff] [blame] | 1727 | PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | Py_ssize_t i, l; |
| 1730 | PyObject **o; |
| 1731 | va_list vargs; |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1732 | |
| 1733 | #ifdef HAVE_STDARG_PROTOTYPES |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | va_start(vargs, max); |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1735 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1736 | va_start(vargs); |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1737 | #endif |
| 1738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | assert(min >= 0); |
| 1740 | assert(min <= max); |
| 1741 | if (!PyTuple_Check(args)) { |
| 1742 | PyErr_SetString(PyExc_SystemError, |
| 1743 | "PyArg_UnpackTuple() argument list is not a tuple"); |
| 1744 | return 0; |
| 1745 | } |
| 1746 | l = PyTuple_GET_SIZE(args); |
| 1747 | if (l < min) { |
| 1748 | if (name != NULL) |
| 1749 | PyErr_Format( |
| 1750 | PyExc_TypeError, |
| 1751 | "%s expected %s%zd arguments, got %zd", |
| 1752 | name, (min == max ? "" : "at least "), min, l); |
| 1753 | else |
| 1754 | PyErr_Format( |
| 1755 | PyExc_TypeError, |
| 1756 | "unpacked tuple should have %s%zd elements," |
| 1757 | " but has %zd", |
| 1758 | (min == max ? "" : "at least "), min, l); |
| 1759 | va_end(vargs); |
| 1760 | return 0; |
| 1761 | } |
| 1762 | if (l > max) { |
| 1763 | if (name != NULL) |
| 1764 | PyErr_Format( |
| 1765 | PyExc_TypeError, |
| 1766 | "%s expected %s%zd arguments, got %zd", |
| 1767 | name, (min == max ? "" : "at most "), max, l); |
| 1768 | else |
| 1769 | PyErr_Format( |
| 1770 | PyExc_TypeError, |
| 1771 | "unpacked tuple should have %s%zd elements," |
| 1772 | " but has %zd", |
| 1773 | (min == max ? "" : "at most "), max, l); |
| 1774 | va_end(vargs); |
| 1775 | return 0; |
| 1776 | } |
| 1777 | for (i = 0; i < l; i++) { |
| 1778 | o = va_arg(vargs, PyObject **); |
| 1779 | *o = PyTuple_GET_ITEM(args, i); |
| 1780 | } |
| 1781 | va_end(vargs); |
| 1782 | return 1; |
Fred Drake | e4616e6 | 2001-10-23 21:09:29 +0000 | [diff] [blame] | 1783 | } |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1784 | |
| 1785 | |
| 1786 | /* For type constructors that don't take keyword args |
| 1787 | * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1788 | * Sets a TypeError and returns 0 if the kwds dict is |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1789 | * not empty, returns 1 otherwise |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1790 | */ |
| 1791 | int |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 1792 | _PyArg_NoKeywords(const char *funcname, PyObject *kw) |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1794 | if (kw == NULL) |
| 1795 | return 1; |
| 1796 | if (!PyDict_CheckExact(kw)) { |
| 1797 | PyErr_BadInternalCall(); |
| 1798 | return 0; |
| 1799 | } |
| 1800 | if (PyDict_Size(kw) == 0) |
| 1801 | return 1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | PyErr_Format(PyExc_TypeError, "%s does not take keyword arguments", |
| 1804 | funcname); |
| 1805 | return 0; |
Georg Brandl | 02c4287 | 2005-08-26 06:42:30 +0000 | [diff] [blame] | 1806 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1807 | #ifdef __cplusplus |
| 1808 | }; |
| 1809 | #endif |