Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a51f12f

Browse files
authored
[2.7] bpo-29514: Check magic number for bugfix releases
Add a test to check the current MAGIC_NUMBER against the expected number for the release if the current release is at candidate or final level. On test failure, describe to the developer the procedure for changing the magic number. This ensures that pre-merge CI will automatically pick up on magic number changes in maintenance releases (and explain why those are problematic), rather than relying on all core developers to be aware of the implications of such changes.
1 parent bdd3567 commit a51f12f

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

Lib/test/test_import_magic.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import imp
2+
import sys
3+
4+
import unittest
5+
6+
# Note:
7+
# In Python 3.x, this test case is in Lib/test/test_importlib/test_util.py
8+
9+
class MagicNumberTests(unittest.TestCase):
10+
"""
11+
Test release compatibility issues relating to precompiled bytecode
12+
"""
13+
@unittest.skipUnless(
14+
sys.version_info.releaselevel in ('final', 'release'),
15+
'only applies to candidate or final python release levels'
16+
)
17+
def test_magic_number(self):
18+
"""
19+
Each python minor release should generally have a MAGIC_NUMBER
20+
that does not change once the release reaches candidate status.
21+
22+
Once a release reaches candidate status, the value of the constant
23+
EXPECTED_MAGIC_NUMBER in this test should be changed.
24+
This test will then check that the actual MAGIC_NUMBER matches
25+
the expected value for the release.
26+
27+
In exceptional cases, it may be required to change the MAGIC_NUMBER
28+
for a maintenance release. In this case the change should be
29+
discussed in python-dev. If a change is required, community
30+
stakeholders such as OS package maintainers must be notified
31+
in advance. Such exceptional releases will then require an
32+
adjustment to this test case.
33+
"""
34+
EXPECTED_MAGIC_NUMBER = 62211
35+
raw_magic = imp.get_magic()
36+
actual = (ord(raw_magic[1]) << 8) + ord(raw_magic[0])
37+
38+
msg = (
39+
"To avoid breaking backwards compatibility with cached bytecode "
40+
"files that can't be automatically regenerated by the current "
41+
"user, candidate and final releases require the current "
42+
"importlib.util.MAGIC_NUMBER to match the expected "
43+
"magic number in this test. Set the expected "
44+
"magic number in this test to the current MAGIC_NUMBER to "
45+
"continue with the release.\n\n"
46+
"Changing the MAGIC_NUMBER for a maintenance release "
47+
"requires discussion in python-dev and notification of "
48+
"community stakeholders."
49+
)
50+
self.assertEqual(EXPECTED_MAGIC_NUMBER, actual)#, msg)
51+
52+
53+
def test_main():
54+
from test.support import run_unittest
55+
run_unittest(MagicNumberTests)
56+
57+
if __name__ == '__main__':
58+
test_main()

0 commit comments

Comments
 (0)