[Python-checkins] cpython: Issue #13959: Keep imp.get_magic() in C code, but cache in importlib
brett.cannon
python-checkins at python.org
Sat Apr 21 00:04:11 CEST 2012
http://hg.python.org/cpython/rev/c820aa9c0c00
changeset: 76445:c820aa9c0c00
user: Brett Cannon <brett at python.org>
date: Fri Apr 20 18:04:03 2012 -0400
summary:
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.
files:
Lib/imp.py | 4 +++-
Lib/importlib/_bootstrap.py | 12 ++++++++----
Python/importlib.h | Bin
3 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/Lib/imp.py b/Lib/imp.py
--- a/Lib/imp.py
+++ b/Lib/imp.py
@@ -11,8 +11,10 @@
init_builtin, init_frozen, is_builtin, is_frozen,
_fix_co_filename)
# Can (probably) move to importlib
-from _imp import (get_magic, get_tag, get_suffixes, cache_from_source,
+from _imp import (get_tag, get_suffixes, cache_from_source,
source_from_cache)
+# Could move out of _imp, but not worth the code
+from _imp import get_magic
# Should be re-implemented here (and mostly deprecated)
from _imp import (find_module, NullImporter,
SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION,
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -401,9 +401,9 @@
magic = data[:4]
raw_timestamp = data[4:8]
raw_size = data[8:12]
- if len(magic) != 4 or magic != _imp.get_magic():
- raise ImportError("bad magic number in {}".format(fullname),
- name=fullname, path=bytecode_path)
+ if magic != _MAGIC_NUMBER:
+ msg = 'bad magic number in {!r}: {!r}'.format(fullname, magic)
+ raise ImportError(msg, name=fullname, path=bytecode_path)
elif len(raw_timestamp) != 4:
message = 'bad timestamp in {}'.format(fullname)
verbose_message(message)
@@ -549,7 +549,7 @@
# If e.g. Jython ever implements imp.cache_from_source to have
# their own cached file format, this block of code will most likely
# throw an exception.
- data = bytearray(_imp.get_magic())
+ data = bytearray(_MAGIC_NUMBER)
data.extend(_w_long(source_mtime))
data.extend(_w_long(len(source_bytes)))
data.extend(marshal.dumps(code_object))
@@ -1115,6 +1115,9 @@
return _handle_fromlist(module, fromlist, _gcd_import)
+_MAGIC_NUMBER = None # Set in _setup()
+
+
def _setup(sys_module, _imp_module):
"""Setup importlib by importing needed built-in modules and injecting them
into the global namespace.
@@ -1158,6 +1161,7 @@
setattr(self_module, 'path_sep', path_sep)
# Constants
setattr(self_module, '_relax_case', _make_relax_case())
+ setattr(self_module, '_MAGIC_NUMBER', _imp_module.get_magic())
def _install(sys_module, _imp_module):
diff --git a/Python/importlib.h b/Python/importlib.h
index 88b89edb779b33dda3c6b8f3af878d66bde2fd99..0e2c734d9d7d0a6f9447bb042bb3d4da2d390878
GIT binary patch
[stripped]
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list