1 | n/a | from .. import util |
---|
2 | n/a | import sys |
---|
3 | n/a | import unittest |
---|
4 | n/a | from test import support |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class ParentModuleTests: |
---|
8 | n/a | |
---|
9 | n/a | """Importing a submodule should import the parent modules.""" |
---|
10 | n/a | |
---|
11 | n/a | def test_import_parent(self): |
---|
12 | n/a | with util.mock_spec('pkg.__init__', 'pkg.module') as mock: |
---|
13 | n/a | with util.import_state(meta_path=[mock]): |
---|
14 | n/a | module = self.__import__('pkg.module') |
---|
15 | n/a | self.assertIn('pkg', sys.modules) |
---|
16 | n/a | |
---|
17 | n/a | def test_bad_parent(self): |
---|
18 | n/a | with util.mock_spec('pkg.module') as mock: |
---|
19 | n/a | with util.import_state(meta_path=[mock]): |
---|
20 | n/a | with self.assertRaises(ImportError) as cm: |
---|
21 | n/a | self.__import__('pkg.module') |
---|
22 | n/a | self.assertEqual(cm.exception.name, 'pkg') |
---|
23 | n/a | |
---|
24 | n/a | def test_raising_parent_after_importing_child(self): |
---|
25 | n/a | def __init__(): |
---|
26 | n/a | import pkg.module |
---|
27 | n/a | 1/0 |
---|
28 | n/a | mock = util.mock_spec('pkg.__init__', 'pkg.module', |
---|
29 | n/a | module_code={'pkg': __init__}) |
---|
30 | n/a | with mock: |
---|
31 | n/a | with util.import_state(meta_path=[mock]): |
---|
32 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
33 | n/a | self.__import__('pkg') |
---|
34 | n/a | self.assertNotIn('pkg', sys.modules) |
---|
35 | n/a | self.assertIn('pkg.module', sys.modules) |
---|
36 | n/a | with self.assertRaises(ZeroDivisionError): |
---|
37 | n/a | self.__import__('pkg.module') |
---|
38 | n/a | self.assertNotIn('pkg', sys.modules) |
---|
39 | n/a | self.assertIn('pkg.module', sys.modules) |
---|
40 | n/a | |
---|
41 | n/a | def test_raising_parent_after_relative_importing_child(self): |
---|
42 | n/a | def __init__(): |
---|
43 | n/a | from . import module |
---|
44 | n/a | 1/0 |
---|
45 | n/a | mock = util.mock_spec('pkg.__init__', 'pkg.module', |
---|
46 | n/a | module_code={'pkg': __init__}) |
---|
47 | n/a | with mock: |
---|
48 | n/a | with util.import_state(meta_path=[mock]): |
---|
49 | n/a | with self.assertRaises((ZeroDivisionError, ImportError)): |
---|
50 | n/a | # This raises ImportError on the "from . import module" |
---|
51 | n/a | # line, not sure why. |
---|
52 | n/a | self.__import__('pkg') |
---|
53 | n/a | self.assertNotIn('pkg', sys.modules) |
---|
54 | n/a | with self.assertRaises((ZeroDivisionError, ImportError)): |
---|
55 | n/a | self.__import__('pkg.module') |
---|
56 | n/a | self.assertNotIn('pkg', sys.modules) |
---|
57 | n/a | # XXX False |
---|
58 | n/a | #self.assertIn('pkg.module', sys.modules) |
---|
59 | n/a | |
---|
60 | n/a | def test_raising_parent_after_double_relative_importing_child(self): |
---|
61 | n/a | def __init__(): |
---|
62 | n/a | from ..subpkg import module |
---|
63 | n/a | 1/0 |
---|
64 | n/a | mock = util.mock_spec('pkg.__init__', 'pkg.subpkg.__init__', |
---|
65 | n/a | 'pkg.subpkg.module', |
---|
66 | n/a | module_code={'pkg.subpkg': __init__}) |
---|
67 | n/a | with mock: |
---|
68 | n/a | with util.import_state(meta_path=[mock]): |
---|
69 | n/a | with self.assertRaises((ZeroDivisionError, ImportError)): |
---|
70 | n/a | # This raises ImportError on the "from ..subpkg import module" |
---|
71 | n/a | # line, not sure why. |
---|
72 | n/a | self.__import__('pkg.subpkg') |
---|
73 | n/a | self.assertNotIn('pkg.subpkg', sys.modules) |
---|
74 | n/a | with self.assertRaises((ZeroDivisionError, ImportError)): |
---|
75 | n/a | self.__import__('pkg.subpkg.module') |
---|
76 | n/a | self.assertNotIn('pkg.subpkg', sys.modules) |
---|
77 | n/a | # XXX False |
---|
78 | n/a | #self.assertIn('pkg.subpkg.module', sys.modules) |
---|
79 | n/a | |
---|
80 | n/a | def test_module_not_package(self): |
---|
81 | n/a | # Try to import a submodule from a non-package should raise ImportError. |
---|
82 | n/a | assert not hasattr(sys, '__path__') |
---|
83 | n/a | with self.assertRaises(ImportError) as cm: |
---|
84 | n/a | self.__import__('sys.no_submodules_here') |
---|
85 | n/a | self.assertEqual(cm.exception.name, 'sys.no_submodules_here') |
---|
86 | n/a | |
---|
87 | n/a | def test_module_not_package_but_side_effects(self): |
---|
88 | n/a | # If a module injects something into sys.modules as a side-effect, then |
---|
89 | n/a | # pick up on that fact. |
---|
90 | n/a | name = 'mod' |
---|
91 | n/a | subname = name + '.b' |
---|
92 | n/a | def module_injection(): |
---|
93 | n/a | sys.modules[subname] = 'total bunk' |
---|
94 | n/a | mock_spec = util.mock_spec('mod', |
---|
95 | n/a | module_code={'mod': module_injection}) |
---|
96 | n/a | with mock_spec as mock: |
---|
97 | n/a | with util.import_state(meta_path=[mock]): |
---|
98 | n/a | try: |
---|
99 | n/a | submodule = self.__import__(subname) |
---|
100 | n/a | finally: |
---|
101 | n/a | support.unload(subname) |
---|
102 | n/a | |
---|
103 | n/a | |
---|
104 | n/a | (Frozen_ParentTests, |
---|
105 | n/a | Source_ParentTests |
---|
106 | n/a | ) = util.test_both(ParentModuleTests, __import__=util.__import__) |
---|
107 | n/a | |
---|
108 | n/a | |
---|
109 | n/a | if __name__ == '__main__': |
---|
110 | n/a | unittest.main() |
---|