blob: 14c9be87d31b33a4ba4cb76e98c13a7dbc19aa0c [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,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000016 const char *shortname,
17 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000024 PyObject *m;
25 char *lastdot, *shortname, *packagecontext, *oldcontext;
26 dl_funcptr p;
Guido van Rossum6ea90921999-12-20 21:20:42 +000027
Antoine Pitrouc83ea132010-05-09 14:46:46 +000028 if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
29 Py_INCREF(m);
30 return m;
31 }
32 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
Antoine Pitrouc83ea132010-05-09 14:46:46 +000042 p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
43 if (PyErr_Occurred())
44 return NULL;
45 if (p == NULL) {
46 PyErr_Format(PyExc_ImportError,
47 "dynamic module does not define init function (init%.200s)",
48 shortname);
49 return NULL;
50 }
51 oldcontext = _Py_PackageContext;
52 _Py_PackageContext = packagecontext;
53 (*p)();
54 _Py_PackageContext = oldcontext;
55 if (PyErr_Occurred())
56 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057
Antoine Pitrouc83ea132010-05-09 14:46:46 +000058 m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
59 if (m == NULL) {
60 PyErr_SetString(PyExc_SystemError,
61 "dynamic module not initialized properly");
62 return NULL;
63 }
64 /* Remember the filename as the __file__ attribute */
65 if (PyModule_AddStringConstant(m, "__file__", pathname) < 0)
66 PyErr_Clear(); /* Not important enough to report */
Martin v. Löwise81e9b12003-09-04 18:45:59 +000067
Antoine Pitrouc83ea132010-05-09 14:46:46 +000068 if (_PyImport_FixupExtension(name, pathname) == NULL)
69 return NULL;
70 if (Py_VerboseFlag)
71 PySys_WriteStderr(
72 "import %s # dynamically loaded from %s\n",
73 name, pathname);
74 Py_INCREF(m);
75 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000076}
Guido van Rossum96a8fb71999-12-22 14:09:35 +000077
78#endif /* HAVE_DYNAMIC_LOADING */