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