Debian Codegraph @

Lib/test/test_importlib/import_/test_path.py

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