[pypy-svn] r24281 - in pypy/dist/pypy: rpython/rctypes rpython/rctypes/test translator/c

goden at codespeak.net goden at codespeak.net
Mon Mar 13 06:42:05 CET 2006


Author: goden
Date: Mon Mar 13 06:41:58 2006
New Revision: 24281

Modified:
   pypy/dist/pypy/rpython/rctypes/rarray.py
   pypy/dist/pypy/rpython/rctypes/rprimitive.py
   pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
   pypy/dist/pypy/translator/c/funcgen.py
Log:
- rctypes: added a conversion from PrimitiveRepr to IntegerRepr to handle the
  implicit conversion that occurs when assigning ctypes' array items with
  ctypes' primitives.  fixed the rarray low-level representation, getitem,
  and setitem for use with boxed ctypes primitives. re-enabled two fixed
  test_rarray tests and added another to test compiling.  added a check in
  pypy.translator.c.funcgen.OP_MALLOC_VARSIZE() to check the 'nolength'
  hint and avoid setting the length of the rctypes translated arrays when
  malloc'd.



Modified: pypy/dist/pypy/rpython/rctypes/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rarray.py	Mon Mar 13 06:41:58 2006
@@ -15,11 +15,18 @@
         self.length = type._length_
         
         entry = extregistry.lookup_type(item_ctype)
-        self.r_item = entry.get_repr(rtyper, item_ctype)
+        # XXX: goden: So at this point item_ctype is a ctypes object.
+        #             I *think* we need to send a "SomeCTypesObject" box
+        #             to get the PrimitiveRepr instead of the ctypes object
+        #             itself.
+        self.r_item = entry.get_repr(rtyper, SomeCTypesObject(item_ctype,
+            SomeCTypesObject.OWNSMEMORY))
         
+        # Array elements are of the low-level type (Signed, etc) and not 
+        # of the boxed low level type (Ptr(GcStruct(...)))
         self.lowleveltype = lltype.Ptr(
             lltype.GcStruct( "CtypesGcArray_%s" % type.__name__,
-                ( "c_data", lltype.Array(self.r_item.lowleveltype, 
+                ( "c_data", lltype.Array(self.r_item.ll_type, 
                     hints={"nolength": True})
                 )
             )
@@ -28,7 +35,7 @@
 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)
+                r_array.r_item.ll_type)
         inputargs = [v_array, hop.inputconst(lltype.Void, "c_data")]
         v_c_data = hop.genop('getsubstruct',
                     inputargs,
@@ -43,7 +50,7 @@
                     inputargs,
                     lltype.Ptr(r_array.lowleveltype.TO.c_data) )
         return hop.genop('getarrayitem', [v_c_data, v_index],
-                r_array.r_item.lowleveltype)
+                r_array.r_item.ll_type)
 
 def arraytype_specialize_call(hop):
     r_array = hop.r_result

Modified: pypy/dist/pypy/rpython/rctypes/rprimitive.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rprimitive.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rprimitive.py	Mon Mar 13 06:41:58 2006
@@ -82,6 +82,15 @@
                                                         self.ll_type)
         self.setfield(hop.llops, v_primitive, v_value)
 
+# need to extend primitive repr to implement convert_from_to() for various
+# conversions, firstly the conversion from c_long() to Signed
+
+class __extend__(pairtype(PrimitiveRepr, IntegerRepr)):
+    def convert_from_to((r_from, r_to), v, llops):
+        assert r_from.ll_type == r_to.lowleveltype
+
+        return r_from.getfield(llops, v)
+
 def primitive_specialize_call(hop):
     r_primitive = hop.r_result
     c1 = hop.inputconst(lltype.Void, r_primitive.lowleveltype.TO) 

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	Mon Mar 13 06:41:58 2006
@@ -6,6 +6,7 @@
 from pypy.annotation.annrpython import RPythonAnnotator
 from pypy.translator.translator import TranslationContext
 from pypy import conftest
+from pypy.translator.c.test.test_genc import compile
 import sys
 from pypy.rpython.test.test_llinterp import interpret
 
@@ -127,7 +128,7 @@
         py.test.raises(IndexError, "s = a.build_types(access_with_invalid_negative_index,[])")
 
 class Test_specialization:
-    def x_test_specialize_array(self):
+    def test_specialize_array(self):
         def create_array():
             return c_int_10()
 
@@ -138,12 +139,26 @@
         py.test.raises(IndexError, "c_data[10]")
         py.test.raises(TypeError, "len(c_data)")
 
-    def x_test_specialize_array_access(self):
+    def test_specialize_array_access(self):
         def access_array():
             my_array = c_int_10()
             my_array[0] = 1
+            my_array[1] = c_int(1)
 
             return my_array[0]
 
         res = interpret(access_array, [])
         assert res == 1
+
+class Test_compilation:
+    def test_compile_array_access(self):
+        def access_array():
+            my_array = c_int_10()
+            my_array[0] = 1
+            my_array[1] = c_int(2)
+
+            return my_array[1]
+
+        fn = compile(access_array, [])
+        
+        assert fn() == 2

Modified: pypy/dist/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/dist/pypy/translator/c/funcgen.py	(original)
+++ pypy/dist/pypy/translator/c/funcgen.py	Mon Mar 13 06:41:58 2006
@@ -563,7 +563,10 @@
                 elength,
                 itemtype)
         result += self.gcpolicy.zero_malloc(TYPE, esize, eresult, err)
-        result += '\n%s->%s = %s;' % (eresult, lenfld, elength)
+
+        # ctypes Arrays have no length field
+        if not VARPART._hints.get('nolength', False):
+            result += '\n%s->%s = %s;' % (eresult, lenfld, elength)
         return result
 
     def OP_FLAVORED_MALLOC(self, op, err):



More information about the Pypy-commit mailing list