Пример #1
0
 def _eval_element_not_cached(self, element):
     debug.dbg('eval_element %s@%s', element, element.start_pos)
     types = set()
     if isinstance(element, (tree.Name, tree.Literal)) or tree.is_node(
             element, 'atom'):
         types = self._eval_atom(element)
     elif isinstance(element, tree.Keyword):
         # For False/True/None
         if element.value in ('False', 'True', 'None'):
             types.add(compiled.builtin_from_name(self, element.value))
         # else: print e.g. could be evaluated like this in Python 2.7
     elif element.isinstance(tree.Lambda):
         types = set([er.LambdaWrapper(self, element)])
     elif element.isinstance(er.LambdaWrapper):
         types = set([element
                      ])  # TODO this is no real evaluation. id:121 gh:122
     elif element.type == 'expr_stmt':
         types = self.eval_statement(element)
     elif element.type in ('power', 'atom_expr'):
         types = self._eval_atom(element.children[0])
         for trailer in element.children[1:]:
             if trailer == '**':  # has a power operation.
                 right = self.eval_element(element.children[2])
                 types = set(
                     precedence.calculate(self, types, trailer, right))
                 break
             types = self.eval_trailer(types, trailer)
     elif element.type in (
             'testlist_star_expr',
             'testlist',
     ):
         # The implicit tuple in statements.
         types = set([iterable.ImplicitTuple(self, element)])
     elif element.type in ('not_test', 'factor'):
         types = self.eval_element(element.children[-1])
         for operator in element.children[:-1]:
             types = set(precedence.factor_calculate(self, types, operator))
     elif element.type == 'test':
         # `x if foo else y` case.
         types = (self.eval_element(element.children[0])
                  | self.eval_element(element.children[-1]))
     elif element.type == 'operator':
         # Must be an ellipsis, other operators are not evaluated.
         assert element.value == '...'
         types = set([compiled.create(self, Ellipsis)])
     elif element.type == 'dotted_name':
         types = self._eval_atom(element.children[0])
         for next_name in element.children[2::2]:
             types = set(
                 chain.from_iterable(
                     self.find_types(typ, next_name) for typ in types))
         types = types
     elif element.type == 'eval_input':
         types = self._eval_element_not_cached(element.children[0])
     elif element.type == 'annassign':
         types = self.eval_element(element.children[1])
     else:
         types = precedence.calculate_children(self, element.children)
     debug.dbg('eval_element result %s', types)
     return types
Пример #2
0
    def eval_element(self, element):
        if isinstance(element, iterable.AlreadyEvaluated):
            return list(element)
        elif isinstance(element, iterable.MergedNodes):
            return iterable.unite(self.eval_element(e) for e in element)

        debug.dbg('eval_element %s@%s', element, element.start_pos)
        if isinstance(element,
                      (pr.Name, pr.Literal)) or pr.is_node(element, 'atom'):
            return self._eval_atom(element)
        elif isinstance(element, pr.Keyword):
            # For False/True/None
            if element.value in ('False', 'True', 'None'):
                return [compiled.builtin.get_by_name(element.value)]
            else:
                return []
        elif element.isinstance(pr.Lambda):
            return [er.LambdaWrapper(self, element)]
        elif element.isinstance(er.LambdaWrapper):
            return [element]  # TODO this is no real evaluation.
        elif element.type == 'expr_stmt':
            return self.eval_statement(element)
        elif element.type == 'power':
            types = self._eval_atom(element.children[0])
            for trailer in element.children[1:]:
                if trailer == '**':  # has a power operation.
                    raise NotImplementedError
                types = self.eval_trailer(types, trailer)

            return types
        elif element.type in (
                'testlist_star_expr',
                'testlist',
        ):
            # The implicit tuple in statements.
            return [iterable.ImplicitTuple(self, element)]
        elif element.type in ('not_test', 'factor'):
            types = self.eval_element(element.children[-1])
            for operator in element.children[:-1]:
                types = list(precedence.factor_calculate(
                    self, types, operator))
            return types
        elif element.type == 'test':
            # `x if foo else y` case.
            return (self.eval_element(element.children[0]) +
                    self.eval_element(element.children[-1]))
        elif element.type == 'operator':
            # Must be an ellipsis, other operators are not evaluated.
            return []  # Ignore for now.
        elif element.type == 'dotted_name':
            types = self._eval_atom(element.children[0])
            for next_name in element.children[2::2]:
                types = list(
                    chain.from_iterable(
                        self.find_types(typ, next_name) for typ in types))
            return types
        else:
            return precedence.calculate_children(self, element.children)