Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Python/modsupport.c

    r2 r391  
    3131PyObject *
    3232Py_InitModule4(const char *name, PyMethodDef *methods, const char *doc,
    33                PyObject *passthrough, int module_api_version)
    34 {
    35         PyObject *m, *d, *v, *n;
    36         PyMethodDef *ml;
    37         if (!Py_IsInitialized())
    38             Py_FatalError("Interpreter not initialized (version mismatch?)");
    39         if (module_api_version != PYTHON_API_VERSION) {
    40                 char message[512];
    41                 PyOS_snprintf(message, sizeof(message),
    42                               api_version_warning, name,
    43                               PYTHON_API_VERSION, name,
    44                               module_api_version);
    45                 if (PyErr_Warn(PyExc_RuntimeWarning, message))
    46                         return NULL;
    47         }
    48         /* Make sure name is fully qualified.
    49 
    50            This is a bit of a hack: when the shared library is loaded,
    51            the module name is "package.module", but the module calls
    52            Py_InitModule*() with just "module" for the name.  The shared
    53            library loader squirrels away the true name of the module in
    54            _Py_PackageContext, and Py_InitModule*() will substitute this
    55            (if the name actually matches).
    56         */
    57         if (_Py_PackageContext != NULL) {
    58                 char *p = strrchr(_Py_PackageContext, '.');
    59                 if (p != NULL && strcmp(name, p+1) == 0) {
    60                         name = _Py_PackageContext;
    61                         _Py_PackageContext = NULL;
    62                 }
    63         }
    64         if ((m = PyImport_AddModule(name)) == NULL)
    65                 return NULL;
    66         d = PyModule_GetDict(m);
    67         if (methods != NULL) {
    68                 n = PyString_FromString(name);
    69                 if (n == NULL)
    70                         return NULL;
    71                 for (ml = methods; ml->ml_name != NULL; ml++) {
    72                         if ((ml->ml_flags & METH_CLASS) ||
    73                             (ml->ml_flags & METH_STATIC)) {
    74                                 PyErr_SetString(PyExc_ValueError,
    75                                                 "module functions cannot set"
    76                                                 " METH_CLASS or METH_STATIC");
    77                                 Py_DECREF(n);
    78                                 return NULL;
    79                         }
    80                         v = PyCFunction_NewEx(ml, passthrough, n);
    81                         if (v == NULL) {
    82                                 Py_DECREF(n);
    83                                 return NULL;
    84                         }
    85                         if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
    86                                 Py_DECREF(v);
    87                                 Py_DECREF(n);
    88                                 return NULL;
    89                         }
    90                         Py_DECREF(v);
    91                 }
    92                 Py_DECREF(n);
    93         }
    94         if (doc != NULL) {
    95                 v = PyString_FromString(doc);
    96                 if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
    97                         Py_XDECREF(v);
    98                         return NULL;
    99                 }
    100                 Py_DECREF(v);
    101         }
    102         return m;
     33               PyObject *passthrough, int module_api_version)
     34{
     35    PyObject *m, *d, *v, *n;
     36    PyMethodDef *ml;
     37    PyInterpreterState *interp = PyThreadState_Get()->interp;
     38    if (interp->modules == NULL)
     39        Py_FatalError("Python import machinery not initialized");
     40    if (module_api_version != PYTHON_API_VERSION) {
     41        char message[512];
     42        PyOS_snprintf(message, sizeof(message),
     43                      api_version_warning, name,
     44                      PYTHON_API_VERSION, name,
     45                      module_api_version);
     46        if (PyErr_Warn(PyExc_RuntimeWarning, message))
     47            return NULL;
     48    }
     49    /* Make sure name is fully qualified.
     50
     51       This is a bit of a hack: when the shared library is loaded,
     52       the module name is "package.module", but the module calls
     53       Py_InitModule*() with just "module" for the name.  The shared
     54       library loader squirrels away the true name of the module in
     55       _Py_PackageContext, and Py_InitModule*() will substitute this
     56       (if the name actually matches).
     57    */
     58    if (_Py_PackageContext != NULL) {
     59        char *p = strrchr(_Py_PackageContext, '.');
     60        if (p != NULL && strcmp(name, p+1) == 0) {
     61            name = _Py_PackageContext;
     62            _Py_PackageContext = NULL;
     63        }
     64    }
     65    if ((m = PyImport_AddModule(name)) == NULL)
     66        return NULL;
     67    d = PyModule_GetDict(m);
     68    if (methods != NULL) {
     69        n = PyString_FromString(name);
     70        if (n == NULL)
     71            return NULL;
     72        for (ml = methods; ml->ml_name != NULL; ml++) {
     73            if ((ml->ml_flags & METH_CLASS) ||
     74                (ml->ml_flags & METH_STATIC)) {
     75                PyErr_SetString(PyExc_ValueError,
     76                                "module functions cannot set"
     77                                " METH_CLASS or METH_STATIC");
     78                Py_DECREF(n);
     79                return NULL;
     80            }
     81            v = PyCFunction_NewEx(ml, passthrough, n);
     82            if (v == NULL) {
     83                Py_DECREF(n);
     84                return NULL;
     85            }
     86            if (PyDict_SetItemString(d, ml->ml_name, v) != 0) {
     87                Py_DECREF(v);
     88                Py_DECREF(n);
     89                return NULL;
     90            }
     91            Py_DECREF(v);
     92        }
     93        Py_DECREF(n);
     94    }
     95    if (doc != NULL) {
     96        v = PyString_FromString(doc);
     97        if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) {
     98            Py_XDECREF(v);
     99            return NULL;
     100        }
     101        Py_DECREF(v);
     102    }
     103    return m;
    103104}
    104105
     
    109110countformat(const char *format, int endchar)
    110111{
    111         int count = 0;
    112         int level = 0;
    113         while (level > 0 || *format != endchar) {
    114                 switch (*format) {
    115                 case '\0':
    116                         /* Premature end */
    117                         PyErr_SetString(PyExc_SystemError,
    118                                         "unmatched paren in format");
    119                         return -1;
    120                 case '(':
    121                 case '[':
    122                 case '{':
    123                         if (level == 0)
    124                                 count++;
    125                         level++;
    126                         break;
    127                 case ')':
    128                 case ']':
    129                 case '}':
    130                         level--;
    131                         break;
    132                 case '#':
    133                 case '&':
    134                 case ',':
    135                 case ':':
    136                 case ' ':
    137                 case '\t':
    138                         break;
    139                 default:
    140                         if (level == 0)
    141                                 count++;
    142                 }
    143                 format++;
    144         }
    145         return count;
     112    int count = 0;
     113    int level = 0;
     114    while (level > 0 || *format != endchar) {
     115        switch (*format) {
     116        case '\0':
     117            /* Premature end */
     118            PyErr_SetString(PyExc_SystemError,
     119                            "unmatched paren in format");
     120            return -1;
     121        case '(':
     122        case '[':
     123        case '{':
     124            if (level == 0)
     125                count++;
     126            level++;
     127            break;
     128        case ')':
     129        case ']':
     130        case '}':
     131            level--;
     132            break;
     133        case '#':
     134        case '&':
     135        case ',':
     136        case ':':
     137        case ' ':
     138        case '\t':
     139            break;
     140        default:
     141            if (level == 0)
     142                count++;
     143        }
     144        format++;
     145    }
     146    return count;
    146147}
    147148
     
    159160do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
    160161{
    161         PyObject *d;
    162         int i;
    163         int itemfailed = 0;
    164         if (n < 0)
    165                 return NULL;
    166         if ((d = PyDict_New()) == NULL)
    167                 return NULL;
    168         /* Note that we can't bail immediately on error as this will leak
    169            refcounts on any 'N' arguments. */
    170         for (i = 0; i < n; i+= 2) {
    171                 PyObject *k, *v;
    172                 int err;
    173                 k = do_mkvalue(p_format, p_va, flags);
    174                 if (k == NULL) {
    175                         itemfailed = 1;
    176                         Py_INCREF(Py_None);
    177                         k = Py_None;
    178                 }
    179                 v = do_mkvalue(p_format, p_va, flags);
    180                 if (v == NULL) {
    181                         itemfailed = 1;
    182                         Py_INCREF(Py_None);
    183                         v = Py_None;
    184                 }
    185                 err = PyDict_SetItem(d, k, v);
    186                 Py_DECREF(k);
    187                 Py_DECREF(v);
    188                 if (err < 0 || itemfailed) {
    189                         Py_DECREF(d);
    190                         return NULL;
    191                 }
    192         }
    193         if (d != NULL && **p_format != endchar) {
    194                 Py_DECREF(d);
    195                 d = NULL;
    196                 PyErr_SetString(PyExc_SystemError,
    197                                 "Unmatched paren in format");
    198         }
    199         else if (endchar)
    200                 ++*p_format;
    201         return d;
     162    PyObject *d;
     163    int i;
     164    int itemfailed = 0;
     165    if (n < 0)
     166        return NULL;
     167    if ((d = PyDict_New()) == NULL)
     168        return NULL;
     169    /* Note that we can't bail immediately on error as this will leak
     170       refcounts on any 'N' arguments. */
     171    for (i = 0; i < n; i+= 2) {
     172        PyObject *k, *v;
     173        int err;
     174        k = do_mkvalue(p_format, p_va, flags);
     175        if (k == NULL) {
     176            itemfailed = 1;
     177            Py_INCREF(Py_None);
     178            k = Py_None;
     179        }
     180        v = do_mkvalue(p_format, p_va, flags);
     181        if (v == NULL) {
     182            itemfailed = 1;
     183            Py_INCREF(Py_None);
     184            v = Py_None;
     185        }
     186        err = PyDict_SetItem(d, k, v);
     187        Py_DECREF(k);
     188        Py_DECREF(v);
     189        if (err < 0 || itemfailed) {
     190            Py_DECREF(d);
     191            return NULL;
     192        }
     193    }
     194    if (d != NULL && **p_format != endchar) {
     195        Py_DECREF(d);
     196        d = NULL;
     197        PyErr_SetString(PyExc_SystemError,
     198                        "Unmatched paren in format");
     199    }
     200    else if (endchar)
     201        ++*p_format;
     202    return d;
    202203}
    203204
     
    205206do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
    206207{
    207         PyObject *v;
    208         int i;
    209         int itemfailed = 0;
    210         if (n < 0)
    211                 return NULL;
    212         v = PyList_New(n);
    213         if (v == NULL)
    214                 return NULL;
    215         /* Note that we can't bail immediately on error as this will leak
    216            refcounts on any 'N' arguments. */
    217         for (i = 0; i < n; i++) {
    218                 PyObject *w = do_mkvalue(p_format, p_va, flags);
    219                 if (w == NULL) {
    220                         itemfailed = 1;
    221                         Py_INCREF(Py_None);
    222                         w = Py_None;
    223                 }
    224                 PyList_SET_ITEM(v, i, w);
    225         }
    226 
    227         if (itemfailed) {
    228                 /* do_mkvalue() should have already set an error */
    229                 Py_DECREF(v);
    230                 return NULL;
    231         }
    232         if (**p_format != endchar) {
    233                 Py_DECREF(v);
    234                 PyErr_SetString(PyExc_SystemError,
    235                                 "Unmatched paren in format");
    236                 return NULL;
    237         }
    238         if (endchar)
    239                 ++*p_format;
    240         return v;
     208    PyObject *v;
     209    int i;
     210    int itemfailed = 0;
     211    if (n < 0)
     212        return NULL;
     213    v = PyList_New(n);
     214    if (v == NULL)
     215        return NULL;
     216    /* Note that we can't bail immediately on error as this will leak
     217       refcounts on any 'N' arguments. */
     218    for (i = 0; i < n; i++) {
     219        PyObject *w = do_mkvalue(p_format, p_va, flags);
     220        if (w == NULL) {
     221            itemfailed = 1;
     222            Py_INCREF(Py_None);
     223            w = Py_None;
     224        }
     225        PyList_SET_ITEM(v, i, w);
     226    }
     227
     228    if (itemfailed) {
     229        /* do_mkvalue() should have already set an error */
     230        Py_DECREF(v);
     231        return NULL;
     232    }
     233    if (**p_format != endchar) {
     234        Py_DECREF(v);
     235        PyErr_SetString(PyExc_SystemError,
     236                        "Unmatched paren in format");
     237        return NULL;
     238    }
     239    if (endchar)
     240        ++*p_format;
     241    return v;
    241242}
    242243
     
    245246_ustrlen(Py_UNICODE *u)
    246247{
    247         int i = 0;
    248         Py_UNICODE *v = u;
    249         while (*v != 0) { i++; v++; }
    250         return i;
     248    int i = 0;
     249    Py_UNICODE *v = u;
     250    while (*v != 0) { i++; v++; }
     251    return i;
    251252}
    252253#endif
     
    255256do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
    256257{
    257         PyObject *v;
    258         int i;
    259         int itemfailed = 0;
    260         if (n < 0)
    261                 return NULL;
    262         if ((v = PyTuple_New(n)) == NULL)
    263                 return NULL;
    264         /* Note that we can't bail immediately on error as this will leak
    265            refcounts on any 'N' arguments. */
    266         for (i = 0; i < n; i++) {
    267                 PyObject *w = do_mkvalue(p_format, p_va, flags);
    268                 if (w == NULL) {
    269                         itemfailed = 1;
    270                         Py_INCREF(Py_None);
    271                         w = Py_None;
    272                 }
    273                 PyTuple_SET_ITEM(v, i, w);
    274         }
    275         if (itemfailed) {
    276                 /* do_mkvalue() should have already set an error */
    277                 Py_DECREF(v);
    278                 return NULL;
    279         }
    280         if (**p_format != endchar) {
    281                 Py_DECREF(v);
    282                 PyErr_SetString(PyExc_SystemError,
    283                                 "Unmatched paren in format");
    284                 return NULL;
    285         }
    286         if (endchar)
    287                 ++*p_format;
    288         return v;
     258    PyObject *v;
     259    int i;
     260    int itemfailed = 0;
     261    if (n < 0)
     262        return NULL;
     263    if ((v = PyTuple_New(n)) == NULL)
     264        return NULL;
     265    /* Note that we can't bail immediately on error as this will leak
     266       refcounts on any 'N' arguments. */
     267    for (i = 0; i < n; i++) {
     268        PyObject *w = do_mkvalue(p_format, p_va, flags);
     269        if (w == NULL) {
     270            itemfailed = 1;
     271            Py_INCREF(Py_None);
     272            w = Py_None;
     273        }
     274        PyTuple_SET_ITEM(v, i, w);
     275    }
     276    if (itemfailed) {
     277        /* do_mkvalue() should have already set an error */
     278        Py_DECREF(v);
     279        return NULL;
     280    }
     281    if (**p_format != endchar) {
     282        Py_DECREF(v);
     283        PyErr_SetString(PyExc_SystemError,
     284                        "Unmatched paren in format");
     285        return NULL;
     286    }
     287    if (endchar)
     288        ++*p_format;
     289    return v;
    289290}
    290291
     
    292293do_mkvalue(const char **p_format, va_list *p_va, int flags)
    293294{
    294         for (;;) {
    295                 switch (*(*p_format)++) {
    296                 case '(':
    297                         return do_mktuple(p_format, p_va, ')',
    298                                           countformat(*p_format, ')'), flags);
    299 
    300                 case '[':
    301                         return do_mklist(p_format, p_va, ']',
    302                                         countformat(*p_format, ']'), flags);
    303 
    304                 case '{':
    305                         return do_mkdict(p_format, p_va, '}',
    306                                         countformat(*p_format, '}'), flags);
    307 
    308                 case 'b':
    309                 case 'B':
    310                 case 'h':
    311                 case 'i':
    312                         return PyInt_FromLong((long)va_arg(*p_va, int));
    313                        
    314                 case 'H':
    315                         return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
    316 
    317                 case 'I':
    318                 {
    319                         unsigned int n;
    320                         n = va_arg(*p_va, unsigned int);
    321                         if (n > (unsigned long)PyInt_GetMax())
    322                                 return PyLong_FromUnsignedLong((unsigned long)n);
    323                         else
    324                                 return PyInt_FromLong(n);
    325                 }
    326                
    327                 case 'n':
     295    for (;;) {
     296        switch (*(*p_format)++) {
     297        case '(':
     298            return do_mktuple(p_format, p_va, ')',
     299                              countformat(*p_format, ')'), flags);
     300
     301        case '[':
     302            return do_mklist(p_format, p_va, ']',
     303                            countformat(*p_format, ']'), flags);
     304
     305        case '{':
     306            return do_mkdict(p_format, p_va, '}',
     307                            countformat(*p_format, '}'), flags);
     308
     309        case 'b':
     310        case 'B':
     311        case 'h':
     312        case 'i':
     313            return PyInt_FromLong((long)va_arg(*p_va, int));
     314
     315        case 'H':
     316            return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
     317
     318        case 'I':
     319        {
     320            unsigned int n;
     321            n = va_arg(*p_va, unsigned int);
     322            if (n > (unsigned long)PyInt_GetMax())
     323                return PyLong_FromUnsignedLong((unsigned long)n);
     324            else
     325                return PyInt_FromLong(n);
     326        }
     327
     328        case 'n':
    328329#if SIZEOF_SIZE_T!=SIZEOF_LONG
    329                         return PyInt_FromSsize_t(va_arg(*p_va, Py_ssize_t));
     330            return PyInt_FromSsize_t(va_arg(*p_va, Py_ssize_t));
    330331#endif
    331                         /* Fall through from 'n' to 'l' if Py_ssize_t is long */
    332                 case 'l':
    333                         return PyInt_FromLong(va_arg(*p_va, long));
    334 
    335                 case 'k':
    336                 {
    337                         unsigned long n;
    338                         n = va_arg(*p_va, unsigned long);
    339                         if (n > (unsigned long)PyInt_GetMax())
    340                                 return PyLong_FromUnsignedLong(n);
    341                         else
    342                                 return PyInt_FromLong(n);
    343                 }
     332            /* Fall through from 'n' to 'l' if Py_ssize_t is long */
     333        case 'l':
     334            return PyInt_FromLong(va_arg(*p_va, long));
     335
     336        case 'k':
     337        {
     338            unsigned long n;
     339            n = va_arg(*p_va, unsigned long);
     340            if (n > (unsigned long)PyInt_GetMax())
     341                return PyLong_FromUnsignedLong(n);
     342            else
     343                return PyInt_FromLong(n);
     344        }
    344345
    345346#ifdef HAVE_LONG_LONG
    346                 case 'L':
    347                         return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
    348 
    349                 case 'K':
    350                         return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
     347        case 'L':
     348            return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
     349
     350        case 'K':
     351            return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
    351352#endif
    352353#ifdef Py_USING_UNICODE
    353                 case 'u':
    354                 {
    355                         PyObject *v;
    356                         Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
    357                         Py_ssize_t n;   
    358                         if (**p_format == '#') {
    359                                 ++*p_format;
    360                                 if (flags & FLAG_SIZE_T)
    361                                         n = va_arg(*p_va, Py_ssize_t);
    362                                 else
    363                                         n = va_arg(*p_va, int);
    364                         }
    365                         else
    366                                 n = -1;
    367                         if (u == NULL) {
    368                                 v = Py_None;
    369                                 Py_INCREF(v);
    370                         }
    371                         else {
    372                                 if (n < 0)
    373                                         n = _ustrlen(u);
    374                                 v = PyUnicode_FromUnicode(u, n);
    375                         }
    376                         return v;
    377                 }
     354        case 'u':
     355        {
     356            PyObject *v;
     357            Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
     358            Py_ssize_t n;
     359            if (**p_format == '#') {
     360                ++*p_format;
     361                if (flags & FLAG_SIZE_T)
     362                    n = va_arg(*p_va, Py_ssize_t);
     363                else
     364                    n = va_arg(*p_va, int);
     365            }
     366            else
     367                n = -1;
     368            if (u == NULL) {
     369                v = Py_None;
     370                Py_INCREF(v);
     371            }
     372            else {
     373                if (n < 0)
     374                    n = _ustrlen(u);
     375                v = PyUnicode_FromUnicode(u, n);
     376            }
     377            return v;
     378        }
    378379#endif
    379                 case 'f':
    380                 case 'd':
    381                         return PyFloat_FromDouble(
    382                                 (double)va_arg(*p_va, va_double));
     380        case 'f':
     381        case 'd':
     382            return PyFloat_FromDouble(
     383                (double)va_arg(*p_va, va_double));
    383384
    384385#ifndef WITHOUT_COMPLEX
    385                 case 'D':
    386                         return PyComplex_FromCComplex(
    387                                 *((Py_complex *)va_arg(*p_va, Py_complex *)));
     386        case 'D':
     387            return PyComplex_FromCComplex(
     388                *((Py_complex *)va_arg(*p_va, Py_complex *)));
    388389#endif /* WITHOUT_COMPLEX */
    389390
    390                 case 'c':
    391                 {
    392                         char p[1];
    393                         p[0] = (char)va_arg(*p_va, int);
    394                         return PyString_FromStringAndSize(p, 1);
    395                 }
    396 
    397                 case 's':
    398                 case 'z':
    399                 {
    400                         PyObject *v;
    401                         char *str = va_arg(*p_va, char *);
    402                         Py_ssize_t n;
    403                         if (**p_format == '#') {
    404                                 ++*p_format;
    405                                 if (flags & FLAG_SIZE_T)
    406                                         n = va_arg(*p_va, Py_ssize_t);
    407                                 else
    408                                         n = va_arg(*p_va, int);
    409                         }
    410                         else
    411                                 n = -1;
    412                         if (str == NULL) {
    413                                 v = Py_None;
    414                                 Py_INCREF(v);
    415                         }
    416                         else {
    417                                 if (n < 0) {
    418                                         size_t m = strlen(str);
    419                                         if (m > PY_SSIZE_T_MAX) {
    420                                                 PyErr_SetString(PyExc_OverflowError,
    421                                                         "string too long for Python string");
    422                                                 return NULL;
    423                                         }
    424                                         n = (Py_ssize_t)m;
    425                                 }
    426                                 v = PyString_FromStringAndSize(str, n);
    427                         }
    428                         return v;
    429                 }
    430 
    431                 case 'N':
    432                 case 'S':
    433                 case 'O':
    434                 if (**p_format == '&') {
    435                         typedef PyObject *(*converter)(void *);
    436                         converter func = va_arg(*p_va, converter);
    437                         void *arg = va_arg(*p_va, void *);
    438                         ++*p_format;
    439                         return (*func)(arg);
    440                 }
    441                 else {
    442                         PyObject *v;
    443                         v = va_arg(*p_va, PyObject *);
    444                         if (v != NULL) {
    445                                 if (*(*p_format - 1) != 'N')
    446                                         Py_INCREF(v);
    447                         }
    448                         else if (!PyErr_Occurred())
    449                                 /* If a NULL was passed
    450                                 * because a call that should
    451                                 * have constructed a value
    452                                 * failed, that's OK, and we
    453                                 * pass the error on; but if
    454                                 * no error occurred it's not
    455                                 * clear that the caller knew
    456                                 * what she was doing. */
    457                                 PyErr_SetString(PyExc_SystemError,
    458                                         "NULL object passed to Py_BuildValue");
    459                         return v;
    460                 }
    461 
    462                 case ':':
    463                 case ',':
    464                 case ' ':
    465                 case '\t':
    466                         break;
    467 
    468                 default:
    469                         PyErr_SetString(PyExc_SystemError,
    470                                 "bad format char passed to Py_BuildValue");
    471                         return NULL;
    472 
    473                 }
    474         }
     391        case 'c':
     392        {
     393            char p[1];
     394            p[0] = (char)va_arg(*p_va, int);
     395            return PyString_FromStringAndSize(p, 1);
     396        }
     397
     398        case 's':
     399        case 'z':
     400        {
     401            PyObject *v;
     402            char *str = va_arg(*p_va, char *);
     403            Py_ssize_t n;
     404            if (**p_format == '#') {
     405                ++*p_format;
     406                if (flags & FLAG_SIZE_T)
     407                    n = va_arg(*p_va, Py_ssize_t);
     408                else
     409                    n = va_arg(*p_va, int);
     410            }
     411            else
     412                n = -1;
     413            if (str == NULL) {
     414                v = Py_None;
     415                Py_INCREF(v);
     416            }
     417            else {
     418                if (n < 0) {
     419                    size_t m = strlen(str);
     420                    if (m > PY_SSIZE_T_MAX) {
     421                        PyErr_SetString(PyExc_OverflowError,
     422                            "string too long for Python string");
     423                        return NULL;
     424                    }
     425                    n = (Py_ssize_t)m;
     426                }
     427                v = PyString_FromStringAndSize(str, n);
     428            }
     429            return v;
     430        }
     431
     432        case 'N':
     433        case 'S':
     434        case 'O':
     435        if (**p_format == '&') {
     436            typedef PyObject *(*converter)(void *);
     437            converter func = va_arg(*p_va, converter);
     438            void *arg = va_arg(*p_va, void *);
     439            ++*p_format;
     440            return (*func)(arg);
     441        }
     442        else {
     443            PyObject *v;
     444            v = va_arg(*p_va, PyObject *);
     445            if (v != NULL) {
     446                if (*(*p_format - 1) != 'N')
     447                    Py_INCREF(v);
     448            }
     449            else if (!PyErr_Occurred())
     450                /* If a NULL was passed
     451                * because a call that should
     452                * have constructed a value
     453                * failed, that's OK, and we
     454                * pass the error on; but if
     455                * no error occurred it's not
     456                * clear that the caller knew
     457                * what she was doing. */
     458                PyErr_SetString(PyExc_SystemError,
     459                    "NULL object passed to Py_BuildValue");
     460            return v;
     461        }
     462
     463        case ':':
     464        case ',':
     465        case ' ':
     466        case '\t':
     467            break;
     468
     469        default:
     470            PyErr_SetString(PyExc_SystemError,
     471                "bad format char passed to Py_BuildValue");
     472            return NULL;
     473
     474        }
     475    }
    475476}
    476477
     
    479480Py_BuildValue(const char *format, ...)
    480481{
    481         va_list va;
    482         PyObject* retval;
    483         va_start(va, format);
    484         retval = va_build_value(format, va, 0);
    485         va_end(va);
    486         return retval;
     482    va_list va;
     483    PyObject* retval;
     484    va_start(va, format);
     485    retval = va_build_value(format, va, 0);
     486    va_end(va);
     487    return retval;
    487488}
    488489
     
    490491_Py_BuildValue_SizeT(const char *format, ...)
    491492{
    492         va_list va;
    493         PyObject* retval;
    494         va_start(va, format);
    495         retval = va_build_value(format, va, FLAG_SIZE_T);
    496         va_end(va);
    497         return retval;
     493    va_list va;
     494    PyObject* retval;
     495    va_start(va, format);
     496    retval = va_build_value(format, va, FLAG_SIZE_T);
     497    va_end(va);
     498    return retval;
    498499}
    499500
     
    501502Py_VaBuildValue(const char *format, va_list va)
    502503{
    503         return va_build_value(format, va, 0);
     504    return va_build_value(format, va, 0);
    504505}
    505506
     
    507508_Py_VaBuildValue_SizeT(const char *format, va_list va)
    508509{
    509         return va_build_value(format, va, FLAG_SIZE_T);
     510    return va_build_value(format, va, FLAG_SIZE_T);
    510511}
    511512
     
    513514va_build_value(const char *format, va_list va, int flags)
    514515{
    515         const char *f = format;
    516         int n = countformat(f, '\0');
    517         va_list lva;
     516    const char *f = format;
     517    int n = countformat(f, '\0');
     518    va_list lva;
    518519
    519520#ifdef VA_LIST_IS_ARRAY
    520         memcpy(lva, va, sizeof(va_list));
     521    memcpy(lva, va, sizeof(va_list));
    521522#else
    522523#ifdef __va_copy
    523         __va_copy(lva, va);
     524    __va_copy(lva, va);
    524525#else
    525         lva = va;
     526    lva = va;
    526527#endif
    527528#endif
    528529
    529         if (n < 0)
    530                 return NULL;
    531         if (n == 0) {
    532                 Py_INCREF(Py_None);
    533                 return Py_None;
    534         }
    535         if (n == 1)
    536                 return do_mkvalue(&f, &lva, flags);
    537         return do_mktuple(&f, &lva, '\0', n, flags);
     530    if (n < 0)
     531        return NULL;
     532    if (n == 0) {
     533        Py_INCREF(Py_None);
     534        return Py_None;
     535    }
     536    if (n == 1)
     537        return do_mkvalue(&f, &lva, flags);
     538    return do_mktuple(&f, &lva, '\0', n, flags);
    538539}
    539540
     
    542543PyEval_CallFunction(PyObject *obj, const char *format, ...)
    543544{
    544         va_list vargs;
    545         PyObject *args;
    546         PyObject *res;
    547 
    548         va_start(vargs, format);
    549 
    550         args = Py_VaBuildValue(format, vargs);
    551         va_end(vargs);
    552 
    553         if (args == NULL)
    554                 return NULL;
    555 
    556         res = PyEval_CallObject(obj, args);
    557         Py_DECREF(args);
    558 
    559         return res;
     545    va_list vargs;
     546    PyObject *args;
     547    PyObject *res;
     548
     549    va_start(vargs, format);
     550
     551    args = Py_VaBuildValue(format, vargs);
     552    va_end(vargs);
     553
     554    if (args == NULL)
     555        return NULL;
     556
     557    res = PyEval_CallObject(obj, args);
     558    Py_DECREF(args);
     559
     560    return res;
    560561}
    561562
     
    564565PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
    565566{
    566         va_list vargs;
    567         PyObject *meth;
    568         PyObject *args;
    569         PyObject *res;
    570 
    571         meth = PyObject_GetAttrString(obj, methodname);
    572         if (meth == NULL)
    573                 return NULL;
    574 
    575         va_start(vargs, format);
    576 
    577         args = Py_VaBuildValue(format, vargs);
    578         va_end(vargs);
    579 
    580         if (args == NULL) {
    581                 Py_DECREF(meth);
    582                 return NULL;
    583         }
    584 
    585         res = PyEval_CallObject(meth, args);
    586         Py_DECREF(meth);
    587         Py_DECREF(args);
    588 
    589         return res;
     567    va_list vargs;
     568    PyObject *meth;
     569    PyObject *args;
     570    PyObject *res;
     571
     572    meth = PyObject_GetAttrString(obj, methodname);
     573    if (meth == NULL)
     574        return NULL;
     575
     576    va_start(vargs, format);
     577
     578    args = Py_VaBuildValue(format, vargs);
     579    va_end(vargs);
     580
     581    if (args == NULL) {
     582        Py_DECREF(meth);
     583        return NULL;
     584    }
     585
     586    res = PyEval_CallObject(meth, args);
     587    Py_DECREF(meth);
     588    Py_DECREF(args);
     589
     590    return res;
    590591}
    591592
     
    593594PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
    594595{
    595         PyObject *dict;
    596         if (!PyModule_Check(m)) {
    597                 PyErr_SetString(PyExc_TypeError,
    598                             "PyModule_AddObject() needs module as first arg");
    599                 return -1;
    600         }
    601         if (!o) {
    602                 if (!PyErr_Occurred())
    603                         PyErr_SetString(PyExc_TypeError,
    604                                         "PyModule_AddObject() needs non-NULL value");
    605                 return -1;
    606         }
    607 
    608         dict = PyModule_GetDict(m);
    609         if (dict == NULL) {
    610                 /* Internal error -- modules must have a dict! */
    611                 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
    612                              PyModule_GetName(m));
    613                 return -1;
    614         }
    615         if (PyDict_SetItemString(dict, name, o))
    616                 return -1;
    617         Py_DECREF(o);
    618         return 0;
    619 }
    620 
    621 int 
     596    PyObject *dict;
     597    if (!PyModule_Check(m)) {
     598        PyErr_SetString(PyExc_TypeError,
     599                    "PyModule_AddObject() needs module as first arg");
     600        return -1;
     601    }
     602    if (!o) {
     603        if (!PyErr_Occurred())
     604            PyErr_SetString(PyExc_TypeError,
     605                            "PyModule_AddObject() needs non-NULL value");
     606        return -1;
     607    }
     608
     609    dict = PyModule_GetDict(m);
     610    if (dict == NULL) {
     611        /* Internal error -- modules must have a dict! */
     612        PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
     613                     PyModule_GetName(m));
     614        return -1;
     615    }
     616    if (PyDict_SetItemString(dict, name, o))
     617        return -1;
     618    Py_DECREF(o);
     619    return 0;
     620}
     621
     622int
    622623PyModule_AddIntConstant(PyObject *m, const char *name, long value)
    623624{
    624         PyObject *o = PyInt_FromLong(value);
    625         if (!o)
    626                 return -1;
    627         if (PyModule_AddObject(m, name, o) == 0)
    628                 return 0;
    629         Py_DECREF(o);
    630         return -1;
    631 }
    632 
    633 int 
     625    PyObject *o = PyInt_FromLong(value);
     626    if (!o)
     627        return -1;
     628    if (PyModule_AddObject(m, name, o) == 0)
     629        return 0;
     630    Py_DECREF(o);
     631    return -1;
     632}
     633
     634int
    634635PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
    635636{
    636         PyObject *o = PyString_FromString(value);
    637         if (!o)
    638                 return -1;
    639         if (PyModule_AddObject(m, name, o) == 0)
    640                 return 0;
    641         Py_DECREF(o);
    642         return -1;
    643 }
     637    PyObject *o = PyString_FromString(value);
     638    if (!o)
     639        return -1;
     640    if (PyModule_AddObject(m, name, o) == 0)
     641        return 0;
     642    Py_DECREF(o);
     643    return -1;
     644}
Note: See TracChangeset for help on using the changeset viewer.