|
4 | 4 | machinery = util.import_importlib('importlib.machinery')
|
5 | 5 | importlib_util = util.import_importlib('importlib.util')
|
6 | 6 |
|
| 7 | +import importlib.util |
7 | 8 | import os
|
8 | 9 | import string
|
9 | 10 | import sys
|
@@ -744,5 +745,48 @@ def test_source_from_cache_missing_optimization(self):
|
744 | 745 | ) = util.test_both(PEP3147Tests, util=importlib_util)
|
745 | 746 |
|
746 | 747 |
|
| 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 | + |
747 | 791 | if __name__ == '__main__':
|
748 | 792 | unittest.main()
|
0 commit comments