importdl.c 3.58 KB
Newer Older
1 2 3

/* Support for dynamic loading of extension modules */

4
#include "Python.h"
5

6 7 8 9
/* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
   supported on this platform. configure will then compile and link in one
   of the dynload_*.c files, as appropriate. We will call a function in
   those modules to get a function pointer to the module's init function.
10
*/
11
#ifdef HAVE_DYNAMIC_LOADING
12

13 14
#include "importdl.h"

15 16 17 18
#ifdef MS_WINDOWS
extern dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
                                              PyObject *pathname, FILE *fp);
#else
19
extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
20
                                           const char *pathname, FILE *fp);
21
#endif
22

23
PyObject *
24
_PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
25
{
26
    PyObject *m = NULL;
27
#ifndef MS_WINDOWS
28
    PyObject *pathbytes;
29
#endif
30
    PyObject *nameascii;
31
    char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext;
32 33 34
    dl_funcptr p0;
    PyObject* (*p)(void);
    struct PyModuleDef *def;
35

36
    m = _PyImport_FindExtensionObject(name, path);
37
    if (m != NULL) {
38
        Py_INCREF(m);
39
        return m;
40
    }
41

42 43 44 45 46 47 48 49 50 51 52
    /* name must be encodable to ASCII because dynamic module must have a
       function called "PyInit_NAME", they are written in C, and the C language
       doesn't accept non-ASCII identifiers. */
    nameascii = PyUnicode_AsEncodedString(name, "ascii", NULL);
    if (nameascii == NULL)
        return NULL;

    namestr = PyBytes_AS_STRING(nameascii);
    if (namestr == NULL)
        goto error;

53
    lastdot = strrchr(namestr, '.');
54 55
    if (lastdot == NULL) {
        packagecontext = NULL;
56
        shortname = namestr;
57 58
    }
    else {
59
        packagecontext = namestr;
60 61
        shortname = lastdot+1;
    }
62

63 64 65
#ifdef MS_WINDOWS
    p0 = _PyImport_GetDynLoadWindows(shortname, path, fp);
#else
66 67
    pathbytes = PyUnicode_EncodeFSDefault(path);
    if (pathbytes == NULL)
68
        goto error;
69 70 71
    p0 = _PyImport_GetDynLoadFunc(shortname,
                                  PyBytes_AS_STRING(pathbytes), fp);
    Py_DECREF(pathbytes);
72
#endif
73 74
    p = (PyObject*(*)(void))p0;
    if (PyErr_Occurred())
75
        goto error;
76
    if (p == NULL) {
77 78 79 80 81
        PyObject *msg = PyUnicode_FromFormat("dynamic module does not define "
                                             "init function (PyInit_%s)",
                                             shortname);
        PyErr_SetImportError(msg, name, path);
        Py_DECREF(msg);
82
        goto error;
83 84 85 86 87 88
    }
    oldcontext = _Py_PackageContext;
    _Py_PackageContext = packagecontext;
    m = (*p)();
    _Py_PackageContext = oldcontext;
    if (m == NULL)
89
        goto error;
90

91 92 93 94
    if (PyErr_Occurred()) {
        PyErr_Format(PyExc_SystemError,
                     "initialization of %s raised unreported exception",
                     shortname);
95
        goto error;
96
    }
97

98 99
    /* Remember pointer to module init function. */
    def = PyModule_GetDef(m);
100 101 102 103
    if (def == NULL) {
        PyErr_Format(PyExc_SystemError,
                     "initialization of %s did not return an extension "
                     "module", shortname);
104
        goto error;
105
    }
106
    def->m_base.m_init = p;
107

108 109 110
    /* Remember the filename as the __file__ attribute */
    if (PyModule_AddObject(m, "__file__", path) < 0)
        PyErr_Clear(); /* Not important enough to report */
111 112
    else
        Py_INCREF(path);
113

114
    if (_PyImport_FixupExtensionObject(m, name, path) < 0)
115 116
        goto error;
    Py_DECREF(nameascii);
117
    return m;
118 119 120 121 122

error:
    Py_DECREF(nameascii);
    Py_XDECREF(m);
    return NULL;
123
}
124 125

#endif /* HAVE_DYNAMIC_LOADING */