Branch data Line data Source code
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 : 114 : get_encoded_name(PyObject *name, const char **hook_prefix) {
41 : : PyObject *tmp;
42 : 114 : PyObject *encoded = NULL;
43 : 114 : PyObject *modname = NULL;
44 : : Py_ssize_t name_len, lastdot;
45 : :
46 : : /* Get the short name (substring after last dot) */
47 : 114 : name_len = PyUnicode_GetLength(name);
48 [ - + ]: 114 : if (name_len < 0) {
49 : 0 : return NULL;
50 : : }
51 : 114 : lastdot = PyUnicode_FindChar(name, '.', 0, name_len, -1);
52 [ - + ]: 114 : if (lastdot < -1) {
53 : 0 : return NULL;
54 [ - + ]: 114 : } else if (lastdot >= 0) {
55 : 0 : tmp = PyUnicode_Substring(name, lastdot + 1, name_len);
56 [ # # ]: 0 : if (tmp == NULL)
57 : 0 : return NULL;
58 : 0 : name = tmp;
59 : : /* "name" now holds a new reference to the substring */
60 : : } else {
61 : 114 : Py_INCREF(name);
62 : : }
63 : :
64 : : /* Encode to ASCII or Punycode, as needed */
65 : 114 : encoded = PyUnicode_AsEncodedString(name, "ascii", NULL);
66 [ + - ]: 114 : if (encoded != NULL) {
67 : 114 : *hook_prefix = ascii_only_prefix;
68 : : } else {
69 [ # # ]: 0 : if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) {
70 : 0 : PyErr_Clear();
71 : 0 : encoded = PyUnicode_AsEncodedString(name, "punycode", NULL);
72 [ # # ]: 0 : if (encoded == NULL) {
73 : 0 : goto error;
74 : : }
75 : 0 : *hook_prefix = nonascii_prefix;
76 : : } else {
77 : 0 : goto error;
78 : : }
79 : : }
80 : :
81 : : /* Replace '-' by '_' */
82 : 114 : modname = _PyObject_CallMethod(encoded, &_Py_ID(replace), "cc", '-', '_');
83 [ - + ]: 114 : if (modname == NULL)
84 : 0 : goto error;
85 : :
86 : 114 : Py_DECREF(name);
87 : 114 : Py_DECREF(encoded);
88 : 114 : return modname;
89 : 0 : error:
90 : 0 : Py_DECREF(name);
91 : 0 : Py_XDECREF(encoded);
92 : 0 : return NULL;
93 : : }
94 : :
95 : : PyObject *
96 : 114 : _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
97 : : {
98 : : #ifndef MS_WINDOWS
99 : 114 : PyObject *pathbytes = NULL;
100 : : #endif
101 : 114 : 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 : 114 : name_unicode = PyObject_GetAttrString(spec, "name");
109 [ - + ]: 114 : if (name_unicode == NULL) {
110 : 0 : return NULL;
111 : : }
112 [ - + ]: 114 : if (!PyUnicode_Check(name_unicode)) {
113 : 0 : PyErr_SetString(PyExc_TypeError,
114 : : "spec.name must be a string");
115 : 0 : goto error;
116 : : }
117 : 114 : newcontext = PyUnicode_AsUTF8(name_unicode);
118 [ - + ]: 114 : if (newcontext == NULL) {
119 : 0 : goto error;
120 : : }
121 : :
122 : 114 : name = get_encoded_name(name_unicode, &hook_prefix);
123 [ - + ]: 114 : if (name == NULL) {
124 : 0 : goto error;
125 : : }
126 : 114 : name_buf = PyBytes_AS_STRING(name);
127 : :
128 : 114 : path = PyObject_GetAttrString(spec, "origin");
129 [ - + ]: 114 : if (path == NULL)
130 : 0 : goto error;
131 : :
132 [ - + ]: 114 : if (PySys_Audit("import", "OOOOO", name_unicode, path,
133 : : Py_None, Py_None, Py_None) < 0) {
134 : 0 : goto error;
135 : : }
136 : :
137 : : #ifdef MS_WINDOWS
138 : : exportfunc = _PyImport_FindSharedFuncptrWindows(hook_prefix, name_buf,
139 : : path, fp);
140 : : #else
141 : 114 : pathbytes = PyUnicode_EncodeFSDefault(path);
142 [ - + ]: 114 : if (pathbytes == NULL)
143 : 0 : goto error;
144 : 114 : exportfunc = _PyImport_FindSharedFuncptr(hook_prefix, name_buf,
145 : 114 : PyBytes_AS_STRING(pathbytes),
146 : : fp);
147 : 114 : Py_DECREF(pathbytes);
148 : : #endif
149 : :
150 [ - + ]: 114 : if (exportfunc == NULL) {
151 [ # # ]: 0 : if (!PyErr_Occurred()) {
152 : : PyObject *msg;
153 : 0 : msg = PyUnicode_FromFormat(
154 : : "dynamic module does not define "
155 : : "module export function (%s_%s)",
156 : : hook_prefix, name_buf);
157 [ # # ]: 0 : if (msg == NULL)
158 : 0 : goto error;
159 : 0 : PyErr_SetImportError(msg, name_unicode, path);
160 : 0 : Py_DECREF(msg);
161 : : }
162 : 0 : goto error;
163 : : }
164 : :
165 : 114 : p0 = (PyModInitFunction)exportfunc;
166 : :
167 : : /* Package context is needed for single-phase init */
168 : 114 : oldcontext = _PyImport_SwapPackageContext(newcontext);
169 : 114 : m = _PyImport_InitFunc_TrampolineCall(p0);
170 : 114 : _PyImport_SwapPackageContext(oldcontext);
171 : :
172 [ - + ]: 114 : if (m == NULL) {
173 [ # # ]: 0 : if (!PyErr_Occurred()) {
174 : 0 : PyErr_Format(
175 : : PyExc_SystemError,
176 : : "initialization of %s failed without raising an exception",
177 : : name_buf);
178 : : }
179 : 0 : goto error;
180 [ - + ]: 114 : } else if (PyErr_Occurred()) {
181 : 0 : _PyErr_FormatFromCause(
182 : : PyExc_SystemError,
183 : : "initialization of %s raised unreported exception",
184 : : name_buf);
185 : 0 : m = NULL;
186 : 0 : goto error;
187 : : }
188 [ - + ]: 114 : if (Py_IS_TYPE(m, NULL)) {
189 : : /* This can happen when a PyModuleDef is returned without calling
190 : : * PyModuleDef_Init on it
191 : : */
192 : 0 : PyErr_Format(PyExc_SystemError,
193 : : "init function of %s returned uninitialized object",
194 : : name_buf);
195 : 0 : m = NULL; /* prevent segfault in DECREF */
196 : 0 : goto error;
197 : : }
198 [ + + ]: 114 : if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
199 : 95 : Py_DECREF(name_unicode);
200 : 95 : Py_DECREF(name);
201 : 95 : Py_DECREF(path);
202 : 95 : return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
203 : : }
204 : :
205 : : /* Fall back to single-phase init mechanism */
206 : :
207 [ - + ]: 19 : if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
208 : 0 : goto error;
209 : : }
210 : :
211 [ - + ]: 19 : if (hook_prefix == nonascii_prefix) {
212 : : /* don't allow legacy init for non-ASCII module names */
213 : 0 : PyErr_Format(
214 : : PyExc_SystemError,
215 : : "initialization of %s did not return PyModuleDef",
216 : : name_buf);
217 : 0 : goto error;
218 : : }
219 : :
220 : : /* Remember pointer to module init function. */
221 : 19 : def = PyModule_GetDef(m);
222 [ - + ]: 19 : if (def == NULL) {
223 : 0 : PyErr_Format(PyExc_SystemError,
224 : : "initialization of %s did not return an extension "
225 : : "module", name_buf);
226 : 0 : goto error;
227 : : }
228 : 19 : def->m_base.m_init = p0;
229 : :
230 : : /* Remember the filename as the __file__ attribute */
231 [ - + ]: 19 : if (PyModule_AddObjectRef(m, "__file__", path) < 0) {
232 : 0 : PyErr_Clear(); /* Not important enough to report */
233 : : }
234 : :
235 : 19 : PyObject *modules = PyImport_GetModuleDict();
236 [ - + ]: 19 : if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0)
237 : 0 : goto error;
238 : :
239 : 19 : Py_DECREF(name_unicode);
240 : 19 : Py_DECREF(name);
241 : 19 : Py_DECREF(path);
242 : :
243 : 19 : return m;
244 : :
245 : 0 : error:
246 : 0 : Py_DECREF(name_unicode);
247 : 0 : Py_XDECREF(name);
248 : 0 : Py_XDECREF(path);
249 : 0 : Py_XDECREF(m);
250 : 0 : return NULL;
251 : : }
252 : :
253 : : #endif /* HAVE_DYNAMIC_LOADING */
|