]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#1920: when considering a block starting by "while 0", the compiler optimized the
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Thu, 24 Jan 2008 22:51:18 +0000 (22:51 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Thu, 24 Jan 2008 22:51:18 +0000 (22:51 +0000)
whole construct away, even when an 'else' clause is present::

    while 0:
        print("no")
    else:
        print("yes")

did not generate any code at all.

Now the compiler emits the 'else' block, like it already does for 'if' statements.

Will backport.

Lib/test/test_grammar.py
Python/compile.c

index 435227521e7829469352d356ba909f5e916e84e9..d4bcfda0172c1dd48ad7bf97ec19ce8955c4d242 100644 (file)
@@ -572,6 +572,15 @@ hello world
         while 0: pass
         else: pass
 
+        # Issue1920: "while 0" is optimized away,
+        # ensure that the "else" clause is still present.
+        x = 0
+        while 0:
+            x = 1
+        else:
+            x = 2
+        self.assertEquals(x, 2)
+
     def testFor(self):
         # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
         for i in 1, 2, 3: pass
index c77091ea25a0b695fe12b528f70be79f7444bb36..aee7bda9ae532dbd672db6b6b87b6f3c3c8d425f 100644 (file)
@@ -1598,8 +1598,11 @@ compiler_while(struct compiler *c, stmt_ty s)
        basicblock *loop, *orelse, *end, *anchor = NULL;
        int constant = expr_constant(s->v.While.test);
 
-       if (constant == 0)
+       if (constant == 0) {
+               if (s->v.While.orelse)
+                       VISIT_SEQ(c, stmt, s->v.While.orelse);
                return 1;
+       }
        loop = compiler_new_block(c);
        end = compiler_new_block(c);
        if (constant == -1) {