mirror of
https://github.com/python/cpython.git
synced 2025-10-27 11:44:39 +00:00
Trying to import a submodule from another module and not a package was raising
AttributeError in importlib when it should be an ImportError. Found when running importlib against test_runpy.
This commit is contained in:
parent
82a23fe392
commit
1c1dcbfd5d
4 changed files with 14 additions and 4 deletions
|
|
@ -879,7 +879,11 @@ def _gcd_import(name, package=None, level=0):
|
||||||
_gcd_import(parent)
|
_gcd_import(parent)
|
||||||
# Backwards-compatibility; be nicer to skip the dict lookup.
|
# Backwards-compatibility; be nicer to skip the dict lookup.
|
||||||
parent_module = sys.modules[parent]
|
parent_module = sys.modules[parent]
|
||||||
path = parent_module.__path__
|
try:
|
||||||
|
path = parent_module.__path__
|
||||||
|
except AttributeError:
|
||||||
|
raise ImportError("no module named {}; "
|
||||||
|
"{} is not a package".format(name, parent))
|
||||||
meta_path = sys.meta_path + _IMPLICIT_META_PATH
|
meta_path = sys.meta_path + _IMPLICIT_META_PATH
|
||||||
for finder in meta_path:
|
for finder in meta_path:
|
||||||
loader = finder.find_module(name, path)
|
loader = finder.find_module(name, path)
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,12 @@ def test_bad_parent(self):
|
||||||
with self.assertRaises(ImportError):
|
with self.assertRaises(ImportError):
|
||||||
import_util.import_('pkg.module')
|
import_util.import_('pkg.module')
|
||||||
|
|
||||||
|
def test_module_not_package(self):
|
||||||
|
# Try to import a submodule from a non-package should raise ImportError.
|
||||||
|
assert not hasattr(sys, '__path__')
|
||||||
|
with self.assertRaises(ImportError):
|
||||||
|
import_util.import_('sys.no_submodules_here')
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
from test.support import run_unittest
|
from test.support import run_unittest
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,6 @@
|
||||||
XXX FAILING
|
XXX FAILING
|
||||||
test_import # execution bit, exception name differing, file name differing
|
test_import # execution bit, exception name differing, file name differing
|
||||||
between code and module (?)
|
between code and module (?)
|
||||||
test_runpy # Importing sys.imp.eric raises AttributeError instead of
|
|
||||||
ImportError (as does any attempt to import a sub-module
|
|
||||||
from a non-package, e.g. tokenize.menotreal)
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
import importlib
|
import importlib
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,9 @@ C-API
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Trying to import a submodule from a module that is not a package, ImportError
|
||||||
|
should be raised, not AttributeError.
|
||||||
|
|
||||||
- When the globals past to importlib.__import__() has __package__ set to None,
|
- When the globals past to importlib.__import__() has __package__ set to None,
|
||||||
fall back to computing what __package__ should be instead of giving up.
|
fall back to computing what __package__ should be instead of giving up.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue