ยปCore Development>Code coverage>Lib/test/test_importlib/builtin/test_loader.py

Python code coverage for Lib/test/test_importlib/builtin/test_loader.py

#countcontent
1n/afrom .. import abc
2n/afrom .. import util
3n/a
4n/amachinery = util.import_importlib('importlib.machinery')
5n/a
6n/aimport sys
7n/aimport types
8n/aimport unittest
9n/a
10n/a@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
11n/aclass LoaderTests(abc.LoaderTests):
12n/a
13n/a """Test load_module() for built-in modules."""
14n/a
15n/a def setUp(self):
16n/a self.verification = {'__name__': 'errno', '__package__': '',
17n/a '__loader__': self.machinery.BuiltinImporter}
18n/a
19n/a def verify(self, module):
20n/a """Verify that the module matches against what it should have."""
21n/a self.assertIsInstance(module, types.ModuleType)
22n/a for attr, value in self.verification.items():
23n/a self.assertEqual(getattr(module, attr), value)
24n/a self.assertIn(module.__name__, sys.modules)
25n/a
26n/a def load_module(self, name):
27n/a return self.machinery.BuiltinImporter.load_module(name)
28n/a
29n/a def test_module(self):
30n/a # Common case.
31n/a with util.uncache(util.BUILTINS.good_name):
32n/a module = self.load_module(util.BUILTINS.good_name)
33n/a self.verify(module)
34n/a
35n/a # Built-in modules cannot be a package.
36n/a test_package = test_lacking_parent = None
37n/a
38n/a # No way to force an import failure.
39n/a test_state_after_failure = None
40n/a
41n/a def test_module_reuse(self):
42n/a # Test that the same module is used in a reload.
43n/a with util.uncache(util.BUILTINS.good_name):
44n/a module1 = self.load_module(util.BUILTINS.good_name)
45n/a module2 = self.load_module(util.BUILTINS.good_name)
46n/a self.assertIs(module1, module2)
47n/a
48n/a def test_unloadable(self):
49n/a name = 'dssdsdfff'
50n/a assert name not in sys.builtin_module_names
51n/a with self.assertRaises(ImportError) as cm:
52n/a self.load_module(name)
53n/a self.assertEqual(cm.exception.name, name)
54n/a
55n/a def test_already_imported(self):
56n/a # Using the name of a module already imported but not a built-in should
57n/a # still fail.
58n/a module_name = 'builtin_reload_test'
59n/a assert module_name not in sys.builtin_module_names
60n/a with util.uncache(module_name):
61n/a module = types.ModuleType(module_name)
62n/a sys.modules[module_name] = module
63n/a with self.assertRaises(ImportError) as cm:
64n/a self.load_module(module_name)
65n/a self.assertEqual(cm.exception.name, module_name)
66n/a
67n/a
68n/a(Frozen_LoaderTests,
69n/a Source_LoaderTests
70n/a ) = util.test_both(LoaderTests, machinery=machinery)
71n/a
72n/a
73n/a@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
74n/aclass InspectLoaderTests:
75n/a
76n/a """Tests for InspectLoader methods for BuiltinImporter."""
77n/a
78n/a def test_get_code(self):
79n/a # There is no code object.
80n/a result = self.machinery.BuiltinImporter.get_code(util.BUILTINS.good_name)
81n/a self.assertIsNone(result)
82n/a
83n/a def test_get_source(self):
84n/a # There is no source.
85n/a result = self.machinery.BuiltinImporter.get_source(util.BUILTINS.good_name)
86n/a self.assertIsNone(result)
87n/a
88n/a def test_is_package(self):
89n/a # Cannot be a package.
90n/a result = self.machinery.BuiltinImporter.is_package(util.BUILTINS.good_name)
91n/a self.assertFalse(result)
92n/a
93n/a @unittest.skipIf(util.BUILTINS.bad_name is None, 'all modules are built in')
94n/a def test_not_builtin(self):
95n/a # Modules not built-in should raise ImportError.
96n/a for meth_name in ('get_code', 'get_source', 'is_package'):
97n/a method = getattr(self.machinery.BuiltinImporter, meth_name)
98n/a with self.assertRaises(ImportError) as cm:
99n/a method(util.BUILTINS.bad_name)
100n/a
101n/a
102n/a(Frozen_InspectLoaderTests,
103n/a Source_InspectLoaderTests
104n/a ) = util.test_both(InspectLoaderTests, machinery=machinery)
105n/a
106n/a
107n/aif __name__ == '__main__':
108n/a unittest.main()