|
| 1 | +import importlib.util as util |
| 2 | +import sys |
| 3 | +import unittest |
| 4 | + |
| 5 | + |
| 6 | +class ImportModuleReleaseTests(unittest.TestCase): |
| 7 | + """ |
| 8 | + Test release compatibility issues relating to importlib |
| 9 | + """ |
| 10 | + def test_magic_number(self): |
| 11 | + """ |
| 12 | + Each python minor release should generally have a MAGIC_NUMBER |
| 13 | + that does not change once the release reaches candidate status. |
| 14 | +
|
| 15 | + Once a release reaches candidate status, an entry should be |
| 16 | + added to EXPECTED_MAGIC_NUMBERS. This test will then check that |
| 17 | + the actual MAGIC_NUMBER matches the expected value for the |
| 18 | + release. |
| 19 | +
|
| 20 | + In exceptional cases, it may be required to change the MAGIC_NUMBER |
| 21 | + for a maintenance release. In this case the change should be |
| 22 | + discussed in dev-python. If a change is required, community |
| 23 | + stakeholders such as OS package maintainers must be notified |
| 24 | + in advance. Such exceptional releases will then require an |
| 25 | + adjustment to this test case. |
| 26 | + """ |
| 27 | + if sys.version_info.releaselevel not in ('final', 'candidate'): |
| 28 | + return |
| 29 | + |
| 30 | + release = (sys.version_info.major, sys.version_info.minor) |
| 31 | + |
| 32 | + msg = ( |
| 33 | + "Candidate and final releases require an entry in " |
| 34 | + "importlib.util.EXPECTED_MAGIC_NUMBERS. Set the expected " |
| 35 | + "magic number to the current MAGIC_NUMBER to continue " |
| 36 | + "with the release." |
| 37 | + ) |
| 38 | + self.assertIn(release, util.EXPECTED_MAGIC_NUMBERS, msg=msg) |
| 39 | + expected = util.EXPECTED_MAGIC_NUMBERS[release] |
| 40 | + actual = int.from_bytes(util.MAGIC_NUMBER[:2], 'little') |
| 41 | + |
| 42 | + # Adjust for exceptional releases |
| 43 | + if release == (3, 5): |
| 44 | + expected = 3351 # Changed in 3.5.3 issue 27286 |
| 45 | + |
| 46 | + msg = ( |
| 47 | + "The MAGIC_NUMBER {0} does not match the expected value " |
| 48 | + "{1} for release {2}. Changing the MAGIC_NUMBER for a " |
| 49 | + "maintenance release requires discussion in dev-python and " |
| 50 | + "notification of community stakeholders." |
| 51 | + .format(actual, expected, release) |
| 52 | + ) |
| 53 | + self.assertEqual(expected, actual, msg) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + unittest.main() |
0 commit comments