PythonExtra/tests/extmod/uctypes_bytearray.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

23 lines
471 B
Python
Raw Normal View History

2017-02-14 23:56:22 +01:00
try:
import uctypes
except ImportError:
print("SKIP")
raise SystemExit
desc = {
"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
"arr2": (uctypes.ARRAY | 2, uctypes.INT8 | 2),
}
data = bytearray(b"01234567")
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
# Arrays of UINT8 are accessed as bytearrays
print(S.arr)
# But not INT8, because value range is different
print(type(S.arr2))
# convert to buffer
print(bytearray(S))