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

arigo at codespeak.net arigo at codespeak.net
Mon Apr 17 14:39:59 CEST 2006


Author: arigo
Date: Mon Apr 17 14:39:58 2006
New Revision: 25889

Modified:
   pypy/dist/pypy/rpython/rctypes/rstruct.py
   pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py
Log:
Same bug as in r25888 for structs.


Modified: pypy/dist/pypy/rpython/rctypes/rstruct.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rstruct.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/rstruct.py	Mon Apr 17 14:39:58 2006
@@ -75,14 +75,17 @@
         name = s_attr.const
         r_field = self.r_fields[name]
         v_struct, v_attr = hop.inputargs(self, lltype.Void)
-        if isinstance(r_field, CTypesRefRepr):
+        if isinstance(r_field, PrimitiveRepr):
+            # primitive case (optimization; the below also works in this case)
+            # NB. this optimization is invalid for PointerReprs!  See for
+            # example:  s.p.contents = ...  to change the pointer field 'p'
+            # of 's'.
+            v_value = self.get_field_value(hop.llops, v_struct, name)
+            return r_field.return_value(hop.llops, v_value)
+        else:
             # ByRef case
             v_c_data = self.get_c_data_of_field(hop.llops, v_struct, name)
             return r_field.return_c_data(hop.llops, v_c_data)
-        else:
-            # ByValue case (optimization; the above also works in this case)
-            v_value = self.get_field_value(hop.llops, v_struct, name)
-            return r_field.return_value(hop.llops, v_value)
 
     def rtype_setattr(self, hop):
         s_attr = hop.args_s[1]

Modified: pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py	Mon Apr 17 14:39:58 2006
@@ -138,6 +138,22 @@
         res = interpret(func, [])
         assert res == expected
 
+    def test_struct_of_pointers(self):
+        class S(Structure):
+            _fields_ = [('x', c_int)]
+        class T(Structure):
+            _fields_ = [('p', POINTER(S))]
+        def func():
+            t1 = T()
+            t2 = T()
+            s = S()
+            s.x = 11
+            t1.p = pointer(s)
+            t2.p.contents = s
+            return t1.p.contents.x * t2.p.contents.x
+        res = interpret(func, [])
+        assert res == 121
+
 class Test_compilation:
     def test_compile_struct_access(self):
         def access_struct(n):



More information about the Pypy-commit mailing list