From 73205704160483aded55c031774dc94605e28fa3 Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 28 Apr 2023 11:52:01 -0700 Subject: [PATCH 1/4] Fix match-case location issue --- Lib/test/test_patma.py | 13 +++++++++++++ Python/compile.c | 2 ++ 2 files changed, 15 insertions(+) diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py index db198f77157831..4153f51714f678 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/Python/compile.c b/Python/compile.c index 2170e82d4db69e..f87a423acd1f02 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; From a419532447f87c22a8fd19ba4e6601a5df68d1e2 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 28 Apr 2023 18:57:15 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-04-28-18-57-13.gh-issue-gh-103971.Q3U9lv.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-gh-103971.Q3U9lv.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-gh-103971.Q3U9lv.rst b/Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-gh-103971.Q3U9lv.rst new file mode 100644 index 00000000000000..9f33ada9ad5837 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-gh-103971.Q3U9lv.rst @@ -0,0 +1 @@ +Fix the wrong line number assigned for match-case blocks From 8de15e59252508e157bb7bed12b5d50dfac54ddf Mon Sep 17 00:00:00 2001 From: Tian Gao Date: Fri, 28 Apr 2023 11:59:48 -0700 Subject: [PATCH 3/4] Rename 2023-04-28-18-57-13.gh-issue-gh-103971.Q3U9lv.rst to 2023-04-28-18-57-13.gh-issue-103971.Q3U9lv.rst --- ....Q3U9lv.rst => 2023-04-28-18-57-13.gh-issue-103971.Q3U9lv.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/NEWS.d/next/Core and Builtins/{2023-04-28-18-57-13.gh-issue-gh-103971.Q3U9lv.rst => 2023-04-28-18-57-13.gh-issue-103971.Q3U9lv.rst} (100%) diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-gh-103971.Q3U9lv.rst b/Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-103971.Q3U9lv.rst similarity index 100% rename from Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-gh-103971.Q3U9lv.rst rename to Misc/NEWS.d/next/Core and Builtins/2023-04-28-18-57-13.gh-issue-103971.Q3U9lv.rst From c549aca7aa15579e75e7cf66fbc31701bf88200e Mon Sep 17 00:00:00 2001 From: Brandt Bucher Date: Fri, 28 Apr 2023 12:24:32 -0700 Subject: [PATCH 4/4] Update NEWS --- .../2023-04-28-18-57-13.gh-issue-103971.Q3U9lv.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 9f33ada9ad5837..2d889e9906346b 100644 --- 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 @@ -1 +1 @@ -Fix the wrong line number assigned for match-case blocks +Fix an issue where incorrect locations numbers could be assigned to code following ``case`` blocks.