[Python-checkins] r60268 - in python/branches/release25-maint: Lib/test/test_grammar.py Misc/NEWS Python/compile.c

amaury.forgeotdarc python-checkins at python.org
Fri Jan 25 00:42:09 CET 2008


Author: amaury.forgeotdarc
Date: Fri Jan 25 00:42:08 2008
New Revision: 60268

Modified:
   python/branches/release25-maint/Lib/test/test_grammar.py
   python/branches/release25-maint/Misc/NEWS
   python/branches/release25-maint/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.

Backport of r60265.


Modified: python/branches/release25-maint/Lib/test/test_grammar.py
==============================================================================
--- python/branches/release25-maint/Lib/test/test_grammar.py	(original)
+++ python/branches/release25-maint/Lib/test/test_grammar.py	Fri Jan 25 00:42:08 2008
@@ -511,6 +511,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
+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

Modified: python/branches/release25-maint/Misc/NEWS
==============================================================================
--- python/branches/release25-maint/Misc/NEWS	(original)
+++ python/branches/release25-maint/Misc/NEWS	Fri Jan 25 00:42:08 2008
@@ -12,6 +12,11 @@
 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).

Modified: python/branches/release25-maint/Python/compile.c
==============================================================================
--- python/branches/release25-maint/Python/compile.c	(original)
+++ python/branches/release25-maint/Python/compile.c	Fri Jan 25 00:42:08 2008
@@ -2256,8 +2256,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