1
2 /* Support for dynamic loading of extension modules */
3
4 #include "Python.h"
5 #include "pycore_call.h"
6 #include "pycore_import.h"
7 #include "pycore_pystate.h"
8 #include "pycore_runtime.h"
9
10 /* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
11 supported on this platform. configure will then compile and link in one
12 of the dynload_*.c files, as appropriate. We will call a function in
13 those modules to get a function pointer to the module's init function.
14 */
15 #ifdef HAVE_DYNAMIC_LOADING
16
17 #include "importdl.h"
18
19 #ifdef MS_WINDOWS
20 extern dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
21 const char *shortname,
22 PyObject *pathname,
23 FILE *fp);
24 #else
25 extern dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
26 const char *shortname,
27 const char *pathname, FILE *fp);
28 #endif
29
30 static const char * const ascii_only_prefix = "PyInit";
31 static const char * const nonascii_prefix = "PyInitU";
32
33 /* Get the variable part of a module's export symbol name.
34 * Returns a bytes instance. For non-ASCII-named modules, the name is
35 * encoded as per PEP 489.
36 * The hook_prefix pointer is set to either ascii_only_prefix or
37 * nonascii_prefix, as appropriate.
38 */
39 static PyObject *
40 get_encoded_name(PyObject *name, const char **hook_prefix) {
41 PyObject *tmp;
42 PyObject *encoded = NULL;
43 PyObject *modname = NULL;
44 Py_ssize_t name_len, lastdot;
45
46 /* Get the short name (substring after last dot) */
47 name_len = PyUnicode_GetLength(name);
48 if (name_len < 0) {
49 return NULL;
50 }
51 lastdot = PyUnicode_FindChar(name, '.', 0, name_len, -1);
52 if (lastdot < -1) {
53 return NULL;
54 } else if (lastdot >= 0) {
55 tmp = PyUnicode_Substring(name, lastdot + 1, name_len);
56 if (tmp == NULL)
57 return NULL;
58 name = tmp;
59 /* "name" now holds a new reference to the substring */
60 } else {
61 Py_INCREF(name);
62 }
63
64 /* Encode to ASCII or Punycode, as needed */
65 encoded = PyUnicode_AsEncodedString(name, "ascii", NULL);
66 if (encoded != NULL) {
67 *hook_prefix = ascii_only_prefix;
68 } else {
69 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
70 PyErr_Clear();
71 encoded = PyUnicode_AsEncodedString(name, "punycode", NULL);
72 if (encoded == NULL) {
73 goto error;
74 }
75 *hook_prefix = nonascii_prefix;
76 } else {
77 goto error;
78 }
79 }
80
81 /* Replace '-' by '_' */
82 modname = _PyObject_CallMethod(encoded, &_Py_ID(replace), "cc", '-', '_');
83 if (modname == NULL)
84 goto error;
85
86 Py_DECREF(name);
87 Py_DECREF(encoded);
88 return modname;
89 error:
90 Py_DECREF(name);
91 Py_XDECREF(encoded);
92 return NULL;
93 }
94
95 PyObject *
96 _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
97 {
98 #ifndef MS_WINDOWS
99 PyObject *pathbytes = NULL;
100 #endif
101 PyObject *name_unicode = NULL, *name = NULL, *path = NULL, *m = NULL;
102 const char *name_buf, *hook_prefix;
103 const char *oldcontext, *newcontext;
104 dl_funcptr exportfunc;
105 PyModuleDef *def;
106 PyModInitFunction p0;
107
108 name_unicode = PyObject_GetAttrString(spec, "name");
109 if (name_unicode == NULL) {
110 return NULL;
111 }
112 if (!PyUnicode_Check(name_unicode)) {
113 PyErr_SetString(PyExc_TypeError,
114 "spec.name must be a string");
115 goto error;
116 }
117 newcontext = PyUnicode_AsUTF8(name_unicode);
118 if (newcontext == NULL) {
119 goto error;
120 }
121
122 name = get_encoded_name(name_unicode, &hook_prefix);
123 if (name == NULL) {
124 goto error;
125 }
126 name_buf = PyBytes_AS_STRING(name);
127
128 path = PyObject_GetAttrString(spec, "origin");
129 if (path == NULL)
130 goto error;
131
132 if (PySys_Audit("import", "OOOOO", name_unicode, path,
133 Py_None, Py_None, Py_None) < 0) {
134 goto error;
135 }
136
137 #ifdef MS_WINDOWS
138 exportfunc = _PyImport_FindSharedFuncptrWindows(hook_prefix, name_buf,
139 path, fp);
140 #else
141 pathbytes = PyUnicode_EncodeFSDefault(path);
142 if (pathbytes == NULL)
143 goto error;
144 exportfunc = _PyImport_FindSharedFuncptr(hook_prefix, name_buf,
145 PyBytes_AS_STRING(pathbytes),
146 fp);
147 Py_DECREF(pathbytes);
148 #endif
149
150 if (exportfunc == NULL) {
151 if (!PyErr_Occurred()) {
152 PyObject *msg;
153 msg = PyUnicode_FromFormat(
154 "dynamic module does not define "
155 "module export function (%s_%s)",
156 hook_prefix, name_buf);
157 if (msg == NULL)
158 goto error;
159 PyErr_SetImportError(msg, name_unicode, path);
160 Py_DECREF(msg);
161 }
162 goto error;
163 }
164
165 p0 = (PyModInitFunction)exportfunc;
166
167 /* Package context is needed for single-phase init */
168 oldcontext = _PyImport_SwapPackageContext(newcontext);
169 m = _PyImport_InitFunc_TrampolineCall(p0);
170 _PyImport_SwapPackageContext(oldcontext);
171
172 if (m == NULL) {
173 if (!PyErr_Occurred()) {
174 PyErr_Format(
175 PyExc_SystemError,
176 "initialization of %s failed without raising an exception",
177 name_buf);
178 }
179 goto error;
180 } else if (PyErr_Occurred()) {
181 _PyErr_FormatFromCause(
182 PyExc_SystemError,
183 "initialization of %s raised unreported exception",
184 name_buf);
185 m = NULL;
186 goto error;
187 }
188 if (Py_IS_TYPE(m, NULL)) {
189 /* This can happen when a PyModuleDef is returned without calling
190 * PyModuleDef_Init on it
191 */
192 PyErr_Format(PyExc_SystemError,
193 "init function of %s returned uninitialized object",
194 name_buf);
195 m = NULL; /* prevent segfault in DECREF */
196 goto error;
197 }
198 if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
199 Py_DECREF(name_unicode);
200 Py_DECREF(name);
201 Py_DECREF(path);
202 return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
203 }
204
205 /* Fall back to single-phase init mechanism */
206
207 if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
208 goto error;
209 }
210
211 if (hook_prefix == nonascii_prefix) {
212 /* don't allow legacy init for non-ASCII module names */
213 PyErr_Format(
214 PyExc_SystemError,
215 "initialization of %s did not return PyModuleDef",
216 name_buf);
217 goto error;
218 }
219
220 /* Remember pointer to module init function. */
221 def = PyModule_GetDef(m);
222 if (def == NULL) {
223 PyErr_Format(PyExc_SystemError,
224 "initialization of %s did not return an extension "
225 "module", name_buf);
226 goto error;
227 }
228 def->m_base.m_init = p0;
229
230 /* Remember the filename as the __file__ attribute */
231 if (PyModule_AddObjectRef(m, "__file__", path) < 0) {
232 PyErr_Clear(); /* Not important enough to report */
233 }
234
235 PyObject *modules = PyImport_GetModuleDict();
236 if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0)
237 goto error;
238
239 Py_DECREF(name_unicode);
240 Py_DECREF(name);
241 Py_DECREF(path);
242
243 return m;
244
245 error:
246 Py_DECREF(name_unicode);
247 Py_XDECREF(name);
248 Py_XDECREF(path);
249 Py_XDECREF(m);
250 return NULL;
251 }
252
253 #endif /* HAVE_DYNAMIC_LOADING */