| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | import importlib | 
					
						
							| 
									
										
										
										
											2009-01-22 22:43:07 +00:00
										 |  |  | from importlib import machinery | 
					
						
							| 
									
										
										
										
											2009-02-01 00:37:13 +00:00
										 |  |  | from .. import abc | 
					
						
							| 
									
										
										
										
											2009-02-01 04:00:05 +00:00
										 |  |  | from .. import util | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  | from . import util as builtin_util | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import types | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-01 00:37:13 +00:00
										 |  |  | class LoaderTests(abc.LoaderTests): | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     """Test load_module() for built-in modules.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-10 05:17:37 +00:00
										 |  |  |     verification = {'__name__': 'errno', '__package__': '', | 
					
						
							|  |  |  |                     '__loader__': machinery.BuiltinImporter} | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def verify(self, module): | 
					
						
							|  |  |  |         """Verify that the module matches against what it should have.""" | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(isinstance(module, types.ModuleType)) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |         for attr, value in self.verification.items(): | 
					
						
							|  |  |  |             self.assertEqual(getattr(module, attr), value) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(module.__name__ in sys.modules) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     load_module = staticmethod(lambda name: | 
					
						
							| 
									
										
										
										
											2009-01-22 22:43:07 +00:00
										 |  |  |                                 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. | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  |         with util.uncache(builtin_util.NAME): | 
					
						
							|  |  |  |             module = self.load_module(builtin_util.NAME) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |             self.verify(module) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-01 00:37:13 +00:00
										 |  |  |     def test_package(self): | 
					
						
							|  |  |  |         # Built-in modules cannot be a package. | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_lacking_parent(self): | 
					
						
							|  |  |  |         # Built-in modules cannot be a package. | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_state_after_failure(self): | 
					
						
							|  |  |  |         # Not way to force an imoprt failure. | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_module_reuse(self): | 
					
						
							|  |  |  |         # Test that the same module is used in a reload. | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  |         with util.uncache(builtin_util.NAME): | 
					
						
							|  |  |  |             module1 = self.load_module(builtin_util.NAME) | 
					
						
							|  |  |  |             module2 = self.load_module(builtin_util.NAME) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |             self.assertTrue(module1 is 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 | 
					
						
							|  |  |  |         self.assertRaises(ImportError, self.load_module, name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_already_imported(self): | 
					
						
							|  |  |  |         # Using the name of a module already imported but not a built-in should | 
					
						
							|  |  |  |         # still fail. | 
					
						
							|  |  |  |         assert hasattr(importlib, '__file__')  # Not a built-in. | 
					
						
							|  |  |  |         self.assertRaises(ImportError, self.load_module, 'importlib') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  | class InspectLoaderTests(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Tests for InspectLoader methods for BuiltinImporter.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_code(self): | 
					
						
							|  |  |  |         # There is no code object. | 
					
						
							|  |  |  |         result = machinery.BuiltinImporter.get_code(builtin_util.NAME) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(result is None) | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_source(self): | 
					
						
							|  |  |  |         # There is no source. | 
					
						
							|  |  |  |         result = machinery.BuiltinImporter.get_source(builtin_util.NAME) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(result is None) | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_is_package(self): | 
					
						
							|  |  |  |         # Cannot be a package. | 
					
						
							|  |  |  |         result = machinery.BuiltinImporter.is_package(builtin_util.NAME) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |         self.assertTrue(not result) | 
					
						
							| 
									
										
										
										
											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'): | 
					
						
							|  |  |  |             method = getattr(machinery.BuiltinImporter, meth_name) | 
					
						
							|  |  |  |         self.assertRaises(ImportError, method, builtin_util.BAD_NAME) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | def test_main(): | 
					
						
							|  |  |  |     from test.support import run_unittest | 
					
						
							| 
									
										
										
										
											2009-03-15 01:41:33 +00:00
										 |  |  |     run_unittest(LoaderTests, InspectLoaderTests) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     test_main() |