diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-10-08 09:51:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-08 09:51:55 (GMT) |
commit | 6f059ab80a351a7dd85cc57c5ca240f817a79c5d (patch) | |
tree | ca107824fb2992f89ea5764763d2a40fd161c89d /Lib | |
parent | d13a4e50679c0861971cfae2bb226f35b8bf5142 (diff) | |
download | cpython-6f059ab80a351a7dd85cc57c5ca240f817a79c5d.zip cpython-6f059ab80a351a7dd85cc57c5ca240f817a79c5d.tar.gz cpython-6f059ab80a351a7dd85cc57c5ca240f817a79c5d.tar.bz2 |
[3.6] bpo-31642: Restore blocking "from" import by setting None in sys.modules. (GH-3834). (#3923)
(cherry picked from commit f07e2b64df6304a36fb5e29397d3c77a7ba17704)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 3 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_api.py | 14 |
2 files changed, 16 insertions, 1 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 4cf8aec..4fc5f02 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -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 diff --git a/Lib/test/test_importlib/import_/test_api.py b/Lib/test/test_importlib/import_/test_api.py index a7bf274..0cd9de4 100644 --- a/Lib/test/test_importlib/import_/test_api.py +++ b/Lib/test/test_importlib/import_/test_api.py @@ -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 |