const_fold.py 文件源码

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

项目:fatoptimizer 作者: vstinner 项目源码 文件源码
def compare_cst(self, node):
        node_op = node.ops[0].__class__
        eval_op = EVAL_COMPARE.get(node_op)
        if eval_op is None:
            return

        if node_op in (ast.In, ast.NotIn):
            left_hashable = True
            right_types = ITERABLE_TYPES
        else:
            left_hashable = False
            right_types = None

        if left_hashable:
            left = get_constant(node.left)
        else:
            left = get_literal(node.left)
        if left is UNSET:
            return
        right = get_literal(node.comparators[0], types=right_types)
        if right is UNSET:
            return

        if (node_op in (ast.Eq, ast.NotEq)
           and ((isinstance(left, str) and isinstance(right, bytes))
                or (isinstance(left, bytes) and isinstance(right, str)))):
            # comparison between bytes and str can raise BytesWarning depending
            # on runtime option
            return

        try:
            result = eval_op(left, right)
        except TypeError:
            return
        return self.new_constant(node, result)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号