Author: Carl Friedrich Bolz-Tereick <[email protected]>
Branch: py3.6
Changeset: r97370:de78bcdc0fad
Date: 2019-09-03 14:09 +0200
http://bitbucket.org/pypy/pypy/changeset/de78bcdc0fad/

Log:    fix semicolon handling, only make repair suggestions that are valid
        syntax

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
@@ -853,19 +853,31 @@
 
     def _set_legacy_print_statement_msg(self, space, text):
         text = text[len("print"):]
+        text = text.strip()
         if text.endswith(";"):
             end = len(text) - 1
             assert end >= 0
-            text = text[:end]
-        text = text.strip()
+            text = text[:end].strip()
 
         maybe_end = ""
         if text.endswith(","):
             maybe_end = " end=\" \""
 
-        self.w_msg = space.newtext(
-            "Missing parentheses in call to 'print'. Did you mean 
print(%s%s)?" % (
-                text, maybe_end))
+        suggestion = "print(%s%s)" % (
+                text, maybe_end)
+
+        if "%" in suggestion:
+            import pdb; pdb.set_trace()
+        # try to see whether the suggestion would compile, otherwise discard it
+        compiler = space.createcompiler()
+        try:
+            compiler.compile(suggestion, '?', 'eval', 0)
+        except OperationError:
+            pass
+        else:
+            self.w_msg = space.newtext(
+                "Missing parentheses in call to 'print'. Did you mean %s?" % (
+                    suggestion, ))
 
 
 W_SyntaxError.typedef = TypeDef(
diff --git a/pypy/module/exceptions/test/test_exc.py 
b/pypy/module/exceptions/test/test_exc.py
--- a/pypy/module/exceptions/test/test_exc.py
+++ b/pypy/module/exceptions/test/test_exc.py
@@ -461,6 +461,15 @@
         check(
             "print 1, \t",
             "Missing parentheses in call to 'print'. Did you mean print(1, 
end=\" \")?")
+        check(
+            "print 'a'\n;\t ",
+            "Missing parentheses in call to 'print'. Did you mean print('a')?")
+        check(
+            "print p;",
+            "Missing parentheses in call to 'print'. Did you mean print(p)?")
+        check("print %", "invalid syntax")
+        check("print 1 1",
+            "Missing parentheses in call to 'print'")
 
     def test_importerror_kwarg_error(self):
         msg = "'invalid' is an invalid keyword argument for this function"
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to