Exemplo n.º 1
0
def _factor_calculate(evaluator, operator, right):
    if _is_number(right):
        if operator == '-':
            return create(evaluator, -right.obj)
    if operator == 'not':
        value = right.py__bool__()
        if value is None:  # Uncertainty.
            return None
        return keyword_from_value(not value)
    return right
Exemplo n.º 2
0
def _element_calculate(evaluator, left, operator, right):
    from jedi.evaluate import iterable, representation as er
    l_is_num = _is_number(left)
    r_is_num = _is_number(right)
    if operator == '*':
        # for iterables, ignore * operations
        if isinstance(left, iterable.Array) or is_string(left):
            return [left]
        elif isinstance(right, iterable.Array) or is_string(right):
            return [right]
    elif operator == '+':
        if l_is_num and r_is_num or is_string(left) and is_string(right):
            return [create(evaluator, left.obj + right.obj)]
        elif _is_tuple(left) and _is_tuple(right) or _is_list(
                left) and _is_list(right):
            return [iterable.MergedArray(evaluator, (left, right))]
    elif operator == '-':
        if l_is_num and r_is_num:
            return [create(evaluator, left.obj - right.obj)]
    elif operator == '%':
        # With strings and numbers the left type typically remains. Except for
        # `int() % float()`.
        return [left]
    elif operator in COMPARISON_OPERATORS:
        operation = COMPARISON_OPERATORS[operator]
        if isinstance(left, CompiledObject) and isinstance(
                right, CompiledObject):
            # Possible, because the return is not an option. Just compare.
            left = left.obj
            right = right.obj

        try:
            return [keyword_from_value(operation(left, right))]
        except TypeError:
            # Could be True or False.
            return [true_obj, false_obj]
    elif operator == 'in':
        return []

    def check(obj):
        """Checks if a Jedi object is either a float or an int."""
        return isinstance(
            obj, er.Instance) and obj.name.get_code() in ('int', 'float')

    # Static analysis, one is a number, the other one is not.
    if operator in ('+', '-') and l_is_num != r_is_num \
            and not (check(left) or check(right)):
        message = "TypeError: unsupported operand type(s) for +: %s and %s"
        analysis.add(evaluator, 'type-error-operation', operator,
                     message % (left, right))

    return [left, right]
Exemplo n.º 3
0
def _element_calculate(evaluator, left, operator, right):
    from jedi.evaluate import iterable, representation as er
    l_is_num = _is_number(left)
    r_is_num = _is_number(right)
    if operator == '*':
        # for iterables, ignore * operations
        if isinstance(left, iterable.Array) or is_string(left):
            return [left]
        elif isinstance(right, iterable.Array) or is_string(right):
            return [right]
    elif operator == '+':
        if l_is_num and r_is_num or is_string(left) and is_string(right):
            return [create(evaluator, left.obj + right.obj)]
        elif _is_tuple(left) and _is_tuple(right) or _is_list(left) and _is_list(right):
            return [iterable.MergedArray(evaluator, (left, right))]
    elif operator == '-':
        if l_is_num and r_is_num:
            return [create(evaluator, left.obj - right.obj)]
    elif operator == '%':
        # With strings and numbers the left type typically remains. Except for
        # `int() % float()`.
        return [left]
    elif operator in COMPARISON_OPERATORS:
        operation = COMPARISON_OPERATORS[operator]
        if isinstance(left, CompiledObject) and isinstance(right, CompiledObject):
            # Possible, because the return is not an option. Just compare.
            left = left.obj
            right = right.obj

        try:
            return [keyword_from_value(operation(left, right))]
        except TypeError:
            # Could be True or False.
            return [true_obj, false_obj]
    elif operator == 'in':
        return []

    def check(obj):
        """Checks if a Jedi object is either a float or an int."""
        return isinstance(obj, er.Instance) and obj.name.get_code() in ('int', 'float')

    # Static analysis, one is a number, the other one is not.
    if operator in ('+', '-') and l_is_num != r_is_num \
            and not (check(left) or check(right)):
        message = "TypeError: unsupported operand type(s) for +: %s and %s"
        analysis.add(evaluator, 'type-error-operation', operator,
                     message % (left, right))

    return [left, right]
Exemplo n.º 4
0
def factor_calculate(evaluator, types, operator):
    """
    Calculates `+`, `-`, `~` and `not` prefixes.
    """
    for typ in types:
        if operator == '-':
            if _is_number(typ):
                yield create(evaluator, -typ.obj)
        elif operator == 'not':
            value = typ.py__bool__()
            if value is None:  # Uncertainty.
                return
            yield keyword_from_value(not value)
        else:
            yield typ
Exemplo n.º 5
0
def factor_calculate(evaluator, types, operator):
    """
    Calculates `+`, `-`, `~` and `not` prefixes.
    """
    for typ in types:
        if operator == '-':
            if _is_number(typ):
                yield create(evaluator, -typ.obj)
        elif operator == 'not':
            value = typ.py__bool__()
            if value is None:  # Uncertainty.
                return
            yield keyword_from_value(not value)
        else:
            yield typ
Exemplo n.º 6
0
def builtins_isinstance(evaluator, objects, types):
    bool_results = set([])
    for o in objects:
        try:
            mro_func = o.py__class__(evaluator).py__mro__
        except AttributeError:
            # This is temporary. Everything should have a class attribute in
            # Python?! Maybe we'll leave it here, because some numpy objects or
            # whatever might not.
            return [compiled.true_obj, compiled.false_obj]

        mro = mro_func(evaluator)

        for cls_or_tup in types:
            if cls_or_tup.is_class():
                bool_results.add(cls_or_tup in mro)
            else:
                # Check for tuples.
                classes = iterable.get_iterator_types([cls_or_tup])
                bool_results.add(any(cls in mro for cls in classes))

    return [compiled.keyword_from_value(x) for x in bool_results]
Exemplo n.º 7
0
def builtins_isinstance(evaluator, objects, types):
    bool_results = set([])
    for o in objects:
        try:
            mro_func = o.py__class__(evaluator).py__mro__
        except AttributeError:
            # This is temporary. Everything should have a class attribute in
            # Python?! Maybe we'll leave it here, because some numpy objects or
            # whatever might not.
            return [compiled.true_obj, compiled.false_obj]

        mro = mro_func(evaluator)

        for cls_or_tup in types:
            if cls_or_tup.is_class():
                bool_results.add(cls_or_tup in mro)
            else:
                # Check for tuples.
                classes = iterable.get_iterator_types([cls_or_tup])
                bool_results.add(any(cls in mro for cls in classes))

    return [compiled.keyword_from_value(x) for x in bool_results]