| 
									
										
										
										
											2009-02-01 04:00:05 +00:00
										 |  |  | from . import util | 
					
						
							| 
									
										
										
										
											2009-02-07 01:15:27 +00:00
										 |  |  | import imp | 
					
						
							|  |  |  | import importlib | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ImportModuleTests(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Test importlib.import_module.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_module_import(self): | 
					
						
							|  |  |  |         # Test importing a top-level module. | 
					
						
							| 
									
										
										
										
											2009-02-01 04:00:05 +00:00
										 |  |  |         with util.mock_modules('top_level') as mock: | 
					
						
							|  |  |  |             with util.import_state(meta_path=[mock]): | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |                 module = importlib.import_module('top_level') | 
					
						
							|  |  |  |                 self.assertEqual(module.__name__, 'top_level') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_absolute_package_import(self): | 
					
						
							|  |  |  |         # Test importing a module from a package with an absolute name. | 
					
						
							|  |  |  |         pkg_name = 'pkg' | 
					
						
							|  |  |  |         pkg_long_name = '{0}.__init__'.format(pkg_name) | 
					
						
							|  |  |  |         name = '{0}.mod'.format(pkg_name) | 
					
						
							| 
									
										
										
										
											2009-02-01 04:00:05 +00:00
										 |  |  |         with util.mock_modules(pkg_long_name, name) as mock: | 
					
						
							|  |  |  |             with util.import_state(meta_path=[mock]): | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |                 module = importlib.import_module(name) | 
					
						
							|  |  |  |                 self.assertEqual(module.__name__, name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-04 01:02:54 +00:00
										 |  |  |     def test_shallow_relative_package_import(self): | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |         # Test importing a module from a package through a relatve import. | 
					
						
							|  |  |  |         pkg_name = 'pkg' | 
					
						
							|  |  |  |         pkg_long_name = '{0}.__init__'.format(pkg_name) | 
					
						
							|  |  |  |         module_name = 'mod' | 
					
						
							|  |  |  |         absolute_name = '{0}.{1}'.format(pkg_name, module_name) | 
					
						
							|  |  |  |         relative_name = '.{0}'.format(module_name) | 
					
						
							| 
									
										
										
										
											2009-02-01 04:00:05 +00:00
										 |  |  |         with util.mock_modules(pkg_long_name, absolute_name) as mock: | 
					
						
							|  |  |  |             with util.import_state(meta_path=[mock]): | 
					
						
							| 
									
										
										
										
											2009-02-07 01:15:27 +00:00
										 |  |  |                 importlib.import_module(pkg_name) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |                 module = importlib.import_module(relative_name, pkg_name) | 
					
						
							|  |  |  |                 self.assertEqual(module.__name__, absolute_name) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-04 01:02:54 +00:00
										 |  |  |     def test_deep_relative_package_import(self): | 
					
						
							|  |  |  |         modules = ['a.__init__', 'a.b.__init__', 'a.c'] | 
					
						
							|  |  |  |         with util.mock_modules(*modules) as mock: | 
					
						
							|  |  |  |             with util.import_state(meta_path=[mock]): | 
					
						
							|  |  |  |                 importlib.import_module('a') | 
					
						
							|  |  |  |                 importlib.import_module('a.b') | 
					
						
							|  |  |  |                 module = importlib.import_module('..c', 'a.b') | 
					
						
							|  |  |  |                 self.assertEqual(module.__name__, 'a.c') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |     def test_absolute_import_with_package(self): | 
					
						
							|  |  |  |         # Test importing a module from a package with an absolute name with | 
					
						
							|  |  |  |         # the 'package' argument given. | 
					
						
							|  |  |  |         pkg_name = 'pkg' | 
					
						
							|  |  |  |         pkg_long_name = '{0}.__init__'.format(pkg_name) | 
					
						
							|  |  |  |         name = '{0}.mod'.format(pkg_name) | 
					
						
							| 
									
										
										
										
											2009-02-01 04:00:05 +00:00
										 |  |  |         with util.mock_modules(pkg_long_name, name) as mock: | 
					
						
							|  |  |  |             with util.import_state(meta_path=[mock]): | 
					
						
							| 
									
										
										
										
											2009-02-07 01:15:27 +00:00
										 |  |  |                 importlib.import_module(pkg_name) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  |                 module = importlib.import_module(name, pkg_name) | 
					
						
							|  |  |  |                 self.assertEqual(module.__name__, name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_relative_import_wo_package(self): | 
					
						
							|  |  |  |         # Relative imports cannot happen without the 'package' argument being | 
					
						
							|  |  |  |         # set. | 
					
						
							| 
									
										
										
										
											2009-08-27 23:49:21 +00:00
										 |  |  |         with self.assertRaises(TypeError): | 
					
						
							|  |  |  |             importlib.import_module('.support') | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_main(): | 
					
						
							|  |  |  |     from test.support import run_unittest | 
					
						
							|  |  |  |     run_unittest(ImportModuleTests) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     test_main() |