Пример #1
0
def register(cls: type,
             encode: Callable[[cls], bytes] = pickle.dumps,
             decode: Callable[[bytes], cls] = pickle.loads) -> None:
    """
    Register a serializable type

    Args:
        cls: type
        encode: Callable
            translate an object of type `cls` into `bytes`
            default: pickle.dumps
        decode: Callable
            translate `bytes` to an object of type `cls`
            default: pickle.loads
    """
    global __index
    __index += 1
    t = __index
    __pack_handlers[cls] = lambda obj: umsgpack.Ext(t, encode(obj))
    __unpack_handlers[t] = lambda ext: decode(ext.data)
Пример #2
0
    def test_ext_exceptions(self):
        with self.assertRaises(TypeError):
            _ = umsgpack.Ext(5.0, b"")

        with self.assertRaises(TypeError):
            _ = umsgpack.Ext(0, u"unicode string")
Пример #3
0
     b"\xd9\x28\xd0\x9f\xd0\xbe\x20\xd0\xbe\xd0\xb6\xd0\xb8\xd0\xb2\xd0\xbb\xd1\x91\xd0\xbd\xd0\xbd\xd1\x8b\xd0\xbc\x20\xd0\xb1\xd0\xb5\xd1\x80\xd0\xb5\xd0\xb3\xd0\xb0\xd0\xbc"
 ],
 # 8-bit Binary
 ["8-bit binary", b"\x80" * 1, b"\xc4\x01" + b"\x80" * 1],
 ["8-bit binary", b"\x80" * 32, b"\xc4\x20" + b"\x80" * 32],
 ["8-bit binary", b"\x80" * 255, b"\xc4\xff" + b"\x80" * 255],
 # 16-bit Binary
 ["16-bit binary", b"\x80" * 256, b"\xc5\x01\x00" + b"\x80" * 256],
 # 32-bit Binary
 [
     "32-bit binary", b"\x80" * 65536,
     b"\xc6\x00\x01\x00\x00" + b"\x80" * 65536
 ],
 # Fixext 1
 ["fixext 1",
  umsgpack.Ext(0x05, b"\x80" * 1), b"\xd4\x05" + b"\x80" * 1],
 # Fixext 2
 ["fixext 2",
  umsgpack.Ext(0x05, b"\x80" * 2), b"\xd5\x05" + b"\x80" * 2],
 # Fixext 4
 ["fixext 4",
  umsgpack.Ext(0x05, b"\x80" * 4), b"\xd6\x05" + b"\x80" * 4],
 # Fixext 8
 ["fixext 8",
  umsgpack.Ext(0x05, b"\x80" * 8), b"\xd7\x05" + b"\x80" * 8],
 # Fixext 16
 [
     "fixext 16",
     umsgpack.Ext(0x05, b"\x80" * 16), b"\xd8\x05" + b"\x80" * 16
 ],
 # 8-bit Ext