]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-124871: fix 'visited' tracking in compiler's reachability analysis (#124952)
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Fri, 4 Oct 2024 16:37:38 +0000 (17:37 +0100)
committerGitHub <noreply@github.com>
Fri, 4 Oct 2024 16:37:38 +0000 (17:37 +0100)
Lib/test/test_compile.py
Misc/NEWS.d/next/Core and Builtins/2024-10-03-22-26-39.gh-issue-124871.tAMF47.rst [new file with mode: 0644]
Python/flowgraph.c

index e9ee72cf234fdc0e0427835c22df88b89e813371..e6dc7a53189e1280309874aa21a28bca45959dc2 100644 (file)
@@ -479,6 +479,19 @@ class TestSpecifics(unittest.TestCase):
                     x = 2
                """), '<eval>', 'exec')
 
+    def test_try_except_in_while_with_chained_condition_compiles(self):
+        # see gh-124871
+        compile(textwrap.dedent("""
+            name_1, name_2, name_3 = 1, 2, 3
+            while name_3 <= name_2 > name_1:
+                try:
+                    raise
+                except:
+                    pass
+                finally:
+                    pass
+            """), '<eval>', 'exec')
+
     def test_compile_invalid_namedexpr(self):
         # gh-109351
         m = ast.Module(
diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-10-03-22-26-39.gh-issue-124871.tAMF47.rst b/Misc/NEWS.d/next/Core and Builtins/2024-10-03-22-26-39.gh-issue-124871.tAMF47.rst
new file mode 100644 (file)
index 0000000..185cb30
--- /dev/null
@@ -0,0 +1,2 @@
+Fix compiler bug (in some versions of 3.13) where an assertion fails during reachability
+analysis.
index 69d7e0a872aa48959065bfa7494ec58adaafe657..388862912d68266d1e773cb6e5c72bb5d10ddc28 100644 (file)
@@ -1001,13 +1001,14 @@ remove_unreachable(basicblock *entryblock) {
     basicblock **sp = stack;
     entryblock->b_predecessors = 1;
     *sp++ = entryblock;
+    entryblock->b_visited = 1;
     while (sp > stack) {
         basicblock *b = *(--sp);
-        b->b_visited = 1;
         if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
             if (!b->b_next->b_visited) {
                 assert(b->b_next->b_predecessors == 0);
                 *sp++ = b->b_next;
+                b->b_next->b_visited = 1;
             }
             b->b_next->b_predecessors++;
         }
@@ -1017,8 +1018,8 @@ remove_unreachable(basicblock *entryblock) {
             if (is_jump(instr) || is_block_push(instr)) {
                 target = instr->i_target;
                 if (!target->b_visited) {
-                    assert(target->b_predecessors == 0 || target == b->b_next);
                     *sp++ = target;
+                    target->b_visited = 1;
                 }
                 target->b_predecessors++;
             }