https://github.com/python/cpython/commit/bffed80230f2617de2ee02bd4bdded1024234dab
commit: bffed80230f2617de2ee02bd4bdded1024234dab
branch: main
author: Irit Katriel <[email protected]>
committer: iritkatriel <[email protected]>
date: 2024-08-20T11:39:41+01:00
summary:

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

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-08-20-11-09-16.gh-issue-123048.2TISpv.rst
M Lib/test/test_patma.py
M Python/compile.c

diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py
index 1bdab125dc6ef0..8325b83a5932b9 100644
--- a/Lib/test/test_patma.py
+++ b/Lib/test/test_patma.py
@@ -1,6 +1,7 @@
 import array
 import collections
 import dataclasses
+import dis
 import enum
 import inspect
 import sys
@@ -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):
 
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-08-20-11-09-16.gh-issue-123048.2TISpv.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-08-20-11-09-16.gh-issue-123048.2TISpv.rst
new file mode 100644
index 00000000000000..f0b756febbc1b8
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-08-20-11-09-16.gh-issue-123048.2TISpv.rst 
@@ -0,0 +1,2 @@
+Fix a bug where pattern matching code could emit a :opcode:`JUMP_FORWARD`
+with no source location.
diff --git a/Python/compile.c b/Python/compile.c
index 0a338b169872d6..c369202d53b384 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -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

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to