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

goden at codespeak.net goden at codespeak.net
Wed Mar 1 18:44:04 CET 2006


Author: goden
Date: Wed Mar  1 18:43:56 2006
New Revision: 23852

Modified:
   pypy/dist/pypy/rpython/rctypes/rarray.py
   pypy/dist/pypy/rpython/rctypes/test/test_rctypes.py
Log:
(arigo, goden) - added specialization of ctypes array access



Modified: pypy/dist/pypy/rpython/rctypes/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rarray.py	Wed Mar  1 18:43:56 2006
@@ -3,6 +3,8 @@
 from pypy.rpython import extregistry
 from pypy.rpython.rmodel import Repr
 from pypy.rpython.lltypesystem import lltype
+from pypy.annotation.pairtype import pairtype
+from pypy.rpython.rmodel import IntegerRepr
 
 ArrayType = type(ARRAY(c_int, 10))
 
@@ -20,16 +22,36 @@
         self.length = type._length_
         
         entry = extregistry.lookup_type(item_ctype)
-        r_item = entry.get_repr(rtyper, item_ctype)
+        self.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, 
+                ( "c_data", lltype.Array(self.r_item.lowleveltype, 
                     hints={"nolength": True})
                 )
             )
         )
 
+class __extend__(pairtype(ArrayRepr, IntegerRepr)):
+    def rtype_setitem((r_array, r_int), hop):
+        v_array, v_index, v_item = hop.inputargs(r_array, lltype.Signed,
+                r_array.r_item)
+        inputargs = [v_array, hop.inputconst(lltype.Void, "c_data")]
+        v_c_data = hop.genop('getsubstruct',
+                    inputargs,
+                    lltype.Ptr(r_array.lowleveltype.TO.c_data) )
+        hop.genop('setarrayitem', [v_c_data, v_index, v_item])
+
+    def rtype_getitem((r_array, r_int), hop):
+        v_array, v_index = hop.inputargs(r_array, lltype.Signed)
+
+        inputargs = [v_array, hop.inputconst(lltype.Void, "c_data")]
+        v_c_data = hop.genop('getsubstruct',
+                    inputargs,
+                    lltype.Ptr(r_array.lowleveltype.TO.c_data) )
+        return hop.genop('getarrayitem', [v_c_data, v_index],
+                r_array.r_item.lowleveltype)
+
 def arraytype_specialize_call(hop):
     r_array = hop.r_result
     return hop.genop("malloc_varsize", [

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 18:43:56 2006
@@ -537,3 +537,13 @@
         assert c_data[9] == 0
         py.test.raises(IndexError, "c_data[10]")
         py.test.raises(TypeError, "len(c_data)")
+
+    def test_specialize_array_access(self):
+        def test_specialize_array_access():
+            my_array = c_int_10()
+            my_array[0] = 1
+
+            return my_array[0]
+
+        res = interpret(test_specialize_array_access, [])
+        assert res == 1



More information about the Pypy-commit mailing list