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