]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122188: Remove _imp.pyc_magic_number (GH-122503)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 2 Aug 2024 10:12:19 +0000 (13:12 +0300)
committerGitHub <noreply@github.com>
Fri, 2 Aug 2024 10:12:19 +0000 (13:12 +0300)
_imp.pyc_magic_number_token should be enough.

Lib/importlib/_bootstrap_external.py
Lib/test/test_import/__init__.py
Python/import.c

index 4d154dc4c25edc60541d2eaf7b3a25071aae0dac..5bbcb376a4a6b3dd5503ea3e58c8d5524460b620 100644 (file)
@@ -221,7 +221,7 @@ def _write_atomic(path, data, mode=0o666):
 
 _code_type = type(_write_atomic.__code__)
 
-MAGIC_NUMBER = (_imp.pyc_magic_number).to_bytes(2, 'little') + b'\r\n'
+MAGIC_NUMBER = _imp.pyc_magic_number_token.to_bytes(4, 'little')
 
 _PYCACHE = '__pycache__'
 _OPT = 'opt-'
index 56c6ffe93fce37b4699e4662dab3be043c7814cb..fd778ec216cc98adac41d6ce697786d89b00a81e 100644 (file)
@@ -3116,10 +3116,12 @@ class CAPITests(unittest.TestCase):
 @cpython_only
 class TestMagicNumber(unittest.TestCase):
     def test_magic_number_endianness(self):
-        magic_number = (_imp.pyc_magic_number).to_bytes(2, 'little') + b'\r\n'
-        raw_magic_number = int.from_bytes(magic_number, 'little')
-
-        self.assertEqual(raw_magic_number, _imp.pyc_magic_number_token)
+        magic_number_bytes = _imp.pyc_magic_number_token.to_bytes(4, 'little')
+        self.assertEqual(magic_number_bytes[2:], b'\r\n')
+        # Starting with Python 3.11, Python 3.n starts with magic number 2900+50n.
+        magic_number = int.from_bytes(magic_number_bytes[:2], 'little')
+        start = 2900 + sys.version_info.minor * 50
+        self.assertIn(magic_number, range(start, start + 50))
 
 
 if __name__ == '__main__':
index 540874a0f0414f19bc563371e41f576a22d34212..f4c0d544fbdefac39cbc73cf9815a839cb04ad2f 100644 (file)
@@ -6,7 +6,7 @@
 #include "pycore_import.h"        // _PyImport_BootstrapImp()
 #include "pycore_initconfig.h"    // _PyStatus_OK()
 #include "pycore_interp.h"        // struct _import_runtime_state
-#include "pycore_magic_number.h"  // PYC_MAGIC_NUMBER
+#include "pycore_magic_number.h"  // PYC_MAGIC_NUMBER_TOKEN
 #include "pycore_namespace.h"     // _PyNamespace_Type
 #include "pycore_object.h"        // _Py_SetImmortal()
 #include "pycore_pyerrors.h"      // _PyErr_SetString()
@@ -4810,10 +4810,6 @@ imp_module_exec(PyObject *module)
         return -1;
     }
 
-    if (PyModule_AddIntConstant(module, "pyc_magic_number", PYC_MAGIC_NUMBER) < 0) {
-        return -1;
-    }
-
     if (PyModule_AddIntConstant(
             module, "pyc_magic_number_token", PYC_MAGIC_NUMBER_TOKEN) < 0)
     {