2015-05-04 14:35:40 +01:00
|
|
|
try:
|
|
|
|
import ustruct as struct
|
|
|
|
except:
|
2016-12-19 16:40:43 +00:00
|
|
|
try:
|
|
|
|
import struct
|
|
|
|
except ImportError:
|
|
|
|
print("SKIP")
|
2017-06-10 18:03:01 +01:00
|
|
|
raise SystemExit
|
2016-12-19 16:40:43 +00:00
|
|
|
|
2014-04-10 01:45:38 +01:00
|
|
|
print(struct.calcsize("<bI"))
|
|
|
|
print(struct.unpack("<bI", b"\x80\0\0\x01\0"))
|
2014-04-10 20:19:32 +01:00
|
|
|
print(struct.calcsize(">bI"))
|
|
|
|
print(struct.unpack(">bI", b"\x80\0\0\x01\0"))
|
2014-04-11 01:47:21 +01:00
|
|
|
|
|
|
|
# 32-bit little-endian specific
|
|
|
|
#print(struct.unpack("bI", b"\x80\xaa\x55\xaa\0\0\x01\0"))
|
2014-04-19 01:13:15 +01:00
|
|
|
|
2016-09-19 14:59:49 +01:00
|
|
|
print(struct.pack("<l", 1))
|
|
|
|
print(struct.pack(">l", 1))
|
2014-04-19 01:13:15 +01:00
|
|
|
print(struct.pack("<i", 1))
|
|
|
|
print(struct.pack(">i", 1))
|
|
|
|
print(struct.pack("<h", 1))
|
|
|
|
print(struct.pack(">h", 1))
|
|
|
|
print(struct.pack("<b", 1))
|
|
|
|
print(struct.pack(">b", 1))
|
|
|
|
|
|
|
|
print(struct.pack("<bI", -128, 256))
|
|
|
|
print(struct.pack(">bI", -128, 256))
|
2014-05-12 21:45:50 +01:00
|
|
|
|
|
|
|
print(struct.calcsize("100sI"))
|
|
|
|
print(struct.calcsize("97sI"))
|
|
|
|
print(struct.unpack("<6sH", b"foo\0\0\0\x12\x34"))
|
|
|
|
print(struct.pack("<6sH", b"foo", 10000))
|
2014-06-25 20:25:53 +01:00
|
|
|
|
|
|
|
s = struct.pack("BHBI", 10, 100, 200, 300)
|
|
|
|
v = struct.unpack("BHBI", s)
|
|
|
|
print(v == (10, 100, 200, 300))
|
2014-12-05 23:13:52 +00:00
|
|
|
|
2015-04-05 00:03:43 +01:00
|
|
|
# network byte order
|
|
|
|
print(struct.pack('!i', 123))
|
2015-09-03 23:06:18 +01:00
|
|
|
|
2017-09-01 01:53:29 +01:00
|
|
|
# check that we get an error if the buffer is too small
|
|
|
|
try:
|
|
|
|
struct.unpack('I', b'\x00\x00\x00')
|
|
|
|
except:
|
|
|
|
print('struct.error')
|
|
|
|
|
2015-09-03 23:06:18 +01:00
|
|
|
# first arg must be a string
|
|
|
|
try:
|
|
|
|
struct.pack(1, 2)
|
|
|
|
except TypeError:
|
|
|
|
print('TypeError')
|
2015-10-31 15:43:55 +00:00
|
|
|
|
2016-05-14 13:54:09 +01:00
|
|
|
# make sure that unknown types are detected
|
|
|
|
try:
|
|
|
|
struct.pack("z", 1)
|
|
|
|
except:
|
|
|
|
print("Unknown type")
|
|
|
|
|
2015-10-31 15:43:55 +00:00
|
|
|
# Initially repitition counters were supported only for strings,
|
|
|
|
# but later were implemented for all.
|
|
|
|
print(struct.unpack("<3B2h", b"foo\x12\x34\xff\xff"))
|
|
|
|
print(struct.pack("<3B", 1, 2, 3))
|
2015-12-24 03:11:27 +00:00
|
|
|
|
|
|
|
# pack_into
|
|
|
|
buf = bytearray(b'>>>123<<<')
|
|
|
|
struct.pack_into('<bbb', buf, 3, 0x41, 0x42, 0x43)
|
|
|
|
print(buf)
|
|
|
|
struct.pack_into('<bbb', buf, -6, 0x44, 0x45, 0x46)
|
|
|
|
print(buf)
|
|
|
|
|
2017-09-01 02:11:09 +01:00
|
|
|
# check that we get an error if the buffer is too small
|
|
|
|
try:
|
|
|
|
struct.pack_into('I', bytearray(1), 0, 0)
|
|
|
|
except:
|
|
|
|
print('struct.error')
|
|
|
|
|
2015-12-24 03:11:27 +00:00
|
|
|
try:
|
|
|
|
struct.pack_into('<bbb', buf, 7, 0x41, 0x42, 0x43)
|
|
|
|
except:
|
|
|
|
print('struct.error')
|
|
|
|
try:
|
|
|
|
struct.pack_into('<bbb', buf, -10, 0x41, 0x42, 0x43)
|
|
|
|
except:
|
|
|
|
print('struct.error')
|
|
|
|
|
|
|
|
# unpack_from
|
|
|
|
buf = b'0123456789'
|
|
|
|
print(struct.unpack_from('<b', buf, 4))
|
|
|
|
print(struct.unpack_from('<b', buf, -4))
|
|
|
|
try:
|
|
|
|
print(struct.unpack_from('<b', buf, 10))
|
|
|
|
except:
|
|
|
|
print('struct.error')
|
|
|
|
try:
|
|
|
|
print(struct.unpack_from('<b', buf, -11))
|
|
|
|
except:
|
|
|
|
print('struct.error')
|