Skip to content

Commit 1069d52

Browse files
bpo-47089: Avoid test_compileall failures on Windows (GH-32037)
(cherry picked from commit 76b8a07) Co-authored-by: Jeremy Kloth <[email protected]>
1 parent 55d5c96 commit 1069d52

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
@@ -457,31 +457,29 @@ def test_error(self):
457457
class CommandLineTestsBase:
458458
"""Test compileall's CLI."""
459459

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

486484
def _get_run_args(self, args):
487485
return [*support.optim_args_from_interpreter_flags(),
@@ -509,49 +507,39 @@ def assertNotCompiled(self, fn):
509507
path = importlib.util.cache_from_source(fn)
510508
self.assertFalse(os.path.exists(path))
511509

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

531519
@without_source_date_epoch # timestamp invalidation test
532520
def test_no_args_respects_force_flag(self):
533-
self._skip_if_sys_path_not_writable()
534521
bazfn = script_helper.make_script(self.directory, 'baz', '')
535-
self.assertRunOK(PYTHONPATH=self.directory)
536-
pycpath = importlib.util.cache_from_source(bazfn)
522+
with self.temporary_pycache_prefix() as env:
523+
self.assertRunOK(**env)
524+
pycpath = importlib.util.cache_from_source(bazfn)
537525
# Set atime/mtime backward to avoid file timestamp resolution issues
538526
os.utime(pycpath, (time.time()-60,)*2)
539527
mtime = os.stat(pycpath).st_mtime
540528
# Without force, no recompilation
541-
self.assertRunOK(PYTHONPATH=self.directory)
529+
self.assertRunOK(**env)
542530
mtime2 = os.stat(pycpath).st_mtime
543531
self.assertEqual(mtime, mtime2)
544532
# Now force it.
545-
self.assertRunOK('-f', PYTHONPATH=self.directory)
533+
self.assertRunOK('-f', **env)
546534
mtime2 = os.stat(pycpath).st_mtime
547535
self.assertNotEqual(mtime, mtime2)
548536

549537
def test_no_args_respects_quiet_flag(self):
550-
self._skip_if_sys_path_not_writable()
551538
script_helper.make_script(self.directory, 'baz', '')
552-
noisy = self.assertRunOK(PYTHONPATH=self.directory)
539+
with self.temporary_pycache_prefix() as env:
540+
noisy = self.assertRunOK(**env)
553541
self.assertIn(b'Listing ', noisy)
554-
quiet = self.assertRunOK('-q', PYTHONPATH=self.directory)
542+
quiet = self.assertRunOK('-q', **env)
555543
self.assertNotIn(b'Listing ', quiet)
556544

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

0 commit comments

Comments
 (0)