Skip to content

gh-122188: Remove _imp.pyc_magic_number #122503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -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-'
Expand Down
10 changes: 6 additions & 4 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3116,10 +3116,12 @@ def test_pyimport_addmodule_create(self):
@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__':
Expand Down
6 changes: 1 addition & 5 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
{
Expand Down
Loading