summaryrefslogtreecommitdiffstats
path: root/Lib/importlib
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2012-07-15 12:12:14 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2012-07-15 12:12:14 (GMT)
commit2824cb507d80a6d609d4a2f4400185cbd08b4c70 (patch)
treebf42d60ed1446af04a3433b2fc5c0ca05a5a3e79 /Lib/importlib
parent8ecf50474ce3d0ef34fbcce940566c70370e57ad (diff)
downloadcpython-2824cb507d80a6d609d4a2f4400185cbd08b4c70.zip
cpython-2824cb507d80a6d609d4a2f4400185cbd08b4c70.tar.gz
cpython-2824cb507d80a6d609d4a2f4400185cbd08b4c70.tar.bz2
Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Diffstat (limited to 'Lib/importlib')
-rw-r--r--Lib/importlib/_bootstrap.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index ee39a31..780928a 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -845,12 +845,21 @@ class SourceLoader(_LoaderBasics):
path = self.get_filename(fullname)
try:
source_bytes = self.get_data(path)
- except IOError:
+ except IOError as exc:
raise ImportError("source not available through get_data()",
- name=fullname)
- encoding = tokenize.detect_encoding(_io.BytesIO(source_bytes).readline)
+ name=fullname) from exc
+ 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)
- 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):
"""Concrete implementation of InspectLoader.get_code.