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