Author: Ronan Lamy <[email protected]>
Branch: 
Changeset: r68950:a920e8967669
Date: 2014-01-26 06:03 +0000
http://bitbucket.org/pypy/pypy/changeset/a920e8967669/

Log:    Use a decorator to register builtin_analyzers.

        (Had anybody noticed that test() and termios_error_init() did
        nothing?)

diff --git a/rpython/annotator/builtin.py b/rpython/annotator/builtin.py
--- a/rpython/annotator/builtin.py
+++ b/rpython/annotator/builtin.py
@@ -44,6 +44,14 @@
             func, args, realresult, s_result))
     return s_realresult
 
+BUILTIN_ANALYZERS = {}
+
+def analyzer_for(func):
+    def wrapped(ann_func):
+        BUILTIN_ANALYZERS[func] = ann_func
+        return func
+    return wrapped
+
 # ____________________________________________________________
 
 def builtin_range(*args):
@@ -250,30 +258,46 @@
                 s = SomeInteger(nonneg=True, knowntype=s.knowntype)
         return s
 
+# collect all functions
+import __builtin__
+for name, value in globals().items():
+    if name.startswith('builtin_'):
+        original = getattr(__builtin__, name[8:])
+        BUILTIN_ANALYZERS[original] = value
 
+
+@analyzer_for(getattr(OSError.__init__, 'im_func', OSError.__init__))
 def OSError_init(s_self, *args):
     pass
 
-def WindowsError_init(s_self, *args):
+try:
+    WindowsError
+except NameError:
     pass
+else:
+    @analyzer_for(getattr(WindowsError.__init__, 'im_func', 
WindowsError.__init__))
+    def WindowsError_init(s_self, *args):
+        pass
 
-def termios_error_init(s_self, *args):
-    pass
-
+@analyzer_for(getattr(object.__init__, 'im_func', object.__init__))
 def object_init(s_self, *args):
     # ignore - mostly used for abstract classes initialization
     pass
 
 
+@analyzer_for(sys.getdefaultencoding)
 def conf():
     return SomeString()
 
+@analyzer_for(rpython.rlib.rarithmetic.intmask)
 def rarith_intmask(s_obj):
     return SomeInteger()
 
+@analyzer_for(rpython.rlib.rarithmetic.longlongmask)
 def rarith_longlongmask(s_obj):
     return SomeInteger(knowntype=rpython.rlib.rarithmetic.r_longlong)
 
+@analyzer_for(rpython.rlib.objectmodel.instantiate)
 def robjmodel_instantiate(s_clspbc):
     assert isinstance(s_clspbc, SomePBC)
     clsdef = None
@@ -288,6 +312,7 @@
             clsdef = clsdef.commonbase(cdef)
     return SomeInstance(clsdef)
 
+@analyzer_for(rpython.rlib.objectmodel.r_dict)
 def robjmodel_r_dict(s_eqfn, s_hashfn, s_force_non_null=None):
     if s_force_non_null is None:
         force_non_null = False
@@ -299,11 +324,13 @@
     dictdef.dictkey.update_rdict_annotations(s_eqfn, s_hashfn)
     return SomeDict(dictdef)
 
+@analyzer_for(rpython.rlib.objectmodel.r_ordereddict)
 def robjmodel_r_ordereddict(s_eqfn, s_hashfn):
     dictdef = getbookkeeper().getdictdef(is_r_dict=True)
     dictdef.dictkey.update_rdict_annotations(s_eqfn, s_hashfn)
     return SomeOrderedDict(dictdef)
 
+@analyzer_for(rpython.rlib.objectmodel.hlinvoke)
 def robjmodel_hlinvoke(s_repr, s_llcallable, *args_s):
     from rpython.rtyper import rmodel
     from rpython.rtyper.error import TyperError
@@ -323,81 +350,49 @@
     return lltype_to_annotation(rresult.lowleveltype)
 
 
+@analyzer_for(rpython.rlib.objectmodel.keepalive_until_here)
 def robjmodel_keepalive_until_here(*args_s):
     return immutablevalue(None)
 
+@analyzer_for(rpython.rtyper.lltypesystem.llmemory.cast_ptr_to_adr)
 def llmemory_cast_ptr_to_adr(s):
     from rpython.annotator.model import SomeInteriorPtr
     assert not isinstance(s, SomeInteriorPtr)
     return SomeAddress()
 
+@analyzer_for(rpython.rtyper.lltypesystem.llmemory.cast_adr_to_ptr)
 def llmemory_cast_adr_to_ptr(s, s_type):
     assert s_type.is_constant()
     return SomePtr(s_type.const)
 
+@analyzer_for(rpython.rtyper.lltypesystem.llmemory.cast_adr_to_int)
 def llmemory_cast_adr_to_int(s, s_mode=None):
     return SomeInteger() # xxx
 
+@analyzer_for(rpython.rtyper.lltypesystem.llmemory.cast_int_to_adr)
 def llmemory_cast_int_to_adr(s):
     return SomeAddress()
 
-def unicodedata_decimal(s_uchr):
-    raise TypeError("unicodedate.decimal() calls should not happen at 
interp-level")
-
-def test(*args):
-    return s_Bool
-
-# collect all functions
-import __builtin__
-BUILTIN_ANALYZERS = {}
-for name, value in globals().items():
-    if name.startswith('builtin_'):
-        original = getattr(__builtin__, name[8:])
-        BUILTIN_ANALYZERS[original] = value
-
-BUILTIN_ANALYZERS[rpython.rlib.rarithmetic.intmask] = rarith_intmask
-BUILTIN_ANALYZERS[rpython.rlib.rarithmetic.longlongmask] = rarith_longlongmask
-BUILTIN_ANALYZERS[rpython.rlib.objectmodel.instantiate] = robjmodel_instantiate
-BUILTIN_ANALYZERS[rpython.rlib.objectmodel.r_dict] = robjmodel_r_dict
-BUILTIN_ANALYZERS[rpython.rlib.objectmodel.r_ordereddict] = 
robjmodel_r_ordereddict
-BUILTIN_ANALYZERS[SomeOrderedDict.knowntype] = lambda : 
SomeOrderedDict(getbookkeeper().getdictdef())
-BUILTIN_ANALYZERS[rpython.rlib.objectmodel.hlinvoke] = robjmodel_hlinvoke
-BUILTIN_ANALYZERS[rpython.rlib.objectmodel.keepalive_until_here] = 
robjmodel_keepalive_until_here
-BUILTIN_ANALYZERS[rpython.rtyper.lltypesystem.llmemory.cast_ptr_to_adr] = 
llmemory_cast_ptr_to_adr
-BUILTIN_ANALYZERS[rpython.rtyper.lltypesystem.llmemory.cast_adr_to_ptr] = 
llmemory_cast_adr_to_ptr
-BUILTIN_ANALYZERS[rpython.rtyper.lltypesystem.llmemory.cast_adr_to_int] = 
llmemory_cast_adr_to_int
-BUILTIN_ANALYZERS[rpython.rtyper.lltypesystem.llmemory.cast_int_to_adr] = 
llmemory_cast_int_to_adr
-
-BUILTIN_ANALYZERS[getattr(OSError.__init__, 'im_func', OSError.__init__)] = (
-    OSError_init)
-
-try:
-    WindowsError
-except NameError:
-    pass
-else:
-    BUILTIN_ANALYZERS[getattr(WindowsError.__init__, 'im_func',
-                              WindowsError.__init__)] = (
-        WindowsError_init)
-
-BUILTIN_ANALYZERS[sys.getdefaultencoding] = conf
 try:
     import unicodedata
 except ImportError:
     pass
 else:
-    BUILTIN_ANALYZERS[unicodedata.decimal] = unicodedata_decimal # xxx
+    @analyzer_for(unicodedata.decimal)
+    def unicodedata_decimal(s_uchr):
+        raise TypeError("unicodedate.decimal() calls should not happen at 
interp-level")
 
-# object - just ignore object.__init__
-if hasattr(object.__init__, 'im_func'):
-    BUILTIN_ANALYZERS[object.__init__.im_func] = object_init
-else:
-    BUILTIN_ANALYZERS[object.__init__] = object_init
+@analyzer_for(SomeOrderedDict.knowntype)
+def analyze():
+    return SomeOrderedDict(getbookkeeper().getdictdef())
+
+
 
 # annotation of low-level types
 from rpython.annotator.model import SomePtr
 from rpython.rtyper.lltypesystem import lltype
 
+@analyzer_for(lltype.malloc)
 def malloc(s_T, s_n=None, s_flavor=None, s_zero=None, s_track_allocation=None,
            s_add_memory_pressure=None):
     assert (s_n is None or s_n.knowntype == int
@@ -422,6 +417,7 @@
         r = SomePtr(lltype.Ptr(s_T.const))
     return r
 
+@analyzer_for(lltype.free)
 def free(s_p, s_flavor, s_track_allocation=None):
     assert s_flavor.is_constant()
     assert s_track_allocation is None or s_track_allocation.is_constant()
@@ -431,34 +427,41 @@
     #p = lltype.malloc(T, flavor=s_flavor.const)
     #lltype.free(p, flavor=s_flavor.const)
 
+@analyzer_for(lltype.render_immortal)
 def render_immortal(s_p, s_track_allocation=None):
     assert s_track_allocation is None or s_track_allocation.is_constant()
 
+@analyzer_for(lltype.typeOf)
 def typeOf(s_val):
     lltype = annotation_to_lltype(s_val, info="in typeOf(): ")
     return immutablevalue(lltype)
 
+@analyzer_for(lltype.cast_primitive)
 def cast_primitive(T, s_v):
     assert T.is_constant()
     return ll_to_annotation(lltype.cast_primitive(T.const, 
annotation_to_lltype(s_v)._defl()))
 
+@analyzer_for(lltype.nullptr)
 def nullptr(T):
     assert T.is_constant()
     p = lltype.nullptr(T.const)
     return immutablevalue(p)
 
+@analyzer_for(lltype.cast_pointer)
 def cast_pointer(PtrT, s_p):
     assert isinstance(s_p, SomePtr), "casting of non-pointer: %r" % s_p
     assert PtrT.is_constant()
     cast_p = lltype.cast_pointer(PtrT.const, s_p.ll_ptrtype._defl())
     return SomePtr(ll_ptrtype=lltype.typeOf(cast_p))
 
+@analyzer_for(lltype.cast_opaque_ptr)
 def cast_opaque_ptr(PtrT, s_p):
     assert isinstance(s_p, SomePtr), "casting of non-pointer: %r" % s_p
     assert PtrT.is_constant()
     cast_p = lltype.cast_opaque_ptr(PtrT.const, s_p.ll_ptrtype._defl())
     return SomePtr(ll_ptrtype=lltype.typeOf(cast_p))
 
+@analyzer_for(lltype.direct_fieldptr)
 def direct_fieldptr(s_p, s_fieldname):
     assert isinstance(s_p, SomePtr), "direct_* of non-pointer: %r" % s_p
     assert s_fieldname.is_constant()
@@ -466,62 +469,54 @@
                                     s_fieldname.const)
     return SomePtr(ll_ptrtype=lltype.typeOf(cast_p))
 
+@analyzer_for(lltype.direct_arrayitems)
 def direct_arrayitems(s_p):
     assert isinstance(s_p, SomePtr), "direct_* of non-pointer: %r" % s_p
     cast_p = lltype.direct_arrayitems(s_p.ll_ptrtype._example())
     return SomePtr(ll_ptrtype=lltype.typeOf(cast_p))
 
+@analyzer_for(lltype.direct_ptradd)
 def direct_ptradd(s_p, s_n):
     assert isinstance(s_p, SomePtr), "direct_* of non-pointer: %r" % s_p
     # don't bother with an example here: the resulting pointer is the same
     return s_p
 
+@analyzer_for(lltype.cast_ptr_to_int)
 def cast_ptr_to_int(s_ptr): # xxx
     return SomeInteger()
 
+@analyzer_for(lltype.cast_int_to_ptr)
 def cast_int_to_ptr(PtrT, s_int):
     assert PtrT.is_constant()
     return SomePtr(ll_ptrtype=PtrT.const)
 
+@analyzer_for(lltype.identityhash)
 def identityhash(s_obj):
     assert isinstance(s_obj, SomePtr)
     return SomeInteger()
 
+@analyzer_for(lltype.getRuntimeTypeInfo)
 def getRuntimeTypeInfo(T):
     assert T.is_constant()
     return immutablevalue(lltype.getRuntimeTypeInfo(T.const))
 
+@analyzer_for(lltype.runtime_type_info)
 def runtime_type_info(s_p):
     assert isinstance(s_p, SomePtr), "runtime_type_info of non-pointer: %r" % 
s_p
     return 
SomePtr(lltype.typeOf(lltype.runtime_type_info(s_p.ll_ptrtype._example())))
 
+@analyzer_for(lltype.Ptr)
 def constPtr(T):
     assert T.is_constant()
     return immutablevalue(lltype.Ptr(T.const))
 
-BUILTIN_ANALYZERS[lltype.malloc] = malloc
-BUILTIN_ANALYZERS[lltype.free] = free
-BUILTIN_ANALYZERS[lltype.render_immortal] = render_immortal
-BUILTIN_ANALYZERS[lltype.typeOf] = typeOf
-BUILTIN_ANALYZERS[lltype.cast_primitive] = cast_primitive
-BUILTIN_ANALYZERS[lltype.nullptr] = nullptr
-BUILTIN_ANALYZERS[lltype.cast_pointer] = cast_pointer
-BUILTIN_ANALYZERS[lltype.cast_opaque_ptr] = cast_opaque_ptr
-BUILTIN_ANALYZERS[lltype.direct_fieldptr] = direct_fieldptr
-BUILTIN_ANALYZERS[lltype.direct_arrayitems] = direct_arrayitems
-BUILTIN_ANALYZERS[lltype.direct_ptradd] = direct_ptradd
-BUILTIN_ANALYZERS[lltype.cast_ptr_to_int] = cast_ptr_to_int
-BUILTIN_ANALYZERS[lltype.cast_int_to_ptr] = cast_int_to_ptr
-BUILTIN_ANALYZERS[lltype.identityhash] = identityhash
-BUILTIN_ANALYZERS[lltype.getRuntimeTypeInfo] = getRuntimeTypeInfo
-BUILTIN_ANALYZERS[lltype.runtime_type_info] = runtime_type_info
-BUILTIN_ANALYZERS[lltype.Ptr] = constPtr
 
 #________________________________
 # weakrefs
 
 import weakref
 
+@analyzer_for(weakref.ref)
 def weakref_ref(s_obj):
     if not isinstance(s_obj, SomeInstance):
         raise Exception("cannot take a weakref to %r" % (s_obj,))
@@ -530,8 +525,10 @@
                         "a weakref to cannot be None")
     return SomeWeakRef(s_obj.classdef)
 
-BUILTIN_ANALYZERS[weakref.ref] = weakref_ref
 
+from rpython.rtyper.lltypesystem import llmemory
+
+@analyzer_for(llmemory.weakref_create)
 def llweakref_create(s_obj):
     if (not isinstance(s_obj, SomePtr) or
         s_obj.ll_ptrtype.TO._gckind != 'gc'):
@@ -539,6 +536,7 @@
             s_obj,))
     return SomePtr(llmemory.WeakRefPtr)
 
+@analyzer_for(llmemory.weakref_deref )
 def llweakref_deref(s_ptrtype, s_wref):
     if not (s_ptrtype.is_constant() and
             isinstance(s_ptrtype.const, lltype.Ptr) and
@@ -551,10 +549,12 @@
                         "got %s" % (s_wref,))
     return SomePtr(s_ptrtype.const)
 
+@analyzer_for(llmemory.cast_ptr_to_weakrefptr)
 def llcast_ptr_to_weakrefptr(s_ptr):
     assert isinstance(s_ptr, SomePtr)
     return SomePtr(llmemory.WeakRefPtr)
 
+@analyzer_for(llmemory.cast_weakrefptr_to_ptr)
 def llcast_weakrefptr_to_ptr(s_ptrtype, s_wref):
     if not (s_ptrtype.is_constant() and
             isinstance(s_ptrtype.const, lltype.Ptr)):
@@ -566,56 +566,47 @@
                         "got %s" % (s_wref,))
     return SomePtr(s_ptrtype.const)
 
-from rpython.rtyper.lltypesystem import llmemory
-BUILTIN_ANALYZERS[llmemory.weakref_create] = llweakref_create
-BUILTIN_ANALYZERS[llmemory.weakref_deref ] = llweakref_deref
-BUILTIN_ANALYZERS[llmemory.cast_ptr_to_weakrefptr] = llcast_ptr_to_weakrefptr
-BUILTIN_ANALYZERS[llmemory.cast_weakrefptr_to_ptr] = llcast_weakrefptr_to_ptr
-
 #________________________________
 # non-gc objects
 
+@analyzer_for(rpython.rlib.objectmodel.free_non_gc_object)
 def robjmodel_free_non_gc_object(obj):
     pass
 
-BUILTIN_ANALYZERS[rpython.rlib.objectmodel.free_non_gc_object] = (
-    robjmodel_free_non_gc_object)
 
 #_________________________________
 # memory address
 
+@analyzer_for(llmemory.raw_malloc)
 def raw_malloc(s_size):
     assert isinstance(s_size, SomeInteger) #XXX add noneg...?
     return SomeAddress()
 
+@analyzer_for(llmemory.raw_malloc_usage)
 def raw_malloc_usage(s_size):
     assert isinstance(s_size, SomeInteger) #XXX add noneg...?
     return SomeInteger(nonneg=True)
 
+@analyzer_for(llmemory.raw_free)
 def raw_free(s_addr):
     assert isinstance(s_addr, SomeAddress)
 
+@analyzer_for(llmemory.raw_memclear)
 def raw_memclear(s_addr, s_int):
     assert isinstance(s_addr, SomeAddress)
     assert isinstance(s_int, SomeInteger)
 
+@analyzer_for(llmemory.raw_memcopy)
 def raw_memcopy(s_addr1, s_addr2, s_int):
     assert isinstance(s_addr1, SomeAddress)
     assert isinstance(s_addr2, SomeAddress)
     assert isinstance(s_int, SomeInteger) #XXX add noneg...?
 
-BUILTIN_ANALYZERS[llmemory.raw_malloc] = raw_malloc
-BUILTIN_ANALYZERS[llmemory.raw_malloc_usage] = raw_malloc_usage
-BUILTIN_ANALYZERS[llmemory.raw_free] = raw_free
-BUILTIN_ANALYZERS[llmemory.raw_memclear] = raw_memclear
-BUILTIN_ANALYZERS[llmemory.raw_memcopy] = raw_memcopy
 
 #_________________________________
 # offsetof/sizeof
 
 
+@analyzer_for(llmemory.offsetof)
 def offsetof(TYPE, fldname):
     return SomeInteger()
-
-BUILTIN_ANALYZERS[llmemory.offsetof] = offsetof
-
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to