• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from test.test_importlib import abc, util
2
3machinery = util.import_importlib('importlib.machinery')
4
5import sys
6import unittest
7import warnings
8
9
10@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
11class FindSpecTests(abc.FinderTests):
12
13    """Test find_spec() for built-in modules."""
14
15    def test_module(self):
16        # Common case.
17        with util.uncache(util.BUILTINS.good_name):
18            found = self.machinery.BuiltinImporter.find_spec(util.BUILTINS.good_name)
19            self.assertTrue(found)
20            self.assertEqual(found.origin, 'built-in')
21
22    # Built-in modules cannot be a package.
23    test_package = None
24
25    # Built-in modules cannot be in a package.
26    test_module_in_package = None
27
28    # Built-in modules cannot be a package.
29    test_package_in_package = None
30
31    # Built-in modules cannot be a package.
32    test_package_over_module = None
33
34    def test_failure(self):
35        name = 'importlib'
36        assert name not in sys.builtin_module_names
37        spec = self.machinery.BuiltinImporter.find_spec(name)
38        self.assertIsNone(spec)
39
40
41(Frozen_FindSpecTests,
42 Source_FindSpecTests
43 ) = util.test_both(FindSpecTests, machinery=machinery)
44
45
46@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
47class FinderTests(abc.FinderTests):
48
49    """Test find_module() for built-in modules."""
50
51    def test_module(self):
52        # Common case.
53        with util.uncache(util.BUILTINS.good_name):
54            with warnings.catch_warnings():
55                warnings.simplefilter("ignore", DeprecationWarning)
56                found = self.machinery.BuiltinImporter.find_module(util.BUILTINS.good_name)
57            self.assertTrue(found)
58            self.assertTrue(hasattr(found, 'load_module'))
59
60    # Built-in modules cannot be a package.
61    test_package = test_package_in_package = test_package_over_module = None
62
63    # Built-in modules cannot be in a package.
64    test_module_in_package = None
65
66    def test_failure(self):
67        assert 'importlib' not in sys.builtin_module_names
68        with warnings.catch_warnings():
69            warnings.simplefilter("ignore", DeprecationWarning)
70            loader = self.machinery.BuiltinImporter.find_module('importlib')
71        self.assertIsNone(loader)
72
73
74(Frozen_FinderTests,
75 Source_FinderTests
76 ) = util.test_both(FinderTests, machinery=machinery)
77
78
79if __name__ == '__main__':
80    unittest.main()
81