Example #1
0
 def compute_result_annotation(self, s_arg):
     from rpython.annotator.model import SomeInteger
     if isinstance(s_arg, SomeInteger) and s_arg.unsigned:
         raise UnexpectedRUInt("check_nonneg() arg is a %s" % (
             s_arg.knowntype,))
     s_nonneg = SomeInteger(nonneg=True)
     if not s_nonneg.contains(s_arg):
         raise IntegerCanBeNegative
     return s_arg
Example #2
0
def test_annotation_to_lltype():
    s_i = SomeInteger()
    s_pos = SomeInteger(nonneg=True)
    s_1 = SomeInteger(nonneg=True)
    s_1.const = 1
    s_m1 = SomeInteger(nonneg=False)
    s_m1.const = -1
    s_u = SomeInteger(nonneg=True, unsigned=True)
    s_u1 = SomeInteger(nonneg=True, unsigned=True)
    s_u1.const = r_uint(1)
    assert annotation_to_lltype(s_i) == lltype.Signed
    assert annotation_to_lltype(s_pos) == lltype.Signed
    assert annotation_to_lltype(s_1) == lltype.Signed
    assert annotation_to_lltype(s_m1) == lltype.Signed
    assert annotation_to_lltype(s_u) == lltype.Unsigned
    assert annotation_to_lltype(s_u1) == lltype.Unsigned
    assert annotation_to_lltype(SomeBool()) == lltype.Bool
    assert annotation_to_lltype(SomeChar()) == lltype.Char
    PS = lltype.Ptr(lltype.GcStruct('s'))
    s_p = SomePtr(ll_ptrtype=PS)
    assert annotation_to_lltype(s_p) == PS
    si0 = SomeInstance(DummyClassDef(), True)
    with py.test.raises(ValueError):
        annotation_to_lltype(si0)
    s_singlefloat = SomeSingleFloat()
    s_singlefloat.const = r_singlefloat(0.0)
    assert annotation_to_lltype(s_singlefloat) == lltype.SingleFloat
Example #3
0
def rarith_intmask(s_obj):
    return SomeInteger()
Example #4
0
 def ord(self):
     # warning, on 32-bit with 32-bit unichars, this might return
     # negative numbers
     return SomeInteger()
Example #5
0
    def execute(self, space):
        getattr(space, self.method)(*self.args)

    def __repr__(self):
        return "space.%s(%s)" % (self.method, ', '.join(map(repr, self.args)))


class PseudoRTyper:
    cache_dummy_values = {}


# XXX: None keys crash the test, but translation sort-of allows it
keytypes_s = [
    SomeString(),
    SomeInteger(),
    SomeChar(),
    SomeUnicodeString(),
    SomeUnicodeCodePoint()
]
st_keys = sampled_from(keytypes_s)
st_values = sampled_from(keytypes_s + [SomeString(can_be_None=True)])


class MappingSpace(object):
    def __init__(self, s_key, s_value):
        from rpython.rtyper.rtuple import TupleRepr

        self.s_key = s_key
        self.s_value = s_value
        rtyper = PseudoRTyper()
Example #6
0
 def method_index(self, s_value):
     self.listdef.generalize(s_value)
     return SomeInteger(nonneg=True)
Example #7
0
 def method_count(self, frag, start=None, end=None):
     check_negative_slice(start, end, "count")
     return SomeInteger(nonneg=True)
Example #8
0
 def abs(self):
     return SomeInteger(nonneg=True, knowntype=self.knowntype)
Example #9
0
 def neg(self):
     return SomeInteger()
Example #10
0
def rarith_longlongmask(s_obj):
    return SomeInteger(knowntype=rpython.rlib.rarithmetic.r_longlong)
Example #11
0
 def invert(self):
     return SomeInteger(knowntype=self.knowntype)
Example #12
0
 def rshift((int1, int2)):
     if isinstance(int1, SomeBool):
         return SomeInteger(nonneg=True)
     else:
         return SomeInteger(nonneg=int1.nonneg, knowntype=int1.knowntype)
Example #13
0
 def getitem((s_b, s_i)):
     return SomeInteger()
Example #14
0
 def lshift((int1, int2)):
     if isinstance(int1, SomeBool):
         return SomeInteger()
     else:
         return SomeInteger(knowntype=int1.knowntype)
Example #15
0
 def and_((int1, int2)):
     knowntype = rarithmetic.compute_restype(int1.knowntype, int2.knowntype)
     return SomeInteger(nonneg=int1.nonneg or int2.nonneg,
                        knowntype=knowntype)
Example #16
0
 def sub((int1, int2)):
     knowntype = rarithmetic.compute_restype(int1.knowntype, int2.knowntype)
     return SomeInteger(knowntype=knowntype)
Example #17
0
 def cmp((obj1, obj2)):
     if obj1.is_immutable_constant() and obj2.is_immutable_constant():
         return immutablevalue(cmp(obj1.const, obj2.const))
     else:
         return SomeInteger()
Example #18
0
def test_annotation_to_lltype():
    s_i = SomeInteger()
    s_pos = SomeInteger(nonneg=True)
    s_1 = SomeInteger(nonneg=True)
    s_1.const = 1
    s_m1 = SomeInteger(nonneg=False)
    s_m1.const = -1
    s_u = SomeInteger(nonneg=True, unsigned=True)
    s_u1 = SomeInteger(nonneg=True, unsigned=True)
    s_u1.const = r_uint(1)
    assert annotation_to_lltype(s_i) == lltype.Signed
    assert annotation_to_lltype(s_pos) == lltype.Signed
    assert annotation_to_lltype(s_1) == lltype.Signed
    assert annotation_to_lltype(s_m1) == lltype.Signed
    assert annotation_to_lltype(s_u) == lltype.Unsigned
    assert annotation_to_lltype(s_u1) == lltype.Unsigned
    assert annotation_to_lltype(SomeBool()) == lltype.Bool
    assert annotation_to_lltype(SomeChar()) == lltype.Char
    PS = lltype.Ptr(lltype.GcStruct('s'))
    s_p = SomePtr(ll_ptrtype=PS)
    assert annotation_to_lltype(s_p) == PS
    si0 = SomeInstance(DummyClassDef(), True)
    with py.test.raises(ValueError):
        annotation_to_lltype(si0)
    s_singlefloat = SomeSingleFloat()
    s_singlefloat.const = r_singlefloat(0.0)
    assert annotation_to_lltype(s_singlefloat) == lltype.SingleFloat
Example #19
0
 def len(self):
     return SomeInteger(nonneg=True)
Example #20
0
 def immutablevalue(self, x):
     """The most precise SomeValue instance that contains the
     immutable value x."""
     # convert unbound methods to the underlying function
     if hasattr(x, 'im_self') and x.im_self is None:
         x = x.im_func
         assert not hasattr(x, 'im_self')
     tp = type(x)
     if issubclass(tp, Symbolic):  # symbolic constants support
         result = x.annotation()
         result.const_box = Constant(x)
         return result
     if tp is bool:
         result = SomeBool()
     elif tp is int:
         result = SomeInteger(nonneg=x >= 0)
     elif tp is long:
         if -sys.maxint - 1 <= x <= sys.maxint:
             x = int(x)
             result = SomeInteger(nonneg=x >= 0)
         else:
             # XXX: better error reporting?
             raise ValueError("seeing a prebuilt long (value %s)" % hex(x))
     elif issubclass(tp, str):  # py.lib uses annotated str subclasses
         no_nul = not '\x00' in x
         if len(x) == 1:
             result = SomeChar(no_nul=no_nul)
         else:
             result = SomeString(no_nul=no_nul)
     elif tp is unicode:
         no_nul = not u'\x00' in x
         if len(x) == 1:
             result = SomeUnicodeCodePoint(no_nul=no_nul)
         else:
             result = SomeUnicodeString(no_nul=no_nul)
     elif tp is bytearray:
         result = SomeByteArray()
     elif tp is tuple:
         result = SomeTuple(items=[self.immutablevalue(e) for e in x])
     elif tp is float:
         result = SomeFloat()
     elif tp is list:
         key = Constant(x)
         try:
             return self.immutable_cache[key]
         except KeyError:
             result = SomeList(ListDef(self, s_ImpossibleValue))
             self.immutable_cache[key] = result
             for e in x:
                 result.listdef.generalize(self.immutablevalue(e))
             result.const_box = key
             return result
     elif (tp is dict or tp is r_dict or tp is OrderedDict
           or tp is r_ordereddict):
         key = Constant(x)
         try:
             return self.immutable_cache[key]
         except KeyError:
             if tp is OrderedDict or tp is r_ordereddict:
                 cls = SomeOrderedDict
             else:
                 cls = SomeDict
             is_r_dict = issubclass(tp, r_dict)
             result = cls(
                 DictDef(self,
                         s_ImpossibleValue,
                         s_ImpossibleValue,
                         is_r_dict=is_r_dict))
             self.immutable_cache[key] = result
             if is_r_dict:
                 s_eqfn = self.immutablevalue(x.key_eq)
                 s_hashfn = self.immutablevalue(x.key_hash)
                 result.dictdef.dictkey.update_rdict_annotations(
                     s_eqfn, s_hashfn)
             seen_elements = 0
             while seen_elements != len(x):
                 items = x.items()
                 for ek, ev in items:
                     result.dictdef.generalize_key(self.immutablevalue(ek))
                     result.dictdef.generalize_value(
                         self.immutablevalue(ev))
                     #dictdef.seen_prebuilt_key(ek)---not needed any more
                 seen_elements = len(items)
                 # if the dictionary grew during the iteration,
                 # start over again
             result.const_box = key
             return result
     elif tp is weakref.ReferenceType:
         x1 = x()
         if x1 is None:
             result = SomeWeakRef(None)  # dead weakref
         else:
             s1 = self.immutablevalue(x1)
             assert isinstance(s1, SomeInstance)
             result = SomeWeakRef(s1.classdef)
     elif tp is property:
         return SomeProperty(x)
     elif ishashable(x) and x in BUILTIN_ANALYZERS:
         _module = getattr(x, "__module__", "unknown")
         result = SomeBuiltin(BUILTIN_ANALYZERS[x],
                              methodname="%s.%s" % (_module, x.__name__))
     elif extregistry.is_registered(x):
         entry = extregistry.lookup(x)
         result = entry.compute_annotation_bk(self)
     elif tp is type:
         result = SomeConstantType(x, self)
     elif callable(x):
         if hasattr(x, 'im_self') and hasattr(x, 'im_func'):
             # on top of PyPy, for cases like 'l.append' where 'l' is a
             # global constant list, the find_method() returns non-None
             s_self = self.immutablevalue(x.im_self)
             result = s_self.find_method(x.im_func.__name__)
         elif hasattr(x, '__self__') and x.__self__ is not None:
             # for cases like 'l.append' where 'l' is a global constant list
             s_self = self.immutablevalue(x.__self__)
             result = s_self.find_method(x.__name__)
             assert result is not None
         else:
             result = None
         if result is None:
             result = SomePBC([self.getdesc(x)])
     elif hasattr(x, '_freeze_'):
         assert x._freeze_() is True
         # user-defined classes can define a method _freeze_(), which
         # is called when a prebuilt instance is found.  If the method
         # returns True, the instance is considered immutable and becomes
         # a SomePBC().  Otherwise it's just SomeInstance().
         result = SomePBC([self.getdesc(x)])
     elif hasattr(x, '__class__') \
              and x.__class__.__module__ != '__builtin__':
         if hasattr(x, '_cleanup_'):
             x._cleanup_()
         classdef = self.getuniqueclassdef(x.__class__)
         classdef.see_instance(x)
         result = SomeInstance(classdef)
     elif x is None:
         return s_None
     else:
         raise AnnotatorError("Don't know how to represent %r" % (x, ))
     result.const = x
     return result
Example #21
0
 def neg(self):
     return SomeInteger(knowntype=self.knowntype)
Example #22
0
def ann_raw_malloc_usage(s_size):
    assert isinstance(s_size, SomeInteger)  # XXX add noneg...?
    return SomeInteger(nonneg=True)
Example #23
0
 def invert(self):
     return SomeInteger()
Example #24
0
def ann_offsetof(TYPE, fldname):
    return SomeInteger()
Example #25
0
 def pos(self):
     return SomeInteger(nonneg=True)
Example #26
0
def ann_cast_adr_to_int(s, s_mode=None):
    return SomeInteger()  # xxx
Example #27
0
 def method_rfind(self, frag, start=None, end=None):
     check_negative_slice(start, end, "rfind")
     return SomeInteger()
Example #28
0
# ____________________________________________________________


class Sample(object):
    __slots__ = 'x'


MemberDescriptorTypes = [type(Sample.x)]
del Sample
try:
    MemberDescriptorTypes.append(type(OSError.errno))
except AttributeError:  # on CPython <= 2.4
    pass

# ____________________________________________________________

FORCE_ATTRIBUTES_INTO_CLASSES = {
    EnvironmentError: {
        'errno': SomeInteger(),
        'strerror': SomeString(can_be_None=True),
        'filename': SomeString(can_be_None=True)
    },
}

try:
    WindowsError
except NameError:
    pass
else:
    FORCE_ATTRIBUTES_INTO_CLASSES[WindowsError] = {'winerror': SomeInteger()}
Example #29
0
 def ord(self):
     return SomeInteger(nonneg=True)
Example #30
0
File: rgc.py Project: sota/pypy
 def compute_result_annotation(self, s_no):
     from rpython.annotator.model import SomeInteger
     if not isinstance(s_no, SomeInteger):
         raise Exception("expecting an integer")
     return SomeInteger()
Example #31
0
 def method_getlength(self):
     return SomeInteger(nonneg=True)
Example #32
0
 def compute_result_annotation(self, s_arg):
     from rpython.annotator.model import SomeInteger
     if not SomeInteger().contains(s_arg):
         raise ExpectedRegularInt(s_arg)
     return s_arg