Skip to content

Commit 76b8a07

Browse files
authored
bpo-47089: Avoid test_compileall failures on Windows (GH-32037)
1 parent 17245c8 commit 76b8a07

File tree

1 file changed

+36
-48
lines changed

1 file changed

+36
-48
lines changed

Lib/test/test_compileall.py

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -460,31 +460,29 @@ def test_error(self):
460460
class CommandLineTestsBase:
461461
"""Test compileall's CLI."""
462462

463-
@classmethod
464-
def setUpClass(cls):
465-
for path in filter(os.path.isdir, sys.path):
466-
directory_created = False
467-
directory = pathlib.Path(path) / '__pycache__'
468-
path = directory / 'test.try'
469-
try:
470-
if not directory.is_dir():
471-
directory.mkdir()
472-
directory_created = True
473-
path.write_text('# for test_compileall', encoding="utf-8")
474-
except OSError:
475-
sys_path_writable = False
476-
break
477-
finally:
478-
os_helper.unlink(str(path))
479-
if directory_created:
480-
directory.rmdir()
481-
else:
482-
sys_path_writable = True
483-
cls._sys_path_writable = sys_path_writable
484-
485-
def _skip_if_sys_path_not_writable(self):
486-
if not self._sys_path_writable:
487-
raise unittest.SkipTest('not all entries on sys.path are writable')
463+
def setUp(self):
464+
self.directory = tempfile.mkdtemp()
465+
self.addCleanup(os_helper.rmtree, self.directory)
466+
self.pkgdir = os.path.join(self.directory, 'foo')
467+
os.mkdir(self.pkgdir)
468+
self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__')
469+
# Create the __init__.py and a package module.
470+
self.initfn = script_helper.make_script(self.pkgdir, '__init__', '')
471+
self.barfn = script_helper.make_script(self.pkgdir, 'bar', '')
472+
473+
@contextlib.contextmanager
474+
def temporary_pycache_prefix(self):
475+
"""Adjust and restore sys.pycache_prefix."""
476+
old_prefix = sys.pycache_prefix
477+
new_prefix = os.path.join(self.directory, '__testcache__')
478+
try:
479+
sys.pycache_prefix = new_prefix
480+
yield {
481+
'PYTHONPATH': self.directory,
482+
'PYTHONPYCACHEPREFIX': new_prefix,
483+
}
484+
finally:
485+
sys.pycache_prefix = old_prefix
488486

489487
def _get_run_args(self, args):
490488
return [*support.optim_args_from_interpreter_flags(),
@@ -512,49 +510,39 @@ def assertNotCompiled(self, fn):
512510
path = importlib.util.cache_from_source(fn)
513511
self.assertFalse(os.path.exists(path))
514512

515-
def setUp(self):
516-
self.directory = tempfile.mkdtemp()
517-
self.addCleanup(os_helper.rmtree, self.directory)
518-
self.pkgdir = os.path.join(self.directory, 'foo')
519-
os.mkdir(self.pkgdir)
520-
self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__')
521-
# Create the __init__.py and a package module.
522-
self.initfn = script_helper.make_script(self.pkgdir, '__init__', '')
523-
self.barfn = script_helper.make_script(self.pkgdir, 'bar', '')
524-
525513
def test_no_args_compiles_path(self):
526514
# Note that -l is implied for the no args case.
527-
self._skip_if_sys_path_not_writable()
528515
bazfn = script_helper.make_script(self.directory, 'baz', '')
529-
self.assertRunOK(PYTHONPATH=self.directory)
530-
self.assertCompiled(bazfn)
531-
self.assertNotCompiled(self.initfn)
532-
self.assertNotCompiled(self.barfn)
516+
with self.temporary_pycache_prefix() as env:
517+
self.assertRunOK(**env)
518+
self.assertCompiled(bazfn)
519+
self.assertNotCompiled(self.initfn)
520+
self.assertNotCompiled(self.barfn)
533521

534522
@without_source_date_epoch # timestamp invalidation test
535523
def test_no_args_respects_force_flag(self):
536-
self._skip_if_sys_path_not_writable()
537524
bazfn = script_helper.make_script(self.directory, 'baz', '')
538-
self.assertRunOK(PYTHONPATH=self.directory)
539-
pycpath = importlib.util.cache_from_source(bazfn)
525+
with self.temporary_pycache_prefix() as env:
526+
self.assertRunOK(**env)
527+
pycpath = importlib.util.cache_from_source(bazfn)
540528
# Set atime/mtime backward to avoid file timestamp resolution issues
541529
os.utime(pycpath, (time.time()-60,)*2)
542530
mtime = os.stat(pycpath).st_mtime
543531
# Without force, no recompilation
544-
self.assertRunOK(PYTHONPATH=self.directory)
532+
self.assertRunOK(**env)
545533
mtime2 = os.stat(pycpath).st_mtime
546534
self.assertEqual(mtime, mtime2)
547535
# Now force it.
548-
self.assertRunOK('-f', PYTHONPATH=self.directory)
536+
self.assertRunOK('-f', **env)
549537
mtime2 = os.stat(pycpath).st_mtime
550538
self.assertNotEqual(mtime, mtime2)
551539

552540
def test_no_args_respects_quiet_flag(self):
553-
self._skip_if_sys_path_not_writable()
554541
script_helper.make_script(self.directory, 'baz', '')
555-
noisy = self.assertRunOK(PYTHONPATH=self.directory)
542+
with self.temporary_pycache_prefix() as env:
543+
noisy = self.assertRunOK(**env)
556544
self.assertIn(b'Listing ', noisy)
557-
quiet = self.assertRunOK('-q', PYTHONPATH=self.directory)
545+
quiet = self.assertRunOK('-q', **env)
558546
self.assertNotIn(b'Listing ', quiet)
559547

560548
# Ensure that the default behavior of compileall's CLI is to create

0 commit comments

Comments
 (0)