mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-04-09 21:10:18 +00:00
Use new msgpack spec by default. (#386)
This commit is contained in:
parent
de320488ae
commit
7e9905bdfa
11 changed files with 75 additions and 126 deletions
|
|
@ -17,7 +17,7 @@ def test_unpack_buffer():
|
|||
|
||||
|
||||
def test_unpack_bytearray():
|
||||
buf = bytearray(packb(("foo", "bar")))
|
||||
buf = bytearray(packb((b"foo", b"bar")))
|
||||
obj = unpackb(buf, use_list=1)
|
||||
assert [b"foo", b"bar"] == obj
|
||||
expected_type = bytes
|
||||
|
|
@ -25,7 +25,7 @@ def test_unpack_bytearray():
|
|||
|
||||
|
||||
def test_unpack_memoryview():
|
||||
buf = bytearray(packb(("foo", "bar")))
|
||||
buf = bytearray(packb((b"foo", b"bar")))
|
||||
view = memoryview(buf)
|
||||
obj = unpackb(view, use_list=1)
|
||||
assert [b"foo", b"bar"] == obj
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
from msgpack import packb, unpackb
|
||||
|
||||
|
||||
def check(length, obj):
|
||||
v = packb(obj)
|
||||
def check(length, obj, use_bin_type=True):
|
||||
v = packb(obj, use_bin_type=use_bin_type)
|
||||
assert len(v) == length, "%r length should be %r but get %r" % (obj, length, len(v))
|
||||
assert unpackb(v, use_list=0) == obj
|
||||
assert unpackb(v, use_list=0, raw=not use_bin_type) == obj
|
||||
|
||||
|
||||
def test_1():
|
||||
|
|
@ -56,7 +55,7 @@ def test_9():
|
|||
|
||||
|
||||
def check_raw(overhead, num):
|
||||
check(num + overhead, b" " * num)
|
||||
check(num + overhead, b" " * num, use_bin_type=False)
|
||||
|
||||
|
||||
def test_fixraw():
|
||||
|
|
@ -135,4 +134,4 @@ def test_match():
|
|||
|
||||
|
||||
def test_unicode():
|
||||
assert unpackb(packb("foobar"), use_list=1) == b"foobar"
|
||||
assert unpackb(packb(u"foobar"), use_list=1) == u"foobar"
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@
|
|||
from msgpack import unpackb
|
||||
|
||||
|
||||
def check(src, should, use_list=0):
|
||||
assert unpackb(src, use_list=use_list) == should
|
||||
def check(src, should, use_list=0, raw=True):
|
||||
assert unpackb(src, use_list=use_list, raw=raw) == should
|
||||
|
||||
|
||||
def testSimpleValue():
|
||||
|
|
@ -59,6 +59,12 @@ def testRaw():
|
|||
b"\x00\x00\xdb\x00\x00\x00\x01a\xdb\x00\x00\x00\x02ab",
|
||||
(b"", b"a", b"ab", b"", b"a", b"ab"),
|
||||
)
|
||||
check(
|
||||
b"\x96\xda\x00\x00\xda\x00\x01a\xda\x00\x02ab\xdb\x00\x00"
|
||||
b"\x00\x00\xdb\x00\x00\x00\x01a\xdb\x00\x00\x00\x02ab",
|
||||
("", "a", "ab", "", "a", "ab"),
|
||||
raw=False,
|
||||
)
|
||||
|
||||
|
||||
def testArray():
|
||||
|
|
|
|||
|
|
@ -1,50 +1,33 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
import pytest
|
||||
from array import array
|
||||
from msgpack import packb, unpackb
|
||||
import sys
|
||||
|
||||
|
||||
# For Python < 3:
|
||||
# - array type only supports old buffer interface
|
||||
# - array.frombytes is not available, must use deprecated array.fromstring
|
||||
if sys.version_info[0] < 3:
|
||||
|
||||
def make_memoryview(obj):
|
||||
return memoryview(buffer(obj))
|
||||
|
||||
def make_array(f, data):
|
||||
a = array(f)
|
||||
a.fromstring(data)
|
||||
return a
|
||||
|
||||
def get_data(a):
|
||||
return a.tostring()
|
||||
pytestmark = pytest.mark.skipif(
|
||||
sys.version_info[0] < 3, reason="Only Python 3 supports buffer protocol"
|
||||
)
|
||||
|
||||
|
||||
else:
|
||||
make_memoryview = memoryview
|
||||
|
||||
def make_array(f, data):
|
||||
a = array(f)
|
||||
a.frombytes(data)
|
||||
return a
|
||||
|
||||
def get_data(a):
|
||||
return a.tobytes()
|
||||
def make_array(f, data):
|
||||
a = array(f)
|
||||
a.frombytes(data)
|
||||
return a
|
||||
|
||||
|
||||
def _runtest(format, nbytes, expected_header, expected_prefix, use_bin_type):
|
||||
# create a new array
|
||||
original_array = array(format)
|
||||
original_array.fromlist([255] * (nbytes // original_array.itemsize))
|
||||
original_data = get_data(original_array)
|
||||
view = make_memoryview(original_array)
|
||||
original_data = original_array.tobytes()
|
||||
view = memoryview(original_array)
|
||||
|
||||
# pack, unpack, and reconstruct array
|
||||
packed = packb(view, use_bin_type=use_bin_type)
|
||||
unpacked = unpackb(packed)
|
||||
unpacked = unpackb(packed, raw=(not use_bin_type))
|
||||
reconstructed_array = make_array(format, unpacked)
|
||||
|
||||
# check that we got the right amount of data
|
||||
|
|
|
|||
|
|
@ -10,14 +10,16 @@ def test_str8():
|
|||
assert len(b) == len(data) + 2
|
||||
assert b[0:2] == header + b"\x20"
|
||||
assert b[2:] == data
|
||||
assert unpackb(b) == data
|
||||
assert unpackb(b, raw=True) == data
|
||||
assert unpackb(b, raw=False) == data.decode()
|
||||
|
||||
data = b"x" * 255
|
||||
b = packb(data.decode(), use_bin_type=True)
|
||||
assert len(b) == len(data) + 2
|
||||
assert b[0:2] == header + b"\xff"
|
||||
assert b[2:] == data
|
||||
assert unpackb(b) == data
|
||||
assert unpackb(b, raw=True) == data
|
||||
assert unpackb(b, raw=False) == data.decode()
|
||||
|
||||
|
||||
def test_bin8():
|
||||
|
|
|
|||
|
|
@ -8,9 +8,9 @@ def test_read_array_header():
|
|||
unpacker = Unpacker()
|
||||
unpacker.feed(packb(["a", "b", "c"]))
|
||||
assert unpacker.read_array_header() == 3
|
||||
assert unpacker.unpack() == b"a"
|
||||
assert unpacker.unpack() == b"b"
|
||||
assert unpacker.unpack() == b"c"
|
||||
assert unpacker.unpack() == "a"
|
||||
assert unpacker.unpack() == "b"
|
||||
assert unpacker.unpack() == "c"
|
||||
try:
|
||||
unpacker.unpack()
|
||||
assert 0, "should raise exception"
|
||||
|
|
@ -22,8 +22,8 @@ def test_read_map_header():
|
|||
unpacker = Unpacker()
|
||||
unpacker.feed(packb({"a": "A"}))
|
||||
assert unpacker.read_map_header() == 1
|
||||
assert unpacker.unpack() == b"a"
|
||||
assert unpacker.unpack() == b"A"
|
||||
assert unpacker.unpack() == "a"
|
||||
assert unpacker.unpack() == "A"
|
||||
try:
|
||||
unpacker.unpack()
|
||||
assert 0, "should raise exception"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
|
||||
import io
|
||||
from msgpack import Unpacker, BufferFull
|
||||
from msgpack import pack
|
||||
|
|
@ -26,7 +25,7 @@ def test_partialdata():
|
|||
with raises(StopIteration):
|
||||
next(iter(unpacker))
|
||||
unpacker.feed(b"o")
|
||||
assert next(iter(unpacker)) == b"hallo"
|
||||
assert next(iter(unpacker)) == "hallo"
|
||||
|
||||
|
||||
def test_foobar():
|
||||
|
|
@ -98,13 +97,13 @@ def test_readbytes():
|
|||
def test_issue124():
|
||||
unpacker = Unpacker()
|
||||
unpacker.feed(b"\xa1?\xa1!")
|
||||
assert tuple(unpacker) == (b"?", b"!")
|
||||
assert tuple(unpacker) == ("?", "!")
|
||||
assert tuple(unpacker) == ()
|
||||
unpacker.feed(b"\xa1?\xa1")
|
||||
assert tuple(unpacker) == (b"?",)
|
||||
assert tuple(unpacker) == ("?",)
|
||||
assert tuple(unpacker) == ()
|
||||
unpacker.feed(b"!")
|
||||
assert tuple(unpacker) == (b"!",)
|
||||
assert tuple(unpacker) == ("!",)
|
||||
assert tuple(unpacker) == ()
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue