Kaydet (Commit) f07e2b64 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka Kaydeden (comit) GitHub

bpo-31642: Restore blocking "from" import by setting None in sys.modules. (#3834)

üst 73ffd3f2
......@@ -1019,7 +1019,8 @@ def _handle_fromlist(module, fromlist, import_):
# Backwards-compatibility dictates we ignore failed
# imports triggered by fromlist for modules that don't
# exist.
if exc.name == from_name:
if (exc.name == from_name and
sys.modules.get(from_name, _NEEDS_LOADING) is not None):
continue
raise
return module
......
......@@ -82,6 +82,20 @@ class APITest:
self.__import__(PKG_NAME,
fromlist=[SUBMOD_NAME.rpartition('.')[-1]])
def test_blocked_fromlist(self):
# If fromlist entry is None, let a ModuleNotFoundError propagate.
# issue31642
mod = types.ModuleType(PKG_NAME)
mod.__path__ = []
with util.import_state(meta_path=[self.bad_finder_loader]):
with util.uncache(PKG_NAME, SUBMOD_NAME):
sys.modules[PKG_NAME] = mod
sys.modules[SUBMOD_NAME] = None
with self.assertRaises(ModuleNotFoundError) as cm:
self.__import__(PKG_NAME,
fromlist=[SUBMOD_NAME.rpartition('.')[-1]])
self.assertEqual(cm.exception.name, SUBMOD_NAME)
class OldAPITests(APITest):
bad_finder_loader = BadLoaderFinder
......
Restored blocking "from package import module" by setting
sys.modules["package.module"] to None.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment