blob: 74ca8a7cca190bf67a5e15f8c417a92338822019 [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 Stinner49d3f252010-10-17 01:24:53 +000029 PyObject *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
35 if ((m = _PyImport_FindExtensionUnicode(name, path)) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 Py_INCREF(m);
Victor Stinner49d3f252010-10-17 01:24:53 +000037 result = m;
38 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 }
40 lastdot = strrchr(name, '.');
41 if (lastdot == NULL) {
42 packagecontext = NULL;
43 shortname = name;
44 }
45 else {
46 packagecontext = name;
47 shortname = lastdot+1;
48 }
Guido van Rossumd5962ad1996-07-31 22:44:53 +000049
Victor Stinner42040fb2011-02-22 23:16:19 +000050 p0 = _PyImport_GetDynLoadFunc(shortname, pathname, fp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 p = (PyObject*(*)(void))p0;
52 if (PyErr_Occurred())
Victor Stinner49d3f252010-10-17 01:24:53 +000053 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 if (p == NULL) {
55 PyErr_Format(PyExc_ImportError,
56 "dynamic module does not define init function (PyInit_%.200s)",
57 shortname);
Victor Stinner49d3f252010-10-17 01:24:53 +000058 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 }
60 oldcontext = _Py_PackageContext;
61 _Py_PackageContext = packagecontext;
62 m = (*p)();
63 _Py_PackageContext = oldcontext;
64 if (m == NULL)
Victor Stinner49d3f252010-10-17 01:24:53 +000065 goto error;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 if (PyErr_Occurred()) {
68 Py_DECREF(m);
69 PyErr_Format(PyExc_SystemError,
70 "initialization of %s raised unreported exception",
71 shortname);
Victor Stinner49d3f252010-10-17 01:24:53 +000072 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 }
Martin v. Löwis1a214512008-06-11 05:26:20 +000074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 /* Remember pointer to module init function. */
76 def = PyModule_GetDef(m);
77 def->m_base.m_init = p;
Martin v. Löwis1a214512008-06-11 05:26:20 +000078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 /* Remember the filename as the __file__ attribute */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 if (PyModule_AddObject(m, "__file__", path) < 0)
81 PyErr_Clear(); /* Not important enough to report */
Victor Stinner49d3f252010-10-17 01:24:53 +000082 else
83 Py_INCREF(path);
Martin v. Löwise81e9b12003-09-04 18:45:59 +000084
Victor Stinner49d3f252010-10-17 01:24:53 +000085 if (_PyImport_FixupExtensionUnicode(m, name, path) < 0)
86 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 if (Py_VerboseFlag)
88 PySys_WriteStderr(
89 "import %s # dynamically loaded from %s\n",
90 name, pathname);
Victor Stinner49d3f252010-10-17 01:24:53 +000091 result = m;
92 goto finally;
93
94error:
95 result = NULL;
96finally:
97 Py_DECREF(path);
98 return result;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000099}
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000100
101#endif /* HAVE_DYNAMIC_LOADING */