Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_compileall.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 584c47d8c93f |
| children | 70e115c34f97 |
| rev | line source |
|---|---|
|
51500
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
1 import compileall |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
2 import imp |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
3 import os |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
4 import py_compile |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
5 import shutil |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
6 import struct |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
7 import sys |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
8 import tempfile |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
9 import time |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
10 from test import test_support |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
11 import unittest |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
12 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
13 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
14 class CompileallTests(unittest.TestCase): |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
15 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
16 def setUp(self): |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
17 self.directory = tempfile.mkdtemp() |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
18 self.source_path = os.path.join(self.directory, '_test.py') |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
19 self.bc_path = self.source_path + ('c' if __debug__ else 'o') |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
20 with open(self.source_path, 'w') as file: |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
21 file.write('x = 123\n') |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
22 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
23 def tearDown(self): |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
24 shutil.rmtree(self.directory) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
25 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
26 def data(self): |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
27 with open(self.bc_path, 'rb') as file: |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
28 data = file.read(8) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
29 mtime = int(os.stat(self.source_path).st_mtime) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
30 compare = struct.pack('<4sl', imp.get_magic(), mtime) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
31 return data, compare |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
32 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
33 def recreation_check(self, metadata): |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
34 """Check that compileall recreates bytecode when the new metadata is |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
35 used.""" |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
36 if not hasattr(os, 'stat'): |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
37 return |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
38 py_compile.compile(self.source_path) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
39 self.assertEqual(*self.data()) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
40 with open(self.bc_path, 'rb') as file: |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
41 bc = file.read()[len(metadata):] |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
42 with open(self.bc_path, 'wb') as file: |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
43 file.write(metadata) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
44 file.write(bc) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
45 self.assertNotEqual(*self.data()) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
46 compileall.compile_dir(self.directory, force=False, quiet=True) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
47 self.assertTrue(*self.data()) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
48 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
49 def test_mtime(self): |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
50 # Test a change in mtime leads to a new .pyc. |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
51 self.recreation_check(struct.pack('<4sl', imp.get_magic(), 1)) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
52 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
53 def test_magic_number(self): |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
54 # Test a change in mtime leads to a new .pyc. |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
55 self.recreation_check(b'\0\0\0\0') |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
56 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
57 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
58 def test_main(): |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
59 test_support.run_unittest(CompileallTests) |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
60 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
61 |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
62 if __name__ == "__main__": |
|
584c47d8c93f
compileall used the ctime of bytecode and source to determine if the bytecode
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
63 test_main() |
