| 1 | n/a | from .. import util |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | importlib = util.import_importlib('importlib') |
|---|
| 4 | n/a | machinery = util.import_importlib('importlib.machinery') |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | import os |
|---|
| 7 | n/a | import sys |
|---|
| 8 | n/a | import tempfile |
|---|
| 9 | n/a | from types import ModuleType |
|---|
| 10 | n/a | import unittest |
|---|
| 11 | n/a | import warnings |
|---|
| 12 | n/a | import zipimport |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | class FinderTests: |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | """Tests for PathFinder.""" |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | find = None |
|---|
| 20 | n/a | check_found = None |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | def test_failure(self): |
|---|
| 23 | n/a | # Test None returned upon not finding a suitable loader. |
|---|
| 24 | n/a | module = '<test module>' |
|---|
| 25 | n/a | with util.import_state(): |
|---|
| 26 | n/a | self.assertIsNone(self.find(module)) |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | def test_sys_path(self): |
|---|
| 29 | n/a | # Test that sys.path is used when 'path' is None. |
|---|
| 30 | n/a | # Implicitly tests that sys.path_importer_cache is used. |
|---|
| 31 | n/a | module = '<test module>' |
|---|
| 32 | n/a | path = '<test path>' |
|---|
| 33 | n/a | importer = util.mock_spec(module) |
|---|
| 34 | n/a | with util.import_state(path_importer_cache={path: importer}, |
|---|
| 35 | n/a | path=[path]): |
|---|
| 36 | n/a | found = self.find(module) |
|---|
| 37 | n/a | self.check_found(found, importer) |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | def test_path(self): |
|---|
| 40 | n/a | # Test that 'path' is used when set. |
|---|
| 41 | n/a | # Implicitly tests that sys.path_importer_cache is used. |
|---|
| 42 | n/a | module = '<test module>' |
|---|
| 43 | n/a | path = '<test path>' |
|---|
| 44 | n/a | importer = util.mock_spec(module) |
|---|
| 45 | n/a | with util.import_state(path_importer_cache={path: importer}): |
|---|
| 46 | n/a | found = self.find(module, [path]) |
|---|
| 47 | n/a | self.check_found(found, importer) |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | def test_empty_list(self): |
|---|
| 50 | n/a | # An empty list should not count as asking for sys.path. |
|---|
| 51 | n/a | module = 'module' |
|---|
| 52 | n/a | path = '<test path>' |
|---|
| 53 | n/a | importer = util.mock_spec(module) |
|---|
| 54 | n/a | with util.import_state(path_importer_cache={path: importer}, |
|---|
| 55 | n/a | path=[path]): |
|---|
| 56 | n/a | self.assertIsNone(self.find('module', [])) |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | def test_path_hooks(self): |
|---|
| 59 | n/a | # Test that sys.path_hooks is used. |
|---|
| 60 | n/a | # Test that sys.path_importer_cache is set. |
|---|
| 61 | n/a | module = '<test module>' |
|---|
| 62 | n/a | path = '<test path>' |
|---|
| 63 | n/a | importer = util.mock_spec(module) |
|---|
| 64 | n/a | hook = util.mock_path_hook(path, importer=importer) |
|---|
| 65 | n/a | with util.import_state(path_hooks=[hook]): |
|---|
| 66 | n/a | found = self.find(module, [path]) |
|---|
| 67 | n/a | self.check_found(found, importer) |
|---|
| 68 | n/a | self.assertIn(path, sys.path_importer_cache) |
|---|
| 69 | n/a | self.assertIs(sys.path_importer_cache[path], importer) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def test_empty_path_hooks(self): |
|---|
| 72 | n/a | # Test that if sys.path_hooks is empty a warning is raised, |
|---|
| 73 | n/a | # sys.path_importer_cache gets None set, and PathFinder returns None. |
|---|
| 74 | n/a | path_entry = 'bogus_path' |
|---|
| 75 | n/a | with util.import_state(path_importer_cache={}, path_hooks=[], |
|---|
| 76 | n/a | path=[path_entry]): |
|---|
| 77 | n/a | with warnings.catch_warnings(record=True) as w: |
|---|
| 78 | n/a | warnings.simplefilter('always') |
|---|
| 79 | n/a | self.assertIsNone(self.find('os')) |
|---|
| 80 | n/a | self.assertIsNone(sys.path_importer_cache[path_entry]) |
|---|
| 81 | n/a | self.assertEqual(len(w), 1) |
|---|
| 82 | n/a | self.assertTrue(issubclass(w[-1].category, ImportWarning)) |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | def test_path_importer_cache_empty_string(self): |
|---|
| 85 | n/a | # The empty string should create a finder using the cwd. |
|---|
| 86 | n/a | path = '' |
|---|
| 87 | n/a | module = '<test module>' |
|---|
| 88 | n/a | importer = util.mock_spec(module) |
|---|
| 89 | n/a | hook = util.mock_path_hook(os.getcwd(), importer=importer) |
|---|
| 90 | n/a | with util.import_state(path=[path], path_hooks=[hook]): |
|---|
| 91 | n/a | found = self.find(module) |
|---|
| 92 | n/a | self.check_found(found, importer) |
|---|
| 93 | n/a | self.assertIn(os.getcwd(), sys.path_importer_cache) |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def test_None_on_sys_path(self): |
|---|
| 96 | n/a | # Putting None in sys.path[0] caused an import regression from Python |
|---|
| 97 | n/a | # 3.2: http://bugs.python.org/issue16514 |
|---|
| 98 | n/a | new_path = sys.path[:] |
|---|
| 99 | n/a | new_path.insert(0, None) |
|---|
| 100 | n/a | new_path_importer_cache = sys.path_importer_cache.copy() |
|---|
| 101 | n/a | new_path_importer_cache.pop(None, None) |
|---|
| 102 | n/a | new_path_hooks = [zipimport.zipimporter, |
|---|
| 103 | n/a | self.machinery.FileFinder.path_hook( |
|---|
| 104 | n/a | *self.importlib._bootstrap_external._get_supported_file_loaders())] |
|---|
| 105 | n/a | missing = object() |
|---|
| 106 | n/a | email = sys.modules.pop('email', missing) |
|---|
| 107 | n/a | try: |
|---|
| 108 | n/a | with util.import_state(meta_path=sys.meta_path[:], |
|---|
| 109 | n/a | path=new_path, |
|---|
| 110 | n/a | path_importer_cache=new_path_importer_cache, |
|---|
| 111 | n/a | path_hooks=new_path_hooks): |
|---|
| 112 | n/a | module = self.importlib.import_module('email') |
|---|
| 113 | n/a | self.assertIsInstance(module, ModuleType) |
|---|
| 114 | n/a | finally: |
|---|
| 115 | n/a | if email is not missing: |
|---|
| 116 | n/a | sys.modules['email'] = email |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | def test_finder_with_find_module(self): |
|---|
| 119 | n/a | class TestFinder: |
|---|
| 120 | n/a | def find_module(self, fullname): |
|---|
| 121 | n/a | return self.to_return |
|---|
| 122 | n/a | failing_finder = TestFinder() |
|---|
| 123 | n/a | failing_finder.to_return = None |
|---|
| 124 | n/a | path = 'testing path' |
|---|
| 125 | n/a | with util.import_state(path_importer_cache={path: failing_finder}): |
|---|
| 126 | n/a | self.assertIsNone( |
|---|
| 127 | n/a | self.machinery.PathFinder.find_spec('whatever', [path])) |
|---|
| 128 | n/a | success_finder = TestFinder() |
|---|
| 129 | n/a | success_finder.to_return = __loader__ |
|---|
| 130 | n/a | with util.import_state(path_importer_cache={path: success_finder}): |
|---|
| 131 | n/a | spec = self.machinery.PathFinder.find_spec('whatever', [path]) |
|---|
| 132 | n/a | self.assertEqual(spec.loader, __loader__) |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | def test_finder_with_find_loader(self): |
|---|
| 135 | n/a | class TestFinder: |
|---|
| 136 | n/a | loader = None |
|---|
| 137 | n/a | portions = [] |
|---|
| 138 | n/a | def find_loader(self, fullname): |
|---|
| 139 | n/a | return self.loader, self.portions |
|---|
| 140 | n/a | path = 'testing path' |
|---|
| 141 | n/a | with util.import_state(path_importer_cache={path: TestFinder()}): |
|---|
| 142 | n/a | self.assertIsNone( |
|---|
| 143 | n/a | self.machinery.PathFinder.find_spec('whatever', [path])) |
|---|
| 144 | n/a | success_finder = TestFinder() |
|---|
| 145 | n/a | success_finder.loader = __loader__ |
|---|
| 146 | n/a | with util.import_state(path_importer_cache={path: success_finder}): |
|---|
| 147 | n/a | spec = self.machinery.PathFinder.find_spec('whatever', [path]) |
|---|
| 148 | n/a | self.assertEqual(spec.loader, __loader__) |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | def test_finder_with_find_spec(self): |
|---|
| 151 | n/a | class TestFinder: |
|---|
| 152 | n/a | spec = None |
|---|
| 153 | n/a | def find_spec(self, fullname, target=None): |
|---|
| 154 | n/a | return self.spec |
|---|
| 155 | n/a | path = 'testing path' |
|---|
| 156 | n/a | with util.import_state(path_importer_cache={path: TestFinder()}): |
|---|
| 157 | n/a | self.assertIsNone( |
|---|
| 158 | n/a | self.machinery.PathFinder.find_spec('whatever', [path])) |
|---|
| 159 | n/a | success_finder = TestFinder() |
|---|
| 160 | n/a | success_finder.spec = self.machinery.ModuleSpec('whatever', __loader__) |
|---|
| 161 | n/a | with util.import_state(path_importer_cache={path: success_finder}): |
|---|
| 162 | n/a | got = self.machinery.PathFinder.find_spec('whatever', [path]) |
|---|
| 163 | n/a | self.assertEqual(got, success_finder.spec) |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | def test_deleted_cwd(self): |
|---|
| 166 | n/a | # Issue #22834 |
|---|
| 167 | n/a | old_dir = os.getcwd() |
|---|
| 168 | n/a | self.addCleanup(os.chdir, old_dir) |
|---|
| 169 | n/a | new_dir = tempfile.mkdtemp() |
|---|
| 170 | n/a | try: |
|---|
| 171 | n/a | os.chdir(new_dir) |
|---|
| 172 | n/a | try: |
|---|
| 173 | n/a | os.rmdir(new_dir) |
|---|
| 174 | n/a | except OSError: |
|---|
| 175 | n/a | # EINVAL on Solaris, EBUSY on AIX, ENOTEMPTY on Windows |
|---|
| 176 | n/a | self.skipTest("platform does not allow " |
|---|
| 177 | n/a | "the deletion of the cwd") |
|---|
| 178 | n/a | except: |
|---|
| 179 | n/a | os.chdir(old_dir) |
|---|
| 180 | n/a | os.rmdir(new_dir) |
|---|
| 181 | n/a | raise |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | with util.import_state(path=['']): |
|---|
| 184 | n/a | # Do not want FileNotFoundError raised. |
|---|
| 185 | n/a | self.assertIsNone(self.machinery.PathFinder.find_spec('whatever')) |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | class FindModuleTests(FinderTests): |
|---|
| 189 | n/a | def find(self, *args, **kwargs): |
|---|
| 190 | n/a | return self.machinery.PathFinder.find_module(*args, **kwargs) |
|---|
| 191 | n/a | def check_found(self, found, importer): |
|---|
| 192 | n/a | self.assertIs(found, importer) |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | (Frozen_FindModuleTests, |
|---|
| 196 | n/a | Source_FindModuleTests |
|---|
| 197 | n/a | ) = util.test_both(FindModuleTests, importlib=importlib, machinery=machinery) |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | class FindSpecTests(FinderTests): |
|---|
| 201 | n/a | def find(self, *args, **kwargs): |
|---|
| 202 | n/a | return self.machinery.PathFinder.find_spec(*args, **kwargs) |
|---|
| 203 | n/a | def check_found(self, found, importer): |
|---|
| 204 | n/a | self.assertIs(found.loader, importer) |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | (Frozen_FindSpecTests, |
|---|
| 208 | n/a | Source_FindSpecTests |
|---|
| 209 | n/a | ) = util.test_both(FindSpecTests, importlib=importlib, machinery=machinery) |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | class PathEntryFinderTests: |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | def test_finder_with_failing_find_spec(self): |
|---|
| 215 | n/a | # PathEntryFinder with find_module() defined should work. |
|---|
| 216 | n/a | # Issue #20763. |
|---|
| 217 | n/a | class Finder: |
|---|
| 218 | n/a | path_location = 'test_finder_with_find_module' |
|---|
| 219 | n/a | def __init__(self, path): |
|---|
| 220 | n/a | if path != self.path_location: |
|---|
| 221 | n/a | raise ImportError |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | @staticmethod |
|---|
| 224 | n/a | def find_module(fullname): |
|---|
| 225 | n/a | return None |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | with util.import_state(path=[Finder.path_location]+sys.path[:], |
|---|
| 229 | n/a | path_hooks=[Finder]): |
|---|
| 230 | n/a | self.machinery.PathFinder.find_spec('importlib') |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | def test_finder_with_failing_find_module(self): |
|---|
| 233 | n/a | # PathEntryFinder with find_module() defined should work. |
|---|
| 234 | n/a | # Issue #20763. |
|---|
| 235 | n/a | class Finder: |
|---|
| 236 | n/a | path_location = 'test_finder_with_find_module' |
|---|
| 237 | n/a | def __init__(self, path): |
|---|
| 238 | n/a | if path != self.path_location: |
|---|
| 239 | n/a | raise ImportError |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | @staticmethod |
|---|
| 242 | n/a | def find_module(fullname): |
|---|
| 243 | n/a | return None |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | with util.import_state(path=[Finder.path_location]+sys.path[:], |
|---|
| 247 | n/a | path_hooks=[Finder]): |
|---|
| 248 | n/a | self.machinery.PathFinder.find_module('importlib') |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | (Frozen_PEFTests, |
|---|
| 252 | n/a | Source_PEFTests |
|---|
| 253 | n/a | ) = util.test_both(PathEntryFinderTests, machinery=machinery) |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | if __name__ == '__main__': |
|---|
| 257 | n/a | unittest.main() |
|---|