| 
									
										
										
										
											2009-02-01 00:37:13 +00:00
										 |  |  | from .. import abc | 
					
						
							| 
									
										
										
										
											2009-02-01 04:00:05 +00:00
										 |  |  | from .. import util | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | machinery = util.import_importlib('importlib.machinery') | 
					
						
							| 
									
										
										
										
											2013-10-25 12:44:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | import sys | 
					
						
							|  |  |  | import types | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  | @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') | 
					
						
							| 
									
										
										
										
											2013-10-25 12:44:36 -04:00
										 |  |  | class LoaderTests(abc.LoaderTests): | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     """Test load_module() for built-in modules.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-25 12:44:36 -04:00
										 |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.verification = {'__name__': 'errno', '__package__': '', | 
					
						
							|  |  |  |                              '__loader__': self.machinery.BuiltinImporter} | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def verify(self, module): | 
					
						
							|  |  |  |         """Verify that the module matches against what it should have.""" | 
					
						
							| 
									
										
										
										
											2012-06-27 15:26:26 -04:00
										 |  |  |         self.assertIsInstance(module, types.ModuleType) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |         for attr, value in self.verification.items(): | 
					
						
							|  |  |  |             self.assertEqual(getattr(module, attr), value) | 
					
						
							| 
									
										
										
										
											2012-06-27 15:26:26 -04:00
										 |  |  |         self.assertIn(module.__name__, sys.modules) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-25 12:44:36 -04:00
										 |  |  |     def load_module(self, name): | 
					
						
							|  |  |  |         return self.machinery.BuiltinImporter.load_module(name) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-01 00:37:13 +00:00
										 |  |  |     def test_module(self): | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |         # Common case. | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  |         with util.uncache(util.BUILTINS.good_name): | 
					
						
							|  |  |  |             module = self.load_module(util.BUILTINS.good_name) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |             self.verify(module) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |     # Built-in modules cannot be a package. | 
					
						
							|  |  |  |     test_package = test_lacking_parent = None | 
					
						
							| 
									
										
										
										
											2009-02-01 00:37:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |     # No way to force an import failure. | 
					
						
							|  |  |  |     test_state_after_failure = None | 
					
						
							| 
									
										
										
										
											2009-02-01 00:37:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_module_reuse(self): | 
					
						
							|  |  |  |         # Test that the same module is used in a reload. | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  |         with util.uncache(util.BUILTINS.good_name): | 
					
						
							|  |  |  |             module1 = self.load_module(util.BUILTINS.good_name) | 
					
						
							|  |  |  |             module2 = self.load_module(util.BUILTINS.good_name) | 
					
						
							| 
									
										
										
										
											2012-06-27 15:26:26 -04:00
										 |  |  |             self.assertIs(module1, module2) | 
					
						
							| 
									
										
										
										
											2009-02-01 00:37:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_unloadable(self): | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |         name = 'dssdsdfff' | 
					
						
							|  |  |  |         assert name not in sys.builtin_module_names | 
					
						
							| 
									
										
										
										
											2012-04-12 21:09:01 -04:00
										 |  |  |         with self.assertRaises(ImportError) as cm: | 
					
						
							| 
									
										
										
										
											2009-08-27 23:49:21 +00:00
										 |  |  |             self.load_module(name) | 
					
						
							| 
									
										
										
										
											2012-04-12 21:09:01 -04:00
										 |  |  |         self.assertEqual(cm.exception.name, name) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_already_imported(self): | 
					
						
							|  |  |  |         # Using the name of a module already imported but not a built-in should | 
					
						
							|  |  |  |         # still fail. | 
					
						
							| 
									
										
										
										
											2013-11-22 14:38:09 -05:00
										 |  |  |         module_name = 'builtin_reload_test' | 
					
						
							|  |  |  |         assert module_name not in sys.builtin_module_names | 
					
						
							|  |  |  |         with util.uncache(module_name): | 
					
						
							|  |  |  |             module = types.ModuleType(module_name) | 
					
						
							|  |  |  |             sys.modules[module_name] = module | 
					
						
							| 
									
										
										
										
											2012-04-12 21:09:01 -04:00
										 |  |  |         with self.assertRaises(ImportError) as cm: | 
					
						
							| 
									
										
										
										
											2013-11-22 14:38:09 -05:00
										 |  |  |             self.load_module(module_name) | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.name, module_name) | 
					
						
							| 
									
										
										
										
											2013-10-25 12:44:36 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | (Frozen_LoaderTests, | 
					
						
							|  |  |  |  Source_LoaderTests | 
					
						
							|  |  |  |  ) = util.test_both(LoaderTests, machinery=machinery) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  | @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') | 
					
						
							| 
									
										
										
										
											2013-10-25 12:44:36 -04:00
										 |  |  | class InspectLoaderTests: | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     """Tests for InspectLoader methods for BuiltinImporter.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_code(self): | 
					
						
							|  |  |  |         # There is no code object. | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  |         result = self.machinery.BuiltinImporter.get_code(util.BUILTINS.good_name) | 
					
						
							| 
									
										
										
										
											2012-06-28 06:15:01 -04:00
										 |  |  |         self.assertIsNone(result) | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_source(self): | 
					
						
							|  |  |  |         # There is no source. | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  |         result = self.machinery.BuiltinImporter.get_source(util.BUILTINS.good_name) | 
					
						
							| 
									
										
										
										
											2012-06-28 06:15:01 -04:00
										 |  |  |         self.assertIsNone(result) | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_is_package(self): | 
					
						
							|  |  |  |         # Cannot be a package. | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  |         result = self.machinery.BuiltinImporter.is_package(util.BUILTINS.good_name) | 
					
						
							| 
									
										
										
										
											2014-07-07 14:08:19 +03:00
										 |  |  |         self.assertFalse(result) | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  |     @unittest.skipIf(util.BUILTINS.bad_name is None, 'all modules are built in') | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  |     def test_not_builtin(self): | 
					
						
							|  |  |  |         # Modules not built-in should raise ImportError. | 
					
						
							|  |  |  |         for meth_name in ('get_code', 'get_source', 'is_package'): | 
					
						
							| 
									
										
										
										
											2013-10-25 12:44:36 -04:00
										 |  |  |             method = getattr(self.machinery.BuiltinImporter, meth_name) | 
					
						
							| 
									
										
										
										
											2012-04-12 21:09:01 -04:00
										 |  |  |         with self.assertRaises(ImportError) as cm: | 
					
						
							| 
									
										
										
										
											2014-05-09 14:32:57 -04:00
										 |  |  |             method(util.BUILTINS.bad_name) | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | (Frozen_InspectLoaderTests, | 
					
						
							|  |  |  |  Source_InspectLoaderTests | 
					
						
							|  |  |  |  ) = util.test_both(InspectLoaderTests, machinery=machinery) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2013-10-25 12:44:36 -04:00
										 |  |  |     unittest.main() |