Beispiel #1
0
def annotationoftype(t, bookkeeper=False):
    from rpython.rtyper import extregistry
    """The most precise SomeValue instance that contains all
    objects of type t."""
    assert isinstance(t, (type, types.ClassType))
    if t is bool:
        return SomeBool()
    elif t is int:
        return SomeInteger()
    elif t is float:
        return SomeFloat()
    elif issubclass(t, str):  # py.lib uses annotated str subclasses
        return SomeString()
    elif t is unicode:
        return SomeUnicodeString()
    elif t is types.NoneType:
        return s_None
    elif bookkeeper and extregistry.is_registered_type(t):
        entry = extregistry.lookup_type(t)
        return entry.compute_annotation_bk(bookkeeper)
    elif t is type:
        return SomeType()
    elif bookkeeper and not hasattr(t, '_freeze_'):
        classdef = bookkeeper.getuniqueclassdef(t)
        return SomeInstance(classdef)
    else:
        raise AssertionError("annotationoftype(%r)" % (t, ))
Beispiel #2
0
 def method_decode(self, s_enc):
     if not s_enc.is_constant():
         raise AnnotatorError("Non-constant encoding not supported")
     enc = s_enc.const
     if enc not in ('ascii', 'latin-1', 'utf-8'):
         raise AnnotatorError("Encoding %s not supported for strings" % (enc,))
     return SomeUnicodeString(no_nul=self.no_nul)
Beispiel #3
0
 def method_decode(self, s_enc):
     if not s_enc.is_constant():
         raise AnnotatorError("Non-constant encoding not supported")
     enc = s_enc.const
     if enc not in ('ascii', 'latin-1', 'utf-8', 'utf8'):
         raise AnnotatorError("Encoding %s not supported for strings" % (enc,))
     if enc == 'utf-8':
         from rpython.rlib import runicode
         bookkeeper = getbookkeeper()
         s_func = bookkeeper.immutablevalue(
                         runicode.str_decode_utf_8_elidable)
         s_errors = bookkeeper.immutablevalue('strict')
         s_final = bookkeeper.immutablevalue(True)
         s_errorhandler = bookkeeper.immutablevalue(
                                 runicode.default_unicode_error_decode)
         s_allow_surr = bookkeeper.immutablevalue(True)
         args = [self, self.len(), s_errors, s_final, s_errorhandler,
                 s_allow_surr]
         bookkeeper.emulate_pbc_call(bookkeeper.position_key, s_func, args)
     return SomeUnicodeString(no_nul=self.no_nul)
Beispiel #4
0
 def add((str1, str2)):
     # propagate const-ness to help getattr(obj, 'prefix' + const_name)
     result = SomeUnicodeString()
     if str1.is_immutable_constant() and str2.is_immutable_constant():
         result.const = str1.const + str2.const
     return result
Beispiel #5
0
 def add((str1, str2)):
     # propagate const-ness to help getattr(obj, 'prefix' + const_name)
     result = SomeUnicodeString(no_nul=str1.no_nul and str2.no_nul)
     if str1.is_immutable_constant() and str2.is_immutable_constant():
         result.const = str1.const + str2.const
     return result
Beispiel #6
0
 def union((str1, str2)):
     can_be_None = str1.can_be_None or str2.can_be_None
     no_nul = str1.no_nul and str2.no_nul
     return SomeUnicodeString(can_be_None=can_be_None, no_nul=no_nul)
Beispiel #7
0
 def mul((str1, int2)):  # xxx do we want to support this
     return SomeUnicodeString(no_nul=str1.no_nul)
Beispiel #8
0
 def add((chr1, chr2)):
     return SomeUnicodeString()
Beispiel #9
0
 def unicode(self):
     return SomeUnicodeString()
Beispiel #10
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:
             raise Exception("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:
         if len(x) == 1:
             result = SomeUnicodeCodePoint()
         else:
             result = SomeUnicodeString()
     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 SomeOrderedDict.knowntype
           or tp is r_ordereddict):
         key = Constant(x)
         try:
             return self.immutable_cache[key]
         except KeyError:
             if tp is SomeOrderedDict.knowntype 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))
                     result.dictdef.seen_prebuilt_key(ek)
                 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_()
         self.see_mutable(x)
         result = SomeInstance(self.getuniqueclassdef(x.__class__))
     elif x is None:
         return s_None
     else:
         raise Exception("Don't know how to represent %r" % (x, ))
     result.const = x
     return result
Beispiel #11
0
def builtin_unicode(s_unicode):
    return constpropagate(unicode, [s_unicode], SomeUnicodeString())
Beispiel #12
0
 def method_build(self):
     return SomeUnicodeString(can_be_None=False)
Beispiel #13
0
 def union((str1, str2)):
     return SomeUnicodeString(
         can_be_None=str1.can_be_none() or str2.can_be_none())
Beispiel #14
0
        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()
        r_key = s_key.rtyper_makerepr(rtyper)
        r_value = s_value.rtyper_makerepr(rtyper)
Beispiel #15
0
 def method_build(self):
     return SomeUnicodeString()