Home | History | Annotate | Download | only in Python
      1 
      2 /* Module support implementation */
      3 
      4 #include "Python.h"
      5 
      6 #define FLAG_SIZE_T 1
      7 typedef double va_double;
      8 
      9 static PyObject *va_build_value(const char *, va_list, int);
     10 
     11 /* Package context -- the full module name for package imports */
     12 char *_Py_PackageContext = NULL;
     13 
     14 /* Py_InitModule4() parameters:
     15    - name is the module name
     16    - methods is the list of top-level functions
     17    - doc is the documentation string
     18    - passthrough is passed as self to functions defined in the module
     19    - api_version is the value of PYTHON_API_VERSION at the time the
     20      module was compiled
     21 
     22    Return value is a borrowed reference to the module object; or NULL
     23    if an error occurred (in Python 1.4 and before, errors were fatal).
     24    Errors may still leak memory.
     25 */
     26 
     27 static char api_version_warning[] =
     28 "Python C API version mismatch for module %.100s:\
     29  This Python has API version %d, module %.100s has version %d.";
     30 
     31 PyObject *
     32 Py_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     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;
    104 }
    105 
    106 
    107 /* Helper for mkvalue() to scan the length of a format */
    108 
    109 static int
    110 countformat(const char *format, int endchar)
    111 {
    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;
    147 }
    148 
    149 
    150 /* Generic function to create a value -- the inverse of getargs() */
    151 /* After an original idea and first implementation by Steven Miale */
    152 
    153 static PyObject *do_mktuple(const char**, va_list *, int, int, int);
    154 static PyObject *do_mklist(const char**, va_list *, int, int, int);
    155 static PyObject *do_mkdict(const char**, va_list *, int, int, int);
    156 static PyObject *do_mkvalue(const char**, va_list *, int);
    157 
    158 
    159 static void
    160 do_ignore(const char **p_format, va_list *p_va, int endchar, int n, int flags)
    161 {
    162     PyObject *v;
    163     int i;
    164     assert(PyErr_Occurred());
    165     v = PyTuple_New(n);
    166     for (i = 0; i < n; i++) {
    167         PyObject *exception, *value, *tb, *w;
    168         PyErr_Fetch(&exception, &value, &tb);
    169         w = do_mkvalue(p_format, p_va, flags);
    170         PyErr_Restore(exception, value, tb);
    171         if (w != NULL) {
    172             if (v != NULL) {
    173                 PyTuple_SET_ITEM(v, i, w);
    174             }
    175             else {
    176                 Py_DECREF(w);
    177             }
    178         }
    179     }
    180     Py_XDECREF(v);
    181     if (**p_format != endchar) {
    182         PyErr_SetString(PyExc_SystemError,
    183                         "Unmatched paren in format");
    184         return;
    185     }
    186     if (endchar)
    187         ++*p_format;
    188 }
    189 
    190 static PyObject *
    191 do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
    192 {
    193     PyObject *d;
    194     int i;
    195     if (n < 0)
    196         return NULL;
    197     if (n % 2) {
    198         PyErr_SetString(PyExc_SystemError,
    199                         "Bad dict format");
    200         do_ignore(p_format, p_va, endchar, n, flags);
    201         return NULL;
    202     }
    203     /* Note that we can't bail immediately on error as this will leak
    204        refcounts on any 'N' arguments. */
    205     if ((d = PyDict_New()) == NULL) {
    206         do_ignore(p_format, p_va, endchar, n, flags);
    207         return NULL;
    208     }
    209     for (i = 0; i < n; i+= 2) {
    210         PyObject *k, *v;
    211 
    212         k = do_mkvalue(p_format, p_va, flags);
    213         if (k == NULL) {
    214             do_ignore(p_format, p_va, endchar, n - i - 1, flags);
    215             Py_DECREF(d);
    216             return NULL;
    217         }
    218         v = do_mkvalue(p_format, p_va, flags);
    219         if (v == NULL || PyDict_SetItem(d, k, v) < 0) {
    220             do_ignore(p_format, p_va, endchar, n - i - 2, flags);
    221             Py_DECREF(k);
    222             Py_XDECREF(v);
    223             Py_DECREF(d);
    224             return NULL;
    225         }
    226         Py_DECREF(k);
    227         Py_DECREF(v);
    228     }
    229     if (**p_format != endchar) {
    230         Py_DECREF(d);
    231         PyErr_SetString(PyExc_SystemError,
    232                         "Unmatched paren in format");
    233         return NULL;
    234     }
    235     if (endchar)
    236         ++*p_format;
    237     return d;
    238 }
    239 
    240 static PyObject *
    241 do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
    242 {
    243     PyObject *v;
    244     int i;
    245     if (n < 0)
    246         return NULL;
    247     /* Note that we can't bail immediately on error as this will leak
    248        refcounts on any 'N' arguments. */
    249     v = PyList_New(n);
    250     if (v == NULL) {
    251         do_ignore(p_format, p_va, endchar, n, flags);
    252         return NULL;
    253     }
    254     for (i = 0; i < n; i++) {
    255         PyObject *w = do_mkvalue(p_format, p_va, flags);
    256         if (w == NULL) {
    257             do_ignore(p_format, p_va, endchar, n - i - 1, flags);
    258             Py_DECREF(v);
    259             return NULL;
    260         }
    261         PyList_SET_ITEM(v, i, w);
    262     }
    263     if (**p_format != endchar) {
    264         Py_DECREF(v);
    265         PyErr_SetString(PyExc_SystemError,
    266                         "Unmatched paren in format");
    267         return NULL;
    268     }
    269     if (endchar)
    270         ++*p_format;
    271     return v;
    272 }
    273 
    274 #ifdef Py_USING_UNICODE
    275 static int
    276 _ustrlen(Py_UNICODE *u)
    277 {
    278     int i = 0;
    279     Py_UNICODE *v = u;
    280     while (*v != 0) { i++; v++; }
    281     return i;
    282 }
    283 #endif
    284 
    285 static PyObject *
    286 do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
    287 {
    288     PyObject *v;
    289     int i;
    290     if (n < 0)
    291         return NULL;
    292     /* Note that we can't bail immediately on error as this will leak
    293        refcounts on any 'N' arguments. */
    294     if ((v = PyTuple_New(n)) == NULL) {
    295         do_ignore(p_format, p_va, endchar, n, flags);
    296         return NULL;
    297     }
    298     for (i = 0; i < n; i++) {
    299         PyObject *w = do_mkvalue(p_format, p_va, flags);
    300         if (w == NULL) {
    301             do_ignore(p_format, p_va, endchar, n - i - 1, flags);
    302             Py_DECREF(v);
    303             return NULL;
    304         }
    305         PyTuple_SET_ITEM(v, i, w);
    306     }
    307     if (**p_format != endchar) {
    308         Py_DECREF(v);
    309         PyErr_SetString(PyExc_SystemError,
    310                         "Unmatched paren in format");
    311         return NULL;
    312     }
    313     if (endchar)
    314         ++*p_format;
    315     return v;
    316 }
    317 
    318 static PyObject *
    319 do_mkvalue(const char **p_format, va_list *p_va, int flags)
    320 {
    321     for (;;) {
    322         switch (*(*p_format)++) {
    323         case '(':
    324             return do_mktuple(p_format, p_va, ')',
    325                               countformat(*p_format, ')'), flags);
    326 
    327         case '[':
    328             return do_mklist(p_format, p_va, ']',
    329                              countformat(*p_format, ']'), flags);
    330 
    331         case '{':
    332             return do_mkdict(p_format, p_va, '}',
    333                              countformat(*p_format, '}'), flags);
    334 
    335         case 'b':
    336         case 'B':
    337         case 'h':
    338         case 'i':
    339             return PyInt_FromLong((long)va_arg(*p_va, int));
    340 
    341         case 'H':
    342             return PyInt_FromLong((long)va_arg(*p_va, unsigned int));
    343 
    344         case 'I':
    345         {
    346             unsigned int n;
    347             n = va_arg(*p_va, unsigned int);
    348             if (n > (unsigned long)PyInt_GetMax())
    349                 return PyLong_FromUnsignedLong((unsigned long)n);
    350             else
    351                 return PyInt_FromLong(n);
    352         }
    353 
    354         case 'n':
    355 #if SIZEOF_SIZE_T!=SIZEOF_LONG
    356             return PyInt_FromSsize_t(va_arg(*p_va, Py_ssize_t));
    357 #endif
    358             /* Fall through from 'n' to 'l' if Py_ssize_t is long */
    359         case 'l':
    360             return PyInt_FromLong(va_arg(*p_va, long));
    361 
    362         case 'k':
    363         {
    364             unsigned long n;
    365             n = va_arg(*p_va, unsigned long);
    366             if (n > (unsigned long)PyInt_GetMax())
    367                 return PyLong_FromUnsignedLong(n);
    368             else
    369                 return PyInt_FromLong(n);
    370         }
    371 
    372 #ifdef HAVE_LONG_LONG
    373         case 'L':
    374             return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
    375 
    376         case 'K':
    377             return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
    378 #endif
    379 #ifdef Py_USING_UNICODE
    380         case 'u':
    381         {
    382             PyObject *v;
    383             Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
    384             Py_ssize_t n;
    385             if (**p_format == '#') {
    386                 ++*p_format;
    387                 if (flags & FLAG_SIZE_T)
    388                     n = va_arg(*p_va, Py_ssize_t);
    389                 else
    390                     n = va_arg(*p_va, int);
    391             }
    392             else
    393                 n = -1;
    394             if (u == NULL) {
    395                 v = Py_None;
    396                 Py_INCREF(v);
    397             }
    398             else {
    399                 if (n < 0)
    400                     n = _ustrlen(u);
    401                 v = PyUnicode_FromUnicode(u, n);
    402             }
    403             return v;
    404         }
    405 #endif
    406         case 'f':
    407         case 'd':
    408             return PyFloat_FromDouble(
    409                 (double)va_arg(*p_va, va_double));
    410 
    411 #ifndef WITHOUT_COMPLEX
    412         case 'D':
    413             return PyComplex_FromCComplex(
    414                 *((Py_complex *)va_arg(*p_va, Py_complex *)));
    415 #endif /* WITHOUT_COMPLEX */
    416 
    417         case 'c':
    418         {
    419             char p[1];
    420             p[0] = (char)va_arg(*p_va, int);
    421             return PyString_FromStringAndSize(p, 1);
    422         }
    423 
    424         case 's':
    425         case 'z':
    426         {
    427             PyObject *v;
    428             char *str = va_arg(*p_va, char *);
    429             Py_ssize_t n;
    430             if (**p_format == '#') {
    431                 ++*p_format;
    432                 if (flags & FLAG_SIZE_T)
    433                     n = va_arg(*p_va, Py_ssize_t);
    434                 else
    435                     n = va_arg(*p_va, int);
    436             }
    437             else
    438                 n = -1;
    439             if (str == NULL) {
    440                 v = Py_None;
    441                 Py_INCREF(v);
    442             }
    443             else {
    444                 if (n < 0) {
    445                     size_t m = strlen(str);
    446                     if (m > PY_SSIZE_T_MAX) {
    447                         PyErr_SetString(PyExc_OverflowError,
    448                             "string too long for Python string");
    449                         return NULL;
    450                     }
    451                     n = (Py_ssize_t)m;
    452                 }
    453                 v = PyString_FromStringAndSize(str, n);
    454             }
    455             return v;
    456         }
    457 
    458         case 'N':
    459         case 'S':
    460         case 'O':
    461         if (**p_format == '&') {
    462             typedef PyObject *(*converter)(void *);
    463             converter func = va_arg(*p_va, converter);
    464             void *arg = va_arg(*p_va, void *);
    465             ++*p_format;
    466             return (*func)(arg);
    467         }
    468         else {
    469             PyObject *v;
    470             v = va_arg(*p_va, PyObject *);
    471             if (v != NULL) {
    472                 if (*(*p_format - 1) != 'N')
    473                     Py_INCREF(v);
    474             }
    475             else if (!PyErr_Occurred())
    476                 /* If a NULL was passed
    477                  * because a call that should
    478                  * have constructed a value
    479                  * failed, that's OK, and we
    480                  * pass the error on; but if
    481                  * no error occurred it's not
    482                  * clear that the caller knew
    483                  * what she was doing. */
    484                 PyErr_SetString(PyExc_SystemError,
    485                     "NULL object passed to Py_BuildValue");
    486             return v;
    487         }
    488 
    489         case ':':
    490         case ',':
    491         case ' ':
    492         case '\t':
    493             break;
    494 
    495         default:
    496             PyErr_SetString(PyExc_SystemError,
    497                 "bad format char passed to Py_BuildValue");
    498             return NULL;
    499 
    500         }
    501     }
    502 }
    503 
    504 
    505 PyObject *
    506 Py_BuildValue(const char *format, ...)
    507 {
    508     va_list va;
    509     PyObject* retval;
    510     va_start(va, format);
    511     retval = va_build_value(format, va, 0);
    512     va_end(va);
    513     return retval;
    514 }
    515 
    516 PyObject *
    517 _Py_BuildValue_SizeT(const char *format, ...)
    518 {
    519     va_list va;
    520     PyObject* retval;
    521     va_start(va, format);
    522     retval = va_build_value(format, va, FLAG_SIZE_T);
    523     va_end(va);
    524     return retval;
    525 }
    526 
    527 PyObject *
    528 Py_VaBuildValue(const char *format, va_list va)
    529 {
    530     return va_build_value(format, va, 0);
    531 }
    532 
    533 PyObject *
    534 _Py_VaBuildValue_SizeT(const char *format, va_list va)
    535 {
    536     return va_build_value(format, va, FLAG_SIZE_T);
    537 }
    538 
    539 static PyObject *
    540 va_build_value(const char *format, va_list va, int flags)
    541 {
    542     const char *f = format;
    543     int n = countformat(f, '\0');
    544     va_list lva;
    545 
    546 #ifdef VA_LIST_IS_ARRAY
    547     memcpy(lva, va, sizeof(va_list));
    548 #else
    549 #ifdef __va_copy
    550     __va_copy(lva, va);
    551 #else
    552     lva = va;
    553 #endif
    554 #endif
    555 
    556     if (n < 0)
    557         return NULL;
    558     if (n == 0) {
    559         Py_INCREF(Py_None);
    560         return Py_None;
    561     }
    562     if (n == 1)
    563         return do_mkvalue(&f, &lva, flags);
    564     return do_mktuple(&f, &lva, '\0', n, flags);
    565 }
    566 
    567 
    568 PyObject *
    569 PyEval_CallFunction(PyObject *obj, const char *format, ...)
    570 {
    571     va_list vargs;
    572     PyObject *args;
    573     PyObject *res;
    574 
    575     va_start(vargs, format);
    576 
    577     args = Py_VaBuildValue(format, vargs);
    578     va_end(vargs);
    579 
    580     if (args == NULL)
    581         return NULL;
    582 
    583     res = PyEval_CallObject(obj, args);
    584     Py_DECREF(args);
    585 
    586     return res;
    587 }
    588 
    589 
    590 PyObject *
    591 PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
    592 {
    593     va_list vargs;
    594     PyObject *meth;
    595     PyObject *args;
    596     PyObject *res;
    597 
    598     meth = PyObject_GetAttrString(obj, methodname);
    599     if (meth == NULL)
    600         return NULL;
    601 
    602     va_start(vargs, format);
    603 
    604     args = Py_VaBuildValue(format, vargs);
    605     va_end(vargs);
    606 
    607     if (args == NULL) {
    608         Py_DECREF(meth);
    609         return NULL;
    610     }
    611 
    612     res = PyEval_CallObject(meth, args);
    613     Py_DECREF(meth);
    614     Py_DECREF(args);
    615 
    616     return res;
    617 }
    618 
    619 int
    620 PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
    621 {
    622     PyObject *dict;
    623     if (!PyModule_Check(m)) {
    624         PyErr_SetString(PyExc_TypeError,
    625                     "PyModule_AddObject() needs module as first arg");
    626         return -1;
    627     }
    628     if (!o) {
    629         if (!PyErr_Occurred())
    630             PyErr_SetString(PyExc_TypeError,
    631                             "PyModule_AddObject() needs non-NULL value");
    632         return -1;
    633     }
    634 
    635     dict = PyModule_GetDict(m);
    636     if (dict == NULL) {
    637         /* Internal error -- modules must have a dict! */
    638         PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
    639                      PyModule_GetName(m));
    640         return -1;
    641     }
    642     if (PyDict_SetItemString(dict, name, o))
    643         return -1;
    644     Py_DECREF(o);
    645     return 0;
    646 }
    647 
    648 int
    649 PyModule_AddIntConstant(PyObject *m, const char *name, long value)
    650 {
    651     PyObject *o = PyInt_FromLong(value);
    652     if (!o)
    653         return -1;
    654     if (PyModule_AddObject(m, name, o) == 0)
    655         return 0;
    656     Py_DECREF(o);
    657     return -1;
    658 }
    659 
    660 int
    661 PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
    662 {
    663     PyObject *o = PyString_FromString(value);
    664     if (!o)
    665         return -1;
    666     if (PyModule_AddObject(m, name, o) == 0)
    667         return 0;
    668     Py_DECREF(o);
    669     return -1;
    670 }
    671