mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Issue #16803: Move test.test_importlib.extension to use both frozen and source importlib code
This commit is contained in:
		
							parent
							
								
									6a4cbc00ab
								
							
						
					
					
						commit
						e38b0544c4
					
				
					 4 changed files with 44 additions and 44 deletions
				
			
		|  | @ -1,23 +1,25 @@ | ||||||
|  | from importlib import _bootstrap | ||||||
| import sys | import sys | ||||||
| from test import support | from test import support | ||||||
| import unittest | import unittest | ||||||
| 
 | 
 | ||||||
| from importlib import _bootstrap |  | ||||||
| from importlib import machinery |  | ||||||
| from .. import util | from .. import util | ||||||
| from . import util as ext_util | from . import util as ext_util | ||||||
| 
 | 
 | ||||||
|  | frozen_machinery, source_machinery = util.import_importlib('importlib.machinery') | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | @unittest.skipIf(ext_util.FILENAME is None, '_testcapi not available') | ||||||
| @util.case_insensitive_tests | @util.case_insensitive_tests | ||||||
| class ExtensionModuleCaseSensitivityTest(unittest.TestCase): | class ExtensionModuleCaseSensitivityTest: | ||||||
| 
 | 
 | ||||||
|     def find_module(self): |     def find_module(self): | ||||||
|         good_name = ext_util.NAME |         good_name = ext_util.NAME | ||||||
|         bad_name = good_name.upper() |         bad_name = good_name.upper() | ||||||
|         assert good_name != bad_name |         assert good_name != bad_name | ||||||
|         finder = machinery.FileFinder(ext_util.PATH, |         finder = self.machinery.FileFinder(ext_util.PATH, | ||||||
|                                         (machinery.ExtensionFileLoader, |                                           (self.machinery.ExtensionFileLoader, | ||||||
|                                          machinery.EXTENSION_SUFFIXES)) |                                            self.machinery.EXTENSION_SUFFIXES)) | ||||||
|         return finder.find_module(bad_name) |         return finder.find_module(bad_name) | ||||||
| 
 | 
 | ||||||
|     def test_case_sensitive(self): |     def test_case_sensitive(self): | ||||||
|  | @ -38,14 +40,10 @@ def test_case_insensitivity(self): | ||||||
|             loader = self.find_module() |             loader = self.find_module() | ||||||
|             self.assertTrue(hasattr(loader, 'load_module')) |             self.assertTrue(hasattr(loader, 'load_module')) | ||||||
| 
 | 
 | ||||||
| 
 | Frozen_ExtensionCaseSensitivity, Source_ExtensionCaseSensitivity = util.test_both( | ||||||
| 
 |         ExtensionModuleCaseSensitivityTest, | ||||||
| 
 |         machinery=[frozen_machinery, source_machinery]) | ||||||
| def test_main(): |  | ||||||
|     if ext_util.FILENAME is None: |  | ||||||
|         return |  | ||||||
|     support.run_unittest(ExtensionModuleCaseSensitivityTest) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     test_main() |     unittest.main() | ||||||
|  |  | ||||||
|  | @ -1,17 +1,20 @@ | ||||||
| from importlib import machinery |  | ||||||
| from .. import abc | from .. import abc | ||||||
|  | from .. import util as test_util | ||||||
| from . import util | from . import util | ||||||
| 
 | 
 | ||||||
|  | machinery = test_util.import_importlib('importlib.machinery') | ||||||
|  | 
 | ||||||
| import unittest | import unittest | ||||||
| 
 | 
 | ||||||
| class FinderTests(unittest.TestCase, abc.FinderTests): | 
 | ||||||
|  | class FinderTests(abc.FinderTests): | ||||||
| 
 | 
 | ||||||
|     """Test the finder for extension modules.""" |     """Test the finder for extension modules.""" | ||||||
| 
 | 
 | ||||||
|     def find_module(self, fullname): |     def find_module(self, fullname): | ||||||
|         importer = machinery.FileFinder(util.PATH, |         importer = self.machinery.FileFinder(util.PATH, | ||||||
|                                         (machinery.ExtensionFileLoader, |                                             (self.machinery.ExtensionFileLoader, | ||||||
|                                          machinery.EXTENSION_SUFFIXES)) |                                              self.machinery.EXTENSION_SUFFIXES)) | ||||||
|         return importer.find_module(fullname) |         return importer.find_module(fullname) | ||||||
| 
 | 
 | ||||||
|     def test_module(self): |     def test_module(self): | ||||||
|  | @ -36,11 +39,9 @@ def test_package_over_module(self): | ||||||
|     def test_failure(self): |     def test_failure(self): | ||||||
|         self.assertIsNone(self.find_module('asdfjkl;')) |         self.assertIsNone(self.find_module('asdfjkl;')) | ||||||
| 
 | 
 | ||||||
| 
 | Frozen_FinderTests, Source_FinderTests = test_util.test_both( | ||||||
| def test_main(): |         FinderTests, machinery=machinery) | ||||||
|     from test.support import run_unittest |  | ||||||
|     run_unittest(FinderTests) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     test_main() |     unittest.main() | ||||||
|  |  | ||||||
|  | @ -1,19 +1,20 @@ | ||||||
| from importlib import machinery |  | ||||||
| from . import util as ext_util | from . import util as ext_util | ||||||
| from .. import abc | from .. import abc | ||||||
| from .. import util | from .. import util | ||||||
| 
 | 
 | ||||||
|  | machinery = util.import_importlib('importlib.machinery') | ||||||
|  | 
 | ||||||
| import os.path | import os.path | ||||||
| import sys | import sys | ||||||
| import unittest | import unittest | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class LoaderTests(unittest.TestCase, abc.LoaderTests): | class LoaderTests(abc.LoaderTests): | ||||||
| 
 | 
 | ||||||
|     """Test load_module() for extension modules.""" |     """Test load_module() for extension modules.""" | ||||||
| 
 | 
 | ||||||
|     def setUp(self): |     def setUp(self): | ||||||
|         self.loader = machinery.ExtensionFileLoader(ext_util.NAME, |         self.loader = self.machinery.ExtensionFileLoader(ext_util.NAME, | ||||||
|                                                          ext_util.FILEPATH) |                                                          ext_util.FILEPATH) | ||||||
| 
 | 
 | ||||||
|     def load_module(self, fullname): |     def load_module(self, fullname): | ||||||
|  | @ -36,7 +37,7 @@ def test_module(self): | ||||||
|                 self.assertEqual(getattr(module, attr), value) |                 self.assertEqual(getattr(module, attr), value) | ||||||
|             self.assertIn(ext_util.NAME, sys.modules) |             self.assertIn(ext_util.NAME, sys.modules) | ||||||
|             self.assertIsInstance(module.__loader__, |             self.assertIsInstance(module.__loader__, | ||||||
|                                   machinery.ExtensionFileLoader) |                                   self.machinery.ExtensionFileLoader) | ||||||
| 
 | 
 | ||||||
|     def test_package(self): |     def test_package(self): | ||||||
|         # No extension module as __init__ available for testing. |         # No extension module as __init__ available for testing. | ||||||
|  | @ -64,16 +65,15 @@ def test_unloadable(self): | ||||||
| 
 | 
 | ||||||
|     def test_is_package(self): |     def test_is_package(self): | ||||||
|         self.assertFalse(self.loader.is_package(ext_util.NAME)) |         self.assertFalse(self.loader.is_package(ext_util.NAME)) | ||||||
|         for suffix in machinery.EXTENSION_SUFFIXES: |         for suffix in self.machinery.EXTENSION_SUFFIXES: | ||||||
|             path = os.path.join('some', 'path', 'pkg', '__init__' + suffix) |             path = os.path.join('some', 'path', 'pkg', '__init__' + suffix) | ||||||
|             loader = machinery.ExtensionFileLoader('pkg', path) |             loader = self.machinery.ExtensionFileLoader('pkg', path) | ||||||
|             self.assertTrue(loader.is_package('pkg')) |             self.assertTrue(loader.is_package('pkg')) | ||||||
| 
 | 
 | ||||||
|  | Frozen_LoaderTests, Source_LoaderTests = util.test_both( | ||||||
|  |         LoaderTests, machinery=machinery) | ||||||
| 
 | 
 | ||||||
| def test_main(): |  | ||||||
|     from test.support import run_unittest |  | ||||||
|     run_unittest(LoaderTests) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     test_main() |     unittest.main() | ||||||
|  |  | ||||||
|  | @ -1,31 +1,32 @@ | ||||||
| from importlib import machinery | from .. import util as test_util | ||||||
| from . import util | from . import util | ||||||
| 
 | 
 | ||||||
|  | machinery = test_util.import_importlib('importlib.machinery') | ||||||
|  | 
 | ||||||
| import collections | import collections | ||||||
| import sys | import sys | ||||||
| import unittest | import unittest | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class PathHookTests(unittest.TestCase): | class PathHookTests: | ||||||
| 
 | 
 | ||||||
|     """Test the path hook for extension modules.""" |     """Test the path hook for extension modules.""" | ||||||
|     # XXX Should it only succeed for pre-existing directories? |     # XXX Should it only succeed for pre-existing directories? | ||||||
|     # XXX Should it only work for directories containing an extension module? |     # XXX Should it only work for directories containing an extension module? | ||||||
| 
 | 
 | ||||||
|     def hook(self, entry): |     def hook(self, entry): | ||||||
|         return machinery.FileFinder.path_hook((machinery.ExtensionFileLoader, |         return self.machinery.FileFinder.path_hook( | ||||||
|             machinery.EXTENSION_SUFFIXES))(entry) |                 (self.machinery.ExtensionFileLoader, | ||||||
|  |                  self.machinery.EXTENSION_SUFFIXES))(entry) | ||||||
| 
 | 
 | ||||||
|     def test_success(self): |     def test_success(self): | ||||||
|         # Path hook should handle a directory where a known extension module |         # Path hook should handle a directory where a known extension module | ||||||
|         # exists. |         # exists. | ||||||
|         self.assertTrue(hasattr(self.hook(util.PATH), 'find_module')) |         self.assertTrue(hasattr(self.hook(util.PATH), 'find_module')) | ||||||
| 
 | 
 | ||||||
| 
 | Frozen_PathHooksTests, Source_PathHooksTests = test_util.test_both( | ||||||
| def test_main(): |         PathHookTests, machinery=machinery) | ||||||
|     from test.support import run_unittest |  | ||||||
|     run_unittest(PathHookTests) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     test_main() |     unittest.main() | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Brett Cannon
						Brett Cannon