Skip to content

Commit fee3c91

Browse files
[3.11] GH-103971: Fix incorrect locations for code following case blocks
1 parent 4219074 commit fee3c91

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

Lib/test/test_patma.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,6 +3151,19 @@ def f(command): # 0
31513151
self.assertListEqual(self._trace(f, "go x"), [1, 2, 3])
31523152
self.assertListEqual(self._trace(f, "spam"), [1, 2, 3])
31533153

3154+
def test_unreachable_code(self):
3155+
def f(command): # 0
3156+
match command: # 1
3157+
case 1: # 2
3158+
if False: # 3
3159+
return 1 # 4
3160+
case _: # 5
3161+
if False: # 6
3162+
return 0 # 7
3163+
3164+
self.assertListEqual(self._trace(f, 1), [1, 2, 3])
3165+
self.assertListEqual(self._trace(f, 0), [1, 2, 5, 6])
3166+
31543167
def test_parser_deeply_nested_patterns(self):
31553168
# Deeply nested patterns can cause exponential backtracking when parsing.
31563169
# See gh-93671 for more information.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix an issue where incorrect locations numbers could be assigned to code following ``case`` blocks.

Python/compile.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7057,6 +7057,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
70577057
ADDOP(c, POP_TOP);
70587058
}
70597059
VISIT_SEQ(c, stmt, m->body);
7060+
UNSET_LOC(c);
70607061
ADDOP_JUMP(c, JUMP, end);
70617062
// If the pattern fails to match, we want the line number of the
70627063
// cleanup to be associated with the failed pattern, not the last line
@@ -7081,6 +7082,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
70817082
RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
70827083
}
70837084
VISIT_SEQ(c, stmt, m->body);
7085+
UNSET_LOC(c);
70847086
}
70857087
compiler_use_next_block(c, end);
70867088
return 1;

0 commit comments

Comments
 (0)