If sys.meta_path is empty - instead of raising ModuleNotFoundError, produce dummy directly

This commit is contained in:
BigYajuu 2022-09-14 15:23:45 +08:00
parent d59f6b92d7
commit 5a085ed4cd
1 changed files with 22 additions and 9 deletions

View File

@ -609,20 +609,33 @@ def insert_missing_modules(modules: Dict[str, ModuleType], module_name: str) ->
module_parts = module_name.split(".")
while module_name:
if module_name not in modules:
try:
# If sys.meta_path is empty, calling import_module will issue
# a warning and raise ModuleNotFoundError. To avoid the
# warning, we check sys.meta_path explicitly and raise the error
# ourselves to fall back to creating a dummy module.
if not sys.meta_path:
raise ModuleNotFoundError
importlib.import_module(module_name)
except ModuleNotFoundError:
# try:
# # If sys.meta_path is empty, calling import_module will issue
# # a warning and raise ModuleNotFoundError. To avoid the
# # warning, we check sys.meta_path explicitly and raise the error
# # ourselves to fall back to creating a dummy module.
# if not sys.meta_path:
# raise ModuleNotFoundError
# importlib.import_module(module_name)
# except ModuleNotFoundError:
# module = ModuleType(
# module_name,
# doc="Empty module created by pytest's importmode=importlib.",
# )
# modules[module_name] = module
# Instead of raising ModuleNotFoundError, replace with dummy directly.
if not sys.meta_path:
module = ModuleType(
module_name,
doc="Empty module created by pytest's importmode=importlib.",
)
modules[module_name] = module
else:
importlib.import_module(module_name)
module_parts.pop(-1)
module_name = ".".join(module_parts)