2017-02-14 17:56:22 -05:00
|
|
|
try:
|
|
|
|
import uctypes
|
|
|
|
except ImportError:
|
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2014-10-29 20:43:53 -04:00
|
|
|
|
|
|
|
desc = {
|
|
|
|
# arr is array at offset 0, of UINT8 elements, array size is 2
|
|
|
|
"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
|
|
|
|
# arr2 is array at offset 0, size 2, of structures defined recursively
|
|
|
|
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
|
|
|
|
"arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
|
2016-01-03 13:38:36 -05:00
|
|
|
"arr4": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0, "w": uctypes.UINT16 | 1}),
|
2021-03-15 09:57:36 -04:00
|
|
|
"sub": (
|
|
|
|
0,
|
|
|
|
{
|
|
|
|
"b1": uctypes.BFUINT8 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
|
|
|
|
"b2": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
|
|
|
|
},
|
|
|
|
),
|
2014-10-29 20:43:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
data = bytearray(b"01234567")
|
|
|
|
|
2015-06-06 15:57:31 -04:00
|
|
|
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
|
2014-10-29 20:43:53 -04:00
|
|
|
|
|
|
|
print(uctypes.sizeof(S.arr))
|
|
|
|
assert uctypes.sizeof(S.arr) == 2
|
|
|
|
|
|
|
|
print(uctypes.sizeof(S.arr2))
|
|
|
|
assert uctypes.sizeof(S.arr2) == 2
|
|
|
|
|
|
|
|
print(uctypes.sizeof(S.arr3))
|
|
|
|
|
|
|
|
try:
|
|
|
|
print(uctypes.sizeof(S.arr3[0]))
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
2015-12-09 11:47:31 -05:00
|
|
|
|
|
|
|
print(uctypes.sizeof(S.arr4))
|
|
|
|
assert uctypes.sizeof(S.arr4) == 6
|
|
|
|
|
2016-01-03 13:38:36 -05:00
|
|
|
print(uctypes.sizeof(S.sub))
|
|
|
|
assert uctypes.sizeof(S.sub) == 1
|
|
|
|
|
2017-12-19 00:48:41 -05:00
|
|
|
# invalid descriptor
|
|
|
|
try:
|
|
|
|
print(uctypes.sizeof([]))
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|