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