diff options
author | Milan Oberkirch <zvyn@oberkirch.org> | 2017-06-14 21:34:50 (GMT) |
---|---|---|
committer | Brett Cannon <brettcannon@users.noreply.github.com> | 2017-06-14 21:34:50 (GMT) |
commit | 8c3f05e9f0f0b30a3d4a2433e92471794d8258af (patch) | |
tree | 57f6ae60b1701bf4ca4be1f9db1afce1e2bea795 /Lib/importlib | |
parent | 32fd874afe55e396e3c9a5af35e7bb3d8e0b8f02 (diff) | |
download | cpython-8c3f05e9f0f0b30a3d4a2433e92471794d8258af.zip cpython-8c3f05e9f0f0b30a3d4a2433e92471794d8258af.tar.gz cpython-8c3f05e9f0f0b30a3d4a2433e92471794d8258af.tar.bz2 |
bpo-30436: Raise ModuleNotFoundError for importlib.util.find_spec() when parent isn't a package (GH-1899)
Previously AttributeError was raised, but that's not very reflective of the fact that the requested module can't be found since the specified parent isn't actually a package.
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/util.py | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/Lib/importlib/util.py b/Lib/importlib/util.py index 6bdf0d4..41c74d4 100644 --- a/Lib/importlib/util.py +++ b/Lib/importlib/util.py @@ -84,11 +84,16 @@ def find_spec(name, package=None): if fullname not in sys.modules: parent_name = fullname.rpartition('.')[0] if parent_name: - # Use builtins.__import__() in case someone replaced it. parent = __import__(parent_name, fromlist=['__path__']) - return _find_spec(fullname, parent.__path__) + try: + parent_path = parent.__path__ + except AttributeError as e: + raise ModuleNotFoundError( + f"__path__ attribute not found on {parent_name!r}" + f"while trying to find {fullname!r}", name=fullname) from e else: - return _find_spec(fullname, None) + parent_path = None + return _find_spec(fullname, parent_path) else: module = sys.modules[fullname] if module is None: |