[Python-checkins] cpython: importlib.abc.SourceLoader.get_source() was re-raising SyntaxError and

brett.cannon python-checkins at python.org
Mon Jun 17 00:06:02 CEST 2013


http://hg.python.org/cpython/rev/e353f64dfd95
changeset:   84172:e353f64dfd95
user:        Brett Cannon <brett at python.org>
date:        Sun Jun 16 18:05:54 2013 -0400
summary:
  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.

files:
  Doc/whatsnew/3.4.rst        |     9 +
  Lib/importlib/_bootstrap.py |    14 +-
  Misc/NEWS                   |     3 +
  Python/importlib.h          |  3428 +++++++++++-----------
  4 files changed, 1721 insertions(+), 1733 deletions(-)


diff --git a/Doc/whatsnew/3.4.rst b/Doc/whatsnew/3.4.rst
--- a/Doc/whatsnew/3.4.rst
+++ b/Doc/whatsnew/3.4.rst
@@ -282,3 +282,12 @@
   it would write to is a symlink or a non-regular file. This is to act as a
   warning that import will overwrite those files with a regular file regardless
   of what type of file path they were originally.
+
+* :meth:`importlib.abc.SourceLoader.get_source` no longer raises
+  :exc:`ImportError` when the source code being loaded triggers a
+  :exc:`SyntaxError` or :exc:`UnicodeDecodeError`. As :exc:`ImportError` is
+  meant to be raised only when source code cannot be found but it should, it was
+  felt to be over-reaching/overloading of that meaning when the source code is
+  found but improperly structured. If you were catching ImportError before and
+  wish to continue to ignore syntax or decoding issues, catch all three
+  exceptions now.
\ No newline at end of file
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -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.
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -123,6 +123,9 @@
 Library
 -------
 
+- importlib.abc.SourceLoader.get_source() no longer changes SyntaxError or
+  UnicodeDecodeError into ImportError.
+
 - Issue #18058, 18057: Make the namespace package loader meet the
   importlib.abc.InspectLoader ABC, allowing for namespace packages to work with
   runpy.
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