mirror of
https://github.com/python/cpython.git
synced 2025-10-28 20:25:04 +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
|
|
@ -84,8 +84,9 @@ class mock_modules:
|
|||
|
||||
"""A mock importer/loader."""
|
||||
|
||||
def __init__(self, *names):
|
||||
def __init__(self, *names, module_code={}):
|
||||
self.modules = {}
|
||||
self.module_code = {}
|
||||
for name in names:
|
||||
if not name.endswith('.__init__'):
|
||||
import_name = name
|
||||
|
|
@ -105,6 +106,8 @@ def __init__(self, *names):
|
|||
if import_name != name:
|
||||
module.__path__ = ['<mock __path__>']
|
||||
self.modules[import_name] = module
|
||||
if import_name in module_code:
|
||||
self.module_code[import_name] = module_code[import_name]
|
||||
|
||||
def __getitem__(self, name):
|
||||
return self.modules[name]
|
||||
|
|
@ -120,6 +123,8 @@ def load_module(self, fullname):
|
|||
raise ImportError
|
||||
else:
|
||||
sys.modules[fullname] = self.modules[fullname]
|
||||
if fullname in self.module_code:
|
||||
self.module_code[fullname]()
|
||||
return self.modules[fullname]
|
||||
|
||||
def __enter__(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue