mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #13591: import_module potentially imports a module twice.
This commit is contained in:
parent
061c0289af
commit
416f12ddb3
4 changed files with 29 additions and 2 deletions
|
@ -67,6 +67,23 @@ class ImportModuleTests(unittest.TestCase):
|
|||
importlib.import_module('.support')
|
||||
|
||||
|
||||
def test_loaded_once(self):
|
||||
# Issue #13591: Modules should only be loaded once when
|
||||
# initializing the parent package attempts to import the
|
||||
# module currently being imported.
|
||||
b_load_count = 0
|
||||
def load_a():
|
||||
importlib.import_module('a.b')
|
||||
def load_b():
|
||||
nonlocal b_load_count
|
||||
b_load_count += 1
|
||||
code = {'a': load_a, 'a.b': load_b}
|
||||
modules = ['a.__init__', 'a.b']
|
||||
with util.mock_modules(*modules, module_code=code) as mock:
|
||||
with util.import_state(meta_path=[mock]):
|
||||
importlib.import_module('a.b')
|
||||
self.assertEqual(b_load_count, 1)
|
||||
|
||||
def test_main():
|
||||
from test.support import run_unittest
|
||||
run_unittest(ImportModuleTests)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue