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