blob: 9caed453aaaaf51ff676ff287a282269f2b55819 [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 Pitrouf95a1b32010-05-09 15:52:27 +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 Pitrouf95a1b32010-05-09 15:52:27 +000024 PyObject *m;
25 PyObject *path;
26 char *lastdot, *shortname, *packagecontext, *oldcontext;
27 dl_funcptr p0;
28 PyObject* (*p)(void);
29 struct PyModuleDef *def;
Victor Stinner49d3f252010-10-17 01:24:53 +000030 PyObject *result;
Guido van Rossum6ea90921999-12-20 21:20:42 +000031
Victor Stinner49d3f252010-10-17 01:24:53 +000032 path = PyUnicode_DecodeFSDefault(pathname);
33 if (path == NULL)
34 return NULL;
35
36 if ((m = _PyImport_FindExtensionUnicode(name, path)) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 Py_INCREF(m);
Victor Stinner49d3f252010-10-17 01:24:53 +000038 result = m;
39 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 }
41 lastdot = strrchr(name, '.');
42 if (lastdot == NULL) {
43 packagecontext = NULL;
44 shortname = name;
45 }
46 else {
47 packagecontext = name;
48 shortname = lastdot+1;
49 }
Guido van Rossumd5962ad1996-07-31 22:44:53 +000050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 p0 = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
52 p = (PyObject*(*)(void))p0;
53 if (PyErr_Occurred())
Victor Stinner49d3f252010-10-17 01:24:53 +000054 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 if (p == NULL) {
56 PyErr_Format(PyExc_ImportError,
57 "dynamic module does not define init function (PyInit_%.200s)",
58 shortname);
Victor Stinner49d3f252010-10-17 01:24:53 +000059 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 }
61 oldcontext = _Py_PackageContext;
62 _Py_PackageContext = packagecontext;
63 m = (*p)();
64 _Py_PackageContext = oldcontext;
65 if (m == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +000066 goto error;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (PyErr_Occurred()) {
69 Py_DECREF(m);
70 PyErr_Format(PyExc_SystemError,
71 "initialization of %s raised unreported exception",
72 shortname);
Victor Stinner49d3f252010-10-17 01:24:53 +000073 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 }
Martin v. Löwis1a214512008-06-11 05:26:20 +000075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 /* Remember pointer to module init function. */
77 def = PyModule_GetDef(m);
78 def->m_base.m_init = p;
Martin v. Löwis1a214512008-06-11 05:26:20 +000079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 /* Remember the filename as the __file__ attribute */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (PyModule_AddObject(m, "__file__", path) < 0)
82 PyErr_Clear(); /* Not important enough to report */
Victor Stinner49d3f252010-10-17 01:24:53 +000083 else
84 Py_INCREF(path);
Martin v. Löwise81e9b12003-09-04 18:45:59 +000085
Victor Stinner49d3f252010-10-17 01:24:53 +000086 if (_PyImport_FixupExtensionUnicode(m, name, path) < 0)
87 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (Py_VerboseFlag)
89 PySys_WriteStderr(
90 "import %s # dynamically loaded from %s\n",
91 name, pathname);
Victor Stinner49d3f252010-10-17 01:24:53 +000092 result = m;
93 goto finally;
94
95error:
96 result = NULL;
97finally:
98 Py_DECREF(path);
99 return result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000100}
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000101
102#endif /* HAVE_DYNAMIC_LOADING */