If sys.meta_path is empty - instead of raising ModuleNotFoundError, produce dummy directly
This commit is contained in:
parent
d59f6b92d7
commit
5a085ed4cd
|
@ -609,20 +609,33 @@ def insert_missing_modules(modules: Dict[str, ModuleType], module_name: str) ->
|
||||||
module_parts = module_name.split(".")
|
module_parts = module_name.split(".")
|
||||||
while module_name:
|
while module_name:
|
||||||
if module_name not in modules:
|
if module_name not in modules:
|
||||||
try:
|
# try:
|
||||||
# If sys.meta_path is empty, calling import_module will issue
|
# # If sys.meta_path is empty, calling import_module will issue
|
||||||
# a warning and raise ModuleNotFoundError. To avoid the
|
# # a warning and raise ModuleNotFoundError. To avoid the
|
||||||
# warning, we check sys.meta_path explicitly and raise the error
|
# # warning, we check sys.meta_path explicitly and raise the error
|
||||||
# ourselves to fall back to creating a dummy module.
|
# # ourselves to fall back to creating a dummy module.
|
||||||
if not sys.meta_path:
|
# if not sys.meta_path:
|
||||||
raise ModuleNotFoundError
|
# raise ModuleNotFoundError
|
||||||
importlib.import_module(module_name)
|
# importlib.import_module(module_name)
|
||||||
except ModuleNotFoundError:
|
# 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 = ModuleType(
|
||||||
module_name,
|
module_name,
|
||||||
doc="Empty module created by pytest's importmode=importlib.",
|
doc="Empty module created by pytest's importmode=importlib.",
|
||||||
)
|
)
|
||||||
modules[module_name] = module
|
modules[module_name] = module
|
||||||
|
else:
|
||||||
|
importlib.import_module(module_name)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module_parts.pop(-1)
|
module_parts.pop(-1)
|
||||||
module_name = ".".join(module_parts)
|
module_name = ".".join(module_parts)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue