Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1 | |
| 2 | /* Support for dynamic loading of extension modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 10 | */ |
Guido van Rossum | 6ea9092 | 1999-12-20 21:20:42 +0000 | [diff] [blame] | 11 | #ifdef HAVE_DYNAMIC_LOADING |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 13 | #include "importdl.h" |
| 14 | |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 15 | #ifdef MS_WINDOWS |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 16 | extern dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix, |
| 17 | const char *shortname, |
| 18 | PyObject *pathname, |
| 19 | FILE *fp); |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 20 | #else |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 21 | extern dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix, |
| 22 | const char *shortname, |
| 23 | const char *pathname, FILE *fp); |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 24 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 25 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 26 | static const char * const ascii_only_prefix = "PyInit"; |
| 27 | static const char * const nonascii_prefix = "PyInitU"; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 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 * |
| 36 | get_encoded_name(PyObject *name, const char **hook_prefix) { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 37 | PyObject *tmp; |
| 38 | PyObject *encoded = NULL; |
Benjamin Peterson | e20056c | 2015-05-29 17:10:30 -0500 | [diff] [blame] | 39 | PyObject *modname = NULL; |
| 40 | Py_ssize_t name_len, lastdot; |
| 41 | _Py_IDENTIFIER(replace); |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 42 | |
| 43 | /* Get the short name (substring after last dot) */ |
| 44 | name_len = PyUnicode_GetLength(name); |
Miss Islington (bot) | 56825b6 | 2021-10-11 04:40:43 -0700 | [diff] [blame] | 45 | if (name_len < 0) { |
| 46 | return NULL; |
| 47 | } |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 48 | lastdot = PyUnicode_FindChar(name, '.', 0, name_len, -1); |
| 49 | if (lastdot < -1) { |
| 50 | return NULL; |
| 51 | } else if (lastdot >= 0) { |
Nick Coghlan | 55871f0 | 2015-05-26 21:48:17 +1000 | [diff] [blame] | 52 | tmp = PyUnicode_Substring(name, lastdot + 1, name_len); |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 53 | if (tmp == NULL) |
| 54 | return NULL; |
| 55 | name = tmp; |
| 56 | /* "name" now holds a new reference to the substring */ |
| 57 | } else { |
| 58 | Py_INCREF(name); |
| 59 | } |
| 60 | |
| 61 | /* Encode to ASCII or Punycode, as needed */ |
| 62 | encoded = PyUnicode_AsEncodedString(name, "ascii", NULL); |
| 63 | if (encoded != NULL) { |
| 64 | *hook_prefix = ascii_only_prefix; |
| 65 | } else { |
| 66 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) { |
| 67 | PyErr_Clear(); |
| 68 | encoded = PyUnicode_AsEncodedString(name, "punycode", NULL); |
| 69 | if (encoded == NULL) { |
| 70 | goto error; |
| 71 | } |
| 72 | *hook_prefix = nonascii_prefix; |
| 73 | } else { |
| 74 | goto error; |
| 75 | } |
| 76 | } |
| 77 | |
Benjamin Peterson | e20056c | 2015-05-29 17:10:30 -0500 | [diff] [blame] | 78 | /* Replace '-' by '_' */ |
| 79 | modname = _PyObject_CallMethodId(encoded, &PyId_replace, "cc", '-', '_'); |
| 80 | if (modname == NULL) |
| 81 | goto error; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 82 | |
| 83 | Py_DECREF(name); |
Benjamin Peterson | e20056c | 2015-05-29 17:10:30 -0500 | [diff] [blame] | 84 | Py_DECREF(encoded); |
| 85 | return modname; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 86 | error: |
| 87 | Py_DECREF(name); |
| 88 | Py_XDECREF(encoded); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 92 | PyObject * |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 93 | _PyImport_LoadDynamicModuleWithSpec(PyObject *spec, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 94 | { |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 95 | #ifndef MS_WINDOWS |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 96 | PyObject *pathbytes = NULL; |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 97 | #endif |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 98 | PyObject *name_unicode = NULL, *name = NULL, *path = NULL, *m = NULL; |
| 99 | const char *name_buf, *hook_prefix; |
Serhiy Storchaka | b57d9ea | 2016-11-21 10:25:54 +0200 | [diff] [blame] | 100 | const char *oldcontext; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 101 | dl_funcptr exportfunc; |
| 102 | PyModuleDef *def; |
| 103 | PyObject *(*p0)(void); |
Guido van Rossum | 6ea9092 | 1999-12-20 21:20:42 +0000 | [diff] [blame] | 104 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 105 | name_unicode = PyObject_GetAttrString(spec, "name"); |
| 106 | if (name_unicode == NULL) { |
| 107 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | } |
Oren Milman | 9974e1b | 2017-09-19 14:39:47 +0300 | [diff] [blame] | 109 | if (!PyUnicode_Check(name_unicode)) { |
| 110 | PyErr_SetString(PyExc_TypeError, |
| 111 | "spec.name must be a string"); |
| 112 | goto error; |
| 113 | } |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 114 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 115 | name = get_encoded_name(name_unicode, &hook_prefix); |
| 116 | if (name == NULL) { |
| 117 | goto error; |
| 118 | } |
| 119 | name_buf = PyBytes_AS_STRING(name); |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 120 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 121 | path = PyObject_GetAttrString(spec, "origin"); |
| 122 | if (path == NULL) |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 123 | goto error; |
| 124 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 125 | if (PySys_Audit("import", "OOOOO", name_unicode, path, |
| 126 | Py_None, Py_None, Py_None) < 0) { |
Miss Islington (bot) | 3a58d60 | 2021-10-11 02:22:29 -0700 | [diff] [blame] | 127 | goto error; |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 130 | #ifdef MS_WINDOWS |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 131 | exportfunc = _PyImport_FindSharedFuncptrWindows(hook_prefix, name_buf, |
| 132 | path, fp); |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 133 | #else |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 134 | pathbytes = PyUnicode_EncodeFSDefault(path); |
| 135 | if (pathbytes == NULL) |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 136 | goto error; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 137 | exportfunc = _PyImport_FindSharedFuncptr(hook_prefix, name_buf, |
| 138 | PyBytes_AS_STRING(pathbytes), |
| 139 | fp); |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 140 | Py_DECREF(pathbytes); |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 141 | #endif |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 142 | |
| 143 | if (exportfunc == NULL) { |
| 144 | if (!PyErr_Occurred()) { |
| 145 | PyObject *msg; |
| 146 | msg = PyUnicode_FromFormat( |
| 147 | "dynamic module does not define " |
| 148 | "module export function (%s_%s)", |
| 149 | hook_prefix, name_buf); |
| 150 | if (msg == NULL) |
| 151 | goto error; |
| 152 | PyErr_SetImportError(msg, name_unicode, path); |
| 153 | Py_DECREF(msg); |
| 154 | } |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 155 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | } |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 157 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 158 | p0 = (PyObject *(*)(void))exportfunc; |
| 159 | |
| 160 | /* Package context is needed for single-phase init */ |
| 161 | oldcontext = _Py_PackageContext; |
| 162 | _Py_PackageContext = PyUnicode_AsUTF8(name_unicode); |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 163 | if (_Py_PackageContext == NULL) { |
| 164 | _Py_PackageContext = oldcontext; |
| 165 | goto error; |
| 166 | } |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 167 | m = p0(); |
| 168 | _Py_PackageContext = oldcontext; |
| 169 | |
| 170 | if (m == NULL) { |
| 171 | if (!PyErr_Occurred()) { |
| 172 | PyErr_Format( |
| 173 | PyExc_SystemError, |
| 174 | "initialization of %s failed without raising an exception", |
| 175 | name_buf); |
| 176 | } |
| 177 | goto error; |
| 178 | } else if (PyErr_Occurred()) { |
| 179 | PyErr_Clear(); |
| 180 | PyErr_Format( |
| 181 | PyExc_SystemError, |
| 182 | "initialization of %s raised unreported exception", |
| 183 | name_buf); |
| 184 | m = NULL; |
| 185 | goto error; |
| 186 | } |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 187 | if (Py_IS_TYPE(m, NULL)) { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 188 | /* This can happen when a PyModuleDef is returned without calling |
| 189 | * PyModuleDef_Init on it |
| 190 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | PyErr_Format(PyExc_SystemError, |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 192 | "init function of %s returned uninitialized object", |
| 193 | name_buf); |
| 194 | m = NULL; /* prevent segfault in DECREF */ |
| 195 | goto error; |
| 196 | } |
| 197 | if (PyObject_TypeCheck(m, &PyModuleDef_Type)) { |
| 198 | Py_DECREF(name_unicode); |
| 199 | Py_DECREF(name); |
| 200 | Py_DECREF(path); |
| 201 | return PyModule_FromDefAndSpec((PyModuleDef*)m, spec); |
| 202 | } |
| 203 | |
| 204 | /* Fall back to single-phase init mechanism */ |
| 205 | |
| 206 | if (hook_prefix == nonascii_prefix) { |
| 207 | /* don't allow legacy init for non-ASCII module names */ |
| 208 | PyErr_Format( |
| 209 | PyExc_SystemError, |
Miss Islington (bot) | 854db7e | 2021-10-12 10:10:59 -0700 | [diff] [blame] | 210 | "initialization of %s did not return PyModuleDef", |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 211 | name_buf); |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 212 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | /* Remember pointer to module init function. */ |
| 216 | def = PyModule_GetDef(m); |
Christian Heimes | 7ce57d6 | 2013-07-11 13:02:30 +0200 | [diff] [blame] | 217 | if (def == NULL) { |
| 218 | PyErr_Format(PyExc_SystemError, |
| 219 | "initialization of %s did not return an extension " |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 220 | "module", name_buf); |
Christian Heimes | 848ee09 | 2013-07-11 11:22:21 +0200 | [diff] [blame] | 221 | goto error; |
Christian Heimes | 7ce57d6 | 2013-07-11 13:02:30 +0200 | [diff] [blame] | 222 | } |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 223 | def->m_base.m_init = p0; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | /* Remember the filename as the __file__ attribute */ |
Victor Stinner | 58ca33b | 2020-11-04 17:33:06 +0100 | [diff] [blame] | 226 | if (PyModule_AddObjectRef(m, "__file__", path) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | PyErr_Clear(); /* Not important enough to report */ |
Victor Stinner | 58ca33b | 2020-11-04 17:33:06 +0100 | [diff] [blame] | 228 | } |
Martin v. Löwis | e81e9b1 | 2003-09-04 18:45:59 +0000 | [diff] [blame] | 229 | |
Eric Snow | d393c1b | 2017-09-14 12:18:12 -0600 | [diff] [blame] | 230 | PyObject *modules = PyImport_GetModuleDict(); |
| 231 | if (_PyImport_FixupExtensionObject(m, name_unicode, path, modules) < 0) |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 232 | goto error; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 233 | |
| 234 | Py_DECREF(name_unicode); |
| 235 | Py_DECREF(name); |
| 236 | Py_DECREF(path); |
| 237 | |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 238 | return m; |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 239 | |
| 240 | error: |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 241 | Py_DECREF(name_unicode); |
| 242 | Py_XDECREF(name); |
| 243 | Py_XDECREF(path); |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 244 | Py_XDECREF(m); |
| 245 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 246 | } |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 247 | |
| 248 | #endif /* HAVE_DYNAMIC_LOADING */ |