[pypy-svn] r50623 - in pypy/branch/applevel-ctypes2/pypy/module/_rawffi: . test

arigo at codespeak.net arigo at codespeak.net
Tue Jan 15 12:51:58 CET 2008


Author: arigo
Date: Tue Jan 15 12:51:57 2008
New Revision: 50623

Modified:
   pypy/branch/applevel-ctypes2/pypy/module/_rawffi/array.py
   pypy/branch/applevel-ctypes2/pypy/module/_rawffi/structure.py
   pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test_nested.py
Log:
(fijal, cfbolz, arigo)
Arrays of structures.


Modified: pypy/branch/applevel-ctypes2/pypy/module/_rawffi/array.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_rawffi/array.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_rawffi/array.py	Tue Jan 15 12:51:57 2008
@@ -111,13 +111,25 @@
                           self.shape.itemtp)
     getitem.unwrap_spec = ['self', ObjSpace, int]
 
+    def getlength(self, space):
+        return space.wrap(self.length)
+    getlength.unwrap_spec = ['self', ObjSpace]
+
+    def descr_itemaddress(self, space, num):
+        _, itemsize, _ = self.shape.itemtp
+        ptr = rffi.ptradd(self.ll_buffer, itemsize * num)
+        return space.wrap(rffi.cast(lltype.Signed, ptr))
+    descr_itemaddress.unwrap_spec = ['self', ObjSpace, int]
+
 W_ArrayInstance.typedef = TypeDef(
     'ArrayInstance',
     __setitem__ = interp2app(W_ArrayInstance.setitem),
     __getitem__ = interp2app(W_ArrayInstance.getitem),
+    __len__     = interp2app(W_ArrayInstance.getlength),
     buffer      = GetSetProperty(W_ArrayInstance.getbuffer),
     shape       = interp_attrproperty('shape', W_ArrayInstance),
     free        = interp2app(W_ArrayInstance.free),
     byptr       = interp2app(W_ArrayInstance.byptr),
+    itemaddress = interp2app(W_ArrayInstance.descr_itemaddress),
 )
 W_ArrayInstance.typedef.acceptable_as_base_class = False

Modified: pypy/branch/applevel-ctypes2/pypy/module/_rawffi/structure.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_rawffi/structure.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_rawffi/structure.py	Tue Jan 15 12:51:57 2008
@@ -82,10 +82,10 @@
         return space.wrap(W_StructureInstance(space, self, address, None))
     fromaddress.unwrap_spec = ['self', ObjSpace, int]
 
-    def descr_getfieldoffset(self, space, attr):
+    def descr_fieldoffset(self, space, attr):
         index = self.getindex(space, attr)
         return space.wrap(self.ll_positions[index])
-    descr_getfieldoffset.unwrap_spec = ['self', ObjSpace, str]
+    descr_fieldoffset.unwrap_spec = ['self', ObjSpace, str]
 
     def descr_gettypecode(self, space):
         return space.newtuple([space.wrap(self.size),
@@ -98,11 +98,11 @@
 W_Structure.typedef = TypeDef(
     'Structure',
     __new__     = interp2app(descr_new_structure),
-    __call__ = interp2app(W_Structure.descr_call),
+    __call__    = interp2app(W_Structure.descr_call),
     fromaddress = interp2app(W_Structure.fromaddress),
     size        = interp_attrproperty('size', W_Structure),
     alignment   = interp_attrproperty('alignment', W_Structure),
-    getfieldoffset = interp2app(W_Structure.descr_getfieldoffset),
+    fieldoffset = interp2app(W_Structure.descr_fieldoffset),
     gettypecode = interp2app(W_Structure.descr_gettypecode),
 )
 W_Structure.typedef.acceptable_as_base_class = False

Modified: pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test_nested.py
==============================================================================
--- pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test_nested.py	(original)
+++ pypy/branch/applevel-ctypes2/pypy/module/_rawffi/test/test_nested.py	Tue Jan 15 12:51:57 2008
@@ -20,9 +20,9 @@
         S = _rawffi.Structure([('a', 'i'), ('b', 'P'), ('c', 'c')])
         assert S.size == round_up(struct.calcsize("iPc"))
         assert S.alignment == align
-        assert S.getfieldoffset('a') == 0
-        assert S.getfieldoffset('b') == align
-        assert S.getfieldoffset('c') == round_up(struct.calcsize("iP"))
+        assert S.fieldoffset('a') == 0
+        assert S.fieldoffset('b') == align
+        assert S.fieldoffset('c') == round_up(struct.calcsize("iP"))
         assert S.gettypecode() == (S.size, S.alignment)
 
     def test_nested_structures(self):
@@ -31,15 +31,35 @@
         S = _rawffi.Structure([('x', 'c'), ('s1', S1.gettypecode())])
         assert S.size == S1.alignment + S1.size
         assert S.alignment == S1.alignment
-        assert S.getfieldoffset('x') == 0
-        assert S.getfieldoffset('s1') == S1.alignment
+        assert S.fieldoffset('x') == 0
+        assert S.fieldoffset('s1') == S1.alignment
         s = S()
         s.x = 'G'
         raises(TypeError, 's.s1')
-        assert s.fieldaddress('s1') == s.buffer + S.getfieldoffset('s1')
+        assert s.fieldaddress('s1') == s.buffer + S.fieldoffset('s1')
         s1 = S1.fromaddress(s.fieldaddress('s1'))
         s1.c = 'H'
         rawbuf = _rawffi.Array('c').fromaddress(s.buffer, S.size)
         assert rawbuf[0] == 'G'
-        assert rawbuf[S1.alignment + S1.getfieldoffset('c')] == 'H'
+        assert rawbuf[S1.alignment + S1.fieldoffset('c')] == 'H'
         s.free()
+
+    def test_array_of_structures(self):
+        import _rawffi
+        S = _rawffi.Structure([('a', 'i'), ('b', 'P'), ('c', 'c')])
+        A = _rawffi.Array(S.gettypecode())
+        a = A(3)
+        raises(TypeError, "a[0]")
+        s0 = S.fromaddress(a.buffer)
+        s0.c = 'B'
+        assert a.itemaddress(1) == a.buffer + S.size
+        s1 = S.fromaddress(a.itemaddress(1))
+        s1.c = 'A'
+        s2 = S.fromaddress(a.itemaddress(2))
+        s2.c = 'Z'
+        rawbuf = _rawffi.Array('c').fromaddress(a.buffer, S.size * len(a))
+        ofs = S.fieldoffset('c')
+        assert rawbuf[0*S.size+ofs] == 'B'
+        assert rawbuf[1*S.size+ofs] == 'A'
+        assert rawbuf[2*S.size+ofs] == 'Z'
+        a.free()



More information about the Pypy-commit mailing list