[pypy-svn] r23792 - in pypy/dist/pypy/rpython: . rctypes test

micktwomey at codespeak.net micktwomey at codespeak.net
Tue Feb 28 23:20:12 CET 2006


Author: micktwomey
Date: Tue Feb 28 23:20:08 2006
New Revision: 23792

Modified:
   pypy/dist/pypy/rpython/extregistry.py
   pypy/dist/pypy/rpython/rctypes/implementation.py
   pypy/dist/pypy/rpython/rctypes/interface.py
   pypy/dist/pypy/rpython/test/test_extregistry.py
Log:
Simplified extregistry and collapsed the different type classes together

Registering ARRAY in the extregistry instead of using the RARRAY class.


Modified: pypy/dist/pypy/rpython/extregistry.py
==============================================================================
--- pypy/dist/pypy/rpython/extregistry.py	(original)
+++ pypy/dist/pypy/rpython/extregistry.py	Tue Feb 28 23:20:08 2006
@@ -10,16 +10,10 @@
         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
-    
-    def get_annotation(self, type, instance=None):
-        return self.compute_annotation(instance)
-
-class ExtRegistryMetaType(object):
-    def __init__(self, compute_annotation):
+class ExtRegistryInstance(object):
+    def __init__(self, compute_annotation, specialize_call):
         self.compute_annotation = compute_annotation
+        self.specialize_call = specialize_call
     
     def get_annotation(self, type, instance=None):
         return self.compute_annotation(type, instance)
@@ -28,35 +22,34 @@
 EXT_REGISTRY_BY_TYPE = weakref.WeakKeyDictionary()
 EXT_REGISTRY_BY_METATYPE = weakref.WeakKeyDictionary()
 
-def register_func(func, compute_result_annotation, specialize_call=None):
+def create_annotation_callable(annotation):
     from pypy.annotation import model as annmodel
-    if isinstance(compute_result_annotation, annmodel.SomeObject):
-        s_result = compute_result_annotation
+    if isinstance(annotation, annmodel.SomeObject):
+        s_result = annotation
         def annotation(*args):
             return s_result
         
-        compute_result_annotation = annotation
-    
-    EXT_REGISTRY_BY_VALUE[func] = ExtRegistryFunc(compute_result_annotation,
-                                                    specialize_call)
-    return EXT_REGISTRY_BY_VALUE[func]
-    
-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)
+    return annotation
 
+def create_entry(compute_result_annotation=None, compute_annotation=None,
+    specialize_call=None):
+    if compute_result_annotation is not None:
+        compute_result_annotation = create_annotation_callable(
+            compute_result_annotation)
+        return ExtRegistryFunc(compute_result_annotation, specialize_call)
+    else:
+        return ExtRegistryInstance(compute_annotation, specialize_call)
+
+def register_value(value, **kwargs):
+    EXT_REGISTRY_BY_VALUE[value] = create_entry(**kwargs)
+    return EXT_REGISTRY_BY_VALUE[value]
+    
+def register_type(t, **kwargs):
+    EXT_REGISTRY_BY_TYPE[t] = create_entry(**kwargs)
     return EXT_REGISTRY_BY_TYPE[t]
-
-def register_metatype(t, compute_annotation):
-    EXT_REGISTRY_BY_METATYPE[t] = ExtRegistryMetaType(compute_annotation)
-
+    
+def register_metatype(t, **kwargs):
+    EXT_REGISTRY_BY_METATYPE[t] = create_entry(**kwargs)
     return EXT_REGISTRY_BY_METATYPE[t]
 
 def lookup_type(tp):

Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py	Tue Feb 28 23:20:08 2006
@@ -16,7 +16,8 @@
         Void
 from pypy.rpython.rmodel import Repr, IntegerRepr, inputconst
 from pypy.rpython.error import TyperError
-from pypy.rpython.extregistry import register_func, register_metatype
+from pypy.rpython.extregistry import register_value, register_metatype, \
+    register_type
 from pypy.annotation.pairtype import pairtype
 
 
@@ -66,7 +67,8 @@
             # no 'wrap_arg'. This might change in the future
             #the_type.compute_result_annotation = classmethod(lambda cls, s_arg:SomeCTypesObject(cls))
             def do_register(the_type):
-                register_func(the_type, lambda s_arg: SomeCTypesObject(the_type))
+                register_value(the_type, 
+                compute_result_annotation=lambda s_arg: SomeCTypesObject(the_type))
             do_register(the_type)
             the_type.default_memorystate = SomeCTypesObject.NOMEMORY
 
@@ -97,11 +99,11 @@
             return instance.restype.annotator_type
         except AttributeError:
             return SomeCTypesObject(instance.restype)
-
+        
     return SomeBuiltin(compute_result_annotation, 
         methodname=instance.__name__)
 
-def specialize_call(hop):
+def cfuncptrtype_specialize_call(hop):
     # this is necessary to get the original function pointer when specializing
     # the metatype
     cfuncptr = hop.spaceop.args[0].value
@@ -125,8 +127,9 @@
             _callable=None,
             convert_params = convert_params ) 
 
-entry = register_metatype(CFuncPtrType, cfuncptrtype_compute_annotation)
-entry.specialize_call = specialize_call
+register_metatype(CFuncPtrType, 
+    compute_annotation=cfuncptrtype_compute_annotation,
+    specialize_call=cfuncptrtype_specialize_call)
 
 class FunctionPointerTranslation(object):
 
@@ -366,17 +369,28 @@
 #             """
 #             _flags_ = _FUNCFLAG_STDCALL
 
-def RARRAY(typ,length):
-    answer = ARRAY(typ,length)
-    def compute_result_annotation(cls, *arg_s):
-        """
-        Answer the result annotation of calling 'cls'.
-        """
-        assert answer is cls
-        return SomeCTypesObject(cls, SomeCTypesObject.OWNSMEMORY)
-    answer.compute_result_annotation = classmethod(compute_result_annotation)
-    return answer
-
+# def RARRAY(typ,length):
+#     answer = ARRAY(typ,length)
+#     def compute_result_annotation(cls, *arg_s):
+#         """
+#         Answer the result annotation of calling 'cls'.
+#         """
+#         assert answer is cls
+#         return SomeCTypesObject(cls, SomeCTypesObject.OWNSMEMORY)
+#     answer.compute_result_annotation = classmethod(compute_result_annotation)
+#     return answer
+
+ArrayType = type(ARRAY(c_int, 10))
+
+def arraytype_compute_annotation(metatype, type):
+    def compute_result_annotation(*arg_s):
+        return SomeCTypesObject(type, SomeCTypesObject.OWNSMEMORY)
+    return SomeBuiltin(compute_result_annotation,
+        methodname=type.__name__)
+
+register_type(ArrayType, 
+    compute_annotation=arraytype_compute_annotation,
+    specialize_call=None)
 
 class AbstractCtypesRepresentation( Repr ):
     """

Modified: pypy/dist/pypy/rpython/rctypes/interface.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/interface.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/interface.py	Tue Feb 28 23:20:08 2006
@@ -4,7 +4,7 @@
         c_short, c_ushort, c_uint,\
         c_long, c_ulong, c_longlong, c_ulonglong, c_float, c_double, \
         RStructure as Structure, RByref as byref, RPOINTER as POINTER, \
-        RARRAY as ARRAY
+        ARRAY
 #try:
 #    from implementation import RWinDLL as WinDLL
 #except ImportError:

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	Tue Feb 28 23:20:08 2006
@@ -3,7 +3,7 @@
 ##py.test.skip('In progress at PyCon')
 
 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_value, register_type
 from pypy.rpython.extregistry import register_metatype
 from pypy.annotation import model as annmodel
 from pypy.annotation.annrpython import RPythonAnnotator
@@ -14,7 +14,7 @@
 def dummy(): 
     raiseNameError
 
-register_func(dummy, annmodel.SomeInteger())
+register_value(dummy, compute_result_annotation=annmodel.SomeInteger())
 
 def test_call_dummy():
     def func():
@@ -32,7 +32,7 @@
     def return_annotation():
         return annmodel.SomeInteger()
     
-    register_func(dummy2, return_annotation)
+    register_value(dummy2, compute_result_annotation=return_annotation)
     
     def func():
         x = dummy2()
@@ -41,21 +41,6 @@
     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_type_with_callable():
     class DummyType(object):
@@ -66,11 +51,11 @@
     def func():
         return dummy_type
     
-    def get_annotation(instance):
+    def get_annotation(type, instance):
         assert instance is dummy_type
         return annmodel.SomeInteger()
     
-    register_type(DummyType, get_annotation)
+    register_type(DummyType, compute_annotation=get_annotation)
     
     a = RPythonAnnotator()
     s = a.build_types(func, [])
@@ -93,7 +78,7 @@
         assert x is real_class
         return annmodel.SomeInteger()
     
-    register_metatype(MetaType, get_annotation)
+    register_metatype(MetaType, compute_annotation=get_annotation)
     
     a = RPythonAnnotator()
     s = a.build_types(func, [])
@@ -114,20 +99,22 @@
         assert x is None
         return annmodel.SomeInteger()
     
-    register_metatype(MetaType, get_annotation)
+    register_metatype(MetaType, compute_annotation=get_annotation)
     
     a = RPythonAnnotator()
     s = a.build_types(func, [RealClass])
     assert isinstance(s, annmodel.SomeInteger)
 
-def test_register_func_with_specialization():
+def test_register_value_with_specialization():
     def dummy_func():
         raiseNameError
 
     def dummy_specialize(hop):
         return hop.inputconst(lltype.Signed, 42)
     
-    register_func(dummy_func, annmodel.SomeInteger(), dummy_specialize)
+    register_value(dummy_func, 
+        compute_result_annotation=annmodel.SomeInteger(), 
+        specialize_call=dummy_specialize)
     
     def func():
         return dummy_func()



More information about the Pypy-commit mailing list