Skip to content

Commit 6be55f1

Browse files
authored
[3.13] gh-119724: Revert "bpo-45759: Better error messages for non-matching 'elif'/'else' statements (GH-29513)" (GH-119974) (GH-120013)
This reverts commit 1c8f912. (cherry picked from commit 31a4fb3)
1 parent e5fb3a2 commit 6be55f1

File tree

4 files changed

+454
-611
lines changed

4 files changed

+454
-611
lines changed

Grammar/python.gram

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ simple_stmt[stmt_ty] (memo):
127127
| &'nonlocal' nonlocal_stmt
128128

129129
compound_stmt[stmt_ty]:
130-
| invalid_compound_stmt
131130
| &('def' | '@' | 'async') function_def
132131
| &'if' if_stmt
133132
| &('class' | '@') class_def
@@ -1317,10 +1316,6 @@ invalid_import_from_targets:
13171316
| token=NEWLINE {
13181317
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
13191318

1320-
invalid_compound_stmt:
1321-
| a='elif' named_expression ':' { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "'elif' must match an if-statement here") }
1322-
| a='else' ':' { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "'else' must match a valid statement here") }
1323-
13241319
invalid_with_stmt:
13251320
| ['async'] 'with' ','.(expression ['as' star_target])+ NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
13261321
| ['async'] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }

Lib/test/test_syntax.py

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,28 +1853,6 @@
18531853
Traceback (most recent call last):
18541854
SyntaxError: positional patterns follow keyword patterns
18551855
1856-
Non-matching 'elif'/'else' statements:
1857-
1858-
>>> if a == b:
1859-
... ...
1860-
... elif a == c:
1861-
Traceback (most recent call last):
1862-
SyntaxError: 'elif' must match an if-statement here
1863-
1864-
>>> if x == y:
1865-
... ...
1866-
... else:
1867-
Traceback (most recent call last):
1868-
SyntaxError: 'else' must match a valid statement here
1869-
1870-
>>> elif m == n:
1871-
Traceback (most recent call last):
1872-
SyntaxError: 'elif' must match an if-statement here
1873-
1874-
>>> else:
1875-
Traceback (most recent call last):
1876-
SyntaxError: 'else' must match a valid statement here
1877-
18781856
Uses of the star operator which should fail:
18791857
18801858
A[:*b]
@@ -2167,8 +2145,8 @@ def _check_error(self, code, errtext,
21672145
lineno=None, offset=None, end_lineno=None, end_offset=None):
21682146
"""Check that compiling code raises SyntaxError with errtext.
21692147
2170-
errtext is a regular expression that must be present in the
2171-
test of the exception raised. If subclass is specified, it
2148+
errtest is a regular expression that must be present in the
2149+
test of the exception raised. If subclass is specified it
21722150
is the expected subclass of SyntaxError (e.g. IndentationError).
21732151
"""
21742152
try:
@@ -2192,22 +2170,6 @@ def _check_error(self, code, errtext,
21922170
else:
21932171
self.fail("compile() did not raise SyntaxError")
21942172

2195-
def _check_noerror(self, code,
2196-
errtext="compile() raised unexpected SyntaxError",
2197-
filename="<testcase>", mode="exec", subclass=None):
2198-
"""Check that compiling code does not raise a SyntaxError.
2199-
2200-
errtext is the message passed to self.fail if there is
2201-
a SyntaxError. If the subclass parameter is specified,
2202-
it is the subclass of SyntaxError (e.g. IndentationError)
2203-
that the raised error is checked against.
2204-
"""
2205-
try:
2206-
compile(code, filename, mode)
2207-
except SyntaxError as err:
2208-
if (not subclass) or isinstance(err, subclass):
2209-
self.fail(errtext)
2210-
22112173
def test_expression_with_assignment(self):
22122174
self._check_error(
22132175
"print(end1 + end2 = ' ')",
@@ -2609,25 +2571,6 @@ def test_syntax_error_on_deeply_nested_blocks(self):
26092571
"""
26102572
self._check_error(source, "too many statically nested blocks")
26112573

2612-
def test_syntax_error_non_matching_elif_else_statements(self):
2613-
# Check bpo-45759: 'elif' statements that doesn't match an
2614-
# if-statement or 'else' statements that doesn't match any
2615-
# valid else-able statement (e.g. 'while')
2616-
self._check_error(
2617-
"elif m == n:\n ...",
2618-
"'elif' must match an if-statement here")
2619-
self._check_error(
2620-
"else:\n ...",
2621-
"'else' must match a valid statement here")
2622-
self._check_noerror("if a == b:\n ...\nelif a == c:\n ...")
2623-
self._check_noerror("if x == y:\n ...\nelse:\n ...")
2624-
self._check_error(
2625-
"else = 123",
2626-
"invalid syntax")
2627-
self._check_error(
2628-
"elif 55 = 123",
2629-
"cannot assign to literal here")
2630-
26312574
@support.cpython_only
26322575
def test_error_on_parser_stack_overflow(self):
26332576
source = "-" * 100000 + "4"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Reverted improvements to error messages for ``elif``/``else`` statements not
2+
matching any valid statements, which made in hard to locate the syntax
3+
errors inside those ``elif``/``else`` blocks.

0 commit comments

Comments
 (0)