[Python-checkins] r60265 - in python/trunk: Lib/test/test_grammar.py Python/compile.c

amaury.forgeotdarc python-checkins at python.org
Thu Jan 24 23:51:22 CET 2008


Author: amaury.forgeotdarc
Date: Thu Jan 24 23:51:18 2008
New Revision: 60265

Modified:
   python/trunk/Lib/test/test_grammar.py
   python/trunk/Python/compile.c
Log:
#1920: when considering a block starting by "while 0", the compiler optimized the
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.


Modified: python/trunk/Lib/test/test_grammar.py
==============================================================================
--- python/trunk/Lib/test/test_grammar.py	(original)
+++ python/trunk/Lib/test/test_grammar.py	Thu Jan 24 23:51:18 2008
@@ -572,6 +572,15 @@
         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

Modified: python/trunk/Python/compile.c
==============================================================================
--- python/trunk/Python/compile.c	(original)
+++ python/trunk/Python/compile.c	Thu Jan 24 23:51:18 2008
@@ -1598,8 +1598,11 @@
 	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) {


More information about the Python-checkins mailing list