blob: 9127d61de35a3b6368098f8cab3518ef35dc2093 [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 Stinner2d322272011-04-04 23:05:53 +020015#ifdef MS_WINDOWS
16extern dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
17 PyObject *pathname, FILE *fp);
18#else
Victor Stinner42040fb2011-02-22 23:16:19 +000019extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 const char *pathname, FILE *fp);
Victor Stinner2d322272011-04-04 23:05:53 +020021#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +000022
Guido van Rossum79f25d91997-04-29 20:08:16 +000023PyObject *
Victor Stinnerfefd70c2011-03-14 15:54:07 -040024_PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000025{
Victor Stinner65b7eff2011-05-07 12:46:05 +020026 PyObject *m = NULL;
Victor Stinner2d322272011-04-04 23:05:53 +020027#ifndef MS_WINDOWS
Victor Stinnerfefd70c2011-03-14 15:54:07 -040028 PyObject *pathbytes;
Victor Stinner2d322272011-04-04 23:05:53 +020029#endif
Victor Stinner65b7eff2011-05-07 12:46:05 +020030 PyObject *nameascii;
Victor Stinnerfefd70c2011-03-14 15:54:07 -040031 char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 dl_funcptr p0;
33 PyObject* (*p)(void);
34 struct PyModuleDef *def;
Guido van Rossum6ea90921999-12-20 21:20:42 +000035
Victor Stinnerfefd70c2011-03-14 15:54:07 -040036 m = _PyImport_FindExtensionObject(name, path);
Victor Stinner95872862011-03-07 18:20:56 +010037 if (m != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 Py_INCREF(m);
Victor Stinnerfefd70c2011-03-14 15:54:07 -040039 return m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -040041
Victor Stinner65b7eff2011-05-07 12:46:05 +020042 /* name must be encodable to ASCII because dynamic module must have a
43 function called "PyInit_NAME", they are written in C, and the C language
44 doesn't accept non-ASCII identifiers. */
45 nameascii = PyUnicode_AsEncodedString(name, "ascii", NULL);
46 if (nameascii == NULL)
47 return NULL;
48
49 namestr = PyBytes_AS_STRING(nameascii);
50 if (namestr == NULL)
51 goto error;
52
Victor Stinnerfefd70c2011-03-14 15:54:07 -040053 lastdot = strrchr(namestr, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 if (lastdot == NULL) {
55 packagecontext = NULL;
Victor Stinnerfefd70c2011-03-14 15:54:07 -040056 shortname = namestr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 }
58 else {
Victor Stinnerfefd70c2011-03-14 15:54:07 -040059 packagecontext = namestr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 shortname = lastdot+1;
61 }
Guido van Rossumd5962ad1996-07-31 22:44:53 +000062
Victor Stinner2d322272011-04-04 23:05:53 +020063#ifdef MS_WINDOWS
64 p0 = _PyImport_GetDynLoadWindows(shortname, path, fp);
65#else
Victor Stinnerfefd70c2011-03-14 15:54:07 -040066 pathbytes = PyUnicode_EncodeFSDefault(path);
67 if (pathbytes == NULL)
Victor Stinner65b7eff2011-05-07 12:46:05 +020068 goto error;
Victor Stinnerfefd70c2011-03-14 15:54:07 -040069 p0 = _PyImport_GetDynLoadFunc(shortname,
70 PyBytes_AS_STRING(pathbytes), fp);
71 Py_DECREF(pathbytes);
Victor Stinner2d322272011-04-04 23:05:53 +020072#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 p = (PyObject*(*)(void))p0;
74 if (PyErr_Occurred())
Victor Stinner65b7eff2011-05-07 12:46:05 +020075 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 if (p == NULL) {
77 PyErr_Format(PyExc_ImportError,
Victor Stinnerfefd70c2011-03-14 15:54:07 -040078 "dynamic module does not define init function"
79 " (PyInit_%s)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 shortname);
Victor Stinner65b7eff2011-05-07 12:46:05 +020081 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 }
83 oldcontext = _Py_PackageContext;
84 _Py_PackageContext = packagecontext;
85 m = (*p)();
86 _Py_PackageContext = oldcontext;
87 if (m == NULL)
Victor Stinner65b7eff2011-05-07 12:46:05 +020088 goto error;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 PyErr_Format(PyExc_SystemError,
92 "initialization of %s raised unreported exception",
93 shortname);
Victor Stinner65b7eff2011-05-07 12:46:05 +020094 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 }
Martin v. Löwis1a214512008-06-11 05:26:20 +000096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 /* Remember pointer to module init function. */
98 def = PyModule_GetDef(m);
99 def->m_base.m_init = p;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 /* Remember the filename as the __file__ attribute */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 if (PyModule_AddObject(m, "__file__", path) < 0)
103 PyErr_Clear(); /* Not important enough to report */
Victor Stinner49d3f252010-10-17 01:24:53 +0000104 else
105 Py_INCREF(path);
Martin v. Löwise81e9b12003-09-04 18:45:59 +0000106
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400107 if (_PyImport_FixupExtensionObject(m, name, path) < 0)
Victor Stinner65b7eff2011-05-07 12:46:05 +0200108 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 if (Py_VerboseFlag)
Victor Stinner95872862011-03-07 18:20:56 +0100110 PySys_FormatStderr(
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400111 "import %U # dynamically loaded from %R\n",
112 name, path);
Victor Stinner65b7eff2011-05-07 12:46:05 +0200113 Py_DECREF(nameascii);
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400114 return m;
Victor Stinner65b7eff2011-05-07 12:46:05 +0200115
116error:
117 Py_DECREF(nameascii);
118 Py_XDECREF(m);
119 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000120}
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000121
122#endif /* HAVE_DYNAMIC_LOADING */