Mercurial > cpython-lru_cache-weakref
diff Lib/importlib/_bootstrap.py @ 76303:2dd046be2c88
Issue #2377: Make importlib the implementation of __import__().
importlib._bootstrap is now frozen into Python/importlib.h and stored
as _frozen_importlib in sys.modules. Py_Initialize() loads the frozen
code along with sys and imp and then uses _frozen_importlib._install()
to set builtins.__import__() w/ _frozen_importlib.__import__().
| author | Brett Cannon <brett@python.org> |
|---|---|
| date | Sat, 14 Apr 2012 14:10:13 -0400 |
| parents | c071af95772a |
| children | e2f48b076012 |
line wrap: on
line diff
--- a/Lib/importlib/_bootstrap.py Sat Apr 14 16:01:49 2012 +0200 +++ b/Lib/importlib/_bootstrap.py Sat Apr 14 14:10:13 2012 -0400 @@ -160,6 +160,13 @@ # Finder/loader utility code ################################################## +def verbose_message(message, *args): + """Print the message to stderr if -v/PYTHONVERBOSE is turned on.""" + if sys.flags.verbose: + if not message.startswith('#') and not message.startswith('import '): + message = '# ' + message + print(message.format(*args), file=sys.stderr) + def set_package(fxn): """Set __package__ on the returned module.""" @@ -388,9 +395,13 @@ raise ImportError("bad magic number in {}".format(fullname), name=fullname, path=bytecode_path) elif len(raw_timestamp) != 4: - raise EOFError("bad timestamp in {}".format(fullname)) + message = 'bad timestamp in {}'.format(fullname) + verbose_message(message) + raise EOFError(message) elif len(raw_size) != 4: - raise EOFError("bad size in {}".format(fullname)) + message = 'bad size in {}'.format(fullname) + verbose_message(message) + raise EOFError(message) if source_stats is not None: try: source_mtime = int(source_stats['mtime']) @@ -398,9 +409,10 @@ pass else: if _r_long(raw_timestamp) != source_mtime: - raise ImportError( - "bytecode is stale for {}".format(fullname), - name=fullname, path=bytecode_path) + message = 'bytecode is stale for {}'.format(fullname) + verbose_message(message) + raise ImportError(message, name=fullname, + path=bytecode_path) try: source_size = source_stats['size'] & 0xFFFFFFFF except KeyError: @@ -506,9 +518,13 @@ except (ImportError, EOFError): pass else: + verbose_message('{} matches {}', bytecode_path, + source_path) found = marshal.loads(bytes_data) if isinstance(found, code_type): imp._fix_co_filename(found, source_path) + verbose_message('code object from {}', + bytecode_path) return found else: msg = "Non-code object in {}" @@ -517,6 +533,7 @@ source_bytes = self.get_data(source_path) code_object = compile(source_bytes, source_path, 'exec', dont_inherit=True) + verbose_message('code object from {}', source_path) if (not sys.dont_write_bytecode and bytecode_path is not None and source_mtime is not None): # If e.g. Jython ever implements imp.cache_from_source to have @@ -528,6 +545,7 @@ data.extend(marshal.dumps(code_object)) try: self.set_data(bytecode_path, data) + verbose_message('wrote {!r}', bytecode_path) except NotImplementedError: pass return code_object @@ -596,6 +614,7 @@ return try: _write_atomic(path, data) + verbose_message('created {!r}', path) except (PermissionError, FileExistsError): # Don't worry if you can't write bytecode or someone is writing # it at the same time. @@ -615,6 +634,7 @@ bytes_data = self._bytes_from_bytecode(fullname, data, path, None) found = marshal.loads(bytes_data) if isinstance(found, code_type): + verbose_message('code object from {!r}', path) return found else: raise ImportError("Non-code object in {}".format(path), @@ -644,7 +664,9 @@ """Load an extension module.""" is_reload = fullname in sys.modules try: - return imp.load_dynamic(fullname, self._path) + module = imp.load_dynamic(fullname, self._path) + verbose_message('extension module loaded from {!r}', self._path) + return module except: if not is_reload and fullname in sys.modules: del sys.modules[fullname] @@ -953,6 +975,7 @@ elif name not in sys.modules: # The parent import may have already imported this module. loader.load_module(name) + verbose_message('import {!r} # {!r}', name, loader) # Backwards-compatibility; be nicer to skip the dict lookup. module = sys.modules[name] if parent:
