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 def setUp(self):
338 # Most of these tests assume the default (unset) pycache prefix, so
339 # clear it for the duration of the test (e.g. when the test suite is
340 # run with PYTHONPYCACHEPREFIX set). Tests that need a specific prefix
341 # set their own via util.temporary_pycache_prefix().
342 self._orig_pycache_prefix = sys.pycache_prefix
343 sys.pycache_prefix = None
344
345 def tearDown(self):
346 sys.pycache_prefix = self._orig_pycache_prefix
347
348 @unittest.skipIf(sys.implementation.cache_tag is None,
349 'requires sys.implementation.cache_tag not be None')
350 def test_cache_from_source(self):
351 # Given the path to a .py file, return the path to its PEP 3147
352 # defined .pyc file (i.e. under __pycache__).
353 path = os.path.join('foo', 'bar', 'baz', 'qux.py')
354 expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
355 'qux.{}.pyc'.format(self.tag))
356 self.assertEqual(self.util.cache_from_source(path, optimization=''),
357 expect)
358
359 def test_cache_from_source_no_cache_tag(self):
360 # No cache tag means NotImplementedError.
361 with support.swap_attr(sys.implementation, 'cache_tag', None):
362 with self.assertRaises(NotImplementedError):
363 self.util.cache_from_source('whatever.py')
364
365 def test_cache_from_source_no_dot(self):
366 # Directory with a dot, filename without dot.
367 path = os.path.join('foo.bar', 'file')
368 expect = os.path.join('foo.bar', '__pycache__',
369 'file{}.pyc'.format(self.tag))
370 self.assertEqual(self.util.cache_from_source(path, optimization=''),
371 expect)
372
373 def test_cache_from_source_debug_override(self):
374 # Given the path to a .py file, return the path to its PEP 3147/PEP 488
375 # defined .pyc file (i.e. under __pycache__).
376 path = os.path.join('foo', 'bar', 'baz', 'qux.py')
377 with warnings.catch_warnings():
378 warnings.simplefilter('ignore')
379 self.assertEqual(self.util.cache_from_source(path, False),
380 self.util.cache_from_source(path, optimization=1))
381 self.assertEqual(self.util.cache_from_source(path, True),
382 self.util.cache_from_source(path, optimization=''))
383 with warnings.catch_warnings():
384 warnings.simplefilter('error')
385 with self.assertRaises(DeprecationWarning):
386 self.util.cache_from_source(path, False)
387 with self.assertRaises(DeprecationWarning):
388 self.util.cache_from_source(path, True)
389
390 def test_cache_from_source_cwd(self):
391 path = 'foo.py'
392 expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
393 self.assertEqual(self.util.cache_from_source(path, optimization=''),
394 expect)
395
396 def test_cache_from_source_override(self):
397 # When debug_override is not None, it can be any true-ish or false-ish
398 # value.
399 path = os.path.join('foo', 'bar', 'baz.py')
400 # However if the bool-ishness can't be determined, the exception
401 # propagates.
402 class Bearish:
403 def __bool__(self): raise RuntimeError
404 with warnings.catch_warnings():
405 warnings.simplefilter('ignore')
406 self.assertEqual(self.util.cache_from_source(path, []),
407 self.util.cache_from_source(path, optimization=1))
408 self.assertEqual(self.util.cache_from_source(path, [17]),
409 self.util.cache_from_source(path, optimization=''))
410 with self.assertRaises(RuntimeError):
411 self.util.cache_from_source('/foo/bar/baz.py', Bearish())
412
413
414 def test_cache_from_source_optimization_empty_string(self):
415 # Setting 'optimization' to '' leads to no optimization tag (PEP 488).
416 path = 'foo.py'
417 expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
418 self.assertEqual(self.util.cache_from_source(path, optimization=''),
419 expect)
420
421 def test_cache_from_source_optimization_None(self):
422 # Setting 'optimization' to None uses the interpreter's optimization.
423 # (PEP 488)
424 path = 'foo.py'
425 optimization_level = sys.flags.optimize
426 almost_expect = os.path.join('__pycache__', 'foo.{}'.format(self.tag))
427 if optimization_level == 0:
428 expect = almost_expect + '.pyc'
429 elif optimization_level <= 2:
430 expect = almost_expect + '.opt-{}.pyc'.format(optimization_level)
431 else:
432 msg = '{!r} is a non-standard optimization level'.format(optimization_level)
433 self.skipTest(msg)
434 self.assertEqual(self.util.cache_from_source(path, optimization=None),
435 expect)
436
437 def test_cache_from_source_optimization_set(self):
438 # The 'optimization' parameter accepts anything that has a string repr
439 # that passes str.alnum().
440 path = 'foo.py'
441 valid_characters = string.ascii_letters + string.digits
442 almost_expect = os.path.join('__pycache__', 'foo.{}'.format(self.tag))
443 got = self.util.cache_from_source(path, optimization=valid_characters)
444 # Test all valid characters are accepted.
445 self.assertEqual(got,
446 almost_expect + '.opt-{}.pyc'.format(valid_characters))
447 # str() should be called on argument.
448 self.assertEqual(self.util.cache_from_source(path, optimization=42),
449 almost_expect + '.opt-42.pyc')
450 # Invalid characters raise ValueError.
451 with self.assertRaises(ValueError):
452 self.util.cache_from_source(path, optimization='path/is/bad')
453
454 def test_cache_from_source_debug_override_optimization_both_set(self):
455 # Can only set one of the optimization-related parameters.
456 with warnings.catch_warnings():
457 warnings.simplefilter('ignore')
458 with self.assertRaises(TypeError):
459 self.util.cache_from_source('foo.py', False, optimization='')
460
461 @unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
462 'test meaningful only where os.altsep is defined')
463 def test_sep_altsep_and_sep_cache_from_source(self):
464 # Windows path and PEP 3147 where sep is right of altsep.
465 self.assertEqual(
466 self.util.cache_from_source('\\foo\\bar\\baz/qux.py', optimization=''),
467 '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
468
469 @unittest.skipIf(sys.implementation.cache_tag is None,
470 'requires sys.implementation.cache_tag not be None')
471 def test_cache_from_source_path_like_arg(self):
472 path = pathlib.PurePath('foo', 'bar', 'baz', 'qux.py')
473 expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
474 'qux.{}.pyc'.format(self.tag))
475 self.assertEqual(self.util.cache_from_source(path, optimization=''),
476 expect)
477
478 @unittest.skipIf(sys.implementation.cache_tag is None,
479 'requires sys.implementation.cache_tag to not be None')
480 def test_source_from_cache(self):
481 # Given the path to a PEP 3147 defined .pyc file, return the path to
482 # its source. This tests the good path.
483 path = os.path.join('foo', 'bar', 'baz', '__pycache__',
484 'qux.{}.pyc'.format(self.tag))
485 expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
486 self.assertEqual(self.util.source_from_cache(path), expect)
487
488 def test_source_from_cache_no_cache_tag(self):
489 # If sys.implementation.cache_tag is None, raise NotImplementedError.
490 path = os.path.join('blah', '__pycache__', 'whatever.pyc')
491 with support.swap_attr(sys.implementation, 'cache_tag', None):
492 with self.assertRaises(NotImplementedError):
493 self.util.source_from_cache(path)
494
495 def test_source_from_cache_bad_path(self):
496 # When the path to a pyc file is not in PEP 3147 format, a ValueError
497 # is raised.
498 self.assertRaises(
499 ValueError, self.util.source_from_cache, '/foo/bar/bazqux.pyc')
500
501 def test_source_from_cache_no_slash(self):
502 # No slashes at all in path -> ValueError
503 self.assertRaises(
504 ValueError, self.util.source_from_cache, 'foo.cpython-32.pyc')
505
506 def test_source_from_cache_too_few_dots(self):
507 # Too few dots in final path component -> ValueError
508 self.assertRaises(
509 ValueError, self.util.source_from_cache, '__pycache__/foo.pyc')
510
511 def test_source_from_cache_too_many_dots(self):
512 with self.assertRaises(ValueError):
513 self.util.source_from_cache(
514 '__pycache__/foo.cpython-32.opt-1.foo.pyc')
515
516 def test_source_from_cache_not_opt(self):
517 # Non-`opt-` path component -> ValueError
518 self.assertRaises(
519 ValueError, self.util.source_from_cache,
520 '__pycache__/foo.cpython-32.foo.pyc')
521
522 def test_source_from_cache_no__pycache__(self):
523 # Another problem with the path -> ValueError
524 self.assertRaises(
525 ValueError, self.util.source_from_cache,
526 '/foo/bar/foo.cpython-32.foo.pyc')
527
528 def test_source_from_cache_optimized_bytecode(self):
529 # Optimized bytecode is not an issue.
530 path = os.path.join('__pycache__', 'foo.{}.opt-1.pyc'.format(self.tag))
531 self.assertEqual(self.util.source_from_cache(path), 'foo.py')
532
533 def test_source_from_cache_missing_optimization(self):
534 # An empty optimization level is a no-no.
535 path = os.path.join('__pycache__', 'foo.{}.opt-.pyc'.format(self.tag))
536 with self.assertRaises(ValueError):
537 self.util.source_from_cache(path)
538
539 @unittest.skipIf(sys.implementation.cache_tag is None,
540 'requires sys.implementation.cache_tag to not be None')
541 def test_source_from_cache_path_like_arg(self):
542 path = pathlib.PurePath('foo', 'bar', 'baz', '__pycache__',
543 'qux.{}.pyc'.format(self.tag))
544 expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
545 self.assertEqual(self.util.source_from_cache(path), expect)
546
547 @unittest.skipIf(sys.implementation.cache_tag is None,
548 'requires sys.implementation.cache_tag to not be None')
549 def test_cache_from_source_respects_pycache_prefix(self):
550 # If pycache_prefix is set, cache_from_source will return a bytecode
551 # path inside that directory (in a subdirectory mirroring the .py file's
552 # path) rather than in a __pycache__ dir next to the py file.
553 pycache_prefixes = [
554 os.path.join(os.path.sep, 'tmp', 'bytecode'),
555 os.path.join(os.path.sep, 'tmp', '\u2603'), # non-ASCII in path!
556 os.path.join(os.path.sep, 'tmp', 'trailing-slash') + os.path.sep,
557 ]
558 drive = ''
559 if os.name == 'nt':
560 drive = 'C:'
561 pycache_prefixes = [
562 f'{drive}{prefix}' for prefix in pycache_prefixes]
563 pycache_prefixes += [r'\\?\C:\foo', r'\\localhost\c$\bar']
564 for pycache_prefix in pycache_prefixes:
565 with self.subTest(path=pycache_prefix):
566 path = drive + os.path.join(
567 os.path.sep, 'foo', 'bar', 'baz', 'qux.py')
568 expect = os.path.join(
569 pycache_prefix, 'foo', 'bar', 'baz',
570 'qux.{}.pyc'.format(self.tag))
571 with util.temporary_pycache_prefix(pycache_prefix):
572 self.assertEqual(
573 self.util.cache_from_source(path, optimization=''),
574 expect)
575
576 @unittest.skipIf(sys.implementation.cache_tag is None,
577 'requires sys.implementation.cache_tag to not be None')
578 def test_cache_from_source_respects_pycache_prefix_relative(self):
579 # If the .py path we are given is relative, we will resolve to an
580 # absolute path before prefixing with pycache_prefix, to avoid any
581 # possible ambiguity.
582 pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
583 path = os.path.join('foo', 'bar', 'baz', 'qux.py')
584 root = os.path.splitdrive(os.getcwd())[0] + os.path.sep
585 expect = os.path.join(
586 pycache_prefix,
587 os.path.relpath(os.getcwd(), root),
588 'foo', 'bar', 'baz', f'qux.{self.tag}.pyc')
589 with util.temporary_pycache_prefix(pycache_prefix):
590 self.assertEqual(
591 self.util.cache_from_source(path, optimization=''),
592 os.path.normpath(expect))
593
594 @unittest.skipIf(sys.implementation.cache_tag is None,
595 'requires sys.implementation.cache_tag to not be None')
596 def test_cache_from_source_in_root_with_pycache_prefix(self):
597 # Regression test for gh-82916
598 pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
599 path = 'qux.py'
600 expect = os.path.join(os.path.sep, 'tmp', 'bytecode',
601 f'qux.{self.tag}.pyc')
602 with util.temporary_pycache_prefix(pycache_prefix):
603 with os_helper.change_cwd('/'):
604 self.assertEqual(self.util.cache_from_source(path), expect)
605
606 @unittest.skipIf(sys.implementation.cache_tag is None,
607 'requires sys.implementation.cache_tag to not be None')
608 def test_source_from_cache_inside_pycache_prefix(self):
609 # If pycache_prefix is set and the cache path we get is inside it,
610 # we return an absolute path to the py file based on the remainder of
611 # the path within pycache_prefix.
612 pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
613 path = os.path.join(pycache_prefix, 'foo', 'bar', 'baz',
614 f'qux.{self.tag}.pyc')
615 expect = os.path.join(os.path.sep, 'foo', 'bar', 'baz', 'qux.py')
616 with util.temporary_pycache_prefix(pycache_prefix):
617 self.assertEqual(self.util.source_from_cache(path), expect)
618
619 @unittest.skipIf(sys.implementation.cache_tag is None,
620 'requires sys.implementation.cache_tag to not be None')
621 def test_source_from_cache_outside_pycache_prefix(self):
622 # If pycache_prefix is set but the cache path we get is not inside
623 # it, just ignore it and handle the cache path according to the default
624 # behavior.
625 pycache_prefix = os.path.join(os.path.sep, 'tmp', 'bytecode')
626 path = os.path.join('foo', 'bar', 'baz', '__pycache__',
627 f'qux.{self.tag}.pyc')
628 expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
629 with util.temporary_pycache_prefix(pycache_prefix):
630 self.assertEqual(self.util.source_from_cache(path), expect)
631
632
633 (Frozen_PEP3147Tests,
634 Source_PEP3147Tests
635 ) = util.test_both(PEP3147Tests, util=importlib_util)
636
637
638 class MagicNumberTests(unittest.TestCase):
639 """
640 Test release compatibility issues relating to importlib
641 """
642 @unittest.skipUnless(
643 sys.version_info.releaselevel in ('candidate', 'final'),
644 'only applies to candidate or final python release levels'
645 )
646 def test_magic_number(self):
647 # Each python minor release should generally have a MAGIC_NUMBER
648 # that does not change once the release reaches candidate status.
649
650 # Once a release reaches candidate status, the value of the constant
651 # EXPECTED_MAGIC_NUMBER in this test should be changed.
652 # This test will then check that the actual MAGIC_NUMBER matches
653 # the expected value for the release.
654
655 # In exceptional cases, it may be required to change the MAGIC_NUMBER
656 # for a maintenance release. In this case the change should be
657 # discussed in python-dev. If a change is required, community
658 # stakeholders such as OS package maintainers must be notified
659 # in advance. Such exceptional releases will then require an
660 # adjustment to this test case.
661 EXPECTED_MAGIC_NUMBER = 3627
662 actual = int.from_bytes(importlib.util.MAGIC_NUMBER[:2], 'little')
663
664 msg = (
665 "To avoid breaking backwards compatibility with cached bytecode "
666 "files that can't be automatically regenerated by the current "
667 "user, candidate and final releases require the current "
668 "importlib.util.MAGIC_NUMBER to match the expected "
669 "magic number in this test. Set the expected "
670 "magic number in this test to the current MAGIC_NUMBER to "
671 "continue with the release.\n\n"
672 "Changing the MAGIC_NUMBER for a maintenance release "
673 "requires discussion in python-dev and notification of "
674 "community stakeholders."
675 )
676 self.assertEqual(EXPECTED_MAGIC_NUMBER, actual, msg)
677
678
679 @unittest.skipIf(_interpreters is None, 'subinterpreters required')
680 class IncompatibleExtensionModuleRestrictionsTests(unittest.TestCase):
681
682 def run_with_own_gil(self, script):
683 interpid = _interpreters.create('isolated')
684 def ensure_destroyed():
685 try:
686 _interpreters.destroy(interpid)
687 except _interpreters.InterpreterNotFoundError:
688 pass
689 self.addCleanup(ensure_destroyed)
690 excsnap = _interpreters.exec(interpid, script)
691 if excsnap is not None:
692 if excsnap.type.__name__ == 'ImportError':
693 raise ImportError(excsnap.msg)
694
695 def run_with_shared_gil(self, script):
696 interpid = _interpreters.create('legacy')
697 def ensure_destroyed():
698 try:
699 _interpreters.destroy(interpid)
700 except _interpreters.InterpreterNotFoundError:
701 pass
702 self.addCleanup(ensure_destroyed)
703 excsnap = _interpreters.exec(interpid, script)
704 if excsnap is not None:
705 if excsnap.type.__name__ == 'ImportError':
706 raise ImportError(excsnap.msg)
707
708 @unittest.skipIf(_testsinglephase is None, "test requires _testsinglephase module")
709 # gh-117649: single-phase init modules are not currently supported in
710 # subinterpreters in the free-threaded build
711 @support.expected_failure_if_gil_disabled()
712 def test_single_phase_init_module(self):
713 script = textwrap.dedent('''
714 from importlib.util import _incompatible_extension_module_restrictions
715 with _incompatible_extension_module_restrictions(disable_check=True):
716 import _testsinglephase
717 ''')
718 with self.subTest('check disabled, shared GIL'):
719 self.run_with_shared_gil(script)
720 with self.subTest('check disabled, per-interpreter GIL'):
721 self.run_with_own_gil(script)
722
723 script = textwrap.dedent(f'''
724 from importlib.util import _incompatible_extension_module_restrictions
725 with _incompatible_extension_module_restrictions(disable_check=False):
726 import _testsinglephase
727 ''')
728 with self.subTest('check enabled, shared GIL'):
729 with self.assertRaises(ImportError):
730 self.run_with_shared_gil(script)
731 with self.subTest('check enabled, per-interpreter GIL'):
732 with self.assertRaises(ImportError):
733 self.run_with_own_gil(script)
734
735 @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
736 @support.requires_gil_enabled("gh-117649: not supported in free-threaded build")
737 def test_incomplete_multi_phase_init_module(self):
738 # Apple extensions must be distributed as frameworks. This requires
739 # a specialist loader.
740 if support.is_apple_mobile:
741 loader = "AppleFrameworkLoader"
742 else:
743 loader = "ExtensionFileLoader"
744
745 prescript = textwrap.dedent(f'''
746 from importlib.util import spec_from_loader, module_from_spec
747 from importlib.machinery import {loader}
748
749 name = '_test_shared_gil_only'
750 filename = {_testmultiphase.__file__!r}
751 loader = {loader}(name, filename)
752 spec = spec_from_loader(name, loader)
753
754 ''')
755
756 script = prescript + textwrap.dedent('''
757 from importlib.util import _incompatible_extension_module_restrictions
758 with _incompatible_extension_module_restrictions(disable_check=True):
759 module = module_from_spec(spec)
760 loader.exec_module(module)
761 ''')
762 with self.subTest('check disabled, shared GIL'):
763 self.run_with_shared_gil(script)
764 with self.subTest('check disabled, per-interpreter GIL'):
765 self.run_with_own_gil(script)
766
767 script = prescript + textwrap.dedent('''
768 from importlib.util import _incompatible_extension_module_restrictions
769 with _incompatible_extension_module_restrictions(disable_check=False):
770 module = module_from_spec(spec)
771 loader.exec_module(module)
772 ''')
773 with self.subTest('check enabled, shared GIL'):
774 self.run_with_shared_gil(script)
775 with self.subTest('check enabled, per-interpreter GIL'):
776 with self.assertRaises(ImportError):
777 self.run_with_own_gil(script)
778
779 @unittest.skipIf(_testmultiphase is None, "test requires _testmultiphase module")
780 def test_complete_multi_phase_init_module(self):
781 script = textwrap.dedent('''
782 from importlib.util import _incompatible_extension_module_restrictions
783 with _incompatible_extension_module_restrictions(disable_check=True):
784 import _testmultiphase
785 ''')
786 with self.subTest('check disabled, shared GIL'):
787 self.run_with_shared_gil(script)
788 with self.subTest('check disabled, per-interpreter GIL'):
789 self.run_with_own_gil(script)
790
791 script = textwrap.dedent(f'''
792 from importlib.util import _incompatible_extension_module_restrictions
793 with _incompatible_extension_module_restrictions(disable_check=False):
794 import _testmultiphase
795 ''')
796 with self.subTest('check enabled, shared GIL'):
797 self.run_with_shared_gil(script)
798 with self.subTest('check enabled, per-interpreter GIL'):
799 self.run_with_own_gil(script)
800
801
802 class PatchAtomicWrites:
803 def __init__(self, truncate_at_length, never_complete=False):
804 self.truncate_at_length = truncate_at_length
805 self.never_complete = never_complete
806 self.seen_write = False
807 self._children = []
808
809 def __enter__(self):
810 import _pyio
811
812 oldwrite = os.write
813
814 # Emulate an os.write that only writes partial data.
815 def write(fd, data):
816 if self.seen_write and self.never_complete:
817 return None
818 self.seen_write = True
819 return oldwrite(fd, data[:self.truncate_at_length])
820
821 # Need to patch _io to be _pyio, so that io.FileIO is affected by the
822 # os.write patch.
823 self.children = [
824 support.swap_attr(_bootstrap_external, '_io', _pyio),
825 support.swap_attr(os, 'write', write)
826 ]
827 for child in self.children:
828 child.__enter__()
829 return self
830
831 def __exit__(self, exc_type, exc_val, exc_tb):
832 for child in self.children:
833 child.__exit__(exc_type, exc_val, exc_tb)
834
835
836 class MiscTests(unittest.TestCase):
837
838 def test_atomic_write_retries_incomplete_writes(self):
839 truncate_at_length = 100
840 length = truncate_at_length * 2
841
842 with PatchAtomicWrites(truncate_at_length=truncate_at_length) as cm:
843 # Make sure we write something longer than the point where we
844 # truncate.
845 content = b'x' * length
846 _bootstrap_external._write_atomic(os_helper.TESTFN, content)
847 self.assertTrue(cm.seen_write)
848
849 self.assertEqual(os.stat(support.os_helper.TESTFN).st_size, length)
850 os.unlink(support.os_helper.TESTFN)
851
852 def test_atomic_write_errors_if_unable_to_complete(self):
853 truncate_at_length = 100
854
855 with (
856 PatchAtomicWrites(
857 truncate_at_length=truncate_at_length, never_complete=True,
858 ) as cm,
859 self.assertRaises(OSError)
860 ):
861 # Make sure we write something longer than the point where we
862 # truncate.
863 content = b'x' * (truncate_at_length * 2)
864 _bootstrap_external._write_atomic(os_helper.TESTFN, content)
865 self.assertTrue(cm.seen_write)
866
867 with self.assertRaises(OSError):
868 os.stat(support.os_helper.TESTFN) # Check that the file did not get written.
869
870
871 if __name__ == '__main__':
872 unittest.main()