2009-06-29 09:31:43 +09:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# coding: utf-8
|
2014-02-15 22:36:52 +09:00
|
|
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
2009-06-29 09:31:43 +09:00
|
|
|
|
2012-09-21 14:15:30 +09:00
|
|
|
import struct
|
2012-12-29 11:24:25 +09:00
|
|
|
from pytest import raises, xfail
|
2009-06-29 09:31:43 +09:00
|
|
|
|
2012-07-04 14:58:36 +09:00
|
|
|
from msgpack import packb, unpackb, Unpacker, Packer
|
2009-06-29 09:31:43 +09:00
|
|
|
|
2012-06-19 13:55:14 +09:00
|
|
|
from io import BytesIO
|
2011-08-22 01:52:45 +09:00
|
|
|
|
2012-09-24 02:12:55 +09:00
|
|
|
def check(data, use_list=False):
|
|
|
|
re = unpackb(packb(data), use_list=use_list)
|
2012-12-29 11:24:25 +09:00
|
|
|
assert re == data
|
2009-06-29 09:31:43 +09:00
|
|
|
|
|
|
|
def testPack():
|
|
|
|
test_data = [
|
2013-06-03 13:17:03 +09:00
|
|
|
0, 1, 127, 128, 255, 256, 65535, 65536, 4294967295, 4294967296,
|
|
|
|
-1, -32, -33, -128, -129, -32768, -32769, -4294967296, -4294967297,
|
2009-06-29 09:31:43 +09:00
|
|
|
1.0,
|
2012-06-19 13:55:14 +09:00
|
|
|
b"", b"a", b"a"*31, b"a"*32,
|
2009-06-29 09:31:43 +09:00
|
|
|
None, True, False,
|
2011-04-15 17:36:17 +03:00
|
|
|
(), ((),), ((), None,),
|
|
|
|
{None: 0},
|
|
|
|
(1<<23),
|
2009-06-29 09:31:43 +09:00
|
|
|
]
|
|
|
|
for td in test_data:
|
|
|
|
check(td)
|
|
|
|
|
2011-04-15 17:36:17 +03:00
|
|
|
def testPackUnicode():
|
2014-02-15 22:36:52 +09:00
|
|
|
test_data = ["", "abcd", ["defgh"], "Русский текст"]
|
2011-04-15 17:36:17 +03:00
|
|
|
for td in test_data:
|
2012-09-24 02:20:53 +09:00
|
|
|
re = unpackb(packb(td, encoding='utf-8'), use_list=1, encoding='utf-8')
|
2012-12-29 11:24:25 +09:00
|
|
|
assert re == td
|
2011-08-22 01:52:45 +09:00
|
|
|
packer = Packer(encoding='utf-8')
|
|
|
|
data = packer.pack(td)
|
2014-02-15 22:36:52 +09:00
|
|
|
re = Unpacker(BytesIO(data), encoding=str('utf-8'), use_list=1).unpack()
|
2012-12-29 11:24:25 +09:00
|
|
|
assert re == td
|
2011-04-15 17:36:17 +03:00
|
|
|
|
|
|
|
def testPackUTF32():
|
2011-06-01 18:30:43 +09:00
|
|
|
try:
|
|
|
|
test_data = [
|
2014-02-15 22:36:52 +09:00
|
|
|
"",
|
|
|
|
"abcd",
|
|
|
|
["defgh"],
|
|
|
|
"Русский текст",
|
2011-06-01 18:30:43 +09:00
|
|
|
]
|
|
|
|
for td in test_data:
|
2012-09-24 02:12:55 +09:00
|
|
|
re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32')
|
2012-12-29 11:24:25 +09:00
|
|
|
assert re == td
|
|
|
|
except LookupError as e:
|
|
|
|
xfail(e)
|
2011-04-15 17:36:17 +03:00
|
|
|
|
|
|
|
def testPackBytes():
|
|
|
|
test_data = [
|
2012-06-19 13:55:14 +09:00
|
|
|
b"", b"abcd", (b"defgh",),
|
2011-04-15 17:36:17 +03:00
|
|
|
]
|
|
|
|
for td in test_data:
|
2017-05-18 13:03:15 +02:00
|
|
|
check(td)
|
|
|
|
|
|
|
|
def testPackByteArrays():
|
|
|
|
test_data = [
|
|
|
|
bytearray(b""), bytearray(b"abcd"), (bytearray(b"defgh"),),
|
|
|
|
]
|
|
|
|
for td in test_data:
|
2011-04-15 17:36:17 +03:00
|
|
|
check(td)
|
|
|
|
|
|
|
|
def testIgnoreUnicodeErrors():
|
2012-09-24 02:20:53 +09:00
|
|
|
re = unpackb(packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1)
|
2012-12-29 11:24:25 +09:00
|
|
|
assert re == "abcdef"
|
2011-04-15 17:36:17 +03:00
|
|
|
|
|
|
|
def testStrictUnicodeUnpack():
|
2012-12-29 11:24:25 +09:00
|
|
|
with raises(UnicodeDecodeError):
|
|
|
|
unpackb(packb(b'abc\xeddef'), encoding='utf-8', use_list=1)
|
2011-04-15 17:36:17 +03:00
|
|
|
|
|
|
|
def testStrictUnicodePack():
|
2012-12-29 11:24:25 +09:00
|
|
|
with raises(UnicodeEncodeError):
|
2014-02-15 22:36:52 +09:00
|
|
|
packb("abc\xeddef", encoding='ascii', unicode_errors='strict')
|
2011-04-15 17:36:17 +03:00
|
|
|
|
|
|
|
def testIgnoreErrorsPack():
|
2014-02-15 22:36:52 +09:00
|
|
|
re = unpackb(packb("abcФФФdef", encoding='ascii', unicode_errors='ignore'), encoding='utf-8', use_list=1)
|
|
|
|
assert re == "abcdef"
|
2011-04-15 17:36:17 +03:00
|
|
|
|
|
|
|
def testNoEncoding():
|
2012-12-29 11:24:25 +09:00
|
|
|
with raises(TypeError):
|
2014-02-15 22:36:52 +09:00
|
|
|
packb("abc", encoding=None)
|
2011-04-15 17:36:17 +03:00
|
|
|
|
|
|
|
def testDecodeBinary():
|
2014-02-15 22:36:52 +09:00
|
|
|
re = unpackb(packb(b"abc"), encoding=None, use_list=1)
|
2012-12-29 11:24:25 +09:00
|
|
|
assert re == b"abc"
|
2011-04-15 17:36:17 +03:00
|
|
|
|
2012-09-21 14:15:30 +09:00
|
|
|
def testPackFloat():
|
2014-02-15 22:36:52 +09:00
|
|
|
assert packb(1.0, use_single_float=True) == b'\xca' + struct.pack(str('>f'), 1.0)
|
|
|
|
assert packb(1.0, use_single_float=False) == b'\xcb' + struct.pack(str('>d'), 1.0)
|
2012-09-21 14:15:30 +09:00
|
|
|
|
2012-09-23 17:26:16 +10:00
|
|
|
def testArraySize(sizes=[0, 5, 50, 1000]):
|
2014-02-15 22:16:01 +09:00
|
|
|
bio = BytesIO()
|
2012-09-23 17:26:16 +10:00
|
|
|
packer = Packer()
|
|
|
|
for size in sizes:
|
|
|
|
bio.write(packer.pack_array_header(size))
|
|
|
|
for i in range(size):
|
|
|
|
bio.write(packer.pack(i))
|
|
|
|
|
|
|
|
bio.seek(0)
|
2012-10-01 01:34:58 +09:00
|
|
|
unpacker = Unpacker(bio, use_list=1)
|
2012-09-23 17:26:16 +10:00
|
|
|
for size in sizes:
|
2012-10-01 01:34:58 +09:00
|
|
|
assert unpacker.unpack() == list(range(size))
|
2012-09-23 17:26:16 +10:00
|
|
|
|
2012-12-10 21:47:18 +09:00
|
|
|
def test_manualreset(sizes=[0, 5, 50, 1000]):
|
|
|
|
packer = Packer(autoreset=False)
|
|
|
|
for size in sizes:
|
|
|
|
packer.pack_array_header(size)
|
|
|
|
for i in range(size):
|
|
|
|
packer.pack(i)
|
|
|
|
|
2014-02-15 22:16:01 +09:00
|
|
|
bio = BytesIO(packer.bytes())
|
2012-12-10 21:47:18 +09:00
|
|
|
unpacker = Unpacker(bio, use_list=1)
|
|
|
|
for size in sizes:
|
|
|
|
assert unpacker.unpack() == list(range(size))
|
|
|
|
|
|
|
|
packer.reset()
|
|
|
|
assert packer.bytes() == b''
|
|
|
|
|
2012-09-23 17:26:16 +10:00
|
|
|
def testMapSize(sizes=[0, 5, 50, 1000]):
|
2014-02-15 22:16:01 +09:00
|
|
|
bio = BytesIO()
|
2012-09-23 17:26:16 +10:00
|
|
|
packer = Packer()
|
|
|
|
for size in sizes:
|
|
|
|
bio.write(packer.pack_map_header(size))
|
|
|
|
for i in range(size):
|
|
|
|
bio.write(packer.pack(i)) # key
|
|
|
|
bio.write(packer.pack(i * 2)) # value
|
|
|
|
|
|
|
|
bio.seek(0)
|
|
|
|
unpacker = Unpacker(bio)
|
|
|
|
for size in sizes:
|
2012-10-01 01:34:58 +09:00
|
|
|
assert unpacker.unpack() == dict((i, i * 2) for i in range(size))
|
2012-09-23 17:26:16 +10:00
|
|
|
|
|
|
|
|
2012-09-23 02:13:32 +09:00
|
|
|
class odict(dict):
|
2017-01-11 04:04:23 +01:00
|
|
|
"""Reimplement OrderedDict to run test on Python 2.6"""
|
2012-09-23 02:13:32 +09:00
|
|
|
def __init__(self, seq):
|
|
|
|
self._seq = seq
|
|
|
|
dict.__init__(self, seq)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return self._seq[:]
|
|
|
|
|
|
|
|
def iteritems(self):
|
|
|
|
return iter(self._seq)
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return [x[0] for x in self._seq]
|
|
|
|
|
|
|
|
def test_odict():
|
|
|
|
seq = [(b'one', 1), (b'two', 2), (b'three', 3), (b'four', 4)]
|
|
|
|
od = odict(seq)
|
2012-12-29 11:24:25 +09:00
|
|
|
assert unpackb(packb(od), use_list=1) == dict(seq)
|
2012-09-23 19:37:28 +10:00
|
|
|
def pair_hook(seq):
|
2013-02-03 00:20:00 +09:00
|
|
|
return list(seq)
|
2012-12-29 11:24:25 +09:00
|
|
|
assert unpackb(packb(od), object_pairs_hook=pair_hook, use_list=1) == seq
|
2012-09-23 02:13:32 +09:00
|
|
|
|
|
|
|
|
2012-12-10 21:26:41 +09:00
|
|
|
def test_pairlist():
|
|
|
|
pairlist = [(b'a', 1), (2, b'b'), (b'foo', b'bar')]
|
|
|
|
packer = Packer()
|
|
|
|
packed = packer.pack_map_pairs(pairlist)
|
|
|
|
unpacked = unpackb(packed, object_pairs_hook=list)
|
|
|
|
assert pairlist == unpacked
|