Skip to content

Commit bffed80

Browse files
authored
gh-123048: Fix missing source location in pattern matching code (#123167)
1 parent 77133f5 commit bffed80

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_patma.py

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import array
22
import collections
33
import dataclasses
4+
import dis
45
import enum
56
import inspect
67
import sys
@@ -3377,6 +3378,24 @@ class Keys:
33773378
self.assertIs(y, None)
33783379
self.assertIs(z, None)
33793380

3381+
class TestSourceLocations(unittest.TestCase):
3382+
def test_jump_threading(self):
3383+
# See gh-123048
3384+
def f():
3385+
x = 0
3386+
v = 1
3387+
match v:
3388+
case 1:
3389+
if x < 0:
3390+
x = 1
3391+
case 2:
3392+
if x < 0:
3393+
x = 1
3394+
x += 1
3395+
3396+
for inst in dis.get_instructions(f):
3397+
if inst.opcode in dis.hasjump:
3398+
self.assertIsNotNone(inst.positions.lineno, "jump without location")
33803399

33813400
class TestTracing(unittest.TestCase):
33823401

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a bug where pattern matching code could emit a :opcode:`JUMP_FORWARD`
2+
with no source location.

Python/compile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7301,7 +7301,7 @@ codegen_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
73017301
ADDOP(c, LOC(m->pattern), POP_TOP);
73027302
}
73037303
VISIT_SEQ(c, stmt, m->body);
7304-
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
7304+
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
73057305
// If the pattern fails to match, we want the line number of the
73067306
// cleanup to be associated with the failed pattern, not the last line
73077307
// of the body

0 commit comments

Comments
 (0)