8000 [3.11] gh-103971: Fix wrong line number generated for match-case blocks by gaogaotiantian · Pull Request #103980 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.11] gh-103971: Fix wrong line number generated for match-case blocks #103980

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions Lib/test/test_patma.py
Original file line number Diff line number Diff line change
Expand Up @@ -3151,6 +3151,19 @@ def f(command): # 0
self.assertListEqual(self._trace(f, "go x"), [1, 2, 3])
self.assertListEqual(self._trace(f, "spam"), [1, 2, 3])

def test_unreachable_code(self):
def f(command): # 0
match command: # 1
case 1: # 2
if False: # 3
return 1 # 4
case _: # 5
if False: # 6
return 0 # 7

self.assertListEqual(self._trace(f, 1), [1, 2, 3])
self.assertListEqual(self._trace(f, 0), [1, 2, 5, 6])

def test_parser_deeply_nested_patterns(self):
# Deeply nested patterns can cause exponential backtracking when parsing.
# See gh-93671 for more information.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix an issue where incorrect locations numbers could be assigned to code following ``case`` blocks.
2 changes: 2 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7057,6 +7057,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
ADDOP(c, POP_TOP);
}
VISIT_SEQ(c, stmt, m->body);
UNSET_LOC(c);
ADDOP_JUMP(c, JUMP, end);
// If the pattern fails to match, we want the line number of the
// cleanup to be associated with the failed pattern, not the last line
Expand All @@ -7081,6 +7082,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
}
VISIT_SEQ(c, stmt, m->body);
UNSET_LOC(c);
}
compiler_use_next_block(c, end);
return 1;
Expand Down
0