def visit_BinOp(self, node):
if not self.config.constant_folding:
return
eval_binop = EVAL_BINOP.get(node.op.__class__)
if not eval_binop:
return
if isinstance(node.op, ast.Mod):
# FIXME: optimize str%args and bytes%args
left_types = COMPLEX_TYPES
else:
left_types = None
left = get_constant(node.left, types=left_types)
if left is UNSET:
return
right = get_constant(node.right)
if right is UNSET:
return
ok = self.check_binop(node.op, left, right)
if not ok:
return
result = eval_binop(left, right)
new_node = self.new_constant(node, result)
if new_node is None:
return
op_str = BINOP_STR[node.op.__class__]
self.log(node, "constant folding: replace %s %s %s with %s",
compact_ascii(left), op_str, compact_ascii(right),
compact_ascii(result), add_line=True)
return new_node
评论列表
文章目录