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 |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 10 | import unittest |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 11 | import io |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 12 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 13 | from test import support |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 14 | |
| 15 | class CompileallTests(unittest.TestCase): |
| 16 | |
| 17 | def setUp(self): |
| 18 | self.directory = tempfile.mkdtemp() |
| 19 | self.source_path = os.path.join(self.directory, '_test.py') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 20 | self.bc_path = imp.cache_from_source(self.source_path) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 21 | with open(self.source_path, 'w') as file: |
| 22 | file.write('x = 123\n') |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 23 | self.source_path2 = os.path.join(self.directory, '_test2.py') |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 24 | self.bc_path2 = imp.cache_from_source(self.source_path2) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 25 | shutil.copyfile(self.source_path, self.source_path2) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 26 | |
| 27 | def tearDown(self): |
| 28 | shutil.rmtree(self.directory) |
| 29 | |
| 30 | def data(self): |
| 31 | with open(self.bc_path, 'rb') as file: |
| 32 | data = file.read(8) |
| 33 | mtime = int(os.stat(self.source_path).st_mtime) |
| 34 | compare = struct.pack('<4sl', imp.get_magic(), mtime) |
| 35 | return data, compare |
| 36 | |
| 37 | def recreation_check(self, metadata): |
| 38 | """Check that compileall recreates bytecode when the new metadata is |
| 39 | used.""" |
| 40 | if not hasattr(os, 'stat'): |
| 41 | return |
| 42 | py_compile.compile(self.source_path) |
| 43 | self.assertEqual(*self.data()) |
| 44 | with open(self.bc_path, 'rb') as file: |
| 45 | bc = file.read()[len(metadata):] |
| 46 | with open(self.bc_path, 'wb') as file: |
| 47 | file.write(metadata) |
| 48 | file.write(bc) |
| 49 | self.assertNotEqual(*self.data()) |
| 50 | compileall.compile_dir(self.directory, force=False, quiet=True) |
| 51 | self.assertTrue(*self.data()) |
| 52 | |
| 53 | def test_mtime(self): |
| 54 | # Test a change in mtime leads to a new .pyc. |
| 55 | self.recreation_check(struct.pack('<4sl', imp.get_magic(), 1)) |
| 56 | |
| 57 | def test_magic_number(self): |
| 58 | # Test a change in mtime leads to a new .pyc. |
| 59 | self.recreation_check(b'\0\0\0\0') |
| 60 | |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 61 | def test_compile_files(self): |
| 62 | # Test compiling a single file, and complete directory |
| 63 | for fn in (self.bc_path, self.bc_path2): |
| 64 | try: |
| 65 | os.unlink(fn) |
| 66 | except: |
| 67 | pass |
| 68 | compileall.compile_file(self.source_path, force=False, quiet=True) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 69 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 70 | not os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 71 | os.unlink(self.bc_path) |
| 72 | compileall.compile_dir(self.directory, force=False, quiet=True) |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 73 | self.assertTrue(os.path.isfile(self.bc_path) and |
| 74 | os.path.isfile(self.bc_path2)) |
Matthias Klose | c33b902 | 2010-03-16 00:36:26 +0000 | [diff] [blame] | 75 | os.unlink(self.bc_path) |
| 76 | os.unlink(self.bc_path2) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 77 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 78 | def test_no_pycache_in_non_package(self): |
| 79 | # Bug 8563 reported that __pycache__ directories got created by |
| 80 | # compile_file() for non-.py files. |
| 81 | data_dir = os.path.join(self.directory, 'data') |
| 82 | data_file = os.path.join(data_dir, 'file') |
| 83 | os.mkdir(data_dir) |
| 84 | # touch data/file |
| 85 | with open(data_file, 'w'): |
| 86 | pass |
| 87 | compileall.compile_file(data_file) |
| 88 | self.assertFalse(os.path.exists(os.path.join(data_dir, '__pycache__'))) |
| 89 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 90 | |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 91 | class EncodingTest(unittest.TestCase): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 92 | """Issue 6716: compileall should escape source code when printing errors |
| 93 | to stdout.""" |
Martin v. Löwis | 4b00307 | 2010-03-16 13:19:21 +0000 | [diff] [blame] | 94 | |
| 95 | def setUp(self): |
| 96 | self.directory = tempfile.mkdtemp() |
| 97 | self.source_path = os.path.join(self.directory, '_test.py') |
| 98 | with open(self.source_path, 'w', encoding='utf-8') as file: |
| 99 | file.write('# -*- coding: utf-8 -*-\n') |
| 100 | file.write('print u"\u20ac"\n') |
| 101 | |
| 102 | def tearDown(self): |
| 103 | shutil.rmtree(self.directory) |
| 104 | |
| 105 | def test_error(self): |
| 106 | try: |
| 107 | orig_stdout = sys.stdout |
| 108 | sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii') |
| 109 | compileall.compile_dir(self.directory) |
| 110 | finally: |
| 111 | sys.stdout = orig_stdout |
| 112 | |
Barry Warsaw | c8a99de | 2010-04-29 18:43:10 +0000 | [diff] [blame] | 113 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 114 | class CommandLineTests(unittest.TestCase): |
| 115 | """Test some aspects of compileall's CLI.""" |
| 116 | |
| 117 | def setUp(self): |
| 118 | self.addCleanup(self._cleanup) |
| 119 | self.directory = tempfile.mkdtemp() |
| 120 | self.pkgdir = os.path.join(self.directory, 'foo') |
| 121 | os.mkdir(self.pkgdir) |
| 122 | # Touch the __init__.py and a package module. |
| 123 | with open(os.path.join(self.pkgdir, '__init__.py'), 'w'): |
| 124 | pass |
| 125 | with open(os.path.join(self.pkgdir, 'bar.py'), 'w'): |
| 126 | pass |
| 127 | sys.path.insert(0, self.directory) |
| 128 | |
| 129 | def _cleanup(self): |
| 130 | support.rmtree(self.directory) |
| 131 | assert sys.path[0] == self.directory, 'Missing path' |
| 132 | del sys.path[0] |
| 133 | |
| 134 | def test_pep3147_paths(self): |
| 135 | # Ensure that the default behavior of compileall's CLI is to create |
| 136 | # PEP 3147 pyc/pyo files. |
| 137 | retcode = subprocess.call( |
| 138 | (sys.executable, '-m', 'compileall', '-q', self.pkgdir)) |
| 139 | self.assertEqual(retcode, 0) |
| 140 | # Verify the __pycache__ directory contents. |
| 141 | cachedir = os.path.join(self.pkgdir, '__pycache__') |
| 142 | self.assertTrue(os.path.exists(cachedir)) |
| 143 | ext = ('pyc' if __debug__ else 'pyo') |
| 144 | expected = sorted(base.format(imp.get_tag(), ext) for base in |
| 145 | ('__init__.{}.{}', 'bar.{}.{}')) |
| 146 | self.assertEqual(sorted(os.listdir(cachedir)), expected) |
| 147 | # Make sure there are no .pyc files in the source directory. |
| 148 | self.assertFalse([pyc_file for pyc_file in os.listdir(self.pkgdir) |
| 149 | if pyc_file.endswith(ext)]) |
| 150 | |
| 151 | def test_legacy_paths(self): |
| 152 | # Ensure that with the proper switch, compileall leaves legacy |
| 153 | # pyc/pyo files, and no __pycache__ directory. |
| 154 | retcode = subprocess.call( |
| 155 | (sys.executable, '-m', 'compileall', '-b', '-q', self.pkgdir)) |
| 156 | self.assertEqual(retcode, 0) |
| 157 | # Verify the __pycache__ directory contents. |
| 158 | cachedir = os.path.join(self.pkgdir, '__pycache__') |
| 159 | self.assertFalse(os.path.exists(cachedir)) |
| 160 | ext = ('pyc' if __debug__ else 'pyo') |
| 161 | expected = [base.format(ext) for base in ('__init__.{}', 'bar.{}')] |
| 162 | expected.extend(['__init__.py', 'bar.py']) |
| 163 | expected.sort() |
| 164 | self.assertEqual(sorted(os.listdir(self.pkgdir)), expected) |
| 165 | |
Barry Warsaw | c04317f | 2010-04-26 15:59:03 +0000 | [diff] [blame] | 166 | def test_multiple_runs(self): |
| 167 | # Bug 8527 reported that multiple calls produced empty |
| 168 | # __pycache__/__pycache__ directories. |
| 169 | retcode = subprocess.call( |
| 170 | (sys.executable, '-m', 'compileall', '-q', self.pkgdir)) |
| 171 | self.assertEqual(retcode, 0) |
| 172 | # Verify the __pycache__ directory contents. |
| 173 | cachedir = os.path.join(self.pkgdir, '__pycache__') |
| 174 | self.assertTrue(os.path.exists(cachedir)) |
| 175 | cachecachedir = os.path.join(cachedir, '__pycache__') |
| 176 | self.assertFalse(os.path.exists(cachecachedir)) |
| 177 | # Call compileall again. |
| 178 | retcode = subprocess.call( |
| 179 | (sys.executable, '-m', 'compileall', '-q', self.pkgdir)) |
| 180 | self.assertEqual(retcode, 0) |
| 181 | self.assertTrue(os.path.exists(cachedir)) |
| 182 | self.assertFalse(os.path.exists(cachecachedir)) |
| 183 | |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 184 | |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 185 | def test_main(): |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 186 | support.run_unittest( |
| 187 | CommandLineTests, |
| 188 | CompileallTests, |
| 189 | EncodingTest, |
| 190 | ) |
Brett Cannon | befb14f | 2009-02-10 02:10:16 +0000 | [diff] [blame] | 191 | |
| 192 | |
| 193 | if __name__ == "__main__": |
| 194 | test_main() |