blob: b60f1c75fbd3445b3af476b9df9b5e6b597869ee [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);
Victor Stinner8e54b1c2013-07-17 21:53:23 +020080 if (msg == NULL)
81 goto error;
Brett Cannonf0434e62012-04-20 15:22:50 -040082 PyErr_SetImportError(msg, name, path);
83 Py_DECREF(msg);
Victor Stinner65b7eff2011-05-07 12:46:05 +020084 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 }
86 oldcontext = _Py_PackageContext;
87 _Py_PackageContext = packagecontext;
88 m = (*p)();
89 _Py_PackageContext = oldcontext;
90 if (m == NULL)
Victor Stinner65b7eff2011-05-07 12:46:05 +020091 goto error;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 PyErr_Format(PyExc_SystemError,
95 "initialization of %s raised unreported exception",
96 shortname);
Victor Stinner65b7eff2011-05-07 12:46:05 +020097 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 }
Martin v. Löwis1a214512008-06-11 05:26:20 +000099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 /* Remember pointer to module init function. */
101 def = PyModule_GetDef(m);
Christian Heimes7ce57d62013-07-11 13:02:30 +0200102 if (def == NULL) {
103 PyErr_Format(PyExc_SystemError,
104 "initialization of %s did not return an extension "
105 "module", shortname);
Christian Heimes848ee092013-07-11 11:22:21 +0200106 goto error;
Christian Heimes7ce57d62013-07-11 13:02:30 +0200107 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 def->m_base.m_init = p;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 /* Remember the filename as the __file__ attribute */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 if (PyModule_AddObject(m, "__file__", path) < 0)
112 PyErr_Clear(); /* Not important enough to report */
Victor Stinner49d3f252010-10-17 01:24:53 +0000113 else
114 Py_INCREF(path);
Martin v. Löwise81e9b12003-09-04 18:45:59 +0000115
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400116 if (_PyImport_FixupExtensionObject(m, name, path) < 0)
Victor Stinner65b7eff2011-05-07 12:46:05 +0200117 goto error;
Victor Stinner65b7eff2011-05-07 12:46:05 +0200118 Py_DECREF(nameascii);
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400119 return m;
Victor Stinner65b7eff2011-05-07 12:46:05 +0200120
121error:
122 Py_DECREF(nameascii);
123 Py_XDECREF(m);
124 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000125}
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000126
127#endif /* HAVE_DYNAMIC_LOADING */