blob: 4a1048d4960fd34b6132f252a625c94626bd0424 [file] [log] [blame]
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001
2/* Support for dynamic loading of extension modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +00005
Guido van Rossum96a8fb71999-12-22 14:09:35 +00006/* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
7 supported on this platform. configure will then compile and link in one
8 of the dynload_*.c files, as appropriate. We will call a function in
9 those modules to get a function pointer to the module's init function.
Guido van Rossum1ae940a1995-01-02 19:04:15 +000010*/
Guido van Rossum6ea90921999-12-20 21:20:42 +000011#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumff4af061996-01-12 01:17:50 +000012
Guido van Rossum96a8fb71999-12-22 14:09:35 +000013#include "importdl.h"
14
Guido van Rossum6ea90921999-12-20 21:20:42 +000015extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
Guido van Rossum96a8fb71999-12-22 14:09:35 +000016 const char *shortname,
Guido van Rossum6ea90921999-12-20 21:20:42 +000017 const char *pathname, FILE *fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000018
Guido van Rossum38234201996-07-31 17:55:19 +000019
Guido van Rossum6ea90921999-12-20 21:20:42 +000020
Guido van Rossum79f25d91997-04-29 20:08:16 +000021PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000022_PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000023{
Guido van Rossum79f25d91997-04-29 20:08:16 +000024 PyObject *m, *d, *s;
Guido van Rossum2e58ff31997-11-19 18:53:33 +000025 char *lastdot, *shortname, *packagecontext;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000026 dl_funcptr p;
Guido van Rossum6ea90921999-12-20 21:20:42 +000027
Guido van Rossum25ce5661997-08-02 03:10:38 +000028 if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
29 Py_INCREF(m);
30 return m;
31 }
Guido van Rossum2e58ff31997-11-19 18:53:33 +000032 lastdot = strrchr(name, '.');
33 if (lastdot == NULL) {
34 packagecontext = NULL;
35 shortname = name;
36 }
37 else {
38 packagecontext = name;
39 shortname = lastdot+1;
40 }
Guido van Rossumd5962ad1996-07-31 22:44:53 +000041
Guido van Rossum96a8fb71999-12-22 14:09:35 +000042 p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
Guido van Rossum6ea90921999-12-20 21:20:42 +000043 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +000044 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000045 if (p == NULL) {
Guido van Rossum2e58ff31997-11-19 18:53:33 +000046 PyErr_Format(PyExc_ImportError,
Guido van Rossum96a8fb71999-12-22 14:09:35 +000047 "dynamic module does not define init function (init%.200s)",
48 shortname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049 return NULL;
50 }
Guido van Rossum2e58ff31997-11-19 18:53:33 +000051 _Py_PackageContext = packagecontext;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000052 (*p)();
Guido van Rossum2e58ff31997-11-19 18:53:33 +000053 _Py_PackageContext = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000054 if (PyErr_Occurred())
55 return NULL;
56 if (_PyImport_FixupExtension(name, pathname) == NULL)
57 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000058
Guido van Rossumef3d02e1997-07-21 14:54:36 +000059 m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000060 if (m == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000061 PyErr_SetString(PyExc_SystemError,
62 "dynamic module not initialized properly");
Guido van Rossum1ae940a1995-01-02 19:04:15 +000063 return NULL;
64 }
Guido van Rossum1e612491996-08-19 22:12:10 +000065 /* Remember the filename as the __file__ attribute */
Guido van Rossum79f25d91997-04-29 20:08:16 +000066 d = PyModule_GetDict(m);
67 s = PyString_FromString(pathname);
68 if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0)
69 PyErr_Clear(); /* Not important enough to report */
70 Py_XDECREF(s);
71 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +000072 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +000073 "import %s # dynamically loaded from %s\n",
74 name, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +000075 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000076 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000077}
Guido van Rossum96a8fb71999-12-22 14:09:35 +000078
79#endif /* HAVE_DYNAMIC_LOADING */