Skip to content

Commit d071a20

Browse files
authored
[3.5] bpo-29514: Check magic number for bugfix releases (#2158)
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 e0e0029 commit d071a20

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 string
910
import sys
@@ -744,5 +745,48 @@ def test_source_from_cache_missing_optimization(self):
744745
) = util.test_both(PEP3147Tests, util=importlib_util)
745746

746747

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

0 commit comments

Comments
 (0)