blob: bfa18dc217268b656a123b60e6444012be873a99 [file] [log] [blame]
Brett Cannonbcb26c52009-02-01 04:00:05 +00001from .. import util
Brett Cannond720b362009-02-01 04:28:04 +00002from . import util as import_util
Brett Cannon23cbd8a2009-01-18 00:24:28 +00003import sys
4import unittest
5import importlib
Brett Cannond62cd562012-04-06 13:13:08 -04006from test import support
Brett Cannon23cbd8a2009-01-18 00:24:28 +00007
8
9class ParentModuleTests(unittest.TestCase):
10
11 """Importing a submodule should import the parent modules."""
12
13 def test_import_parent(self):
Brett Cannonbcb26c52009-02-01 04:00:05 +000014 with util.mock_modules('pkg.__init__', 'pkg.module') as mock:
15 with util.import_state(meta_path=[mock]):
Brett Cannond720b362009-02-01 04:28:04 +000016 module = import_util.import_('pkg.module')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040017 self.assertIn('pkg', sys.modules)
Brett Cannon23cbd8a2009-01-18 00:24:28 +000018
19 def test_bad_parent(self):
Brett Cannonbcb26c52009-02-01 04:00:05 +000020 with util.mock_modules('pkg.module') as mock:
21 with util.import_state(meta_path=[mock]):
Brett Cannonbbb66802012-04-12 21:09:01 -040022 with self.assertRaises(ImportError) as cm:
Brett Cannon2153dc02009-08-27 23:49:21 +000023 import_util.import_('pkg.module')
Brett Cannonbbb66802012-04-12 21:09:01 -040024 self.assertEqual(cm.exception.name, 'pkg')
Brett Cannon23cbd8a2009-01-18 00:24:28 +000025
Antoine Pitrou6efa50a2012-05-07 21:41:59 +020026 def test_raising_parent_after_importing_child(self):
27 def __init__():
28 import pkg.module
29 1/0
30 mock = util.mock_modules('pkg.__init__', 'pkg.module',
31 module_code={'pkg': __init__})
32 with mock:
33 with util.import_state(meta_path=[mock]):
34 with self.assertRaises(ZeroDivisionError):
35 import_util.import_('pkg')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040036 self.assertNotIn('pkg', sys.modules)
37 self.assertIn('pkg.module', sys.modules)
Antoine Pitrou6efa50a2012-05-07 21:41:59 +020038 with self.assertRaises(ZeroDivisionError):
39 import_util.import_('pkg.module')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040040 self.assertNotIn('pkg', sys.modules)
41 self.assertIn('pkg.module', sys.modules)
Antoine Pitrou6efa50a2012-05-07 21:41:59 +020042
43 def test_raising_parent_after_relative_importing_child(self):
44 def __init__():
45 from . import module
46 1/0
47 mock = util.mock_modules('pkg.__init__', 'pkg.module',
48 module_code={'pkg': __init__})
49 with mock:
50 with util.import_state(meta_path=[mock]):
51 with self.assertRaises((ZeroDivisionError, ImportError)):
52 # This raises ImportError on the "from . import module"
53 # line, not sure why.
54 import_util.import_('pkg')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040055 self.assertNotIn('pkg', sys.modules)
Antoine Pitrou6efa50a2012-05-07 21:41:59 +020056 with self.assertRaises((ZeroDivisionError, ImportError)):
57 import_util.import_('pkg.module')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040058 self.assertNotIn('pkg', sys.modules)
Antoine Pitrou6efa50a2012-05-07 21:41:59 +020059 # XXX False
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040060 #self.assertIn('pkg.module', sys.modules)
Antoine Pitrou6efa50a2012-05-07 21:41:59 +020061
62 def test_raising_parent_after_double_relative_importing_child(self):
63 def __init__():
64 from ..subpkg import module
65 1/0
66 mock = util.mock_modules('pkg.__init__', 'pkg.subpkg.__init__',
67 'pkg.subpkg.module',
68 module_code={'pkg.subpkg': __init__})
69 with mock:
70 with util.import_state(meta_path=[mock]):
71 with self.assertRaises((ZeroDivisionError, ImportError)):
72 # This raises ImportError on the "from ..subpkg import module"
73 # line, not sure why.
74 import_util.import_('pkg.subpkg')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040075 self.assertNotIn('pkg.subpkg', sys.modules)
Antoine Pitrou6efa50a2012-05-07 21:41:59 +020076 with self.assertRaises((ZeroDivisionError, ImportError)):
77 import_util.import_('pkg.subpkg.module')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040078 self.assertNotIn('pkg.subpkg', sys.modules)
Antoine Pitrou6efa50a2012-05-07 21:41:59 +020079 # XXX False
Eric V. Smithfaae3ad2012-06-27 15:26:26 -040080 #self.assertIn('pkg.subpkg.module', sys.modules)
Antoine Pitrou6efa50a2012-05-07 21:41:59 +020081
Brett Cannon1c1dcbf2009-08-30 20:22:21 +000082 def test_module_not_package(self):
83 # Try to import a submodule from a non-package should raise ImportError.
84 assert not hasattr(sys, '__path__')
Brett Cannonbbb66802012-04-12 21:09:01 -040085 with self.assertRaises(ImportError) as cm:
Brett Cannon1c1dcbf2009-08-30 20:22:21 +000086 import_util.import_('sys.no_submodules_here')
Brett Cannonbbb66802012-04-12 21:09:01 -040087 self.assertEqual(cm.exception.name, 'sys.no_submodules_here')
Brett Cannon1c1dcbf2009-08-30 20:22:21 +000088
Brett Cannon927d8742012-04-02 20:33:56 -040089 def test_module_not_package_but_side_effects(self):
90 # If a module injects something into sys.modules as a side-effect, then
91 # pick up on that fact.
92 name = 'mod'
93 subname = name + '.b'
94 def module_injection():
95 sys.modules[subname] = 'total bunk'
96 mock_modules = util.mock_modules('mod',
97 module_code={'mod': module_injection})
98 with mock_modules as mock:
99 with util.import_state(meta_path=[mock]):
Brett Cannond62cd562012-04-06 13:13:08 -0400100 try:
101 submodule = import_util.import_(subname)
102 finally:
103 support.unload(subname)
Brett Cannon927d8742012-04-02 20:33:56 -0400104
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000105
106def test_main():
107 from test.support import run_unittest
108 run_unittest(ParentModuleTests)
109
110
111if __name__ == '__main__':
112 test_main()