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 |
| 3 | import imp |
| 4 | import os |
| 5 | import py_compile |
| 6 | import shutil |
| 7 | import struct |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 8 | import subprocess |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 9 | import tempfile |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 10 | import time |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 11 | import unittest |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 12 | import io |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 13 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 14 | from test import support |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 15 | |
| 16 | class CompileallTests(unittest.TestCase): |
| 17 | |
| 18 | def setUp(self): |
| 19 | self.directory = tempfile.mkdtemp() |
| 20 | self.source_path = os.path.join(self.directory, '_test.py') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 21 | self.bc_path = imp.cache_from_source(self.source_path) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 22 | with open(self.source_path, 'w') as file: |
| 23 | file.write('x = 123\n') |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 24 | self.source_path2 = os.path.join(self.directory, '_test2.py') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 25 | self.bc_path2 = imp.cache_from_source(self.source_path2) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 26 | shutil.copyfile(self.source_path, self.source_path2) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 27 | |
| 28 | def tearDown(self): |
| 29 | shutil.rmtree(self.directory) |
| 30 | |
| 31 | def data(self): |
| 32 | with open(self.bc_path, 'rb') as file: |
| 33 | data = file.read(8) |
| 34 | mtime = int(os.stat(self.source_path).st_mtime) |
| 35 | compare = struct.pack('<4sl', imp.get_magic(), mtime) |
| 36 | return data, compare |
| 37 | |
| 38 | def recreation_check(self, metadata): |
| 39 | """Check that compileall recreates bytecode when the new metadata is |
| 40 | used.""" |
| 41 | if not hasattr(os, 'stat'): |
| 42 | return |
| 43 | py_compile.compile(self.source_path) |
| 44 | self.assertEqual(*self.data()) |
| 45 | with open(self.bc_path, 'rb') as file: |
| 46 | bc = file.read()[len(metadata):] |
| 47 | with open(self.bc_path, 'wb') as file: |
| 48 | file.write(metadata) |
| 49 | file.write(bc) |
| 50 | self.assertNotEqual(*self.data()) |
| 51 | compileall.compile_dir(self.directory, force=False, quiet=True) |
| 52 | self.assertTrue(*self.data()) |
| 53 | |
| 54 | def test_mtime(self): |
| 55 | # Test a change in mtime leads to a new .pyc. |
| 56 | self.recreation_check(struct.pack('<4sl', imp.get_magic(), 1)) |
| 57 | |
| 58 | def test_magic_number(self): |
| 59 | # Test a change in mtime leads to a new .pyc. |
| 60 | self.recreation_check(b'\0\0\0\0') |
| 61 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 62 | def test_compile_files(self): |
| 63 | # Test compiling a single file, and complete directory |
| 64 | for fn in (self.bc_path, self.bc_path2): |
| 65 | try: |
| 66 | os.unlink(fn) |
| 67 | except: |
| 68 | pass |
| 69 | compileall.compile_file(self.source_path, force=False, quiet=True) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 70 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 71 | not os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 72 | os.unlink(self.bc_path) |
| 73 | compileall.compile_dir(self.directory, force=False, quiet=True) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 74 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 75 | os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 76 | os.unlink(self.bc_path) |
| 77 | os.unlink(self.bc_path2) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 78 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 79 | def test_no_pycache_in_non_package(self): |
| 80 | # Bug 8563 reported that __pycache__ directories got created by |
| 81 | # compile_file() for non-.py files. |
| 82 | data_dir = os.path.join(self.directory, 'data') |
| 83 | data_file = os.path.join(data_dir, 'file') |
| 84 | os.mkdir(data_dir) |
| 85 | # touch data/file |
| 86 | with open(data_file, 'w'): |
| 87 | pass |
| 88 | compileall.compile_file(data_file) |
| 89 | self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__'))) |
| 90 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 91 | |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 92 | class EncodingTest(unittest.TestCase): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 93 | """Issue 6716: compileall should escape source code when printing errors |
| 94 | to stdout.""" |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 95 | |
| 96 | def setUp(self): |
| 97 | self.directory = tempfile.mkdtemp() |
| 98 | self.source_path = os.path.join(self.directory, '_test.py') |
| 99 | with open(self.source_path, 'w', encoding='utf-8') as file: |
| 100 | file.write('# -*- coding: utf-8 -*-\n') |
| 101 | file.write('print u"\u20ac"\n') |
| 102 | |
| 103 | def tearDown(self): |
| 104 | shutil.rmtree(self.directory) |
| 105 | |
| 106 | def test_error(self): |
| 107 | try: |
| 108 | orig_stdout = sys.stdout |
| 109 | sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii') |
| 110 | compileall.compile_dir(self.directory) |
| 111 | finally: |
| 112 | sys.stdout = orig_stdout |
| 113 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 114 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 115 | class CommandLineTests(unittest.TestCase): |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 116 | """Test compileall's CLI.""" |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 117 | |
| 118 | def setUp(self): |
| 119 | self.addCleanup(self._cleanup) |
| 120 | self.directory = tempfile.mkdtemp() |
| 121 | self.pkgdir = os.path.join(self.directory, 'foo') |
| 122 | os.mkdir(self.pkgdir) |
| 123 | # Touch the __init__.py and a package module. |
| 124 | with open(os.path.join(self.pkgdir, '__init__.py'), 'w'): |
| 125 | pass |
| 126 | with open(os.path.join(self.pkgdir, 'bar.py'), 'w'): |
| 127 | pass |
| 128 | sys.path.insert(0, self.directory) |
| 129 | |
| 130 | def _cleanup(self): |
| 131 | support.rmtree(self.directory) |
| 132 | assert sys.path[0] == self.directory, 'Missing path' |
| 133 | del sys.path[0] |
| 134 | |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 135 | # Ensure that the default behavior of compileall's CLI is to create |
| 136 | # PEP 3147 pyc/pyo files. |
| 137 | for name, ext, switch in [ |
| 138 | ('normal', 'pyc', []), |
| 139 | ('optimize', 'pyo', ['-O']), |
| 140 | ('doubleoptimize', 'pyo', ['-OO']) |
| 141 | ]: |
| 142 | def f(self, ext=ext, switch=switch): |
| 143 | retcode = subprocess.call( |
| 144 | [sys.executable] + switch + |
| 145 | ['-m', 'compileall', '-q', self.pkgdir]) |
| 146 | self.assertEqual(retcode, 0) |
| 147 | # Verify the __pycache__ directory contents. |
| 148 | cachedir = os.path.join(self.pkgdir, '__pycache__') |
| 149 | self.assertTrue(os.path.exists(cachedir)) |
| 150 | expected = sorted(base.format(imp.get_tag(), ext) for base in |
| 151 | ('__init__.{}.{}', 'bar.{}.{}')) |
| 152 | self.assertEqual(sorted(os.listdir(cachedir)), expected) |
| 153 | # Make sure there are no .pyc files in the source directory. |
| 154 | self.assertFalse([pyc_file for pyc_file in os.listdir(self.pkgdir) |
| 155 | if pyc_file.endswith(ext)]) |
| 156 | locals()['test_pep3147_paths_' + name] = f |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 157 | |
| 158 | def test_legacy_paths(self): |
| 159 | # Ensure that with the proper switch, compileall leaves legacy |
| 160 | # pyc/pyo files, and no __pycache__ directory. |
| 161 | retcode = subprocess.call( |
| 162 | (sys.executable, '-m', 'compileall', '-b', '-q', self.pkgdir)) |
| 163 | self.assertEqual(retcode, 0) |
| 164 | # Verify the __pycache__ directory contents. |
| 165 | cachedir = os.path.join(self.pkgdir, '__pycache__') |
| 166 | self.assertFalse(os.path.exists(cachedir)) |
Georg Brandl | 1463a3f | 2010-10-14 07:42:27 +0000 | [diff] [blame] | 167 | expected = sorted(['__init__.py', '__init__.pyc', 'bar.py', 'bar.pyc']) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 168 | self.assertEqual(sorted(os.listdir(self.pkgdir)), expected) |
| 169 | |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 170 | def test_multiple_runs(self): |
| 171 | # Bug 8527 reported that multiple calls produced empty |
| 172 | # __pycache__/__pycache__ directories. |
| 173 | retcode = subprocess.call( |
| 174 | (sys.executable, '-m', 'compileall', '-q', self.pkgdir)) |
| 175 | self.assertEqual(retcode, 0) |
| 176 | # Verify the __pycache__ directory contents. |
| 177 | cachedir = os.path.join(self.pkgdir, '__pycache__') |
| 178 | self.assertTrue(os.path.exists(cachedir)) |
| 179 | cachecachedir = os.path.join(cachedir, '__pycache__') |
| 180 | self.assertFalse(os.path.exists(cachecachedir)) |
| 181 | # Call compileall again. |
| 182 | retcode = subprocess.call( |
| 183 | (sys.executable, '-m', 'compileall', '-q', self.pkgdir)) |
| 184 | self.assertEqual(retcode, 0) |
| 185 | self.assertTrue(os.path.exists(cachedir)) |
| 186 | self.assertFalse(os.path.exists(cachecachedir)) |
| 187 | |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 188 | def test_force(self): |
| 189 | retcode = subprocess.call( |
| 190 | (sys.executable, '-m', 'compileall', '-q', self.pkgdir)) |
| 191 | self.assertEqual(retcode, 0) |
| 192 | pycpath = imp.cache_from_source(os.path.join(self.pkgdir, 'bar.py')) |
| 193 | # set atime/mtime backward to avoid file timestamp resolution issues |
| 194 | os.utime(pycpath, (time.time()-60,)*2) |
| 195 | access = os.stat(pycpath).st_mtime |
| 196 | retcode = subprocess.call( |
| 197 | (sys.executable, '-m', 'compileall', '-q', '-f', self.pkgdir)) |
| 198 | self.assertEqual(retcode, 0) |
| 199 | access2 = os.stat(pycpath).st_mtime |
| 200 | self.assertNotEqual(access, access2) |
| 201 | |
| 202 | def test_legacy(self): |
| 203 | # create a new module |
| 204 | newpackage = os.path.join(self.pkgdir, 'spam') |
| 205 | os.mkdir(newpackage) |
| 206 | with open(os.path.join(newpackage, '__init__.py'), 'w'): |
| 207 | pass |
| 208 | with open(os.path.join(newpackage, 'ham.py'), 'w'): |
| 209 | pass |
| 210 | sourcefile = os.path.join(newpackage, 'ham.py') |
| 211 | |
| 212 | retcode = subprocess.call( |
| 213 | (sys.executable, '-m', 'compileall', '-q', '-l', self.pkgdir)) |
| 214 | self.assertEqual(retcode, 0) |
| 215 | self.assertFalse(os.path.exists(imp.cache_from_source(sourcefile))) |
| 216 | |
| 217 | retcode = subprocess.call( |
| 218 | (sys.executable, '-m', 'compileall', '-q', self.pkgdir)) |
| 219 | self.assertEqual(retcode, 0) |
| 220 | self.assertTrue(os.path.exists(imp.cache_from_source(sourcefile))) |
| 221 | |
| 222 | def test_quiet(self): |
| 223 | noise = subprocess.getoutput('{} -m compileall {}'.format( |
| 224 | sys.executable, self.pkgdir)) |
Éric Araujo | 5cb823d | 2010-11-22 02:42:43 +0000 | [diff] [blame^] | 225 | quiet = subprocess.getoutput(('{} -m compileall -f -q {}'.format( |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 226 | sys.executable, self.pkgdir))) |
Éric Araujo | a491ced | 2010-11-21 02:19:09 +0000 | [diff] [blame] | 227 | self.assertGreater(len(noise), len(quiet)) |
R. David Murray | 650f147 | 2010-11-20 21:18:51 +0000 | [diff] [blame] | 228 | |
| 229 | def test_regexp(self): |
| 230 | retcode = subprocess.call( |
| 231 | (sys.executable, '-m', 'compileall', '-q', '-x', 'bar.*', self.pkgdir)) |
| 232 | self.assertEqual(retcode, 0) |
| 233 | |
| 234 | sourcefile = os.path.join(self.pkgdir, 'bar.py') |
| 235 | self.assertFalse(os.path.exists(imp.cache_from_source(sourcefile))) |
| 236 | sourcefile = os.path.join(self.pkgdir, '__init__.py') |
| 237 | self.assertTrue(os.path.exists(imp.cache_from_source(sourcefile))) |
| 238 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 239 | |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 240 | def test_main(): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 241 | support.run_unittest( |
| 242 | CommandLineTests, |
| 243 | CompileallTests, |
| 244 | EncodingTest, |
| 245 | ) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 246 | |
| 247 | |
| 248 | if __name__ == "__main__": |
| 249 | test_main() |