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