Skip to content

Commit 92e9e35

Browse files
authored
[3.6] bpo-29514: Check magic number for bugfix releases (#2157)
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 932946c commit 92e9e35

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Lib/test/test_importlib/test_util.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
machinery = util.import_importlib('importlib.machinery')
55
importlib_util = util.import_importlib('importlib.util')
66

7+
import importlib.util
78
import os
89
import pathlib
910
import string
@@ -757,5 +758,48 @@ def test_source_from_cache_path_like_arg(self):
757758
) = util.test_both(PEP3147Tests, util=importlib_util)
758759

759760

761+
class MagicNumberTests(unittest.TestCase):
762+
"""
763+
Test release compatibility issues relating to importlib
764+
"""
765+
@unittest.skipUnless(
766+
sys.version_info.releaselevel in ('final', 'release'),
767+
'only applies to candidate or final python release levels'
768+
)
769+
def test_magic_number(self):
770+
"""
771+
Each python minor release should generally have a MAGIC_NUMBER
772+
that does not change once the release reaches candidate status.
773+
774+
Once a release reaches candidate status, the value of the constant
775+
EXPECTED_MAGIC_NUMBER in this test should be changed.
776+
This test will then check that the actual MAGIC_NUMBER matches
777+
the expected value for the release.
778+
779+
In exceptional cases, it may be required to change the MAGIC_NUMBER
780+
for a maintenance release. In this case the change should be
781+
discussed in python-dev. If a change is required, community
782+
stakeholders such as OS package maintainers must be notified
783+
in advance. Such exceptional releases will then require an
784+
adjustment to this test case.
785+
"""
786+
EXPECTED_MAGIC_NUMBER = 3379
787+
actual = int.from_bytes(importlib.util.MAGIC_NUMBER[:2], 'little')
788+
789+
msg = (
790+
"To avoid breaking backwards compatibility with cached bytecode "
791+
"files that can't be automatically regenerated by the current "
792+
"user, candidate and final releases require the current "
793+
"importlib.util.MAGIC_NUMBER to match the expected "
794+
"magic number in this test. Set the expected "
795+
"magic number in this test to the current MAGIC_NUMBER to "
796+
"continue with the release.\n\n"
797+
"Changing the MAGIC_NUMBER for a maintenance release "
798+
"requires discussion in python-dev and notification of "
799+
"community stakeholders."
800+
)
801+
self.assertEqual(EXPECTED_MAGIC_NUMBER, actual, msg)
802+
803+
760804
if __name__ == '__main__':
761805
unittest.main()

0 commit comments

Comments
 (0)