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
This commit is contained in:
Brett Cannon 2019-03-22 15:16:50 -07:00 committed by Miss Islington (bot)
parent dd7c4ceed9
commit 5086589305
3 changed files with 29 additions and 4 deletions

View file

@ -160,17 +160,20 @@ def _readmodule(module, path, inpackage=None):
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)