Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[3.13] gh-119724: Revert "bpo-45759: Better error messages for non-matching 'elif'/'else' statements (GH-29513)" (GH-119974)#120013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
encukou merged 2 commits intopython:3.13fromYhg1s:backport-31a4fb3-3.13
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletionsGrammar/python.gram
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -127,7 +127,6 @@ simple_stmt[stmt_ty] (memo):
| &'nonlocal' nonlocal_stmt

compound_stmt[stmt_ty]:
| invalid_compound_stmt
| &('def' | '@' | 'async') function_def
| &'if' if_stmt
| &('class' | '@') class_def
Expand DownExpand Up@@ -1317,10 +1316,6 @@ invalid_import_from_targets:
| token=NEWLINE {
RAISE_SYNTAX_ERROR_STARTING_FROM(token, "Expected one or more names after 'import'") }

invalid_compound_stmt:
| a='elif' named_expression ':' { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "'elif' must match an if-statement here") }
| a='else' ':' { RAISE_SYNTAX_ERROR_STARTING_FROM(a, "'else' must match a valid statement here") }

invalid_with_stmt:
| ['async'] 'with' ','.(expression ['as' star_target])+ NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| ['async'] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
Expand Down
61 changes: 2 additions & 59 deletionsLib/test/test_syntax.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1853,28 +1853,6 @@
Traceback (most recent call last):
SyntaxError: positional patterns follow keyword patterns

Non-matching 'elif'/'else' statements:

>>> if a == b:
... ...
... elif a == c:
Traceback (most recent call last):
SyntaxError: 'elif' must match an if-statement here

>>> if x == y:
... ...
... else:
Traceback (most recent call last):
SyntaxError: 'else' must match a valid statement here

>>> elif m == n:
Traceback (most recent call last):
SyntaxError: 'elif' must match an if-statement here

>>> else:
Traceback (most recent call last):
SyntaxError: 'else' must match a valid statement here

Uses of the star operator which should fail:

A[:*b]
Expand DownExpand Up@@ -2167,8 +2145,8 @@ def _check_error(self, code, errtext,
lineno=None, offset=None, end_lineno=None, end_offset=None):
"""Check that compiling code raises SyntaxError with errtext.

errtext is a regular expression that must be present in the
test of the exception raised. If subclass is specified, it
errtest is a regular expression that must be present in the
test of the exception raised.If subclass is specified it
is the expected subclass of SyntaxError (e.g. IndentationError).
"""
try:
Expand All@@ -2192,22 +2170,6 @@ def _check_error(self, code, errtext,
else:
self.fail("compile() did not raise SyntaxError")

def _check_noerror(self, code,
errtext="compile() raised unexpected SyntaxError",
filename="<testcase>", mode="exec", subclass=None):
"""Check that compiling code does not raise a SyntaxError.

errtext is the message passed to self.fail if there is
a SyntaxError. If the subclass parameter is specified,
it is the subclass of SyntaxError (e.g. IndentationError)
that the raised error is checked against.
"""
try:
compile(code, filename, mode)
except SyntaxError as err:
if (not subclass) or isinstance(err, subclass):
self.fail(errtext)

def test_expression_with_assignment(self):
self._check_error(
"print(end1 + end2 = ' ')",
Expand DownExpand Up@@ -2609,25 +2571,6 @@ def test_syntax_error_on_deeply_nested_blocks(self):
"""
self._check_error(source, "too many statically nested blocks")

def test_syntax_error_non_matching_elif_else_statements(self):
# Check bpo-45759: 'elif' statements that doesn't match an
# if-statement or 'else' statements that doesn't match any
# valid else-able statement (e.g. 'while')
self._check_error(
"elif m == n:\n ...",
"'elif' must match an if-statement here")
self._check_error(
"else:\n ...",
"'else' must match a valid statement here")
self._check_noerror("if a == b:\n ...\nelif a == c:\n ...")
self._check_noerror("if x == y:\n ...\nelse:\n ...")
self._check_error(
"else = 123",
"invalid syntax")
self._check_error(
"elif 55 = 123",
"cannot assign to literal here")

@support.cpython_only
def test_error_on_parser_stack_overflow(self):
source = "-" * 100000 + "4"
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
Reverted improvements to error messages for ``elif``/``else`` statements not
matching any valid statements, which made in hard to locate the syntax
errors inside those ``elif``/``else`` blocks.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp