Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 24117a7

Browse files
committed
Issue #13959: Keep imp.get_magic() in C code, but cache in importlib
for performance. While get_magic() could move to Lib/imp.py, having to support PyImport_GetMagicNumber() would lead to equal, if not more, C code than sticking with the status quo.
1 parent 9e924ed commit 24117a7

3 files changed

Lines changed: 1514 additions & 1503 deletions

File tree

Lib/imp.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
init_builtin, init_frozen, is_builtin, is_frozen,
1212
_fix_co_filename)
1313
# Can (probably) move to importlib
14-
from _imp import (get_magic, get_tag, get_suffixes, cache_from_source,
14+
from _imp import (get_tag, get_suffixes, cache_from_source,
1515
source_from_cache)
16+
# Could move out of _imp, but not worth the code
17+
from _imp import get_magic
1618
# Should be re-implemented here (and mostly deprecated)
1719
from _imp import (find_module, NullImporter,
1820
SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION,

Lib/importlib/_bootstrap.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,9 +401,9 @@ def _bytes_from_bytecode(self, fullname, data, bytecode_path, source_stats):
401401
magic = data[:4]
402402
raw_timestamp = data[4:8]
403403
raw_size = data[8:12]
404-
if len(magic) != 4 or magic != _imp.get_magic():
405-
raise ImportError("bad magic number in {}".format(fullname),
406-
name=fullname, path=bytecode_path)
404+
if magic != _MAGIC_NUMBER:
405+
msg = 'bad magic number in {!r}: {!r}'.format(fullname, magic)
406+
raise ImportError(msg, name=fullname, path=bytecode_path)
407407
elif len(raw_timestamp) != 4:
408408
message = 'bad timestamp in {}'.format(fullname)
409409
verbose_message(message)
@@ -549,7 +549,7 @@ def get_code(self, fullname):
549549
# If e.g. Jython ever implements imp.cache_from_source to have
550550
# their own cached file format, this block of code will most likely
551551
# throw an exception.
552-
data = bytearray(_imp.get_magic())
552+
data = bytearray(_MAGIC_NUMBER)
553553
data.extend(_w_long(source_mtime))
554554
data.extend(_w_long(len(source_bytes)))
555555
data.extend(marshal.dumps(code_object))
@@ -1115,6 +1115,9 @@ def __import__(name, globals={}, locals={}, fromlist=[], level=0):
11151115
return _handle_fromlist(module, fromlist, _gcd_import)
11161116

11171117

1118+
_MAGIC_NUMBER = None # Set in _setup()
1119+
1120+
11181121
def _setup(sys_module, _imp_module):
11191122
"""Setup importlib by importing needed built-in modules and injecting them
11201123
into the global namespace.
@@ -1158,6 +1161,7 @@ def _setup(sys_module, _imp_module):
11581161
setattr(self_module, 'path_sep', path_sep)
11591162
# Constants
11601163
setattr(self_module, '_relax_case', _make_relax_case())
1164+
setattr(self_module, '_MAGIC_NUMBER', _imp_module.get_magic())
11611165

11621166

11631167
def _install(sys_module, _imp_module):

0 commit comments

Comments
 (0)