const_fold.py 文件源码

python
阅读 37 收藏 0 点赞 0 评论 0

项目:fatoptimizer 作者: vstinner 项目源码 文件源码
def visit_Compare(self, node):
        if not self.config.constant_folding:
            return

        if len(node.ops) != 1:
            # FIXME: implement 1 < 2 < 3
            return
        if len(node.comparators) != 1:
            # FIXME: support this case? What's the syntax of this case?
            return

        new_node = self.compare_cst(node)
        if new_node is not None:
            return new_node

        # replace 'None is None' with True
        if (isinstance(node.ops[0], (ast.Is, ast.IsNot))
           and isinstance(node.left, ast.Constant)
           and node.left.value is None
           and isinstance(node.comparators[0], ast.Constant)
           and node.comparators[0].value is None):
            result = isinstance(node.ops[0], ast.Is)
            return self.new_constant(node, result)

        # replace 'x in {1, 2}' with 'x in frozenset({1, 2})'
        # replace 'x in [1, 2]' with 'x in frozenset((1, 2))'
        if isinstance(node.ops[0], ast.In):
            new_node = self.compare_contains(node)
            if new_node is not None:
                return new_node
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号