2009-02-01 00:00:05 -04:00
|
|
|
from .. import util
|
2009-02-01 00:28:04 -04:00
|
|
|
from . import util as import_util
|
2009-01-17 20:24:28 -04:00
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
import importlib
|
2012-04-06 14:13:08 -03:00
|
|
|
from test import support
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ParentModuleTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""Importing a submodule should import the parent modules."""
|
|
|
|
|
|
|
|
def test_import_parent(self):
|
2009-02-01 00:00:05 -04:00
|
|
|
with util.mock_modules('pkg.__init__', 'pkg.module') as mock:
|
|
|
|
with util.import_state(meta_path=[mock]):
|
2009-02-01 00:28:04 -04:00
|
|
|
module = import_util.import_('pkg.module')
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue('pkg' in sys.modules)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
def test_bad_parent(self):
|
2009-02-01 00:00:05 -04:00
|
|
|
with util.mock_modules('pkg.module') as mock:
|
|
|
|
with util.import_state(meta_path=[mock]):
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2009-08-27 20:49:21 -03:00
|
|
|
import_util.import_('pkg.module')
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertEqual(cm.exception.name, 'pkg')
|
2009-01-17 20:24:28 -04:00
|
|
|
|
2012-05-07 16:41:59 -03:00
|
|
|
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)
|
|
|
|
|
2009-08-30 17:22:21 -03:00
|
|
|
def test_module_not_package(self):
|
|
|
|
# Try to import a submodule from a non-package should raise ImportError.
|
|
|
|
assert not hasattr(sys, '__path__')
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2009-08-30 17:22:21 -03:00
|
|
|
import_util.import_('sys.no_submodules_here')
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertEqual(cm.exception.name, 'sys.no_submodules_here')
|
2009-08-30 17:22:21 -03:00
|
|
|
|
2012-04-02 21:33:56 -03:00
|
|
|
def test_module_not_package_but_side_effects(self):
|
|
|
|
# If a module injects something into sys.modules as a side-effect, then
|
|
|
|
# pick up on that fact.
|
|
|
|
name = 'mod'
|
|
|
|
subname = name + '.b'
|
|
|
|
def module_injection():
|
|
|
|
sys.modules[subname] = 'total bunk'
|
|
|
|
mock_modules = util.mock_modules('mod',
|
|
|
|
module_code={'mod': module_injection})
|
|
|
|
with mock_modules as mock:
|
|
|
|
with util.import_state(meta_path=[mock]):
|
2012-04-06 14:13:08 -03:00
|
|
|
try:
|
|
|
|
submodule = import_util.import_(subname)
|
|
|
|
finally:
|
|
|
|
support.unload(subname)
|
2012-04-02 21:33:56 -03:00
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
from test.support import run_unittest
|
|
|
|
run_unittest(ParentModuleTests)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|