LCOV - code coverage report
Current view: top level - libreoffice/workdir/unxlngi6.pro/UnpackedTarball/python3/Python - getargs.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 267 808 33.0 %
Date: 2012-12-17 Functions: 17 28 60.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10