| 
									
										
										
										
											2009-01-30 00:22:35 +00:00
										 |  |  | from .. import abc | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  | from .. import util | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | machinery = util.import_importlib('importlib.machinery') | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 10:01:27 -06:00
										 |  |  | import _imp | 
					
						
							|  |  |  | import marshal | 
					
						
							|  |  |  | import os.path | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2021-04-06 08:56:57 -07:00
										 |  |  | import warnings | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  | from test.support import import_helper, REPO_ROOT, STDLIB_DIR | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def resolve_stdlib_file(name, ispkg=False): | 
					
						
							|  |  |  |     assert name | 
					
						
							|  |  |  |     if ispkg: | 
					
						
							|  |  |  |         return os.path.join(STDLIB_DIR, *name.split('.'), '__init__.py') | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return os.path.join(STDLIB_DIR, *name.split('.')) + '.py' | 
					
						
							| 
									
										
										
										
											2021-09-14 17:31:45 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  | class FindSpecTests(abc.FinderTests): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Test finding frozen modules.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  |     def find(self, name, **kwargs): | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |         finder = self.machinery.FrozenImporter | 
					
						
							| 
									
										
										
										
											2021-09-14 17:31:45 -06:00
										 |  |  |         with import_helper.frozen_modules(): | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  |             return finder.find_spec(name, **kwargs) | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 10:01:27 -06:00
										 |  |  |     def check_basic(self, spec, name, ispkg=False): | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  |         self.assertEqual(spec.name, name) | 
					
						
							|  |  |  |         self.assertIs(spec.loader, self.machinery.FrozenImporter) | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |         self.assertEqual(spec.origin, 'frozen') | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  |         self.assertFalse(spec.has_location) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:01:27 -06:00
										 |  |  |         if ispkg: | 
					
						
							|  |  |  |             self.assertIsNotNone(spec.submodule_search_locations) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.assertIsNone(spec.submodule_search_locations) | 
					
						
							|  |  |  |         self.assertIsNotNone(spec.loader_state) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |     def check_loader_state(self, spec, origname=None, filename=None): | 
					
						
							|  |  |  |         if not filename: | 
					
						
							|  |  |  |             if not origname: | 
					
						
							|  |  |  |                 origname = spec.name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         actual = dict(vars(spec.loader_state)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Check the code object used to import the frozen module. | 
					
						
							|  |  |  |         # We can't compare the marshaled data directly because | 
					
						
							|  |  |  |         # marshal.dumps() would mark "expected" (below) as a ref, | 
					
						
							|  |  |  |         # which slightly changes the output. | 
					
						
							|  |  |  |         # (See https://bugs.python.org/issue34093.) | 
					
						
							|  |  |  |         data = actual.pop('data') | 
					
						
							| 
									
										
										
										
											2021-10-05 10:01:27 -06:00
										 |  |  |         with import_helper.frozen_modules(): | 
					
						
							|  |  |  |             expected = _imp.get_frozen_object(spec.name) | 
					
						
							|  |  |  |         code = marshal.loads(data) | 
					
						
							|  |  |  |         self.assertEqual(code, expected) | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |         # Check the rest of spec.loader_state. | 
					
						
							|  |  |  |         expected = dict( | 
					
						
							|  |  |  |             origname=origname, | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         self.assertDictEqual(actual, expected) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |     def check_search_locations(self, spec): | 
					
						
							|  |  |  |         # Frozen packages do not have any path entries. | 
					
						
							|  |  |  |         # (See https://bugs.python.org/issue21736.) | 
					
						
							|  |  |  |         expected = [] | 
					
						
							|  |  |  |         self.assertListEqual(spec.submodule_search_locations, expected) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  |     def test_module(self): | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |         modules = [ | 
					
						
							|  |  |  |             '__hello__', | 
					
						
							|  |  |  |             '__phello__.spam', | 
					
						
							|  |  |  |             '__phello__.ham.eggs', | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         for name in modules: | 
					
						
							|  |  |  |             with self.subTest(f'{name} -> {name}'): | 
					
						
							|  |  |  |                 spec = self.find(name) | 
					
						
							|  |  |  |                 self.check_basic(spec, name) | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |                 self.check_loader_state(spec) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:01:27 -06:00
										 |  |  |         modules = { | 
					
						
							|  |  |  |             '__hello_alias__': '__hello__', | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |             '_frozen_importlib': 'importlib._bootstrap', | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         for name, origname in modules.items(): | 
					
						
							|  |  |  |             with self.subTest(f'{name} -> {origname}'): | 
					
						
							|  |  |  |                 spec = self.find(name) | 
					
						
							|  |  |  |                 self.check_basic(spec, name) | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |                 self.check_loader_state(spec, origname) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |         modules = [ | 
					
						
							|  |  |  |             '__phello__.__init__', | 
					
						
							|  |  |  |             '__phello__.ham.__init__', | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         for name in modules: | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |             origname = '<' + name.rpartition('.')[0] | 
					
						
							|  |  |  |             filename = resolve_stdlib_file(name) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |             with self.subTest(f'{name} -> {origname}'): | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  |                 spec = self.find(name) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:01:27 -06:00
										 |  |  |                 self.check_basic(spec, name) | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |                 self.check_loader_state(spec, origname, filename) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |         modules = { | 
					
						
							|  |  |  |             '__hello_only__': ('Tools', 'freeze', 'flag.py'), | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         for name, path in modules.items(): | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |             origname = None | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |             filename = os.path.join(REPO_ROOT, *path) | 
					
						
							|  |  |  |             with self.subTest(f'{name} -> {filename}'): | 
					
						
							|  |  |  |                 spec = self.find(name) | 
					
						
							|  |  |  |                 self.check_basic(spec, name) | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |                 self.check_loader_state(spec, origname, filename) | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  |     def test_package(self): | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |         packages = [ | 
					
						
							|  |  |  |             '__phello__', | 
					
						
							|  |  |  |             '__phello__.ham', | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         for name in packages: | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |             filename = resolve_stdlib_file(name, ispkg=True) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |             with self.subTest(f'{name} -> {name}'): | 
					
						
							|  |  |  |                 spec = self.find(name) | 
					
						
							|  |  |  |                 self.check_basic(spec, name, ispkg=True) | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |                 self.check_loader_state(spec, name, filename) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |                 self.check_search_locations(spec) | 
					
						
							|  |  |  |         packages = { | 
					
						
							| 
									
										
										
										
											2021-10-05 10:01:27 -06:00
										 |  |  |             '__phello_alias__': '__hello__', | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |         for name, origname in packages.items(): | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |             filename = resolve_stdlib_file(origname, ispkg=False) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |             with self.subTest(f'{name} -> {origname}'): | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  |                 spec = self.find(name) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:01:27 -06:00
										 |  |  |                 self.check_basic(spec, name, ispkg=True) | 
					
						
							| 
									
										
										
										
											2021-10-05 11:26:37 -06:00
										 |  |  |                 self.check_loader_state(spec, origname, filename) | 
					
						
							| 
									
										
										
										
											2021-10-05 10:37:14 -06:00
										 |  |  |                 self.check_search_locations(spec) | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # These are covered by test_module() and test_package(). | 
					
						
							|  |  |  |     test_module_in_package = None | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |     test_package_in_package = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # No easy way to test. | 
					
						
							|  |  |  |     test_package_over_module = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  |     def test_path_ignored(self): | 
					
						
							|  |  |  |         for name in ('__hello__', '__phello__', '__phello__.spam'): | 
					
						
							|  |  |  |             actual = self.find(name) | 
					
						
							|  |  |  |             for path in (None, object(), '', 'eggs', [], [''], ['eggs']): | 
					
						
							|  |  |  |                 with self.subTest((name, path)): | 
					
						
							|  |  |  |                     spec = self.find(name, path=path) | 
					
						
							|  |  |  |                     self.assertEqual(spec, actual) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_target_ignored(self): | 
					
						
							|  |  |  |         imported = ('__hello__', '__phello__') | 
					
						
							|  |  |  |         with import_helper.CleanImport(*imported, usefrozen=True): | 
					
						
							|  |  |  |             import __hello__ as match | 
					
						
							|  |  |  |             import __phello__ as nonmatch | 
					
						
							|  |  |  |         name = '__hello__' | 
					
						
							|  |  |  |         actual = self.find(name) | 
					
						
							|  |  |  |         for target in (None, match, nonmatch, object(), 'not-a-module-object'): | 
					
						
							|  |  |  |             with self.subTest(target): | 
					
						
							|  |  |  |                 spec = self.find(name, target=target) | 
					
						
							|  |  |  |                 self.assertEqual(spec, actual) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |     def test_failure(self): | 
					
						
							|  |  |  |         spec = self.find('<not real>') | 
					
						
							|  |  |  |         self.assertIsNone(spec) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 18:38:52 -06:00
										 |  |  |     def test_not_using_frozen(self): | 
					
						
							|  |  |  |         finder = self.machinery.FrozenImporter | 
					
						
							|  |  |  |         with import_helper.frozen_modules(enabled=False): | 
					
						
							|  |  |  |             # both frozen and not frozen | 
					
						
							|  |  |  |             spec1 = finder.find_spec('__hello__') | 
					
						
							|  |  |  |             # only frozen | 
					
						
							|  |  |  |             spec2 = finder.find_spec('__hello_only__') | 
					
						
							|  |  |  |         self.assertIsNone(spec1) | 
					
						
							|  |  |  |         self.assertIsNone(spec2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | (Frozen_FindSpecTests, | 
					
						
							|  |  |  |  Source_FindSpecTests | 
					
						
							|  |  |  |  ) = util.test_both(FindSpecTests, machinery=machinery) | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  | class FinderTests(abc.FinderTests): | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     """Test finding frozen modules.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def find(self, name, path=None): | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |         finder = self.machinery.FrozenImporter | 
					
						
							| 
									
										
										
										
											2021-04-06 08:56:57 -07:00
										 |  |  |         with warnings.catch_warnings(): | 
					
						
							|  |  |  |             warnings.simplefilter("ignore", DeprecationWarning) | 
					
						
							| 
									
										
										
										
											2021-09-14 17:31:45 -06:00
										 |  |  |             with import_helper.frozen_modules(): | 
					
						
							|  |  |  |                 return finder.find_module(name, path) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_module(self): | 
					
						
							|  |  |  |         name = '__hello__' | 
					
						
							|  |  |  |         loader = self.find(name) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(hasattr(loader, 'load_module')) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_package(self): | 
					
						
							|  |  |  |         loader = self.find('__phello__') | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(hasattr(loader, 'load_module')) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_module_in_package(self): | 
					
						
							|  |  |  |         loader = self.find('__phello__.spam', ['__phello__']) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(hasattr(loader, 'load_module')) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |     # No frozen package within another package to test with. | 
					
						
							|  |  |  |     test_package_in_package = None | 
					
						
							| 
									
										
										
										
											2009-01-27 01:44:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |     # No easy way to test. | 
					
						
							|  |  |  |     test_package_over_module = None | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_failure(self): | 
					
						
							|  |  |  |         loader = self.find('<not real>') | 
					
						
							| 
									
										
										
										
											2012-06-28 06:15:01 -04:00
										 |  |  |         self.assertIsNone(loader) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | (Frozen_FinderTests, | 
					
						
							|  |  |  |  Source_FinderTests | 
					
						
							|  |  |  |  ) = util.test_both(FinderTests, machinery=machinery) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |     unittest.main() |