Beispiel #1
0
def contains_String(annotator, string, char):
    if annotator.annotation(char).is_constant() and annotator.annotation(char).const == "\0":
        r = SomeBool()
        knowntypedata = {}
        add_knowntypedata(knowntypedata, False, [string],
                          annotator.annotation(string).nonnulify())
        r.set_knowntypedata(knowntypedata)
        return r
    else:
        return contains_SomeObject(annotator, string, char)
Beispiel #2
0
def bool_SomeObject(annotator, obj):
    r = SomeBool()
    annotator.annotation(obj).bool_behavior(r)
    s_nonnone_obj = annotator.annotation(obj)
    if s_nonnone_obj.can_be_none():
        s_nonnone_obj = s_nonnone_obj.nonnoneify()
    knowntypedata = {}
    add_knowntypedata(knowntypedata, True, [obj], s_nonnone_obj)
    r.set_knowntypedata(knowntypedata)
    return r
Beispiel #3
0
def contains_String(annotator, string, char):
    if annotator.annotation(char).is_constant() and annotator.annotation(char).const == "\0":
        r = SomeBool()
        knowntypedata = defaultdict(dict)
        add_knowntypedata(knowntypedata, False, [string],
                          annotator.annotation(string).nonnulify())
        r.set_knowntypedata(knowntypedata)
        return r
    else:
        return contains_SomeObject(annotator, string, char)
Beispiel #4
0
def bool_SomeObject(annotator, obj):
    r = SomeBool()
    annotator.annotation(obj).bool_behavior(r)
    s_nonnone_obj = annotator.annotation(obj)
    if s_nonnone_obj.can_be_none():
        s_nonnone_obj = s_nonnone_obj.nonnoneify()
    knowntypedata = {}
    add_knowntypedata(knowntypedata, True, [obj], s_nonnone_obj)
    r.set_knowntypedata(knowntypedata)
    return r
Beispiel #5
0
 def op_contains(self, s_element):
     if s_element.is_constant() and s_element.const == "\0":
         r = SomeBool()
         bk = getbookkeeper()
         op = bk._find_current_op(opname="contains", arity=2, pos=0, s_type=self)
         knowntypedata = {}
         add_knowntypedata(knowntypedata, False, [op.args[0]], self.nonnulify())
         r.set_knowntypedata(knowntypedata)
         return r
     else:
         return SomeObject.op_contains(self, s_element)
Beispiel #6
0
    def bool(s_obj):
        r = SomeBool()
        s_obj.bool_behavior(r)

        bk = getbookkeeper()
        knowntypedata = {}
        op = bk._find_current_op(opname="bool", arity=1)
        arg = op.args[0]
        s_nonnone_obj = s_obj
        if s_obj.can_be_none():
            s_nonnone_obj = s_obj.nonnoneify()
        add_knowntypedata(knowntypedata, True, [arg], s_nonnone_obj)
        r.set_knowntypedata(knowntypedata)
        return r
Beispiel #7
0
 def _compare_helper(annotator, int1, int2):
     r = SomeBool()
     s_int1, s_int2 = annotator.annotation(int1), annotator.annotation(int2)
     if s_int1.is_immutable_constant() and s_int2.is_immutable_constant():
         r.const = cmp_op.pyfunc(s_int1.const, s_int2.const)
     #
     # The rest of the code propagates nonneg information between
     # the two arguments.
     #
     # Doing the right thing when int1 or int2 change from signed
     # to unsigned (r_uint) is almost impossible.  See test_intcmp_bug.
     # Instead, we only deduce constrains on the operands in the
     # case where they are both signed.  In other words, if y is
     # nonneg then "assert x>=y" will let the annotator know that
     # x is nonneg too, but it will not work if y is unsigned.
     #
     if not (rarithmetic.signedtype(s_int1.knowntype) and
             rarithmetic.signedtype(s_int2.knowntype)):
         return r
     knowntypedata = defaultdict(dict)
     def tointtype(s_int0):
         if s_int0.knowntype is bool:
             return int
         return s_int0.knowntype
     if s_int1.nonneg and isinstance(int2, Variable):
         case = cmp_op.opname in ('lt', 'le', 'eq')
         add_knowntypedata(knowntypedata, case, [int2],
                           SomeInteger(nonneg=True, knowntype=tointtype(s_int2)))
     if s_int2.nonneg and isinstance(int1, Variable):
         case = cmp_op.opname in ('gt', 'ge', 'eq')
         add_knowntypedata(knowntypedata, case, [int1],
                           SomeInteger(nonneg=True, knowntype=tointtype(s_int1)))
     r.set_knowntypedata(knowntypedata)
     # a special case for 'x < 0' or 'x >= 0',
     # where 0 is a flow graph Constant
     # (in this case we are sure that it cannot become a r_uint later)
     if (isinstance(int2, Constant) and
             type(int2.value) is int and  # filter out Symbolics
             int2.value == 0):
         if s_int1.nonneg:
             if cmp_op.opname == 'lt':
                 r.const = False
             if cmp_op.opname == 'ge':
                 r.const = True
     return r
Beispiel #8
0
def builtin_isinstance(s_obj, s_type, variables=None):
    r = SomeBool()
    if s_type.is_constant():
        typ = s_type.const
        if issubclass(typ, rpython.rlib.rarithmetic.base_int):
            try:
                r.const = issubclass(s_obj.knowntype, typ)
            except TypeError:    # s_obj.knowntype is not a Python type at all
                r.const = False
        else:
            if typ == long:
                getbookkeeper().warning("isinstance(., long) is not RPython")
                r.const = False
                return r

            assert not issubclass(typ, (int, long)) or typ in (bool, int, long), (
                "for integers only isinstance(.,int|r_uint) are supported")

            if s_obj.is_constant():
                r.const = isinstance(s_obj.const, typ)
            elif our_issubclass(s_obj.knowntype, typ):
                if not s_obj.can_be_none():
                    r.const = True
            elif not our_issubclass(typ, s_obj.knowntype):
                r.const = False
            elif s_obj.knowntype == int and typ == bool: # xxx this will explode in case of generalisation
                                                   # from bool to int, notice that isinstance( , bool|int)
                                                   # is quite border case for RPython
                r.const = False
        bk = getbookkeeper()
        if variables is None:
            op = bk._find_current_op("simple_call", 3)
            assert op.args[0] == Constant(isinstance)
            variables = [op.args[1]]
        for variable in variables:
            assert bk.annotator.binding(variable) == s_obj
        knowntypedata = {}
        if not hasattr(typ, '_freeze_') and isinstance(s_type, SomePBC):
            add_knowntypedata(knowntypedata, True, variables, bk.valueoftype(typ))
        r.set_knowntypedata(knowntypedata)
    return r
Beispiel #9
0
def s_isinstance(annotator, s_obj, s_type, variables):
    if not s_type.is_constant():
        return SomeBool()
    r = SomeBool()
    typ = s_type.const
    bk = annotator.bookkeeper
    if s_obj.is_constant():
        r.const = isinstance(s_obj.const, typ)
    elif our_issubclass(bk, s_obj.knowntype, typ):
        if not s_obj.can_be_none():
            r.const = True
    elif not our_issubclass(bk, typ, s_obj.knowntype):
        r.const = False
    elif s_obj.knowntype == int and typ == bool:  # xxx this will explode in case of generalisation
        # from bool to int, notice that isinstance( , bool|int)
        # is quite border case for RPython
        r.const = False
    for v in variables:
        assert v.annotation == s_obj
    knowntypedata = defaultdict(dict)
    if not hasattr(typ, '_freeze_') and isinstance(s_type, SomePBC):
        add_knowntypedata(knowntypedata, True, variables, bk.valueoftype(typ))
    r.set_knowntypedata(knowntypedata)
    return r
Beispiel #10
0
def s_isinstance(annotator, s_obj, s_type, variables):
    if not s_type.is_constant():
        return SomeBool()
    r = SomeBool()
    typ = s_type.const
    bk = annotator.bookkeeper
    if s_obj.is_constant():
        r.const = isinstance(s_obj.const, typ)
    elif our_issubclass(bk, s_obj.knowntype, typ):
        if not s_obj.can_be_none():
            r.const = True
    elif not our_issubclass(bk, typ, s_obj.knowntype):
        r.const = False
    elif s_obj.knowntype == int and typ == bool: # xxx this will explode in case of generalisation
                                            # from bool to int, notice that isinstance( , bool|int)
                                            # is quite border case for RPython
        r.const = False
    for v in variables:
        assert v.annotation == s_obj
    knowntypedata = defaultdict(dict)
    if not hasattr(typ, '_freeze_') and isinstance(s_type, SomePBC):
        add_knowntypedata(knowntypedata, True, variables, bk.valueoftype(typ))
    r.set_knowntypedata(knowntypedata)
    return r
Beispiel #11
0
 def bind(src_obj, tgt_obj):
     s_src = annotator.annotation(src_obj)
     s_tgt = annotator.annotation(tgt_obj)
     if hasattr(s_tgt, 'is_type_of') and s_src.is_constant():
         add_knowntypedata(knowntypedata, True, s_tgt.is_type_of,
                           bk.valueoftype(s_src.const))
     add_knowntypedata(knowntypedata, True, [tgt_obj], s_src)
     s_nonnone = s_tgt
     if (s_src.is_constant() and s_src.const is None
             and s_tgt.can_be_none()):
         s_nonnone = s_tgt.nonnoneify()
     add_knowntypedata(knowntypedata, False, [tgt_obj], s_nonnone)
Beispiel #12
0
            def bind(src_obj, tgt_obj, tgt_arg):
                if hasattr(tgt_obj, 'is_type_of') and src_obj.is_constant():
                    add_knowntypedata(knowntypedata, True, tgt_obj.is_type_of,
                                      bk.valueoftype(src_obj.const))

                assert annotator.binding(op.args[tgt_arg]) == tgt_obj
                add_knowntypedata(knowntypedata, True, [op.args[tgt_arg]], src_obj)

                nonnone_obj = tgt_obj
                if src_obj.is_constant() and src_obj.const is None and tgt_obj.can_be_none():
                    nonnone_obj = tgt_obj.nonnoneify()

                add_knowntypedata(knowntypedata, False, [op.args[tgt_arg]], nonnone_obj)
Beispiel #13
0
 def bind(src_obj, tgt_obj):
     s_src = annotator.annotation(src_obj)
     s_tgt = annotator.annotation(tgt_obj)
     if hasattr(s_tgt, 'is_type_of') and s_src.is_constant():
         add_knowntypedata(
             knowntypedata, True,
             s_tgt.is_type_of,
             bk.valueoftype(s_src.const))
     add_knowntypedata(knowntypedata, True, [tgt_obj], s_src)
     s_nonnone = s_tgt
     if (s_src.is_constant() and s_src.const is None and
             s_tgt.can_be_none()):
         s_nonnone = s_tgt.nonnoneify()
     add_knowntypedata(knowntypedata, False, [tgt_obj], s_nonnone)