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

arigo at codespeak.net arigo at codespeak.net
Wed Apr 12 21:57:35 CEST 2006


Author: arigo
Date: Wed Apr 12 21:57:34 2006
New Revision: 25740

Modified:
   pypy/dist/pypy/rpython/rctypes/rarray.py
   pypy/dist/pypy/rpython/rctypes/rchar_p.py
   pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
   pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py
Log:
Yay!  Progress on rctypes.

* don't use addresses directly any more; CCHARP is now a
    Ptr(FixedSizeArray(Char, 1)).

* casting from an array of chars to a CCHARP is based on the new
    cast_subarray_pointer().

* rarray can be done more naturally as an array of ll_typed items
    instead of an array of c_data_type boxes.




Modified: pypy/dist/pypy/rpython/rctypes/rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rarray.py	Wed Apr 12 21:57:34 2006
@@ -1,10 +1,12 @@
 from ctypes import ARRAY, c_int
 from pypy.annotation.model import SomeCTypesObject, SomeBuiltin
 from pypy.rpython import extregistry
+from pypy.rpython.rbuiltin import gen_cast_array_pointer
 from pypy.rpython.rmodel import Repr, IntegerRepr, inputconst
 from pypy.rpython.lltypesystem import lltype
 from pypy.annotation.pairtype import pairtype
-from pypy.rpython.rctypes.rmodel import CTypesRefRepr, genreccopy, reccopy
+from pypy.rpython.rctypes.rmodel import CTypesRefRepr, CTypesValueRepr
+from pypy.rpython.rctypes.rmodel import genreccopy, reccopy
 
 ArrayType = type(ARRAY(c_int, 10))
 
@@ -20,7 +22,7 @@
                                             SomeCTypesObject.MEMORYALIAS))
 
         # Here, self.c_data_type == self.ll_type
-        c_data_type = lltype.FixedSizeArray(self.r_item.c_data_type,
+        c_data_type = lltype.FixedSizeArray(self.r_item.ll_type,
                                             self.length)
 
         super(ArrayRepr, self).__init__(rtyper, s_array, c_data_type)
@@ -28,12 +30,38 @@
     def initialize_const(self, p, value):
         for i in range(self.length):
             llitem = self.r_item.convert_const(value[i])
-            reccopy(llitem.c_data, p.c_data[i])
+            if isinstance(self.r_item, CTypesRefRepr):
+                # ByRef case
+                reccopy(llitem.c_data, p.c_data[i])
+            else:
+                # ByValue case
+                p.c_data[i] = llitem.c_data[0]
 
     def get_c_data_of_item(self, llops, v_array, v_index):
         v_c_array = self.get_c_data(llops, v_array)
-        return llops.genop('getarraysubstruct', [v_c_array, v_index],
-                           lltype.Ptr(self.r_item.c_data_type))
+        if isinstance(self.r_item, CTypesRefRepr):
+            # ByRef case
+            return llops.genop('getarraysubstruct', [v_c_array, v_index],
+                               lltype.Ptr(self.r_item.c_data_type))
+        else:
+            # ByValue case
+            A = lltype.FixedSizeArray(self.r_item.ll_type, 1)
+            return gen_cast_array_pointer(llops, lltype.Ptr(A),
+                                          v_c_array, v_index)
+
+    def get_item_value(self, llops, v_array, v_index):
+        # ByValue case only
+        assert isinstance(self.r_item, CTypesValueRepr)
+        v_c_array = self.get_c_data(llops, v_array)
+        return llops.genop('getarrayitem', [v_c_array, v_index],
+                           resulttype = self.r_item.ll_type)
+
+    def set_item_value(self, llops, v_array, v_index, v_newvalue):
+        # ByValue case only
+        assert isinstance(self.r_item, CTypesValueRepr)
+        v_c_array = self.get_c_data(llops, v_array)
+        llops.genop('setarrayitem', [v_c_array, v_index, v_newvalue])
+
 
 class __extend__(pairtype(ArrayRepr, IntegerRepr)):
     def rtype_setitem((r_array, r_int), hop):

Modified: pypy/dist/pypy/rpython/rctypes/rchar_p.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rchar_p.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rchar_p.py	Wed Apr 12 21:57:34 2006
@@ -1,8 +1,8 @@
 from pypy.rpython import extregistry
 from pypy.rpython.rmodel import inputconst
-from pypy.rpython.lltypesystem import lltype, llmemory
+from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.rstr import StringRepr, string_repr
-from pypy.rpython.rctypes.rmodel import CTypesValueRepr
+from pypy.rpython.rctypes.rmodel import CTypesValueRepr, C_ZERO
 from pypy.rpython.rctypes.rarray import ArrayRepr
 from pypy.annotation import model as annmodel
 from pypy.annotation.pairtype import pairtype
@@ -53,43 +53,35 @@
         r_temp.setstring(llops, v_owned_box, v)
         return llops.convertvar(v_owned_box, r_temp, r_to)
 
-##class __extend__(pairtype(ArrayRepr, CCharPRepr)):
-##    def convert_from_to((r_from, r_to), v, llops):
-##        if r_from.r_item.ctype != c_char:
-##            return NotImplemented
-##        # warning: no keepalives, only for short-lived conversions like
-##        # in argument passing
-##        r_temp = r_to.r_memoryowner
-##        v_owned_box = r_temp.allocate_instance(llops)
-##        v_char_array_ptr = r_from.get_c_data(llops, v)
-##        v_char_array_adr = llops.genop('cast_ptr_to_adr', [v_char_array_ptr],
-##                                       resulttype = llmemory.Address)
-##        cofs = inputconst(lltype.Signed,
-##                          llmemory.itemoffsetof(r_from.ll_type) +
-##                          llmemory.offsetof(r_from.ll_type.OF, 'value'))
-##        v_first_char_adr = llops.genop('adr_add', [v_char_array_adr, cofs],
-##                                       resulttype = llmemory.Address)
-##        r_temp.setvalue(llops, v_owned_box, v_first_char_adr)
-##        return llops.convertvar(v_owned_box, r_temp, r_to)
+class __extend__(pairtype(ArrayRepr, CCharPRepr)):
+    def convert_from_to((r_from, r_to), v, llops):
+        if r_from.r_item.ctype != c_char:
+            return NotImplemented
+        # warning: no keepalives, only for short-lived conversions like
+        # in argument passing
+        r_temp = r_to.r_memoryowner
+        v_owned_box = r_temp.allocate_instance(llops)
+        v_c_array = r_from.get_c_data_of_item(llops, v, C_ZERO)
+        r_temp.setvalue(llops, v_owned_box, v_c_array)
+        return llops.convertvar(v_owned_box, r_temp, r_to)
 
 
-CCHARP = llmemory.Address    # char *
-FIRSTITEMOFS = llmemory.ArrayItemsOffset(string_repr.lowleveltype.TO.chars)
+CCHARP = lltype.Ptr(lltype.FixedSizeArray(lltype.Char, 1))
 
 def ll_strlen(p):
     i = 0
-    while ord(p.char[i]) != 0:
+    while ord(p[i]) != 0:
         i += 1
     return i
 
 def ll_strnlen(p, maxlen):
     i = 0
-    while i < maxlen and ord(p.char[i]) != 0:
+    while i < maxlen and ord(p[i]) != 0:
         i += 1
     return i
 
 def ll_str2charp(s):
-    return llmemory.cast_ptr_to_adr(s.chars) + FIRSTITEMOFS
+    return lltype.cast_subarray_pointer(CCHARP, s.chars, 0)
 
 def ll_getstring(box):
     p = box.c_data[0]
@@ -104,7 +96,7 @@
             length = ll_strlen(p)
         newstr = lltype.malloc(string_repr.lowleveltype.TO, length)
         for i in range(length):
-            newstr.chars[i] = p.char[i]
+            newstr.chars[i] = p[i]
         return newstr
     else:
         return lltype.nullptr(string_repr.lowleveltype.TO)
@@ -113,7 +105,7 @@
     if string:
         box.c_data[0] = ll_str2charp(string)
     else:
-        box.c_data[0] = llmemory.NULL
+        box.c_data[0] = lltype.nullptr(CCHARP.TO)
     box.keepalive_str = string
 
 

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	Wed Apr 12 21:57:34 2006
@@ -124,8 +124,8 @@
 
         res = interpret(create_array, [])
         c_data = res.c_data
-        assert c_data[0][0] == 0
-        assert c_data[9][0] == 0
+        assert c_data[0] == 0
+        assert c_data[9] == 0
         py.test.raises(IndexError, "c_data[10]")
         assert len(c_data) == 10
 

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rfunc.py	Wed Apr 12 21:57:34 2006
@@ -5,7 +5,7 @@
 from pypy.translator.c.test.test_genc import compile
 from pypy import conftest
 from pypy.rpython.rstr import string_repr
-from pypy.rpython.lltypesystem import lltype, llmemory
+from pypy.rpython.lltypesystem import lltype
 
 from ctypes import c_int, c_long, c_char_p, c_char
 
@@ -20,11 +20,11 @@
 atoi = mylib.atoi
 atoi.restype = c_int
 atoi.argtypes = [c_char_p]
-def ll_atoi(s_addr):
+def ll_atoi(p):
     "Very approximative ll implementation of atoi(), for testing"
     i = result = 0
-    while '0' <= s_addr.char[i] <= '9':
-        result = result * 10 + ord(s_addr.char[i]) - ord('0')
+    while '0' <= p[i] <= '9':
+        result = result * 10 + ord(p[i]) - ord('0')
         i += 1
     return result
 atoi.llinterp_friendly_version = ll_atoi
@@ -49,15 +49,15 @@
 
 def test_ll_atoi():
     keepalive = []
-    def str2addr(string):
+    def str2subarray(string):
         llstring = string_repr.convert_const(string)
         keepalive.append(llstring)
-        return (llmemory.cast_ptr_to_adr(llstring.chars) +
-                llmemory.ArrayItemsOffset(lltype.typeOf(llstring.chars).TO))
-    assert ll_atoi(str2addr("")) == 0
-    assert ll_atoi(str2addr("42z7")) == 42
-    assert ll_atoi(str2addr("blah")) == 0
-    assert ll_atoi(str2addr("18238")) == 18238
+        A = lltype.FixedSizeArray(lltype.Char, 1)
+        return lltype.cast_subarray_pointer(lltype.Ptr(A), llstring.chars, 0)
+    assert ll_atoi(str2subarray("")) == 0
+    assert ll_atoi(str2subarray("42z7")) == 42
+    assert ll_atoi(str2subarray("blah")) == 0
+    assert ll_atoi(str2subarray("18238")) == 18238
 
 class Test_annotation:
     def test_annotate_labs(self):
@@ -90,7 +90,6 @@
         assert res == [0, 42, 0, 18238]
 
     def test_specialize_atoi_char_array(self):
-        py.test.skip("in-progress")
         A = c_char * 10
         choices = [A('\x00'),
                    A('4', '2', 'z', '7', '\x00'),



More information about the Pypy-commit mailing list