Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 import sys | 1 import sys |
2 import compileall | 2 import compileall |
3 import imp | 3 import imp |
4 import os | 4 import os |
5 import py_compile | 5 import py_compile |
6 import shutil | 6 import shutil |
7 import struct | 7 import struct |
8 import subprocess | |
8 import tempfile | 9 import tempfile |
9 import unittest | 10 import unittest |
10 import io | 11 import io |
11 | 12 |
12 from test import support | 13 from test import support |
13 | 14 |
14 class CompileallTests(unittest.TestCase): | 15 class CompileallTests(unittest.TestCase): |
15 | 16 |
16 def setUp(self): | 17 def setUp(self): |
17 self.directory = tempfile.mkdtemp() | 18 self.directory = tempfile.mkdtemp() |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 self.recreation_check(b'\0\0\0\0') | 59 self.recreation_check(b'\0\0\0\0') |
59 | 60 |
60 def test_compile_files(self): | 61 def test_compile_files(self): |
61 # Test compiling a single file, and complete directory | 62 # Test compiling a single file, and complete directory |
62 for fn in (self.bc_path, self.bc_path2): | 63 for fn in (self.bc_path, self.bc_path2): |
63 try: | 64 try: |
64 os.unlink(fn) | 65 os.unlink(fn) |
65 except: | 66 except: |
66 pass | 67 pass |
67 compileall.compile_file(self.source_path, force=False, quiet=True) | 68 compileall.compile_file(self.source_path, force=False, quiet=True) |
68 self.assertTrue(os.path.isfile(self.bc_path) | 69 self.assertTrue(os.path.isfile(self.bc_path) and |
Benjamin
2010/04/07 02:50:04
As long as you're changing style you may as well p
barry
2010/04/08 12:53:47
Done.
| |
69 and not os.path.isfile(self.bc_path2)) | 70 not os.path.isfile(self.bc_path2)) |
70 os.unlink(self.bc_path) | 71 os.unlink(self.bc_path) |
71 compileall.compile_dir(self.directory, force=False, quiet=True) | 72 compileall.compile_dir(self.directory, force=False, quiet=True) |
72 self.assertTrue(os.path.isfile(self.bc_path) | 73 self.assertTrue(os.path.isfile(self.bc_path) and |
73 and os.path.isfile(self.bc_path2)) | 74 os.path.isfile(self.bc_path2)) |
74 os.unlink(self.bc_path) | 75 os.unlink(self.bc_path) |
75 os.unlink(self.bc_path2) | 76 os.unlink(self.bc_path2) |
76 | 77 |
78 | |
77 class EncodingTest(unittest.TestCase): | 79 class EncodingTest(unittest.TestCase): |
78 'Issue 6716: compileall should escape source code when printing errors to st dout.' | 80 """Issue 6716: compileall should escape source code when printing errors |
81 to stdout.""" | |
79 | 82 |
80 def setUp(self): | 83 def setUp(self): |
81 self.directory = tempfile.mkdtemp() | 84 self.directory = tempfile.mkdtemp() |
82 self.source_path = os.path.join(self.directory, '_test.py') | 85 self.source_path = os.path.join(self.directory, '_test.py') |
83 with open(self.source_path, 'w', encoding='utf-8') as file: | 86 with open(self.source_path, 'w', encoding='utf-8') as file: |
84 file.write('# -*- coding: utf-8 -*-\n') | 87 file.write('# -*- coding: utf-8 -*-\n') |
85 file.write('print u"\u20ac"\n') | 88 file.write('print u"\u20ac"\n') |
86 | 89 |
87 def tearDown(self): | 90 def tearDown(self): |
88 shutil.rmtree(self.directory) | 91 shutil.rmtree(self.directory) |
89 | 92 |
90 def test_error(self): | 93 def test_error(self): |
91 try: | 94 try: |
92 orig_stdout = sys.stdout | 95 orig_stdout = sys.stdout |
93 sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii') | 96 sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii') |
94 compileall.compile_dir(self.directory) | 97 compileall.compile_dir(self.directory) |
95 finally: | 98 finally: |
96 sys.stdout = orig_stdout | 99 sys.stdout = orig_stdout |
97 | 100 |
101 class CommandLineTests(unittest.TestCase): | |
102 """Test some aspects of compileall's CLI.""" | |
103 | |
104 def setUp(self): | |
105 self.addCleanup(self._cleanup) | |
106 self.directory = tempfile.mkdtemp() | |
107 self.pkgdir = os.path.join(self.directory, 'foo') | |
108 os.mkdir(self.pkgdir) | |
109 # Touch the __init__.py and a package module. | |
110 with open(os.path.join(self.pkgdir, '__init__.py'), 'w'): | |
111 pass | |
112 with open(os.path.join(self.pkgdir, 'bar.py'), 'w'): | |
113 pass | |
114 sys.path.insert(0, self.directory) | |
115 | |
116 def _cleanup(self): | |
117 support.rmtree(self.directory) | |
118 assert sys.path[0] == self.directory, 'Missing path' | |
119 del sys.path[0] | |
120 | |
121 def test_pep3147_paths(self): | |
122 # Ensure that the default behavior of compileall's CLI is to create | |
123 # PEP 3147 pyc/pyo files. | |
124 retcode = subprocess.call( | |
125 (sys.executable, '-m', 'compileall', '-q', self.pkgdir)) | |
126 self.assertEqual(retcode, 0) | |
127 # Verify the __pycache__ directory contents. | |
128 cachedir = os.path.join(self.pkgdir, '__pycache__') | |
129 self.assertTrue(os.path.exists(cachedir)) | |
130 ext = ('pyc' if __debug__ else 'pyo') | |
131 expected = sorted(base.format(imp.get_tag(), ext) for base in | |
132 ('__init__.{}.{}', 'bar.{}.{}')) | |
133 self.assertEqual(sorted(os.listdir(cachedir)), expected) | |
134 # Make sure there are no .pyc files in the source directory. | |
135 self.assertFalse([pyc_file for pyc_file in os.listdir(self.pkgdir) | |
136 if pyc_file.endswith(ext)]) | |
137 | |
138 def test_legacy_paths(self): | |
139 # Ensure that with the proper switch, compileall leaves legacy | |
140 # pyc/pyo files, and no __pycache__ directory. | |
141 retcode = subprocess.call( | |
142 (sys.executable, '-m', 'compileall', '-b', '-q', self.pkgdir)) | |
143 self.assertEqual(retcode, 0) | |
144 # Verify the __pycache__ directory contents. | |
145 cachedir = os.path.join(self.pkgdir, '__pycache__') | |
146 self.assertFalse(os.path.exists(cachedir)) | |
147 ext = ('pyc' if __debug__ else 'pyo') | |
148 expected = [base.format(ext) for base in ('__init__.{}', 'bar.{}')] | |
149 expected.extend(['__init__.py', 'bar.py']) | |
150 expected.sort() | |
151 self.assertEqual(sorted(os.listdir(self.pkgdir)), expected) | |
152 | |
153 | |
98 def test_main(): | 154 def test_main(): |
99 support.run_unittest(CompileallTests, | 155 support.run_unittest( |
100 EncodingTest) | 156 CommandLineTests, |
157 CompileallTests, | |
158 EncodingTest, | |
159 ) | |
101 | 160 |
102 | 161 |
103 if __name__ == "__main__": | 162 if __name__ == "__main__": |
104 test_main() | 163 test_main() |
LEFT | RIGHT |