Skip to content

Commit 33c9d36

Browse files
committed
Move _RAW_MAGIC_NUMBER to the C side as well
1 parent 0aa2337 commit 33c9d36

File tree

6 files changed

+20
-23
lines changed

6 files changed

+20
-23
lines changed

Include/internal/pycore_magic_number.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ PC/launcher.c must also be updated.
268268
*/
269269

270270
#define PYC_MAGIC_NUMBER 3603
271+
#define PYC_MAGIC_NUMBER_TOKEN ((uint32_t)PYC_MAGIC_NUMBER | ((uint32_t)'\r' << 16) | ((uint32_t)'\n' << 24))
272+
271273

272274
#ifdef __cplusplus
273275
}

Lib/importlib/_bootstrap_external.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ def _write_atomic(path, data, mode=0o666):
223223

224224
MAGIC_NUMBER = (_imp._pyc_magic_number).to_bytes(2, 'little') + b'\r\n'
225225

226-
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # for import.c
227-
228226
_PYCACHE = '__pycache__'
229227
_OPT = 'opt-'
230228

@@ -862,7 +860,7 @@ def get_code(self, fullname):
862860
_imp.check_hash_based_pycs == 'always')):
863861
source_bytes = self.get_data(source_path)
864862
source_hash = _imp.source_hash(
865-
_RAW_MAGIC_NUMBER,
863+
_imp._pyc_magic_number_token,
866864
source_bytes,
867865
)
868866
_validate_hash_pyc(data, source_hash, fullname,
@@ -891,7 +889,7 @@ def get_code(self, fullname):
891889
source_mtime is not None):
892890
if hash_based:
893891
if source_hash is None:
894-
source_hash = _imp.source_hash(_RAW_MAGIC_NUMBER,
892+
source_hash = _imp.source_hash(_imp._pyc_magic_number_token,
895893
source_bytes)
896894
data = _code_to_hash_pyc(code_object, source_hash, check_source)
897895
else:

Lib/importlib/util.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from ._bootstrap import spec_from_loader
66
from ._bootstrap import _find_spec
77
from ._bootstrap_external import MAGIC_NUMBER
8-
from ._bootstrap_external import _RAW_MAGIC_NUMBER
98
from ._bootstrap_external import cache_from_source
109
from ._bootstrap_external import decode_source
1110
from ._bootstrap_external import source_from_cache
@@ -18,7 +17,7 @@
1817

1918
def source_hash(source_bytes):
2019
"Return the hash of *source_bytes* as used in hash-based pyc files."
21-
return _imp.source_hash(_RAW_MAGIC_NUMBER, source_bytes)
20+
return _imp.source_hash(_imp._pyc_magic_number_token, source_bytes)
2221

2322

2423
def resolve_name(name, package):

Lib/test/test_import/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,6 +3113,15 @@ def test_pyimport_addmodule_create(self):
31133113
self.assertIs(mod, sys.modules[name])
31143114

31153115

3116+
@cpython_only
3117+
class TestMagicNumber(unittest.TestCase):
3118+
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+
assert raw_magic_number == _imp._pyc_magic_number_token
3123+
3124+
31163125
if __name__ == '__main__':
31173126
# Test needs to be a package, so we can do relative imports.
31183127
unittest.main()

Lib/zipimport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ def _unmarshal_code(self, pathname, fullpath, fullname, data):
705705
source_bytes = _get_pyc_source(self, fullpath)
706706
if source_bytes is not None:
707707
source_hash = _imp.source_hash(
708-
_bootstrap_external._RAW_MAGIC_NUMBER,
708+
_imp._pyc_magic_number_token,
709709
source_bytes,
710710
)
711711

Python/import.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,22 +2477,7 @@ _PyImport_GetBuiltinModuleNames(void)
24772477
long
24782478
PyImport_GetMagicNumber(void)
24792479
{
2480-
union magic_number {
2481-
uint32_t raw;
2482-
uint8_t parts[4];
2483-
} n;
2484-
2485-
n.parts[0] = (uint16_t)PYC_MAGIC_NUMBER & 0xff;
2486-
n.parts[1] = (uint16_t)PYC_MAGIC_NUMBER >> 8;
2487-
n.parts[2] = '\r';
2488-
n.parts[3] = '\n';
2489-
2490-
#if PY_BIG_ENDIAN
2491-
uint32_t result = _Py_bswap32(n.raw);
2492-
return (long)result;
2493-
#else
2494-
return (long)n.raw;
2495-
#endif
2480+
return PYC_MAGIC_NUMBER_TOKEN;
24962481
}
24972482

24982483
extern const char * _PySys_ImplCacheTag;
@@ -4830,6 +4815,10 @@ imp_module_exec(PyObject *module)
48304815
return -1;
48314816
}
48324817

4818+
if (PyModule_AddIntConstant(module, "_pyc_magic_number_token", PYC_MAGIC_NUMBER_TOKEN) < 0) {
4819+
return -1;
4820+
}
4821+
48334822
return 0;
48344823
}
48354824

0 commit comments

Comments
 (0)