[pypy-svn] r23717 - in pypy/dist/pypy: annotation rpython

micktwomey at codespeak.net micktwomey at codespeak.net
Tue Feb 28 01:41:09 CET 2006


Author: micktwomey
Date: Tue Feb 28 01:41:06 2006
New Revision: 23717

Modified:
   pypy/dist/pypy/annotation/bookkeeper.py
   pypy/dist/pypy/rpython/extregistry.py
Log:
Refactored extregistry to use more consistent get_annotation method signatures

This allows us to implement generic lookup and is_registered functions.


Modified: pypy/dist/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/dist/pypy/annotation/bookkeeper.py	(original)
+++ pypy/dist/pypy/annotation/bookkeeper.py	Tue Feb 28 01:41:06 2006
@@ -23,8 +23,7 @@
 from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.rpython.ootypesystem import ootype
 from pypy.rpython.memory import lladdress
-from pypy.rpython.extregistry import EXT_REGISTRY_BY_VALUE, EXT_REGISTRY_BY_TYPE
-from pypy.rpython.extregistry import EXT_REGISTRY_BY_METATYPE
+from pypy.rpython import extregistry
 
 class Stats:
 
@@ -360,16 +359,12 @@
         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 ishashable(x) and x in EXT_REGISTRY_BY_VALUE:
-            result = EXT_REGISTRY_BY_VALUE[x].get_annotation(x)
+        elif extregistry.is_registered(x):
+            result = extregistry.lookup(x).get_annotation(tp, x)
         elif hasattr(x, "compute_result_annotation"):
             result = SomeBuiltin(x.compute_result_annotation, methodname=x.__name__)
-        elif tp in EXT_REGISTRY_BY_TYPE:
-            result = EXT_REGISTRY_BY_TYPE[tp].get_annotation(x)
         elif hasattr(tp, "compute_annotation"):
             result = tp.compute_annotation()
-        elif type(tp) in EXT_REGISTRY_BY_METATYPE:
-            result = EXT_REGISTRY_BY_METATYPE[type(tp)].get_annotation(tp, x)
         elif tp in DEFINED_SOMEOBJECTS:
             return SomeObject()
         elif tp in EXTERNAL_TYPE_ANALYZERS:
@@ -519,10 +514,8 @@
             return SomeExternalObject(t)
         elif hasattr(t, "compute_annotation"):
             return t.compute_annotation()
-        elif t in EXT_REGISTRY_BY_TYPE:
-            return EXT_REGISTRY_BY_TYPE[t].get_annotation()
-        elif type(t) in EXT_REGISTRY_BY_METATYPE:
-            return EXT_REGISTRY_BY_METATYPE[type(t)].get_annotation(t)
+        elif extregistry.is_registered_type(t):
+            return extregistry.lookup_type(t).get_annotation(t)
         elif t.__module__ != '__builtin__' and t not in self.pbctypes:
             classdef = self.getuniqueclassdef(t)
             return SomeInstance(classdef)

Modified: pypy/dist/pypy/rpython/extregistry.py
==============================================================================
--- pypy/dist/pypy/rpython/extregistry.py	(original)
+++ pypy/dist/pypy/rpython/extregistry.py	Tue Feb 28 01:41:06 2006
@@ -4,7 +4,8 @@
     def __init__(self, compute_result_annotation):
         self.compute_result_annotation = compute_result_annotation
     
-    def get_annotation(self, func):
+    def get_annotation(self, type, func=None):
+        assert func is not None
         from pypy.annotation import model as annmodel
         return annmodel.SomeBuiltin(self.compute_result_annotation, methodname=func.__name__)
 
@@ -12,7 +13,7 @@
     def __init__(self, compute_annotation):
         self.compute_annotation = compute_annotation
     
-    def get_annotation(self, instance=None):
+    def get_annotation(self, type, instance=None):
         return self.compute_annotation(instance)
 
 class ExtRegistryMetaType(object):
@@ -50,4 +51,29 @@
 
 def register_metatype(t, compute_annotation):
     EXT_REGISTRY_BY_METATYPE[t] = ExtRegistryMetaType(compute_annotation)
-    
\ No newline at end of file
+
+def lookup_type(tp):
+    try:
+        return EXT_REGISTRY_BY_TYPE[tp]
+    except KeyError:
+        return EXT_REGISTRY_BY_METATYPE[type(tp)]
+
+def is_registered_type(tp):
+    try:
+        lookup_type(tp)
+    except KeyError:
+        return False
+    return True
+
+def lookup(instance):
+    try:
+        return EXT_REGISTRY_BY_VALUE[instance]
+    except (KeyError, TypeError):
+        return lookup_type(type(instance))
+        
+def is_registered(instance):
+    try:
+        lookup(instance)
+    except KeyError:
+        return False
+    return True



More information about the Pypy-commit mailing list