blob: f0e1f55d2730572f6ba94bc9e27ec4927f73560f [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
Victor Stinner42040fb2011-02-22 23:16:19 +000015extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 const char *pathname, FILE *fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000017
Guido van Rossum38234201996-07-31 17:55:19 +000018
Guido van Rossum6ea90921999-12-20 21:20:42 +000019
Guido van Rossum79f25d91997-04-29 20:08:16 +000020PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000021_PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000023 PyObject *m;
24 PyObject *path;
25 char *lastdot, *shortname, *packagecontext, *oldcontext;
26 dl_funcptr p0;
27 PyObject* (*p)(void);
28 struct PyModuleDef *def;
Victor Stinner95872862011-03-07 18:20:56 +010029 PyObject *nameobj, *result;
Guido van Rossum6ea90921999-12-20 21:20:42 +000030
Victor Stinner49d3f252010-10-17 01:24:53 +000031 path = PyUnicode_DecodeFSDefault(pathname);
32 if (path == NULL)
33 return NULL;
34
Victor Stinner95872862011-03-07 18:20:56 +010035 nameobj = PyUnicode_FromString(name);
36 if (nameobj == NULL)
37 return NULL;
38 m = _PyImport_FindExtensionObject(nameobj, path);
39 if (m != NULL) {
40 Py_DECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 Py_INCREF(m);
Victor Stinner49d3f252010-10-17 01:24:53 +000042 result = m;
43 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 }
Victor Stinner95872862011-03-07 18:20:56 +010045 Py_DECREF(nameobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 lastdot = strrchr(name, '.');
47 if (lastdot == NULL) {
48 packagecontext = NULL;
49 shortname = name;
50 }
51 else {
52 packagecontext = name;
53 shortname = lastdot+1;
54 }
Guido van Rossumd5962ad1996-07-31 22:44:53 +000055
Victor Stinner42040fb2011-02-22 23:16:19 +000056 p0 = _PyImport_GetDynLoadFunc(shortname, pathname, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 p = (PyObject*(*)(void))p0;
58 if (PyErr_Occurred())
Victor Stinner49d3f252010-10-17 01:24:53 +000059 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 if (p == NULL) {
61 PyErr_Format(PyExc_ImportError,
62 "dynamic module does not define init function (PyInit_%.200s)",
63 shortname);
Victor Stinner49d3f252010-10-17 01:24:53 +000064 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 }
66 oldcontext = _Py_PackageContext;
67 _Py_PackageContext = packagecontext;
68 m = (*p)();
69 _Py_PackageContext = oldcontext;
70 if (m == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +000071 goto error;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (PyErr_Occurred()) {
74 Py_DECREF(m);
75 PyErr_Format(PyExc_SystemError,
76 "initialization of %s raised unreported exception",
77 shortname);
Victor Stinner49d3f252010-10-17 01:24:53 +000078 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 }
Martin v. Löwis1a214512008-06-11 05:26:20 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 /* Remember pointer to module init function. */
82 def = PyModule_GetDef(m);
83 def->m_base.m_init = p;
Martin v. Löwis1a214512008-06-11 05:26:20 +000084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 /* Remember the filename as the __file__ attribute */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 if (PyModule_AddObject(m, "__file__", path) < 0)
87 PyErr_Clear(); /* Not important enough to report */
Victor Stinner49d3f252010-10-17 01:24:53 +000088 else
89 Py_INCREF(path);
Martin v. Löwise81e9b12003-09-04 18:45:59 +000090
Victor Stinner95872862011-03-07 18:20:56 +010091 nameobj = PyUnicode_FromString(name);
92 if (nameobj == NULL)
93 return NULL;
94 if (_PyImport_FixupExtensionObject(m, nameobj, path) < 0) {
95 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +000096 goto error;
Victor Stinner95872862011-03-07 18:20:56 +010097 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +010099 PySys_FormatStderr(
100 "import %U # dynamically loaded from %s\n",
101 nameobj, pathname);
102 Py_DECREF(nameobj);
Victor Stinner49d3f252010-10-17 01:24:53 +0000103 result = m;
104 goto finally;
105
106error:
107 result = NULL;
108finally:
109 Py_DECREF(path);
110 return result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000111}
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000112
113#endif /* HAVE_DYNAMIC_LOADING */