[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 bffed80230)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2024-08-20 13:13:43 +02:00 committed by GitHub
parent 595fb38e91
commit 159db050f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import array import array
import collections import collections
import dataclasses import dataclasses
import dis
import enum import enum
import inspect import inspect
import sys import sys
@ -3377,6 +3378,24 @@ class TestValueErrors(unittest.TestCase):
self.assertIs(y, None) self.assertIs(y, None)
self.assertIs(z, 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): class TestTracing(unittest.TestCase):

View File

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

View File

@ -7478,7 +7478,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
ADDOP(c, LOC(m->pattern), POP_TOP); ADDOP(c, LOC(m->pattern), POP_TOP);
} }
VISIT_SEQ(c, stmt, m->body); 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 // 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 // cleanup to be associated with the failed pattern, not the last line
// of the body // of the body