Skip to content

Commit 03b8852

Browse files
gh-122188: Remove _imp.pyc_magic_number (GH-122503)
_imp.pyc_magic_number_token should be enough.
1 parent d57f8a9 commit 03b8852

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

Lib/importlib/_bootstrap_external.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def _write_atomic(path, data, mode=0o666):
221221

222222
_code_type = type(_write_atomic.__code__)
223223

224-
MAGIC_NUMBER = (_imp.pyc_magic_number).to_bytes(2, 'little') + b'\r\n'
224+
MAGIC_NUMBER = _imp.pyc_magic_number_token.to_bytes(4, 'little')
225225

226226
_PYCACHE = '__pycache__'
227227
_OPT = 'opt-'

Lib/test/test_import/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,10 +3116,12 @@ def test_pyimport_addmodule_create(self):
31163116
@cpython_only
31173117
class TestMagicNumber(unittest.TestCase):
31183118
def test_magic_number_endianness(self):
3119-
magic_number = (_imp.pyc_magic_number).to_bytes(2, 'little') + b'\r\n'
3120-
raw_magic_number = int.from_bytes(magic_number, 'little')
3121-
3122-
self.assertEqual(raw_magic_number, _imp.pyc_magic_number_token)
3119+
magic_number_bytes = _imp.pyc_magic_number_token.to_bytes(4, 'little')
3120+
self.assertEqual(magic_number_bytes[2:], b'\r\n')
3121+
# Starting with Python 3.11, Python 3.n starts with magic number 2900+50n.
3122+
magic_number = int.from_bytes(magic_number_bytes[:2], 'little')
3123+
start = 2900 + sys.version_info.minor * 50
3124+
self.assertIn(magic_number, range(start, start + 50))
31233125

31243126

31253127
if __name__ == '__main__':

Python/import.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "pycore_import.h" // _PyImport_BootstrapImp()
77
#include "pycore_initconfig.h" // _PyStatus_OK()
88
#include "pycore_interp.h" // struct _import_runtime_state
9-
#include "pycore_magic_number.h" // PYC_MAGIC_NUMBER
9+
#include "pycore_magic_number.h" // PYC_MAGIC_NUMBER_TOKEN
1010
#include "pycore_namespace.h" // _PyNamespace_Type
1111
#include "pycore_object.h" // _Py_SetImmortal()
1212
#include "pycore_pyerrors.h" // _PyErr_SetString()
@@ -4810,10 +4810,6 @@ imp_module_exec(PyObject *module)
48104810
return -1;
48114811
}
48124812

4813-
if (PyModule_AddIntConstant(module, "pyc_magic_number", PYC_MAGIC_NUMBER) < 0) {
4814-
return -1;
4815-
}
4816-
48174813
if (PyModule_AddIntConstant(
48184814
module, "pyc_magic_number_token", PYC_MAGIC_NUMBER_TOKEN) < 0)
48194815
{

0 commit comments

Comments
 (0)