2013-10-15 16:59:43 +02:00
|
|
|
import array
|
|
|
|
import msgpack
|
|
|
|
|
|
|
|
def test_extension_type():
|
|
|
|
class MyPacker(msgpack.Packer):
|
2013-10-18 15:03:58 +02:00
|
|
|
def handle_unknown_type(self, obj):
|
2013-10-15 16:59:43 +02:00
|
|
|
if isinstance(obj, array.array):
|
|
|
|
fmt = "ext 32"
|
|
|
|
typecode = 123 # application specific typecode
|
|
|
|
data = obj.tostring()
|
2013-10-18 15:03:58 +02:00
|
|
|
self.pack_extended_type(fmt, typecode, data)
|
|
|
|
return True
|
2013-10-15 16:59:43 +02:00
|
|
|
|
|
|
|
class MyUnpacker(msgpack.Unpacker):
|
2013-10-18 15:03:58 +02:00
|
|
|
def read_extended_type(self, typecode, data):
|
2013-10-15 16:59:43 +02:00
|
|
|
assert typecode == 123
|
|
|
|
obj = array.array('d')
|
|
|
|
obj.fromstring(data)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
obj = [42, 'hello', array.array('d', [1.1, 2.2, 3.3])]
|
2013-10-18 14:38:52 +02:00
|
|
|
packer = MyPacker()
|
|
|
|
unpacker = MyUnpacker(None)
|
|
|
|
s = packer.pack(obj)
|
|
|
|
unpacker.feed(s)
|
|
|
|
obj2 = unpacker.unpack_one()
|
2013-10-15 16:59:43 +02:00
|
|
|
assert obj == obj2
|