1995-01-02 15:04:15 -04:00
|
|
|
|
|
|
|
/* Support for dynamic loading of extension modules */
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
#include "Python.h"
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1999-12-22 10:09:35 -04:00
|
|
|
/* ./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.
|
1995-01-02 15:04:15 -04:00
|
|
|
*/
|
1999-12-20 17:20:42 -04:00
|
|
|
#ifdef HAVE_DYNAMIC_LOADING
|
1995-01-02 15:04:15 -04:00
|
|
|
|
1999-12-22 10:09:35 -04:00
|
|
|
#include "importdl.h"
|
|
|
|
|
2011-04-04 18:05:53 -03:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
extern dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
|
|
|
|
PyObject *pathname, FILE *fp);
|
|
|
|
#else
|
2011-02-22 19:16:19 -04:00
|
|
|
extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
|
2010-05-09 12:52:27 -03:00
|
|
|
const char *pathname, FILE *fp);
|
2011-04-04 18:05:53 -03:00
|
|
|
#endif
|
1997-11-22 17:53:48 -04:00
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *
|
2011-03-14 16:54:07 -03:00
|
|
|
_PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
|
1995-01-02 15:04:15 -04:00
|
|
|
{
|
2011-05-07 07:46:05 -03:00
|
|
|
PyObject *m = NULL;
|
2011-04-04 18:05:53 -03:00
|
|
|
#ifndef MS_WINDOWS
|
2011-03-14 16:54:07 -03:00
|
|
|
PyObject *pathbytes;
|
2011-04-04 18:05:53 -03:00
|
|
|
#endif
|
2011-05-07 07:46:05 -03:00
|
|
|
PyObject *nameascii;
|
2011-03-14 16:54:07 -03:00
|
|
|
char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext;
|
2010-05-09 12:52:27 -03:00
|
|
|
dl_funcptr p0;
|
|
|
|
PyObject* (*p)(void);
|
|
|
|
struct PyModuleDef *def;
|
1999-12-20 17:20:42 -04:00
|
|
|
|
2011-03-14 16:54:07 -03:00
|
|
|
m = _PyImport_FindExtensionObject(name, path);
|
2011-03-07 13:20:56 -04:00
|
|
|
if (m != NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_INCREF(m);
|
2011-03-14 16:54:07 -03:00
|
|
|
return m;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2011-03-14 16:54:07 -03:00
|
|
|
|
2011-05-07 07:46:05 -03:00
|
|
|
/* 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;
|
|
|
|
|
2011-03-14 16:54:07 -03:00
|
|
|
lastdot = strrchr(namestr, '.');
|
2010-05-09 12:52:27 -03:00
|
|
|
if (lastdot == NULL) {
|
|
|
|
packagecontext = NULL;
|
2011-03-14 16:54:07 -03:00
|
|
|
shortname = namestr;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
else {
|
2011-03-14 16:54:07 -03:00
|
|
|
packagecontext = namestr;
|
2010-05-09 12:52:27 -03:00
|
|
|
shortname = lastdot+1;
|
|
|
|
}
|
1995-07-18 11:40:09 -03:00
|
|
|
|
2011-04-04 18:05:53 -03:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
p0 = _PyImport_GetDynLoadWindows(shortname, path, fp);
|
|
|
|
#else
|
2011-03-14 16:54:07 -03:00
|
|
|
pathbytes = PyUnicode_EncodeFSDefault(path);
|
|
|
|
if (pathbytes == NULL)
|
2011-05-07 07:46:05 -03:00
|
|
|
goto error;
|
2011-03-14 16:54:07 -03:00
|
|
|
p0 = _PyImport_GetDynLoadFunc(shortname,
|
|
|
|
PyBytes_AS_STRING(pathbytes), fp);
|
|
|
|
Py_DECREF(pathbytes);
|
2011-04-04 18:05:53 -03:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
p = (PyObject*(*)(void))p0;
|
|
|
|
if (PyErr_Occurred())
|
2011-05-07 07:46:05 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (p == NULL) {
|
2012-04-20 16:22:50 -03:00
|
|
|
PyObject *msg = PyUnicode_FromFormat("dynamic module does not define "
|
|
|
|
"init function (PyInit_%s)",
|
|
|
|
shortname);
|
|
|
|
PyErr_SetImportError(msg, name, path);
|
|
|
|
Py_DECREF(msg);
|
2011-05-07 07:46:05 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
oldcontext = _Py_PackageContext;
|
|
|
|
_Py_PackageContext = packagecontext;
|
|
|
|
m = (*p)();
|
|
|
|
_Py_PackageContext = oldcontext;
|
|
|
|
if (m == NULL)
|
2011-05-07 07:46:05 -03:00
|
|
|
goto error;
|
1995-01-02 15:04:15 -04:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
|
|
"initialization of %s raised unreported exception",
|
|
|
|
shortname);
|
2011-05-07 07:46:05 -03:00
|
|
|
goto error;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2008-06-11 02:26:20 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Remember pointer to module init function. */
|
|
|
|
def = PyModule_GetDef(m);
|
|
|
|
def->m_base.m_init = p;
|
2008-06-11 02:26:20 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Remember the filename as the __file__ attribute */
|
|
|
|
if (PyModule_AddObject(m, "__file__", path) < 0)
|
|
|
|
PyErr_Clear(); /* Not important enough to report */
|
2010-10-16 22:24:53 -03:00
|
|
|
else
|
|
|
|
Py_INCREF(path);
|
2003-09-04 15:45:59 -03:00
|
|
|
|
2011-03-14 16:54:07 -03:00
|
|
|
if (_PyImport_FixupExtensionObject(m, name, path) < 0)
|
2011-05-07 07:46:05 -03:00
|
|
|
goto error;
|
|
|
|
Py_DECREF(nameascii);
|
2011-03-14 16:54:07 -03:00
|
|
|
return m;
|
2011-05-07 07:46:05 -03:00
|
|
|
|
|
|
|
error:
|
|
|
|
Py_DECREF(nameascii);
|
|
|
|
Py_XDECREF(m);
|
|
|
|
return NULL;
|
1998-08-04 19:46:29 -03:00
|
|
|
}
|
1999-12-22 10:09:35 -04:00
|
|
|
|
|
|
|
#endif /* HAVE_DYNAMIC_LOADING */
|