mirror of
https://github.com/python/cpython.git
synced 2025-07-30 14:44:10 +00:00
#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.
This commit is contained in:
parent
5310b69419
commit
16570f59ca
2 changed files with 13 additions and 1 deletions
|
@ -572,6 +572,15 @@ hello world
|
||||||
while 0: pass
|
while 0: pass
|
||||||
else: 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):
|
def testFor(self):
|
||||||
# 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
|
# 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
|
||||||
for i in 1, 2, 3: pass
|
for i in 1, 2, 3: pass
|
||||||
|
|
|
@ -1598,8 +1598,11 @@ compiler_while(struct compiler *c, stmt_ty s)
|
||||||
basicblock *loop, *orelse, *end, *anchor = NULL;
|
basicblock *loop, *orelse, *end, *anchor = NULL;
|
||||||
int constant = expr_constant(s->v.While.test);
|
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;
|
return 1;
|
||||||
|
}
|
||||||
loop = compiler_new_block(c);
|
loop = compiler_new_block(c);
|
||||||
end = compiler_new_block(c);
|
end = compiler_new_block(c);
|
||||||
if (constant == -1) {
|
if (constant == -1) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue