[Python-checkins] [3.11] GH-103971: Fix incorrect locations for code following case blocks

brandtbucher webhook-mailer at python.org
Fri Apr 28 16:08:31 EDT 2023


https://github.com/python/cpython/commit/fee3c91a196e8c716ff86cb5e8bd3fda32f4e6a8
commit: fee3c91a196e8c716ff86cb5e8bd3fda32f4e6a8
branch: 3.11
author: Tian Gao <gaogaotiantian at hotmail.com>
committer: brandtbucher <brandtbucher at gmail.com>
date: 2023-04-28T20:08:25Z
summary:

[3.11] GH-103971: Fix incorrect locations for code following case blocks

files:
A Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-103971.Q3U9lv.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 db198f771578..4153f51714f6 100644
--- a/Lib/test/test_patma.py
+++ b/Lib/test/test_patma.py
@@ -3151,6 +3151,19 @@ def f(command):             # 0
         self.assertListEqual(self._trace(f, "go x"), [1, 2, 3])
         self.assertListEqual(self._trace(f, "spam"), [1, 2, 3])
 
+    def test_unreachable_code(self):
+        def f(command):               # 0
+            match command:            # 1
+                case 1:               # 2
+                    if False:         # 3
+                        return 1      # 4
+                case _:               # 5
+                    if False:         # 6
+                        return 0      # 7
+
+        self.assertListEqual(self._trace(f, 1), [1, 2, 3])
+        self.assertListEqual(self._trace(f, 0), [1, 2, 5, 6])
+
     def test_parser_deeply_nested_patterns(self):
         # Deeply nested patterns can cause exponential backtracking when parsing.
         # See gh-93671 for more information.
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-103971.Q3U9lv.rst b/Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-103971.Q3U9lv.rst
new file mode 100644
index 000000000000..2d889e990634
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-103971.Q3U9lv.rst	
@@ -0,0 +1 @@
+Fix an issue where incorrect locations numbers could be assigned to code following ``case`` blocks.
diff --git a/Python/compile.c b/Python/compile.c
index 2170e82d4db6..f87a423acd1f 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -7057,6 +7057,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
             ADDOP(c, POP_TOP);
         }
         VISIT_SEQ(c, stmt, m->body);
+        UNSET_LOC(c);
         ADDOP_JUMP(c, 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
@@ -7081,6 +7082,7 @@ compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
             RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
         }
         VISIT_SEQ(c, stmt, m->body);
+        UNSET_LOC(c);
     }
     compiler_use_next_block(c, end);
     return 1;



More information about the Python-checkins mailing list