| 1 | n/a | import sys |
|---|
| 2 | n/a | import compileall |
|---|
| 3 | n/a | import importlib.util |
|---|
| 4 | n/a | import test.test_importlib.util |
|---|
| 5 | n/a | import os |
|---|
| 6 | n/a | import pathlib |
|---|
| 7 | n/a | import py_compile |
|---|
| 8 | n/a | import shutil |
|---|
| 9 | n/a | import struct |
|---|
| 10 | n/a | import tempfile |
|---|
| 11 | n/a | import time |
|---|
| 12 | n/a | import unittest |
|---|
| 13 | n/a | import io |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | from unittest import mock, skipUnless |
|---|
| 16 | n/a | try: |
|---|
| 17 | n/a | from concurrent.futures import ProcessPoolExecutor |
|---|
| 18 | n/a | _have_multiprocessing = True |
|---|
| 19 | n/a | except ImportError: |
|---|
| 20 | n/a | _have_multiprocessing = False |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | from test import support |
|---|
| 23 | n/a | from test.support import script_helper |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | class CompileallTests(unittest.TestCase): |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | def setUp(self): |
|---|
| 28 | n/a | self.directory = tempfile.mkdtemp() |
|---|
| 29 | n/a | self.source_path = os.path.join(self.directory, '_test.py') |
|---|
| 30 | n/a | self.bc_path = importlib.util.cache_from_source(self.source_path) |
|---|
| 31 | n/a | with open(self.source_path, 'w') as file: |
|---|
| 32 | n/a | file.write('x = 123\n') |
|---|
| 33 | n/a | self.source_path2 = os.path.join(self.directory, '_test2.py') |
|---|
| 34 | n/a | self.bc_path2 = importlib.util.cache_from_source(self.source_path2) |
|---|
| 35 | n/a | shutil.copyfile(self.source_path, self.source_path2) |
|---|
| 36 | n/a | self.subdirectory = os.path.join(self.directory, '_subdir') |
|---|
| 37 | n/a | os.mkdir(self.subdirectory) |
|---|
| 38 | n/a | self.source_path3 = os.path.join(self.subdirectory, '_test3.py') |
|---|
| 39 | n/a | shutil.copyfile(self.source_path, self.source_path3) |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | def tearDown(self): |
|---|
| 42 | n/a | shutil.rmtree(self.directory) |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | def add_bad_source_file(self): |
|---|
| 45 | n/a | self.bad_source_path = os.path.join(self.directory, '_test_bad.py') |
|---|
| 46 | n/a | with open(self.bad_source_path, 'w') as file: |
|---|
| 47 | n/a | file.write('x (\n') |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | def data(self): |
|---|
| 50 | n/a | with open(self.bc_path, 'rb') as file: |
|---|
| 51 | n/a | data = file.read(8) |
|---|
| 52 | n/a | mtime = int(os.stat(self.source_path).st_mtime) |
|---|
| 53 | n/a | compare = struct.pack('<4sl', importlib.util.MAGIC_NUMBER, mtime) |
|---|
| 54 | n/a | return data, compare |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()') |
|---|
| 57 | n/a | def recreation_check(self, metadata): |
|---|
| 58 | n/a | """Check that compileall recreates bytecode when the new metadata is |
|---|
| 59 | n/a | used.""" |
|---|
| 60 | n/a | py_compile.compile(self.source_path) |
|---|
| 61 | n/a | self.assertEqual(*self.data()) |
|---|
| 62 | n/a | with open(self.bc_path, 'rb') as file: |
|---|
| 63 | n/a | bc = file.read()[len(metadata):] |
|---|
| 64 | n/a | with open(self.bc_path, 'wb') as file: |
|---|
| 65 | n/a | file.write(metadata) |
|---|
| 66 | n/a | file.write(bc) |
|---|
| 67 | n/a | self.assertNotEqual(*self.data()) |
|---|
| 68 | n/a | compileall.compile_dir(self.directory, force=False, quiet=True) |
|---|
| 69 | n/a | self.assertTrue(*self.data()) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def test_mtime(self): |
|---|
| 72 | n/a | # Test a change in mtime leads to a new .pyc. |
|---|
| 73 | n/a | self.recreation_check(struct.pack('<4sl', importlib.util.MAGIC_NUMBER, |
|---|
| 74 | n/a | 1)) |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | def test_magic_number(self): |
|---|
| 77 | n/a | # Test a change in mtime leads to a new .pyc. |
|---|
| 78 | n/a | self.recreation_check(b'\0\0\0\0') |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | def test_compile_files(self): |
|---|
| 81 | n/a | # Test compiling a single file, and complete directory |
|---|
| 82 | n/a | for fn in (self.bc_path, self.bc_path2): |
|---|
| 83 | n/a | try: |
|---|
| 84 | n/a | os.unlink(fn) |
|---|
| 85 | n/a | except: |
|---|
| 86 | n/a | pass |
|---|
| 87 | n/a | self.assertTrue(compileall.compile_file(self.source_path, |
|---|
| 88 | n/a | force=False, quiet=True)) |
|---|
| 89 | n/a | self.assertTrue(os.path.isfile(self.bc_path) and |
|---|
| 90 | n/a | not os.path.isfile(self.bc_path2)) |
|---|
| 91 | n/a | os.unlink(self.bc_path) |
|---|
| 92 | n/a | self.assertTrue(compileall.compile_dir(self.directory, force=False, |
|---|
| 93 | n/a | quiet=True)) |
|---|
| 94 | n/a | self.assertTrue(os.path.isfile(self.bc_path) and |
|---|
| 95 | n/a | os.path.isfile(self.bc_path2)) |
|---|
| 96 | n/a | os.unlink(self.bc_path) |
|---|
| 97 | n/a | os.unlink(self.bc_path2) |
|---|
| 98 | n/a | # Test against bad files |
|---|
| 99 | n/a | self.add_bad_source_file() |
|---|
| 100 | n/a | self.assertFalse(compileall.compile_file(self.bad_source_path, |
|---|
| 101 | n/a | force=False, quiet=2)) |
|---|
| 102 | n/a | self.assertFalse(compileall.compile_dir(self.directory, |
|---|
| 103 | n/a | force=False, quiet=2)) |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | def test_compile_file_pathlike(self): |
|---|
| 106 | n/a | self.assertFalse(os.path.isfile(self.bc_path)) |
|---|
| 107 | n/a | # we should also test the output |
|---|
| 108 | n/a | with support.captured_stdout() as stdout: |
|---|
| 109 | n/a | self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path))) |
|---|
| 110 | n/a | self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)') |
|---|
| 111 | n/a | self.assertTrue(os.path.isfile(self.bc_path)) |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | def test_compile_file_pathlike_ddir(self): |
|---|
| 114 | n/a | self.assertFalse(os.path.isfile(self.bc_path)) |
|---|
| 115 | n/a | self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path), |
|---|
| 116 | n/a | ddir=pathlib.Path('ddir_path'), |
|---|
| 117 | n/a | quiet=2)) |
|---|
| 118 | n/a | self.assertTrue(os.path.isfile(self.bc_path)) |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | def test_compile_path(self): |
|---|
| 121 | n/a | with test.test_importlib.util.import_state(path=[self.directory]): |
|---|
| 122 | n/a | self.assertTrue(compileall.compile_path(quiet=2)) |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | with test.test_importlib.util.import_state(path=[self.directory]): |
|---|
| 125 | n/a | self.add_bad_source_file() |
|---|
| 126 | n/a | self.assertFalse(compileall.compile_path(skip_curdir=False, |
|---|
| 127 | n/a | force=True, quiet=2)) |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | def test_no_pycache_in_non_package(self): |
|---|
| 130 | n/a | # Bug 8563 reported that __pycache__ directories got created by |
|---|
| 131 | n/a | # compile_file() for non-.py files. |
|---|
| 132 | n/a | data_dir = os.path.join(self.directory, 'data') |
|---|
| 133 | n/a | data_file = os.path.join(data_dir, 'file') |
|---|
| 134 | n/a | os.mkdir(data_dir) |
|---|
| 135 | n/a | # touch data/file |
|---|
| 136 | n/a | with open(data_file, 'w'): |
|---|
| 137 | n/a | pass |
|---|
| 138 | n/a | compileall.compile_file(data_file) |
|---|
| 139 | n/a | self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__'))) |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | def test_optimize(self): |
|---|
| 142 | n/a | # make sure compiling with different optimization settings than the |
|---|
| 143 | n/a | # interpreter's creates the correct file names |
|---|
| 144 | n/a | optimize, opt = (1, 1) if __debug__ else (0, '') |
|---|
| 145 | n/a | compileall.compile_dir(self.directory, quiet=True, optimize=optimize) |
|---|
| 146 | n/a | cached = importlib.util.cache_from_source(self.source_path, |
|---|
| 147 | n/a | optimization=opt) |
|---|
| 148 | n/a | self.assertTrue(os.path.isfile(cached)) |
|---|
| 149 | n/a | cached2 = importlib.util.cache_from_source(self.source_path2, |
|---|
| 150 | n/a | optimization=opt) |
|---|
| 151 | n/a | self.assertTrue(os.path.isfile(cached2)) |
|---|
| 152 | n/a | cached3 = importlib.util.cache_from_source(self.source_path3, |
|---|
| 153 | n/a | optimization=opt) |
|---|
| 154 | n/a | self.assertTrue(os.path.isfile(cached3)) |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | def test_compile_dir_pathlike(self): |
|---|
| 157 | n/a | self.assertFalse(os.path.isfile(self.bc_path)) |
|---|
| 158 | n/a | with support.captured_stdout() as stdout: |
|---|
| 159 | n/a | compileall.compile_dir(pathlib.Path(self.directory)) |
|---|
| 160 | n/a | line = stdout.getvalue().splitlines()[0] |
|---|
| 161 | n/a | self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)') |
|---|
| 162 | n/a | self.assertTrue(os.path.isfile(self.bc_path)) |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | @mock.patch('compileall.ProcessPoolExecutor') |
|---|
| 165 | n/a | def test_compile_pool_called(self, pool_mock): |
|---|
| 166 | n/a | compileall.compile_dir(self.directory, quiet=True, workers=5) |
|---|
| 167 | n/a | self.assertTrue(pool_mock.called) |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | def test_compile_workers_non_positive(self): |
|---|
| 170 | n/a | with self.assertRaisesRegex(ValueError, |
|---|
| 171 | n/a | "workers must be greater or equal to 0"): |
|---|
| 172 | n/a | compileall.compile_dir(self.directory, workers=-1) |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | @mock.patch('compileall.ProcessPoolExecutor') |
|---|
| 175 | n/a | def test_compile_workers_cpu_count(self, pool_mock): |
|---|
| 176 | n/a | compileall.compile_dir(self.directory, quiet=True, workers=0) |
|---|
| 177 | n/a | self.assertEqual(pool_mock.call_args[1]['max_workers'], None) |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | @mock.patch('compileall.ProcessPoolExecutor') |
|---|
| 180 | n/a | @mock.patch('compileall.compile_file') |
|---|
| 181 | n/a | def test_compile_one_worker(self, compile_file_mock, pool_mock): |
|---|
| 182 | n/a | compileall.compile_dir(self.directory, quiet=True) |
|---|
| 183 | n/a | self.assertFalse(pool_mock.called) |
|---|
| 184 | n/a | self.assertTrue(compile_file_mock.called) |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | @mock.patch('compileall.ProcessPoolExecutor', new=None) |
|---|
| 187 | n/a | @mock.patch('compileall.compile_file') |
|---|
| 188 | n/a | def test_compile_missing_multiprocessing(self, compile_file_mock): |
|---|
| 189 | n/a | compileall.compile_dir(self.directory, quiet=True, workers=5) |
|---|
| 190 | n/a | self.assertTrue(compile_file_mock.called) |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | class EncodingTest(unittest.TestCase): |
|---|
| 193 | n/a | """Issue 6716: compileall should escape source code when printing errors |
|---|
| 194 | n/a | to stdout.""" |
|---|
| 195 | n/a | |
|---|
| 196 | n/a | def setUp(self): |
|---|
| 197 | n/a | self.directory = tempfile.mkdtemp() |
|---|
| 198 | n/a | self.source_path = os.path.join(self.directory, '_test.py') |
|---|
| 199 | n/a | with open(self.source_path, 'w', encoding='utf-8') as file: |
|---|
| 200 | n/a | file.write('# -*- coding: utf-8 -*-\n') |
|---|
| 201 | n/a | file.write('print u"\u20ac"\n') |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | def tearDown(self): |
|---|
| 204 | n/a | shutil.rmtree(self.directory) |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | def test_error(self): |
|---|
| 207 | n/a | try: |
|---|
| 208 | n/a | orig_stdout = sys.stdout |
|---|
| 209 | n/a | sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii') |
|---|
| 210 | n/a | compileall.compile_dir(self.directory) |
|---|
| 211 | n/a | finally: |
|---|
| 212 | n/a | sys.stdout = orig_stdout |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | class CommandLineTests(unittest.TestCase): |
|---|
| 216 | n/a | """Test compileall's CLI.""" |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | @classmethod |
|---|
| 219 | n/a | def setUpClass(cls): |
|---|
| 220 | n/a | for path in filter(os.path.isdir, sys.path): |
|---|
| 221 | n/a | directory_created = False |
|---|
| 222 | n/a | directory = pathlib.Path(path) / '__pycache__' |
|---|
| 223 | n/a | path = directory / 'test.try' |
|---|
| 224 | n/a | try: |
|---|
| 225 | n/a | if not directory.is_dir(): |
|---|
| 226 | n/a | directory.mkdir() |
|---|
| 227 | n/a | directory_created = True |
|---|
| 228 | n/a | with path.open('w') as file: |
|---|
| 229 | n/a | file.write('# for test_compileall') |
|---|
| 230 | n/a | except OSError: |
|---|
| 231 | n/a | sys_path_writable = False |
|---|
| 232 | n/a | break |
|---|
| 233 | n/a | finally: |
|---|
| 234 | n/a | support.unlink(str(path)) |
|---|
| 235 | n/a | if directory_created: |
|---|
| 236 | n/a | directory.rmdir() |
|---|
| 237 | n/a | else: |
|---|
| 238 | n/a | sys_path_writable = True |
|---|
| 239 | n/a | cls._sys_path_writable = sys_path_writable |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | def _skip_if_sys_path_not_writable(self): |
|---|
| 242 | n/a | if not self._sys_path_writable: |
|---|
| 243 | n/a | raise unittest.SkipTest('not all entries on sys.path are writable') |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | def _get_run_args(self, args): |
|---|
| 246 | n/a | return [*support.optim_args_from_interpreter_flags(), |
|---|
| 247 | n/a | '-S', '-m', 'compileall', |
|---|
| 248 | n/a | *args] |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | def assertRunOK(self, *args, **env_vars): |
|---|
| 251 | n/a | rc, out, err = script_helper.assert_python_ok( |
|---|
| 252 | n/a | *self._get_run_args(args), **env_vars) |
|---|
| 253 | n/a | self.assertEqual(b'', err) |
|---|
| 254 | n/a | return out |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | def assertRunNotOK(self, *args, **env_vars): |
|---|
| 257 | n/a | rc, out, err = script_helper.assert_python_failure( |
|---|
| 258 | n/a | *self._get_run_args(args), **env_vars) |
|---|
| 259 | n/a | return rc, out, err |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | def assertCompiled(self, fn): |
|---|
| 262 | n/a | path = importlib.util.cache_from_source(fn) |
|---|
| 263 | n/a | self.assertTrue(os.path.exists(path)) |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | def assertNotCompiled(self, fn): |
|---|
| 266 | n/a | path = importlib.util.cache_from_source(fn) |
|---|
| 267 | n/a | self.assertFalse(os.path.exists(path)) |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | def setUp(self): |
|---|
| 270 | n/a | self.directory = tempfile.mkdtemp() |
|---|
| 271 | n/a | self.addCleanup(support.rmtree, self.directory) |
|---|
| 272 | n/a | self.pkgdir = os.path.join(self.directory, 'foo') |
|---|
| 273 | n/a | os.mkdir(self.pkgdir) |
|---|
| 274 | n/a | self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__') |
|---|
| 275 | n/a | # Create the __init__.py and a package module. |
|---|
| 276 | n/a | self.initfn = script_helper.make_script(self.pkgdir, '__init__', '') |
|---|
| 277 | n/a | self.barfn = script_helper.make_script(self.pkgdir, 'bar', '') |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | def test_no_args_compiles_path(self): |
|---|
| 280 | n/a | # Note that -l is implied for the no args case. |
|---|
| 281 | n/a | self._skip_if_sys_path_not_writable() |
|---|
| 282 | n/a | bazfn = script_helper.make_script(self.directory, 'baz', '') |
|---|
| 283 | n/a | self.assertRunOK(PYTHONPATH=self.directory) |
|---|
| 284 | n/a | self.assertCompiled(bazfn) |
|---|
| 285 | n/a | self.assertNotCompiled(self.initfn) |
|---|
| 286 | n/a | self.assertNotCompiled(self.barfn) |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | def test_no_args_respects_force_flag(self): |
|---|
| 289 | n/a | self._skip_if_sys_path_not_writable() |
|---|
| 290 | n/a | bazfn = script_helper.make_script(self.directory, 'baz', '') |
|---|
| 291 | n/a | self.assertRunOK(PYTHONPATH=self.directory) |
|---|
| 292 | n/a | pycpath = importlib.util.cache_from_source(bazfn) |
|---|
| 293 | n/a | # Set atime/mtime backward to avoid file timestamp resolution issues |
|---|
| 294 | n/a | os.utime(pycpath, (time.time()-60,)*2) |
|---|
| 295 | n/a | mtime = os.stat(pycpath).st_mtime |
|---|
| 296 | n/a | # Without force, no recompilation |
|---|
| 297 | n/a | self.assertRunOK(PYTHONPATH=self.directory) |
|---|
| 298 | n/a | mtime2 = os.stat(pycpath).st_mtime |
|---|
| 299 | n/a | self.assertEqual(mtime, mtime2) |
|---|
| 300 | n/a | # Now force it. |
|---|
| 301 | n/a | self.assertRunOK('-f', PYTHONPATH=self.directory) |
|---|
| 302 | n/a | mtime2 = os.stat(pycpath).st_mtime |
|---|
| 303 | n/a | self.assertNotEqual(mtime, mtime2) |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | def test_no_args_respects_quiet_flag(self): |
|---|
| 306 | n/a | self._skip_if_sys_path_not_writable() |
|---|
| 307 | n/a | script_helper.make_script(self.directory, 'baz', '') |
|---|
| 308 | n/a | noisy = self.assertRunOK(PYTHONPATH=self.directory) |
|---|
| 309 | n/a | self.assertIn(b'Listing ', noisy) |
|---|
| 310 | n/a | quiet = self.assertRunOK('-q', PYTHONPATH=self.directory) |
|---|
| 311 | n/a | self.assertNotIn(b'Listing ', quiet) |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | # Ensure that the default behavior of compileall's CLI is to create |
|---|
| 314 | n/a | # PEP 3147/PEP 488 pyc files. |
|---|
| 315 | n/a | for name, ext, switch in [ |
|---|
| 316 | n/a | ('normal', 'pyc', []), |
|---|
| 317 | n/a | ('optimize', 'opt-1.pyc', ['-O']), |
|---|
| 318 | n/a | ('doubleoptimize', 'opt-2.pyc', ['-OO']), |
|---|
| 319 | n/a | ]: |
|---|
| 320 | n/a | def f(self, ext=ext, switch=switch): |
|---|
| 321 | n/a | script_helper.assert_python_ok(*(switch + |
|---|
| 322 | n/a | ['-m', 'compileall', '-q', self.pkgdir])) |
|---|
| 323 | n/a | # Verify the __pycache__ directory contents. |
|---|
| 324 | n/a | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
|---|
| 325 | n/a | expected = sorted(base.format(sys.implementation.cache_tag, ext) |
|---|
| 326 | n/a | for base in ('__init__.{}.{}', 'bar.{}.{}')) |
|---|
| 327 | n/a | self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected) |
|---|
| 328 | n/a | # Make sure there are no .pyc files in the source directory. |
|---|
| 329 | n/a | self.assertFalse([fn for fn in os.listdir(self.pkgdir) |
|---|
| 330 | n/a | if fn.endswith(ext)]) |
|---|
| 331 | n/a | locals()['test_pep3147_paths_' + name] = f |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | def test_legacy_paths(self): |
|---|
| 334 | n/a | # Ensure that with the proper switch, compileall leaves legacy |
|---|
| 335 | n/a | # pyc files, and no __pycache__ directory. |
|---|
| 336 | n/a | self.assertRunOK('-b', '-q', self.pkgdir) |
|---|
| 337 | n/a | # Verify the __pycache__ directory contents. |
|---|
| 338 | n/a | self.assertFalse(os.path.exists(self.pkgdir_cachedir)) |
|---|
| 339 | n/a | expected = sorted(['__init__.py', '__init__.pyc', 'bar.py', |
|---|
| 340 | n/a | 'bar.pyc']) |
|---|
| 341 | n/a | self.assertEqual(sorted(os.listdir(self.pkgdir)), expected) |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | def test_multiple_runs(self): |
|---|
| 344 | n/a | # Bug 8527 reported that multiple calls produced empty |
|---|
| 345 | n/a | # __pycache__/__pycache__ directories. |
|---|
| 346 | n/a | self.assertRunOK('-q', self.pkgdir) |
|---|
| 347 | n/a | # Verify the __pycache__ directory contents. |
|---|
| 348 | n/a | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
|---|
| 349 | n/a | cachecachedir = os.path.join(self.pkgdir_cachedir, '__pycache__') |
|---|
| 350 | n/a | self.assertFalse(os.path.exists(cachecachedir)) |
|---|
| 351 | n/a | # Call compileall again. |
|---|
| 352 | n/a | self.assertRunOK('-q', self.pkgdir) |
|---|
| 353 | n/a | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
|---|
| 354 | n/a | self.assertFalse(os.path.exists(cachecachedir)) |
|---|
| 355 | n/a | |
|---|
| 356 | n/a | def test_force(self): |
|---|
| 357 | n/a | self.assertRunOK('-q', self.pkgdir) |
|---|
| 358 | n/a | pycpath = importlib.util.cache_from_source(self.barfn) |
|---|
| 359 | n/a | # set atime/mtime backward to avoid file timestamp resolution issues |
|---|
| 360 | n/a | os.utime(pycpath, (time.time()-60,)*2) |
|---|
| 361 | n/a | mtime = os.stat(pycpath).st_mtime |
|---|
| 362 | n/a | # without force, no recompilation |
|---|
| 363 | n/a | self.assertRunOK('-q', self.pkgdir) |
|---|
| 364 | n/a | mtime2 = os.stat(pycpath).st_mtime |
|---|
| 365 | n/a | self.assertEqual(mtime, mtime2) |
|---|
| 366 | n/a | # now force it. |
|---|
| 367 | n/a | self.assertRunOK('-q', '-f', self.pkgdir) |
|---|
| 368 | n/a | mtime2 = os.stat(pycpath).st_mtime |
|---|
| 369 | n/a | self.assertNotEqual(mtime, mtime2) |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | def test_recursion_control(self): |
|---|
| 372 | n/a | subpackage = os.path.join(self.pkgdir, 'spam') |
|---|
| 373 | n/a | os.mkdir(subpackage) |
|---|
| 374 | n/a | subinitfn = script_helper.make_script(subpackage, '__init__', '') |
|---|
| 375 | n/a | hamfn = script_helper.make_script(subpackage, 'ham', '') |
|---|
| 376 | n/a | self.assertRunOK('-q', '-l', self.pkgdir) |
|---|
| 377 | n/a | self.assertNotCompiled(subinitfn) |
|---|
| 378 | n/a | self.assertFalse(os.path.exists(os.path.join(subpackage, '__pycache__'))) |
|---|
| 379 | n/a | self.assertRunOK('-q', self.pkgdir) |
|---|
| 380 | n/a | self.assertCompiled(subinitfn) |
|---|
| 381 | n/a | self.assertCompiled(hamfn) |
|---|
| 382 | n/a | |
|---|
| 383 | n/a | def test_recursion_limit(self): |
|---|
| 384 | n/a | subpackage = os.path.join(self.pkgdir, 'spam') |
|---|
| 385 | n/a | subpackage2 = os.path.join(subpackage, 'ham') |
|---|
| 386 | n/a | subpackage3 = os.path.join(subpackage2, 'eggs') |
|---|
| 387 | n/a | for pkg in (subpackage, subpackage2, subpackage3): |
|---|
| 388 | n/a | script_helper.make_pkg(pkg) |
|---|
| 389 | n/a | |
|---|
| 390 | n/a | subinitfn = os.path.join(subpackage, '__init__.py') |
|---|
| 391 | n/a | hamfn = script_helper.make_script(subpackage, 'ham', '') |
|---|
| 392 | n/a | spamfn = script_helper.make_script(subpackage2, 'spam', '') |
|---|
| 393 | n/a | eggfn = script_helper.make_script(subpackage3, 'egg', '') |
|---|
| 394 | n/a | |
|---|
| 395 | n/a | self.assertRunOK('-q', '-r 0', self.pkgdir) |
|---|
| 396 | n/a | self.assertNotCompiled(subinitfn) |
|---|
| 397 | n/a | self.assertFalse( |
|---|
| 398 | n/a | os.path.exists(os.path.join(subpackage, '__pycache__'))) |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | self.assertRunOK('-q', '-r 1', self.pkgdir) |
|---|
| 401 | n/a | self.assertCompiled(subinitfn) |
|---|
| 402 | n/a | self.assertCompiled(hamfn) |
|---|
| 403 | n/a | self.assertNotCompiled(spamfn) |
|---|
| 404 | n/a | |
|---|
| 405 | n/a | self.assertRunOK('-q', '-r 2', self.pkgdir) |
|---|
| 406 | n/a | self.assertCompiled(subinitfn) |
|---|
| 407 | n/a | self.assertCompiled(hamfn) |
|---|
| 408 | n/a | self.assertCompiled(spamfn) |
|---|
| 409 | n/a | self.assertNotCompiled(eggfn) |
|---|
| 410 | n/a | |
|---|
| 411 | n/a | self.assertRunOK('-q', '-r 5', self.pkgdir) |
|---|
| 412 | n/a | self.assertCompiled(subinitfn) |
|---|
| 413 | n/a | self.assertCompiled(hamfn) |
|---|
| 414 | n/a | self.assertCompiled(spamfn) |
|---|
| 415 | n/a | self.assertCompiled(eggfn) |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | def test_quiet(self): |
|---|
| 418 | n/a | noisy = self.assertRunOK(self.pkgdir) |
|---|
| 419 | n/a | quiet = self.assertRunOK('-q', self.pkgdir) |
|---|
| 420 | n/a | self.assertNotEqual(b'', noisy) |
|---|
| 421 | n/a | self.assertEqual(b'', quiet) |
|---|
| 422 | n/a | |
|---|
| 423 | n/a | def test_silent(self): |
|---|
| 424 | n/a | script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax') |
|---|
| 425 | n/a | _, quiet, _ = self.assertRunNotOK('-q', self.pkgdir) |
|---|
| 426 | n/a | _, silent, _ = self.assertRunNotOK('-qq', self.pkgdir) |
|---|
| 427 | n/a | self.assertNotEqual(b'', quiet) |
|---|
| 428 | n/a | self.assertEqual(b'', silent) |
|---|
| 429 | n/a | |
|---|
| 430 | n/a | def test_regexp(self): |
|---|
| 431 | n/a | self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir) |
|---|
| 432 | n/a | self.assertNotCompiled(self.barfn) |
|---|
| 433 | n/a | self.assertCompiled(self.initfn) |
|---|
| 434 | n/a | |
|---|
| 435 | n/a | def test_multiple_dirs(self): |
|---|
| 436 | n/a | pkgdir2 = os.path.join(self.directory, 'foo2') |
|---|
| 437 | n/a | os.mkdir(pkgdir2) |
|---|
| 438 | n/a | init2fn = script_helper.make_script(pkgdir2, '__init__', '') |
|---|
| 439 | n/a | bar2fn = script_helper.make_script(pkgdir2, 'bar2', '') |
|---|
| 440 | n/a | self.assertRunOK('-q', self.pkgdir, pkgdir2) |
|---|
| 441 | n/a | self.assertCompiled(self.initfn) |
|---|
| 442 | n/a | self.assertCompiled(self.barfn) |
|---|
| 443 | n/a | self.assertCompiled(init2fn) |
|---|
| 444 | n/a | self.assertCompiled(bar2fn) |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | def test_d_compile_error(self): |
|---|
| 447 | n/a | script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax') |
|---|
| 448 | n/a | rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir) |
|---|
| 449 | n/a | self.assertRegex(out, b'File "dinsdale') |
|---|
| 450 | n/a | |
|---|
| 451 | n/a | def test_d_runtime_error(self): |
|---|
| 452 | n/a | bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception') |
|---|
| 453 | n/a | self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir) |
|---|
| 454 | n/a | fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz') |
|---|
| 455 | n/a | pyc = importlib.util.cache_from_source(bazfn) |
|---|
| 456 | n/a | os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc')) |
|---|
| 457 | n/a | os.remove(bazfn) |
|---|
| 458 | n/a | rc, out, err = script_helper.assert_python_failure(fn, __isolated=False) |
|---|
| 459 | n/a | self.assertRegex(err, b'File "dinsdale') |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | def test_include_bad_file(self): |
|---|
| 462 | n/a | rc, out, err = self.assertRunNotOK( |
|---|
| 463 | n/a | '-i', os.path.join(self.directory, 'nosuchfile'), self.pkgdir) |
|---|
| 464 | n/a | self.assertRegex(out, b'rror.*nosuchfile') |
|---|
| 465 | n/a | self.assertNotRegex(err, b'Traceback') |
|---|
| 466 | n/a | self.assertFalse(os.path.exists(importlib.util.cache_from_source( |
|---|
| 467 | n/a | self.pkgdir_cachedir))) |
|---|
| 468 | n/a | |
|---|
| 469 | n/a | def test_include_file_with_arg(self): |
|---|
| 470 | n/a | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
|---|
| 471 | n/a | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
|---|
| 472 | n/a | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
|---|
| 473 | n/a | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
|---|
| 474 | n/a | with open(os.path.join(self.directory, 'l1'), 'w') as l1: |
|---|
| 475 | n/a | l1.write(os.path.join(self.pkgdir, 'f1.py')+os.linesep) |
|---|
| 476 | n/a | l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep) |
|---|
| 477 | n/a | self.assertRunOK('-i', os.path.join(self.directory, 'l1'), f4) |
|---|
| 478 | n/a | self.assertCompiled(f1) |
|---|
| 479 | n/a | self.assertCompiled(f2) |
|---|
| 480 | n/a | self.assertNotCompiled(f3) |
|---|
| 481 | n/a | self.assertCompiled(f4) |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | def test_include_file_no_arg(self): |
|---|
| 484 | n/a | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
|---|
| 485 | n/a | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
|---|
| 486 | n/a | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
|---|
| 487 | n/a | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
|---|
| 488 | n/a | with open(os.path.join(self.directory, 'l1'), 'w') as l1: |
|---|
| 489 | n/a | l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep) |
|---|
| 490 | n/a | self.assertRunOK('-i', os.path.join(self.directory, 'l1')) |
|---|
| 491 | n/a | self.assertNotCompiled(f1) |
|---|
| 492 | n/a | self.assertCompiled(f2) |
|---|
| 493 | n/a | self.assertNotCompiled(f3) |
|---|
| 494 | n/a | self.assertNotCompiled(f4) |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | def test_include_on_stdin(self): |
|---|
| 497 | n/a | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
|---|
| 498 | n/a | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
|---|
| 499 | n/a | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
|---|
| 500 | n/a | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
|---|
| 501 | n/a | p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-'])) |
|---|
| 502 | n/a | p.stdin.write((f3+os.linesep).encode('ascii')) |
|---|
| 503 | n/a | script_helper.kill_python(p) |
|---|
| 504 | n/a | self.assertNotCompiled(f1) |
|---|
| 505 | n/a | self.assertNotCompiled(f2) |
|---|
| 506 | n/a | self.assertCompiled(f3) |
|---|
| 507 | n/a | self.assertNotCompiled(f4) |
|---|
| 508 | n/a | |
|---|
| 509 | n/a | def test_compiles_as_much_as_possible(self): |
|---|
| 510 | n/a | bingfn = script_helper.make_script(self.pkgdir, 'bing', 'syntax(error') |
|---|
| 511 | n/a | rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn, |
|---|
| 512 | n/a | bingfn, self.barfn) |
|---|
| 513 | n/a | self.assertRegex(out, b'rror') |
|---|
| 514 | n/a | self.assertNotCompiled(bingfn) |
|---|
| 515 | n/a | self.assertCompiled(self.initfn) |
|---|
| 516 | n/a | self.assertCompiled(self.barfn) |
|---|
| 517 | n/a | |
|---|
| 518 | n/a | def test_invalid_arg_produces_message(self): |
|---|
| 519 | n/a | out = self.assertRunOK('badfilename') |
|---|
| 520 | n/a | self.assertRegex(out, b"Can't list 'badfilename'") |
|---|
| 521 | n/a | |
|---|
| 522 | n/a | @skipUnless(_have_multiprocessing, "requires multiprocessing") |
|---|
| 523 | n/a | def test_workers(self): |
|---|
| 524 | n/a | bar2fn = script_helper.make_script(self.directory, 'bar2', '') |
|---|
| 525 | n/a | files = [] |
|---|
| 526 | n/a | for suffix in range(5): |
|---|
| 527 | n/a | pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix)) |
|---|
| 528 | n/a | os.mkdir(pkgdir) |
|---|
| 529 | n/a | fn = script_helper.make_script(pkgdir, '__init__', '') |
|---|
| 530 | n/a | files.append(script_helper.make_script(pkgdir, 'bar2', '')) |
|---|
| 531 | n/a | |
|---|
| 532 | n/a | self.assertRunOK(self.directory, '-j', '0') |
|---|
| 533 | n/a | self.assertCompiled(bar2fn) |
|---|
| 534 | n/a | for file in files: |
|---|
| 535 | n/a | self.assertCompiled(file) |
|---|
| 536 | n/a | |
|---|
| 537 | n/a | @mock.patch('compileall.compile_dir') |
|---|
| 538 | n/a | def test_workers_available_cores(self, compile_dir): |
|---|
| 539 | n/a | with mock.patch("sys.argv", |
|---|
| 540 | n/a | new=[sys.executable, self.directory, "-j0"]): |
|---|
| 541 | n/a | compileall.main() |
|---|
| 542 | n/a | self.assertTrue(compile_dir.called) |
|---|
| 543 | n/a | self.assertEqual(compile_dir.call_args[-1]['workers'], None) |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | |
|---|
| 546 | n/a | if __name__ == "__main__": |
|---|
| 547 | n/a | unittest.main() |
|---|