Author: Antonio Cuni <[email protected]>
Branch: ffistruct
Changeset: r49005:25ce0a707991
Date: 2011-11-09 12:40 +0100
http://bitbucket.org/pypy/pypy/changeset/25ce0a707991/
Log: add support and tests for unsigned types
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
@@ -2,6 +2,7 @@
from pypy.rlib import clibffi
from pypy.rlib import libffi
from pypy.rlib import jit
+from pypy.rlib.rarithmetic import r_uint
from pypy.interpreter.baseobjspace import Wrappable
from pypy.interpreter.typedef import TypeDef, interp_attrproperty
from pypy.interpreter.gateway import interp2app, unwrap_spec
@@ -130,6 +131,8 @@
def getfield(self, space, name):
w_ffitype, offset =
self.structdescr.get_type_and_offset_for_field(name)
value = libffi.struct_getfield_int(w_ffitype.ffitype, self.rawmem,
offset)
+ if w_ffitype.is_unsigned():
+ return space.wrap(r_uint(value))
return space.wrap(value)
@unwrap_spec(name=str)
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
@@ -153,7 +153,30 @@
assert struct.getfield('slong') == -sys.maxint-1
struct.setfield('slong', sys.maxint*3)
assert struct.getfield('slong') == sys.maxint-2
-
+
+ def test_getfield_setfield_unsigned_types(self):
+ import sys
+ from _ffi import _StructDescr, Field, types
+ longsize = types.slong.sizeof()
+ fields = [
+ Field('ubyte', types.ubyte),
+ Field('ushort', types.ushort),
+ Field('uint', types.uint),
+ Field('ulong', types.ulong),
+ ]
+ descr = _StructDescr('foo', fields)
+ struct = descr.allocate()
+ struct.setfield('ubyte', -1)
+ assert struct.getfield('ubyte') == 255
+ struct.setfield('ushort', -1)
+ assert struct.getfield('ushort') == 65535
+ struct.setfield('uint', 43)
+ assert struct.getfield('uint') == 43
+ struct.setfield('ulong', -1)
+ assert struct.getfield('ulong') == sys.maxint*2 + 1
+ struct.setfield('ulong', sys.maxint*2 + 2)
+ assert struct.getfield('ulong') == 0
+
def test_compute_shape(self):
from _ffi import Structure, Field, types
class Point(Structure):
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit