[Python-checkins] bpo-31161: only check for parens error for SyntaxError (#3082)

Łukasz Langa webhook-mailer at python.org
Tue Aug 22 16:16:26 EDT 2017


https://github.com/python/cpython/commit/772d809a63f40fd35679da3fb115cdf7fa81bd20
commit: 772d809a63f40fd35679da3fb115cdf7fa81bd20
branch: master
author: Martijn Pieters <github.com at zopatista.com>
committer: Łukasz Langa <lukasz at langa.pl>
date: 2017-08-22T13:16:23-07:00
summary:

bpo-31161: only check for parens error for SyntaxError (#3082)

Subclasses such as IndentError and TabError should not have this message
applied.

files:
M Lib/test/test_exceptions.py
M Misc/NEWS
M Objects/exceptions.c

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 3378ceb701c..a25f3bf03a5 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -156,6 +156,34 @@ def ckmsg(src, msg):
         ckmsg(s, "'continue' not properly in loop")
         ckmsg("continue\n", "'continue' not properly in loop")
 
+    def testSyntaxErrorMissingParens(self):
+        def ckmsg(src, msg, exception=SyntaxError):
+            try:
+                compile(src, '<fragment>', 'exec')
+            except exception as e:
+                if e.msg != msg:
+                    self.fail("expected %s, got %s" % (msg, e.msg))
+            else:
+                self.fail("failed to get expected SyntaxError")
+
+        s = '''print "old style"'''
+        ckmsg(s, "Missing parentheses in call to 'print'. "
+                 "Did you mean print(\"old style\")?")
+
+        s = '''print "old style",'''
+        ckmsg(s, "Missing parentheses in call to 'print'. "
+                 "Did you mean print(\"old style\", end=\" \")?")
+
+        s = '''exec "old style"'''
+        ckmsg(s, "Missing parentheses in call to 'exec'")
+
+        # should not apply to subclasses, see issue #31161
+        s = '''if True:\nprint "No indent"'''
+        ckmsg(s, "expected an indented block", IndentationError)
+
+        s = '''if True:\n        print()\n\texec "mixed tabs and spaces"'''
+        ckmsg(s, "inconsistent use of tabs and spaces in indentation", TabError)
+
     def testSyntaxErrorOffset(self):
         def check(src, lineno, offset):
             with self.assertRaises(SyntaxError) as cm:
diff --git a/Misc/NEWS b/Misc/NEWS
index 1ef28927266..9927e3490f9 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
 Core and Builtins
 -----------------
 
+- bpo-31161: Make sure the 'Missing parentheses' syntax error message is
+  only applied to SyntaxError, not to subclasses. Patch by Martijn Pieters.
+
 - bpo-30814: Fixed a race condition when import a submodule from a package.
 
 - bpo-30736: The internal unicodedata database has been upgraded to Unicode
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index 190ad065402..13c1be90d88 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -1352,11 +1352,16 @@ SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
 
         Py_DECREF(info);
 
-        /* Issue #21669: Custom error for 'print' & 'exec' as statements */
-        if (self->text && PyUnicode_Check(self->text)) {
-            if (_report_missing_parentheses(self) < 0) {
-                return -1;
-            }
+        /*
+         * Issue #21669: Custom error for 'print' & 'exec' as statements
+         *
+         * Only applies to SyntaxError instances, not to subclasses such
+         * as TabError or IndentationError (see issue #31161)
+         */
+        if ((PyObject*)Py_TYPE(self) == PyExc_SyntaxError &&
+                self->text && PyUnicode_Check(self->text) &&
+                _report_missing_parentheses(self) < 0) {
+            return -1;
         }
     }
     return 0;



More information about the Python-checkins mailing list