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