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): ...@@ -3117,11 +3117,17 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
break break
else: else:
assert condition_result == False assert condition_result == False
# prevent killing generators, but simplify them as much as possible # prevent killing generator expressions,
yield_expr = self._find_genexpr_yield(if_clause.body) # but simplify them as much as possible
if yield_expr is not None: yield_expr_stat = self._find_genexpr_yield_stat(if_clause.body)
if_clause.condition = ExprNodes.BoolNode(if_clause.condition.pos, value=False) 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) yield_expr.arg = ExprNodes.NoneNode(yield_expr.arg.pos)
if_clause.body = yield_expr_stat
if_clauses.append(if_clause) if_clauses.append(if_clause)
else: else:
# False clauses outside of generators can safely be deleted # False clauses outside of generators can safely be deleted
...@@ -3131,13 +3137,13 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations): ...@@ -3131,13 +3137,13 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
node.if_clauses = if_clauses node.if_clauses = if_clauses
return node return node
def _find_genexpr_yield(self, node): def _find_genexpr_yield_stat(self, node):
body_node_types = (Nodes.ForInStatNode, Nodes.IfStatNode) body_node_types = (Nodes.ForInStatNode, Nodes.IfStatNode)
while isinstance(node, body_node_types): while isinstance(node, body_node_types):
node = node.body node = node.body
if isinstance(node, Nodes.ExprStatNode): if isinstance(node, Nodes.ExprStatNode):
node = node.expr expr = node.expr
if isinstance(node, ExprNodes.YieldExprNode): if isinstance(expr, ExprNodes.YieldExprNode):
return node return node
return None 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