Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 31a4fb3

Browse filesBrowse files
authored
gh-119724: Revert "bpo-45759: Better error messages for non-matching 'elif'/'else' statements (#29513)" (#119974)
This reverts commit 1c8f912.
1 parent 105f22e commit 31a4fb3
Copy full SHA for 31a4fb3

File tree

Expand file treeCollapse file tree

4 files changed

+455
-612
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+455
-612
lines changed

‎Grammar/python.gram

Copy file name to clipboardExpand all lines: Grammar/python.gram
-5Lines 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
@@ -1323,10 +1322,6 @@ invalid_import_from_targets:
13231322
| token=NEWLINE {
13241323
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }
13251324

1326-
invalid_compound_stmt:
1327-
| a='elif' named_expression ':' { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "'elif' must match an if-statement here") }
1328-
| a='else' ':' { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "'else' must match a valid statement here") }
1329-
13301325
invalid_with_stmt:
13311326
| ['async'] 'with' ','.(expression ['as' star_target])+ NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
13321327
| ['async'] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }

‎Lib/test/test_syntax.py

Copy file name to clipboardExpand all lines: Lib/test/test_syntax.py
+2-59Lines 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"
+3Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.