No issues found
1 /* Support for dynamic loading of extension modules */
2
3 #include "Python.h"
4
5 /* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
6 supported on this platform. configure will then compile and link in one
7 of the dynload_*.c files, as appropriate. We will call a function in
8 those modules to get a function pointer to the module's init function.
9 */
10 #ifdef HAVE_DYNAMIC_LOADING
11
12 #include "importdl.h"
13
14 extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
15 const char *shortname,
16 const char *pathname, FILE *fp);
17
18
19
20 PyObject *
21 _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
22 {
23 PyObject *m;
24 char *lastdot, *shortname, *packagecontext, *oldcontext;
25 dl_funcptr p;
26
27 if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
28 Py_INCREF(m);
29 return m;
30 }
31 lastdot = strrchr(name, '.');
32 if (lastdot == NULL) {
33 packagecontext = NULL;
34 shortname = name;
35 }
36 else {
37 packagecontext = name;
38 shortname = lastdot+1;
39 }
40
41 p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
42 if (PyErr_Occurred())
43 return NULL;
44 if (p == NULL) {
45 PyErr_Format(PyExc_ImportError,
46 "dynamic module does not define init function (init%.200s)",
47 shortname);
48 return NULL;
49 }
50 oldcontext = _Py_PackageContext;
51 _Py_PackageContext = packagecontext;
52 (*p)();
53 _Py_PackageContext = oldcontext;
54 if (PyErr_Occurred())
55 return NULL;
56
57 m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
58 if (m == NULL) {
59 PyErr_SetString(PyExc_SystemError,
60 "dynamic module not initialized properly");
61 return NULL;
62 }
63 /* Remember the filename as the __file__ attribute */
64 if (PyModule_AddStringConstant(m, "__file__", pathname) < 0)
65 PyErr_Clear(); /* Not important enough to report */
66
67 if (_PyImport_FixupExtension(name, pathname) == NULL)
68 return NULL;
69 if (Py_VerboseFlag)
70 PySys_WriteStderr(
71 "import %s # dynamically loaded from %s\n",
72 name, pathname);
73 Py_INCREF(m);
74 return m;
75 }
76
77 #endif /* HAVE_DYNAMIC_LOADING */