Debian Codegraph @

Lib/test/test_importlib/test_util.py

raw
1 from test.test_importlib import util
2
3 abc = util.import_importlib('importlib.abc')
4 init = util.import_importlib('importlib')
5 machinery = util.import_importlib('importlib.machinery')
6 importlib_util = util.import_importlib('importlib.util')
7
8 import importlib.util
9 from importlib import _bootstrap_external
10 import os
11 import pathlib
12 import string
13 import sys
14 from test import support
15 from test.support import os_helper
16 import textwrap
17 import types
18 import unittest
19 import unittest.mock
20 import warnings
21
22 try:
23 import _testsinglephase
24 except ImportError:
25 _testsinglephase = None
26 try:
27 import _testmultiphase
28 except ImportError:
29 _testmultiphase = None
30 try:
31 import _interpreters
32 except ModuleNotFoundError:
33 _interpreters = None
34
35
36 class DecodeSourceBytesTests:
37
38 source = "string ='ü'"
39
40 def test_ut8_default(self):
41 source_bytes = self.source.encode('utf-8')
42 self.assertEqual(self.util.decode_source(source_bytes), self.source)
43
44 def test_specified_encoding(self):
45 source = '# coding=latin-1\n' + self.source
46 source_bytes = source.encode('latin-1')
47 assert source_bytes != source.encode('utf-8')
48 self.assertEqual(self.util.decode_source(source_bytes), source)
49
50 def test_universal_newlines(self):
51 source = '\r\n'.join([self.source, self.source])
52 source_bytes = source.encode('utf-8')
53 self.assertEqual(self.util.decode_source(source_bytes),
54 '\n'.join([self.source, self.source]))
55
56
57 (Frozen_DecodeSourceBytesTests,
58 Source_DecodeSourceBytesTests
59 ) = util.test_both(DecodeSourceBytesTests, util=importlib_util)
60
61
62 class ModuleFromSpecTests:
63
64 def test_no_create_module(self):
65 class Loader:
66 def exec_module(self, module):
67 pass
68 spec = self.machinery.ModuleSpec('test', Loader())
69 with self.assertRaises(ImportError):
70 module = self.util.module_from_spec(spec)
71
72 def test_create_module_returns_None(self):
73 class Loader(self.abc.Loader):
74 def create_module(self, spec):
75 return None
76 spec = self.machinery.ModuleSpec('test', Loader())
77 module = self.util.module_from_spec(spec)
78 self.assertIsInstance(module, types.ModuleType)
79 self.assertEqual(module.__name__, spec.name)
80
81 def test_create_module(self):
82 name = 'already set'
83 class CustomModule(types.ModuleType):
84 pass
85 class Loader(self.abc.Loader):
86 def create_module(self, spec):
87 module = CustomModule(spec.name)
88 module.__name__ = name
89 return module
90 spec = self.machinery.ModuleSpec('test', Loader())
91 module = self.util.module_from_spec(spec)
92 self.assertIsInstance(module, CustomModule)
93 self.assertEqual(module.__name__, name)
94
95 def test___name__(self):
96 spec = self.machinery.ModuleSpec('test', object())
97 module = self.util.module_from_spec(spec)
98 self.assertEqual(module.__name__, spec.name)
99
100 def test___spec__(self):
101 spec = self.machinery.ModuleSpec('test', object())
102 module = self.util.module_from_spec(spec)
103 self.assertEqual(module.__spec__, spec)
104
105 def test___loader__(self):
106 loader = object()
107 spec = self.machinery.ModuleSpec('test', loader)
108 module = self.util.module_from_spec(spec)
109 self.assertIs(module.__loader__, loader)
110
111 def test___package__(self):
112 spec = self.machinery.ModuleSpec('test.pkg', object())
113 module = self.util.module_from_spec(spec)
114 self.assertEqual(module.__package__, spec.parent)
115
116 def test___path__(self):
117 spec = self.machinery.ModuleSpec('test', object(), is_package=True)
118 module = self.util.module_from_spec(spec)
119 self.assertEqual(module.__path__, spec.submodule_search_locations)
120
121 def test___file__(self):
122 spec = self.machinery.ModuleSpec('test', object(), origin='some/path')
123 spec.has_location = True
124 module = self.util.module_from_spec(spec)
125 self.assertEqual(module.__file__, spec.origin)
126
127 def test___cached__(self):
128 spec = self.machinery.ModuleSpec('test', object())
129 spec.cached = 'some/path'
130 spec.has_location = True
131 module = self.util.module_from_spec(spec)
132 self.assertEqual(module.__cached__, spec.cached)
133
134 (Frozen_ModuleFromSpecTests,
135 Source_ModuleFromSpecTests
136 ) = util.test_both(ModuleFromSpecTests, abc=abc, machinery=machinery,
137 util=importlib_util)
138
139
140 class ResolveNameTests:
141
142 """Tests importlib.util.resolve_name()."""
143
144 def test_absolute(self):
145 # bacon
146 self.assertEqual('bacon', self.util.resolve_name('bacon', None))
147
148 def test_absolute_within_package(self):
149 # bacon in spam
150 self.assertEqual('bacon', self.util.resolve_name('bacon', 'spam'))
151
152 def test_no_package(self):
153 # .bacon in ''
154 with self.assertRaises(ImportError):
155 self.util.resolve_name('.bacon', '')
156
157 def test_in_package(self):
158 # .bacon in spam
159 self.assertEqual('spam.eggs.bacon',
160 self.util.resolve_name('.bacon', 'spam.eggs'))
161
162 def test_other_package(self):
163 # ..bacon in spam.bacon
164 self.assertEqual('spam.bacon',
165 self.util.resolve_name('..bacon', 'spam.eggs'))
166
167 def test_escape(self):
168 # ..bacon in spam
169 with self.assertRaises(ImportError):
170 self.util.resolve_name('..bacon', 'spam')
171
172
173 (Frozen_ResolveNameTests,
174 Source_ResolveNameTests
175 ) = util.test_both(ResolveNameTests, util=importlib_util)
176
177
178 class FindSpecTests:
179
180 class FakeMetaFinder:
181 @staticmethod
182 def find_spec(name, path=None, target=None): return name, path, target
183
184 def test_sys_modules(self):
185 name = 'some_mod'
186 with util.uncache(name):
187 module = types.ModuleType(name)
188 loader = 'a loader!'
189 spec = self.machinery.ModuleSpec(name, loader)
190 module.__loader__ = loader
191 module.__spec__ = spec
192 sys.modules[name] = module
193 found = self.util.find_spec(name)
194 self.assertEqual(found, spec)
195
196 def test_sys_modules_without___loader__(self):
197 name = 'some_mod'
198 with util.uncache(name):
199 module = types.ModuleType(name)
200 del module.__loader__
201 loader = 'a loader!'
202 spec = self.machinery.ModuleSpec(name, loader)
203 module.__spec__ = spec
204 sys.modules[name] = module
205 found = self.util.find_spec(name)
206 self.assertEqual(found, spec)
207
208 def test_sys_modules_spec_is_None(self):
209 name = 'some_mod'
210 with util.uncache(name):
211 module = types.ModuleType(name)
212 module.__spec__ = None
213 sys.modules[name] = module
214 with self.assertRaises(ValueError):
215 self.util.find_spec(name)
216
217 def test_sys_modules_loader_is_None(self):
218 name = 'some_mod'
219 with util.uncache(name):
220 module = types.ModuleType(name)
221 spec = self.machinery.ModuleSpec(name, None)
222 module.__spec__ = spec
223 sys.modules[name] = module
224 found = self.util.find_spec(name)
225 self.assertEqual(found, spec)
226
227 def test_sys_modules_spec_is_not_set(self):
228 name = 'some_mod'
229 with util.uncache(name):
230 module = types.ModuleType(name)
231 try:
232 del module.__spec__
233 except AttributeError:
234 pass
235 sys.modules[name] = module
236 with self.assertRaises(ValueError):
237 self.util.find_spec(name)
238
239 def test_success(self):
240 name = 'some_mod'
241 with util.uncache(name):
242 with util.import_state(meta_path=[self.FakeMetaFinder]):
243 self.assertEqual((name, None, None),
244 self.util.find_spec(name))
245
246 def test_nothing(self):
247 # None is returned upon failure to find a loader.
248 self.assertIsNone(self.util.find_spec('nevergoingtofindthismodule'))
249
250 def test_find_submodule(self):
251 name = 'spam'
252 subname = 'ham'
253 with util.temp_module(name, pkg=True) as pkg_dir:
254 fullname, _ = util.submodule(name, subname, pkg_dir)
255 spec = self.util.find_spec(fullname)
256 self.assertIsNot(spec, None)
257 self.assertIn(name, sorted(sys.modules))
258 self.assertNotIn(fullname, sorted(sys.modules))
259 # Ensure successive calls behave the same.
260 spec_again = self.util.find_spec(fullname)
261 self.assertEqual(spec_again, spec)
262
263 def test_find_submodule_parent_already_imported(self):
264 name = 'spam'
265 subname = 'ham'
266 with util.temp_module(name, pkg=True) as pkg_dir:
267 self.init.import_module(name)
268 fullname, _ = util.submodule(name, subname, pkg_dir)
269 spec = self.util.find_spec(fullname)
270 self.assertIsNot(spec, None)
271 self.assertIn(name, sorted(sys.modules))
272 self.assertNotIn(fullname, sorted(sys.modules))
273 # Ensure successive calls behave the same.
274 spec_again = self.util.find_spec(fullname)
275 self.assertEqual(spec_again, spec)
276
277 def test_find_relative_module(self):
278 name = 'spam'
279 subname = 'ham'
280 with util.temp_module(name, pkg=True) as pkg_dir:
281 fullname, _ = util.submodule(name, subname, pkg_dir)
282 relname = '.' + subname
283 spec = self.util.find_spec(relname, name)
284 self.assertIsNot(spec, None)
285 self.assertIn(name, sorted(sys.modules))
286 self.assertNotIn(fullname, sorted(sys.modules))
287 # Ensure successive calls behave the same.
288 spec_again = self.util.find_spec(fullname)
289 self.assertEqual(spec_again, spec)
290
291 def test_find_relative_module_missing_package(self):
292 name = 'spam'
293 subname = 'ham'
294 with util.temp_module(name, pkg=True) as pkg_dir:
295 fullname, _ = util.submodule(name, subname, pkg_dir)
296 relname = '.' + subname
297 with self.assertRaises(ImportError):
298 self.util.find_spec(relname)
299 self.assertNotIn(name, sorted(sys.modules))
300 self.assertNotIn(fullname, sorted(sys.modules))
301
302 def test_find_submodule_in_module(self):
303 # ModuleNotFoundError raised when a module is specified as
304 # a parent instead of a package.
305 with self.assertRaises(ModuleNotFoundError):
306 self.util.find_spec('module.name')
307
308
309 (Frozen_FindSpecTests,
310 Source_FindSpecTests
311 ) = util.test_both(FindSpecTests, init=init, util=importlib_util,
312 machinery=machinery)
313
314
315 class MagicNumberTests:
316
317 def test_length(self):
318 # Should be 4 bytes.
319 self.assertEqual(len(self.util.MAGIC_NUMBER), 4)
320
321 def test_incorporates_rn(self):
322 # The magic number uses \r\n to come out wrong when splitting on lines.
323 self.assertEndsWith(self.util.MAGIC_NUMBER, b'\r\n')
324
325
326 (Frozen_MagicNumberTests,
327 Source_MagicNumberTests
328 ) = util.test_both(MagicNumberTests, util=importlib_util)
329
330
331 class PEP3147Tests:
332
333 """Tests of PEP 3147-related functions: cache_from_source and source_from_cache."""
334
335 tag = sys.implementation.cache_tag
336
337 @unittest.skipIf(sys.implementation.cache_tag is None,
338 'requires sys.implementation.cache_tag not be None')
339 def test_cache_from_source(self):
340 # Given the path to a .py file, return the path to its PEP 3147
341 # defined .pyc file (i.e. under __pycache__).
342 path = os.path.join('foo', 'bar', 'baz', 'qux.py')
343 expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
344 'qux.{}.pyc'.format(self.tag))
345 self.assertEqual(self.util.cache_from_source(path, optimization=''),
346 expect)
347
348 def test_cache_from_source_no_cache_tag(self):
349 # No cache tag means NotImplementedError.
350 with support.swap_attr(sys.implementation, 'cache_tag', None):
351 with self.assertRaises(NotImplementedError):
352 self.util.cache_from_source('whatever.py')
353
354 def test_cache_from_source_no_dot(self):
355 # Directory with a dot, filename without dot.
356 path = os.path.join('foo.bar', 'file')
357 expect = os.path.join('foo.bar', '__pycache__',
358 'file{}.pyc'.format(self.tag))
359 self.assertEqual(self.util.cache_from_source(path, optimization=''),
360 expect)
361
362 def test_cache_from_source_debug_override(self):
363 # Given the path to a .py file, return the path to its PEP 3147/PEP 488
364 # defined .pyc file (i.e. under __pycache__).
365 path = os.path.join('foo', 'bar', 'baz', 'qux.py')
366 with warnings.catch_warnings():
367 warnings.simplefilter('ignore')
368 self.assertEqual(self.util.cache_from_source(path, False),
369 self.util.cache_from_source(path, optimization=1))
370 self.assertEqual(self.util.cache_from_source(path, True),
371 self.util.cache_from_source(path, optimization=''))
372 with warnings.catch_warnings():
373 warnings.simplefilter('error')
374 with self.assertRaises(DeprecationWarning):
375 self.util.cache_from_source(path, False)
376 with self.assertRaises(DeprecationWarning):
377 self.util.cache_from_source(path, True)
378
379 def test_cache_from_source_cwd(self):
380 path = 'foo.py'
381 expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
382 self.assertEqual(self.util.cache_from_source(path, optimization=''),
383 expect)
384
385 def test_cache_from_source_override(self):
386 # When debug_override is not None, it can be any true-ish or false-ish
387 # value.
388 path = os.path.join('foo', 'bar', 'baz.py')
389 # However if the bool-ishness can't be determined, the exception
390 # propagates.
391 class Bearish:
392 def __bool__(self): raise RuntimeError
393 with warnings.catch_warnings():
394 warnings.simplefilter('ignore')
395 self.assertEqual(self.util.cache_from_source(path, []),
396 self.util.cache_from_source(path, optimization=1))
397 self.assertEqual(self.util.cache_from_source(path, [17]),
398 self.util.cache_from_source(path, optimization=''))
399 with self.assertRaises(RuntimeError):
400 self.util.cache_from_source('/foo/bar/baz.py', Bearish())
401
402
403 def test_cache_from_source_optimization_empty_string(self):
404 # Setting 'optimization' to '' leads to no optimization tag (PEP 488).
405 path = 'foo.py'
406 expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
407 self.assertEqual(self.util.cache_from_source(path, optimization=''),
408 expect)
409
410 def test_cache_from_source_optimization_None(self):
411 # Setting 'optimization' to None uses the interpreter's optimization.
412 # (PEP 488)
413 path = 'foo.py'
414 optimization_level = sys.flags.optimize
415 almost_expect = os.path.join('__pycache__', 'foo.{}'.format(self.tag))
416 if optimization_level == 0:
417 expect = almost_expect + '.pyc'
418 elif optimization_level <= 2:
419 expect = almost_expect + '.opt-{}.pyc'.format(optimization_level)
420 else:
421 msg = '{!r} is a non-standard optimization level'.format(optimization_level)
422 self.skipTest(msg)
423 self.assertEqual(self.util.cache_from_source(path, optimization=None),
424 expect)
425
426 def test_cache_from_source_optimization_set(self):
427 # The 'optimization' parameter accepts anything that has a string repr
428 # that passes str.alnum().
429 path = 'foo.py'
430 valid_characters = string.ascii_letters + string.digits
431 almost_expect = os.path.join('__pycache__', 'foo.{}'.format(self.tag))
432 got = self.util.cache_from_source(path, optimization=valid_characters)
433 # Test all valid characters are accepted.
434 self.assertEqual(got,
435 almost_expect + '.opt-{}.pyc'.format(valid_characters))
436 # str() should be called on argument.
437 self.assertEqual(self.util.cache_from_source(path, optimization=42),
438 almost_expect + '.opt-42.pyc')
439 # Invalid characters raise ValueError.
440 with self.assertRaises(ValueError):
441 self.util.cache_from_source(path, optimization='path/is/bad')
442
443 def test_cache_from_source_debug_override_optimization_both_set(self):
444 # Can only set one of the optimization-related parameters.
445 with warnings.catch_warnings():
446 warnings.simplefilter('ignore')
447 with self.assertRaises(TypeError):
448 self.util.cache_from_source('foo.py', False, optimization='')
449
450 @unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
451 'test meaningful only where os.altsep is defined')
452 def test_sep_altsep_and_sep_cache_from_source(self):
453 # Windows path and PEP 3147 where sep is right of altsep.
454 self.assertEqual(
455 self.util.cache_from_source('\\foo\\bar\\baz/qux.py', optimization=''),
456 '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
457
458 @unittest.skipIf(sys.implementation.cache_tag is None,
459 'requires sys.implementation.cache_tag not be None')
460 def test_cache_from_source_path_like_arg(self):
461 path = pathlib.PurePath('foo', 'bar', 'baz', 'qux.py')
462 expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
463 'qux.{}.pyc'.format(self.tag))
464 self.assertEqual(self.util.cache_from_source(path, optimization=''),
465 expect)
466
467 @unittest.skipIf(sys.implementation.cache_tag is None,
468 'requires sys.implementation.cache_tag to not be None')
469 def test_source_from_cache(self):
470 # Given the path to a PEP 3147 defined .pyc file, return the path to
471 # its source. This tests the good path.
472 path = os.path.join('foo', 'bar', 'baz', '__pycache__',
473 'qux.{}.pyc'.format(self.tag))
474 expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
475 self.assertEqual(self.util.source_from_cache(path), expect)
476
477 def test_source_from_cache_no_cache_tag(self):
478 # If sys.implementation.cache_tag is None, raise NotImplementedError.
479 path = os.path.join('blah', '__pycache__', 'whatever.pyc')
480 with support.swap_attr(sys.implementation, 'cache_tag', None):
481 with self.assertRaises(NotImplementedError):
482 self.util.source_from_cache(path)
483
484 def test_source_from_cache_bad_path(self):
485 # When the path to a pyc file is not in PEP 3147 format, a ValueError
486 # is raised.
487 self.assertRaises(
488 ValueError, self.util.source_from_cache, '/foo/bar/bazqux.pyc')
489
490 def test_source_from_cache_no_slash(self):
491 # No slashes at all in path -> ValueError
492 self.assertRaises(
493 ValueError, self.util.source_from_cache, 'foo.cpython-32.pyc')
494
495 def test_source_from_cache_too_few_dots(self):
496 # Too few dots in final path component -> ValueError
497 self.assertRaises(
498 ValueError, self.util.source_from_cache, '__pycache__/foo.pyc')
499
500 def test_source_from_cache_too_many_dots(self):
501 with self.assertRaises(ValueError):
502 self.util.source_from_cache(
503 '__pycache__/foo.cpython-32.opt-1.foo.pyc')
504
505 def test_source_from_cache_not_opt(self):
506 # Non-`opt-` path component -> ValueError
507 self.assertRaises(
508 ValueError, self.util.source_from_cache,
509 '__pycache__/foo.cpython-32.foo.pyc')
510
511 def test_source_from_cache_no__pycache__(self):
512 # Another problem with the path -> ValueError
513 self.assertRaises(
514 ValueError, self.util.source_from_cache,
515 '/foo/bar/foo.cpython-32.foo.pyc')
516
517 def test_source_from_cache_optimized_bytecode(self):
518 # Optimized bytecode is not an issue.
519 path = os.path.join('__pycache__', 'foo.{}.opt-1.pyc'.format(self.tag))
520 self.assertEqual(self.util.source_from_cache(path), 'foo.py')
521
522 def test_source_from_cache_missing_optimization(self):
523 # An empty optimization level is a no-no.
524 path = os.path.join('__pycache__', 'foo.{}.opt-.pyc'.format(self.tag))
525 with self.assertRaises(ValueError):
526 self.util.source_from_cache(path)
527
528 @unittest.skipIf(sys.implementation.cache_tag is None,
529 'requires sys.implementation.cache_tag to not be None')
530 def test_source_from_cache_path_like_arg(self):
531 path = pathlib.PurePath('foo', 'bar', 'baz', '__pycache__',
532 'qux.{}.pyc'.format(self.tag))
533 expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
534 self.assertEqual(self.util.source_from_cache(path), expect)
535
536 @unittest.skipIf(sys.implementation.cache_tag is None,
537 'requires sys.implementation.cache_tag to not be None')
538 def test_cache_from_source_respects_pycache_prefix(self):
539 # If pycache_prefix is set, cache_from_source will return a bytecode
540 # path inside that directory (in a subdirectory mirroring the .py file's
541 # path) rather than in a __pycache__ dir next to the py file.
542 pycache_prefixes = [
543 os.path.join(os.path.sep, 'tmp', 'bytecode'),
544 os.path.join(os.path.sep, 'tmp', '\u2603'), # non-ASCII in path!
545 os.path.join(os.path.sep, 'tmp', 'trailing-slash') + os.path.sep,
546 ]
547 drive = ''
548 if os.name == 'nt':
549 drive = 'C:'
550 pycache_prefixes = [
551 f'{drive}{prefix}' for prefix in pycache_prefixes]
552 pycache_prefixes += [r'\\?\C:\foo', r'\\localhost\c$\bar']
553 for pycache_prefix in pycache_prefixes:
554 with self.subTest(path=pycache_prefix):
555 path = drive + os.path.join(
556 os.path.sep, 'foo', 'bar', 'baz', 'qux.py')
557 expect = os.path.join(
558 pycache_prefix, 'foo', 'bar', 'baz',
559 'qux.{}.pyc'.format(self.tag))
560 with util.temporary_pycache_prefix(pycache_prefix):
561 self.assertEqual(
562 self.util.cache_from_source(path, optimization=''),
563 expect)
564
565 @unittest.skipIf(sys.implementation.cache_tag is None,
566 'requires sys.implementation.cache_tag to not be None')
567 def test_cache_from_source_respects_pycache_prefix_relative(self):
568 # If the .py path we are given is relative, we will resolve to an
569 # absolute path before prefixing with pycache_prefix, to avoid any
570 # possible ambiguity.
571 pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
572 path = os.path.join('foo', 'bar', 'baz', 'qux.py')
573 root = os.path.splitdrive(os.getcwd())[0] + os.path.sep
574 expect = os.path.join(
575 pycache_prefix,
576 os.path.relpath(os.getcwd(), root),
577 'foo', 'bar', 'baz', f'qux.{self.tag}.pyc')
578 with util.temporary_pycache_prefix(pycache_prefix):
579 self.assertEqual(
580 self.util.cache_from_source(path, optimization=''),
581 os.path.normpath(expect))
582
583 @unittest.skipIf(sys.implementation.cache_tag is None,
584 'requires sys.implementation.cache_tag to not be None')
585 def test_cache_from_source_in_root_with_pycache_prefix(self):
586 # Regression test for gh-82916
587 pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
588 path = 'qux.py'
589 expect = os.path.join(os.path.sep, 'tmp', 'bytecode',
590 f'qux.{self.tag}.pyc')
591 with util.temporary_pycache_prefix(pycache_prefix):
592 with os_helper.change_cwd('/'):
593 self.assertEqual(self.util.cache_from_source(path), expect)
594
595 @unittest.skipIf(sys.implementation.cache_tag is None,
596 'requires sys.implementation.cache_tag to not be None')
597 def test_source_from_cache_inside_pycache_prefix(self):
598 # If pycache_prefix is set and the cache path we get is inside it,
599 # we return an absolute path to the py file based on the remainder of
600 # the path within pycache_prefix.
601 pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
602 path = os.path.join(pycache_prefix, 'foo', 'bar', 'baz',
603 f'qux.{self.tag}.pyc')
604 expect = os.path.join(os.path.sep, 'foo', 'bar', 'baz', 'qux.py')
605 with util.temporary_pycache_prefix(pycache_prefix):
606 self.assertEqual(self.util.source_from_cache(path), expect)
607
608 @unittest.skipIf(sys.implementation.cache_tag is None,
609 'requires sys.implementation.cache_tag to not be None')
610 def test_source_from_cache_outside_pycache_prefix(self):
611 # If pycache_prefix is set but the cache path we get is not inside
612 # it, just ignore it and handle the cache path according to the default
613 # behavior.
614 pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
615 path = os.path.join('foo', 'bar', 'baz', '__pycache__',
616 f'qux.{self.tag}.pyc')
617 expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
618 with util.temporary_pycache_prefix(pycache_prefix):
619 self.assertEqual(self.util.source_from_cache(path), expect)
620
621
622 (Frozen_PEP3147Tests,
623 Source_PEP3147Tests
624 ) = util.test_both(PEP3147Tests, util=importlib_util)
625
626
627 class MagicNumberTests(unittest.TestCase):
628 """
629 Test release compatibility issues relating to importlib
630 """
631 @unittest.skipUnless(
632 sys.version_info.releaselevel in ('candidate', 'final'),
633 'only applies to candidate or final python release levels'
634 )
635 def test_magic_number(self):
636 # Each python minor release should generally have a MAGIC_NUMBER
637 # that does not change once the release reaches candidate status.
638
639 # Once a release reaches candidate status, the value of the constant
640 # EXPECTED_MAGIC_NUMBER in this test should be changed.
641 # This test will then check that the actual MAGIC_NUMBER matches
642 # the expected value for the release.
643
644 # In exceptional cases, it may be required to change the MAGIC_NUMBER
645 # for a maintenance release. In this case the change should be
646 # discussed in python-dev. If a change is required, community
647 # stakeholders such as OS package maintainers must be notified
648 # in advance. Such exceptional releases will then require an
649 # adjustment to this test case.
650 EXPECTED_MAGIC_NUMBER = 3627
651 actual = int.from_bytes(importlib.util.MAGIC_NUMBER[:2], 'little')
652
653 msg = (
654 "To avoid breaking backwards compatibility with cached bytecode "
655 "files that can't be automatically regenerated by the current "
656 "user, candidate and final releases require the current "
657 "importlib.util.MAGIC_NUMBER to match the expected "
658 "magic number in this test. Set the expected "
659 "magic number in this test to the current MAGIC_NUMBER to "
660 "continue with the release.\n\n"
661 "Changing the MAGIC_NUMBER for a maintenance release "
662 "requires discussion in python-dev and notification of "
663 "community stakeholders."
664 )
665 self.assertEqual(EXPECTED_MAGIC_NUMBER, actual, msg)
666
667
668 @unittest.skipIf(_interpreters is None, 'subinterpreters required')
669 class IncompatibleExtensionModuleRestrictionsTests(unittest.TestCase):
670
671 def run_with_own_gil(self, script):
672 interpid = _interpreters.create('isolated')
673 def ensure_destroyed():
674 try:
675 _interpreters.destroy(interpid)
676 except _interpreters.InterpreterNotFoundError:
677 pass
678 self.addCleanup(ensure_destroyed)
679 excsnap = _interpreters.exec(interpid, script)
680 if excsnap is not None:
681 if excsnap.type.__name__ == 'ImportError':
682 raise ImportError(excsnap.msg)
683
684 def run_with_shared_gil(self, script):
685 interpid = _interpreters.create('legacy')
686 def ensure_destroyed():
687 try:
688 _interpreters.destroy(interpid)
689 except _interpreters.InterpreterNotFoundError:
690 pass
691 self.addCleanup(ensure_destroyed)
692 excsnap = _interpreters.exec(interpid, script)
693 if excsnap is not None:
694 if excsnap.type.__name__ == 'ImportError':
695 raise ImportError(excsnap.msg)
696
697 @unittest.skipIf(_testsinglephase is None, "test requires _testsinglephase module")
698 # gh-117649: single-phase init modules are not currently supported in
699 # subinterpreters in the free-threaded build
700 @support.expected_failure_if_gil_disabled()
701 def test_single_phase_init_module(self):
702 script = textwrap.dedent('''
703 from importlib.util import _incompatible_extension_module_restrictions
704 with _incompatible_extension_module_restrictions(disable_check=True):
705 import _testsinglephase
706 ''')
707 with self.subTest('check disabled, shared GIL'):
708 self.run_with_shared_gil(script)
709 with self.subTest('check disabled, per-interpreter GIL'):
710 self.run_with_own_gil(script)
711
712 script = textwrap.dedent(f'''
713 from importlib.util import _incompatible_extension_module_restrictions
714 with _incompatible_extension_module_restrictions(disable_check=False):
715 import _testsinglephase
716 ''')
717 with self.subTest('check enabled, shared GIL'):
718 with self.assertRaises(ImportError):
719 self.run_with_shared_gil(script)
720 with self.subTest('check enabled, per-interpreter GIL'):
721 with self.assertRaises(ImportError):
722 self.run_with_own_gil(script)
723
724 @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
725 @support.requires_gil_enabled("gh-117649: not supported in free-threaded build")
726 def test_incomplete_multi_phase_init_module(self):
727 # Apple extensions must be distributed as frameworks. This requires
728 # a specialist loader.
729 if support.is_apple_mobile:
730 loader = "AppleFrameworkLoader"
731 else:
732 loader = "ExtensionFileLoader"
733
734 prescript = textwrap.dedent(f'''
735 from importlib.util import spec_from_loader, module_from_spec
736 from importlib.machinery import {loader}
737
738 name = '_test_shared_gil_only'
739 filename = {_testmultiphase.__file__!r}
740 loader = {loader}(name, filename)
741 spec = spec_from_loader(name, loader)
742
743 ''')
744
745 script = prescript + textwrap.dedent('''
746 from importlib.util import _incompatible_extension_module_restrictions
747 with _incompatible_extension_module_restrictions(disable_check=True):
748 module = module_from_spec(spec)
749 loader.exec_module(module)
750 ''')
751 with self.subTest('check disabled, shared GIL'):
752 self.run_with_shared_gil(script)
753 with self.subTest('check disabled, per-interpreter GIL'):
754 self.run_with_own_gil(script)
755
756 script = prescript + textwrap.dedent('''
757 from importlib.util import _incompatible_extension_module_restrictions
758 with _incompatible_extension_module_restrictions(disable_check=False):
759 module = module_from_spec(spec)
760 loader.exec_module(module)
761 ''')
762 with self.subTest('check enabled, shared GIL'):
763 self.run_with_shared_gil(script)
764 with self.subTest('check enabled, per-interpreter GIL'):
765 with self.assertRaises(ImportError):
766 self.run_with_own_gil(script)
767
768 @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
769 def test_complete_multi_phase_init_module(self):
770 script = textwrap.dedent('''
771 from importlib.util import _incompatible_extension_module_restrictions
772 with _incompatible_extension_module_restrictions(disable_check=True):
773 import _testmultiphase
774 ''')
775 with self.subTest('check disabled, shared GIL'):
776 self.run_with_shared_gil(script)
777 with self.subTest('check disabled, per-interpreter GIL'):
778 self.run_with_own_gil(script)
779
780 script = textwrap.dedent(f'''
781 from importlib.util import _incompatible_extension_module_restrictions
782 with _incompatible_extension_module_restrictions(disable_check=False):
783 import _testmultiphase
784 ''')
785 with self.subTest('check enabled, shared GIL'):
786 self.run_with_shared_gil(script)
787 with self.subTest('check enabled, per-interpreter GIL'):
788 self.run_with_own_gil(script)
789
790
791 class PatchAtomicWrites:
792 def __init__(self, truncate_at_length, never_complete=False):
793 self.truncate_at_length = truncate_at_length
794 self.never_complete = never_complete
795 self.seen_write = False
796 self._children = []
797
798 def __enter__(self):
799 import _pyio
800
801 oldwrite = os.write
802
803 # Emulate an os.write that only writes partial data.
804 def write(fd, data):
805 if self.seen_write and self.never_complete:
806 return None
807 self.seen_write = True
808 return oldwrite(fd, data[:self.truncate_at_length])
809
810 # Need to patch _io to be _pyio, so that io.FileIO is affected by the
811 # os.write patch.
812 self.children = [
813 support.swap_attr(_bootstrap_external, '_io', _pyio),
814 support.swap_attr(os, 'write', write)
815 ]
816 for child in self.children:
817 child.__enter__()
818 return self
819
820 def __exit__(self, exc_type, exc_val, exc_tb):
821 for child in self.children:
822 child.__exit__(exc_type, exc_val, exc_tb)
823
824
825 class MiscTests(unittest.TestCase):
826
827 def test_atomic_write_retries_incomplete_writes(self):
828 truncate_at_length = 100
829 length = truncate_at_length * 2
830
831 with PatchAtomicWrites(truncate_at_length=truncate_at_length) as cm:
832 # Make sure we write something longer than the point where we
833 # truncate.
834 content = b'x' * length
835 _bootstrap_external._write_atomic(os_helper.TESTFN, content)
836 self.assertTrue(cm.seen_write)
837
838 self.assertEqual(os.stat(support.os_helper.TESTFN).st_size, length)
839 os.unlink(support.os_helper.TESTFN)
840
841 def test_atomic_write_errors_if_unable_to_complete(self):
842 truncate_at_length = 100
843
844 with (
845 PatchAtomicWrites(
846 truncate_at_length=truncate_at_length, never_complete=True,
847 ) as cm,
848 self.assertRaises(OSError)
849 ):
850 # Make sure we write something longer than the point where we
851 # truncate.
852 content = b'x' * (truncate_at_length * 2)
853 _bootstrap_external._write_atomic(os_helper.TESTFN, content)
854 self.assertTrue(cm.seen_write)
855
856 with self.assertRaises(OSError):
857 os.stat(support.os_helper.TESTFN) # Check that the file did not get written.
858
859
860 if __name__ == '__main__':
861 unittest.main()