cpython: d2504d30f259 (original) (raw)
Mercurial > cpython
changeset 73973:d2504d30f259 3.2
Issue #13591: import_module potentially imports a module twice. [#13591]
Meador Inge meadori@gmail.com | |
---|---|
date | Wed, 14 Dec 2011 22:23:46 -0600 |
parents | f1fe411bfd6b |
children | e8fb61a0a2d7 541f215a31f7 |
files | Lib/importlib/_bootstrap.py Lib/importlib/test/test_api.py Lib/importlib/test/util.py Misc/NEWS |
diffstat | 4 files changed, 29 insertions(+), 2 deletions(-)[+] [-] Lib/importlib/_bootstrap.py 4 Lib/importlib/test/test_api.py 17 Lib/importlib/test/util.py 7 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -816,7 +816,9 @@ def _gcd_import(name, package=None, leve for finder in meta_path: loader = finder.find_module(name, path) if loader is not None:
loader.load_module(name)[](#l1.7)
# The parent import may have already imported this module.[](#l1.8)
if name not in sys.modules:[](#l1.9)
loader.load_module(name)[](#l1.10) break[](#l1.11) else:[](#l1.12) raise ImportError(_ERR_MSG.format(name))[](#l1.13)
--- a/Lib/importlib/test/test_api.py +++ b/Lib/importlib/test/test_api.py @@ -67,6 +67,23 @@ class ImportModuleTests(unittest.TestCas importlib.import_module('.support')
- def test_loaded_once(self):
# Issue #13591: Modules should only be loaded once when[](#l2.8)
# initializing the parent package attempts to import the[](#l2.9)
# module currently being imported.[](#l2.10)
b_load_count = 0[](#l2.11)
def load_a():[](#l2.12)
importlib.import_module('a.b')[](#l2.13)
def load_b():[](#l2.14)
nonlocal b_load_count[](#l2.15)
b_load_count += 1[](#l2.16)
code = {'a': load_a, 'a.b': load_b}[](#l2.17)
modules = ['a.__init__', 'a.b'][](#l2.18)
with util.mock_modules(*modules, module_code=code) as mock:[](#l2.19)
with util.import_state(meta_path=[mock]):[](#l2.20)
importlib.import_module('a.b')[](#l2.21)
self.assertEqual(b_load_count, 1)[](#l2.22)
+ def test_main(): from test.support import run_unittest run_unittest(ImportModuleTests)
--- a/Lib/importlib/test/util.py +++ b/Lib/importlib/test/util.py @@ -84,8 +84,9 @@ class mock_modules: """A mock importer/loader."""
- def init(self, *names, module_code={}): self.modules = {}
self.module_code = {}[](#l3.10) for name in names:[](#l3.11) if not name.endswith('.__init__'):[](#l3.12) import_name = name[](#l3.13)
@@ -105,6 +106,8 @@ class mock_modules: if import_name != name: module.path = [''] self.modules[import_name] = module
if import_name in module_code:[](#l3.18)
self.module_code[import_name] = module_code[import_name][](#l3.19)
def getitem(self, name): return self.modules[name] @@ -120,6 +123,8 @@ class mock_modules: raise ImportError else: sys.modules[fullname] = self.modules[fullname]
if fullname in self.module_code:[](#l3.27)
self.module_code[fullname]()[](#l3.28) return self.modules[fullname][](#l3.29)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -1868,6 +1868,9 @@ Core and Builtins Library ------- +- Issue #13591: A bug in importlib has been fixed that caused import_module
- logging: added "handler of last resort". See http://bit.ly/last-resort-handler[](#l4.10)
- test.support: Added TestHandler and Matcher classes for better support of