changeset: 76445:c820aa9c0c00 user: Brett Cannon date: Fri Apr 20 18:04:03 2012 -0400 files: Lib/imp.py Lib/importlib/_bootstrap.py Python/importlib.h description: 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. diff -r a8484e56811b -r c820aa9c0c00 Lib/imp.py --- a/Lib/imp.py Fri Apr 20 17:34:59 2012 -0400 +++ b/Lib/imp.py Fri Apr 20 18:04:03 2012 -0400 @@ -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 -r a8484e56811b -r c820aa9c0c00 Lib/importlib/_bootstrap.py --- a/Lib/importlib/_bootstrap.py Fri Apr 20 17:34:59 2012 -0400 +++ b/Lib/importlib/_bootstrap.py Fri Apr 20 18:04:03 2012 -0400 @@ -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 -r a8484e56811b -r c820aa9c0c00 Python/importlib.h Binary file Python/importlib.h has changed