Skip to content

Commit d6d344d

Browse files
appeltelambv
authored andcommitted
bpo-29514: Check magic number for bugfix release (#54)
* bpo-29514: Check magic number for micro release Add a dict importlib.util.EXPECTED_MAGIC_NUMBERS which details the initial and expected pyc magic number for each minor release. This gives a mechanism for users to check if the magic number has changed within a release and for a test to ensure procedure is followed if a change is necessary. 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. * Simplify magic number release test Simplify the magic number release test by removing EXPECTED_MAGIC_NUMBERS table and making the expected magic number self-contained within the test. BPO: 29514 * Improve magic number test execution and message Improve the execution of the magic number test by using skipUnless for alpha and beta releases, and directly inheriting from unittest.TestCase rather than using the machinery for the other tests. Also improve the error message to explain the reason for caution in changing the magic number. BPO: 29514
1 parent 8e1ddbd commit d6d344d

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)