]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | import compileall\r |
2 | import imp\r | |
3 | import os\r | |
4 | import py_compile\r | |
5 | import shutil\r | |
6 | import struct\r | |
7 | import tempfile\r | |
8 | from test import test_support\r | |
9 | import unittest\r | |
10 | \r | |
11 | \r | |
12 | class CompileallTests(unittest.TestCase):\r | |
13 | \r | |
14 | def setUp(self):\r | |
15 | self.directory = tempfile.mkdtemp()\r | |
16 | self.source_path = os.path.join(self.directory, '_test.py')\r | |
17 | self.bc_path = self.source_path + ('c' if __debug__ else 'o')\r | |
18 | with open(self.source_path, 'w') as file:\r | |
19 | file.write('x = 123\n')\r | |
20 | self.source_path2 = os.path.join(self.directory, '_test2.py')\r | |
21 | self.bc_path2 = self.source_path2 + ('c' if __debug__ else 'o')\r | |
22 | shutil.copyfile(self.source_path, self.source_path2)\r | |
23 | \r | |
24 | def tearDown(self):\r | |
25 | shutil.rmtree(self.directory)\r | |
26 | \r | |
27 | def data(self):\r | |
28 | with open(self.bc_path, 'rb') as file:\r | |
29 | data = file.read(8)\r | |
30 | mtime = int(os.stat(self.source_path).st_mtime)\r | |
31 | compare = struct.pack('<4sl', imp.get_magic(), mtime)\r | |
32 | return data, compare\r | |
33 | \r | |
34 | def recreation_check(self, metadata):\r | |
35 | """Check that compileall recreates bytecode when the new metadata is\r | |
36 | used."""\r | |
37 | if not hasattr(os, 'stat'):\r | |
38 | return\r | |
39 | py_compile.compile(self.source_path)\r | |
40 | self.assertEqual(*self.data())\r | |
41 | with open(self.bc_path, 'rb') as file:\r | |
42 | bc = file.read()[len(metadata):]\r | |
43 | with open(self.bc_path, 'wb') as file:\r | |
44 | file.write(metadata)\r | |
45 | file.write(bc)\r | |
46 | self.assertNotEqual(*self.data())\r | |
47 | compileall.compile_dir(self.directory, force=False, quiet=True)\r | |
48 | self.assertTrue(*self.data())\r | |
49 | \r | |
50 | def test_mtime(self):\r | |
51 | # Test a change in mtime leads to a new .pyc.\r | |
52 | self.recreation_check(struct.pack('<4sl', imp.get_magic(), 1))\r | |
53 | \r | |
54 | def test_magic_number(self):\r | |
55 | # Test a change in mtime leads to a new .pyc.\r | |
56 | self.recreation_check(b'\0\0\0\0')\r | |
57 | \r | |
58 | def test_compile_files(self):\r | |
59 | # Test compiling a single file, and complete directory\r | |
60 | for fn in (self.bc_path, self.bc_path2):\r | |
61 | try:\r | |
62 | os.unlink(fn)\r | |
63 | except:\r | |
64 | pass\r | |
65 | compileall.compile_file(self.source_path, force=False, quiet=True)\r | |
66 | self.assertTrue(os.path.isfile(self.bc_path) \\r | |
67 | and not os.path.isfile(self.bc_path2))\r | |
68 | os.unlink(self.bc_path)\r | |
69 | compileall.compile_dir(self.directory, force=False, quiet=True)\r | |
70 | self.assertTrue(os.path.isfile(self.bc_path) \\r | |
71 | and os.path.isfile(self.bc_path2))\r | |
72 | os.unlink(self.bc_path)\r | |
73 | os.unlink(self.bc_path2)\r | |
74 | \r | |
75 | def test_main():\r | |
76 | test_support.run_unittest(CompileallTests)\r | |
77 | \r | |
78 | \r | |
79 | if __name__ == "__main__":\r | |
80 | test_main()\r |