bpo-36298: Raise ModuleNotFoundError in pyclbr when a module can't be found (GH-12358)
Before, an `AttributeError` was raised due to trying to access an attribute that exists on specs but having received `None` instead for a non-existent module.
https://bugs.python.org/issue36298
diff --git a/Lib/pyclbr.py b/Lib/pyclbr.py
index 2c798df..8fd0523 100644
--- a/Lib/pyclbr.py
+++ b/Lib/pyclbr.py
@@ -160,17 +160,20 @@
else:
search_path = path + sys.path
spec = importlib.util._find_spec_from_path(fullmodule, search_path)
+ if spec is None:
+ raise ModuleNotFoundError(f"no module named {fullmodule!r}", name=fullmodule)
_modules[fullmodule] = tree
# Is module a package?
if spec.submodule_search_locations is not None:
tree['__path__'] = spec.submodule_search_locations
try:
source = spec.loader.get_source(fullmodule)
- if source is None:
- return tree
except (AttributeError, ImportError):
# If module is not Python source, we cannot do anything.
return tree
+ else:
+ if source is None:
+ return tree
fname = spec.loader.get_filename(fullmodule)
return _create_tree(fullmodule, path, fname, source, tree, inpackage)