[Python-checkins] cpython: Issue #15343: A lot more than just unicode decoding can go wrong when

nick.coghlan python-checkins at python.org
Sun Jul 15 14:12:29 CEST 2012


http://hg.python.org/cpython/rev/7d202353a728
changeset:   78112:7d202353a728
user:        Nick Coghlan <ncoghlan at gmail.com>
date:        Sun Jul 15 22:12:14 2012 +1000
summary:
  Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file

files:
  Lib/importlib/_bootstrap.py |    17 +-
  Lib/pydoc.py                |     2 +-
  Python/importlib.h          |  4283 +++++++++++-----------
  3 files changed, 2165 insertions(+), 2137 deletions(-)


diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -845,12 +845,21 @@
         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.
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -2048,7 +2048,7 @@
                 if hasattr(loader, 'get_source'):
                     try:
                         source = loader.get_source(modname)
-                    except UnicodeDecodeError:
+                    except Exception:
                         if onerror:
                             onerror(modname)
                         continue
diff --git a/Python/importlib.h b/Python/importlib.h
--- a/Python/importlib.h
+++ b/Python/importlib.h
[stripped]

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list