diff options
author | Brett Cannon <brett@python.org> | 2012-04-15 19:24:04 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-04-15 19:24:04 (GMT) |
commit | 881535b726c595582920acc69f26b053e32b85b0 (patch) | |
tree | ca3bcfcff049f02a4dc5e1187b0a9e5d1beb489d /Lib/importlib/test | |
parent | 27fc52877ce3cabb2ed524ab0f40f8c8e3a45c18 (diff) | |
download | cpython-881535b726c595582920acc69f26b053e32b85b0.zip cpython-881535b726c595582920acc69f26b053e32b85b0.tar.gz cpython-881535b726c595582920acc69f26b053e32b85b0.tar.bz2 |
Issue #14582: Import returns the module returned by a loader instead
of sys.modules when possible.
This is being done for two reasons. One is to gain a little bit of
performance by skipping an unnecessary dict lookup in sys.modules. But
the other (and main) reason is to be a little bit more clear in how
things should work from the perspective of import's interactions with
loaders. Otherwise loaders can easily forget to return the module even
though PEP 302 explicitly states they are expected to return the module
they loaded.
Diffstat (limited to 'Lib/importlib/test')
-rw-r--r-- | Lib/importlib/test/import_/test_caching.py | 32 |
1 files changed, 4 insertions, 28 deletions
diff --git a/Lib/importlib/test/import_/test_caching.py b/Lib/importlib/test/import_/test_caching.py index 3baff55..467eee1 100644 --- a/Lib/importlib/test/import_/test_caching.py +++ b/Lib/importlib/test/import_/test_caching.py @@ -47,36 +47,12 @@ class UseCache(unittest.TestCase): mock.load_module = MethodType(load_module, mock) return mock - # __import__ inconsistent between loaders and built-in import when it comes - # to when to use the module in sys.modules and when not to. - @import_util.importlib_only - def test_using_cache_after_loader(self): - # [from cache on return] - with self.create_mock('module') as mock: + def test_using_loader_return(self): + loader_return = 'hi there!' + with self.create_mock('module', return_=loader_return) as mock: with util.import_state(meta_path=[mock]): module = import_util.import_('module') - self.assertEqual(id(module), id(sys.modules['module'])) - - # See test_using_cache_after_loader() for reasoning. - @import_util.importlib_only - def test_using_cache_for_assigning_to_attribute(self): - # [from cache to attribute] - with self.create_mock('pkg.__init__', 'pkg.module') as importer: - with util.import_state(meta_path=[importer]): - module = import_util.import_('pkg.module') - self.assertTrue(hasattr(module, 'module')) - self.assertTrue(id(module.module), id(sys.modules['pkg.module'])) - - # See test_using_cache_after_loader() for reasoning. - @import_util.importlib_only - def test_using_cache_for_fromlist(self): - # [from cache for fromlist] - with self.create_mock('pkg.__init__', 'pkg.module') as importer: - with util.import_state(meta_path=[importer]): - module = import_util.import_('pkg', fromlist=['module']) - self.assertTrue(hasattr(module, 'module')) - self.assertEqual(id(module.module), - id(sys.modules['pkg.module'])) + self.assertEqual(module, loader_return) def test_main(): |