diff Lib/importlib/_bootstrap.py @ 84172:e353f64dfd95

importlib.abc.SourceLoader.get_source() was re-raising SyntaxError and UnicodeDecodeError as ImportError. That was over-reaching the point of raising ImportError in get_source() (which is to signal the source code was not found when it should have). Conflating the two exceptions with ImportError could lead to masking errors with the source which should be known outside of whether there was an error simply getting the source to begin with.
author Brett Cannon <brett@python.org>
date Sun, 16 Jun 2013 18:05:54 -0400
parents 205aa49e5cd5
children bdd60bedf933
line wrap: on
line diff
--- a/Lib/importlib/_bootstrap.py	Sun Jun 16 17:23:06 2013 -0400
+++ b/Lib/importlib/_bootstrap.py	Sun Jun 16 18:05:54 2013 -0400
@@ -959,25 +959,17 @@
 
     def get_source(self, fullname):
         """Concrete implementation of InspectLoader.get_source."""
-        import tokenize
         path = self.get_filename(fullname)
         try:
             source_bytes = self.get_data(path)
         except OSError as exc:
             raise ImportError("source not available through get_data()",
                               name=fullname) from exc
+        import tokenize
         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
+        encoding = tokenize.detect_encoding(readsource)
         newline_decoder = _io.IncrementalNewlineDecoder(None, True)
-        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
+        return newline_decoder.decode(source_bytes.decode(encoding[0]))
 
     def source_to_code(self, data, path, *, _optimize=-1):
         """Return the code object compiled from source.