Kaydet (Commit) 2824cb50 authored tarafından Nick Coghlan's avatar Nick Coghlan

Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file

üst 8ecf5047
...@@ -845,12 +845,21 @@ class SourceLoader(_LoaderBasics): ...@@ -845,12 +845,21 @@ class SourceLoader(_LoaderBasics):
path = self.get_filename(fullname) path = self.get_filename(fullname)
try: try:
source_bytes = self.get_data(path) source_bytes = self.get_data(path)
except IOError: except IOError as exc:
raise ImportError("source not available through get_data()", raise ImportError("source not available through get_data()",
name=fullname) name=fullname) from exc
encoding = tokenize.detect_encoding(_io.BytesIO(source_bytes).readline) readsource = _io.BytesIO(source_bytes).readline
try:
encoding = tokenize.detect_encoding(readsource)
except SyntaxError as exc:
raise ImportError("Failed to detect encoding",
name=fullname) from exc
newline_decoder = _io.IncrementalNewlineDecoder(None, True) newline_decoder = _io.IncrementalNewlineDecoder(None, True)
return newline_decoder.decode(source_bytes.decode(encoding[0])) try:
return newline_decoder.decode(source_bytes.decode(encoding[0]))
except UnicodeDecodeError as exc:
raise ImportError("Failed to decode source file",
name=fullname) from exc
def get_code(self, fullname): def get_code(self, fullname):
"""Concrete implementation of InspectLoader.get_code. """Concrete implementation of InspectLoader.get_code.
......
...@@ -2048,7 +2048,7 @@ class ModuleScanner: ...@@ -2048,7 +2048,7 @@ class ModuleScanner:
if hasattr(loader, 'get_source'): if hasattr(loader, 'get_source'):
try: try:
source = loader.get_source(modname) source = loader.get_source(modname)
except UnicodeDecodeError: except Exception:
if onerror: if onerror:
onerror(modname) onerror(modname)
continue continue
......
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment