1 | n/a | from .. import abc |
---|
2 | n/a | from .. import util |
---|
3 | n/a | |
---|
4 | n/a | machinery = util.import_importlib('importlib.machinery') |
---|
5 | n/a | |
---|
6 | n/a | import errno |
---|
7 | n/a | import os |
---|
8 | n/a | import py_compile |
---|
9 | n/a | import stat |
---|
10 | n/a | import sys |
---|
11 | n/a | import tempfile |
---|
12 | n/a | from test.support import make_legacy_pyc |
---|
13 | n/a | import unittest |
---|
14 | n/a | import warnings |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | class FinderTests(abc.FinderTests): |
---|
18 | n/a | |
---|
19 | n/a | """For a top-level module, it should just be found directly in the |
---|
20 | n/a | directory being searched. This is true for a directory with source |
---|
21 | n/a | [top-level source], bytecode [top-level bc], or both [top-level both]. |
---|
22 | n/a | There is also the possibility that it is a package [top-level package], in |
---|
23 | n/a | which case there will be a directory with the module name and an |
---|
24 | n/a | __init__.py file. If there is a directory without an __init__.py an |
---|
25 | n/a | ImportWarning is returned [empty dir]. |
---|
26 | n/a | |
---|
27 | n/a | For sub-modules and sub-packages, the same happens as above but only use |
---|
28 | n/a | the tail end of the name [sub module] [sub package] [sub empty]. |
---|
29 | n/a | |
---|
30 | n/a | When there is a conflict between a package and module having the same name |
---|
31 | n/a | in the same directory, the package wins out [package over module]. This is |
---|
32 | n/a | so that imports of modules within the package can occur rather than trigger |
---|
33 | n/a | an import error. |
---|
34 | n/a | |
---|
35 | n/a | When there is a package and module with the same name, always pick the |
---|
36 | n/a | package over the module [package over module]. This is so that imports from |
---|
37 | n/a | the package have the possibility of succeeding. |
---|
38 | n/a | |
---|
39 | n/a | """ |
---|
40 | n/a | |
---|
41 | n/a | def get_finder(self, root): |
---|
42 | n/a | loader_details = [(self.machinery.SourceFileLoader, |
---|
43 | n/a | self.machinery.SOURCE_SUFFIXES), |
---|
44 | n/a | (self.machinery.SourcelessFileLoader, |
---|
45 | n/a | self.machinery.BYTECODE_SUFFIXES)] |
---|
46 | n/a | return self.machinery.FileFinder(root, *loader_details) |
---|
47 | n/a | |
---|
48 | n/a | def import_(self, root, module): |
---|
49 | n/a | finder = self.get_finder(root) |
---|
50 | n/a | return self._find(finder, module, loader_only=True) |
---|
51 | n/a | |
---|
52 | n/a | def run_test(self, test, create=None, *, compile_=None, unlink=None): |
---|
53 | n/a | """Test the finding of 'test' with the creation of modules listed in |
---|
54 | n/a | 'create'. |
---|
55 | n/a | |
---|
56 | n/a | Any names listed in 'compile_' are byte-compiled. Modules |
---|
57 | n/a | listed in 'unlink' have their source files deleted. |
---|
58 | n/a | |
---|
59 | n/a | """ |
---|
60 | n/a | if create is None: |
---|
61 | n/a | create = {test} |
---|
62 | n/a | with util.create_modules(*create) as mapping: |
---|
63 | n/a | if compile_: |
---|
64 | n/a | for name in compile_: |
---|
65 | n/a | py_compile.compile(mapping[name]) |
---|
66 | n/a | if unlink: |
---|
67 | n/a | for name in unlink: |
---|
68 | n/a | os.unlink(mapping[name]) |
---|
69 | n/a | try: |
---|
70 | n/a | make_legacy_pyc(mapping[name]) |
---|
71 | n/a | except OSError as error: |
---|
72 | n/a | # Some tests do not set compile_=True so the source |
---|
73 | n/a | # module will not get compiled and there will be no |
---|
74 | n/a | # PEP 3147 pyc file to rename. |
---|
75 | n/a | if error.errno != errno.ENOENT: |
---|
76 | n/a | raise |
---|
77 | n/a | loader = self.import_(mapping['.root'], test) |
---|
78 | n/a | self.assertTrue(hasattr(loader, 'load_module')) |
---|
79 | n/a | return loader |
---|
80 | n/a | |
---|
81 | n/a | def test_module(self): |
---|
82 | n/a | # [top-level source] |
---|
83 | n/a | self.run_test('top_level') |
---|
84 | n/a | # [top-level bc] |
---|
85 | n/a | self.run_test('top_level', compile_={'top_level'}, |
---|
86 | n/a | unlink={'top_level'}) |
---|
87 | n/a | # [top-level both] |
---|
88 | n/a | self.run_test('top_level', compile_={'top_level'}) |
---|
89 | n/a | |
---|
90 | n/a | # [top-level package] |
---|
91 | n/a | def test_package(self): |
---|
92 | n/a | # Source. |
---|
93 | n/a | self.run_test('pkg', {'pkg.__init__'}) |
---|
94 | n/a | # Bytecode. |
---|
95 | n/a | self.run_test('pkg', {'pkg.__init__'}, compile_={'pkg.__init__'}, |
---|
96 | n/a | unlink={'pkg.__init__'}) |
---|
97 | n/a | # Both. |
---|
98 | n/a | self.run_test('pkg', {'pkg.__init__'}, compile_={'pkg.__init__'}) |
---|
99 | n/a | |
---|
100 | n/a | # [sub module] |
---|
101 | n/a | def test_module_in_package(self): |
---|
102 | n/a | with util.create_modules('pkg.__init__', 'pkg.sub') as mapping: |
---|
103 | n/a | pkg_dir = os.path.dirname(mapping['pkg.__init__']) |
---|
104 | n/a | loader = self.import_(pkg_dir, 'pkg.sub') |
---|
105 | n/a | self.assertTrue(hasattr(loader, 'load_module')) |
---|
106 | n/a | |
---|
107 | n/a | # [sub package] |
---|
108 | n/a | def test_package_in_package(self): |
---|
109 | n/a | context = util.create_modules('pkg.__init__', 'pkg.sub.__init__') |
---|
110 | n/a | with context as mapping: |
---|
111 | n/a | pkg_dir = os.path.dirname(mapping['pkg.__init__']) |
---|
112 | n/a | loader = self.import_(pkg_dir, 'pkg.sub') |
---|
113 | n/a | self.assertTrue(hasattr(loader, 'load_module')) |
---|
114 | n/a | |
---|
115 | n/a | # [package over modules] |
---|
116 | n/a | def test_package_over_module(self): |
---|
117 | n/a | name = '_temp' |
---|
118 | n/a | loader = self.run_test(name, {'{0}.__init__'.format(name), name}) |
---|
119 | n/a | self.assertIn('__init__', loader.get_filename(name)) |
---|
120 | n/a | |
---|
121 | n/a | def test_failure(self): |
---|
122 | n/a | with util.create_modules('blah') as mapping: |
---|
123 | n/a | nothing = self.import_(mapping['.root'], 'sdfsadsadf') |
---|
124 | n/a | self.assertIsNone(nothing) |
---|
125 | n/a | |
---|
126 | n/a | def test_empty_string_for_dir(self): |
---|
127 | n/a | # The empty string from sys.path means to search in the cwd. |
---|
128 | n/a | finder = self.machinery.FileFinder('', (self.machinery.SourceFileLoader, |
---|
129 | n/a | self.machinery.SOURCE_SUFFIXES)) |
---|
130 | n/a | with open('mod.py', 'w') as file: |
---|
131 | n/a | file.write("# test file for importlib") |
---|
132 | n/a | try: |
---|
133 | n/a | loader = self._find(finder, 'mod', loader_only=True) |
---|
134 | n/a | self.assertTrue(hasattr(loader, 'load_module')) |
---|
135 | n/a | finally: |
---|
136 | n/a | os.unlink('mod.py') |
---|
137 | n/a | |
---|
138 | n/a | def test_invalidate_caches(self): |
---|
139 | n/a | # invalidate_caches() should reset the mtime. |
---|
140 | n/a | finder = self.machinery.FileFinder('', (self.machinery.SourceFileLoader, |
---|
141 | n/a | self.machinery.SOURCE_SUFFIXES)) |
---|
142 | n/a | finder._path_mtime = 42 |
---|
143 | n/a | finder.invalidate_caches() |
---|
144 | n/a | self.assertEqual(finder._path_mtime, -1) |
---|
145 | n/a | |
---|
146 | n/a | # Regression test for http://bugs.python.org/issue14846 |
---|
147 | n/a | def test_dir_removal_handling(self): |
---|
148 | n/a | mod = 'mod' |
---|
149 | n/a | with util.create_modules(mod) as mapping: |
---|
150 | n/a | finder = self.get_finder(mapping['.root']) |
---|
151 | n/a | found = self._find(finder, 'mod', loader_only=True) |
---|
152 | n/a | self.assertIsNotNone(found) |
---|
153 | n/a | found = self._find(finder, 'mod', loader_only=True) |
---|
154 | n/a | self.assertIsNone(found) |
---|
155 | n/a | |
---|
156 | n/a | @unittest.skipUnless(sys.platform != 'win32', |
---|
157 | n/a | 'os.chmod() does not support the needed arguments under Windows') |
---|
158 | n/a | def test_no_read_directory(self): |
---|
159 | n/a | # Issue #16730 |
---|
160 | n/a | tempdir = tempfile.TemporaryDirectory() |
---|
161 | n/a | original_mode = os.stat(tempdir.name).st_mode |
---|
162 | n/a | def cleanup(tempdir): |
---|
163 | n/a | """Cleanup function for the temporary directory. |
---|
164 | n/a | |
---|
165 | n/a | Since we muck with the permissions, we want to set them back to |
---|
166 | n/a | their original values to make sure the directory can be properly |
---|
167 | n/a | cleaned up. |
---|
168 | n/a | |
---|
169 | n/a | """ |
---|
170 | n/a | os.chmod(tempdir.name, original_mode) |
---|
171 | n/a | # If this is not explicitly called then the __del__ method is used, |
---|
172 | n/a | # but since already mucking around might as well explicitly clean |
---|
173 | n/a | # up. |
---|
174 | n/a | tempdir.__exit__(None, None, None) |
---|
175 | n/a | self.addCleanup(cleanup, tempdir) |
---|
176 | n/a | os.chmod(tempdir.name, stat.S_IWUSR | stat.S_IXUSR) |
---|
177 | n/a | finder = self.get_finder(tempdir.name) |
---|
178 | n/a | found = self._find(finder, 'doesnotexist') |
---|
179 | n/a | self.assertEqual(found, self.NOT_FOUND) |
---|
180 | n/a | |
---|
181 | n/a | def test_ignore_file(self): |
---|
182 | n/a | # If a directory got changed to a file from underneath us, then don't |
---|
183 | n/a | # worry about looking for submodules. |
---|
184 | n/a | with tempfile.NamedTemporaryFile() as file_obj: |
---|
185 | n/a | finder = self.get_finder(file_obj.name) |
---|
186 | n/a | found = self._find(finder, 'doesnotexist') |
---|
187 | n/a | self.assertEqual(found, self.NOT_FOUND) |
---|
188 | n/a | |
---|
189 | n/a | |
---|
190 | n/a | class FinderTestsPEP451(FinderTests): |
---|
191 | n/a | |
---|
192 | n/a | NOT_FOUND = None |
---|
193 | n/a | |
---|
194 | n/a | def _find(self, finder, name, loader_only=False): |
---|
195 | n/a | spec = finder.find_spec(name) |
---|
196 | n/a | return spec.loader if spec is not None else spec |
---|
197 | n/a | |
---|
198 | n/a | |
---|
199 | n/a | (Frozen_FinderTestsPEP451, |
---|
200 | n/a | Source_FinderTestsPEP451 |
---|
201 | n/a | ) = util.test_both(FinderTestsPEP451, machinery=machinery) |
---|
202 | n/a | |
---|
203 | n/a | |
---|
204 | n/a | class FinderTestsPEP420(FinderTests): |
---|
205 | n/a | |
---|
206 | n/a | NOT_FOUND = (None, []) |
---|
207 | n/a | |
---|
208 | n/a | def _find(self, finder, name, loader_only=False): |
---|
209 | n/a | with warnings.catch_warnings(): |
---|
210 | n/a | warnings.simplefilter("ignore", DeprecationWarning) |
---|
211 | n/a | loader_portions = finder.find_loader(name) |
---|
212 | n/a | return loader_portions[0] if loader_only else loader_portions |
---|
213 | n/a | |
---|
214 | n/a | |
---|
215 | n/a | (Frozen_FinderTestsPEP420, |
---|
216 | n/a | Source_FinderTestsPEP420 |
---|
217 | n/a | ) = util.test_both(FinderTestsPEP420, machinery=machinery) |
---|
218 | n/a | |
---|
219 | n/a | |
---|
220 | n/a | class FinderTestsPEP302(FinderTests): |
---|
221 | n/a | |
---|
222 | n/a | NOT_FOUND = None |
---|
223 | n/a | |
---|
224 | n/a | def _find(self, finder, name, loader_only=False): |
---|
225 | n/a | with warnings.catch_warnings(): |
---|
226 | n/a | warnings.simplefilter("ignore", DeprecationWarning) |
---|
227 | n/a | return finder.find_module(name) |
---|
228 | n/a | |
---|
229 | n/a | |
---|
230 | n/a | (Frozen_FinderTestsPEP302, |
---|
231 | n/a | Source_FinderTestsPEP302 |
---|
232 | n/a | ) = util.test_both(FinderTestsPEP302, machinery=machinery) |
---|
233 | n/a | |
---|
234 | n/a | |
---|
235 | n/a | if __name__ == '__main__': |
---|
236 | n/a | unittest.main() |
---|