diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2023-07-27 15:08:38 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-27 15:08:38 -0600 |
commit | 75c974f5353685f338344618ad7344e64c2293d0 (patch) | |
tree | 60873d069c1b951fe914538626f8b6f4cbfb137b /Python/import.c | |
parent | b72947a8d26915156323ccfd04d273199ecb870c (diff) | |
download | cpython-75c974f5353685f338344618ad7344e64c2293d0.tar.gz cpython-75c974f5353685f338344618ad7344e64c2293d0.zip |
gh-104621: Check for Incompatible Extensions in import_find_extension() (gh-107184)
This fixes a bug where incompatible modules could still be imported if attempted multiple times.
Diffstat (limited to 'Python/import.c')
-rw-r--r-- | Python/import.c | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/Python/import.c b/Python/import.c index f8cae641a64..711ed5d32ca 100644 --- a/Python/import.c +++ b/Python/import.c @@ -1215,6 +1215,15 @@ import_find_extension(PyThreadState *tstate, PyObject *name, return NULL; } + /* It may have been successfully imported previously + in an interpreter that allows legacy modules + but is not allowed in the current interpreter. */ + const char *name_buf = PyUnicode_AsUTF8(name); + assert(name_buf != NULL); + if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) { + return NULL; + } + PyObject *mod, *mdict; PyObject *modules = MODULES(tstate->interp); @@ -3704,16 +3713,8 @@ _imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file) PyThreadState *tstate = _PyThreadState_GET(); mod = import_find_extension(tstate, name, path); - if (mod != NULL) { - const char *name_buf = PyUnicode_AsUTF8(name); - assert(name_buf != NULL); - if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) { - Py_DECREF(mod); - mod = NULL; - } - goto finally; - } - else if (PyErr_Occurred()) { + if (mod != NULL || _PyErr_Occurred(tstate)) { + assert(mod == NULL || !_PyErr_Occurred(tstate)); goto finally; } |