Python-2.7.3/Python/getargs.c

No issues found

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