Mercurial > cpython-lru_cache-weakref
diff Lib/importlib/_bootstrap.py @ 84173:bdd60bedf933
Issue #18076: Introduce imoportlib.util.decode_source().
The helper function makes it easier to implement
imoprtlib.abc.InspectLoader.get_source() by making that function
require just the raw bytes for source code and handling all other
details.
| author | Brett Cannon <brett@python.org> |
|---|---|
| date | Sun, 16 Jun 2013 18:37:53 -0400 |
| parents | e353f64dfd95 |
| children | 03ad17618ac4 |
line wrap: on
line diff
--- a/Lib/importlib/_bootstrap.py Sun Jun 16 18:05:54 2013 -0400 +++ b/Lib/importlib/_bootstrap.py Sun Jun 16 18:37:53 2013 -0400 @@ -723,6 +723,18 @@ return data +def decode_source(source_bytes): + """Decode bytes representing source code and return the string. + + Universal newline support is used in the decoding. + """ + import tokenize # To avoid bootstrap issues. + source_bytes_readline = _io.BytesIO(source_bytes).readline + encoding = tokenize.detect_encoding(source_bytes_readline) + newline_decoder = _io.IncrementalNewlineDecoder(None, True) + return newline_decoder.decode(source_bytes.decode(encoding[0])) + + # Loaders ##################################################################### class BuiltinImporter: @@ -965,11 +977,7 @@ except OSError as exc: raise ImportError("source not available through get_data()", name=fullname) from exc - import tokenize - readsource = _io.BytesIO(source_bytes).readline - encoding = tokenize.detect_encoding(readsource) - newline_decoder = _io.IncrementalNewlineDecoder(None, True) - return newline_decoder.decode(source_bytes.decode(encoding[0])) + return decode_source(source_bytes) def source_to_code(self, data, path, *, _optimize=-1): """Return the code object compiled from source.
