1 | n/a | """Test that the semantics relating to the 'fromlist' argument are correct.""" |
---|
2 | n/a | from .. import util |
---|
3 | n/a | import unittest |
---|
4 | n/a | |
---|
5 | n/a | |
---|
6 | n/a | class ReturnValue: |
---|
7 | n/a | |
---|
8 | n/a | """The use of fromlist influences what import returns. |
---|
9 | n/a | |
---|
10 | n/a | If direct ``import ...`` statement is used, the root module or package is |
---|
11 | n/a | returned [import return]. But if fromlist is set, then the specified module |
---|
12 | n/a | is actually returned (whether it is a relative import or not) |
---|
13 | n/a | [from return]. |
---|
14 | n/a | |
---|
15 | n/a | """ |
---|
16 | n/a | |
---|
17 | n/a | def test_return_from_import(self): |
---|
18 | n/a | # [import return] |
---|
19 | n/a | with util.mock_spec('pkg.__init__', 'pkg.module') as importer: |
---|
20 | n/a | with util.import_state(meta_path=[importer]): |
---|
21 | n/a | module = self.__import__('pkg.module') |
---|
22 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
23 | n/a | |
---|
24 | n/a | def test_return_from_from_import(self): |
---|
25 | n/a | # [from return] |
---|
26 | n/a | with util.mock_modules('pkg.__init__', 'pkg.module')as importer: |
---|
27 | n/a | with util.import_state(meta_path=[importer]): |
---|
28 | n/a | module = self.__import__('pkg.module', fromlist=['attr']) |
---|
29 | n/a | self.assertEqual(module.__name__, 'pkg.module') |
---|
30 | n/a | |
---|
31 | n/a | |
---|
32 | n/a | (Frozen_ReturnValue, |
---|
33 | n/a | Source_ReturnValue |
---|
34 | n/a | ) = util.test_both(ReturnValue, __import__=util.__import__) |
---|
35 | n/a | |
---|
36 | n/a | |
---|
37 | n/a | class HandlingFromlist: |
---|
38 | n/a | |
---|
39 | n/a | """Using fromlist triggers different actions based on what is being asked |
---|
40 | n/a | of it. |
---|
41 | n/a | |
---|
42 | n/a | If fromlist specifies an object on a module, nothing special happens |
---|
43 | n/a | [object case]. This is even true if the object does not exist [bad object]. |
---|
44 | n/a | |
---|
45 | n/a | If a package is being imported, then what is listed in fromlist may be |
---|
46 | n/a | treated as a module to be imported [module]. And this extends to what is |
---|
47 | n/a | contained in __all__ when '*' is imported [using *]. And '*' does not need |
---|
48 | n/a | to be the only name in the fromlist [using * with others]. |
---|
49 | n/a | |
---|
50 | n/a | """ |
---|
51 | n/a | |
---|
52 | n/a | def test_object(self): |
---|
53 | n/a | # [object case] |
---|
54 | n/a | with util.mock_modules('module') as importer: |
---|
55 | n/a | with util.import_state(meta_path=[importer]): |
---|
56 | n/a | module = self.__import__('module', fromlist=['attr']) |
---|
57 | n/a | self.assertEqual(module.__name__, 'module') |
---|
58 | n/a | |
---|
59 | n/a | def test_nonexistent_object(self): |
---|
60 | n/a | # [bad object] |
---|
61 | n/a | with util.mock_modules('module') as importer: |
---|
62 | n/a | with util.import_state(meta_path=[importer]): |
---|
63 | n/a | module = self.__import__('module', fromlist=['non_existent']) |
---|
64 | n/a | self.assertEqual(module.__name__, 'module') |
---|
65 | n/a | self.assertFalse(hasattr(module, 'non_existent')) |
---|
66 | n/a | |
---|
67 | n/a | def test_module_from_package(self): |
---|
68 | n/a | # [module] |
---|
69 | n/a | with util.mock_modules('pkg.__init__', 'pkg.module') as importer: |
---|
70 | n/a | with util.import_state(meta_path=[importer]): |
---|
71 | n/a | module = self.__import__('pkg', fromlist=['module']) |
---|
72 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
73 | n/a | self.assertTrue(hasattr(module, 'module')) |
---|
74 | n/a | self.assertEqual(module.module.__name__, 'pkg.module') |
---|
75 | n/a | |
---|
76 | n/a | def test_module_from_package_triggers_ModuleNotFoundError(self): |
---|
77 | n/a | # If a submodule causes an ModuleNotFoundError because it tries |
---|
78 | n/a | # to import a module which doesn't exist, that should let the |
---|
79 | n/a | # ModuleNotFoundError propagate. |
---|
80 | n/a | def module_code(): |
---|
81 | n/a | import i_do_not_exist |
---|
82 | n/a | with util.mock_modules('pkg.__init__', 'pkg.mod', |
---|
83 | n/a | module_code={'pkg.mod': module_code}) as importer: |
---|
84 | n/a | with util.import_state(meta_path=[importer]): |
---|
85 | n/a | with self.assertRaises(ModuleNotFoundError) as exc: |
---|
86 | n/a | self.__import__('pkg', fromlist=['mod']) |
---|
87 | n/a | self.assertEqual('i_do_not_exist', exc.exception.name) |
---|
88 | n/a | |
---|
89 | n/a | def test_empty_string(self): |
---|
90 | n/a | with util.mock_modules('pkg.__init__', 'pkg.mod') as importer: |
---|
91 | n/a | with util.import_state(meta_path=[importer]): |
---|
92 | n/a | module = self.__import__('pkg.mod', fromlist=['']) |
---|
93 | n/a | self.assertEqual(module.__name__, 'pkg.mod') |
---|
94 | n/a | |
---|
95 | n/a | def basic_star_test(self, fromlist=['*']): |
---|
96 | n/a | # [using *] |
---|
97 | n/a | with util.mock_modules('pkg.__init__', 'pkg.module') as mock: |
---|
98 | n/a | with util.import_state(meta_path=[mock]): |
---|
99 | n/a | mock['pkg'].__all__ = ['module'] |
---|
100 | n/a | module = self.__import__('pkg', fromlist=fromlist) |
---|
101 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
102 | n/a | self.assertTrue(hasattr(module, 'module')) |
---|
103 | n/a | self.assertEqual(module.module.__name__, 'pkg.module') |
---|
104 | n/a | |
---|
105 | n/a | def test_using_star(self): |
---|
106 | n/a | # [using *] |
---|
107 | n/a | self.basic_star_test() |
---|
108 | n/a | |
---|
109 | n/a | def test_fromlist_as_tuple(self): |
---|
110 | n/a | self.basic_star_test(('*',)) |
---|
111 | n/a | |
---|
112 | n/a | def test_star_with_others(self): |
---|
113 | n/a | # [using * with others] |
---|
114 | n/a | context = util.mock_modules('pkg.__init__', 'pkg.module1', 'pkg.module2') |
---|
115 | n/a | with context as mock: |
---|
116 | n/a | with util.import_state(meta_path=[mock]): |
---|
117 | n/a | mock['pkg'].__all__ = ['module1'] |
---|
118 | n/a | module = self.__import__('pkg', fromlist=['module2', '*']) |
---|
119 | n/a | self.assertEqual(module.__name__, 'pkg') |
---|
120 | n/a | self.assertTrue(hasattr(module, 'module1')) |
---|
121 | n/a | self.assertTrue(hasattr(module, 'module2')) |
---|
122 | n/a | self.assertEqual(module.module1.__name__, 'pkg.module1') |
---|
123 | n/a | self.assertEqual(module.module2.__name__, 'pkg.module2') |
---|
124 | n/a | |
---|
125 | n/a | |
---|
126 | n/a | (Frozen_FromList, |
---|
127 | n/a | Source_FromList |
---|
128 | n/a | ) = util.test_both(HandlingFromlist, __import__=util.__import__) |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | if __name__ == '__main__': |
---|
132 | n/a | unittest.main() |
---|