mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
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.
This commit is contained in:
parent
32fd874afe
commit
8c3f05e9f0
5 changed files with 24 additions and 4 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue