[pypy-svn] r23711 - in pypy/dist/pypy: annotation rpython rpython/test

micktwomey at codespeak.net micktwomey at codespeak.net
Mon Feb 27 23:00:34 CET 2006


Author: micktwomey
Date: Mon Feb 27 23:00:30 2006
New Revision: 23711

Modified:
   pypy/dist/pypy/annotation/bookkeeper.py
   pypy/dist/pypy/rpython/extregistry.py
   pypy/dist/pypy/rpython/test/test_extregistry.py
Log:
Implemented types and metatypes in the extregistry


Modified: pypy/dist/pypy/annotation/bookkeeper.py
==============================================================================
--- pypy/dist/pypy/annotation/bookkeeper.py	(original)
+++ pypy/dist/pypy/annotation/bookkeeper.py	Mon Feb 27 23:00:30 2006
@@ -24,6 +24,7 @@
 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
 
 class Stats:
 
@@ -367,6 +368,8 @@
             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)
         elif tp in DEFINED_SOMEOBJECTS:
             return SomeObject()
         elif tp in EXTERNAL_TYPE_ANALYZERS:
@@ -516,6 +519,10 @@
             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 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	Mon Feb 27 23:00:30 2006
@@ -1,13 +1,23 @@
-class ExtRegistryFunc:
+import weakref
+
+class ExtRegistryFunc(object):
     def __init__(self, compute_result_annotation):
         self.compute_result_annotation = compute_result_annotation
     
     def get_annotation(self, func):
         from pypy.annotation import model as annmodel
         return annmodel.SomeBuiltin(self.compute_result_annotation, methodname=func.__name__)
+
+class ExtRegistryType(object):
+    def __init__(self, compute_annotation):
+        self.compute_annotation = compute_annotation
     
-EXT_REGISTRY_BY_VALUE = {}
-EXT_REGISTRY_BY_TYPE = {}
+    def get_annotation(self, instance=None):
+        return self.compute_annotation(instance)
+    
+EXT_REGISTRY_BY_VALUE = weakref.WeakKeyDictionary()
+EXT_REGISTRY_BY_TYPE = weakref.WeakKeyDictionary()
+EXT_REGISTRY_BY_METATYPE = weakref.WeakKeyDictionary()
 
 def register_func(func, compute_result_annotation):
     from pypy.annotation import model as annmodel
@@ -20,6 +30,17 @@
     
     EXT_REGISTRY_BY_VALUE[func] = ExtRegistryFunc(compute_result_annotation)
     
-def register_type():
-    pass
+def register_type(t, compute_annotation):
+    from pypy.annotation import model as annmodel
+    if isinstance(compute_annotation, annmodel.SomeObject):
+        s_result = compute_annotation
+        def annotation(*args):
+            return s_result
+        
+        compute_annotation = annotation
+    
+    EXT_REGISTRY_BY_TYPE[t] = ExtRegistryType(compute_annotation)
 
+def register_metatype(t, compute_annotation):
+    EXT_REGISTRY_BY_METATYPE[t] = ExtRegistryType(compute_annotation)
+    
\ No newline at end of file

Modified: pypy/dist/pypy/rpython/test/test_extregistry.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_extregistry.py	(original)
+++ pypy/dist/pypy/rpython/test/test_extregistry.py	Mon Feb 27 23:00:30 2006
@@ -4,6 +4,7 @@
 
 from pypy.rpython.extregistry import EXT_REGISTRY_BY_VALUE, EXT_REGISTRY_BY_TYPE
 from pypy.rpython.extregistry import register_func, register_type
+from pypy.rpython.extregistry import register_metatype
 from pypy.annotation import model as annmodel
 from pypy.annotation.annrpython import RPythonAnnotator
 
@@ -20,3 +21,78 @@
     a = RPythonAnnotator()
     s = a.build_types(func, [])
     assert isinstance(s, annmodel.SomeInteger)
+
+def test_callable_annotation():
+    def dummy2():
+        raiseNameError
+    
+    def return_annotation():
+        return annmodel.SomeInteger()
+    
+    register_func(dummy2, return_annotation)
+    
+    def func():
+        x = dummy2()
+        return x
+    
+    a = RPythonAnnotator()
+    s = a.build_types(func, [])
+    assert isinstance(s, annmodel.SomeInteger)
+
+def test_register_type():
+    class DummyType(object):
+        pass
+    
+    dummy_type = DummyType()
+    
+    def func():
+        return dummy_type
+    
+    register_type(DummyType, annmodel.SomeInteger())
+    
+    a = RPythonAnnotator()
+    s = a.build_types(func, [])
+    assert isinstance(s, annmodel.SomeInteger)
+
+def test_register_metatype():
+    class MetaType(type):
+        pass
+    
+    class RealClass(object):
+        __metaclass__ = MetaType
+    
+    real_class = RealClass()
+    
+    def func():
+        return real_class
+    
+    def get_annotation(t):
+        assert t is RealClass
+        return annmodel.SomeInteger()
+    
+    register_metatype(MetaType, get_annotation)
+    
+    a = RPythonAnnotator()
+    s = a.build_types(func, [])
+    assert isinstance(s, annmodel.SomeInteger)
+
+def test_register_metatype_2():
+    class MetaType(type):
+        pass
+    
+    class RealClass(object):
+        __metaclass__ = MetaType
+    
+    def func(real_class):
+        return real_class
+    
+    def get_annotation(t):
+        assert t is RealClass
+        return annmodel.SomeInteger()
+    
+    register_metatype(MetaType, get_annotation)
+    
+    a = RPythonAnnotator()
+    s = a.build_types(func, [RealClass])
+    assert isinstance(s, annmodel.SomeInteger)
+    
\ No newline at end of file



More information about the Pypy-commit mailing list