1 2/* New getargs implementation */ 3 4#include "Python.h" 5#include "pycore_tuple.h" // _PyTuple_ITEMS() 6#include "pycore_pylifecycle.h" // _PyArg_Fini 7 8#include <ctype.h> 9#include <float.h> 10 11 12#ifdef __cplusplus 13extern "C" { 14#endif 15int PyArg_Parse(PyObject *, const char *, ...); 16int PyArg_ParseTuple(PyObject *, const char *, ...); 17int PyArg_VaParse(PyObject *, const char *, va_list); 18 19int PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, 20 const char *, char **, ...); 21int PyArg_VaParseTupleAndKeywords(PyObject *, PyObject *, 22 const char *, char **, va_list); 23 24int _PyArg_ParseTupleAndKeywordsFast(PyObject *, PyObject *, 25 struct _PyArg_Parser *, ...); 26int _PyArg_VaParseTupleAndKeywordsFast(PyObject *, PyObject *, 27 struct _PyArg_Parser *, va_list); 28 29#ifdef HAVE_DECLSPEC_DLL 30/* Export functions */ 31PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...); 32PyAPI_FUNC(int) _PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs, 33 const char *format, ...); 34PyAPI_FUNC(int) _PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, 35 PyObject *kwnames, 36 struct _PyArg_Parser *parser, ...); 37PyAPI_FUNC(int) _PyArg_ParseTuple_SizeT(PyObject *, const char *, ...); 38PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywords_SizeT(PyObject *, PyObject *, 39 const char *, char **, ...); 40PyAPI_FUNC(PyObject *) _Py_BuildValue_SizeT(const char *, ...); 41PyAPI_FUNC(int) _PyArg_VaParse_SizeT(PyObject *, const char *, va_list); 42PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywords_SizeT(PyObject *, PyObject *, 43 const char *, char **, va_list); 44 45PyAPI_FUNC(int) _PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *, 46 struct _PyArg_Parser *, ...); 47PyAPI_FUNC(int) _PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *, PyObject *, 48 struct _PyArg_Parser *, va_list); 49#endif 50 51#define FLAG_COMPAT 1 52#define FLAG_SIZE_T 2 53 54typedef int (*destr_t)(PyObject *, void *); 55 56 57/* Keep track of "objects" that have been allocated or initialized and 58 which will need to be deallocated or cleaned up somehow if overall 59 parsing fails. 60*/ 61typedef struct { 62 void *item; 63 destr_t destructor; 64} freelistentry_t; 65 66typedef struct { 67 freelistentry_t *entries; 68 int first_available; 69 int entries_malloced; 70} freelist_t; 71 72#define STATIC_FREELIST_ENTRIES 8 73 74/* Forward */ 75static int vgetargs1_impl(PyObject *args, PyObject *const *stack, Py_ssize_t nargs, 76 const char *format, va_list *p_va, int flags); 77static int vgetargs1(PyObject *, const char *, va_list *, int); 78static void seterror(Py_ssize_t, const char *, int *, const char *, const char *); 79static const char *convertitem(PyObject *, const char **, va_list *, int, int *, 80 char *, size_t, freelist_t *); 81static const char *converttuple(PyObject *, const char **, va_list *, int, 82 int *, char *, size_t, int, freelist_t *); 83static const char *convertsimple(PyObject *, const char **, va_list *, int, 84 char *, size_t, freelist_t *); 85static Py_ssize_t convertbuffer(PyObject *, const void **p, const char **); 86static int getbuffer(PyObject *, Py_buffer *, const char**); 87 88static int vgetargskeywords(PyObject *, PyObject *, 89 const char *, char **, va_list *, int); 90static int vgetargskeywordsfast(PyObject *, PyObject *, 91 struct _PyArg_Parser *, va_list *, int); 92static int vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, 93 PyObject *keywords, PyObject *kwnames, 94 struct _PyArg_Parser *parser, 95 va_list *p_va, int flags); 96static const char *skipitem(const char **, va_list *, int); 97 98int 99PyArg_Parse(PyObject *args, const char *format, ...) 100{ 101 int retval; 102 va_list va; 103 104 va_start(va, format); 105 retval = vgetargs1(args, format, &va, FLAG_COMPAT); 106 va_end(va); 107 return retval; 108} 109 110PyAPI_FUNC(int) 111_PyArg_Parse_SizeT(PyObject *args, const char *format, ...) 112{ 113 int retval; 114 va_list va; 115 116 va_start(va, format); 117 retval = vgetargs1(args, format, &va, FLAG_COMPAT|FLAG_SIZE_T); 118 va_end(va); 119 return retval; 120} 121 122 123int 124PyArg_ParseTuple(PyObject *args, const char *format, ...) 125{ 126 int retval; 127 va_list va; 128 129 va_start(va, format); 130 retval = vgetargs1(args, format, &va, 0); 131 va_end(va); 132 return retval; 133} 134 135PyAPI_FUNC(int) 136_PyArg_ParseTuple_SizeT(PyObject *args, const char *format, ...) 137{ 138 int retval; 139 va_list va; 140 141 va_start(va, format); 142 retval = vgetargs1(args, format, &va, FLAG_SIZE_T); 143 va_end(va); 144 return retval; 145} 146 147 148int 149_PyArg_ParseStack(PyObject *const *args, Py_ssize_t nargs, const char *format, ...) 150{ 151 int retval; 152 va_list va; 153 154 va_start(va, format); 155 retval = vgetargs1_impl(NULL, args, nargs, format, &va, 0); 156 va_end(va); 157 return retval; 158} 159 160PyAPI_FUNC(int) 161_PyArg_ParseStack_SizeT(PyObject *const *args, Py_ssize_t nargs, const char *format, ...) 162{ 163 int retval; 164 va_list va; 165 166 va_start(va, format); 167 retval = vgetargs1_impl(NULL, args, nargs, format, &va, FLAG_SIZE_T); 168 va_end(va); 169 return retval; 170} 171 172 173int 174PyArg_VaParse(PyObject *args, const char *format, va_list va) 175{ 176 va_list lva; 177 int retval; 178 179 va_copy(lva, va); 180 181 retval = vgetargs1(args, format, &lva, 0); 182 va_end(lva); 183 return retval; 184} 185 186PyAPI_FUNC(int) 187_PyArg_VaParse_SizeT(PyObject *args, const char *format, va_list va) 188{ 189 va_list lva; 190 int retval; 191 192 va_copy(lva, va); 193 194 retval = vgetargs1(args, format, &lva, FLAG_SIZE_T); 195 va_end(lva); 196 return retval; 197} 198 199 200/* Handle cleanup of allocated memory in case of exception */ 201 202static int 203cleanup_ptr(PyObject *self, void *ptr) 204{ 205 void **pptr = (void **)ptr; 206 PyMem_Free(*pptr); 207 *pptr = NULL; 208 return 0; 209} 210 211static int 212cleanup_buffer(PyObject *self, void *ptr) 213{ 214 Py_buffer *buf = (Py_buffer *)ptr; 215 if (buf) { 216 PyBuffer_Release(buf); 217 } 218 return 0; 219} 220 221static int 222addcleanup(void *ptr, freelist_t *freelist, destr_t destructor) 223{ 224 int index; 225 226 index = freelist->first_available; 227 freelist->first_available += 1; 228 229 freelist->entries[index].item = ptr; 230 freelist->entries[index].destructor = destructor; 231 232 return 0; 233} 234 235static int 236cleanreturn(int retval, freelist_t *freelist) 237{ 238 int index; 239 240 if (retval == 0) { 241 /* A failure occurred, therefore execute all of the cleanup 242 functions. 243 */ 244 for (index = 0; index < freelist->first_available; ++index) { 245 freelist->entries[index].destructor(NULL, 246 freelist->entries[index].item); 247 } 248 } 249 if (freelist->entries_malloced) 250 PyMem_Free(freelist->entries); 251 return retval; 252} 253 254 255static int 256vgetargs1_impl(PyObject *compat_args, PyObject *const *stack, Py_ssize_t nargs, const char *format, 257 va_list *p_va, int flags) 258{ 259 char msgbuf[256]; 260 int levels[32]; 261 const char *fname = NULL; 262 const char *message = NULL; 263 int min = -1; 264 int max = 0; 265 int level = 0; 266 int endfmt = 0; 267 const char *formatsave = format; 268 Py_ssize_t i; 269 const char *msg; 270 int compat = flags & FLAG_COMPAT; 271 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; 272 freelist_t freelist; 273 274 assert(nargs == 0 || stack != NULL); 275 276 freelist.entries = static_entries; 277 freelist.first_available = 0; 278 freelist.entries_malloced = 0; 279 280 flags = flags & ~FLAG_COMPAT; 281 282 while (endfmt == 0) { 283 int c = *format++; 284 switch (c) { 285 case '(': 286 if (level == 0) 287 max++; 288 level++; 289 if (level >= 30) 290 Py_FatalError("too many tuple nesting levels " 291 "in argument format string"); 292 break; 293 case ')': 294 if (level == 0) 295 Py_FatalError("excess ')' in getargs format"); 296 else 297 level--; 298 break; 299 case '\0': 300 endfmt = 1; 301 break; 302 case ':': 303 fname = format; 304 endfmt = 1; 305 break; 306 case ';': 307 message = format; 308 endfmt = 1; 309 break; 310 case '|': 311 if (level == 0) 312 min = max; 313 break; 314 default: 315 if (level == 0) { 316 if (Py_ISALPHA(c)) 317 if (c != 'e') /* skip encoded */ 318 max++; 319 } 320 break; 321 } 322 } 323 324 if (level != 0) 325 Py_FatalError(/* '(' */ "missing ')' in getargs format"); 326 327 if (min < 0) 328 min = max; 329 330 format = formatsave; 331 332 if (max > STATIC_FREELIST_ENTRIES) { 333 freelist.entries = PyMem_NEW(freelistentry_t, max); 334 if (freelist.entries == NULL) { 335 PyErr_NoMemory(); 336 return 0; 337 } 338 freelist.entries_malloced = 1; 339 } 340 341 if (compat) { 342 if (max == 0) { 343 if (compat_args == NULL) 344 return 1; 345 PyErr_Format(PyExc_TypeError, 346 "%.200s%s takes no arguments", 347 fname==NULL ? "function" : fname, 348 fname==NULL ? "" : "()"); 349 return cleanreturn(0, &freelist); 350 } 351 else if (min == 1 && max == 1) { 352 if (compat_args == NULL) { 353 PyErr_Format(PyExc_TypeError, 354 "%.200s%s takes at least one argument", 355 fname==NULL ? "function" : fname, 356 fname==NULL ? "" : "()"); 357 return cleanreturn(0, &freelist); 358 } 359 msg = convertitem(compat_args, &format, p_va, flags, levels, 360 msgbuf, sizeof(msgbuf), &freelist); 361 if (msg == NULL) 362 return cleanreturn(1, &freelist); 363 seterror(levels[0], msg, levels+1, fname, message); 364 return cleanreturn(0, &freelist); 365 } 366 else { 367 PyErr_SetString(PyExc_SystemError, 368 "old style getargs format uses new features"); 369 return cleanreturn(0, &freelist); 370 } 371 } 372 373 if (nargs < min || max < nargs) { 374 if (message == NULL) 375 PyErr_Format(PyExc_TypeError, 376 "%.150s%s takes %s %d argument%s (%zd given)", 377 fname==NULL ? "function" : fname, 378 fname==NULL ? "" : "()", 379 min==max ? "exactly" 380 : nargs < min ? "at least" : "at most", 381 nargs < min ? min : max, 382 (nargs < min ? min : max) == 1 ? "" : "s", 383 nargs); 384 else 385 PyErr_SetString(PyExc_TypeError, message); 386 return cleanreturn(0, &freelist); 387 } 388 389 for (i = 0; i < nargs; i++) { 390 if (*format == '|') 391 format++; 392 msg = convertitem(stack[i], &format, p_va, 393 flags, levels, msgbuf, 394 sizeof(msgbuf), &freelist); 395 if (msg) { 396 seterror(i+1, msg, levels, fname, message); 397 return cleanreturn(0, &freelist); 398 } 399 } 400 401 if (*format != '\0' && !Py_ISALPHA(*format) && 402 *format != '(' && 403 *format != '|' && *format != ':' && *format != ';') { 404 PyErr_Format(PyExc_SystemError, 405 "bad format string: %.200s", formatsave); 406 return cleanreturn(0, &freelist); 407 } 408 409 return cleanreturn(1, &freelist); 410} 411 412static int 413vgetargs1(PyObject *args, const char *format, va_list *p_va, int flags) 414{ 415 PyObject **stack; 416 Py_ssize_t nargs; 417 418 if (!(flags & FLAG_COMPAT)) { 419 assert(args != NULL); 420 421 if (!PyTuple_Check(args)) { 422 PyErr_SetString(PyExc_SystemError, 423 "new style getargs format but argument is not a tuple"); 424 return 0; 425 } 426 427 stack = _PyTuple_ITEMS(args); 428 nargs = PyTuple_GET_SIZE(args); 429 } 430 else { 431 stack = NULL; 432 nargs = 0; 433 } 434 435 return vgetargs1_impl(args, stack, nargs, format, p_va, flags); 436} 437 438 439static void 440seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname, 441 const char *message) 442{ 443 char buf[512]; 444 int i; 445 char *p = buf; 446 447 if (PyErr_Occurred()) 448 return; 449 else if (message == NULL) { 450 if (fname != NULL) { 451 PyOS_snprintf(p, sizeof(buf), "%.200s() ", fname); 452 p += strlen(p); 453 } 454 if (iarg != 0) { 455 PyOS_snprintf(p, sizeof(buf) - (p - buf), 456 "argument %zd", iarg); 457 i = 0; 458 p += strlen(p); 459 while (i < 32 && levels[i] > 0 && (int)(p-buf) < 220) { 460 PyOS_snprintf(p, sizeof(buf) - (p - buf), 461 ", item %d", levels[i]-1); 462 p += strlen(p); 463 i++; 464 } 465 } 466 else { 467 PyOS_snprintf(p, sizeof(buf) - (p - buf), "argument"); 468 p += strlen(p); 469 } 470 PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg); 471 message = buf; 472 } 473 if (msg[0] == '(') { 474 PyErr_SetString(PyExc_SystemError, message); 475 } 476 else { 477 PyErr_SetString(PyExc_TypeError, message); 478 } 479} 480 481 482/* Convert a tuple argument. 483 On entry, *p_format points to the character _after_ the opening '('. 484 On successful exit, *p_format points to the closing ')'. 485 If successful: 486 *p_format and *p_va are updated, 487 *levels and *msgbuf are untouched, 488 and NULL is returned. 489 If the argument is invalid: 490 *p_format is unchanged, 491 *p_va is undefined, 492 *levels is a 0-terminated list of item numbers, 493 *msgbuf contains an error message, whose format is: 494 "must be <typename1>, not <typename2>", where: 495 <typename1> is the name of the expected type, and 496 <typename2> is the name of the actual type, 497 and msgbuf is returned. 498*/ 499 500static const char * 501converttuple(PyObject *arg, const char **p_format, va_list *p_va, int flags, 502 int *levels, char *msgbuf, size_t bufsize, int toplevel, 503 freelist_t *freelist) 504{ 505 int level = 0; 506 int n = 0; 507 const char *format = *p_format; 508 int i; 509 Py_ssize_t len; 510 511 for (;;) { 512 int c = *format++; 513 if (c == '(') { 514 if (level == 0) 515 n++; 516 level++; 517 } 518 else if (c == ')') { 519 if (level == 0) 520 break; 521 level--; 522 } 523 else if (c == ':' || c == ';' || c == '\0') 524 break; 525 else if (level == 0 && Py_ISALPHA(c)) 526 n++; 527 } 528 529 if (!PySequence_Check(arg) || PyBytes_Check(arg)) { 530 levels[0] = 0; 531 PyOS_snprintf(msgbuf, bufsize, 532 toplevel ? "expected %d arguments, not %.50s" : 533 "must be %d-item sequence, not %.50s", 534 n, 535 arg == Py_None ? "None" : Py_TYPE(arg)->tp_name); 536 return msgbuf; 537 } 538 539 len = PySequence_Size(arg); 540 if (len != n) { 541 levels[0] = 0; 542 if (toplevel) { 543 PyOS_snprintf(msgbuf, bufsize, 544 "expected %d argument%s, not %zd", 545 n, 546 n == 1 ? "" : "s", 547 len); 548 } 549 else { 550 PyOS_snprintf(msgbuf, bufsize, 551 "must be sequence of length %d, not %zd", 552 n, len); 553 } 554 return msgbuf; 555 } 556 557 format = *p_format; 558 for (i = 0; i < n; i++) { 559 const char *msg; 560 PyObject *item; 561 item = PySequence_GetItem(arg, i); 562 if (item == NULL) { 563 PyErr_Clear(); 564 levels[0] = i+1; 565 levels[1] = 0; 566 strncpy(msgbuf, "is not retrievable", bufsize); 567 return msgbuf; 568 } 569 msg = convertitem(item, &format, p_va, flags, levels+1, 570 msgbuf, bufsize, freelist); 571 /* PySequence_GetItem calls tp->sq_item, which INCREFs */ 572 Py_XDECREF(item); 573 if (msg != NULL) { 574 levels[0] = i+1; 575 return msg; 576 } 577 } 578 579 *p_format = format; 580 return NULL; 581} 582 583 584/* Convert a single item. */ 585 586static const char * 587convertitem(PyObject *arg, const char **p_format, va_list *p_va, int flags, 588 int *levels, char *msgbuf, size_t bufsize, freelist_t *freelist) 589{ 590 const char *msg; 591 const char *format = *p_format; 592 593 if (*format == '(' /* ')' */) { 594 format++; 595 msg = converttuple(arg, &format, p_va, flags, levels, msgbuf, 596 bufsize, 0, freelist); 597 if (msg == NULL) 598 format++; 599 } 600 else { 601 msg = convertsimple(arg, &format, p_va, flags, 602 msgbuf, bufsize, freelist); 603 if (msg != NULL) 604 levels[0] = 0; 605 } 606 if (msg == NULL) 607 *p_format = format; 608 return msg; 609} 610 611 612 613/* Format an error message generated by convertsimple(). 614 displayname must be UTF-8 encoded. 615*/ 616 617void 618_PyArg_BadArgument(const char *fname, const char *displayname, 619 const char *expected, PyObject *arg) 620{ 621 PyErr_Format(PyExc_TypeError, 622 "%.200s() %.200s must be %.50s, not %.50s", 623 fname, displayname, expected, 624 arg == Py_None ? "None" : Py_TYPE(arg)->tp_name); 625} 626 627static const char * 628converterr(const char *expected, PyObject *arg, char *msgbuf, size_t bufsize) 629{ 630 assert(expected != NULL); 631 assert(arg != NULL); 632 if (expected[0] == '(') { 633 PyOS_snprintf(msgbuf, bufsize, 634 "%.100s", expected); 635 } 636 else { 637 PyOS_snprintf(msgbuf, bufsize, 638 "must be %.50s, not %.50s", expected, 639 arg == Py_None ? "None" : Py_TYPE(arg)->tp_name); 640 } 641 return msgbuf; 642} 643 644#define CONV_UNICODE "(unicode conversion error)" 645 646/* Convert a non-tuple argument. Return NULL if conversion went OK, 647 or a string with a message describing the failure. The message is 648 formatted as "must be <desired type>, not <actual type>". 649 When failing, an exception may or may not have been raised. 650 Don't call if a tuple is expected. 651 652 When you add new format codes, please don't forget poor skipitem() below. 653*/ 654 655static const char * 656convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags, 657 char *msgbuf, size_t bufsize, freelist_t *freelist) 658{ 659#define RETURN_ERR_OCCURRED return msgbuf 660 /* For # codes */ 661#define REQUIRE_PY_SSIZE_T_CLEAN \ 662 if (!(flags & FLAG_SIZE_T)) { \ 663 PyErr_SetString(PyExc_SystemError, \ 664 "PY_SSIZE_T_CLEAN macro must be defined for '#' formats"); \ 665 RETURN_ERR_OCCURRED; \ 666 } 667 668 const char *format = *p_format; 669 char c = *format++; 670 const char *sarg; 671 672 switch (c) { 673 674 case 'b': { /* unsigned byte -- very short int */ 675 char *p = va_arg(*p_va, char *); 676 long ival = PyLong_AsLong(arg); 677 if (ival == -1 && PyErr_Occurred()) 678 RETURN_ERR_OCCURRED; 679 else if (ival < 0) { 680 PyErr_SetString(PyExc_OverflowError, 681 "unsigned byte integer is less than minimum"); 682 RETURN_ERR_OCCURRED; 683 } 684 else if (ival > UCHAR_MAX) { 685 PyErr_SetString(PyExc_OverflowError, 686 "unsigned byte integer is greater than maximum"); 687 RETURN_ERR_OCCURRED; 688 } 689 else 690 *p = (unsigned char) ival; 691 break; 692 } 693 694 case 'B': {/* byte sized bitfield - both signed and unsigned 695 values allowed */ 696 char *p = va_arg(*p_va, char *); 697 unsigned long ival = PyLong_AsUnsignedLongMask(arg); 698 if (ival == (unsigned long)-1 && PyErr_Occurred()) 699 RETURN_ERR_OCCURRED; 700 else 701 *p = (unsigned char) ival; 702 break; 703 } 704 705 case 'h': {/* signed short int */ 706 short *p = va_arg(*p_va, short *); 707 long ival = PyLong_AsLong(arg); 708 if (ival == -1 && PyErr_Occurred()) 709 RETURN_ERR_OCCURRED; 710 else if (ival < SHRT_MIN) { 711 PyErr_SetString(PyExc_OverflowError, 712 "signed short integer is less than minimum"); 713 RETURN_ERR_OCCURRED; 714 } 715 else if (ival > SHRT_MAX) { 716 PyErr_SetString(PyExc_OverflowError, 717 "signed short integer is greater than maximum"); 718 RETURN_ERR_OCCURRED; 719 } 720 else 721 *p = (short) ival; 722 break; 723 } 724 725 case 'H': { /* short int sized bitfield, both signed and 726 unsigned allowed */ 727 unsigned short *p = va_arg(*p_va, unsigned short *); 728 unsigned long ival = PyLong_AsUnsignedLongMask(arg); 729 if (ival == (unsigned long)-1 && PyErr_Occurred()) 730 RETURN_ERR_OCCURRED; 731 else 732 *p = (unsigned short) ival; 733 break; 734 } 735 736 case 'i': {/* signed int */ 737 int *p = va_arg(*p_va, int *); 738 long ival = PyLong_AsLong(arg); 739 if (ival == -1 && PyErr_Occurred()) 740 RETURN_ERR_OCCURRED; 741 else if (ival > INT_MAX) { 742 PyErr_SetString(PyExc_OverflowError, 743 "signed integer is greater than maximum"); 744 RETURN_ERR_OCCURRED; 745 } 746 else if (ival < INT_MIN) { 747 PyErr_SetString(PyExc_OverflowError, 748 "signed integer is less than minimum"); 749 RETURN_ERR_OCCURRED; 750 } 751 else 752 *p = ival; 753 break; 754 } 755 756 case 'I': { /* int sized bitfield, both signed and 757 unsigned allowed */ 758 unsigned int *p = va_arg(*p_va, unsigned int *); 759 unsigned long ival = PyLong_AsUnsignedLongMask(arg); 760 if (ival == (unsigned long)-1 && PyErr_Occurred()) 761 RETURN_ERR_OCCURRED; 762 else 763 *p = (unsigned int) ival; 764 break; 765 } 766 767 case 'n': /* Py_ssize_t */ 768 { 769 PyObject *iobj; 770 Py_ssize_t *p = va_arg(*p_va, Py_ssize_t *); 771 Py_ssize_t ival = -1; 772 iobj = _PyNumber_Index(arg); 773 if (iobj != NULL) { 774 ival = PyLong_AsSsize_t(iobj); 775 Py_DECREF(iobj); 776 } 777 if (ival == -1 && PyErr_Occurred()) 778 RETURN_ERR_OCCURRED; 779 *p = ival; 780 break; 781 } 782 case 'l': {/* long int */ 783 long *p = va_arg(*p_va, long *); 784 long ival = PyLong_AsLong(arg); 785 if (ival == -1 && PyErr_Occurred()) 786 RETURN_ERR_OCCURRED; 787 else 788 *p = ival; 789 break; 790 } 791 792 case 'k': { /* long sized bitfield */ 793 unsigned long *p = va_arg(*p_va, unsigned long *); 794 unsigned long ival; 795 if (PyLong_Check(arg)) 796 ival = PyLong_AsUnsignedLongMask(arg); 797 else 798 return converterr("int", arg, msgbuf, bufsize); 799 *p = ival; 800 break; 801 } 802 803 case 'L': {/* long long */ 804 long long *p = va_arg( *p_va, long long * ); 805 long long ival = PyLong_AsLongLong(arg); 806 if (ival == (long long)-1 && PyErr_Occurred()) 807 RETURN_ERR_OCCURRED; 808 else 809 *p = ival; 810 break; 811 } 812 813 case 'K': { /* long long sized bitfield */ 814 unsigned long long *p = va_arg(*p_va, unsigned long long *); 815 unsigned long long ival; 816 if (PyLong_Check(arg)) 817 ival = PyLong_AsUnsignedLongLongMask(arg); 818 else 819 return converterr("int", arg, msgbuf, bufsize); 820 *p = ival; 821 break; 822 } 823 824 case 'f': {/* float */ 825 float *p = va_arg(*p_va, float *); 826 double dval = PyFloat_AsDouble(arg); 827 if (dval == -1.0 && PyErr_Occurred()) 828 RETURN_ERR_OCCURRED; 829 else 830 *p = (float) dval; 831 break; 832 } 833 834 case 'd': {/* double */ 835 double *p = va_arg(*p_va, double *); 836 double dval = PyFloat_AsDouble(arg); 837 if (dval == -1.0 && PyErr_Occurred()) 838 RETURN_ERR_OCCURRED; 839 else 840 *p = dval; 841 break; 842 } 843 844 case 'D': {/* complex double */ 845 Py_complex *p = va_arg(*p_va, Py_complex *); 846 Py_complex cval; 847 cval = PyComplex_AsCComplex(arg); 848 if (PyErr_Occurred()) 849 RETURN_ERR_OCCURRED; 850 else 851 *p = cval; 852 break; 853 } 854 855 case 'c': {/* char */ 856 char *p = va_arg(*p_va, char *); 857 if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1) 858 *p = PyBytes_AS_STRING(arg)[0]; 859 else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1) 860 *p = PyByteArray_AS_STRING(arg)[0]; 861 else 862 return converterr("a byte string of length 1", arg, msgbuf, bufsize); 863 break; 864 } 865 866 case 'C': {/* unicode char */ 867 int *p = va_arg(*p_va, int *); 868 int kind; 869 const void *data; 870 871 if (!PyUnicode_Check(arg)) 872 return converterr("a unicode character", arg, msgbuf, bufsize); 873 874 if (PyUnicode_READY(arg)) 875 RETURN_ERR_OCCURRED; 876 877 if (PyUnicode_GET_LENGTH(arg) != 1) 878 return converterr("a unicode character", arg, msgbuf, bufsize); 879 880 kind = PyUnicode_KIND(arg); 881 data = PyUnicode_DATA(arg); 882 *p = PyUnicode_READ(kind, data, 0); 883 break; 884 } 885 886 case 'p': {/* boolean *p*redicate */ 887 int *p = va_arg(*p_va, int *); 888 int val = PyObject_IsTrue(arg); 889 if (val > 0) 890 *p = 1; 891 else if (val == 0) 892 *p = 0; 893 else 894 RETURN_ERR_OCCURRED; 895 break; 896 } 897 898 /* XXX WAAAAH! 's', 'y', 'z', 'u', 'Z', 'e', 'w' codes all 899 need to be cleaned up! */ 900 901 case 'y': {/* any bytes-like object */ 902 void **p = (void **)va_arg(*p_va, char **); 903 const char *buf; 904 Py_ssize_t count; 905 if (*format == '*') { 906 if (getbuffer(arg, (Py_buffer*)p, &buf) < 0) 907 return converterr(buf, arg, msgbuf, bufsize); 908 format++; 909 if (addcleanup(p, freelist, cleanup_buffer)) { 910 return converterr( 911 "(cleanup problem)", 912 arg, msgbuf, bufsize); 913 } 914 break; 915 } 916 count = convertbuffer(arg, (const void **)p, &buf); 917 if (count < 0) 918 return converterr(buf, arg, msgbuf, bufsize); 919 if (*format == '#') { 920 REQUIRE_PY_SSIZE_T_CLEAN; 921 Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*); 922 *psize = count; 923 format++; 924 } else { 925 if (strlen(*p) != (size_t)count) { 926 PyErr_SetString(PyExc_ValueError, "embedded null byte"); 927 RETURN_ERR_OCCURRED; 928 } 929 } 930 break; 931 } 932 933 case 's': /* text string or bytes-like object */ 934 case 'z': /* text string, bytes-like object or None */ 935 { 936 if (*format == '*') { 937 /* "s*" or "z*" */ 938 Py_buffer *p = (Py_buffer *)va_arg(*p_va, Py_buffer *); 939 940 if (c == 'z' && arg == Py_None) 941 PyBuffer_FillInfo(p, NULL, NULL, 0, 1, 0); 942 else if (PyUnicode_Check(arg)) { 943 Py_ssize_t len; 944 sarg = PyUnicode_AsUTF8AndSize(arg, &len); 945 if (sarg == NULL) 946 return converterr(CONV_UNICODE, 947 arg, msgbuf, bufsize); 948 PyBuffer_FillInfo(p, arg, (void *)sarg, len, 1, 0); 949 } 950 else { /* any bytes-like object */ 951 const char *buf; 952 if (getbuffer(arg, p, &buf) < 0) 953 return converterr(buf, arg, msgbuf, bufsize); 954 } 955 if (addcleanup(p, freelist, cleanup_buffer)) { 956 return converterr( 957 "(cleanup problem)", 958 arg, msgbuf, bufsize); 959 } 960 format++; 961 } else if (*format == '#') { /* a string or read-only bytes-like object */ 962 /* "s#" or "z#" */ 963 const void **p = (const void **)va_arg(*p_va, const char **); 964 REQUIRE_PY_SSIZE_T_CLEAN; 965 Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*); 966 967 if (c == 'z' && arg == Py_None) { 968 *p = NULL; 969 *psize = 0; 970 } 971 else if (PyUnicode_Check(arg)) { 972 Py_ssize_t len; 973 sarg = PyUnicode_AsUTF8AndSize(arg, &len); 974 if (sarg == NULL) 975 return converterr(CONV_UNICODE, 976 arg, msgbuf, bufsize); 977 *p = sarg; 978 *psize = len; 979 } 980 else { /* read-only bytes-like object */ 981 /* XXX Really? */ 982 const char *buf; 983 Py_ssize_t count = convertbuffer(arg, p, &buf); 984 if (count < 0) 985 return converterr(buf, arg, msgbuf, bufsize); 986 *psize = count; 987 } 988 format++; 989 } else { 990 /* "s" or "z" */ 991 const char **p = va_arg(*p_va, const char **); 992 Py_ssize_t len; 993 sarg = NULL; 994 995 if (c == 'z' && arg == Py_None) 996 *p = NULL; 997 else if (PyUnicode_Check(arg)) { 998 sarg = PyUnicode_AsUTF8AndSize(arg, &len); 999 if (sarg == NULL) 1000 return converterr(CONV_UNICODE, 1001 arg, msgbuf, bufsize); 1002 if (strlen(sarg) != (size_t)len) { 1003 PyErr_SetString(PyExc_ValueError, "embedded null character"); 1004 RETURN_ERR_OCCURRED; 1005 } 1006 *p = sarg; 1007 } 1008 else 1009 return converterr(c == 'z' ? "str or None" : "str", 1010 arg, msgbuf, bufsize); 1011 } 1012 break; 1013 } 1014 1015 case 'u': /* raw unicode buffer (Py_UNICODE *) */ 1016 case 'Z': /* raw unicode buffer or None */ 1017 { 1018 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, 1019 "getargs: The '%c' format is deprecated. Use 'U' instead.", c)) { 1020 RETURN_ERR_OCCURRED; 1021 } 1022_Py_COMP_DIAG_PUSH 1023_Py_COMP_DIAG_IGNORE_DEPR_DECLS 1024 Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **); 1025 1026 if (*format == '#') { 1027 /* "u#" or "Z#" */ 1028 REQUIRE_PY_SSIZE_T_CLEAN; 1029 Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*); 1030 1031 if (c == 'Z' && arg == Py_None) { 1032 *p = NULL; 1033 *psize = 0; 1034 } 1035 else if (PyUnicode_Check(arg)) { 1036 Py_ssize_t len; 1037 *p = PyUnicode_AsUnicodeAndSize(arg, &len); 1038 if (*p == NULL) 1039 RETURN_ERR_OCCURRED; 1040 *psize = len; 1041 } 1042 else 1043 return converterr(c == 'Z' ? "str or None" : "str", 1044 arg, msgbuf, bufsize); 1045 format++; 1046 } else { 1047 /* "u" or "Z" */ 1048 if (c == 'Z' && arg == Py_None) 1049 *p = NULL; 1050 else if (PyUnicode_Check(arg)) { 1051 Py_ssize_t len; 1052 *p = PyUnicode_AsUnicodeAndSize(arg, &len); 1053 if (*p == NULL) 1054 RETURN_ERR_OCCURRED; 1055 if (wcslen(*p) != (size_t)len) { 1056 PyErr_SetString(PyExc_ValueError, "embedded null character"); 1057 RETURN_ERR_OCCURRED; 1058 } 1059 } else 1060 return converterr(c == 'Z' ? "str or None" : "str", 1061 arg, msgbuf, bufsize); 1062 } 1063 break; 1064_Py_COMP_DIAG_POP 1065 } 1066 1067 case 'e': {/* encoded string */ 1068 char **buffer; 1069 const char *encoding; 1070 PyObject *s; 1071 int recode_strings; 1072 Py_ssize_t size; 1073 const char *ptr; 1074 1075 /* Get 'e' parameter: the encoding name */ 1076 encoding = (const char *)va_arg(*p_va, const char *); 1077 if (encoding == NULL) 1078 encoding = PyUnicode_GetDefaultEncoding(); 1079 1080 /* Get output buffer parameter: 1081 's' (recode all objects via Unicode) or 1082 't' (only recode non-string objects) 1083 */ 1084 if (*format == 's') 1085 recode_strings = 1; 1086 else if (*format == 't') 1087 recode_strings = 0; 1088 else 1089 return converterr( 1090 "(unknown parser marker combination)", 1091 arg, msgbuf, bufsize); 1092 buffer = (char **)va_arg(*p_va, char **); 1093 format++; 1094 if (buffer == NULL) 1095 return converterr("(buffer is NULL)", 1096 arg, msgbuf, bufsize); 1097 1098 /* Encode object */ 1099 if (!recode_strings && 1100 (PyBytes_Check(arg) || PyByteArray_Check(arg))) { 1101 s = arg; 1102 Py_INCREF(s); 1103 if (PyBytes_Check(arg)) { 1104 size = PyBytes_GET_SIZE(s); 1105 ptr = PyBytes_AS_STRING(s); 1106 } 1107 else { 1108 size = PyByteArray_GET_SIZE(s); 1109 ptr = PyByteArray_AS_STRING(s); 1110 } 1111 } 1112 else if (PyUnicode_Check(arg)) { 1113 /* Encode object; use default error handling */ 1114 s = PyUnicode_AsEncodedString(arg, 1115 encoding, 1116 NULL); 1117 if (s == NULL) 1118 return converterr("(encoding failed)", 1119 arg, msgbuf, bufsize); 1120 assert(PyBytes_Check(s)); 1121 size = PyBytes_GET_SIZE(s); 1122 ptr = PyBytes_AS_STRING(s); 1123 if (ptr == NULL) 1124 ptr = ""; 1125 } 1126 else { 1127 return converterr( 1128 recode_strings ? "str" : "str, bytes or bytearray", 1129 arg, msgbuf, bufsize); 1130 } 1131 1132 /* Write output; output is guaranteed to be 0-terminated */ 1133 if (*format == '#') { 1134 /* Using buffer length parameter '#': 1135 1136 - if *buffer is NULL, a new buffer of the 1137 needed size is allocated and the data 1138 copied into it; *buffer is updated to point 1139 to the new buffer; the caller is 1140 responsible for PyMem_Free()ing it after 1141 usage 1142 1143 - if *buffer is not NULL, the data is 1144 copied to *buffer; *buffer_len has to be 1145 set to the size of the buffer on input; 1146 buffer overflow is signalled with an error; 1147 buffer has to provide enough room for the 1148 encoded string plus the trailing 0-byte 1149 1150 - in both cases, *buffer_len is updated to 1151 the size of the buffer /excluding/ the 1152 trailing 0-byte 1153 1154 */ 1155 REQUIRE_PY_SSIZE_T_CLEAN; 1156 Py_ssize_t *psize = va_arg(*p_va, Py_ssize_t*); 1157 1158 format++; 1159 if (psize == NULL) { 1160 Py_DECREF(s); 1161 return converterr( 1162 "(buffer_len is NULL)", 1163 arg, msgbuf, bufsize); 1164 } 1165 if (*buffer == NULL) { 1166 *buffer = PyMem_NEW(char, size + 1); 1167 if (*buffer == NULL) { 1168 Py_DECREF(s); 1169 PyErr_NoMemory(); 1170 RETURN_ERR_OCCURRED; 1171 } 1172 if (addcleanup(buffer, freelist, cleanup_ptr)) { 1173 Py_DECREF(s); 1174 return converterr( 1175 "(cleanup problem)", 1176 arg, msgbuf, bufsize); 1177 } 1178 } else { 1179 if (size + 1 > *psize) { 1180 Py_DECREF(s); 1181 PyErr_Format(PyExc_ValueError, 1182 "encoded string too long " 1183 "(%zd, maximum length %zd)", 1184 (Py_ssize_t)size, (Py_ssize_t)(*psize - 1)); 1185 RETURN_ERR_OCCURRED; 1186 } 1187 } 1188 memcpy(*buffer, ptr, size+1); 1189 1190 *psize = size; 1191 } 1192 else { 1193 /* Using a 0-terminated buffer: 1194 1195 - the encoded string has to be 0-terminated 1196 for this variant to work; if it is not, an 1197 error raised 1198 1199 - a new buffer of the needed size is 1200 allocated and the data copied into it; 1201 *buffer is updated to point to the new 1202 buffer; the caller is responsible for 1203 PyMem_Free()ing it after usage 1204 1205 */ 1206 if ((Py_ssize_t)strlen(ptr) != size) { 1207 Py_DECREF(s); 1208 return converterr( 1209 "encoded string without null bytes", 1210 arg, msgbuf, bufsize); 1211 } 1212 *buffer = PyMem_NEW(char, size + 1); 1213 if (*buffer == NULL) { 1214 Py_DECREF(s); 1215 PyErr_NoMemory(); 1216 RETURN_ERR_OCCURRED; 1217 } 1218 if (addcleanup(buffer, freelist, cleanup_ptr)) { 1219 Py_DECREF(s); 1220 return converterr("(cleanup problem)", 1221 arg, msgbuf, bufsize); 1222 } 1223 memcpy(*buffer, ptr, size+1); 1224 } 1225 Py_DECREF(s); 1226 break; 1227 } 1228 1229 case 'S': { /* PyBytes object */ 1230 PyObject **p = va_arg(*p_va, PyObject **); 1231 if (PyBytes_Check(arg)) 1232 *p = arg; 1233 else 1234 return converterr("bytes", arg, msgbuf, bufsize); 1235 break; 1236 } 1237 1238 case 'Y': { /* PyByteArray object */ 1239 PyObject **p = va_arg(*p_va, PyObject **); 1240 if (PyByteArray_Check(arg)) 1241 *p = arg; 1242 else 1243 return converterr("bytearray", arg, msgbuf, bufsize); 1244 break; 1245 } 1246 1247 case 'U': { /* PyUnicode object */ 1248 PyObject **p = va_arg(*p_va, PyObject **); 1249 if (PyUnicode_Check(arg)) { 1250 if (PyUnicode_READY(arg) == -1) 1251 RETURN_ERR_OCCURRED; 1252 *p = arg; 1253 } 1254 else 1255 return converterr("str", arg, msgbuf, bufsize); 1256 break; 1257 } 1258 1259 case 'O': { /* object */ 1260 PyTypeObject *type; 1261 PyObject **p; 1262 if (*format == '!') { 1263 type = va_arg(*p_va, PyTypeObject*); 1264 p = va_arg(*p_va, PyObject **); 1265 format++; 1266 if (PyType_IsSubtype(Py_TYPE(arg), type)) 1267 *p = arg; 1268 else 1269 return converterr(type->tp_name, arg, msgbuf, bufsize); 1270 1271 } 1272 else if (*format == '&') { 1273 typedef int (*converter)(PyObject *, void *); 1274 converter convert = va_arg(*p_va, converter); 1275 void *addr = va_arg(*p_va, void *); 1276 int res; 1277 format++; 1278 if (! (res = (*convert)(arg, addr))) 1279 return converterr("(unspecified)", 1280 arg, msgbuf, bufsize); 1281 if (res == Py_CLEANUP_SUPPORTED && 1282 addcleanup(addr, freelist, convert) == -1) 1283 return converterr("(cleanup problem)", 1284 arg, msgbuf, bufsize); 1285 } 1286 else { 1287 p = va_arg(*p_va, PyObject **); 1288 *p = arg; 1289 } 1290 break; 1291 } 1292 1293 1294 case 'w': { /* "w*": memory buffer, read-write access */ 1295 void **p = va_arg(*p_va, void **); 1296 1297 if (*format != '*') 1298 return converterr( 1299 "(invalid use of 'w' format character)", 1300 arg, msgbuf, bufsize); 1301 format++; 1302 1303 /* Caller is interested in Py_buffer, and the object 1304 supports it directly. */ 1305 if (PyObject_GetBuffer(arg, (Py_buffer*)p, PyBUF_WRITABLE) < 0) { 1306 PyErr_Clear(); 1307 return converterr("read-write bytes-like object", 1308 arg, msgbuf, bufsize); 1309 } 1310 if (!PyBuffer_IsContiguous((Py_buffer*)p, 'C')) { 1311 PyBuffer_Release((Py_buffer*)p); 1312 return converterr("contiguous buffer", arg, msgbuf, bufsize); 1313 } 1314 if (addcleanup(p, freelist, cleanup_buffer)) { 1315 return converterr( 1316 "(cleanup problem)", 1317 arg, msgbuf, bufsize); 1318 } 1319 break; 1320 } 1321 1322 default: 1323 return converterr("(impossible<bad format char>)", arg, msgbuf, bufsize); 1324 1325 } 1326 1327 *p_format = format; 1328 return NULL; 1329 1330#undef REQUIRE_PY_SSIZE_T_CLEAN 1331#undef RETURN_ERR_OCCURRED 1332} 1333 1334static Py_ssize_t 1335convertbuffer(PyObject *arg, const void **p, const char **errmsg) 1336{ 1337 PyBufferProcs *pb = Py_TYPE(arg)->tp_as_buffer; 1338 Py_ssize_t count; 1339 Py_buffer view; 1340 1341 *errmsg = NULL; 1342 *p = NULL; 1343 if (pb != NULL && pb->bf_releasebuffer != NULL) { 1344 *errmsg = "read-only bytes-like object"; 1345 return -1; 1346 } 1347 1348 if (getbuffer(arg, &view, errmsg) < 0) 1349 return -1; 1350 count = view.len; 1351 *p = view.buf; 1352 PyBuffer_Release(&view); 1353 return count; 1354} 1355 1356static int 1357getbuffer(PyObject *arg, Py_buffer *view, const char **errmsg) 1358{ 1359 if (PyObject_GetBuffer(arg, view, PyBUF_SIMPLE) != 0) { 1360 *errmsg = "bytes-like object"; 1361 return -1; 1362 } 1363 if (!PyBuffer_IsContiguous(view, 'C')) { 1364 PyBuffer_Release(view); 1365 *errmsg = "contiguous buffer"; 1366 return -1; 1367 } 1368 return 0; 1369} 1370 1371/* Support for keyword arguments donated by 1372 Geoff Philbrick <philbric@delphi.hks.com> */ 1373 1374/* Return false (0) for error, else true. */ 1375int 1376PyArg_ParseTupleAndKeywords(PyObject *args, 1377 PyObject *keywords, 1378 const char *format, 1379 char **kwlist, ...) 1380{ 1381 int retval; 1382 va_list va; 1383 1384 if ((args == NULL || !PyTuple_Check(args)) || 1385 (keywords != NULL && !PyDict_Check(keywords)) || 1386 format == NULL || 1387 kwlist == NULL) 1388 { 1389 PyErr_BadInternalCall(); 1390 return 0; 1391 } 1392 1393 va_start(va, kwlist); 1394 retval = vgetargskeywords(args, keywords, format, kwlist, &va, 0); 1395 va_end(va); 1396 return retval; 1397} 1398 1399PyAPI_FUNC(int) 1400_PyArg_ParseTupleAndKeywords_SizeT(PyObject *args, 1401 PyObject *keywords, 1402 const char *format, 1403 char **kwlist, ...) 1404{ 1405 int retval; 1406 va_list va; 1407 1408 if ((args == NULL || !PyTuple_Check(args)) || 1409 (keywords != NULL && !PyDict_Check(keywords)) || 1410 format == NULL || 1411 kwlist == NULL) 1412 { 1413 PyErr_BadInternalCall(); 1414 return 0; 1415 } 1416 1417 va_start(va, kwlist); 1418 retval = vgetargskeywords(args, keywords, format, 1419 kwlist, &va, FLAG_SIZE_T); 1420 va_end(va); 1421 return retval; 1422} 1423 1424 1425int 1426PyArg_VaParseTupleAndKeywords(PyObject *args, 1427 PyObject *keywords, 1428 const char *format, 1429 char **kwlist, va_list va) 1430{ 1431 int retval; 1432 va_list lva; 1433 1434 if ((args == NULL || !PyTuple_Check(args)) || 1435 (keywords != NULL && !PyDict_Check(keywords)) || 1436 format == NULL || 1437 kwlist == NULL) 1438 { 1439 PyErr_BadInternalCall(); 1440 return 0; 1441 } 1442 1443 va_copy(lva, va); 1444 1445 retval = vgetargskeywords(args, keywords, format, kwlist, &lva, 0); 1446 va_end(lva); 1447 return retval; 1448} 1449 1450PyAPI_FUNC(int) 1451_PyArg_VaParseTupleAndKeywords_SizeT(PyObject *args, 1452 PyObject *keywords, 1453 const char *format, 1454 char **kwlist, va_list va) 1455{ 1456 int retval; 1457 va_list lva; 1458 1459 if ((args == NULL || !PyTuple_Check(args)) || 1460 (keywords != NULL && !PyDict_Check(keywords)) || 1461 format == NULL || 1462 kwlist == NULL) 1463 { 1464 PyErr_BadInternalCall(); 1465 return 0; 1466 } 1467 1468 va_copy(lva, va); 1469 1470 retval = vgetargskeywords(args, keywords, format, 1471 kwlist, &lva, FLAG_SIZE_T); 1472 va_end(lva); 1473 return retval; 1474} 1475 1476PyAPI_FUNC(int) 1477_PyArg_ParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords, 1478 struct _PyArg_Parser *parser, ...) 1479{ 1480 int retval; 1481 va_list va; 1482 1483 va_start(va, parser); 1484 retval = vgetargskeywordsfast(args, keywords, parser, &va, 0); 1485 va_end(va); 1486 return retval; 1487} 1488 1489PyAPI_FUNC(int) 1490_PyArg_ParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords, 1491 struct _PyArg_Parser *parser, ...) 1492{ 1493 int retval; 1494 va_list va; 1495 1496 va_start(va, parser); 1497 retval = vgetargskeywordsfast(args, keywords, parser, &va, FLAG_SIZE_T); 1498 va_end(va); 1499 return retval; 1500} 1501 1502PyAPI_FUNC(int) 1503_PyArg_ParseStackAndKeywords(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, 1504 struct _PyArg_Parser *parser, ...) 1505{ 1506 int retval; 1507 va_list va; 1508 1509 va_start(va, parser); 1510 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, 0); 1511 va_end(va); 1512 return retval; 1513} 1514 1515PyAPI_FUNC(int) 1516_PyArg_ParseStackAndKeywords_SizeT(PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames, 1517 struct _PyArg_Parser *parser, ...) 1518{ 1519 int retval; 1520 va_list va; 1521 1522 va_start(va, parser); 1523 retval = vgetargskeywordsfast_impl(args, nargs, NULL, kwnames, parser, &va, FLAG_SIZE_T); 1524 va_end(va); 1525 return retval; 1526} 1527 1528 1529PyAPI_FUNC(int) 1530_PyArg_VaParseTupleAndKeywordsFast(PyObject *args, PyObject *keywords, 1531 struct _PyArg_Parser *parser, va_list va) 1532{ 1533 int retval; 1534 va_list lva; 1535 1536 va_copy(lva, va); 1537 1538 retval = vgetargskeywordsfast(args, keywords, parser, &lva, 0); 1539 va_end(lva); 1540 return retval; 1541} 1542 1543PyAPI_FUNC(int) 1544_PyArg_VaParseTupleAndKeywordsFast_SizeT(PyObject *args, PyObject *keywords, 1545 struct _PyArg_Parser *parser, va_list va) 1546{ 1547 int retval; 1548 va_list lva; 1549 1550 va_copy(lva, va); 1551 1552 retval = vgetargskeywordsfast(args, keywords, parser, &lva, FLAG_SIZE_T); 1553 va_end(lva); 1554 return retval; 1555} 1556 1557static void 1558error_unexpected_keyword_arg(PyObject *kwargs, PyObject *kwnames, PyObject *kwtuple, const char *fname) 1559{ 1560 /* make sure there are no extraneous keyword arguments */ 1561 Py_ssize_t j = 0; 1562 while (1) { 1563 PyObject *keyword; 1564 if (kwargs != NULL) { 1565 if (!PyDict_Next(kwargs, &j, &keyword, NULL)) 1566 break; 1567 } 1568 else { 1569 if (j >= PyTuple_GET_SIZE(kwnames)) 1570 break; 1571 keyword = PyTuple_GET_ITEM(kwnames, j); 1572 j++; 1573 } 1574 if (!PyUnicode_Check(keyword)) { 1575 PyErr_SetString(PyExc_TypeError, 1576 "keywords must be strings"); 1577 return; 1578 } 1579 1580 int match = PySequence_Contains(kwtuple, keyword); 1581 if (match <= 0) { 1582 if (!match) { 1583 PyErr_Format(PyExc_TypeError, 1584 "'%S' is an invalid keyword " 1585 "argument for %.200s%s", 1586 keyword, 1587 (fname == NULL) ? "this function" : fname, 1588 (fname == NULL) ? "" : "()"); 1589 } 1590 return; 1591 } 1592 } 1593 /* Something wrong happened. There are extraneous keyword arguments, 1594 * but we don't know what. And we don't bother. */ 1595 PyErr_Format(PyExc_TypeError, 1596 "invalid keyword argument for %.200s%s", 1597 (fname == NULL) ? "this function" : fname, 1598 (fname == NULL) ? "" : "()"); 1599} 1600 1601int 1602PyArg_ValidateKeywordArguments(PyObject *kwargs) 1603{ 1604 if (!PyDict_Check(kwargs)) { 1605 PyErr_BadInternalCall(); 1606 return 0; 1607 } 1608 if (!_PyDict_HasOnlyStringKeys(kwargs)) { 1609 PyErr_SetString(PyExc_TypeError, 1610 "keywords must be strings"); 1611 return 0; 1612 } 1613 return 1; 1614} 1615 1616#define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') 1617 1618static int 1619vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, 1620 char **kwlist, va_list *p_va, int flags) 1621{ 1622 char msgbuf[512]; 1623 int levels[32]; 1624 const char *fname, *msg, *custom_msg; 1625 int min = INT_MAX; 1626 int max = INT_MAX; 1627 int i, pos, len; 1628 int skip = 0; 1629 Py_ssize_t nargs, nkwargs; 1630 PyObject *current_arg; 1631 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; 1632 freelist_t freelist; 1633 1634 freelist.entries = static_entries; 1635 freelist.first_available = 0; 1636 freelist.entries_malloced = 0; 1637 1638 assert(args != NULL && PyTuple_Check(args)); 1639 assert(kwargs == NULL || PyDict_Check(kwargs)); 1640 assert(format != NULL); 1641 assert(kwlist != NULL); 1642 assert(p_va != NULL); 1643 1644 /* grab the function name or custom error msg first (mutually exclusive) */ 1645 fname = strchr(format, ':'); 1646 if (fname) { 1647 fname++; 1648 custom_msg = NULL; 1649 } 1650 else { 1651 custom_msg = strchr(format,';'); 1652 if (custom_msg) 1653 custom_msg++; 1654 } 1655 1656 /* scan kwlist and count the number of positional-only parameters */ 1657 for (pos = 0; kwlist[pos] && !*kwlist[pos]; pos++) { 1658 } 1659 /* scan kwlist and get greatest possible nbr of args */ 1660 for (len = pos; kwlist[len]; len++) { 1661 if (!*kwlist[len]) { 1662 PyErr_SetString(PyExc_SystemError, 1663 "Empty keyword parameter name"); 1664 return cleanreturn(0, &freelist); 1665 } 1666 } 1667 1668 if (len > STATIC_FREELIST_ENTRIES) { 1669 freelist.entries = PyMem_NEW(freelistentry_t, len); 1670 if (freelist.entries == NULL) { 1671 PyErr_NoMemory(); 1672 return 0; 1673 } 1674 freelist.entries_malloced = 1; 1675 } 1676 1677 nargs = PyTuple_GET_SIZE(args); 1678 nkwargs = (kwargs == NULL) ? 0 : PyDict_GET_SIZE(kwargs); 1679 if (nargs + nkwargs > len) { 1680 /* Adding "keyword" (when nargs == 0) prevents producing wrong error 1681 messages in some special cases (see bpo-31229). */ 1682 PyErr_Format(PyExc_TypeError, 1683 "%.200s%s takes at most %d %sargument%s (%zd given)", 1684 (fname == NULL) ? "function" : fname, 1685 (fname == NULL) ? "" : "()", 1686 len, 1687 (nargs == 0) ? "keyword " : "", 1688 (len == 1) ? "" : "s", 1689 nargs + nkwargs); 1690 return cleanreturn(0, &freelist); 1691 } 1692 1693 /* convert tuple args and keyword args in same loop, using kwlist to drive process */ 1694 for (i = 0; i < len; i++) { 1695 if (*format == '|') { 1696 if (min != INT_MAX) { 1697 PyErr_SetString(PyExc_SystemError, 1698 "Invalid format string (| specified twice)"); 1699 return cleanreturn(0, &freelist); 1700 } 1701 1702 min = i; 1703 format++; 1704 1705 if (max != INT_MAX) { 1706 PyErr_SetString(PyExc_SystemError, 1707 "Invalid format string ($ before |)"); 1708 return cleanreturn(0, &freelist); 1709 } 1710 } 1711 if (*format == '$') { 1712 if (max != INT_MAX) { 1713 PyErr_SetString(PyExc_SystemError, 1714 "Invalid format string ($ specified twice)"); 1715 return cleanreturn(0, &freelist); 1716 } 1717 1718 max = i; 1719 format++; 1720 1721 if (max < pos) { 1722 PyErr_SetString(PyExc_SystemError, 1723 "Empty parameter name after $"); 1724 return cleanreturn(0, &freelist); 1725 } 1726 if (skip) { 1727 /* Now we know the minimal and the maximal numbers of 1728 * positional arguments and can raise an exception with 1729 * informative message (see below). */ 1730 break; 1731 } 1732 if (max < nargs) { 1733 if (max == 0) { 1734 PyErr_Format(PyExc_TypeError, 1735 "%.200s%s takes no positional arguments", 1736 (fname == NULL) ? "function" : fname, 1737 (fname == NULL) ? "" : "()"); 1738 } 1739 else { 1740 PyErr_Format(PyExc_TypeError, 1741 "%.200s%s takes %s %d positional argument%s" 1742 " (%zd given)", 1743 (fname == NULL) ? "function" : fname, 1744 (fname == NULL) ? "" : "()", 1745 (min != INT_MAX) ? "at most" : "exactly", 1746 max, 1747 max == 1 ? "" : "s", 1748 nargs); 1749 } 1750 return cleanreturn(0, &freelist); 1751 } 1752 } 1753 if (IS_END_OF_FORMAT(*format)) { 1754 PyErr_Format(PyExc_SystemError, 1755 "More keyword list entries (%d) than " 1756 "format specifiers (%d)", len, i); 1757 return cleanreturn(0, &freelist); 1758 } 1759 if (!skip) { 1760 if (i < nargs) { 1761 current_arg = PyTuple_GET_ITEM(args, i); 1762 } 1763 else if (nkwargs && i >= pos) { 1764 current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]); 1765 if (current_arg) { 1766 --nkwargs; 1767 } 1768 else if (PyErr_Occurred()) { 1769 return cleanreturn(0, &freelist); 1770 } 1771 } 1772 else { 1773 current_arg = NULL; 1774 } 1775 1776 if (current_arg) { 1777 msg = convertitem(current_arg, &format, p_va, flags, 1778 levels, msgbuf, sizeof(msgbuf), &freelist); 1779 if (msg) { 1780 seterror(i+1, msg, levels, fname, custom_msg); 1781 return cleanreturn(0, &freelist); 1782 } 1783 continue; 1784 } 1785 1786 if (i < min) { 1787 if (i < pos) { 1788 assert (min == INT_MAX); 1789 assert (max == INT_MAX); 1790 skip = 1; 1791 /* At that moment we still don't know the minimal and 1792 * the maximal numbers of positional arguments. Raising 1793 * an exception is deferred until we encounter | and $ 1794 * or the end of the format. */ 1795 } 1796 else { 1797 PyErr_Format(PyExc_TypeError, "%.200s%s missing required " 1798 "argument '%s' (pos %d)", 1799 (fname == NULL) ? "function" : fname, 1800 (fname == NULL) ? "" : "()", 1801 kwlist[i], i+1); 1802 return cleanreturn(0, &freelist); 1803 } 1804 } 1805 /* current code reports success when all required args 1806 * fulfilled and no keyword args left, with no further 1807 * validation. XXX Maybe skip this in debug build ? 1808 */ 1809 if (!nkwargs && !skip) { 1810 return cleanreturn(1, &freelist); 1811 } 1812 } 1813 1814 /* We are into optional args, skip through to any remaining 1815 * keyword args */ 1816 msg = skipitem(&format, p_va, flags); 1817 if (msg) { 1818 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, 1819 format); 1820 return cleanreturn(0, &freelist); 1821 } 1822 } 1823 1824 if (skip) { 1825 PyErr_Format(PyExc_TypeError, 1826 "%.200s%s takes %s %d positional argument%s" 1827 " (%zd given)", 1828 (fname == NULL) ? "function" : fname, 1829 (fname == NULL) ? "" : "()", 1830 (Py_MIN(pos, min) < i) ? "at least" : "exactly", 1831 Py_MIN(pos, min), 1832 Py_MIN(pos, min) == 1 ? "" : "s", 1833 nargs); 1834 return cleanreturn(0, &freelist); 1835 } 1836 1837 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { 1838 PyErr_Format(PyExc_SystemError, 1839 "more argument specifiers than keyword list entries " 1840 "(remaining format:'%s')", format); 1841 return cleanreturn(0, &freelist); 1842 } 1843 1844 if (nkwargs > 0) { 1845 PyObject *key; 1846 Py_ssize_t j; 1847 /* make sure there are no arguments given by name and position */ 1848 for (i = pos; i < nargs; i++) { 1849 current_arg = _PyDict_GetItemStringWithError(kwargs, kwlist[i]); 1850 if (current_arg) { 1851 /* arg present in tuple and in dict */ 1852 PyErr_Format(PyExc_TypeError, 1853 "argument for %.200s%s given by name ('%s') " 1854 "and position (%d)", 1855 (fname == NULL) ? "function" : fname, 1856 (fname == NULL) ? "" : "()", 1857 kwlist[i], i+1); 1858 return cleanreturn(0, &freelist); 1859 } 1860 else if (PyErr_Occurred()) { 1861 return cleanreturn(0, &freelist); 1862 } 1863 } 1864 /* make sure there are no extraneous keyword arguments */ 1865 j = 0; 1866 while (PyDict_Next(kwargs, &j, &key, NULL)) { 1867 int match = 0; 1868 if (!PyUnicode_Check(key)) { 1869 PyErr_SetString(PyExc_TypeError, 1870 "keywords must be strings"); 1871 return cleanreturn(0, &freelist); 1872 } 1873 for (i = pos; i < len; i++) { 1874 if (_PyUnicode_EqualToASCIIString(key, kwlist[i])) { 1875 match = 1; 1876 break; 1877 } 1878 } 1879 if (!match) { 1880 PyErr_Format(PyExc_TypeError, 1881 "'%U' is an invalid keyword " 1882 "argument for %.200s%s", 1883 key, 1884 (fname == NULL) ? "this function" : fname, 1885 (fname == NULL) ? "" : "()"); 1886 return cleanreturn(0, &freelist); 1887 } 1888 } 1889 /* Something wrong happened. There are extraneous keyword arguments, 1890 * but we don't know what. And we don't bother. */ 1891 PyErr_Format(PyExc_TypeError, 1892 "invalid keyword argument for %.200s%s", 1893 (fname == NULL) ? "this function" : fname, 1894 (fname == NULL) ? "" : "()"); 1895 return cleanreturn(0, &freelist); 1896 } 1897 1898 return cleanreturn(1, &freelist); 1899} 1900 1901 1902/* List of static parsers. */ 1903static struct _PyArg_Parser *static_arg_parsers = NULL; 1904 1905static int 1906parser_init(struct _PyArg_Parser *parser) 1907{ 1908 const char * const *keywords; 1909 const char *format, *msg; 1910 int i, len, min, max, nkw; 1911 PyObject *kwtuple; 1912 1913 assert(parser->keywords != NULL); 1914 if (parser->kwtuple != NULL) { 1915 return 1; 1916 } 1917 1918 keywords = parser->keywords; 1919 /* scan keywords and count the number of positional-only parameters */ 1920 for (i = 0; keywords[i] && !*keywords[i]; i++) { 1921 } 1922 parser->pos = i; 1923 /* scan keywords and get greatest possible nbr of args */ 1924 for (; keywords[i]; i++) { 1925 if (!*keywords[i]) { 1926 PyErr_SetString(PyExc_SystemError, 1927 "Empty keyword parameter name"); 1928 return 0; 1929 } 1930 } 1931 len = i; 1932 1933 format = parser->format; 1934 if (format) { 1935 /* grab the function name or custom error msg first (mutually exclusive) */ 1936 parser->fname = strchr(parser->format, ':'); 1937 if (parser->fname) { 1938 parser->fname++; 1939 parser->custom_msg = NULL; 1940 } 1941 else { 1942 parser->custom_msg = strchr(parser->format,';'); 1943 if (parser->custom_msg) 1944 parser->custom_msg++; 1945 } 1946 1947 min = max = INT_MAX; 1948 for (i = 0; i < len; i++) { 1949 if (*format == '|') { 1950 if (min != INT_MAX) { 1951 PyErr_SetString(PyExc_SystemError, 1952 "Invalid format string (| specified twice)"); 1953 return 0; 1954 } 1955 if (max != INT_MAX) { 1956 PyErr_SetString(PyExc_SystemError, 1957 "Invalid format string ($ before |)"); 1958 return 0; 1959 } 1960 min = i; 1961 format++; 1962 } 1963 if (*format == '$') { 1964 if (max != INT_MAX) { 1965 PyErr_SetString(PyExc_SystemError, 1966 "Invalid format string ($ specified twice)"); 1967 return 0; 1968 } 1969 if (i < parser->pos) { 1970 PyErr_SetString(PyExc_SystemError, 1971 "Empty parameter name after $"); 1972 return 0; 1973 } 1974 max = i; 1975 format++; 1976 } 1977 if (IS_END_OF_FORMAT(*format)) { 1978 PyErr_Format(PyExc_SystemError, 1979 "More keyword list entries (%d) than " 1980 "format specifiers (%d)", len, i); 1981 return 0; 1982 } 1983 1984 msg = skipitem(&format, NULL, 0); 1985 if (msg) { 1986 PyErr_Format(PyExc_SystemError, "%s: '%s'", msg, 1987 format); 1988 return 0; 1989 } 1990 } 1991 parser->min = Py_MIN(min, len); 1992 parser->max = Py_MIN(max, len); 1993 1994 if (!IS_END_OF_FORMAT(*format) && (*format != '|') && (*format != '$')) { 1995 PyErr_Format(PyExc_SystemError, 1996 "more argument specifiers than keyword list entries " 1997 "(remaining format:'%s')", format); 1998 return 0; 1999 } 2000 } 2001 2002 nkw = len - parser->pos; 2003 kwtuple = PyTuple_New(nkw); 2004 if (kwtuple == NULL) { 2005 return 0; 2006 } 2007 keywords = parser->keywords + parser->pos; 2008 for (i = 0; i < nkw; i++) { 2009 PyObject *str = PyUnicode_FromString(keywords[i]); 2010 if (str == NULL) { 2011 Py_DECREF(kwtuple); 2012 return 0; 2013 } 2014 PyUnicode_InternInPlace(&str); 2015 PyTuple_SET_ITEM(kwtuple, i, str); 2016 } 2017 parser->kwtuple = kwtuple; 2018 2019 assert(parser->next == NULL); 2020 parser->next = static_arg_parsers; 2021 static_arg_parsers = parser; 2022 return 1; 2023} 2024 2025static void 2026parser_clear(struct _PyArg_Parser *parser) 2027{ 2028 Py_CLEAR(parser->kwtuple); 2029} 2030 2031static PyObject* 2032find_keyword(PyObject *kwnames, PyObject *const *kwstack, PyObject *key) 2033{ 2034 Py_ssize_t i, nkwargs; 2035 2036 nkwargs = PyTuple_GET_SIZE(kwnames); 2037 for (i = 0; i < nkwargs; i++) { 2038 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i); 2039 2040 /* kwname == key will normally find a match in since keyword keys 2041 should be interned strings; if not retry below in a new loop. */ 2042 if (kwname == key) { 2043 return kwstack[i]; 2044 } 2045 } 2046 2047 for (i = 0; i < nkwargs; i++) { 2048 PyObject *kwname = PyTuple_GET_ITEM(kwnames, i); 2049 assert(PyUnicode_Check(kwname)); 2050 if (_PyUnicode_EQ(kwname, key)) { 2051 return kwstack[i]; 2052 } 2053 } 2054 return NULL; 2055} 2056 2057static int 2058vgetargskeywordsfast_impl(PyObject *const *args, Py_ssize_t nargs, 2059 PyObject *kwargs, PyObject *kwnames, 2060 struct _PyArg_Parser *parser, 2061 va_list *p_va, int flags) 2062{ 2063 PyObject *kwtuple; 2064 char msgbuf[512]; 2065 int levels[32]; 2066 const char *format; 2067 const char *msg; 2068 PyObject *keyword; 2069 int i, pos, len; 2070 Py_ssize_t nkwargs; 2071 PyObject *current_arg; 2072 freelistentry_t static_entries[STATIC_FREELIST_ENTRIES]; 2073 freelist_t freelist; 2074 PyObject *const *kwstack = NULL; 2075 2076 freelist.entries = static_entries; 2077 freelist.first_available = 0; 2078 freelist.entries_malloced = 0; 2079 2080 assert(kwargs == NULL || PyDict_Check(kwargs)); 2081 assert(kwargs == NULL || kwnames == NULL); 2082 assert(p_va != NULL); 2083 2084 if (parser == NULL) { 2085 PyErr_BadInternalCall(); 2086 return 0; 2087 } 2088 2089 if (kwnames != NULL && !PyTuple_Check(kwnames)) { 2090 PyErr_BadInternalCall(); 2091 return 0; 2092 } 2093 2094 if (!parser_init(parser)) { 2095 return 0; 2096 } 2097 2098 kwtuple = parser->kwtuple; 2099 pos = parser->pos; 2100 len = pos + (int)PyTuple_GET_SIZE(kwtuple); 2101 2102 if (len > STATIC_FREELIST_ENTRIES) { 2103 freelist.entries = PyMem_NEW(freelistentry_t, len); 2104 if (freelist.entries == NULL) { 2105 PyErr_NoMemory(); 2106 return 0; 2107 } 2108 freelist.entries_malloced = 1; 2109 } 2110 2111 if (kwargs != NULL) { 2112 nkwargs = PyDict_GET_SIZE(kwargs); 2113 } 2114 else if (kwnames != NULL) { 2115 nkwargs = PyTuple_GET_SIZE(kwnames); 2116 kwstack = args + nargs; 2117 } 2118 else { 2119 nkwargs = 0; 2120 } 2121 if (nargs + nkwargs > len) { 2122 /* Adding "keyword" (when nargs == 0) prevents producing wrong error 2123 messages in some special cases (see bpo-31229). */ 2124 PyErr_Format(PyExc_TypeError, 2125 "%.200s%s takes at most %d %sargument%s (%zd given)", 2126 (parser->fname == NULL) ? "function" : parser->fname, 2127 (parser->fname == NULL) ? "" : "()", 2128 len, 2129 (nargs == 0) ? "keyword " : "", 2130 (len == 1) ? "" : "s", 2131 nargs + nkwargs); 2132 return cleanreturn(0, &freelist); 2133 } 2134 if (parser->max < nargs) { 2135 if (parser->max == 0) { 2136 PyErr_Format(PyExc_TypeError, 2137 "%.200s%s takes no positional arguments", 2138 (parser->fname == NULL) ? "function" : parser->fname, 2139 (parser->fname == NULL) ? "" : "()"); 2140 } 2141 else { 2142 PyErr_Format(PyExc_TypeError, 2143 "%.200s%s takes %s %d positional argument%s (%zd given)", 2144 (parser->fname == NULL) ? "function" : parser->fname, 2145 (parser->fname == NULL) ? "" : "()", 2146 (parser->min < parser->max) ? "at most" : "exactly", 2147 parser->max, 2148 parser->max == 1 ? "" : "s", 2149 nargs); 2150 } 2151 return cleanreturn(0, &freelist); 2152 } 2153 2154 format = parser->format; 2155 /* convert tuple args and keyword args in same loop, using kwtuple to drive process */ 2156 for (i = 0; i < len; i++) { 2157 if (*format == '|') { 2158 format++; 2159 } 2160 if (*format == '$') { 2161 format++; 2162 } 2163 assert(!IS_END_OF_FORMAT(*format)); 2164 2165 if (i < nargs) { 2166 current_arg = args[i]; 2167 } 2168 else if (nkwargs && i >= pos) { 2169 keyword = PyTuple_GET_ITEM(kwtuple, i - pos); 2170 if (kwargs != NULL) { 2171 current_arg = PyDict_GetItemWithError(kwargs, keyword); 2172 if (!current_arg && PyErr_Occurred()) { 2173 return cleanreturn(0, &freelist); 2174 } 2175 } 2176 else { 2177 current_arg = find_keyword(kwnames, kwstack, keyword); 2178 } 2179 if (current_arg) { 2180 --nkwargs; 2181 } 2182 } 2183 else { 2184 current_arg = NULL; 2185 } 2186 2187 if (current_arg) { 2188 msg = convertitem(current_arg, &format, p_va, flags, 2189 levels, msgbuf, sizeof(msgbuf), &freelist); 2190 if (msg) { 2191 seterror(i+1, msg, levels, parser->fname, parser->custom_msg); 2192 return cleanreturn(0, &freelist); 2193 } 2194 continue; 2195 } 2196 2197 if (i < parser->min) { 2198 /* Less arguments than required */ 2199 if (i < pos) { 2200 Py_ssize_t min = Py_MIN(pos, parser->min); 2201 PyErr_Format(PyExc_TypeError, 2202 "%.200s%s takes %s %d positional argument%s" 2203 " (%zd given)", 2204 (parser->fname == NULL) ? "function" : parser->fname, 2205 (parser->fname == NULL) ? "" : "()", 2206 min < parser->max ? "at least" : "exactly", 2207 min, 2208 min == 1 ? "" : "s", 2209 nargs); 2210 } 2211 else { 2212 keyword = PyTuple_GET_ITEM(kwtuple, i - pos); 2213 PyErr_Format(PyExc_TypeError, "%.200s%s missing required " 2214 "argument '%U' (pos %d)", 2215 (parser->fname == NULL) ? "function" : parser->fname, 2216 (parser->fname == NULL) ? "" : "()", 2217 keyword, i+1); 2218 } 2219 return cleanreturn(0, &freelist); 2220 } 2221 /* current code reports success when all required args 2222 * fulfilled and no keyword args left, with no further 2223 * validation. XXX Maybe skip this in debug build ? 2224 */ 2225 if (!nkwargs) { 2226 return cleanreturn(1, &freelist); 2227 } 2228 2229 /* We are into optional args, skip through to any remaining 2230 * keyword args */ 2231 msg = skipitem(&format, p_va, flags); 2232 assert(msg == NULL); 2233 } 2234 2235 assert(IS_END_OF_FORMAT(*format) || (*format == '|') || (*format == '$')); 2236 2237 if (nkwargs > 0) { 2238 /* make sure there are no arguments given by name and position */ 2239 for (i = pos; i < nargs; i++) { 2240 keyword = PyTuple_GET_ITEM(kwtuple, i - pos); 2241 if (kwargs != NULL) { 2242 current_arg = PyDict_GetItemWithError(kwargs, keyword); 2243 if (!current_arg && PyErr_Occurred()) { 2244 return cleanreturn(0, &freelist); 2245 } 2246 } 2247 else { 2248 current_arg = find_keyword(kwnames, kwstack, keyword); 2249 } 2250 if (current_arg) { 2251 /* arg present in tuple and in dict */ 2252 PyErr_Format(PyExc_TypeError, 2253 "argument for %.200s%s given by name ('%U') " 2254 "and position (%d)", 2255 (parser->fname == NULL) ? "function" : parser->fname, 2256 (parser->fname == NULL) ? "" : "()", 2257 keyword, i+1); 2258 return cleanreturn(0, &freelist); 2259 } 2260 } 2261 2262 error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname); 2263 return cleanreturn(0, &freelist); 2264 } 2265 2266 return cleanreturn(1, &freelist); 2267} 2268 2269static int 2270vgetargskeywordsfast(PyObject *args, PyObject *keywords, 2271 struct _PyArg_Parser *parser, va_list *p_va, int flags) 2272{ 2273 PyObject **stack; 2274 Py_ssize_t nargs; 2275 2276 if (args == NULL 2277 || !PyTuple_Check(args) 2278 || (keywords != NULL && !PyDict_Check(keywords))) 2279 { 2280 PyErr_BadInternalCall(); 2281 return 0; 2282 } 2283 2284 stack = _PyTuple_ITEMS(args); 2285 nargs = PyTuple_GET_SIZE(args); 2286 return vgetargskeywordsfast_impl(stack, nargs, keywords, NULL, 2287 parser, p_va, flags); 2288} 2289 2290 2291#undef _PyArg_UnpackKeywords 2292 2293PyObject * const * 2294_PyArg_UnpackKeywords(PyObject *const *args, Py_ssize_t nargs, 2295 PyObject *kwargs, PyObject *kwnames, 2296 struct _PyArg_Parser *parser, 2297 int minpos, int maxpos, int minkw, 2298 PyObject **buf) 2299{ 2300 PyObject *kwtuple; 2301 PyObject *keyword; 2302 int i, posonly, minposonly, maxargs; 2303 int reqlimit = minkw ? maxpos + minkw : minpos; 2304 Py_ssize_t nkwargs; 2305 PyObject *current_arg; 2306 PyObject * const *kwstack = NULL; 2307 2308 assert(kwargs == NULL || PyDict_Check(kwargs)); 2309 assert(kwargs == NULL || kwnames == NULL); 2310 2311 if (parser == NULL) { 2312 PyErr_BadInternalCall(); 2313 return NULL; 2314 } 2315 2316 if (kwnames != NULL && !PyTuple_Check(kwnames)) { 2317 PyErr_BadInternalCall(); 2318 return NULL; 2319 } 2320 2321 if (args == NULL && nargs == 0) { 2322 args = buf; 2323 } 2324 2325 if (!parser_init(parser)) { 2326 return NULL; 2327 } 2328 2329 kwtuple = parser->kwtuple; 2330 posonly = parser->pos; 2331 minposonly = Py_MIN(posonly, minpos); 2332 maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple); 2333 2334 if (kwargs != NULL) { 2335 nkwargs = PyDict_GET_SIZE(kwargs); 2336 } 2337 else if (kwnames != NULL) { 2338 nkwargs = PyTuple_GET_SIZE(kwnames); 2339 kwstack = args + nargs; 2340 } 2341 else { 2342 nkwargs = 0; 2343 } 2344 if (nkwargs == 0 && minkw == 0 && minpos <= nargs && nargs <= maxpos) { 2345 /* Fast path. */ 2346 return args; 2347 } 2348 if (nargs + nkwargs > maxargs) { 2349 /* Adding "keyword" (when nargs == 0) prevents producing wrong error 2350 messages in some special cases (see bpo-31229). */ 2351 PyErr_Format(PyExc_TypeError, 2352 "%.200s%s takes at most %d %sargument%s (%zd given)", 2353 (parser->fname == NULL) ? "function" : parser->fname, 2354 (parser->fname == NULL) ? "" : "()", 2355 maxargs, 2356 (nargs == 0) ? "keyword " : "", 2357 (maxargs == 1) ? "" : "s", 2358 nargs + nkwargs); 2359 return NULL; 2360 } 2361 if (nargs > maxpos) { 2362 if (maxpos == 0) { 2363 PyErr_Format(PyExc_TypeError, 2364 "%.200s%s takes no positional arguments", 2365 (parser->fname == NULL) ? "function" : parser->fname, 2366 (parser->fname == NULL) ? "" : "()"); 2367 } 2368 else { 2369 PyErr_Format(PyExc_TypeError, 2370 "%.200s%s takes %s %d positional argument%s (%zd given)", 2371 (parser->fname == NULL) ? "function" : parser->fname, 2372 (parser->fname == NULL) ? "" : "()", 2373 (minpos < maxpos) ? "at most" : "exactly", 2374 maxpos, 2375 (maxpos == 1) ? "" : "s", 2376 nargs); 2377 } 2378 return NULL; 2379 } 2380 if (nargs < minposonly) { 2381 PyErr_Format(PyExc_TypeError, 2382 "%.200s%s takes %s %d positional argument%s" 2383 " (%zd given)", 2384 (parser->fname == NULL) ? "function" : parser->fname, 2385 (parser->fname == NULL) ? "" : "()", 2386 minposonly < maxpos ? "at least" : "exactly", 2387 minposonly, 2388 minposonly == 1 ? "" : "s", 2389 nargs); 2390 return NULL; 2391 } 2392 2393 /* copy tuple args */ 2394 for (i = 0; i < nargs; i++) { 2395 buf[i] = args[i]; 2396 } 2397 2398 /* copy keyword args using kwtuple to drive process */ 2399 for (i = Py_MAX((int)nargs, posonly); i < maxargs; i++) { 2400 if (nkwargs) { 2401 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); 2402 if (kwargs != NULL) { 2403 current_arg = PyDict_GetItemWithError(kwargs, keyword); 2404 if (!current_arg && PyErr_Occurred()) { 2405 return NULL; 2406 } 2407 } 2408 else { 2409 current_arg = find_keyword(kwnames, kwstack, keyword); 2410 } 2411 } 2412 else if (i >= reqlimit) { 2413 break; 2414 } 2415 else { 2416 current_arg = NULL; 2417 } 2418 2419 buf[i] = current_arg; 2420 2421 if (current_arg) { 2422 --nkwargs; 2423 } 2424 else if (i < minpos || (maxpos <= i && i < reqlimit)) { 2425 /* Less arguments than required */ 2426 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); 2427 PyErr_Format(PyExc_TypeError, "%.200s%s missing required " 2428 "argument '%U' (pos %d)", 2429 (parser->fname == NULL) ? "function" : parser->fname, 2430 (parser->fname == NULL) ? "" : "()", 2431 keyword, i+1); 2432 return NULL; 2433 } 2434 } 2435 2436 if (nkwargs > 0) { 2437 /* make sure there are no arguments given by name and position */ 2438 for (i = posonly; i < nargs; i++) { 2439 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); 2440 if (kwargs != NULL) { 2441 current_arg = PyDict_GetItemWithError(kwargs, keyword); 2442 if (!current_arg && PyErr_Occurred()) { 2443 return NULL; 2444 } 2445 } 2446 else { 2447 current_arg = find_keyword(kwnames, kwstack, keyword); 2448 } 2449 if (current_arg) { 2450 /* arg present in tuple and in dict */ 2451 PyErr_Format(PyExc_TypeError, 2452 "argument for %.200s%s given by name ('%U') " 2453 "and position (%d)", 2454 (parser->fname == NULL) ? "function" : parser->fname, 2455 (parser->fname == NULL) ? "" : "()", 2456 keyword, i+1); 2457 return NULL; 2458 } 2459 } 2460 2461 error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname); 2462 return NULL; 2463 } 2464 2465 return buf; 2466} 2467 2468PyObject * const * 2469_PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs, 2470 PyObject *kwargs, PyObject *kwnames, 2471 struct _PyArg_Parser *parser, 2472 int minpos, int maxpos, int minkw, 2473 int vararg, PyObject **buf) 2474{ 2475 PyObject *kwtuple; 2476 PyObject *keyword; 2477 Py_ssize_t varargssize = 0; 2478 int i, posonly, minposonly, maxargs; 2479 int reqlimit = minkw ? maxpos + minkw : minpos; 2480 Py_ssize_t nkwargs; 2481 PyObject *current_arg; 2482 PyObject * const *kwstack = NULL; 2483 2484 assert(kwargs == NULL || PyDict_Check(kwargs)); 2485 assert(kwargs == NULL || kwnames == NULL); 2486 2487 if (parser == NULL) { 2488 PyErr_BadInternalCall(); 2489 return NULL; 2490 } 2491 2492 if (kwnames != NULL && !PyTuple_Check(kwnames)) { 2493 PyErr_BadInternalCall(); 2494 return NULL; 2495 } 2496 2497 if (args == NULL && nargs == 0) { 2498 args = buf; 2499 } 2500 2501 if (!parser_init(parser)) { 2502 return NULL; 2503 } 2504 2505 kwtuple = parser->kwtuple; 2506 posonly = parser->pos; 2507 minposonly = Py_MIN(posonly, minpos); 2508 maxargs = posonly + (int)PyTuple_GET_SIZE(kwtuple); 2509 if (kwargs != NULL) { 2510 nkwargs = PyDict_GET_SIZE(kwargs); 2511 } 2512 else if (kwnames != NULL) { 2513 nkwargs = PyTuple_GET_SIZE(kwnames); 2514 kwstack = args + nargs; 2515 } 2516 else { 2517 nkwargs = 0; 2518 } 2519 if (nargs < minposonly) { 2520 PyErr_Format(PyExc_TypeError, 2521 "%.200s%s takes %s %d positional argument%s" 2522 " (%zd given)", 2523 (parser->fname == NULL) ? "function" : parser->fname, 2524 (parser->fname == NULL) ? "" : "()", 2525 minposonly < maxpos ? "at least" : "exactly", 2526 minposonly, 2527 minposonly == 1 ? "" : "s", 2528 nargs); 2529 return NULL; 2530 } 2531 2532 /* create varargs tuple */ 2533 varargssize = nargs - maxpos; 2534 if (varargssize < 0) { 2535 varargssize = 0; 2536 } 2537 buf[vararg] = PyTuple_New(varargssize); 2538 if (!buf[vararg]) { 2539 return NULL; 2540 } 2541 2542 /* copy tuple args */ 2543 for (i = 0; i < nargs; i++) { 2544 if (i >= vararg) { 2545 Py_INCREF(args[i]); 2546 PyTuple_SET_ITEM(buf[vararg], i - vararg, args[i]); 2547 continue; 2548 } 2549 else { 2550 buf[i] = args[i]; 2551 } 2552 } 2553 2554 /* copy keyword args using kwtuple to drive process */ 2555 for (i = Py_MAX((int)nargs, posonly) - 2556 Py_SAFE_DOWNCAST(varargssize, Py_ssize_t, int); i < maxargs; i++) { 2557 if (nkwargs) { 2558 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); 2559 if (kwargs != NULL) { 2560 current_arg = PyDict_GetItemWithError(kwargs, keyword); 2561 if (!current_arg && PyErr_Occurred()) { 2562 goto exit; 2563 } 2564 } 2565 else { 2566 current_arg = find_keyword(kwnames, kwstack, keyword); 2567 } 2568 } 2569 else { 2570 current_arg = NULL; 2571 } 2572 2573 /* If an arguments is passed in as a keyword argument, 2574 * it should be placed before `buf[vararg]`. 2575 * 2576 * For example: 2577 * def f(a, /, b, *args): 2578 * pass 2579 * f(1, b=2) 2580 * 2581 * This `buf` array should be: [1, 2, NULL]. 2582 * In this case, nargs < vararg. 2583 * 2584 * Otherwise, we leave a place at `buf[vararg]` for vararg tuple 2585 * so the index is `i + 1`. */ 2586 if (nargs < vararg) { 2587 buf[i] = current_arg; 2588 } 2589 else { 2590 buf[i + 1] = current_arg; 2591 } 2592 2593 if (current_arg) { 2594 --nkwargs; 2595 } 2596 else if (i < minpos || (maxpos <= i && i < reqlimit)) { 2597 /* Less arguments than required */ 2598 keyword = PyTuple_GET_ITEM(kwtuple, i - posonly); 2599 PyErr_Format(PyExc_TypeError, "%.200s%s missing required " 2600 "argument '%U' (pos %d)", 2601 (parser->fname == NULL) ? "function" : parser->fname, 2602 (parser->fname == NULL) ? "" : "()", 2603 keyword, i+1); 2604 goto exit; 2605 } 2606 } 2607 2608 if (nkwargs > 0) { 2609 error_unexpected_keyword_arg(kwargs, kwnames, kwtuple, parser->fname); 2610 goto exit; 2611 } 2612 2613 return buf; 2614 2615exit: 2616 Py_XDECREF(buf[vararg]); 2617 return NULL; 2618} 2619 2620 2621static const char * 2622skipitem(const char **p_format, va_list *p_va, int flags) 2623{ 2624 const char *format = *p_format; 2625 char c = *format++; 2626 2627 switch (c) { 2628 2629 /* 2630 * codes that take a single data pointer as an argument 2631 * (the type of the pointer is irrelevant) 2632 */ 2633 2634 case 'b': /* byte -- very short int */ 2635 case 'B': /* byte as bitfield */ 2636 case 'h': /* short int */ 2637 case 'H': /* short int as bitfield */ 2638 case 'i': /* int */ 2639 case 'I': /* int sized bitfield */ 2640 case 'l': /* long int */ 2641 case 'k': /* long int sized bitfield */ 2642 case 'L': /* long long */ 2643 case 'K': /* long long sized bitfield */ 2644 case 'n': /* Py_ssize_t */ 2645 case 'f': /* float */ 2646 case 'd': /* double */ 2647 case 'D': /* complex double */ 2648 case 'c': /* char */ 2649 case 'C': /* unicode char */ 2650 case 'p': /* boolean predicate */ 2651 case 'S': /* string object */ 2652 case 'Y': /* string object */ 2653 case 'U': /* unicode string object */ 2654 { 2655 if (p_va != NULL) { 2656 (void) va_arg(*p_va, void *); 2657 } 2658 break; 2659 } 2660 2661 /* string codes */ 2662 2663 case 'e': /* string with encoding */ 2664 { 2665 if (p_va != NULL) { 2666 (void) va_arg(*p_va, const char *); 2667 } 2668 if (!(*format == 's' || *format == 't')) 2669 /* after 'e', only 's' and 't' is allowed */ 2670 goto err; 2671 format++; 2672 } 2673 /* fall through */ 2674 2675 case 's': /* string */ 2676 case 'z': /* string or None */ 2677 case 'y': /* bytes */ 2678 case 'u': /* unicode string */ 2679 case 'Z': /* unicode string or None */ 2680 case 'w': /* buffer, read-write */ 2681 { 2682 if (p_va != NULL) { 2683 (void) va_arg(*p_va, char **); 2684 } 2685 if (*format == '#') { 2686 if (p_va != NULL) { 2687 if (!(flags & FLAG_SIZE_T)) { 2688 return "PY_SSIZE_T_CLEAN macro must be defined for '#' formats"; 2689 } 2690 (void) va_arg(*p_va, Py_ssize_t *); 2691 } 2692 format++; 2693 } else if ((c == 's' || c == 'z' || c == 'y' || c == 'w') 2694 && *format == '*') 2695 { 2696 format++; 2697 } 2698 break; 2699 } 2700 2701 case 'O': /* object */ 2702 { 2703 if (*format == '!') { 2704 format++; 2705 if (p_va != NULL) { 2706 (void) va_arg(*p_va, PyTypeObject*); 2707 (void) va_arg(*p_va, PyObject **); 2708 } 2709 } 2710 else if (*format == '&') { 2711 typedef int (*converter)(PyObject *, void *); 2712 if (p_va != NULL) { 2713 (void) va_arg(*p_va, converter); 2714 (void) va_arg(*p_va, void *); 2715 } 2716 format++; 2717 } 2718 else { 2719 if (p_va != NULL) { 2720 (void) va_arg(*p_va, PyObject **); 2721 } 2722 } 2723 break; 2724 } 2725 2726 case '(': /* bypass tuple, not handled at all previously */ 2727 { 2728 const char *msg; 2729 for (;;) { 2730 if (*format==')') 2731 break; 2732 if (IS_END_OF_FORMAT(*format)) 2733 return "Unmatched left paren in format " 2734 "string"; 2735 msg = skipitem(&format, p_va, flags); 2736 if (msg) 2737 return msg; 2738 } 2739 format++; 2740 break; 2741 } 2742 2743 case ')': 2744 return "Unmatched right paren in format string"; 2745 2746 default: 2747err: 2748 return "impossible<bad format char>"; 2749 2750 } 2751 2752 *p_format = format; 2753 return NULL; 2754} 2755 2756 2757#undef _PyArg_CheckPositional 2758 2759int 2760_PyArg_CheckPositional(const char *name, Py_ssize_t nargs, 2761 Py_ssize_t min, Py_ssize_t max) 2762{ 2763 assert(min >= 0); 2764 assert(min <= max); 2765 2766 if (nargs < min) { 2767 if (name != NULL) 2768 PyErr_Format( 2769 PyExc_TypeError, 2770 "%.200s expected %s%zd argument%s, got %zd", 2771 name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs); 2772 else 2773 PyErr_Format( 2774 PyExc_TypeError, 2775 "unpacked tuple should have %s%zd element%s," 2776 " but has %zd", 2777 (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs); 2778 return 0; 2779 } 2780 2781 if (nargs == 0) { 2782 return 1; 2783 } 2784 2785 if (nargs > max) { 2786 if (name != NULL) 2787 PyErr_Format( 2788 PyExc_TypeError, 2789 "%.200s expected %s%zd argument%s, got %zd", 2790 name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs); 2791 else 2792 PyErr_Format( 2793 PyExc_TypeError, 2794 "unpacked tuple should have %s%zd element%s," 2795 " but has %zd", 2796 (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs); 2797 return 0; 2798 } 2799 2800 return 1; 2801} 2802 2803static int 2804unpack_stack(PyObject *const *args, Py_ssize_t nargs, const char *name, 2805 Py_ssize_t min, Py_ssize_t max, va_list vargs) 2806{ 2807 Py_ssize_t i; 2808 PyObject **o; 2809 2810 if (!_PyArg_CheckPositional(name, nargs, min, max)) { 2811 return 0; 2812 } 2813 2814 for (i = 0; i < nargs; i++) { 2815 o = va_arg(vargs, PyObject **); 2816 *o = args[i]; 2817 } 2818 return 1; 2819} 2820 2821int 2822PyArg_UnpackTuple(PyObject *args, const char *name, Py_ssize_t min, Py_ssize_t max, ...) 2823{ 2824 PyObject **stack; 2825 Py_ssize_t nargs; 2826 int retval; 2827 va_list vargs; 2828 2829 if (!PyTuple_Check(args)) { 2830 PyErr_SetString(PyExc_SystemError, 2831 "PyArg_UnpackTuple() argument list is not a tuple"); 2832 return 0; 2833 } 2834 stack = _PyTuple_ITEMS(args); 2835 nargs = PyTuple_GET_SIZE(args); 2836 2837#ifdef HAVE_STDARG_PROTOTYPES 2838 va_start(vargs, max); 2839#else 2840 va_start(vargs); 2841#endif 2842 retval = unpack_stack(stack, nargs, name, min, max, vargs); 2843 va_end(vargs); 2844 return retval; 2845} 2846 2847int 2848_PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name, 2849 Py_ssize_t min, Py_ssize_t max, ...) 2850{ 2851 int retval; 2852 va_list vargs; 2853 2854#ifdef HAVE_STDARG_PROTOTYPES 2855 va_start(vargs, max); 2856#else 2857 va_start(vargs); 2858#endif 2859 retval = unpack_stack(args, nargs, name, min, max, vargs); 2860 va_end(vargs); 2861 return retval; 2862} 2863 2864 2865#undef _PyArg_NoKeywords 2866#undef _PyArg_NoKwnames 2867#undef _PyArg_NoPositional 2868 2869/* For type constructors that don't take keyword args 2870 * 2871 * Sets a TypeError and returns 0 if the args/kwargs is 2872 * not empty, returns 1 otherwise 2873 */ 2874int 2875_PyArg_NoKeywords(const char *funcname, PyObject *kwargs) 2876{ 2877 if (kwargs == NULL) { 2878 return 1; 2879 } 2880 if (!PyDict_CheckExact(kwargs)) { 2881 PyErr_BadInternalCall(); 2882 return 0; 2883 } 2884 if (PyDict_GET_SIZE(kwargs) == 0) { 2885 return 1; 2886 } 2887 2888 PyErr_Format(PyExc_TypeError, "%.200s() takes no keyword arguments", 2889 funcname); 2890 return 0; 2891} 2892 2893int 2894_PyArg_NoPositional(const char *funcname, PyObject *args) 2895{ 2896 if (args == NULL) 2897 return 1; 2898 if (!PyTuple_CheckExact(args)) { 2899 PyErr_BadInternalCall(); 2900 return 0; 2901 } 2902 if (PyTuple_GET_SIZE(args) == 0) 2903 return 1; 2904 2905 PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments", 2906 funcname); 2907 return 0; 2908} 2909 2910int 2911_PyArg_NoKwnames(const char *funcname, PyObject *kwnames) 2912{ 2913 if (kwnames == NULL) { 2914 return 1; 2915 } 2916 2917 assert(PyTuple_CheckExact(kwnames)); 2918 2919 if (PyTuple_GET_SIZE(kwnames) == 0) { 2920 return 1; 2921 } 2922 2923 PyErr_Format(PyExc_TypeError, "%s() takes no keyword arguments", funcname); 2924 return 0; 2925} 2926 2927void 2928_PyArg_Fini(void) 2929{ 2930 struct _PyArg_Parser *tmp, *s = static_arg_parsers; 2931 while (s) { 2932 tmp = s->next; 2933 s->next = NULL; 2934 parser_clear(s); 2935 s = tmp; 2936 } 2937 static_arg_parsers = NULL; 2938} 2939 2940#ifdef __cplusplus 2941}; 2942#endif 2943