8000 gh-104621: Check for Incompatible Extensions in import_find_extension() by ericsnowcurrently · Pull Request #107184 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-104621: Check for Incompatible Extensions in import_find_extension() #107184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,20 @@ def test_isolated_config(self):
with self.subTest(f'{module}: strict, fresh'):
self.check_compatible_fresh(module, strict=True, isolated=True)

@requires_subinterpreters
@requires_singlephase_init
def test_disallowed_reimport(self):
# See https://github.com/python/cpython/issues/104621.
script = textwrap.dedent('''
import _testsinglephase
print(_testsinglephase)
''')
interpid = _interpreters.create()
with self.assertRaises(_interpreters.RunFailedError):
_interpreters.run_string(interpid, script)
with self.assertRaises(_interpreters.RunFailedError):
_interpreters.run_string(interpid, script)


class TestSinglePhaseSnapshot(ModuleSnapshot):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unsupported modules now always fail to be imported.
20 changes: 10 additions & 10 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,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);

Expand Down Expand Up @@ -3715,16 +3724,7 @@ _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)) {
goto finally;
}

Expand Down
0