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 |
Petr Viktorin | 4267c98 | 2019-09-26 11:53:51 +0200 | [diff] [blame] | 14 | import errno |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 15 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 16 | from unittest import mock, skipUnless |
| 17 | try: |
| 18 | from concurrent.futures import ProcessPoolExecutor |
| 19 | _have_multiprocessing = True |
| 20 | except ImportError: |
| 21 | _have_multiprocessing = False |
| 22 | |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 23 | from test import support |
| 24 | from test.support import script_helper |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 25 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 26 | from .test_py_compile import without_source_date_epoch |
| 27 | from .test_py_compile import SourceDateEpochTestMeta |
| 28 | |
| 29 | |
| 30 | class CompileallTestsBase: |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 31 | |
| 32 | def setUp(self): |
| 33 | self.directory = tempfile.mkdtemp() |
| 34 | self.source_path = os.path.join(self.directory, '_test.py') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 35 | self.bc_path = importlib.util.cache_from_source(self.source_path) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 36 | with open(self.source_path, 'w') as file: |
| 37 | file.write('x = 123\n') |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 38 | self.source_path2 = os.path.join(self.directory, '_test2.py') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 39 | self.bc_path2 = importlib.util.cache_from_source(self.source_path2) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 40 | shutil.copyfile(self.source_path, self.source_path2) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 41 | self.subdirectory = os.path.join(self.directory, '_subdir') |
| 42 | os.mkdir(self.subdirectory) |
| 43 | self.source_path3 = os.path.join(self.subdirectory, '_test3.py') |
| 44 | shutil.copyfile(self.source_path, self.source_path3) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 45 | |
| 46 | def tearDown(self): |
| 47 | shutil.rmtree(self.directory) |
| 48 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 49 | def add_bad_source_file(self): |
| 50 | self.bad_source_path = os.path.join(self.directory, '_test_bad.py') |
| 51 | with open(self.bad_source_path, 'w') as file: |
| 52 | file.write('x (\n') |
| 53 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 54 | def timestamp_metadata(self): |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 55 | with open(self.bc_path, 'rb') as file: |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 56 | data = file.read(12) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 57 | mtime = int(os.stat(self.source_path).st_mtime) |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 58 | compare = struct.pack('<4sll', importlib.util.MAGIC_NUMBER, 0, mtime) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 59 | return data, compare |
| 60 | |
| 61 | def recreation_check(self, metadata): |
| 62 | """Check that compileall recreates bytecode when the new metadata is |
| 63 | used.""" |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 64 | if os.environ.get('SOURCE_DATE_EPOCH'): |
| 65 | raise unittest.SkipTest('SOURCE_DATE_EPOCH is set') |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 66 | py_compile.compile(self.source_path) |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 67 | self.assertEqual(*self.timestamp_metadata()) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 68 | with open(self.bc_path, 'rb') as file: |
| 69 | bc = file.read()[len(metadata):] |
| 70 | with open(self.bc_path, 'wb') as file: |
| 71 | file.write(metadata) |
| 72 | file.write(bc) |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 73 | self.assertNotEqual(*self.timestamp_metadata()) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 74 | compileall.compile_dir(self.directory, force=False, quiet=True) |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 75 | self.assertTrue(*self.timestamp_metadata()) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 76 | |
| 77 | def test_mtime(self): |
| 78 | # Test a change in mtime leads to a new .pyc. |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 79 | self.recreation_check(struct.pack('<4sll', importlib.util.MAGIC_NUMBER, |
| 80 | 0, 1)) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 81 | |
| 82 | def test_magic_number(self): |
| 83 | # Test a change in mtime leads to a new .pyc. |
| 84 | self.recreation_check(b'\0\0\0\0') |
| 85 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 86 | def test_compile_files(self): |
| 87 | # Test compiling a single file, and complete directory |
| 88 | for fn in (self.bc_path, self.bc_path2): |
| 89 | try: |
| 90 | os.unlink(fn) |
| 91 | except: |
| 92 | pass |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 93 | self.assertTrue(compileall.compile_file(self.source_path, |
| 94 | force=False, quiet=True)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 95 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 96 | not os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 97 | os.unlink(self.bc_path) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 98 | self.assertTrue(compileall.compile_dir(self.directory, force=False, |
| 99 | quiet=True)) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 100 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 101 | os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 102 | os.unlink(self.bc_path) |
| 103 | os.unlink(self.bc_path2) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 104 | # Test against bad files |
| 105 | self.add_bad_source_file() |
| 106 | self.assertFalse(compileall.compile_file(self.bad_source_path, |
| 107 | force=False, quiet=2)) |
| 108 | self.assertFalse(compileall.compile_dir(self.directory, |
| 109 | force=False, quiet=2)) |
| 110 | |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 111 | def test_compile_file_pathlike(self): |
| 112 | self.assertFalse(os.path.isfile(self.bc_path)) |
| 113 | # we should also test the output |
| 114 | with support.captured_stdout() as stdout: |
| 115 | self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path))) |
Berker Peksag | d8e9713 | 2016-10-01 02:44:37 +0300 | [diff] [blame] | 116 | self.assertRegex(stdout.getvalue(), r'Compiling ([^WindowsPath|PosixPath].*)') |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 117 | self.assertTrue(os.path.isfile(self.bc_path)) |
| 118 | |
| 119 | def test_compile_file_pathlike_ddir(self): |
| 120 | self.assertFalse(os.path.isfile(self.bc_path)) |
| 121 | self.assertTrue(compileall.compile_file(pathlib.Path(self.source_path), |
| 122 | ddir=pathlib.Path('ddir_path'), |
| 123 | quiet=2)) |
| 124 | self.assertTrue(os.path.isfile(self.bc_path)) |
| 125 | |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 126 | def test_compile_path(self): |
Berker Peksag | 408b78c | 2016-09-28 17:38:53 +0300 | [diff] [blame] | 127 | with test.test_importlib.util.import_state(path=[self.directory]): |
| 128 | self.assertTrue(compileall.compile_path(quiet=2)) |
Brett Cannon | 1e3c3e9 | 2015-12-27 13:17:04 -0800 | [diff] [blame] | 129 | |
| 130 | with test.test_importlib.util.import_state(path=[self.directory]): |
| 131 | self.add_bad_source_file() |
| 132 | self.assertFalse(compileall.compile_path(skip_curdir=False, |
| 133 | force=True, quiet=2)) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 134 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 135 | def test_no_pycache_in_non_package(self): |
| 136 | # Bug 8563 reported that __pycache__ directories got created by |
| 137 | # compile_file() for non-.py files. |
| 138 | data_dir = os.path.join(self.directory, 'data') |
| 139 | data_file = os.path.join(data_dir, 'file') |
| 140 | os.mkdir(data_dir) |
| 141 | # touch data/file |
| 142 | with open(data_file, 'w'): |
| 143 | pass |
| 144 | compileall.compile_file(data_file) |
| 145 | self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__'))) |
| 146 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 147 | def test_optimize(self): |
| 148 | # make sure compiling with different optimization settings than the |
| 149 | # interpreter's creates the correct file names |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 150 | optimize, opt = (1, 1) if __debug__ else (0, '') |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 151 | compileall.compile_dir(self.directory, quiet=True, optimize=optimize) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 152 | cached = importlib.util.cache_from_source(self.source_path, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 153 | optimization=opt) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 154 | self.assertTrue(os.path.isfile(cached)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 155 | cached2 = importlib.util.cache_from_source(self.source_path2, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 156 | optimization=opt) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 157 | self.assertTrue(os.path.isfile(cached2)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 158 | cached3 = importlib.util.cache_from_source(self.source_path3, |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 159 | optimization=opt) |
Georg Brandl | 4543846 | 2011-02-07 12:36:54 +0000 | [diff] [blame] | 160 | self.assertTrue(os.path.isfile(cached3)) |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 161 | |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 162 | def test_compile_dir_pathlike(self): |
| 163 | self.assertFalse(os.path.isfile(self.bc_path)) |
| 164 | with support.captured_stdout() as stdout: |
| 165 | compileall.compile_dir(pathlib.Path(self.directory)) |
Berker Peksag | d8e9713 | 2016-10-01 02:44:37 +0300 | [diff] [blame] | 166 | line = stdout.getvalue().splitlines()[0] |
| 167 | self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)') |
Berker Peksag | 812a2b6 | 2016-10-01 00:54:18 +0300 | [diff] [blame] | 168 | self.assertTrue(os.path.isfile(self.bc_path)) |
| 169 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 170 | @mock.patch('concurrent.futures.ProcessPoolExecutor') |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 171 | def test_compile_pool_called(self, pool_mock): |
| 172 | compileall.compile_dir(self.directory, quiet=True, workers=5) |
| 173 | self.assertTrue(pool_mock.called) |
| 174 | |
| 175 | def test_compile_workers_non_positive(self): |
| 176 | with self.assertRaisesRegex(ValueError, |
| 177 | "workers must be greater or equal to 0"): |
| 178 | compileall.compile_dir(self.directory, workers=-1) |
| 179 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 180 | @mock.patch('concurrent.futures.ProcessPoolExecutor') |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 181 | def test_compile_workers_cpu_count(self, pool_mock): |
| 182 | compileall.compile_dir(self.directory, quiet=True, workers=0) |
| 183 | self.assertEqual(pool_mock.call_args[1]['max_workers'], None) |
| 184 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 185 | @mock.patch('concurrent.futures.ProcessPoolExecutor') |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 186 | @mock.patch('compileall.compile_file') |
| 187 | def test_compile_one_worker(self, compile_file_mock, pool_mock): |
| 188 | compileall.compile_dir(self.directory, quiet=True) |
| 189 | self.assertFalse(pool_mock.called) |
| 190 | self.assertTrue(compile_file_mock.called) |
| 191 | |
Dustin Spicuzza | 1d817e4 | 2018-11-23 12:06:55 -0500 | [diff] [blame] | 192 | @mock.patch('concurrent.futures.ProcessPoolExecutor', new=None) |
Berker Peksag | d86ef05 | 2015-04-22 09:39:19 +0300 | [diff] [blame] | 193 | @mock.patch('compileall.compile_file') |
| 194 | def test_compile_missing_multiprocessing(self, compile_file_mock): |
| 195 | compileall.compile_dir(self.directory, quiet=True, workers=5) |
| 196 | self.assertTrue(compile_file_mock.called) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 197 | |
Petr Viktorin | 4267c98 | 2019-09-26 11:53:51 +0200 | [diff] [blame] | 198 | def test_compile_dir_maxlevels(self): |
Victor Stinner | eb1dda2 | 2019-10-15 11:26:13 +0200 | [diff] [blame] | 199 | # Test the actual impact of maxlevels parameter |
| 200 | depth = 3 |
| 201 | path = self.directory |
| 202 | for i in range(1, depth + 1): |
| 203 | path = os.path.join(path, f"dir_{i}") |
| 204 | source = os.path.join(path, 'script.py') |
| 205 | os.mkdir(path) |
| 206 | shutil.copyfile(self.source_path, source) |
| 207 | pyc_filename = importlib.util.cache_from_source(source) |
| 208 | |
| 209 | compileall.compile_dir(self.directory, quiet=True, maxlevels=depth - 1) |
| 210 | self.assertFalse(os.path.isfile(pyc_filename)) |
| 211 | |
| 212 | compileall.compile_dir(self.directory, quiet=True, maxlevels=depth) |
| 213 | self.assertTrue(os.path.isfile(pyc_filename)) |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 214 | |
Gregory P. Smith | 0267335 | 2020-02-28 17:28:37 -0800 | [diff] [blame] | 215 | def _test_ddir_only(self, *, ddir, parallel=True): |
| 216 | """Recursive compile_dir ddir must contain package paths; bpo39769.""" |
| 217 | fullpath = ["test", "foo"] |
| 218 | path = self.directory |
| 219 | mods = [] |
| 220 | for subdir in fullpath: |
| 221 | path = os.path.join(path, subdir) |
| 222 | os.mkdir(path) |
| 223 | script_helper.make_script(path, "__init__", "") |
| 224 | mods.append(script_helper.make_script(path, "mod", |
| 225 | "def fn(): 1/0\nfn()\n")) |
| 226 | compileall.compile_dir( |
| 227 | self.directory, quiet=True, ddir=ddir, |
| 228 | workers=2 if parallel else 1) |
| 229 | self.assertTrue(mods) |
| 230 | for mod in mods: |
| 231 | self.assertTrue(mod.startswith(self.directory), mod) |
| 232 | modcode = importlib.util.cache_from_source(mod) |
| 233 | modpath = mod[len(self.directory+os.sep):] |
| 234 | _, _, err = script_helper.assert_python_failure(modcode) |
| 235 | expected_in = os.path.join(ddir, modpath) |
| 236 | mod_code_obj = test.test_importlib.util.get_code_from_pyc(modcode) |
| 237 | self.assertEqual(mod_code_obj.co_filename, expected_in) |
| 238 | self.assertIn(f'"{expected_in}"', os.fsdecode(err)) |
| 239 | |
| 240 | def test_ddir_only_one_worker(self): |
| 241 | """Recursive compile_dir ddir= contains package paths; bpo39769.""" |
| 242 | return self._test_ddir_only(ddir="<a prefix>", parallel=False) |
| 243 | |
| 244 | def test_ddir_multiple_workers(self): |
| 245 | """Recursive compile_dir ddir= contains package paths; bpo39769.""" |
| 246 | return self._test_ddir_only(ddir="<a prefix>", parallel=True) |
| 247 | |
| 248 | def test_ddir_empty_only_one_worker(self): |
| 249 | """Recursive compile_dir ddir='' contains package paths; bpo39769.""" |
| 250 | return self._test_ddir_only(ddir="", parallel=False) |
| 251 | |
| 252 | def test_ddir_empty_multiple_workers(self): |
| 253 | """Recursive compile_dir ddir='' contains package paths; bpo39769.""" |
| 254 | return self._test_ddir_only(ddir="", parallel=True) |
| 255 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 256 | def test_strip_only(self): |
| 257 | fullpath = ["test", "build", "real", "path"] |
| 258 | path = os.path.join(self.directory, *fullpath) |
| 259 | os.makedirs(path) |
| 260 | script = script_helper.make_script(path, "test", "1 / 0") |
| 261 | bc = importlib.util.cache_from_source(script) |
| 262 | stripdir = os.path.join(self.directory, *fullpath[:2]) |
| 263 | compileall.compile_dir(path, quiet=True, stripdir=stripdir) |
| 264 | rc, out, err = script_helper.assert_python_failure(bc) |
| 265 | expected_in = os.path.join(*fullpath[2:]) |
| 266 | self.assertIn( |
| 267 | expected_in, |
| 268 | str(err, encoding=sys.getdefaultencoding()) |
| 269 | ) |
| 270 | self.assertNotIn( |
| 271 | stripdir, |
| 272 | str(err, encoding=sys.getdefaultencoding()) |
| 273 | ) |
| 274 | |
| 275 | def test_prepend_only(self): |
| 276 | fullpath = ["test", "build", "real", "path"] |
| 277 | path = os.path.join(self.directory, *fullpath) |
| 278 | os.makedirs(path) |
| 279 | script = script_helper.make_script(path, "test", "1 / 0") |
| 280 | bc = importlib.util.cache_from_source(script) |
| 281 | prependdir = "/foo" |
| 282 | compileall.compile_dir(path, quiet=True, prependdir=prependdir) |
| 283 | rc, out, err = script_helper.assert_python_failure(bc) |
| 284 | expected_in = os.path.join(prependdir, self.directory, *fullpath) |
| 285 | self.assertIn( |
| 286 | expected_in, |
| 287 | str(err, encoding=sys.getdefaultencoding()) |
| 288 | ) |
| 289 | |
| 290 | def test_strip_and_prepend(self): |
| 291 | fullpath = ["test", "build", "real", "path"] |
| 292 | path = os.path.join(self.directory, *fullpath) |
| 293 | os.makedirs(path) |
| 294 | script = script_helper.make_script(path, "test", "1 / 0") |
| 295 | bc = importlib.util.cache_from_source(script) |
| 296 | stripdir = os.path.join(self.directory, *fullpath[:2]) |
| 297 | prependdir = "/foo" |
| 298 | compileall.compile_dir(path, quiet=True, |
| 299 | stripdir=stripdir, prependdir=prependdir) |
| 300 | rc, out, err = script_helper.assert_python_failure(bc) |
| 301 | expected_in = os.path.join(prependdir, *fullpath[2:]) |
| 302 | self.assertIn( |
| 303 | expected_in, |
| 304 | str(err, encoding=sys.getdefaultencoding()) |
| 305 | ) |
| 306 | self.assertNotIn( |
| 307 | stripdir, |
| 308 | str(err, encoding=sys.getdefaultencoding()) |
| 309 | ) |
| 310 | |
| 311 | def test_strip_prepend_and_ddir(self): |
| 312 | fullpath = ["test", "build", "real", "path", "ddir"] |
| 313 | path = os.path.join(self.directory, *fullpath) |
| 314 | os.makedirs(path) |
| 315 | script_helper.make_script(path, "test", "1 / 0") |
| 316 | with self.assertRaises(ValueError): |
| 317 | compileall.compile_dir(path, quiet=True, ddir="/bar", |
| 318 | stripdir="/foo", prependdir="/bar") |
| 319 | |
| 320 | def test_multiple_optimization_levels(self): |
| 321 | script = script_helper.make_script(self.directory, |
| 322 | "test_optimization", |
| 323 | "a = 0") |
| 324 | bc = [] |
| 325 | for opt_level in "", 1, 2, 3: |
| 326 | bc.append(importlib.util.cache_from_source(script, |
| 327 | optimization=opt_level)) |
| 328 | test_combinations = [[0, 1], [1, 2], [0, 2], [0, 1, 2]] |
| 329 | for opt_combination in test_combinations: |
| 330 | compileall.compile_file(script, quiet=True, |
| 331 | optimize=opt_combination) |
| 332 | for opt_level in opt_combination: |
| 333 | self.assertTrue(os.path.isfile(bc[opt_level])) |
| 334 | try: |
| 335 | os.unlink(bc[opt_level]) |
| 336 | except Exception: |
| 337 | pass |
| 338 | |
| 339 | @support.skip_unless_symlink |
| 340 | def test_ignore_symlink_destination(self): |
| 341 | # Create folders for allowed files, symlinks and prohibited area |
| 342 | allowed_path = os.path.join(self.directory, "test", "dir", "allowed") |
| 343 | symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks") |
| 344 | prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited") |
| 345 | os.makedirs(allowed_path) |
| 346 | os.makedirs(symlinks_path) |
| 347 | os.makedirs(prohibited_path) |
| 348 | |
| 349 | # Create scripts and symlinks and remember their byte-compiled versions |
| 350 | allowed_script = script_helper.make_script(allowed_path, "test_allowed", "a = 0") |
| 351 | prohibited_script = script_helper.make_script(prohibited_path, "test_prohibited", "a = 0") |
| 352 | allowed_symlink = os.path.join(symlinks_path, "test_allowed.py") |
| 353 | prohibited_symlink = os.path.join(symlinks_path, "test_prohibited.py") |
| 354 | os.symlink(allowed_script, allowed_symlink) |
| 355 | os.symlink(prohibited_script, prohibited_symlink) |
| 356 | allowed_bc = importlib.util.cache_from_source(allowed_symlink) |
| 357 | prohibited_bc = importlib.util.cache_from_source(prohibited_symlink) |
| 358 | |
| 359 | compileall.compile_dir(symlinks_path, quiet=True, limit_sl_dest=allowed_path) |
| 360 | |
| 361 | self.assertTrue(os.path.isfile(allowed_bc)) |
| 362 | self.assertFalse(os.path.isfile(prohibited_bc)) |
| 363 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 364 | |
| 365 | class CompileallTestsWithSourceEpoch(CompileallTestsBase, |
| 366 | unittest.TestCase, |
| 367 | metaclass=SourceDateEpochTestMeta, |
| 368 | source_date_epoch=True): |
| 369 | pass |
| 370 | |
| 371 | |
| 372 | class CompileallTestsWithoutSourceEpoch(CompileallTestsBase, |
| 373 | unittest.TestCase, |
| 374 | metaclass=SourceDateEpochTestMeta, |
| 375 | source_date_epoch=False): |
| 376 | pass |
| 377 | |
| 378 | |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 379 | class EncodingTest(unittest.TestCase): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 380 | """Issue 6716: compileall should escape source code when printing errors |
| 381 | to stdout.""" |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 382 | |
| 383 | def setUp(self): |
| 384 | self.directory = tempfile.mkdtemp() |
| 385 | self.source_path = os.path.join(self.directory, '_test.py') |
| 386 | with open(self.source_path, 'w', encoding='utf-8') as file: |
| 387 | file.write('# -*- coding: utf-8 -*-\n') |
| 388 | file.write('print u"\u20ac"\n') |
| 389 | |
| 390 | def tearDown(self): |
| 391 | shutil.rmtree(self.directory) |
| 392 | |
| 393 | def test_error(self): |
| 394 | try: |
| 395 | orig_stdout = sys.stdout |
| 396 | sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii') |
| 397 | compileall.compile_dir(self.directory) |
| 398 | finally: |
| 399 | sys.stdout = orig_stdout |
| 400 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 401 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 402 | class CommandLineTestsBase: |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 403 | """Test compileall's CLI.""" |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 404 | |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 405 | @classmethod |
| 406 | def setUpClass(cls): |
| 407 | for path in filter(os.path.isdir, sys.path): |
| 408 | directory_created = False |
| 409 | directory = pathlib.Path(path) / '__pycache__' |
| 410 | path = directory / 'test.try' |
| 411 | try: |
| 412 | if not directory.is_dir(): |
| 413 | directory.mkdir() |
| 414 | directory_created = True |
| 415 | with path.open('w') as file: |
| 416 | file.write('# for test_compileall') |
| 417 | except OSError: |
| 418 | sys_path_writable = False |
| 419 | break |
| 420 | finally: |
| 421 | support.unlink(str(path)) |
| 422 | if directory_created: |
| 423 | directory.rmdir() |
| 424 | else: |
| 425 | sys_path_writable = True |
| 426 | cls._sys_path_writable = sys_path_writable |
| 427 | |
| 428 | def _skip_if_sys_path_not_writable(self): |
| 429 | if not self._sys_path_writable: |
| 430 | raise unittest.SkipTest('not all entries on sys.path are writable') |
| 431 | |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 432 | def _get_run_args(self, args): |
Victor Stinner | 9def284 | 2016-01-18 12:15:08 +0100 | [diff] [blame] | 433 | return [*support.optim_args_from_interpreter_flags(), |
| 434 | '-S', '-m', 'compileall', |
| 435 | *args] |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 436 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 437 | def assertRunOK(self, *args, **env_vars): |
| 438 | rc, out, err = script_helper.assert_python_ok( |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 439 | *self._get_run_args(args), **env_vars) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 440 | self.assertEqual(b'', err) |
| 441 | return out |
| 442 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 443 | def assertRunNotOK(self, *args, **env_vars): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 444 | rc, out, err = script_helper.assert_python_failure( |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 445 | *self._get_run_args(args), **env_vars) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 446 | return rc, out, err |
| 447 | |
| 448 | def assertCompiled(self, fn): |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 449 | path = importlib.util.cache_from_source(fn) |
| 450 | self.assertTrue(os.path.exists(path)) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 451 | |
| 452 | def assertNotCompiled(self, fn): |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 453 | path = importlib.util.cache_from_source(fn) |
| 454 | self.assertFalse(os.path.exists(path)) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 455 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 456 | def setUp(self): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 457 | self.directory = tempfile.mkdtemp() |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 458 | self.addCleanup(support.rmtree, self.directory) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 459 | self.pkgdir = os.path.join(self.directory, 'foo') |
| 460 | os.mkdir(self.pkgdir) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 461 | self.pkgdir_cachedir = os.path.join(self.pkgdir, '__pycache__') |
| 462 | # Create the __init__.py and a package module. |
| 463 | self.initfn = script_helper.make_script(self.pkgdir, '__init__', '') |
| 464 | self.barfn = script_helper.make_script(self.pkgdir, 'bar', '') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 465 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 466 | def test_no_args_compiles_path(self): |
| 467 | # Note that -l is implied for the no args case. |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 468 | self._skip_if_sys_path_not_writable() |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 469 | bazfn = script_helper.make_script(self.directory, 'baz', '') |
| 470 | self.assertRunOK(PYTHONPATH=self.directory) |
| 471 | self.assertCompiled(bazfn) |
| 472 | self.assertNotCompiled(self.initfn) |
| 473 | self.assertNotCompiled(self.barfn) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 474 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 475 | @without_source_date_epoch # timestamp invalidation test |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 476 | def test_no_args_respects_force_flag(self): |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 477 | self._skip_if_sys_path_not_writable() |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 478 | bazfn = script_helper.make_script(self.directory, 'baz', '') |
| 479 | self.assertRunOK(PYTHONPATH=self.directory) |
R David Murray | 755d5ea | 2013-12-15 20:56:00 -0500 | [diff] [blame] | 480 | pycpath = importlib.util.cache_from_source(bazfn) |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 481 | # Set atime/mtime backward to avoid file timestamp resolution issues |
| 482 | os.utime(pycpath, (time.time()-60,)*2) |
| 483 | mtime = os.stat(pycpath).st_mtime |
| 484 | # Without force, no recompilation |
| 485 | self.assertRunOK(PYTHONPATH=self.directory) |
| 486 | mtime2 = os.stat(pycpath).st_mtime |
| 487 | self.assertEqual(mtime, mtime2) |
| 488 | # Now force it. |
| 489 | self.assertRunOK('-f', PYTHONPATH=self.directory) |
| 490 | mtime2 = os.stat(pycpath).st_mtime |
| 491 | self.assertNotEqual(mtime, mtime2) |
| 492 | |
| 493 | def test_no_args_respects_quiet_flag(self): |
Brett Cannon | 65ed750 | 2015-10-09 15:09:43 -0700 | [diff] [blame] | 494 | self._skip_if_sys_path_not_writable() |
R David Murray | 8a1d1e6 | 2013-12-15 20:49:38 -0500 | [diff] [blame] | 495 | script_helper.make_script(self.directory, 'baz', '') |
| 496 | noisy = self.assertRunOK(PYTHONPATH=self.directory) |
| 497 | self.assertIn(b'Listing ', noisy) |
| 498 | quiet = self.assertRunOK('-q', PYTHONPATH=self.directory) |
| 499 | self.assertNotIn(b'Listing ', quiet) |
| 500 | |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 501 | # Ensure that the default behavior of compileall's CLI is to create |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 502 | # PEP 3147/PEP 488 pyc files. |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 503 | for name, ext, switch in [ |
| 504 | ('normal', 'pyc', []), |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 505 | ('optimize', 'opt-1.pyc', ['-O']), |
| 506 | ('doubleoptimize', 'opt-2.pyc', ['-OO']), |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 507 | ]: |
| 508 | def f(self, ext=ext, switch=switch): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 509 | script_helper.assert_python_ok(*(switch + |
| 510 | ['-m', 'compileall', '-q', self.pkgdir])) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 511 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 512 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 513 | expected = sorted(base.format(sys.implementation.cache_tag, ext) |
| 514 | for base in ('__init__.{}.{}', 'bar.{}.{}')) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 515 | self.assertEqual(sorted(os.listdir(self.pkgdir_cachedir)), expected) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 516 | # Make sure there are no .pyc files in the source directory. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 517 | self.assertFalse([fn for fn in os.listdir(self.pkgdir) |
| 518 | if fn.endswith(ext)]) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 519 | locals()['test_pep3147_paths_' + name] = f |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 520 | |
| 521 | def test_legacy_paths(self): |
| 522 | # Ensure that with the proper switch, compileall leaves legacy |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 523 | # pyc files, and no __pycache__ directory. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 524 | self.assertRunOK('-b', '-q', self.pkgdir) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 525 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 526 | self.assertFalse(os.path.exists(self.pkgdir_cachedir)) |
Brett Cannon | f299abd | 2015-04-13 14:21:02 -0400 | [diff] [blame] | 527 | expected = sorted(['__init__.py', '__init__.pyc', 'bar.py', |
| 528 | 'bar.pyc']) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 529 | self.assertEqual(sorted(os.listdir(self.pkgdir)), expected) |
| 530 | |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 531 | def test_multiple_runs(self): |
| 532 | # Bug 8527 reported that multiple calls produced empty |
| 533 | # __pycache__/__pycache__ directories. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 534 | self.assertRunOK('-q', self.pkgdir) |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 535 | # Verify the __pycache__ directory contents. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 536 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
| 537 | cachecachedir = os.path.join(self.pkgdir_cachedir, '__pycache__') |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 538 | self.assertFalse(os.path.exists(cachecachedir)) |
| 539 | # Call compileall again. |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 540 | self.assertRunOK('-q', self.pkgdir) |
| 541 | self.assertTrue(os.path.exists(self.pkgdir_cachedir)) |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 542 | self.assertFalse(os.path.exists(cachecachedir)) |
| 543 | |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 544 | @without_source_date_epoch # timestamp invalidation test |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 545 | def test_force(self): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 546 | self.assertRunOK('-q', self.pkgdir) |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 547 | pycpath = importlib.util.cache_from_source(self.barfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 548 | # set atime/mtime backward to avoid file timestamp resolution issues |
| 549 | os.utime(pycpath, (time.time()-60,)*2) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 550 | mtime = os.stat(pycpath).st_mtime |
| 551 | # without force, no recompilation |
| 552 | self.assertRunOK('-q', self.pkgdir) |
| 553 | mtime2 = os.stat(pycpath).st_mtime |
| 554 | self.assertEqual(mtime, mtime2) |
| 555 | # now force it. |
| 556 | self.assertRunOK('-q', '-f', self.pkgdir) |
| 557 | mtime2 = os.stat(pycpath).st_mtime |
| 558 | self.assertNotEqual(mtime, mtime2) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 559 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 560 | def test_recursion_control(self): |
| 561 | subpackage = os.path.join(self.pkgdir, 'spam') |
| 562 | os.mkdir(subpackage) |
| 563 | subinitfn = script_helper.make_script(subpackage, '__init__', '') |
| 564 | hamfn = script_helper.make_script(subpackage, 'ham', '') |
| 565 | self.assertRunOK('-q', '-l', self.pkgdir) |
| 566 | self.assertNotCompiled(subinitfn) |
| 567 | self.assertFalse(os.path.exists(os.path.join(subpackage, '__pycache__'))) |
| 568 | self.assertRunOK('-q', self.pkgdir) |
| 569 | self.assertCompiled(subinitfn) |
| 570 | self.assertCompiled(hamfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 571 | |
Benjamin Peterson | 344ff4a | 2014-08-19 16:13:26 -0500 | [diff] [blame] | 572 | def test_recursion_limit(self): |
| 573 | subpackage = os.path.join(self.pkgdir, 'spam') |
| 574 | subpackage2 = os.path.join(subpackage, 'ham') |
| 575 | subpackage3 = os.path.join(subpackage2, 'eggs') |
| 576 | for pkg in (subpackage, subpackage2, subpackage3): |
| 577 | script_helper.make_pkg(pkg) |
| 578 | |
| 579 | subinitfn = os.path.join(subpackage, '__init__.py') |
| 580 | hamfn = script_helper.make_script(subpackage, 'ham', '') |
| 581 | spamfn = script_helper.make_script(subpackage2, 'spam', '') |
| 582 | eggfn = script_helper.make_script(subpackage3, 'egg', '') |
| 583 | |
| 584 | self.assertRunOK('-q', '-r 0', self.pkgdir) |
| 585 | self.assertNotCompiled(subinitfn) |
| 586 | self.assertFalse( |
| 587 | os.path.exists(os.path.join(subpackage, '__pycache__'))) |
| 588 | |
| 589 | self.assertRunOK('-q', '-r 1', self.pkgdir) |
| 590 | self.assertCompiled(subinitfn) |
| 591 | self.assertCompiled(hamfn) |
| 592 | self.assertNotCompiled(spamfn) |
| 593 | |
| 594 | self.assertRunOK('-q', '-r 2', self.pkgdir) |
| 595 | self.assertCompiled(subinitfn) |
| 596 | self.assertCompiled(hamfn) |
| 597 | self.assertCompiled(spamfn) |
| 598 | self.assertNotCompiled(eggfn) |
| 599 | |
| 600 | self.assertRunOK('-q', '-r 5', self.pkgdir) |
| 601 | self.assertCompiled(subinitfn) |
| 602 | self.assertCompiled(hamfn) |
| 603 | self.assertCompiled(spamfn) |
| 604 | self.assertCompiled(eggfn) |
| 605 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 606 | @support.skip_unless_symlink |
| 607 | def test_symlink_loop(self): |
| 608 | # Currently, compileall ignores symlinks to directories. |
| 609 | # If that limitation is ever lifted, it should protect against |
| 610 | # recursion in symlink loops. |
| 611 | pkg = os.path.join(self.pkgdir, 'spam') |
| 612 | script_helper.make_pkg(pkg) |
| 613 | os.symlink('.', os.path.join(pkg, 'evil')) |
| 614 | os.symlink('.', os.path.join(pkg, 'evil2')) |
| 615 | self.assertRunOK('-q', self.pkgdir) |
| 616 | self.assertCompiled(os.path.join( |
| 617 | self.pkgdir, 'spam', 'evil', 'evil2', '__init__.py' |
| 618 | )) |
| 619 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 620 | def test_quiet(self): |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 621 | noisy = self.assertRunOK(self.pkgdir) |
| 622 | quiet = self.assertRunOK('-q', self.pkgdir) |
| 623 | self.assertNotEqual(b'', noisy) |
| 624 | self.assertEqual(b'', quiet) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 625 | |
Berker Peksag | 6554b86 | 2014-10-15 11:10:57 +0300 | [diff] [blame] | 626 | def test_silent(self): |
| 627 | script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax') |
| 628 | _, quiet, _ = self.assertRunNotOK('-q', self.pkgdir) |
| 629 | _, silent, _ = self.assertRunNotOK('-qq', self.pkgdir) |
| 630 | self.assertNotEqual(b'', quiet) |
| 631 | self.assertEqual(b'', silent) |
| 632 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 633 | def test_regexp(self): |
R David Murray | ee1a7cb | 2011-07-01 14:55:43 -0400 | [diff] [blame] | 634 | self.assertRunOK('-q', '-x', r'ba[^\\/]*$', self.pkgdir) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 635 | self.assertNotCompiled(self.barfn) |
| 636 | self.assertCompiled(self.initfn) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 637 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 638 | def test_multiple_dirs(self): |
| 639 | pkgdir2 = os.path.join(self.directory, 'foo2') |
| 640 | os.mkdir(pkgdir2) |
| 641 | init2fn = script_helper.make_script(pkgdir2, '__init__', '') |
| 642 | bar2fn = script_helper.make_script(pkgdir2, 'bar2', '') |
| 643 | self.assertRunOK('-q', self.pkgdir, pkgdir2) |
| 644 | self.assertCompiled(self.initfn) |
| 645 | self.assertCompiled(self.barfn) |
| 646 | self.assertCompiled(init2fn) |
| 647 | self.assertCompiled(bar2fn) |
| 648 | |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 649 | def test_d_compile_error(self): |
| 650 | script_helper.make_script(self.pkgdir, 'crunchyfrog', 'bad(syntax') |
| 651 | rc, out, err = self.assertRunNotOK('-q', '-d', 'dinsdale', self.pkgdir) |
| 652 | self.assertRegex(out, b'File "dinsdale') |
| 653 | |
| 654 | def test_d_runtime_error(self): |
| 655 | bazfn = script_helper.make_script(self.pkgdir, 'baz', 'raise Exception') |
| 656 | self.assertRunOK('-q', '-d', 'dinsdale', self.pkgdir) |
| 657 | fn = script_helper.make_script(self.pkgdir, 'bing', 'import baz') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 658 | pyc = importlib.util.cache_from_source(bazfn) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 659 | os.rename(pyc, os.path.join(self.pkgdir, 'baz.pyc')) |
| 660 | os.remove(bazfn) |
Victor Stinner | e8785ff | 2013-10-12 14:44:01 +0200 | [diff] [blame] | 661 | rc, out, err = script_helper.assert_python_failure(fn, __isolated=False) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 662 | self.assertRegex(err, b'File "dinsdale') |
| 663 | |
| 664 | def test_include_bad_file(self): |
| 665 | rc, out, err = self.assertRunNotOK( |
| 666 | '-i', os.path.join(self.directory, 'nosuchfile'), self.pkgdir) |
| 667 | self.assertRegex(out, b'rror.*nosuchfile') |
| 668 | self.assertNotRegex(err, b'Traceback') |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 669 | self.assertFalse(os.path.exists(importlib.util.cache_from_source( |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 670 | self.pkgdir_cachedir))) |
| 671 | |
| 672 | def test_include_file_with_arg(self): |
| 673 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 674 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 675 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 676 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
| 677 | with open(os.path.join(self.directory, 'l1'), 'w') as l1: |
| 678 | l1.write(os.path.join(self.pkgdir, 'f1.py')+os.linesep) |
| 679 | l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep) |
| 680 | self.assertRunOK('-i', os.path.join(self.directory, 'l1'), f4) |
| 681 | self.assertCompiled(f1) |
| 682 | self.assertCompiled(f2) |
| 683 | self.assertNotCompiled(f3) |
| 684 | self.assertCompiled(f4) |
| 685 | |
| 686 | def test_include_file_no_arg(self): |
| 687 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 688 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 689 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 690 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
| 691 | with open(os.path.join(self.directory, 'l1'), 'w') as l1: |
| 692 | l1.write(os.path.join(self.pkgdir, 'f2.py')+os.linesep) |
| 693 | self.assertRunOK('-i', os.path.join(self.directory, 'l1')) |
| 694 | self.assertNotCompiled(f1) |
| 695 | self.assertCompiled(f2) |
| 696 | self.assertNotCompiled(f3) |
| 697 | self.assertNotCompiled(f4) |
| 698 | |
| 699 | def test_include_on_stdin(self): |
| 700 | f1 = script_helper.make_script(self.pkgdir, 'f1', '') |
| 701 | f2 = script_helper.make_script(self.pkgdir, 'f2', '') |
| 702 | f3 = script_helper.make_script(self.pkgdir, 'f3', '') |
| 703 | f4 = script_helper.make_script(self.pkgdir, 'f4', '') |
Benjamin Peterson | a820c7c | 2012-09-25 11:42:35 -0400 | [diff] [blame] | 704 | p = script_helper.spawn_python(*(self._get_run_args(()) + ['-i', '-'])) |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 705 | p.stdin.write((f3+os.linesep).encode('ascii')) |
| 706 | script_helper.kill_python(p) |
| 707 | self.assertNotCompiled(f1) |
| 708 | self.assertNotCompiled(f2) |
| 709 | self.assertCompiled(f3) |
| 710 | self.assertNotCompiled(f4) |
| 711 | |
| 712 | def test_compiles_as_much_as_possible(self): |
| 713 | bingfn = script_helper.make_script(self.pkgdir, 'bing', 'syntax(error') |
| 714 | rc, out, err = self.assertRunNotOK('nosuchfile', self.initfn, |
| 715 | bingfn, self.barfn) |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 716 | self.assertRegex(out, b'rror') |
R. David Murray | 95333e3 | 2010-12-14 22:32:50 +0000 | [diff] [blame] | 717 | self.assertNotCompiled(bingfn) |
| 718 | self.assertCompiled(self.initfn) |
| 719 | self.assertCompiled(self.barfn) |
| 720 | |
R. David Murray | 5317e9c | 2010-12-16 19:08:51 +0000 | [diff] [blame] | 721 | def test_invalid_arg_produces_message(self): |
| 722 | out = self.assertRunOK('badfilename') |
Victor Stinner | 5307126 | 2011-05-11 00:36:28 +0200 | [diff] [blame] | 723 | self.assertRegex(out, b"Can't list 'badfilename'") |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 724 | |
Benjamin Peterson | 42aa93b | 2017-12-09 10:26:52 -0800 | [diff] [blame] | 725 | def test_pyc_invalidation_mode(self): |
| 726 | script_helper.make_script(self.pkgdir, 'f1', '') |
| 727 | pyc = importlib.util.cache_from_source( |
| 728 | os.path.join(self.pkgdir, 'f1.py')) |
| 729 | self.assertRunOK('--invalidation-mode=checked-hash', self.pkgdir) |
| 730 | with open(pyc, 'rb') as fp: |
| 731 | data = fp.read() |
| 732 | self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b11) |
| 733 | self.assertRunOK('--invalidation-mode=unchecked-hash', self.pkgdir) |
| 734 | with open(pyc, 'rb') as fp: |
| 735 | data = fp.read() |
| 736 | self.assertEqual(int.from_bytes(data[4:8], 'little'), 0b01) |
| 737 | |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 738 | @skipUnless(_have_multiprocessing, "requires multiprocessing") |
| 739 | def test_workers(self): |
| 740 | bar2fn = script_helper.make_script(self.directory, 'bar2', '') |
| 741 | files = [] |
| 742 | for suffix in range(5): |
| 743 | pkgdir = os.path.join(self.directory, 'foo{}'.format(suffix)) |
| 744 | os.mkdir(pkgdir) |
| 745 | fn = script_helper.make_script(pkgdir, '__init__', '') |
| 746 | files.append(script_helper.make_script(pkgdir, 'bar2', '')) |
| 747 | |
| 748 | self.assertRunOK(self.directory, '-j', '0') |
| 749 | self.assertCompiled(bar2fn) |
| 750 | for file in files: |
| 751 | self.assertCompiled(file) |
| 752 | |
| 753 | @mock.patch('compileall.compile_dir') |
| 754 | def test_workers_available_cores(self, compile_dir): |
| 755 | with mock.patch("sys.argv", |
| 756 | new=[sys.executable, self.directory, "-j0"]): |
| 757 | compileall.main() |
| 758 | self.assertTrue(compile_dir.called) |
Antoine Pitrou | 1a2dd82 | 2019-05-15 23:45:18 +0200 | [diff] [blame] | 759 | self.assertEqual(compile_dir.call_args[-1]['workers'], 0) |
Brett Cannon | f1a8df0 | 2014-09-12 10:39:48 -0400 | [diff] [blame] | 760 | |
Lumír 'Frenzy' Balhar | 8e7bb99 | 2019-09-26 08:28:26 +0200 | [diff] [blame] | 761 | def test_strip_and_prepend(self): |
| 762 | fullpath = ["test", "build", "real", "path"] |
| 763 | path = os.path.join(self.directory, *fullpath) |
| 764 | os.makedirs(path) |
| 765 | script = script_helper.make_script(path, "test", "1 / 0") |
| 766 | bc = importlib.util.cache_from_source(script) |
| 767 | stripdir = os.path.join(self.directory, *fullpath[:2]) |
| 768 | prependdir = "/foo" |
| 769 | self.assertRunOK("-s", stripdir, "-p", prependdir, path) |
| 770 | rc, out, err = script_helper.assert_python_failure(bc) |
| 771 | expected_in = os.path.join(prependdir, *fullpath[2:]) |
| 772 | self.assertIn( |
| 773 | expected_in, |
| 774 | str(err, encoding=sys.getdefaultencoding()) |
| 775 | ) |
| 776 | self.assertNotIn( |
| 777 | stripdir, |
| 778 | str(err, encoding=sys.getdefaultencoding()) |
| 779 | ) |
| 780 | |
| 781 | def test_multiple_optimization_levels(self): |
| 782 | path = os.path.join(self.directory, "optimizations") |
| 783 | os.makedirs(path) |
| 784 | script = script_helper.make_script(path, |
| 785 | "test_optimization", |
| 786 | "a = 0") |
| 787 | bc = [] |
| 788 | for opt_level in "", 1, 2, 3: |
| 789 | bc.append(importlib.util.cache_from_source(script, |
| 790 | optimization=opt_level)) |
| 791 | test_combinations = [["0", "1"], |
| 792 | ["1", "2"], |
| 793 | ["0", "2"], |
| 794 | ["0", "1", "2"]] |
| 795 | for opt_combination in test_combinations: |
| 796 | self.assertRunOK(path, *("-o" + str(n) for n in opt_combination)) |
| 797 | for opt_level in opt_combination: |
| 798 | self.assertTrue(os.path.isfile(bc[int(opt_level)])) |
| 799 | try: |
| 800 | os.unlink(bc[opt_level]) |
| 801 | except Exception: |
| 802 | pass |
| 803 | |
| 804 | @support.skip_unless_symlink |
| 805 | def test_ignore_symlink_destination(self): |
| 806 | # Create folders for allowed files, symlinks and prohibited area |
| 807 | allowed_path = os.path.join(self.directory, "test", "dir", "allowed") |
| 808 | symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks") |
| 809 | prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited") |
| 810 | os.makedirs(allowed_path) |
| 811 | os.makedirs(symlinks_path) |
| 812 | os.makedirs(prohibited_path) |
| 813 | |
| 814 | # Create scripts and symlinks and remember their byte-compiled versions |
| 815 | allowed_script = script_helper.make_script(allowed_path, "test_allowed", "a = 0") |
| 816 | prohibited_script = script_helper.make_script(prohibited_path, "test_prohibited", "a = 0") |
| 817 | allowed_symlink = os.path.join(symlinks_path, "test_allowed.py") |
| 818 | prohibited_symlink = os.path.join(symlinks_path, "test_prohibited.py") |
| 819 | os.symlink(allowed_script, allowed_symlink) |
| 820 | os.symlink(prohibited_script, prohibited_symlink) |
| 821 | allowed_bc = importlib.util.cache_from_source(allowed_symlink) |
| 822 | prohibited_bc = importlib.util.cache_from_source(prohibited_symlink) |
| 823 | |
| 824 | self.assertRunOK(symlinks_path, "-e", allowed_path) |
| 825 | |
| 826 | self.assertTrue(os.path.isfile(allowed_bc)) |
| 827 | self.assertFalse(os.path.isfile(prohibited_bc)) |
| 828 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 829 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 830 | class CommandLineTestsWithSourceEpoch(CommandLineTestsBase, |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 831 | unittest.TestCase, |
| 832 | metaclass=SourceDateEpochTestMeta, |
| 833 | source_date_epoch=True): |
| 834 | pass |
| 835 | |
| 836 | |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 837 | class CommandLineTestsNoSourceEpoch(CommandLineTestsBase, |
Elvis Pranskevichus | a6b3ec5 | 2018-10-10 12:43:14 -0400 | [diff] [blame] | 838 | unittest.TestCase, |
| 839 | metaclass=SourceDateEpochTestMeta, |
| 840 | source_date_epoch=False): |
| 841 | pass |
| 842 | |
| 843 | |
| 844 | |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 845 | if __name__ == "__main__": |
Brett Cannon | 7822e12 | 2013-06-14 23:04:02 -0400 | [diff] [blame] | 846 | unittest.main() |