| 
									
										
										
										
											2009-02-15 05:48:13 +00:00
										 |  |  | from importlib import _bootstrap | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | from importlib import machinery | 
					
						
							| 
									
										
										
										
											2009-02-01 04:00:05 +00:00
										 |  |  | from .. import util | 
					
						
							| 
									
										
										
										
											2009-02-01 04:28:04 +00:00
										 |  |  | from . import util as import_util | 
					
						
							| 
									
										
										
										
											2009-02-06 00:07:49 +00:00
										 |  |  | import imp | 
					
						
							| 
									
										
										
										
											2009-02-05 22:02:03 +00:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2010-07-03 21:48:25 +00:00
										 |  |  | import tempfile | 
					
						
							| 
									
										
										
										
											2009-02-06 00:07:49 +00:00
										 |  |  | from test import support | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | from types import MethodType | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | class FinderTests(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-15 05:48:13 +00:00
										 |  |  |     """Tests for PathFinder.""" | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_failure(self): | 
					
						
							|  |  |  |         # Test None returned upon not finding a suitable finder. | 
					
						
							| 
									
										
										
										
											2009-02-15 05:48:13 +00:00
										 |  |  |         module = '<test module>' | 
					
						
							|  |  |  |         with util.import_state(): | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |             self.assertTrue(machinery.PathFinder.find_module(module) is None) | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_sys_path(self): | 
					
						
							|  |  |  |         # Test that sys.path is used when 'path' is None. | 
					
						
							|  |  |  |         # Implicitly tests that sys.path_importer_cache is used. | 
					
						
							|  |  |  |         module = '<test module>' | 
					
						
							|  |  |  |         path = '<test path>' | 
					
						
							|  |  |  |         importer = util.mock_modules(module) | 
					
						
							|  |  |  |         with util.import_state(path_importer_cache={path: importer}, | 
					
						
							|  |  |  |                                path=[path]): | 
					
						
							|  |  |  |             loader = machinery.PathFinder.find_module(module) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |             self.assertTrue(loader is importer) | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_path(self): | 
					
						
							|  |  |  |         # Test that 'path' is used when set. | 
					
						
							|  |  |  |         # Implicitly tests that sys.path_importer_cache is used. | 
					
						
							|  |  |  |         module = '<test module>' | 
					
						
							|  |  |  |         path = '<test path>' | 
					
						
							|  |  |  |         importer = util.mock_modules(module) | 
					
						
							|  |  |  |         with util.import_state(path_importer_cache={path: importer}): | 
					
						
							|  |  |  |             loader = machinery.PathFinder.find_module(module, [path]) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |             self.assertTrue(loader is importer) | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_path_hooks(self): | 
					
						
							|  |  |  |         # Test that sys.path_hooks is used. | 
					
						
							| 
									
										
										
										
											2009-02-05 23:36:02 +00:00
										 |  |  |         # Test that sys.path_importer_cache is set. | 
					
						
							|  |  |  |         module = '<test module>' | 
					
						
							|  |  |  |         path = '<test path>' | 
					
						
							|  |  |  |         importer = util.mock_modules(module) | 
					
						
							|  |  |  |         hook = import_util.mock_path_hook(path, importer=importer) | 
					
						
							|  |  |  |         with util.import_state(path_hooks=[hook]): | 
					
						
							|  |  |  |             loader = machinery.PathFinder.find_module(module, [path]) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |             self.assertTrue(loader is importer) | 
					
						
							|  |  |  |             self.assertTrue(path in sys.path_importer_cache) | 
					
						
							|  |  |  |             self.assertTrue(sys.path_importer_cache[path] is importer) | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-30 19:57:15 +00:00
										 |  |  |     def test_path_importer_cache_has_None(self): | 
					
						
							|  |  |  |         # Test that if sys.path_importer_cache has None that None is returned. | 
					
						
							|  |  |  |         clear_cache = {path: None for path in sys.path} | 
					
						
							|  |  |  |         with util.import_state(path_importer_cache=clear_cache): | 
					
						
							|  |  |  |             for name in ('asynchat', 'sys', '<test module>'): | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |                 self.assertTrue(machinery.PathFinder.find_module(name) is None) | 
					
						
							| 
									
										
										
										
											2009-03-30 19:57:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_path_importer_cache_has_None_continues(self): | 
					
						
							|  |  |  |         # Test that having None in sys.path_importer_cache causes the search to | 
					
						
							|  |  |  |         # continue. | 
					
						
							|  |  |  |         path = '<test path>' | 
					
						
							|  |  |  |         module = '<test module>' | 
					
						
							|  |  |  |         importer = util.mock_modules(module) | 
					
						
							|  |  |  |         with util.import_state(path=['1', '2'], | 
					
						
							|  |  |  |                             path_importer_cache={'1': None, '2': importer}): | 
					
						
							|  |  |  |             loader = machinery.PathFinder.find_module(module) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |             self.assertTrue(loader is importer) | 
					
						
							| 
									
										
										
										
											2009-03-30 19:57:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-15 05:48:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class DefaultPathFinderTests(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     """Test importlib._bootstrap._DefaultPathFinder.""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  |     def test_implicit_hooks(self): | 
					
						
							|  |  |  |         # Test that the implicit path hooks are used. | 
					
						
							| 
									
										
										
										
											2009-02-06 00:07:49 +00:00
										 |  |  |         bad_path = '<path>' | 
					
						
							|  |  |  |         module = '<module>' | 
					
						
							|  |  |  |         assert not os.path.exists(bad_path) | 
					
						
							| 
									
										
										
										
											2010-07-03 21:48:25 +00:00
										 |  |  |         existing_path = tempfile.mkdtemp() | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             with util.import_state(): | 
					
						
							|  |  |  |                 nothing = _bootstrap._DefaultPathFinder.find_module(module, | 
					
						
							|  |  |  |                                                         path=[existing_path]) | 
					
						
							|  |  |  |                 self.assertTrue(nothing is None) | 
					
						
							|  |  |  |                 self.assertTrue(existing_path in sys.path_importer_cache) | 
					
						
							|  |  |  |                 result = isinstance(sys.path_importer_cache[existing_path], | 
					
						
							|  |  |  |                                     imp.NullImporter) | 
					
						
							|  |  |  |                 self.assertFalse(result) | 
					
						
							|  |  |  |                 nothing = _bootstrap._DefaultPathFinder.find_module(module, | 
					
						
							|  |  |  |                                                             path=[bad_path]) | 
					
						
							|  |  |  |                 self.assertTrue(nothing is None) | 
					
						
							|  |  |  |                 self.assertTrue(bad_path in sys.path_importer_cache) | 
					
						
							|  |  |  |                 self.assertTrue(isinstance(sys.path_importer_cache[bad_path], | 
					
						
							|  |  |  |                                            imp.NullImporter)) | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             os.rmdir(existing_path) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-15 05:48:13 +00:00
										 |  |  |     def test_path_importer_cache_has_None(self): | 
					
						
							|  |  |  |         # Test that the default hook is used when sys.path_importer_cache | 
					
						
							|  |  |  |         # contains None for a path. | 
					
						
							|  |  |  |         module = '<test module>' | 
					
						
							|  |  |  |         importer = util.mock_modules(module) | 
					
						
							|  |  |  |         path = '<test path>' | 
					
						
							|  |  |  |         # XXX Not blackbox. | 
					
						
							| 
									
										
										
										
											2009-02-21 03:15:37 +00:00
										 |  |  |         original_hook = _bootstrap._DEFAULT_PATH_HOOK | 
					
						
							| 
									
										
										
										
											2009-02-15 05:48:13 +00:00
										 |  |  |         mock_hook = import_util.mock_path_hook(path, importer=importer) | 
					
						
							| 
									
										
										
										
											2009-02-21 03:15:37 +00:00
										 |  |  |         _bootstrap._DEFAULT_PATH_HOOK = mock_hook | 
					
						
							| 
									
										
										
										
											2009-02-15 05:48:13 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             with util.import_state(path_importer_cache={path: None}): | 
					
						
							|  |  |  |                 loader = _bootstrap._DefaultPathFinder.find_module(module, | 
					
						
							|  |  |  |                                                                     path=[path]) | 
					
						
							| 
									
										
										
										
											2009-06-30 23:06:06 +00:00
										 |  |  |                 self.assertTrue(loader is importer) | 
					
						
							| 
									
										
										
										
											2009-02-15 05:48:13 +00:00
										 |  |  |         finally: | 
					
						
							| 
									
										
										
										
											2009-02-21 03:15:37 +00:00
										 |  |  |             _bootstrap._DEFAULT_PATH_HOOK = original_hook | 
					
						
							| 
									
										
										
										
											2009-02-15 05:48:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-05 02:53:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | def test_main(): | 
					
						
							|  |  |  |     from test.support import run_unittest | 
					
						
							| 
									
										
										
										
											2009-02-15 05:48:13 +00:00
										 |  |  |     run_unittest(FinderTests, DefaultPathFinderTests) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     test_main() |