Home | History | Annotate | Download | only in builtin
      1 from .. import abc
      2 from .. import util
      3 
      4 machinery = util.import_importlib('importlib.machinery')
      5 
      6 import sys
      7 import unittest
      8 
      9 
     10 @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
     11 class 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 cannobt 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     def test_ignore_path(self):
     41         # The value for 'path' should always trigger a failed import.
     42         with util.uncache(util.BUILTINS.good_name):
     43             spec = self.machinery.BuiltinImporter.find_spec(util.BUILTINS.good_name,
     44                                                             ['pkg'])
     45             self.assertIsNone(spec)
     46 
     47 
     48 (Frozen_FindSpecTests,
     49  Source_FindSpecTests
     50  ) = util.test_both(FindSpecTests, machinery=machinery)
     51 
     52 
     53 @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
     54 class FinderTests(abc.FinderTests):
     55 
     56     """Test find_module() for built-in modules."""
     57 
     58     def test_module(self):
     59         # Common case.
     60         with util.uncache(util.BUILTINS.good_name):
     61             found = self.machinery.BuiltinImporter.find_module(util.BUILTINS.good_name)
     62             self.assertTrue(found)
     63             self.assertTrue(hasattr(found, 'load_module'))
     64 
     65     # Built-in modules cannot be a package.
     66     test_package = test_package_in_package = test_package_over_module = None
     67 
     68     # Built-in modules cannot be in a package.
     69     test_module_in_package = None
     70 
     71     def test_failure(self):
     72         assert 'importlib' not in sys.builtin_module_names
     73         loader = self.machinery.BuiltinImporter.find_module('importlib')
     74         self.assertIsNone(loader)
     75 
     76     def test_ignore_path(self):
     77         # The value for 'path' should always trigger a failed import.
     78         with util.uncache(util.BUILTINS.good_name):
     79             loader = self.machinery.BuiltinImporter.find_module(util.BUILTINS.good_name,
     80                                                             ['pkg'])
     81             self.assertIsNone(loader)
     82 
     83 
     84 (Frozen_FinderTests,
     85  Source_FinderTests
     86  ) = util.test_both(FinderTests, machinery=machinery)
     87 
     88 
     89 if __name__ == '__main__':
     90     unittest.main()
     91