• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* Support for dynamic loading of extension modules */
3 
4 #include "Python.h"
5 
6 /* ./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.
10 */
11 #ifdef HAVE_DYNAMIC_LOADING
12 
13 #include "importdl.h"
14 
15 #ifdef MS_WINDOWS
16 extern dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
17                                                      const char *shortname,
18                                                      PyObject *pathname,
19                                                      FILE *fp);
20 #else
21 extern dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
22                                               const char *shortname,
23                                               const char *pathname, FILE *fp);
24 #endif
25 
26 static const char * const ascii_only_prefix = "PyInit";
27 static const char * const nonascii_prefix = "PyInitU";
28 
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  */
35 static PyObject *
get_encoded_name(PyObject * name,const char ** hook_prefix)36 get_encoded_name(PyObject *name, const char **hook_prefix) {
37     PyObject *tmp;
38     PyObject *encoded = NULL;
39     PyObject *modname = NULL;
40     Py_ssize_t name_len, lastdot;
41     _Py_IDENTIFIER(replace);
42 
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) {
49         tmp = PyUnicode_Substring(name, lastdot + 1, name_len);
50         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 
75     /* Replace '-' by '_' */
76     modname = _PyObject_CallMethodId(encoded, &PyId_replace, "cc", '-', '_');
77     if (modname == NULL)
78         goto error;
79 
80     Py_DECREF(name);
81     Py_DECREF(encoded);
82     return modname;
83 error:
84     Py_DECREF(name);
85     Py_XDECREF(encoded);
86     return NULL;
87 }
88 
89 PyObject *
_PyImport_LoadDynamicModuleWithSpec(PyObject * spec,FILE * fp)90 _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp)
91 {
92 #ifndef MS_WINDOWS
93     PyObject *pathbytes = NULL;
94 #endif
95     PyObject *name_unicode = NULL, *name = NULL, *path = NULL, *m = NULL;
96     const char *name_buf, *hook_prefix;
97     const char *oldcontext;
98     dl_funcptr exportfunc;
99     PyModuleDef *def;
100     PyObject *(*p0)(void);
101 
102     name_unicode = PyObject_GetAttrString(spec, "name");
103     if (name_unicode == NULL) {
104         return NULL;
105     }
106     if (!PyUnicode_Check(name_unicode)) {
107         PyErr_SetString(PyExc_TypeError,
108                         "spec.name must be a string");
109         goto error;
110     }
111 
112     name = get_encoded_name(name_unicode, &hook_prefix);
113     if (name == NULL) {
114         goto error;
115     }
116     name_buf = PyBytes_AS_STRING(name);
117 
118     path = PyObject_GetAttrString(spec, "origin");
119     if (path == NULL)
120         goto error;
121 
122     if (PySys_Audit("import", "OOOOO", name_unicode, path,
123                     Py_None, Py_None, Py_None) < 0) {
124         return NULL;
125     }
126 
127 #ifdef MS_WINDOWS
128     exportfunc = _PyImport_FindSharedFuncptrWindows(hook_prefix, name_buf,
129                                                     path, fp);
130 #else
131     pathbytes = PyUnicode_EncodeFSDefault(path);
132     if (pathbytes == NULL)
133         goto error;
134     exportfunc = _PyImport_FindSharedFuncptr(hook_prefix, name_buf,
135                                              PyBytes_AS_STRING(pathbytes),
136                                              fp);
137     Py_DECREF(pathbytes);
138 #endif
139 
140     if (exportfunc == NULL) {
141         if (!PyErr_Occurred()) {
142             PyObject *msg;
143             msg = PyUnicode_FromFormat(
144                 "dynamic module does not define "
145                 "module export function (%s_%s)",
146                 hook_prefix, name_buf);
147             if (msg == NULL)
148                 goto error;
149             PyErr_SetImportError(msg, name_unicode, path);
150             Py_DECREF(msg);
151         }
152         goto error;
153     }
154 
155     p0 = (PyObject *(*)(void))exportfunc;
156 
157     /* Package context is needed for single-phase init */
158     oldcontext = _Py_PackageContext;
159     _Py_PackageContext = PyUnicode_AsUTF8(name_unicode);
160     if (_Py_PackageContext == NULL) {
161         _Py_PackageContext = oldcontext;
162         goto error;
163     }
164     m = p0();
165     _Py_PackageContext = oldcontext;
166 
167     if (m == NULL) {
168         if (!PyErr_Occurred()) {
169             PyErr_Format(
170                 PyExc_SystemError,
171                 "initialization of %s failed without raising an exception",
172                 name_buf);
173         }
174         goto error;
175     } else if (PyErr_Occurred()) {
176         PyErr_Clear();
177         PyErr_Format(
178             PyExc_SystemError,
179             "initialization of %s raised unreported exception",
180             name_buf);
181         m = NULL;
182         goto error;
183     }
184     if (Py_IS_TYPE(m, NULL)) {
185         /* This can happen when a PyModuleDef is returned without calling
186          * PyModuleDef_Init on it
187          */
188         PyErr_Format(PyExc_SystemError,
189                      "init function of %s returned uninitialized object",
190                      name_buf);
191         m = NULL; /* prevent segfault in DECREF */
192         goto error;
193     }
194     if (PyObject_TypeCheck(m, &PyModuleDef_Type)) {
195         Py_DECREF(name_unicode);
196         Py_DECREF(name);
197         Py_DECREF(path);
198         return PyModule_FromDefAndSpec((PyModuleDef*)m, spec);
199     }
200 
201     /* Fall back to single-phase init mechanism */
202 
203     if (hook_prefix == nonascii_prefix) {
204         /* don't allow legacy init for non-ASCII module names */
205         PyErr_Format(
206             PyExc_SystemError,
207             "initialization of * did not return PyModuleDef",
208             name_buf);
209         goto error;
210     }
211 
212     /* Remember pointer to module init function. */
213     def = PyModule_GetDef(m);
214     if (def == NULL) {
215         PyErr_Format(PyExc_SystemError,
216                      "initialization of %s did not return an extension "
217                      "module", name_buf);
218         goto error;
219     }
220     def->m_base.m_init = p0;
221 
222     /* Remember the filename as the __file__ attribute */
223     if (PyModule_AddObject(m, "__file__", path) < 0)
224         PyErr_Clear(); /* Not important enough to report */
225     else
226         Py_INCREF(path);
227 
228     PyObject *modules = PyImport_GetModuleDict();
229     if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0)
230         goto error;
231 
232     Py_DECREF(name_unicode);
233     Py_DECREF(name);
234     Py_DECREF(path);
235 
236     return m;
237 
238 error:
239     Py_DECREF(name_unicode);
240     Py_XDECREF(name);
241     Py_XDECREF(path);
242     Py_XDECREF(m);
243     return NULL;
244 }
245 
246 #endif /* HAVE_DYNAMIC_LOADING */
247