• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import imp
2import sys
3
4import unittest
5
6# Note:
7#   In Python 3.x, this test case is in Lib/test/test_importlib/test_util.py
8
9class MagicNumberTests(unittest.TestCase):
10    """
11    Test release compatibility issues relating to precompiled bytecode
12    """
13    @unittest.skipUnless(
14        sys.version_info.releaselevel in ('candidate', 'final'),
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
53def test_main():
54    from test.support import run_unittest
55    run_unittest(MagicNumberTests)
56
57if __name__ == '__main__':
58    test_main()
59