10 from test
import test_support
14 class CompileallTests(unittest
.TestCase
):
17 self
.directory
= tempfile
.mkdtemp()
18 self
.source_path
= os
.path
.join(self
.directory
, '_test.py')
19 self
.bc_path
= self
.source_path
+ ('c' if __debug__
else 'o')
20 with
open(self
.source_path
, 'w') as file:
21 file.write('x = 123\n')
24 shutil
.rmtree(self
.directory
)
27 with
open(self
.bc_path
, 'rb') as file:
29 mtime
= int(os
.stat(self
.source_path
).st_mtime
)
30 compare
= struct
.pack('<4sl', imp
.get_magic(), mtime
)
33 def recreation_check(self
, metadata
):
34 """Check that compileall recreates bytecode when the new metadata is
36 if not hasattr(os
, 'stat'):
38 py_compile
.compile(self
.source_path
)
39 self
.assertEqual(*self
.data())
40 with
open(self
.bc_path
, 'rb') as file:
41 bc
= file.read()[len(metadata
):]
42 with
open(self
.bc_path
, 'wb') as file:
45 self
.assertNotEqual(*self
.data())
46 compileall
.compile_dir(self
.directory
, force
=False, quiet
=True)
47 self
.assertTrue(*self
.data())
50 # Test a change in mtime leads to a new .pyc.
51 self
.recreation_check(struct
.pack('<4sl', imp
.get_magic(), 1))
53 def test_magic_number(self
):
54 # Test a change in mtime leads to a new .pyc.
55 self
.recreation_check(b
'\0\0\0\0')
59 test_support
.run_unittest(CompileallTests
)
62 if __name__
== "__main__":