[pypy-commit] pypy py3.5: Let SyntaxError tracebacks show the bad code line (hard to test)

rlamy pypy.commits at gmail.com
Thu Nov 16 14:41:08 EST 2017


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py3.5
Changeset: r93063:12061ebde67d
Date: 2017-11-16 19:40 +0000
http://bitbucket.org/pypy/pypy/changeset/12061ebde67d/

Log:	Let SyntaxError tracebacks show the bad code line (hard to test)

diff --git a/pypy/interpreter/pyparser/error.py b/pypy/interpreter/pyparser/error.py
--- a/pypy/interpreter/pyparser/error.py
+++ b/pypy/interpreter/pyparser/error.py
@@ -14,7 +14,20 @@
     def wrap_info(self, space):
         w_text = w_filename = space.w_None
         offset = self.offset
-        if self.text is not None:
+        w_lineno = space.newint(self.lineno)
+        if self.filename is not None:
+            w_filename = space.newfilename(self.filename)
+        if self.text is None and self.filename is not None:
+            w_text = space.appexec([w_filename, w_lineno],
+                """(filename, lineno):
+                    try:
+                        with open(filename) as f:
+                            for _ in range(lineno):
+                                f.read()
+                            return f.read()
+                    except:  # we can't allow any exceptions here!
+                        return None""")
+        elif self.text is not None:
             from rpython.rlib.runicode import str_decode_utf_8
             # self.text may not be UTF-8 in case of decoding errors.
             # adjust the encoded text offset to a decoded offset
@@ -29,20 +42,15 @@
                 text, _ = str_decode_utf_8(self.text, len(self.text),
                                            'replace')
             w_text = space.newunicode(text)
-        if self.filename is not None:
-            w_filename = space.newfilename(self.filename)
-        return space.newtuple([space.newtext(self.msg),
-                               space.newtuple([w_filename,
-                                               space.newint(self.lineno),
-                                               space.newint(offset),
-                                               w_text,
-                                               space.newint(self.lastlineno)])])
+        return space.newtuple([
+            space.newtext(self.msg),
+            space.newtuple([
+                w_filename, w_lineno, space.newint(offset),
+                w_text, space.newint(self.lastlineno)])])
 
     def __str__(self):
-        return "%s at pos (%d, %d) in %r" % (self.__class__.__name__,
-                                             self.lineno,
-                                             self.offset,
-                                             self.text)
+        return "%s at pos (%d, %d) in %r" % (
+            self.__class__.__name__, self.lineno, self.offset, self.text)
 
 class IndentationError(SyntaxError):
     pass
@@ -51,10 +59,11 @@
     def __init__(self, lineno=0, offset=0, text=None, filename=None,
                  lastlineno=0):
         msg = "inconsistent use of tabs and spaces in indentation"
-        IndentationError.__init__(self, msg, lineno, offset, text, filename, lastlineno)
+        IndentationError.__init__(
+            self, msg, lineno, offset, text, filename, lastlineno)
 
 class ASTError(Exception):
-    def __init__(self, msg, ast_node ):
+    def __init__(self, msg, ast_node):
         self.msg = msg
         self.ast_node = ast_node
 
diff --git a/pypy/module/exceptions/interp_exceptions.py b/pypy/module/exceptions/interp_exceptions.py
--- a/pypy/module/exceptions/interp_exceptions.py
+++ b/pypy/module/exceptions/interp_exceptions.py
@@ -733,10 +733,14 @@
             self.w_msg = args_w[0]
         if len(args_w) == 2:
             values_w = space.fixedview(args_w[1])
-            if len(values_w) > 0: self.w_filename   = values_w[0]
-            if len(values_w) > 1: self.w_lineno     = values_w[1]
-            if len(values_w) > 2: self.w_offset     = values_w[2]
-            if len(values_w) > 3: self.w_text       = values_w[3]
+            if len(values_w) > 0:
+                self.w_filename = values_w[0]
+            if len(values_w) > 1:
+                self.w_lineno = values_w[1]
+            if len(values_w) > 2:
+                self.w_offset = values_w[2]
+            if len(values_w) > 3:
+                self.w_text = values_w[3]
             if len(values_w) > 4:
                 self.w_lastlineno = values_w[4]   # PyPy extension
                 # kill the extra items from args_w to prevent undesired effects


More information about the pypy-commit mailing list