diff options
author | Thomas Heller <theller@ctypes.org> | 2006-03-20 07:54:01 (GMT) |
---|---|---|
committer | Thomas Heller <theller@ctypes.org> | 2006-03-20 07:54:01 (GMT) |
commit | d59ca8f335282ca72896c936d862a9a68633d73f (patch) | |
tree | e3565e8042b2c3bb3a11c06d89580efec8581cfe /Lib | |
parent | 6c2f913805bc0c678c7947502acfea5cb0ae0c3a (diff) | |
download | cpython-d59ca8f335282ca72896c936d862a9a68633d73f.zip cpython-d59ca8f335282ca72896c936d862a9a68633d73f.tar.gz cpython-d59ca8f335282ca72896c936d862a9a68633d73f.tar.bz2 |
Accessing unaligned structure fields works now on all architectures.
Including unittest.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ctypes/test/test_byteswap.py | 81 | ||||
-rw-r--r-- | Lib/ctypes/test/test_unaligned_structures.py | 45 |
2 files changed, 88 insertions, 38 deletions
diff --git a/Lib/ctypes/test/test_byteswap.py b/Lib/ctypes/test/test_byteswap.py index 55a264c..d0ada40 100644 --- a/Lib/ctypes/test/test_byteswap.py +++ b/Lib/ctypes/test/test_byteswap.py @@ -2,7 +2,6 @@ import sys, unittest, struct, math from binascii import hexlify from ctypes import * -from ctypes.test import is_resource_enabled def bin(s): return hexlify(buffer(s)).upper() @@ -222,54 +221,60 @@ class Test(unittest.TestCase): s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14) self.failUnlessEqual(bin(s1), bin(s2)) - if is_resource_enabled("unaligned_access"): - - def test_unaligned_nonnative_struct_fields(self): - if sys.byteorder == "little": - base = BigEndianStructure - fmt = ">b h xi xd" - else: - base = LittleEndianStructure - fmt = "<b h xi xd" + def test_unaligned_nonnative_struct_fields(self): + if sys.byteorder == "little": + base = BigEndianStructure + fmt = ">b h xi xd" + else: + base = LittleEndianStructure + fmt = "<b h xi xd" - class S(base): - _pack_ = 1 - _fields_ = [("b", c_byte), + class S(base): + _pack_ = 1 + _fields_ = [("b", c_byte), - ("h", c_short), + ("h", c_short), - ("_1", c_byte), - ("i", c_int), + ("_1", c_byte), + ("i", c_int), - ("_2", c_byte), - ("d", c_double)] + ("_2", c_byte), + ("d", c_double)] - s1 = S(0x12, 0x1234, 0, 0x12345678, 0, 3.14) - s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14) - self.failUnlessEqual(bin(s1), bin(s2)) + s1 = S() + s1.b = 0x12 + s1.h = 0x1234 + s1.i = 0x12345678 + s1.d = 3.14 + s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14) + self.failUnlessEqual(bin(s1), bin(s2)) - def test_unaligned_native_struct_fields(self): - if sys.byteorder == "little": - fmt = "<b h xi xd" - else: - base = LittleEndianStructure - fmt = ">b h xi xd" + def test_unaligned_native_struct_fields(self): + if sys.byteorder == "little": + fmt = "<b h xi xd" + else: + base = LittleEndianStructure + fmt = ">b h xi xd" - class S(Structure): - _pack_ = 1 - _fields_ = [("b", c_byte), + class S(Structure): + _pack_ = 1 + _fields_ = [("b", c_byte), - ("h", c_short), + ("h", c_short), - ("_1", c_byte), - ("i", c_int), + ("_1", c_byte), + ("i", c_int), - ("_2", c_byte), - ("d", c_double)] + ("_2", c_byte), + ("d", c_double)] - s1 = S(0x12, 0x1234, 0, 0x12345678, 0, 3.14) - s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14) - self.failUnlessEqual(bin(s1), bin(s2)) + s1 = S() + s1.b = 0x12 + s1.h = 0x1234 + s1.i = 0x12345678 + s1.d = 3.14 + s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14) + self.failUnlessEqual(bin(s1), bin(s2)) if __name__ == "__main__": unittest.main() diff --git a/Lib/ctypes/test/test_unaligned_structures.py b/Lib/ctypes/test/test_unaligned_structures.py new file mode 100644 index 0000000..ffbc2be --- /dev/null +++ b/Lib/ctypes/test/test_unaligned_structures.py @@ -0,0 +1,45 @@ +import sys, unittest
+from ctypes import *
+
+structures = []
+byteswapped_structures = []
+
+
+if sys.byteorder == "little":
+ SwappedStructure = BigEndianStructure
+else:
+ SwappedStructure = LittleEndianStructure
+
+for typ in [c_short, c_int, c_long, c_longlong,
+ c_float, c_double,
+ c_ushort, c_uint, c_ulong, c_ulonglong]:
+ class X(Structure):
+ _pack_ = 1
+ _fields_ = [("pad", c_byte),
+ ("value", typ)]
+ class Y(SwappedStructure):
+ _pack_ = 1
+ _fields_ = [("pad", c_byte),
+ ("value", typ)]
+ structures.append(X)
+ byteswapped_structures.append(Y)
+
+class TestStructures(unittest.TestCase):
+ def test_native(self):
+ for typ in structures:
+## print typ.value
+ self.failUnlessEqual(typ.value.offset, 1)
+ o = typ()
+ o.value = 4
+ self.failUnlessEqual(o.value, 4)
+
+ def test_swapped(self):
+ for typ in byteswapped_structures:
+## print >> sys.stderr, typ.value
+ self.failUnlessEqual(typ.value.offset, 1)
+ o = typ()
+ o.value = 4
+ self.failUnlessEqual(o.value, 4)
+
+if __name__ == '__main__':
+ unittest.main()
|