summaryrefslogtreecommitdiffstats
path: root/Lib/importlib/_bootstrap.py
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2013-06-16 22:37:53 (GMT)
committerBrett Cannon <brett@python.org>2013-06-16 22:37:53 (GMT)
commitf24fecd4ac9050799d02a8354b7acfa12b65b1d3 (patch)
tree332e7e70c73bfbe61754e95a173aadbbe6e004a4 /Lib/importlib/_bootstrap.py
parentf4375ef4d458bf24610ffef591f8197a3dbf0b35 (diff)
downloadcpython-f24fecd4ac9050799d02a8354b7acfa12b65b1d3.zip
cpython-f24fecd4ac9050799d02a8354b7acfa12b65b1d3.tar.gz
cpython-f24fecd4ac9050799d02a8354b7acfa12b65b1d3.tar.bz2
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.
Diffstat (limited to 'Lib/importlib/_bootstrap.py')
-rw-r--r--Lib/importlib/_bootstrap.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index 0668dbd..37fe083 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -723,6 +723,18 @@ def _code_to_bytecode(code, mtime=0, source_size=0):
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 @@ class SourceLoader(_LoaderBasics):
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.