ยปCore Development>Code coverage>Lib/test/test_importlib/import_/test_fromlist.py

Python code coverage for Lib/test/test_importlib/import_/test_fromlist.py

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