Commit 0f7f57f5 authored by Stefan Behnel's avatar Stefan Behnel

delete more redundant code when encountering constant bool conditions inside...

delete more redundant code when encountering constant bool conditions inside of generator expressions

--HG--
extra : transplant_source : ac%24%C9%A2%BBI%5B%E9/%3A%0F%15%FCE%DAQ%C1%E5%81
parent 1dc512af
......@@ -3117,11 +3117,17 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
break
else:
assert condition_result == False
# prevent killing generators, but simplify them as much as possible
yield_expr = self._find_genexpr_yield(if_clause.body)
if yield_expr is not None:
if_clause.condition = ExprNodes.BoolNode(if_clause.condition.pos, value=False)
# prevent killing generator expressions,
# but simplify them as much as possible
yield_expr_stat = self._find_genexpr_yield_stat(if_clause.body)
if yield_expr_stat is not None:
# => if False: yield None
yield_expr = yield_expr_stat.expr
if_clause.condition = ExprNodes.BoolNode(
if_clause.condition.pos,
value=False, constant_result=False)
yield_expr.arg = ExprNodes.NoneNode(yield_expr.arg.pos)
if_clause.body = yield_expr_stat
if_clauses.append(if_clause)
else:
# False clauses outside of generators can safely be deleted
......@@ -3131,13 +3137,13 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
node.if_clauses = if_clauses
return node
def _find_genexpr_yield(self, node):
def _find_genexpr_yield_stat(self, node):
body_node_types = (Nodes.ForInStatNode, Nodes.IfStatNode)
while isinstance(node, body_node_types):
node = node.body
if isinstance(node, Nodes.ExprStatNode):
node = node.expr
if isinstance(node, ExprNodes.YieldExprNode):
expr = node.expr
if isinstance(expr, ExprNodes.YieldExprNode):
return node
return None
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment