| 1 |
import array |
| 2 |
|
| 3 |
import msgpack |
| 4 |
from msgpack import ExtType |
| 5 |
|
| 6 |
|
| 7 |
def test_pack_ext_type(): |
| 8 |
def p(s): |
| 9 |
packer = msgpack.Packer(autoreset=False) |
| 10 |
packer.pack_ext_type(0x42, s) |
| 11 |
return packer.bytes() |
| 12 |
|
| 13 |
assert p(b"A") == b"\xd4\x42A" # fixext 1 |
| 14 |
assert p(b"AB") == b"\xd5\x42AB" # fixext 2 |
| 15 |
assert p(b"ABCD") == b"\xd6\x42ABCD" # fixext 4 |
| 16 |
assert p(b"ABCDEFGH") == b"\xd7\x42ABCDEFGH" # fixext 8 |
| 17 |
assert p(b"A" * 16) == b"\xd8\x42" + b"A" * 16 # fixext 16 |
| 18 |
assert p(b"ABC") == b"\xc7\x03\x42ABC" # ext 8 |
| 19 |
assert p(b"A" * 0x0123) == b"\xc8\x01\x23\x42" + b"A" * 0x0123 # ext 16 |
| 20 |
assert p(b"A" * 0x00012345) == b"\xc9\x00\x01\x23\x45\x42" + b"A" * 0x00012345 # ext 32 |
| 21 |
|
| 22 |
|
| 23 |
def test_pack_ext_type_autoreset(): |
| 24 |
packer = msgpack.Packer() |
| 25 |
|
| 26 |
assert packer.pack_ext_type(0x42, b"A") == b"\xd4\x42A" |
| 27 |
assert packer.bytes() == b"" |
| 28 |
assert packer.pack_ext_type(0x42, b"ABC") == b"\xc7\x03\x42ABC" |
| 29 |
assert packer.bytes() == b"" |
| 30 |
|
| 31 |
|
| 32 |
def test_unpack_ext_type(): |
| 33 |
def check(b, expected): |
| 34 |
assert msgpack.unpackb(b) == expected |
| 35 |
|
| 36 |
check(b"\xd4\x42A", ExtType(0x42, b"A")) # fixext 1 |
| 37 |
check(b"\xd5\x42AB", ExtType(0x42, b"AB")) # fixext 2 |
| 38 |
check(b"\xd6\x42ABCD", ExtType(0x42, b"ABCD")) # fixext 4 |
| 39 |
check(b"\xd7\x42ABCDEFGH", ExtType(0x42, b"ABCDEFGH")) # fixext 8 |
| 40 |
check(b"\xd8\x42" + b"A" * 16, ExtType(0x42, b"A" * 16)) # fixext 16 |
| 41 |
check(b"\xc7\x03\x42ABC", ExtType(0x42, b"ABC")) # ext 8 |
| 42 |
check(b"\xc8\x01\x23\x42" + b"A" * 0x0123, ExtType(0x42, b"A" * 0x0123)) # ext 16 |
| 43 |
check( |
| 44 |
b"\xc9\x00\x01\x23\x45\x42" + b"A" * 0x00012345, |
| 45 |
ExtType(0x42, b"A" * 0x00012345), |
| 46 |
) # ext 32 |
| 47 |
|
| 48 |
|
| 49 |
def test_extension_type(): |
| 50 |
def default(obj): |
| 51 |
print("default called", obj) |
| 52 |
if isinstance(obj, array.array): |
| 53 |
typecode = 123 # application specific typecode |
| 54 |
try: |
| 55 |
data = obj.tobytes() |
| 56 |
except AttributeError: |
| 57 |
data = obj.tostring() |
| 58 |
return ExtType(typecode, data) |
| 59 |
raise TypeError(f"Unknown type object {obj!r}") |
| 60 |
|
| 61 |
def ext_hook(code, data): |
| 62 |
print("ext_hook called", code, data) |
| 63 |
assert code == 123 |
| 64 |
obj = array.array("d") |
| 65 |
obj.frombytes(data) |
| 66 |
return obj |
| 67 |
|
| 68 |
obj = [42, b"hello", array.array("d", [1.1, 2.2, 3.3])] |
| 69 |
s = msgpack.packb(obj, default=default) |
| 70 |
obj2 = msgpack.unpackb(s, ext_hook=ext_hook) |
| 71 |
assert obj == obj2 |
| 72 |
|
| 73 |
|
| 74 |
def test_overriding_hooks(): |
| 75 |
def default(obj): |
| 76 |
if isinstance(obj, int): |
| 77 |
return {"__type__": "long", "__data__": str(obj)} |
| 78 |
else: |
| 79 |
return obj |
| 80 |
|
| 81 |
obj = {"testval": 1823746192837461928374619} |
| 82 |
refobj = {"testval": default(obj["testval"])} |
| 83 |
refout = msgpack.packb(refobj) |
| 84 |
assert isinstance(refout, (str, bytes)) |
| 85 |
testout = msgpack.packb(obj, default=default) |
| 86 |
|
| 87 |
assert refout == testout |