3
from binascii import hexlify
7
# helper function to dump memory contents in hex, with a hyphen
9
h = hexlify(buffer(obj))
10
return re.sub(r"(..)", r"\1-", h)[:-1]
13
class Value(Structure):
14
_fields_ = [("val", c_byte)]
16
class Container(Structure):
17
_fields_ = [("pvalues", POINTER(Value))]
19
class Test(unittest.TestCase):
21
# create an array of 4 values
22
val_array = (Value * 4)()
24
# create a container, which holds a pointer to the pvalues array.
28
# memory contains 4 NUL bytes now, that's correct
29
self.failUnlessEqual("00-00-00-00", dump(val_array))
31
# set the values of the array through the pointer:
33
c.pvalues[i].val = i + 1
35
values = [c.pvalues[i].val for i in range(4)]
37
# These are the expected results: here s the bug!
39
(values, dump(val_array)),
40
([1, 2, 3, 4], "01-02-03-04")
45
val_array = (Value * 4)()
47
# memory contains 4 NUL bytes now, that's correct
48
self.failUnlessEqual("00-00-00-00", dump(val_array))
50
ptr = cast(val_array, POINTER(Value))
51
# set the values of the array through the pointer:
55
values = [ptr[i].val for i in range(4)]
57
# These are the expected results: here s the bug!
59
(values, dump(val_array)),
60
([1, 2, 3, 4], "01-02-03-04")
63
if __name__ == "__main__":