[pypy-commit] pypy default: produce syntax errors with the failing line attached even if the error comes

cfbolz pypy.commits at gmail.com
Thu Sep 19 18:06:50 EDT 2019


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: 
Changeset: r97556:df419b83d7f9
Date: 2019-09-20 00:03 +0200
http://bitbucket.org/pypy/pypy/changeset/df419b83d7f9/

Log:	produce syntax errors with the failing line attached even if the
	error comes from the astbuilder

diff --git a/pypy/interpreter/pycompiler.py b/pypy/interpreter/pycompiler.py
--- a/pypy/interpreter/pycompiler.py
+++ b/pypy/interpreter/pycompiler.py
@@ -125,13 +125,13 @@
         info = pyparse.CompileInfo(filename, mode, flags, future_pos)
         return self._compile_ast(node, info)
 
-    def _compile_ast(self, node, info):
+    def _compile_ast(self, node, info, source=None):
         space = self.space
         try:
             mod = optimize.optimize_ast(space, node, info)
             code = codegen.compile_ast(space, mod, info)
         except parseerror.SyntaxError as e:
-            raise OperationError(space.w_SyntaxError, e.wrap_info(space))
+            raise OperationError(space.w_SyntaxError, e.wrap_info(space, source))
         return code
 
     def compile_to_ast(self, source, filename, mode, flags):
@@ -146,11 +146,11 @@
         except parseerror.IndentationError as e:
             raise OperationError(space.w_IndentationError, e.wrap_info(space))
         except parseerror.SyntaxError as e:
-            raise OperationError(space.w_SyntaxError, e.wrap_info(space))
+            raise OperationError(space.w_SyntaxError, e.wrap_info(space, source))
         return mod
 
     def compile(self, source, filename, mode, flags, hidden_applevel=False):
         info = pyparse.CompileInfo(filename, mode, flags,
                                    hidden_applevel=hidden_applevel)
         mod = self._compile_to_ast(source, info)
-        return self._compile_ast(mod, info)
+        return self._compile_ast(mod, info, source)
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
@@ -12,9 +12,13 @@
         self.filename = filename
         self.lastlineno = lastlineno
 
-    def wrap_info(self, space):
+    def wrap_info(self, space, source=None):
+        text = self.text
+        if text is None and source is not None and self.lineno:
+            lines = source.splitlines(True)
+            text = lines[self.lineno - 1]
         w_filename = space.newtext_or_none(self.filename)
-        w_text = space.newtext_or_none(self.text)
+        w_text = space.newtext_or_none(text)
         return space.newtuple([space.newtext(self.msg),
                                space.newtuple([w_filename,
                                                space.newint(self.lineno),
diff --git a/pypy/interpreter/test/test_compiler.py b/pypy/interpreter/test/test_compiler.py
--- a/pypy/interpreter/test/test_compiler.py
+++ b/pypy/interpreter/test/test_compiler.py
@@ -1111,3 +1111,9 @@
             skip()
         code = 'u"""\\\n# -*- coding: utf-8 -*-\n\xc2\xa4"""\n'
         assert eval(code) == u'# -*- coding: utf-8 -*-\n\xc2\xa4'
+
+    def test_asterror_has_line_without_file(self):
+        code = u"print(1)\na/2 = 5\n"
+        with raises(SyntaxError) as excinfo:
+            compile(code, 'not a file!', 'exec')
+        assert excinfo.value.text == "a/2 = 5\n"
diff --git a/pypy/interpreter/test/test_syntax.py b/pypy/interpreter/test/test_syntax.py
--- a/pypy/interpreter/test/test_syntax.py
+++ b/pypy/interpreter/test/test_syntax.py
@@ -659,7 +659,7 @@
             exec program
         except SyntaxError as e:
             assert e.lineno == 1
-            assert e.text is None
+            assert e.text == program
         else:
             raise Exception("no SyntaxError??")
 


More information about the pypy-commit mailing list