1 | n/a | from . import util |
---|
2 | n/a | abc = util.import_importlib('importlib.abc') |
---|
3 | n/a | init = util.import_importlib('importlib') |
---|
4 | n/a | machinery = util.import_importlib('importlib.machinery') |
---|
5 | n/a | importlib_util = util.import_importlib('importlib.util') |
---|
6 | n/a | |
---|
7 | n/a | import os |
---|
8 | n/a | import pathlib |
---|
9 | n/a | import string |
---|
10 | n/a | import sys |
---|
11 | n/a | from test import support |
---|
12 | n/a | import types |
---|
13 | n/a | import unittest |
---|
14 | n/a | import warnings |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | class DecodeSourceBytesTests: |
---|
18 | n/a | |
---|
19 | n/a | source = "string ='ü'" |
---|
20 | n/a | |
---|
21 | n/a | def test_ut8_default(self): |
---|
22 | n/a | source_bytes = self.source.encode('utf-8') |
---|
23 | n/a | self.assertEqual(self.util.decode_source(source_bytes), self.source) |
---|
24 | n/a | |
---|
25 | n/a | def test_specified_encoding(self): |
---|
26 | n/a | source = '# coding=latin-1\n' + self.source |
---|
27 | n/a | source_bytes = source.encode('latin-1') |
---|
28 | n/a | assert source_bytes != source.encode('utf-8') |
---|
29 | n/a | self.assertEqual(self.util.decode_source(source_bytes), source) |
---|
30 | n/a | |
---|
31 | n/a | def test_universal_newlines(self): |
---|
32 | n/a | source = '\r\n'.join([self.source, self.source]) |
---|
33 | n/a | source_bytes = source.encode('utf-8') |
---|
34 | n/a | self.assertEqual(self.util.decode_source(source_bytes), |
---|
35 | n/a | '\n'.join([self.source, self.source])) |
---|
36 | n/a | |
---|
37 | n/a | |
---|
38 | n/a | (Frozen_DecodeSourceBytesTests, |
---|
39 | n/a | Source_DecodeSourceBytesTests |
---|
40 | n/a | ) = util.test_both(DecodeSourceBytesTests, util=importlib_util) |
---|
41 | n/a | |
---|
42 | n/a | |
---|
43 | n/a | class ModuleFromSpecTests: |
---|
44 | n/a | |
---|
45 | n/a | def test_no_create_module(self): |
---|
46 | n/a | class Loader: |
---|
47 | n/a | def exec_module(self, module): |
---|
48 | n/a | pass |
---|
49 | n/a | spec = self.machinery.ModuleSpec('test', Loader()) |
---|
50 | n/a | with self.assertRaises(ImportError): |
---|
51 | n/a | module = self.util.module_from_spec(spec) |
---|
52 | n/a | |
---|
53 | n/a | def test_create_module_returns_None(self): |
---|
54 | n/a | class Loader(self.abc.Loader): |
---|
55 | n/a | def create_module(self, spec): |
---|
56 | n/a | return None |
---|
57 | n/a | spec = self.machinery.ModuleSpec('test', Loader()) |
---|
58 | n/a | module = self.util.module_from_spec(spec) |
---|
59 | n/a | self.assertIsInstance(module, types.ModuleType) |
---|
60 | n/a | self.assertEqual(module.__name__, spec.name) |
---|
61 | n/a | |
---|
62 | n/a | def test_create_module(self): |
---|
63 | n/a | name = 'already set' |
---|
64 | n/a | class CustomModule(types.ModuleType): |
---|
65 | n/a | pass |
---|
66 | n/a | class Loader(self.abc.Loader): |
---|
67 | n/a | def create_module(self, spec): |
---|
68 | n/a | module = CustomModule(spec.name) |
---|
69 | n/a | module.__name__ = name |
---|
70 | n/a | return module |
---|
71 | n/a | spec = self.machinery.ModuleSpec('test', Loader()) |
---|
72 | n/a | module = self.util.module_from_spec(spec) |
---|
73 | n/a | self.assertIsInstance(module, CustomModule) |
---|
74 | n/a | self.assertEqual(module.__name__, name) |
---|
75 | n/a | |
---|
76 | n/a | def test___name__(self): |
---|
77 | n/a | spec = self.machinery.ModuleSpec('test', object()) |
---|
78 | n/a | module = self.util.module_from_spec(spec) |
---|
79 | n/a | self.assertEqual(module.__name__, spec.name) |
---|
80 | n/a | |
---|
81 | n/a | def test___spec__(self): |
---|
82 | n/a | spec = self.machinery.ModuleSpec('test', object()) |
---|
83 | n/a | module = self.util.module_from_spec(spec) |
---|
84 | n/a | self.assertEqual(module.__spec__, spec) |
---|
85 | n/a | |
---|
86 | n/a | def test___loader__(self): |
---|
87 | n/a | loader = object() |
---|
88 | n/a | spec = self.machinery.ModuleSpec('test', loader) |
---|
89 | n/a | module = self.util.module_from_spec(spec) |
---|
90 | n/a | self.assertIs(module.__loader__, loader) |
---|
91 | n/a | |
---|
92 | n/a | def test___package__(self): |
---|
93 | n/a | spec = self.machinery.ModuleSpec('test.pkg', object()) |
---|
94 | n/a | module = self.util.module_from_spec(spec) |
---|
95 | n/a | self.assertEqual(module.__package__, spec.parent) |
---|
96 | n/a | |
---|
97 | n/a | def test___path__(self): |
---|
98 | n/a | spec = self.machinery.ModuleSpec('test', object(), is_package=True) |
---|
99 | n/a | module = self.util.module_from_spec(spec) |
---|
100 | n/a | self.assertEqual(module.__path__, spec.submodule_search_locations) |
---|
101 | n/a | |
---|
102 | n/a | def test___file__(self): |
---|
103 | n/a | spec = self.machinery.ModuleSpec('test', object(), origin='some/path') |
---|
104 | n/a | spec.has_location = True |
---|
105 | n/a | module = self.util.module_from_spec(spec) |
---|
106 | n/a | self.assertEqual(module.__file__, spec.origin) |
---|
107 | n/a | |
---|
108 | n/a | def test___cached__(self): |
---|
109 | n/a | spec = self.machinery.ModuleSpec('test', object()) |
---|
110 | n/a | spec.cached = 'some/path' |
---|
111 | n/a | spec.has_location = True |
---|
112 | n/a | module = self.util.module_from_spec(spec) |
---|
113 | n/a | self.assertEqual(module.__cached__, spec.cached) |
---|
114 | n/a | |
---|
115 | n/a | (Frozen_ModuleFromSpecTests, |
---|
116 | n/a | Source_ModuleFromSpecTests |
---|
117 | n/a | ) = util.test_both(ModuleFromSpecTests, abc=abc, machinery=machinery, |
---|
118 | n/a | util=importlib_util) |
---|
119 | n/a | |
---|
120 | n/a | |
---|
121 | n/a | class ModuleForLoaderTests: |
---|
122 | n/a | |
---|
123 | n/a | """Tests for importlib.util.module_for_loader.""" |
---|
124 | n/a | |
---|
125 | n/a | @classmethod |
---|
126 | n/a | def module_for_loader(cls, func): |
---|
127 | n/a | with warnings.catch_warnings(): |
---|
128 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
129 | n/a | return cls.util.module_for_loader(func) |
---|
130 | n/a | |
---|
131 | n/a | def test_warning(self): |
---|
132 | n/a | # Should raise a PendingDeprecationWarning when used. |
---|
133 | n/a | with warnings.catch_warnings(): |
---|
134 | n/a | warnings.simplefilter('error', DeprecationWarning) |
---|
135 | n/a | with self.assertRaises(DeprecationWarning): |
---|
136 | n/a | func = self.util.module_for_loader(lambda x: x) |
---|
137 | n/a | |
---|
138 | n/a | def return_module(self, name): |
---|
139 | n/a | fxn = self.module_for_loader(lambda self, module: module) |
---|
140 | n/a | return fxn(self, name) |
---|
141 | n/a | |
---|
142 | n/a | def raise_exception(self, name): |
---|
143 | n/a | def to_wrap(self, module): |
---|
144 | n/a | raise ImportError |
---|
145 | n/a | fxn = self.module_for_loader(to_wrap) |
---|
146 | n/a | try: |
---|
147 | n/a | fxn(self, name) |
---|
148 | n/a | except ImportError: |
---|
149 | n/a | pass |
---|
150 | n/a | |
---|
151 | n/a | def test_new_module(self): |
---|
152 | n/a | # Test that when no module exists in sys.modules a new module is |
---|
153 | n/a | # created. |
---|
154 | n/a | module_name = 'a.b.c' |
---|
155 | n/a | with util.uncache(module_name): |
---|
156 | n/a | module = self.return_module(module_name) |
---|
157 | n/a | self.assertIn(module_name, sys.modules) |
---|
158 | n/a | self.assertIsInstance(module, types.ModuleType) |
---|
159 | n/a | self.assertEqual(module.__name__, module_name) |
---|
160 | n/a | |
---|
161 | n/a | def test_reload(self): |
---|
162 | n/a | # Test that a module is reused if already in sys.modules. |
---|
163 | n/a | class FakeLoader: |
---|
164 | n/a | def is_package(self, name): |
---|
165 | n/a | return True |
---|
166 | n/a | @self.module_for_loader |
---|
167 | n/a | def load_module(self, module): |
---|
168 | n/a | return module |
---|
169 | n/a | name = 'a.b.c' |
---|
170 | n/a | module = types.ModuleType('a.b.c') |
---|
171 | n/a | module.__loader__ = 42 |
---|
172 | n/a | module.__package__ = 42 |
---|
173 | n/a | with util.uncache(name): |
---|
174 | n/a | sys.modules[name] = module |
---|
175 | n/a | loader = FakeLoader() |
---|
176 | n/a | returned_module = loader.load_module(name) |
---|
177 | n/a | self.assertIs(returned_module, sys.modules[name]) |
---|
178 | n/a | self.assertEqual(module.__loader__, loader) |
---|
179 | n/a | self.assertEqual(module.__package__, name) |
---|
180 | n/a | |
---|
181 | n/a | def test_new_module_failure(self): |
---|
182 | n/a | # Test that a module is removed from sys.modules if added but an |
---|
183 | n/a | # exception is raised. |
---|
184 | n/a | name = 'a.b.c' |
---|
185 | n/a | with util.uncache(name): |
---|
186 | n/a | self.raise_exception(name) |
---|
187 | n/a | self.assertNotIn(name, sys.modules) |
---|
188 | n/a | |
---|
189 | n/a | def test_reload_failure(self): |
---|
190 | n/a | # Test that a failure on reload leaves the module in-place. |
---|
191 | n/a | name = 'a.b.c' |
---|
192 | n/a | module = types.ModuleType(name) |
---|
193 | n/a | with util.uncache(name): |
---|
194 | n/a | sys.modules[name] = module |
---|
195 | n/a | self.raise_exception(name) |
---|
196 | n/a | self.assertIs(module, sys.modules[name]) |
---|
197 | n/a | |
---|
198 | n/a | def test_decorator_attrs(self): |
---|
199 | n/a | def fxn(self, module): pass |
---|
200 | n/a | wrapped = self.module_for_loader(fxn) |
---|
201 | n/a | self.assertEqual(wrapped.__name__, fxn.__name__) |
---|
202 | n/a | self.assertEqual(wrapped.__qualname__, fxn.__qualname__) |
---|
203 | n/a | |
---|
204 | n/a | def test_false_module(self): |
---|
205 | n/a | # If for some odd reason a module is considered false, still return it |
---|
206 | n/a | # from sys.modules. |
---|
207 | n/a | class FalseModule(types.ModuleType): |
---|
208 | n/a | def __bool__(self): return False |
---|
209 | n/a | |
---|
210 | n/a | name = 'mod' |
---|
211 | n/a | module = FalseModule(name) |
---|
212 | n/a | with util.uncache(name): |
---|
213 | n/a | self.assertFalse(module) |
---|
214 | n/a | sys.modules[name] = module |
---|
215 | n/a | given = self.return_module(name) |
---|
216 | n/a | self.assertIs(given, module) |
---|
217 | n/a | |
---|
218 | n/a | def test_attributes_set(self): |
---|
219 | n/a | # __name__, __loader__, and __package__ should be set (when |
---|
220 | n/a | # is_package() is defined; undefined implicitly tested elsewhere). |
---|
221 | n/a | class FakeLoader: |
---|
222 | n/a | def __init__(self, is_package): |
---|
223 | n/a | self._pkg = is_package |
---|
224 | n/a | def is_package(self, name): |
---|
225 | n/a | return self._pkg |
---|
226 | n/a | @self.module_for_loader |
---|
227 | n/a | def load_module(self, module): |
---|
228 | n/a | return module |
---|
229 | n/a | |
---|
230 | n/a | name = 'pkg.mod' |
---|
231 | n/a | with util.uncache(name): |
---|
232 | n/a | loader = FakeLoader(False) |
---|
233 | n/a | module = loader.load_module(name) |
---|
234 | n/a | self.assertEqual(module.__name__, name) |
---|
235 | n/a | self.assertIs(module.__loader__, loader) |
---|
236 | n/a | self.assertEqual(module.__package__, 'pkg') |
---|
237 | n/a | |
---|
238 | n/a | name = 'pkg.sub' |
---|
239 | n/a | with util.uncache(name): |
---|
240 | n/a | loader = FakeLoader(True) |
---|
241 | n/a | module = loader.load_module(name) |
---|
242 | n/a | self.assertEqual(module.__name__, name) |
---|
243 | n/a | self.assertIs(module.__loader__, loader) |
---|
244 | n/a | self.assertEqual(module.__package__, name) |
---|
245 | n/a | |
---|
246 | n/a | |
---|
247 | n/a | (Frozen_ModuleForLoaderTests, |
---|
248 | n/a | Source_ModuleForLoaderTests |
---|
249 | n/a | ) = util.test_both(ModuleForLoaderTests, util=importlib_util) |
---|
250 | n/a | |
---|
251 | n/a | |
---|
252 | n/a | class SetPackageTests: |
---|
253 | n/a | |
---|
254 | n/a | """Tests for importlib.util.set_package.""" |
---|
255 | n/a | |
---|
256 | n/a | def verify(self, module, expect): |
---|
257 | n/a | """Verify the module has the expected value for __package__ after |
---|
258 | n/a | passing through set_package.""" |
---|
259 | n/a | fxn = lambda: module |
---|
260 | n/a | wrapped = self.util.set_package(fxn) |
---|
261 | n/a | with warnings.catch_warnings(): |
---|
262 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
263 | n/a | wrapped() |
---|
264 | n/a | self.assertTrue(hasattr(module, '__package__')) |
---|
265 | n/a | self.assertEqual(expect, module.__package__) |
---|
266 | n/a | |
---|
267 | n/a | def test_top_level(self): |
---|
268 | n/a | # __package__ should be set to the empty string if a top-level module. |
---|
269 | n/a | # Implicitly tests when package is set to None. |
---|
270 | n/a | module = types.ModuleType('module') |
---|
271 | n/a | module.__package__ = None |
---|
272 | n/a | self.verify(module, '') |
---|
273 | n/a | |
---|
274 | n/a | def test_package(self): |
---|
275 | n/a | # Test setting __package__ for a package. |
---|
276 | n/a | module = types.ModuleType('pkg') |
---|
277 | n/a | module.__path__ = ['<path>'] |
---|
278 | n/a | module.__package__ = None |
---|
279 | n/a | self.verify(module, 'pkg') |
---|
280 | n/a | |
---|
281 | n/a | def test_submodule(self): |
---|
282 | n/a | # Test __package__ for a module in a package. |
---|
283 | n/a | module = types.ModuleType('pkg.mod') |
---|
284 | n/a | module.__package__ = None |
---|
285 | n/a | self.verify(module, 'pkg') |
---|
286 | n/a | |
---|
287 | n/a | def test_setting_if_missing(self): |
---|
288 | n/a | # __package__ should be set if it is missing. |
---|
289 | n/a | module = types.ModuleType('mod') |
---|
290 | n/a | if hasattr(module, '__package__'): |
---|
291 | n/a | delattr(module, '__package__') |
---|
292 | n/a | self.verify(module, '') |
---|
293 | n/a | |
---|
294 | n/a | def test_leaving_alone(self): |
---|
295 | n/a | # If __package__ is set and not None then leave it alone. |
---|
296 | n/a | for value in (True, False): |
---|
297 | n/a | module = types.ModuleType('mod') |
---|
298 | n/a | module.__package__ = value |
---|
299 | n/a | self.verify(module, value) |
---|
300 | n/a | |
---|
301 | n/a | def test_decorator_attrs(self): |
---|
302 | n/a | def fxn(module): pass |
---|
303 | n/a | with warnings.catch_warnings(): |
---|
304 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
305 | n/a | wrapped = self.util.set_package(fxn) |
---|
306 | n/a | self.assertEqual(wrapped.__name__, fxn.__name__) |
---|
307 | n/a | self.assertEqual(wrapped.__qualname__, fxn.__qualname__) |
---|
308 | n/a | |
---|
309 | n/a | |
---|
310 | n/a | (Frozen_SetPackageTests, |
---|
311 | n/a | Source_SetPackageTests |
---|
312 | n/a | ) = util.test_both(SetPackageTests, util=importlib_util) |
---|
313 | n/a | |
---|
314 | n/a | |
---|
315 | n/a | class SetLoaderTests: |
---|
316 | n/a | |
---|
317 | n/a | """Tests importlib.util.set_loader().""" |
---|
318 | n/a | |
---|
319 | n/a | @property |
---|
320 | n/a | def DummyLoader(self): |
---|
321 | n/a | # Set DummyLoader on the class lazily. |
---|
322 | n/a | class DummyLoader: |
---|
323 | n/a | @self.util.set_loader |
---|
324 | n/a | def load_module(self, module): |
---|
325 | n/a | return self.module |
---|
326 | n/a | self.__class__.DummyLoader = DummyLoader |
---|
327 | n/a | return DummyLoader |
---|
328 | n/a | |
---|
329 | n/a | def test_no_attribute(self): |
---|
330 | n/a | loader = self.DummyLoader() |
---|
331 | n/a | loader.module = types.ModuleType('blah') |
---|
332 | n/a | try: |
---|
333 | n/a | del loader.module.__loader__ |
---|
334 | n/a | except AttributeError: |
---|
335 | n/a | pass |
---|
336 | n/a | with warnings.catch_warnings(): |
---|
337 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
338 | n/a | self.assertEqual(loader, loader.load_module('blah').__loader__) |
---|
339 | n/a | |
---|
340 | n/a | def test_attribute_is_None(self): |
---|
341 | n/a | loader = self.DummyLoader() |
---|
342 | n/a | loader.module = types.ModuleType('blah') |
---|
343 | n/a | loader.module.__loader__ = None |
---|
344 | n/a | with warnings.catch_warnings(): |
---|
345 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
346 | n/a | self.assertEqual(loader, loader.load_module('blah').__loader__) |
---|
347 | n/a | |
---|
348 | n/a | def test_not_reset(self): |
---|
349 | n/a | loader = self.DummyLoader() |
---|
350 | n/a | loader.module = types.ModuleType('blah') |
---|
351 | n/a | loader.module.__loader__ = 42 |
---|
352 | n/a | with warnings.catch_warnings(): |
---|
353 | n/a | warnings.simplefilter('ignore', DeprecationWarning) |
---|
354 | n/a | self.assertEqual(42, loader.load_module('blah').__loader__) |
---|
355 | n/a | |
---|
356 | n/a | |
---|
357 | n/a | (Frozen_SetLoaderTests, |
---|
358 | n/a | Source_SetLoaderTests |
---|
359 | n/a | ) = util.test_both(SetLoaderTests, util=importlib_util) |
---|
360 | n/a | |
---|
361 | n/a | |
---|
362 | n/a | class ResolveNameTests: |
---|
363 | n/a | |
---|
364 | n/a | """Tests importlib.util.resolve_name().""" |
---|
365 | n/a | |
---|
366 | n/a | def test_absolute(self): |
---|
367 | n/a | # bacon |
---|
368 | n/a | self.assertEqual('bacon', self.util.resolve_name('bacon', None)) |
---|
369 | n/a | |
---|
370 | n/a | def test_absolute_within_package(self): |
---|
371 | n/a | # bacon in spam |
---|
372 | n/a | self.assertEqual('bacon', self.util.resolve_name('bacon', 'spam')) |
---|
373 | n/a | |
---|
374 | n/a | def test_no_package(self): |
---|
375 | n/a | # .bacon in '' |
---|
376 | n/a | with self.assertRaises(ValueError): |
---|
377 | n/a | self.util.resolve_name('.bacon', '') |
---|
378 | n/a | |
---|
379 | n/a | def test_in_package(self): |
---|
380 | n/a | # .bacon in spam |
---|
381 | n/a | self.assertEqual('spam.eggs.bacon', |
---|
382 | n/a | self.util.resolve_name('.bacon', 'spam.eggs')) |
---|
383 | n/a | |
---|
384 | n/a | def test_other_package(self): |
---|
385 | n/a | # ..bacon in spam.bacon |
---|
386 | n/a | self.assertEqual('spam.bacon', |
---|
387 | n/a | self.util.resolve_name('..bacon', 'spam.eggs')) |
---|
388 | n/a | |
---|
389 | n/a | def test_escape(self): |
---|
390 | n/a | # ..bacon in spam |
---|
391 | n/a | with self.assertRaises(ValueError): |
---|
392 | n/a | self.util.resolve_name('..bacon', 'spam') |
---|
393 | n/a | |
---|
394 | n/a | |
---|
395 | n/a | (Frozen_ResolveNameTests, |
---|
396 | n/a | Source_ResolveNameTests |
---|
397 | n/a | ) = util.test_both(ResolveNameTests, util=importlib_util) |
---|
398 | n/a | |
---|
399 | n/a | |
---|
400 | n/a | class FindSpecTests: |
---|
401 | n/a | |
---|
402 | n/a | class FakeMetaFinder: |
---|
403 | n/a | @staticmethod |
---|
404 | n/a | def find_spec(name, path=None, target=None): return name, path, target |
---|
405 | n/a | |
---|
406 | n/a | def test_sys_modules(self): |
---|
407 | n/a | name = 'some_mod' |
---|
408 | n/a | with util.uncache(name): |
---|
409 | n/a | module = types.ModuleType(name) |
---|
410 | n/a | loader = 'a loader!' |
---|
411 | n/a | spec = self.machinery.ModuleSpec(name, loader) |
---|
412 | n/a | module.__loader__ = loader |
---|
413 | n/a | module.__spec__ = spec |
---|
414 | n/a | sys.modules[name] = module |
---|
415 | n/a | found = self.util.find_spec(name) |
---|
416 | n/a | self.assertEqual(found, spec) |
---|
417 | n/a | |
---|
418 | n/a | def test_sys_modules_without___loader__(self): |
---|
419 | n/a | name = 'some_mod' |
---|
420 | n/a | with util.uncache(name): |
---|
421 | n/a | module = types.ModuleType(name) |
---|
422 | n/a | del module.__loader__ |
---|
423 | n/a | loader = 'a loader!' |
---|
424 | n/a | spec = self.machinery.ModuleSpec(name, loader) |
---|
425 | n/a | module.__spec__ = spec |
---|
426 | n/a | sys.modules[name] = module |
---|
427 | n/a | found = self.util.find_spec(name) |
---|
428 | n/a | self.assertEqual(found, spec) |
---|
429 | n/a | |
---|
430 | n/a | def test_sys_modules_spec_is_None(self): |
---|
431 | n/a | name = 'some_mod' |
---|
432 | n/a | with util.uncache(name): |
---|
433 | n/a | module = types.ModuleType(name) |
---|
434 | n/a | module.__spec__ = None |
---|
435 | n/a | sys.modules[name] = module |
---|
436 | n/a | with self.assertRaises(ValueError): |
---|
437 | n/a | self.util.find_spec(name) |
---|
438 | n/a | |
---|
439 | n/a | def test_sys_modules_loader_is_None(self): |
---|
440 | n/a | name = 'some_mod' |
---|
441 | n/a | with util.uncache(name): |
---|
442 | n/a | module = types.ModuleType(name) |
---|
443 | n/a | spec = self.machinery.ModuleSpec(name, None) |
---|
444 | n/a | module.__spec__ = spec |
---|
445 | n/a | sys.modules[name] = module |
---|
446 | n/a | found = self.util.find_spec(name) |
---|
447 | n/a | self.assertEqual(found, spec) |
---|
448 | n/a | |
---|
449 | n/a | def test_sys_modules_spec_is_not_set(self): |
---|
450 | n/a | name = 'some_mod' |
---|
451 | n/a | with util.uncache(name): |
---|
452 | n/a | module = types.ModuleType(name) |
---|
453 | n/a | try: |
---|
454 | n/a | del module.__spec__ |
---|
455 | n/a | except AttributeError: |
---|
456 | n/a | pass |
---|
457 | n/a | sys.modules[name] = module |
---|
458 | n/a | with self.assertRaises(ValueError): |
---|
459 | n/a | self.util.find_spec(name) |
---|
460 | n/a | |
---|
461 | n/a | def test_success(self): |
---|
462 | n/a | name = 'some_mod' |
---|
463 | n/a | with util.uncache(name): |
---|
464 | n/a | with util.import_state(meta_path=[self.FakeMetaFinder]): |
---|
465 | n/a | self.assertEqual((name, None, None), |
---|
466 | n/a | self.util.find_spec(name)) |
---|
467 | n/a | |
---|
468 | n/a | def test_nothing(self): |
---|
469 | n/a | # None is returned upon failure to find a loader. |
---|
470 | n/a | self.assertIsNone(self.util.find_spec('nevergoingtofindthismodule')) |
---|
471 | n/a | |
---|
472 | n/a | def test_find_submodule(self): |
---|
473 | n/a | name = 'spam' |
---|
474 | n/a | subname = 'ham' |
---|
475 | n/a | with util.temp_module(name, pkg=True) as pkg_dir: |
---|
476 | n/a | fullname, _ = util.submodule(name, subname, pkg_dir) |
---|
477 | n/a | spec = self.util.find_spec(fullname) |
---|
478 | n/a | self.assertIsNot(spec, None) |
---|
479 | n/a | self.assertIn(name, sorted(sys.modules)) |
---|
480 | n/a | self.assertNotIn(fullname, sorted(sys.modules)) |
---|
481 | n/a | # Ensure successive calls behave the same. |
---|
482 | n/a | spec_again = self.util.find_spec(fullname) |
---|
483 | n/a | self.assertEqual(spec_again, spec) |
---|
484 | n/a | |
---|
485 | n/a | def test_find_submodule_parent_already_imported(self): |
---|
486 | n/a | name = 'spam' |
---|
487 | n/a | subname = 'ham' |
---|
488 | n/a | with util.temp_module(name, pkg=True) as pkg_dir: |
---|
489 | n/a | self.init.import_module(name) |
---|
490 | n/a | fullname, _ = util.submodule(name, subname, pkg_dir) |
---|
491 | n/a | spec = self.util.find_spec(fullname) |
---|
492 | n/a | self.assertIsNot(spec, None) |
---|
493 | n/a | self.assertIn(name, sorted(sys.modules)) |
---|
494 | n/a | self.assertNotIn(fullname, sorted(sys.modules)) |
---|
495 | n/a | # Ensure successive calls behave the same. |
---|
496 | n/a | spec_again = self.util.find_spec(fullname) |
---|
497 | n/a | self.assertEqual(spec_again, spec) |
---|
498 | n/a | |
---|
499 | n/a | def test_find_relative_module(self): |
---|
500 | n/a | name = 'spam' |
---|
501 | n/a | subname = 'ham' |
---|
502 | n/a | with util.temp_module(name, pkg=True) as pkg_dir: |
---|
503 | n/a | fullname, _ = util.submodule(name, subname, pkg_dir) |
---|
504 | n/a | relname = '.' + subname |
---|
505 | n/a | spec = self.util.find_spec(relname, name) |
---|
506 | n/a | self.assertIsNot(spec, None) |
---|
507 | n/a | self.assertIn(name, sorted(sys.modules)) |
---|
508 | n/a | self.assertNotIn(fullname, sorted(sys.modules)) |
---|
509 | n/a | # Ensure successive calls behave the same. |
---|
510 | n/a | spec_again = self.util.find_spec(fullname) |
---|
511 | n/a | self.assertEqual(spec_again, spec) |
---|
512 | n/a | |
---|
513 | n/a | def test_find_relative_module_missing_package(self): |
---|
514 | n/a | name = 'spam' |
---|
515 | n/a | subname = 'ham' |
---|
516 | n/a | with util.temp_module(name, pkg=True) as pkg_dir: |
---|
517 | n/a | fullname, _ = util.submodule(name, subname, pkg_dir) |
---|
518 | n/a | relname = '.' + subname |
---|
519 | n/a | with self.assertRaises(ValueError): |
---|
520 | n/a | self.util.find_spec(relname) |
---|
521 | n/a | self.assertNotIn(name, sorted(sys.modules)) |
---|
522 | n/a | self.assertNotIn(fullname, sorted(sys.modules)) |
---|
523 | n/a | |
---|
524 | n/a | |
---|
525 | n/a | (Frozen_FindSpecTests, |
---|
526 | n/a | Source_FindSpecTests |
---|
527 | n/a | ) = util.test_both(FindSpecTests, init=init, util=importlib_util, |
---|
528 | n/a | machinery=machinery) |
---|
529 | n/a | |
---|
530 | n/a | |
---|
531 | n/a | class MagicNumberTests: |
---|
532 | n/a | |
---|
533 | n/a | def test_length(self): |
---|
534 | n/a | # Should be 4 bytes. |
---|
535 | n/a | self.assertEqual(len(self.util.MAGIC_NUMBER), 4) |
---|
536 | n/a | |
---|
537 | n/a | def test_incorporates_rn(self): |
---|
538 | n/a | # The magic number uses \r\n to come out wrong when splitting on lines. |
---|
539 | n/a | self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n')) |
---|
540 | n/a | |
---|
541 | n/a | |
---|
542 | n/a | (Frozen_MagicNumberTests, |
---|
543 | n/a | Source_MagicNumberTests |
---|
544 | n/a | ) = util.test_both(MagicNumberTests, util=importlib_util) |
---|
545 | n/a | |
---|
546 | n/a | |
---|
547 | n/a | class PEP3147Tests: |
---|
548 | n/a | |
---|
549 | n/a | """Tests of PEP 3147-related functions: cache_from_source and source_from_cache.""" |
---|
550 | n/a | |
---|
551 | n/a | tag = sys.implementation.cache_tag |
---|
552 | n/a | |
---|
553 | n/a | @unittest.skipUnless(sys.implementation.cache_tag is not None, |
---|
554 | n/a | 'requires sys.implementation.cache_tag not be None') |
---|
555 | n/a | def test_cache_from_source(self): |
---|
556 | n/a | # Given the path to a .py file, return the path to its PEP 3147 |
---|
557 | n/a | # defined .pyc file (i.e. under __pycache__). |
---|
558 | n/a | path = os.path.join('foo', 'bar', 'baz', 'qux.py') |
---|
559 | n/a | expect = os.path.join('foo', 'bar', 'baz', '__pycache__', |
---|
560 | n/a | 'qux.{}.pyc'.format(self.tag)) |
---|
561 | n/a | self.assertEqual(self.util.cache_from_source(path, optimization=''), |
---|
562 | n/a | expect) |
---|
563 | n/a | |
---|
564 | n/a | def test_cache_from_source_no_cache_tag(self): |
---|
565 | n/a | # No cache tag means NotImplementedError. |
---|
566 | n/a | with support.swap_attr(sys.implementation, 'cache_tag', None): |
---|
567 | n/a | with self.assertRaises(NotImplementedError): |
---|
568 | n/a | self.util.cache_from_source('whatever.py') |
---|
569 | n/a | |
---|
570 | n/a | def test_cache_from_source_no_dot(self): |
---|
571 | n/a | # Directory with a dot, filename without dot. |
---|
572 | n/a | path = os.path.join('foo.bar', 'file') |
---|
573 | n/a | expect = os.path.join('foo.bar', '__pycache__', |
---|
574 | n/a | 'file{}.pyc'.format(self.tag)) |
---|
575 | n/a | self.assertEqual(self.util.cache_from_source(path, optimization=''), |
---|
576 | n/a | expect) |
---|
577 | n/a | |
---|
578 | n/a | def test_cache_from_source_debug_override(self): |
---|
579 | n/a | # Given the path to a .py file, return the path to its PEP 3147/PEP 488 |
---|
580 | n/a | # defined .pyc file (i.e. under __pycache__). |
---|
581 | n/a | path = os.path.join('foo', 'bar', 'baz', 'qux.py') |
---|
582 | n/a | with warnings.catch_warnings(): |
---|
583 | n/a | warnings.simplefilter('ignore') |
---|
584 | n/a | self.assertEqual(self.util.cache_from_source(path, False), |
---|
585 | n/a | self.util.cache_from_source(path, optimization=1)) |
---|
586 | n/a | self.assertEqual(self.util.cache_from_source(path, True), |
---|
587 | n/a | self.util.cache_from_source(path, optimization='')) |
---|
588 | n/a | with warnings.catch_warnings(): |
---|
589 | n/a | warnings.simplefilter('error') |
---|
590 | n/a | with self.assertRaises(DeprecationWarning): |
---|
591 | n/a | self.util.cache_from_source(path, False) |
---|
592 | n/a | with self.assertRaises(DeprecationWarning): |
---|
593 | n/a | self.util.cache_from_source(path, True) |
---|
594 | n/a | |
---|
595 | n/a | def test_cache_from_source_cwd(self): |
---|
596 | n/a | path = 'foo.py' |
---|
597 | n/a | expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag)) |
---|
598 | n/a | self.assertEqual(self.util.cache_from_source(path, optimization=''), |
---|
599 | n/a | expect) |
---|
600 | n/a | |
---|
601 | n/a | def test_cache_from_source_override(self): |
---|
602 | n/a | # When debug_override is not None, it can be any true-ish or false-ish |
---|
603 | n/a | # value. |
---|
604 | n/a | path = os.path.join('foo', 'bar', 'baz.py') |
---|
605 | n/a | # However if the bool-ishness can't be determined, the exception |
---|
606 | n/a | # propagates. |
---|
607 | n/a | class Bearish: |
---|
608 | n/a | def __bool__(self): raise RuntimeError |
---|
609 | n/a | with warnings.catch_warnings(): |
---|
610 | n/a | warnings.simplefilter('ignore') |
---|
611 | n/a | self.assertEqual(self.util.cache_from_source(path, []), |
---|
612 | n/a | self.util.cache_from_source(path, optimization=1)) |
---|
613 | n/a | self.assertEqual(self.util.cache_from_source(path, [17]), |
---|
614 | n/a | self.util.cache_from_source(path, optimization='')) |
---|
615 | n/a | with self.assertRaises(RuntimeError): |
---|
616 | n/a | self.util.cache_from_source('/foo/bar/baz.py', Bearish()) |
---|
617 | n/a | |
---|
618 | n/a | |
---|
619 | n/a | def test_cache_from_source_optimization_empty_string(self): |
---|
620 | n/a | # Setting 'optimization' to '' leads to no optimization tag (PEP 488). |
---|
621 | n/a | path = 'foo.py' |
---|
622 | n/a | expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag)) |
---|
623 | n/a | self.assertEqual(self.util.cache_from_source(path, optimization=''), |
---|
624 | n/a | expect) |
---|
625 | n/a | |
---|
626 | n/a | def test_cache_from_source_optimization_None(self): |
---|
627 | n/a | # Setting 'optimization' to None uses the interpreter's optimization. |
---|
628 | n/a | # (PEP 488) |
---|
629 | n/a | path = 'foo.py' |
---|
630 | n/a | optimization_level = sys.flags.optimize |
---|
631 | n/a | almost_expect = os.path.join('__pycache__', 'foo.{}'.format(self.tag)) |
---|
632 | n/a | if optimization_level == 0: |
---|
633 | n/a | expect = almost_expect + '.pyc' |
---|
634 | n/a | elif optimization_level <= 2: |
---|
635 | n/a | expect = almost_expect + '.opt-{}.pyc'.format(optimization_level) |
---|
636 | n/a | else: |
---|
637 | n/a | msg = '{!r} is a non-standard optimization level'.format(optimization_level) |
---|
638 | n/a | self.skipTest(msg) |
---|
639 | n/a | self.assertEqual(self.util.cache_from_source(path, optimization=None), |
---|
640 | n/a | expect) |
---|
641 | n/a | |
---|
642 | n/a | def test_cache_from_source_optimization_set(self): |
---|
643 | n/a | # The 'optimization' parameter accepts anything that has a string repr |
---|
644 | n/a | # that passes str.alnum(). |
---|
645 | n/a | path = 'foo.py' |
---|
646 | n/a | valid_characters = string.ascii_letters + string.digits |
---|
647 | n/a | almost_expect = os.path.join('__pycache__', 'foo.{}'.format(self.tag)) |
---|
648 | n/a | got = self.util.cache_from_source(path, optimization=valid_characters) |
---|
649 | n/a | # Test all valid characters are accepted. |
---|
650 | n/a | self.assertEqual(got, |
---|
651 | n/a | almost_expect + '.opt-{}.pyc'.format(valid_characters)) |
---|
652 | n/a | # str() should be called on argument. |
---|
653 | n/a | self.assertEqual(self.util.cache_from_source(path, optimization=42), |
---|
654 | n/a | almost_expect + '.opt-42.pyc') |
---|
655 | n/a | # Invalid characters raise ValueError. |
---|
656 | n/a | with self.assertRaises(ValueError): |
---|
657 | n/a | self.util.cache_from_source(path, optimization='path/is/bad') |
---|
658 | n/a | |
---|
659 | n/a | def test_cache_from_source_debug_override_optimization_both_set(self): |
---|
660 | n/a | # Can only set one of the optimization-related parameters. |
---|
661 | n/a | with warnings.catch_warnings(): |
---|
662 | n/a | warnings.simplefilter('ignore') |
---|
663 | n/a | with self.assertRaises(TypeError): |
---|
664 | n/a | self.util.cache_from_source('foo.py', False, optimization='') |
---|
665 | n/a | |
---|
666 | n/a | @unittest.skipUnless(os.sep == '\\' and os.altsep == '/', |
---|
667 | n/a | 'test meaningful only where os.altsep is defined') |
---|
668 | n/a | def test_sep_altsep_and_sep_cache_from_source(self): |
---|
669 | n/a | # Windows path and PEP 3147 where sep is right of altsep. |
---|
670 | n/a | self.assertEqual( |
---|
671 | n/a | self.util.cache_from_source('\\foo\\bar\\baz/qux.py', optimization=''), |
---|
672 | n/a | '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag)) |
---|
673 | n/a | |
---|
674 | n/a | @unittest.skipUnless(sys.implementation.cache_tag is not None, |
---|
675 | n/a | 'requires sys.implementation.cache_tag not be None') |
---|
676 | n/a | def test_source_from_cache_path_like_arg(self): |
---|
677 | n/a | path = pathlib.PurePath('foo', 'bar', 'baz', 'qux.py') |
---|
678 | n/a | expect = os.path.join('foo', 'bar', 'baz', '__pycache__', |
---|
679 | n/a | 'qux.{}.pyc'.format(self.tag)) |
---|
680 | n/a | self.assertEqual(self.util.cache_from_source(path, optimization=''), |
---|
681 | n/a | expect) |
---|
682 | n/a | |
---|
683 | n/a | @unittest.skipUnless(sys.implementation.cache_tag is not None, |
---|
684 | n/a | 'requires sys.implementation.cache_tag to not be ' |
---|
685 | n/a | 'None') |
---|
686 | n/a | def test_source_from_cache(self): |
---|
687 | n/a | # Given the path to a PEP 3147 defined .pyc file, return the path to |
---|
688 | n/a | # its source. This tests the good path. |
---|
689 | n/a | path = os.path.join('foo', 'bar', 'baz', '__pycache__', |
---|
690 | n/a | 'qux.{}.pyc'.format(self.tag)) |
---|
691 | n/a | expect = os.path.join('foo', 'bar', 'baz', 'qux.py') |
---|
692 | n/a | self.assertEqual(self.util.source_from_cache(path), expect) |
---|
693 | n/a | |
---|
694 | n/a | def test_source_from_cache_no_cache_tag(self): |
---|
695 | n/a | # If sys.implementation.cache_tag is None, raise NotImplementedError. |
---|
696 | n/a | path = os.path.join('blah', '__pycache__', 'whatever.pyc') |
---|
697 | n/a | with support.swap_attr(sys.implementation, 'cache_tag', None): |
---|
698 | n/a | with self.assertRaises(NotImplementedError): |
---|
699 | n/a | self.util.source_from_cache(path) |
---|
700 | n/a | |
---|
701 | n/a | def test_source_from_cache_bad_path(self): |
---|
702 | n/a | # When the path to a pyc file is not in PEP 3147 format, a ValueError |
---|
703 | n/a | # is raised. |
---|
704 | n/a | self.assertRaises( |
---|
705 | n/a | ValueError, self.util.source_from_cache, '/foo/bar/bazqux.pyc') |
---|
706 | n/a | |
---|
707 | n/a | def test_source_from_cache_no_slash(self): |
---|
708 | n/a | # No slashes at all in path -> ValueError |
---|
709 | n/a | self.assertRaises( |
---|
710 | n/a | ValueError, self.util.source_from_cache, 'foo.cpython-32.pyc') |
---|
711 | n/a | |
---|
712 | n/a | def test_source_from_cache_too_few_dots(self): |
---|
713 | n/a | # Too few dots in final path component -> ValueError |
---|
714 | n/a | self.assertRaises( |
---|
715 | n/a | ValueError, self.util.source_from_cache, '__pycache__/foo.pyc') |
---|
716 | n/a | |
---|
717 | n/a | def test_source_from_cache_too_many_dots(self): |
---|
718 | n/a | with self.assertRaises(ValueError): |
---|
719 | n/a | self.util.source_from_cache( |
---|
720 | n/a | '__pycache__/foo.cpython-32.opt-1.foo.pyc') |
---|
721 | n/a | |
---|
722 | n/a | def test_source_from_cache_not_opt(self): |
---|
723 | n/a | # Non-`opt-` path component -> ValueError |
---|
724 | n/a | self.assertRaises( |
---|
725 | n/a | ValueError, self.util.source_from_cache, |
---|
726 | n/a | '__pycache__/foo.cpython-32.foo.pyc') |
---|
727 | n/a | |
---|
728 | n/a | def test_source_from_cache_no__pycache__(self): |
---|
729 | n/a | # Another problem with the path -> ValueError |
---|
730 | n/a | self.assertRaises( |
---|
731 | n/a | ValueError, self.util.source_from_cache, |
---|
732 | n/a | '/foo/bar/foo.cpython-32.foo.pyc') |
---|
733 | n/a | |
---|
734 | n/a | def test_source_from_cache_optimized_bytecode(self): |
---|
735 | n/a | # Optimized bytecode is not an issue. |
---|
736 | n/a | path = os.path.join('__pycache__', 'foo.{}.opt-1.pyc'.format(self.tag)) |
---|
737 | n/a | self.assertEqual(self.util.source_from_cache(path), 'foo.py') |
---|
738 | n/a | |
---|
739 | n/a | def test_source_from_cache_missing_optimization(self): |
---|
740 | n/a | # An empty optimization level is a no-no. |
---|
741 | n/a | path = os.path.join('__pycache__', 'foo.{}.opt-.pyc'.format(self.tag)) |
---|
742 | n/a | with self.assertRaises(ValueError): |
---|
743 | n/a | self.util.source_from_cache(path) |
---|
744 | n/a | |
---|
745 | n/a | @unittest.skipUnless(sys.implementation.cache_tag is not None, |
---|
746 | n/a | 'requires sys.implementation.cache_tag to not be ' |
---|
747 | n/a | 'None') |
---|
748 | n/a | def test_source_from_cache_path_like_arg(self): |
---|
749 | n/a | path = pathlib.PurePath('foo', 'bar', 'baz', '__pycache__', |
---|
750 | n/a | 'qux.{}.pyc'.format(self.tag)) |
---|
751 | n/a | expect = os.path.join('foo', 'bar', 'baz', 'qux.py') |
---|
752 | n/a | self.assertEqual(self.util.source_from_cache(path), expect) |
---|
753 | n/a | |
---|
754 | n/a | |
---|
755 | n/a | (Frozen_PEP3147Tests, |
---|
756 | n/a | Source_PEP3147Tests |
---|
757 | n/a | ) = util.test_both(PEP3147Tests, util=importlib_util) |
---|
758 | n/a | |
---|
759 | n/a | |
---|
760 | n/a | if __name__ == '__main__': |
---|
761 | n/a | unittest.main() |
---|