[pypy-commit] pypy ffistruct: (antocuni, arigo around): correctly truncate all the values to a Signed"

antocuni noreply at buildbot.pypy.org
Wed Nov 9 13:51:15 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: ffistruct
Changeset: r49003:3d6add2cfe84
Date: 2011-11-09 12:14 +0100
http://bitbucket.org/pypy/pypy/changeset/3d6add2cfe84/

Log:	(antocuni, arigo around): correctly truncate all the values to a
	Signed"

diff --git a/pypy/module/_ffi/interp_struct.py b/pypy/module/_ffi/interp_struct.py
--- a/pypy/module/_ffi/interp_struct.py
+++ b/pypy/module/_ffi/interp_struct.py
@@ -137,12 +137,10 @@
         w_ffitype, offset = self.structdescr.get_type_and_offset_for_field(name)
         # XXX: add support for long long
         if w_ffitype.is_signed() or w_ffitype.is_unsigned():
-            value = rffi.cast(rffi.LONG, space.uint_w(w_value))
+            value = space.truncatedint(w_value)
         #
         libffi.struct_setfield_int(w_ffitype.ffitype, self.rawmem, offset, value)
 
-
-
 W__StructInstance.typedef = TypeDef(
     '_StructInstance',
     getaddr  = interp2app(W__StructInstance.getaddr),
diff --git a/pypy/module/_ffi/test/test_struct.py b/pypy/module/_ffi/test/test_struct.py
--- a/pypy/module/_ffi/test/test_struct.py
+++ b/pypy/module/_ffi/test/test_struct.py
@@ -1,8 +1,11 @@
+import sys
+from pypy.conftest import gettestobjspace
 from pypy.module._ffi.test.test_funcptr import BaseAppTestFFI
 from pypy.module._ffi.interp_struct import compute_size_and_alignement, W_Field
 from pypy.module._ffi.interp_ffitype import app_types
 
-class TestComputeSizeAndAlignement(object):
+
+class TestStruct(object):
 
     class FakeSpace(object):
         def interp_w(self, cls, obj):
@@ -35,6 +38,15 @@
         assert self.sizeof([T.slonglong, T.sbyte, T.sbyte, T.sbyte]) == llong_size + llong_align
         assert self.sizeof([T.slonglong, T.sbyte, T.sbyte, T.sbyte, T.sbyte]) == llong_size + llong_align
 
+    def test_truncatedint(self):
+        space = gettestobjspace()
+        assert space.truncatedint(space.wrap(42)) == 42
+        assert space.truncatedint(space.wrap(sys.maxint)) == sys.maxint
+        assert space.truncatedint(space.wrap(sys.maxint+1)) == -sys.maxint-1
+        assert space.truncatedint(space.wrap(-1)) == -1
+        assert space.truncatedint(space.wrap(-sys.maxint-2)) == sys.maxint
+
+
 
 class AppTestStruct(BaseAppTestFFI):
 
@@ -131,12 +143,15 @@
         descr = _StructDescr('foo', fields)
         struct = descr.allocate()
         struct.setfield('sbyte', 128)
+        assert struct.getfield('sbyte') == -128
         struct.setfield('sint', 43)
+        assert struct.getfield('sint') == 43
         struct.setfield('slong', sys.maxint+1)
-        assert struct.getfield('sbyte') == -128
-        assert struct.getfield('sint') == 43
         assert struct.getfield('slong') == -sys.maxint-1
+        struct.setfield('slong', sys.maxint*3)
+        assert struct.getfield('slong') == sys.maxint-2
 
+        
     def test_compute_shape(self):
         from _ffi import Structure, Field, types
         class Point(Structure):


More information about the pypy-commit mailing list