8000 [3.13] gh-123048: Fix missing source location in pattern matching cod… · python/cpython@159db05 · GitHub
[go: up one dir, main page]

Skip to content

Commit 159db05

Browse files
[3.13] gh-123048: Fix missing source location in pattern matching code (GH-123167) (#123169)
gh-123048: Fix missing source location in pattern matching code (GH-123167) (cherry picked from commit bffed80) Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
1 parent 595fb38 commit 159db05

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_patma.py

Lines changed: 19 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7478,7 +7478,7 @@ compiler_match_inne 6F09 r(struct compiler *c, stmt_ty s, pattern_context *pc)
74787478
ADDOP(c, LOC(m->pattern), POP_TOP);
74797479
}
74807480
VISIT_SEQ(c, stmt, m->body);
7481-
ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
7481+
ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
74827482
// If the pattern fails to match, we want the line number of the
74837483
// cleanup to be associated with the failed pattern, not the last line
74847484
// of the body

0 commit comments

Comments
 (0)
0