diff options
author | Martijn Pieters <github.com@zopatista.com> | 2017-08-22 20:11:09 (GMT) |
---|---|---|
committer | Ćukasz Langa <lukasz@langa.pl> | 2017-08-22 20:11:09 (GMT) |
commit | 680f04a926bce04e4320ba883068c345eba502a6 (patch) | |
tree | 1230385653b1591d47c03bfce0d814fe321777a0 | |
parent | 468534d92f6879e925c4cd8a228ea300abfb7e4d (diff) | |
download | cpython-680f04a926bce04e4320ba883068c345eba502a6.zip cpython-680f04a926bce04e4320ba883068c345eba502a6.tar.gz cpython-680f04a926bce04e4320ba883068c345eba502a6.tar.bz2 |
bpo-31161: only check for parens error for SyntaxError (#3083)
Subclasses such as IndentError and TabError should not have this message
applied.
-rw-r--r-- | Lib/test/test_exceptions.py | 28 | ||||
-rw-r--r-- | Misc/NEWS | 3 | ||||
-rw-r--r-- | Objects/exceptions.c | 15 |
3 files changed, 41 insertions, 5 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index 960fc0f..53851cb 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -156,6 +156,34 @@ class ExceptionTests(unittest.TestCase): 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: @@ -10,6 +10,9 @@ What's New in Python 3.6.3 release candidate 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-30597: ``print`` now shows expected input in custom error message when diff --git a/Objects/exceptions.c b/Objects/exceptions.c index f286ec0..271e293 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -1356,11 +1356,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; |