diff -r d471cf4a73b2 Lib/test/test_compileall.py --- a/Lib/test/test_compileall.py Fri Oct 09 10:19:33 2015 -0400 +++ b/Lib/test/test_compileall.py Fri Oct 09 12:45:08 2015 -0700 @@ -1,7 +1,9 @@ import sys import compileall +import functools import importlib.util import os +import pathlib import py_compile import shutil import struct @@ -168,6 +170,34 @@ class CommandLineTests(unittest.TestCase): """Test compileall's CLI.""" + @classmethod + def setUpClass(cls): + for path in filter(os.path.isdir, sys.path): + try: + directory = pathlib.Path(path) / '__pycache__' + if not directory.is_dir(): + directory.mkdir() + path = directory / 'test.try' + with path.open('w') as file: + file.write('# for test_compileall') + except OSError: + sys_path_writable = False + break + else: + support.unlink(str(path)) + else: + sys_path_writable = True + cls._sys_path_writable = sys_path_writable + + def _skip_if_sys_path_not_writable(method): + @functools.wraps(method) + def closure(self, *args, **kwargs): + sys_path_writable = getattr(self, '_sys_path_writable', False) + if not sys_path_writable: + raise unittest.SkipTest('not all entries on sys.path are writable') + return method(self, *args, **kwargs) + return closure + def _get_run_args(self, args): interp_args = ['-S'] if sys.flags.optimize: @@ -194,8 +224,8 @@ self.assertFalse(os.path.exists(path)) def setUp(self): - self.addCleanup(self._cleanup) self.directory = tempfile.mkdtemp() + self.addCleanup(support.rmtree, self.directory) self.pkgdir = os.path.join(self.directory, 'foo') os.mkdir(self.pkgdir) self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__') @@ -203,9 +233,7 @@ self.initfn = script_helper.make_script(self.pkgdir, '__init__', '') self.barfn = script_helper.make_script(self.pkgdir, 'bar', '') - def _cleanup(self): - support.rmtree(self.directory) - + @_skip_if_sys_path_not_writable def test_no_args_compiles_path(self): # Note that -l is implied for the no args case. bazfn = script_helper.make_script(self.directory, 'baz', '') @@ -214,6 +242,7 @@ self.assertNotCompiled(self.initfn) self.assertNotCompiled(self.barfn) + @_skip_if_sys_path_not_writable def test_no_args_respects_force_flag(self): bazfn = script_helper.make_script(self.directory, 'baz', '') self.assertRunOK(PYTHONPATH=self.directory) @@ -230,6 +259,7 @@ mtime2 = os.stat(pycpath).st_mtime self.assertNotEqual(mtime, mtime2) + @_skip_if_sys_path_not_writable def test_no_args_respects_quiet_flag(self): script_helper.make_script(self.directory, 'baz', '') noisy = self.assertRunOK(PYTHONPATH=self.directory)