blob: 0ea954cae0a860c1399a029a0acb8e14242d5f3c [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) {
Brett Cannonf0434e62012-04-20 15:22:50 -040077 PyObject *msg = PyUnicode_FromFormat("dynamic module does not define "
78 "init function (PyInit_%s)",
79 shortname);
80 PyErr_SetImportError(msg, name, path);
81 Py_DECREF(msg);
Victor Stinner65b7eff2011-05-07 12:46:05 +020082 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 }
84 oldcontext = _Py_PackageContext;
85 _Py_PackageContext = packagecontext;
86 m = (*p)();
87 _Py_PackageContext = oldcontext;
88 if (m == NULL)
Victor Stinner65b7eff2011-05-07 12:46:05 +020089 goto error;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 PyErr_Format(PyExc_SystemError,
93 "initialization of %s raised unreported exception",
94 shortname);
Victor Stinner65b7eff2011-05-07 12:46:05 +020095 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 }
Martin v. Löwis1a214512008-06-11 05:26:20 +000097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 /* Remember pointer to module init function. */
99 def = PyModule_GetDef(m);
Christian Heimes7ce57d62013-07-11 13:02:30 +0200100 if (def == NULL) {
101 PyErr_Format(PyExc_SystemError,
102 "initialization of %s did not return an extension "
103 "module", shortname);
Christian Heimes848ee092013-07-11 11:22:21 +0200104 goto error;
Christian Heimes7ce57d62013-07-11 13:02:30 +0200105 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 def->m_base.m_init = p;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 /* Remember the filename as the __file__ attribute */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 if (PyModule_AddObject(m, "__file__", path) < 0)
110 PyErr_Clear(); /* Not important enough to report */
Victor Stinner49d3f252010-10-17 01:24:53 +0000111 else
112 Py_INCREF(path);
Martin v. Löwise81e9b12003-09-04 18:45:59 +0000113
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400114 if (_PyImport_FixupExtensionObject(m, name, path) < 0)
Victor Stinner65b7eff2011-05-07 12:46:05 +0200115 goto error;
Victor Stinner65b7eff2011-05-07 12:46:05 +0200116 Py_DECREF(nameascii);
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400117 return m;
Victor Stinner65b7eff2011-05-07 12:46:05 +0200118
119error:
120 Py_DECREF(nameascii);
121 Py_XDECREF(m);
122 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000123}
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000124
125#endif /* HAVE_DYNAMIC_LOADING */