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

micktwomey at codespeak.net micktwomey at codespeak.net
Wed Mar 1 01:37:29 CET 2006


Author: micktwomey
Date: Wed Mar  1 01:37:20 2006
New Revision: 23799

Added:
   pypy/dist/pypy/rpython/rctypes/rarray.py
Modified:
   pypy/dist/pypy/rpython/extregistry.py
   pypy/dist/pypy/rpython/rctypes/implementation.py
   pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
   pypy/dist/pypy/rpython/test/test_extregistry.py
Log:
(arigo, micktwomey, goden)

Added get_repr support to the extregistry

Implemented rctypes ARRAY support using the extregistry and get_repr.

Added a register_type call for each primitive in rctypes.implementation which needs to be updated with the correct repr for each one.


Modified: pypy/dist/pypy/rpython/extregistry.py
==============================================================================
--- pypy/dist/pypy/rpython/extregistry.py	(original)
+++ pypy/dist/pypy/rpython/extregistry.py	Wed Mar  1 01:37:20 2006
@@ -11,9 +11,10 @@
         return annmodel.SomeBuiltin(self.compute_result_annotation, methodname=func.__name__)
 
 class ExtRegistryInstance(object):
-    def __init__(self, compute_annotation, specialize_call):
+    def __init__(self, compute_annotation, specialize_call, get_repr):
         self.compute_annotation = compute_annotation
         self.specialize_call = specialize_call
+        self.get_repr = get_repr
     
     def get_annotation(self, type, instance=None):
         return self.compute_annotation(type, instance)
@@ -32,13 +33,14 @@
     return annotation
 
 def create_entry(compute_result_annotation=None, compute_annotation=None,
-    specialize_call=None):
+    specialize_call=None, get_repr=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)
+        return ExtRegistryInstance(compute_annotation, specialize_call, 
+            get_repr)
 
 def register_value(value, **kwargs):
     EXT_REGISTRY_BY_VALUE[value] = create_entry(**kwargs)

Modified: pypy/dist/pypy/rpython/rctypes/implementation.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/implementation.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/implementation.py	Wed Mar  1 01:37:20 2006
@@ -17,9 +17,12 @@
 from pypy.rpython.rmodel import Repr, IntegerRepr, inputconst
 from pypy.rpython.error import TyperError
 from pypy.rpython.extregistry import register_value, register_metatype, \
-    register_type
+    register_type, is_registered_type, lookup_type
 from pypy.annotation.pairtype import pairtype
+from pypy.rpython import rint
 
+# Importing for side effect of registering types with extregistry
+import pypy.rpython.rctypes.rarray
 
 # ctypes_annotation_list contains various attributes that
 # are used by the pypy annotation.
@@ -69,6 +72,9 @@
             def do_register(the_type):
                 register_value(the_type, 
                 compute_result_annotation=lambda s_arg: SomeCTypesObject(the_type))
+                # XXX we need to register the correct repr for each primitive
+                register_type(the_type,
+                    get_repr=lambda rtyper, s_primitive: rint.signed_repr)
             do_register(the_type)
             the_type.default_memorystate = SomeCTypesObject.NOMEMORY
 
@@ -380,24 +386,11 @@
 #     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 ):
     """
     The abstract base class of all ctypes low level representations.
     """
 
-
 class AbstractCtypesStructureRepresentation( AbstractCtypesRepresentation ):
     """
     The abstract base class of ctypes structures' low level representation.
@@ -505,6 +498,9 @@
         
 class __extend__( SomeCTypesObject ):
     def rtyper_makerepr( self, rtyper ):
+        if is_registered_type(self.knowntype):
+            entry = lookup_type(self.knowntype)
+            return entry.get_repr(rtyper, self)
         return self.knowntype.createLowLevelRepresentation( rtyper, self )
         
     def rtyper_makekey( self ):

Added: pypy/dist/pypy/rpython/rctypes/rarray.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/rarray.py	Wed Mar  1 01:37:20 2006
@@ -0,0 +1,48 @@
+from ctypes import ARRAY, c_int
+from pypy.annotation.model import SomeCTypesObject, SomeBuiltin
+from pypy.rpython import extregistry
+from pypy.rpython.rmodel import Repr
+from pypy.rpython.lltypesystem import lltype
+
+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__)
+
+
+class ArrayRepr(Repr):
+    def __init__(self, rtyper, type):
+        
+        item_ctype = type._type_
+        self.length = type._length_
+        
+        entry = extregistry.lookup_type(item_ctype)
+        r_item = entry.get_repr(rtyper, item_ctype)
+        
+        self.lowleveltype = lltype.Ptr(
+            lltype.GcStruct( "CtypesGcArray_%s" % type.__name__,
+                ( "c_data", lltype.Array(r_item.lowleveltype, 
+                    hints={"nolength": True})
+                )
+            )
+        )
+
+def arraytype_specialize_call(hop):
+    r_array = hop.r_result
+    return hop.genop("malloc_varsize", [
+        hop.inputconst(lltype.Void, r_array.lowleveltype.TO), 
+        hop.inputconst(lltype.Signed, r_array.length),
+        ], resulttype=r_array.lowleveltype,
+    )
+
+extregistry.register_type(ArrayType, 
+    compute_annotation=arraytype_compute_annotation,
+    specialize_call=arraytype_specialize_call)
+
+def arraytype_get_repr(rtyper, s_array):
+    return ArrayRepr(rtyper, s_array.knowntype)
+
+extregistry.register_metatype(ArrayType, get_repr=arraytype_get_repr)

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py	Wed Mar  1 01:37:20 2006
@@ -11,6 +11,7 @@
 from pypy.annotation.model import SomeCTypesObject, SomeObject
 from pypy import conftest
 import sys
+from pypy.rpython.test.test_llinterp import interpret
 
 thisdir = py.magic.autopath().dirpath()
 
@@ -430,7 +431,8 @@
     def test_specialize_pointer_to_struct(self):
         t = self.test_annotate_pointer_to_struct()
         t.buildrtyper().specialize()
-        #d#t.view()
+        if conftest.option.view:
+            t.view()
 
     def x_test_compile_pointer_to_struct(self):
         fn = compile( py_testfunc_struct_pointer_id, [ oppoint_type ] )
@@ -528,3 +530,10 @@
         
         py.test.raises(IndexError, "s = a.build_types(py_test_annotate_array_content_index_error_on_negative_index,[])")
 
+    def test_specialize_array(self):
+        res = interpret(py_test_annotate_array, [])
+        c_data = res.c_data
+        assert c_data[0] == 0
+        assert c_data[9] == 0
+        py.test.raises(IndexError, "c_data[10]")
+        py.test.raises(TypeError, "len(c_data)")

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	Wed Mar  1 01:37:20 2006
@@ -5,11 +5,13 @@
 from pypy.rpython.extregistry import EXT_REGISTRY_BY_VALUE, EXT_REGISTRY_BY_TYPE
 from pypy.rpython.extregistry import register_value, register_type
 from pypy.rpython.extregistry import register_metatype
+from pypy.rpython.extregistry import lookup_type
 from pypy.annotation import model as annmodel
 from pypy.annotation.annrpython import RPythonAnnotator
 from pypy.translator.translator import TranslationContext
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.test.test_llinterp import interpret
+from pypy.rpython.rmodel import Repr
 
 def dummy(): 
     raiseNameError
@@ -122,3 +124,43 @@
     res = interpret(func, [])
 
     assert res == 42
+
+def test_register_type_with_get_repr():
+    class DummyClass(object):
+        pass
+    
+    class SomeDummyObject(annmodel.SomeObject):
+        def rtyper_makerepr(self, rtyper):
+            entry = lookup_type(self.knowntype)
+            return entry.get_repr(rtyper, self)
+            
+        def rtyper_makekey( self ):
+            return self.__class__, self.knowntype
+    
+    def get_annotation(type, instance=None):
+        assert type is DummyClass
+        dummy_object = SomeDummyObject()
+        dummy_object.knowntype = DummyClass
+        return dummy_object
+    
+    class DummyRepr(Repr):
+        lowleveltype = lltype.Signed
+        
+        def convert_const(self, value):
+            return 42
+    
+    def get_repr(rtyper, s_instance):
+        return DummyRepr()
+    
+    register_type(DummyClass, compute_annotation=get_annotation,
+        get_repr=get_repr)
+    
+    dummy_class = DummyClass()
+    
+    def func():
+        return dummy_class
+    
+    res = interpret(func, [])
+    
+    assert res == 42
+    
\ No newline at end of file



More information about the Pypy-commit mailing list