blob: 50231ff28848a18798ca6907408aba3ab450e20d [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
Nick Coghland5cacbb2015-05-23 22:24:10 +100016extern dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
17 const char *shortname,
18 PyObject *pathname,
19 FILE *fp);
Victor Stinner2d322272011-04-04 23:05:53 +020020#else
Nick Coghland5cacbb2015-05-23 22:24:10 +100021extern dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
22 const char *shortname,
23 const char *pathname, FILE *fp);
Victor Stinner2d322272011-04-04 23:05:53 +020024#endif
Guido van Rossum1ae940a1995-01-02 19:04:15 +000025
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020026static const char * const ascii_only_prefix = "PyInit";
27static const char * const nonascii_prefix = "PyInitU";
Nick Coghland5cacbb2015-05-23 22:24:10 +100028
29/* Get the variable part of a module's export symbol name.
30 * Returns a bytes instance. For non-ASCII-named modules, the name is
31 * encoded as per PEP 489.
32 * The hook_prefix pointer is set to either ascii_only_prefix or
33 * nonascii_prefix, as appropriate.
34 */
35static PyObject *
36get_encoded_name(PyObject *name, const char **hook_prefix) {
Nick Coghland5cacbb2015-05-23 22:24:10 +100037 PyObject *tmp;
38 PyObject *encoded = NULL;
Benjamin Petersone20056c2015-05-29 17:10:30 -050039 PyObject *modname = NULL;
40 Py_ssize_t name_len, lastdot;
41 _Py_IDENTIFIER(replace);
Nick Coghland5cacbb2015-05-23 22:24:10 +100042
43 /* Get the short name (substring after last dot) */
44 name_len = PyUnicode_GetLength(name);
45 lastdot = PyUnicode_FindChar(name, '.', 0, name_len, -1);
46 if (lastdot < -1) {
47 return NULL;
48 } else if (lastdot >= 0) {
Nick Coghlan55871f02015-05-26 21:48:17 +100049 tmp = PyUnicode_Substring(name, lastdot + 1, name_len);
Nick Coghland5cacbb2015-05-23 22:24:10 +100050 if (tmp == NULL)
51 return NULL;
52 name = tmp;
53 /* "name" now holds a new reference to the substring */
54 } else {
55 Py_INCREF(name);
56 }
57
58 /* Encode to ASCII or Punycode, as needed */
59 encoded = PyUnicode_AsEncodedString(name, "ascii", NULL);
60 if (encoded != NULL) {
61 *hook_prefix = ascii_only_prefix;
62 } else {
63 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
64 PyErr_Clear();
65 encoded = PyUnicode_AsEncodedString(name, "punycode", NULL);
66 if (encoded == NULL) {
67 goto error;
68 }
69 *hook_prefix = nonascii_prefix;
70 } else {
71 goto error;
72 }
73 }
74
Benjamin Petersone20056c2015-05-29 17:10:30 -050075 /* Replace '-' by '_' */
76 modname = _PyObject_CallMethodId(encoded, &PyId_replace, "cc", '-', '_');
77 if (modname == NULL)
78 goto error;
Nick Coghland5cacbb2015-05-23 22:24:10 +100079
80 Py_DECREF(name);
Benjamin Petersone20056c2015-05-29 17:10:30 -050081 Py_DECREF(encoded);
82 return modname;
Nick Coghland5cacbb2015-05-23 22:24:10 +100083error:
84 Py_DECREF(name);
85 Py_XDECREF(encoded);
86 return NULL;
87}
88
Guido van Rossum79f25d91997-04-29 20:08:16 +000089PyObject *
Nick Coghland5cacbb2015-05-23 22:24:10 +100090_PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000091{
Victor Stinner2d322272011-04-04 23:05:53 +020092#ifndef MS_WINDOWS
Nick Coghland5cacbb2015-05-23 22:24:10 +100093 PyObject *pathbytes = NULL;
Victor Stinner2d322272011-04-04 23:05:53 +020094#endif
Nick Coghland5cacbb2015-05-23 22:24:10 +100095 PyObject *name_unicode = NULL, *name = NULL, *path = NULL, *m = NULL;
96 const char *name_buf, *hook_prefix;
Serhiy Storchakab57d9ea2016-11-21 10:25:54 +020097 const char *oldcontext;
Nick Coghland5cacbb2015-05-23 22:24:10 +100098 dl_funcptr exportfunc;
99 PyModuleDef *def;
100 PyObject *(*p0)(void);
Guido van Rossum6ea90921999-12-20 21:20:42 +0000101
Nick Coghland5cacbb2015-05-23 22:24:10 +1000102 name_unicode = PyObject_GetAttrString(spec, "name");
103 if (name_unicode == NULL) {
104 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 }
Oren Milman9974e1b2017-09-19 14:39:47 +0300106 if (!PyUnicode_Check(name_unicode)) {
107 PyErr_SetString(PyExc_TypeError,
108 "spec.name must be a string");
109 goto error;
110 }
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400111
Nick Coghland5cacbb2015-05-23 22:24:10 +1000112 name = get_encoded_name(name_unicode, &hook_prefix);
113 if (name == NULL) {
114 goto error;
115 }
116 name_buf = PyBytes_AS_STRING(name);
Victor Stinner65b7eff2011-05-07 12:46:05 +0200117
Nick Coghland5cacbb2015-05-23 22:24:10 +1000118 path = PyObject_GetAttrString(spec, "origin");
119 if (path == NULL)
Victor Stinner65b7eff2011-05-07 12:46:05 +0200120 goto error;
121
Victor Stinner2d322272011-04-04 23:05:53 +0200122#ifdef MS_WINDOWS
Nick Coghland5cacbb2015-05-23 22:24:10 +1000123 exportfunc = _PyImport_FindSharedFuncptrWindows(hook_prefix, name_buf,
124 path, fp);
Victor Stinner2d322272011-04-04 23:05:53 +0200125#else
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400126 pathbytes = PyUnicode_EncodeFSDefault(path);
127 if (pathbytes == NULL)
Victor Stinner65b7eff2011-05-07 12:46:05 +0200128 goto error;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000129 exportfunc = _PyImport_FindSharedFuncptr(hook_prefix, name_buf,
130 PyBytes_AS_STRING(pathbytes),
131 fp);
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400132 Py_DECREF(pathbytes);
Victor Stinner2d322272011-04-04 23:05:53 +0200133#endif
Nick Coghland5cacbb2015-05-23 22:24:10 +1000134
135 if (exportfunc == NULL) {
136 if (!PyErr_Occurred()) {
137 PyObject *msg;
138 msg = PyUnicode_FromFormat(
139 "dynamic module does not define "
140 "module export function (%s_%s)",
141 hook_prefix, name_buf);
142 if (msg == NULL)
143 goto error;
144 PyErr_SetImportError(msg, name_unicode, path);
145 Py_DECREF(msg);
146 }
Victor Stinner65b7eff2011-05-07 12:46:05 +0200147 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000149
Nick Coghland5cacbb2015-05-23 22:24:10 +1000150 p0 = (PyObject *(*)(void))exportfunc;
151
152 /* Package context is needed for single-phase init */
153 oldcontext = _Py_PackageContext;
154 _Py_PackageContext = PyUnicode_AsUTF8(name_unicode);
Serhiy Storchaka144f77a2016-11-20 08:47:21 +0200155 if (_Py_PackageContext == NULL) {
156 _Py_PackageContext = oldcontext;
157 goto error;
158 }
Nick Coghland5cacbb2015-05-23 22:24:10 +1000159 m = p0();
160 _Py_PackageContext = oldcontext;
161
162 if (m == NULL) {
163 if (!PyErr_Occurred()) {
164 PyErr_Format(
165 PyExc_SystemError,
166 "initialization of %s failed without raising an exception",
167 name_buf);
168 }
169 goto error;
170 } else if (PyErr_Occurred()) {
171 PyErr_Clear();
172 PyErr_Format(
173 PyExc_SystemError,
174 "initialization of %s raised unreported exception",
175 name_buf);
176 m = NULL;
177 goto error;
178 }
179 if (Py_TYPE(m) == NULL) {
180 /* This can happen when a PyModuleDef is returned without calling
181 * PyModuleDef_Init on it
182 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 PyErr_Format(PyExc_SystemError,
Nick Coghland5cacbb2015-05-23 22:24:10 +1000184 "init function of %s returned uninitialized object",
185 name_buf);
186 m = NULL; /* prevent segfault in DECREF */
187 goto error;
188 }
189 if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
190 Py_DECREF(name_unicode);
191 Py_DECREF(name);
192 Py_DECREF(path);
193 return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
194 }
195
196 /* Fall back to single-phase init mechanism */
197
198 if (hook_prefix == nonascii_prefix) {
199 /* don't allow legacy init for non-ASCII module names */
200 PyErr_Format(
201 PyExc_SystemError,
202 "initialization of * did not return PyModuleDef",
203 name_buf);
Victor Stinner65b7eff2011-05-07 12:46:05 +0200204 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 }
Martin v. Löwis1a214512008-06-11 05:26:20 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 /* Remember pointer to module init function. */
208 def = PyModule_GetDef(m);
Christian Heimes7ce57d62013-07-11 13:02:30 +0200209 if (def == NULL) {
210 PyErr_Format(PyExc_SystemError,
211 "initialization of %s did not return an extension "
Nick Coghland5cacbb2015-05-23 22:24:10 +1000212 "module", name_buf);
Christian Heimes848ee092013-07-11 11:22:21 +0200213 goto error;
Christian Heimes7ce57d62013-07-11 13:02:30 +0200214 }
Nick Coghland5cacbb2015-05-23 22:24:10 +1000215 def->m_base.m_init = p0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 /* Remember the filename as the __file__ attribute */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (PyModule_AddObject(m, "__file__", path) < 0)
219 PyErr_Clear(); /* Not important enough to report */
Victor Stinner49d3f252010-10-17 01:24:53 +0000220 else
221 Py_INCREF(path);
Martin v. Löwise81e9b12003-09-04 18:45:59 +0000222
Eric Snowd393c1b2017-09-14 12:18:12 -0600223 PyObject *modules = PyImport_GetModuleDict();
224 if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0)
Victor Stinner65b7eff2011-05-07 12:46:05 +0200225 goto error;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000226
227 Py_DECREF(name_unicode);
228 Py_DECREF(name);
229 Py_DECREF(path);
230
Victor Stinnerfefd70c2011-03-14 15:54:07 -0400231 return m;
Victor Stinner65b7eff2011-05-07 12:46:05 +0200232
233error:
Nick Coghland5cacbb2015-05-23 22:24:10 +1000234 Py_DECREF(name_unicode);
235 Py_XDECREF(name);
236 Py_XDECREF(path);
Victor Stinner65b7eff2011-05-07 12:46:05 +0200237 Py_XDECREF(m);
238 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000239}
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000240
241#endif /* HAVE_DYNAMIC_LOADING */