[pypy-svn] r36057 - in pypy/dist/pypy/rlib/rctypes: . test

arigo at codespeak.net arigo at codespeak.net
Sat Dec 30 11:46:15 CET 2006


Author: arigo
Date: Sat Dec 30 11:46:12 2006
New Revision: 36057

Modified:
   pypy/dist/pypy/rlib/rctypes/rctypesobject.py
   pypy/dist/pypy/rlib/rctypes/test/test_rctypesobject.py
Log:
Fix pointer indexing for pointers to structures.
Add a direct way to support pointer indexing: get_contents_at_index().
Some more tests.


Modified: pypy/dist/pypy/rlib/rctypes/rctypesobject.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/rctypesobject.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/rctypesobject.py	Sat Dec 30 11:46:12 2006
@@ -249,6 +249,15 @@
                 targetmemblock = self._getmemblock(0, targetkeepalives)
                 return contentscls(targetaddr, targetmemblock)
 
+            def get_contents_at_index(self, index):
+                ptr = self.ll_ref(RCTypesPtr.CDATATYPE)
+                targetaddr = llmemory.cast_ptr_to_adr(ptr[0])
+                targetaddr += ofs_item * index
+                keepalive_until_here(self)
+                targetkeepalives = contentscls.num_keepalives
+                targetmemblock = self._getmemblock(0, targetkeepalives)
+                return contentscls(targetaddr, targetmemblock)
+
             def set_contents(self, newcontentsbox):
                 targetaddr = newcontentsbox.addr
                 targetmemblock = newcontentsbox.memblock
@@ -258,6 +267,7 @@
                 keepalive_until_here(self)
                 self._keepalivememblock(0, targetmemblock)
 
+        ofs_item = llmemory.sizeof(contentscls.LLTYPE)
         contentscls._ptrcls = RCTypesPtr
         return RCTypesPtr
 RPointer._annspecialcase_ = 'specialize:memo'

Modified: pypy/dist/pypy/rlib/rctypes/test/test_rctypesobject.py
==============================================================================
--- pypy/dist/pypy/rlib/rctypes/test/test_rctypesobject.py	(original)
+++ pypy/dist/pypy/rlib/rctypes/test/test_rctypesobject.py	Sat Dec 30 11:46:12 2006
@@ -66,6 +66,18 @@
         res = self.do(func)
         assert res == 303
 
+    def test_copyfrom_2(self):
+        def func():
+            x1 = rc_int.allocate()
+            x1.set_value(11)
+            x2 = rc_int.allocate()
+            x2.set_value(7)
+            p1 = pointer(x1)
+            p1.get_contents().copyfrom(x2)
+            return x1.get_value()
+        res = self.do(func)
+        assert res == 7
+
     def test_fixedarray(self):
         def func():
             a = RFixedArray(rc_int, 10).allocate()
@@ -99,11 +111,27 @@
             a1 = RVarArray(rc_int).fromitem(p.get_contents(), 8)
             del p
             for i in range(8):
-                a1.ref(i).get_value() == 100 + 5 * i
+                assert a1.ref(i).get_value() == 100 + 5 * i
             return a1.length
         res = self.do(func)
         assert res == 8
 
+    def test_varstructarray_cast(self):
+        S1 = RStruct('S1', [('x', rc_int),
+                            ('y', rc_int)])
+        def func():
+            a = RVarArray(S1).allocate(10)
+            for i in range(10):
+                a.ref(i).ref_x().set_value(100 + 5 * i)
+                a.ref(i).ref_y().set_value(200 + 2 * i)
+            p = pointer(a.ref(0))
+            del a
+            a1 = RVarArray(S1).fromitem(p.get_contents(), 8)
+            del p
+            return a1.ref(4).ref_y().get_value()
+        res = self.do(func)
+        assert res == 208
+
     def test_char_p(self):
         def func():
             p = rc_char_p.allocate()
@@ -186,6 +214,31 @@
         res = self.do(func)
         assert res == 7
 
+    def test_pointer_indexing(self):
+        def func():
+            a = RFixedArray(rc_int, 10).allocate()
+            for i in range(10):
+                a.ref(i).set_value(100 + 5 * i)
+            p = pointer(a.ref(0))
+            del a
+            return p.get_contents_at_index(7).get_value()
+        res = self.do(func)
+        assert res == 135
+
+    def test_structpointer_indexing(self):
+        S1 = RStruct('S1', [('x', rc_int),
+                            ('y', rc_int)])
+        def func():
+            a = RFixedArray(S1, 10).allocate()
+            for i in range(10):
+                a.ref(i).ref_x().set_value(100 + 5 * i)
+                a.ref(i).ref_y().set_value(200 + 2 * i)
+            p = pointer(a.ref(0))
+            del a
+            s1 = p.get_contents_at_index(3)
+            return s1.ref_x().get_value() + s1.ref_y().get_value()
+        res = self.do(func)
+        assert res == 115 + 206
 
 POLICY = AnnotatorPolicy()
 POLICY.allow_someobjects = False



More information about the Pypy-commit mailing list