Skip to content

gh-123048: Fix missing source location in pattern matching code #123167

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 1 commit into from
Aug 20, 2024
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
19 changes: 19 additions & 0 deletions Lib/test/test_patma.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import array
import collections
import dataclasses
import dis
import enum
import inspect
import sys
Expand Down Expand Up @@ -3377,6 +3378,24 @@ class Keys:
self.assertIs(y, None)
self.assertIs(z, None)

class TestSourceLocations(unittest.TestCase):
def test_jump_threading(self):
# See gh-123048
def f():
x = 0
v = 1
match v:
case 1:
if x < 0:
x = 1
case 2:
if x < 0:
x = 1
x += 1

for inst in dis.get_instructions(f):
if inst.opcode in dis.hasjump:
self.assertIsNotNone(inst.positions.lineno, "jump without location")

class TestTracing(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug where pattern matching code could emit a :opcode:`JUMP_FORWARD`
with no source location.
2 changes: 1 addition & 1 deletion Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7301,7 +7301,7 @@ codegen_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
ADDOP(c, LOC(m->pattern), POP_TOP);
}
VISIT_SEQ(c, stmt, m->body);
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
ADDOP_JUMP(c, NO_LOCATION, 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
// of the body
Expand Down
Loading