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