blob: dd0962f20a835b5c70fe01d9617f4071939ebcb2 [file] [log] [blame]
Martin v. Löwis4b003072010-03-16 13:19:21 +00001import sys
Brett Cannonbefb14f2009-02-10 02:10:16 +00002import compileall
3import imp
4import os
5import py_compile
6import shutil
7import struct
Barry Warsaw28a691b2010-04-17 00:19:56 +00008import subprocess
Brett Cannonbefb14f2009-02-10 02:10:16 +00009import tempfile
R. David Murray650f1472010-11-20 21:18:51 +000010import time
Brett Cannonbefb14f2009-02-10 02:10:16 +000011import unittest
Martin v. Löwis4b003072010-03-16 13:19:21 +000012import io
Brett Cannonbefb14f2009-02-10 02:10:16 +000013
Barry Warsaw28a691b2010-04-17 00:19:56 +000014from test import support
Brett Cannonbefb14f2009-02-10 02:10:16 +000015
16class 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 Warsaw28a691b2010-04-17 00:19:56 +000021 self.bc_path = imp.cache_from_source(self.source_path)
Brett Cannonbefb14f2009-02-10 02:10:16 +000022 with open(self.source_path, 'w') as file:
23 file.write('x = 123\n')
Matthias Klosec33b9022010-03-16 00:36:26 +000024 self.source_path2 = os.path.join(self.directory, '_test2.py')
Barry Warsaw28a691b2010-04-17 00:19:56 +000025 self.bc_path2 = imp.cache_from_source(self.source_path2)
Matthias Klosec33b9022010-03-16 00:36:26 +000026 shutil.copyfile(self.source_path, self.source_path2)
Brett Cannonbefb14f2009-02-10 02:10:16 +000027
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 Klosec33b9022010-03-16 00:36:26 +000062 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 Warsaw28a691b2010-04-17 00:19:56 +000070 self.assertTrue(os.path.isfile(self.bc_path) and
71 not os.path.isfile(self.bc_path2))
Matthias Klosec33b9022010-03-16 00:36:26 +000072 os.unlink(self.bc_path)
73 compileall.compile_dir(self.directory, force=False, quiet=True)
Barry Warsaw28a691b2010-04-17 00:19:56 +000074 self.assertTrue(os.path.isfile(self.bc_path) and
75 os.path.isfile(self.bc_path2))
Matthias Klosec33b9022010-03-16 00:36:26 +000076 os.unlink(self.bc_path)
77 os.unlink(self.bc_path2)
Brett Cannonbefb14f2009-02-10 02:10:16 +000078
Barry Warsawc8a99de2010-04-29 18:43:10 +000079 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 Warsaw28a691b2010-04-17 00:19:56 +000091
Martin v. Löwis4b003072010-03-16 13:19:21 +000092class EncodingTest(unittest.TestCase):
Barry Warsaw28a691b2010-04-17 00:19:56 +000093 """Issue 6716: compileall should escape source code when printing errors
94 to stdout."""
Martin v. Löwis4b003072010-03-16 13:19:21 +000095
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 Warsawc8a99de2010-04-29 18:43:10 +0000114
Barry Warsaw28a691b2010-04-17 00:19:56 +0000115class CommandLineTests(unittest.TestCase):
R. David Murray650f1472010-11-20 21:18:51 +0000116 """Test compileall's CLI."""
Barry Warsaw28a691b2010-04-17 00:19:56 +0000117
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 Brandl1463a3f2010-10-14 07:42:27 +0000135 # 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 Warsaw28a691b2010-04-17 00:19:56 +0000157
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 Brandl1463a3f2010-10-14 07:42:27 +0000167 expected = sorted(['__init__.py', '__init__.pyc', 'bar.py', 'bar.pyc'])
Barry Warsaw28a691b2010-04-17 00:19:56 +0000168 self.assertEqual(sorted(os.listdir(self.pkgdir)), expected)
169
Barry Warsawc04317f2010-04-26 15:59:03 +0000170 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 Murray650f1472010-11-20 21:18:51 +0000188 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))
225 quiet = subprocess.getoutput(('{} -m compileall {}'.format(
226 sys.executable, self.pkgdir)))
Éric Araujoa491ced2010-11-21 02:19:09 +0000227 self.assertGreater(len(noise), len(quiet))
R. David Murray650f1472010-11-20 21:18:51 +0000228
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 Warsaw28a691b2010-04-17 00:19:56 +0000239
Brett Cannonbefb14f2009-02-10 02:10:16 +0000240def test_main():
Barry Warsaw28a691b2010-04-17 00:19:56 +0000241 support.run_unittest(
242 CommandLineTests,
243 CompileallTests,
244 EncodingTest,
245 )
Brett Cannonbefb14f2009-02-10 02:10:16 +0000246
247
248if __name__ == "__main__":
249 test_main()