]> granicus.if.org Git - python/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 23:42:08 +0000 (23:42 +0000)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Thu, 24 Jan 2008 23:42:08 +0000 (23:42 +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.

Backport of r60265.

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

index 76483f676a0389d686c0e0cb2f4364daa9d58913..89e9e67895028321c99af4114757d6d23294c38d 100644 (file)
@@ -511,6 +511,15 @@ while 0: pass
 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
+assert x == 2
+
 print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
 for i in 1, 2, 3: pass
 for i, j, k in (): pass
index 8b0084aad96716ef8d3c26781b7ab7ccf289a533..4913c79bcb7e2199d1c99c678717b1643e3ddbd6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,11 @@ What's New in Python 2.5.2c1?
 Core and builtins
 -----------------
 
+- Issue #1920: "while 0" statements were completely removed by the compiler,
+  even in the presence of an "else" clause, which is supposed to be run when 
+  the condition is false. Now the compiler correctly emits bytecode for the
+  "else" suite.
+
 - A few crashers fixed: weakref_in_del.py (issue #1377858);
   loosing_dict_ref.py (issue #1303614, test67.py);
   borrowed_ref_[34].py (not in tracker).
index 0e824caabdbc98c4d9f4ded5c18e9cbf0ed66f18..8e96ddfa62c9a1b9c9e5933f3cb47c803d526c67 100644 (file)
@@ -2256,8 +2256,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) {