From 9c074eb44cdd0f0b462167a459ad324413a755e1 Mon Sep 17 00:00:00 2001 From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> Date: Tue, 20 Aug 2024 11:39:41 +0100 Subject: [PATCH 1/2] gh-123048: Fix missing source location in pattern matching code (GH-123167) (cherry picked from commit bffed80230f2617de2ee02bd4bdded1024234dab) Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com> --- Lib/test/test_patma.py | 19 +++++++++++++++++++ ...-08-20-11-09-16.gh-issue-123048.2TISpv.rst | 2 ++ 2 files changed, 21 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-08-20-11-09-16.gh-issue-123048.2TISpv.rst diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py index 3dbd19dfffd318..853f6dfde5cf91 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 @@ -3083,6 +3084,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. From b5318a2c0c04b14e1c443158ea3c602e87dc4d3e Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 6 Sep 2024 11:25:17 +0100 Subject: [PATCH 2/2] fix test_patma --- Lib/test/test_patma.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py index 853f6dfde5cf91..6fe5360b5f296a 100644 --- a/Lib/test/test_patma.py +++ b/Lib/test/test_patma.py @@ -3100,7 +3100,7 @@ def f(): x += 1 for inst in dis.get_instructions(f): - if inst.opcode in dis.hasjump: + if inst.opcode in dis.hasjrel or inst.opcode in dis.hasjabs: self.assertIsNotNone(inst.positions.lineno, "jump without location") class TestTracing(unittest.TestCase):