[pypy-svn] r25806 - in pypy/dist/pypy/rpython/rctypes: . test
arigo at codespeak.net
arigo at codespeak.net
Thu Apr 13 17:59:23 CEST 2006
Author: arigo
Date: Thu Apr 13 17:59:22 2006
New Revision: 25806
Modified:
pypy/dist/pypy/rpython/rctypes/rstruct.py
pypy/dist/pypy/rpython/rctypes/test/test_rstruct.py
Log:
Prebuilt structs.
Modified: pypy/dist/pypy/rpython/rctypes/rstruct.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/rstruct.py (original)
+++ pypy/dist/pypy/rpython/rctypes/rstruct.py Thu Apr 13 17:59:22 2006
@@ -5,7 +5,7 @@
from pypy.rpython.rbuiltin import gen_cast_structfield_pointer
from pypy.rpython.lltypesystem import lltype
from pypy.rpython.rctypes.rmodel import CTypesRefRepr, CTypesValueRepr
-from pypy.rpython.rctypes.rmodel import genreccopy
+from pypy.rpython.rctypes.rmodel import genreccopy, reccopy
from pypy.rpython.rctypes.rprimitive import PrimitiveRepr
StructType = type(Structure)
@@ -28,6 +28,16 @@
super(StructRepr, self).__init__(rtyper, s_struct, c_data_type)
+ def initialize_const(self, p, value):
+ for name, r_field in self.r_fields.items():
+ llitem = r_field.convert_const(getattr(value, name))
+ if isinstance(r_field, CTypesRefRepr):
+ # ByRef case
+ reccopy(llitem.c_data, getattr(p.c_data, name))
+ else:
+ # ByValue case
+ setattr(p.c_data, name, llitem.c_data[0])
+
def get_c_data_of_field(self, llops, v_struct, fieldname):
v_c_struct = self.get_c_data(llops, v_struct)
r_field = self.r_fields[fieldname]
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 Thu Apr 13 17:59:22 2006
@@ -89,3 +89,45 @@
res = interpret(access_struct, [44])
assert res == 90
+
+ def test_specialize_prebuilt(self):
+ my_struct_2 = tagpoint(5, 7)
+ my_struct_3 = tagpoint(x=6, y=11)
+ def func(i):
+ if i == 2:
+ struct = my_struct_2
+ else:
+ struct = my_struct_3
+ return struct.y
+
+ res = interpret(func, [2])
+ assert res == 7
+ res = interpret(func, [3])
+ assert res == 11
+
+class Test_compilation:
+ def test_compile_struct_access(self):
+ def access_struct(n):
+ my_struct = tagpoint()
+ my_struct.x = c_int(1)
+ my_struct.y = 2
+ my_struct.x += n
+
+ return my_struct.x * my_struct.y
+
+ fn = compile(access_struct, [int])
+ assert fn(44) == 90
+
+ def test_compile_prebuilt(self):
+ my_struct_2 = tagpoint(5, 7)
+ my_struct_3 = tagpoint(x=6, y=11)
+ def func(i):
+ if i == 2:
+ struct = my_struct_2
+ else:
+ struct = my_struct_3
+ return struct.y
+
+ fn = compile(func, [int])
+ assert fn(2) == 7
+ assert fn(3) == 11
More information about the Pypy-commit
mailing list