Skip to content

Commit 772d809

Browse files
mjpietersambv
authored andcommitted
bpo-31161: only check for parens error for SyntaxError (#3082)
Subclasses such as IndentError and TabError should not have this message applied.
1 parent 5df8c58 commit 772d809

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

Lib/test/test_exceptions.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,34 @@ def ckmsg(src, msg):
156156
ckmsg(s, "'continue' not properly in loop")
157157
ckmsg("continue\n", "'continue' not properly in loop")
158158

159+
def testSyntaxErrorMissingParens(self):
160+
def ckmsg(src, msg, exception=SyntaxError):
161+
try:
162+
compile(src, '<fragment>', 'exec')
163+
except exception as e:
164+
if e.msg != msg:
165+
self.fail("expected %s, got %s" % (msg, e.msg))
166+
else:
167+
self.fail("failed to get expected SyntaxError")
168+
169+
s = '''print "old style"'''
170+
ckmsg(s, "Missing parentheses in call to 'print'. "
171+
"Did you mean print(\"old style\")?")
172+
173+
s = '''print "old style",'''
174+
ckmsg(s, "Missing parentheses in call to 'print'. "
175+
"Did you mean print(\"old style\", end=\" \")?")
176+
177+
s = '''exec "old style"'''
178+
ckmsg(s, "Missing parentheses in call to 'exec'")
179+
180+
# should not apply to subclasses, see issue #31161
181+
s = '''if True:\nprint "No indent"'''
182+
ckmsg(s, "expected an indented block", IndentationError)
183+
184+
s = '''if True:\n print()\n\texec "mixed tabs and spaces"'''
185+
ckmsg(s, "inconsistent use of tabs and spaces in indentation", TabError)
186+
159187
def testSyntaxErrorOffset(self):
160188
def check(src, lineno, offset):
161189
with self.assertRaises(SyntaxError) as cm:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-31161: Make sure the 'Missing parentheses' syntax error message is
14+
only applied to SyntaxError, not to subclasses. Patch by Martijn Pieters.
15+
1316
- bpo-30814: Fixed a race condition when import a submodule from a package.
1417

1518
- bpo-30736: The internal unicodedata database has been upgraded to Unicode

Objects/exceptions.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,11 +1352,16 @@ SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
13521352

13531353
Py_DECREF(info);
13541354

1355-
/* Issue #21669: Custom error for 'print' & 'exec' as statements */
1356-
if (self->text && PyUnicode_Check(self->text)) {
1357-
if (_report_missing_parentheses(self) < 0) {
1358-
return -1;
1359-
}
1355+
/*
1356+
* Issue #21669: Custom error for 'print' & 'exec' as statements
1357+
*
1358+
* Only applies to SyntaxError instances, not to subclasses such
1359+
* as TabError or IndentationError (see issue #31161)
1360+
*/
1361+
if ((PyObject*)Py_TYPE(self) == PyExc_SyntaxError &&
1362+
self->text && PyUnicode_Check(self->text) &&
1363+
_report_missing_parentheses(self) < 0) {
1364+
return -1;
13601365
}
13611366
}
13621367
return 0;

0 commit comments

Comments
 (0)