8000 Add raw_as_bytes option to Unpacker. by methane · Pull Request #265 · msgpack/msgpack-python · GitHub
[go: up one dir, main page]

Skip to content

Add raw_as_bytes option to Unpacker. #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use raw_as_bytes in test
  • Loading branch information
methane committed Jan 11, 2018
commit 382c00e297af12b21bbb83fb357a2c96b4f3f4b5
4 changes: 2 additions & 2 deletions test/test_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def test_max_str_len():
d = 'x' * 3
packed = packb(d)

unpacker = Unpacker(max_str_len=3, encoding='utf-8')
unpacker = Unpacker(max_str_len=3, raw_as_bytes=False)
unpacker.feed(packed)
assert unpacker.unpack() == d

unpacker = Unpacker(max_str_len=2, encoding='utf-8')
unpacker = Unpacker(max_str_len=2, raw_as_bytes=False)
with pytest.raises(UnpackValueError):
unpacker.feed(packed)
unpacker.unpack()
Expand Down
18 changes: 9 additions & 9 deletions test/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ def testPack():
def testPackUnicode():
test_data = ["", "abcd", ["defgh"], "Русский текст"]
for td in test_data:
re = unpackb(packb(td, encoding='utf-8'), use_list=1, encoding='utf-8')
re = unpackb(packb(td), use_list=1, raw_as_bytes=False)
assert re == td
packer = Packer(encoding='utf-8')
packer = Packer()
data = packer.pack(td)
re = Unpacker(BytesIO(data), encoding=str('utf-8'), use_list=1).unpack()
re = Unpacker(BytesIO(data), raw_as_bytes=False, use_list=1).unpack()
assert re == td

def testPackUTF32():
def testPackUTF32(): # deprecated
try:
test_data = [
"",
< 8000 svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-fold-down"> Expand Down Expand Up @@ -66,20 +66,20 @@ def testPackByteArrays():
for td in test_data:
check(td)

def testIgnoreUnicodeErrors():
def testIgnoreUnicodeErrors(): # deprecated
re = unpackb(packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1)
assert re == "abcdef"

def testStrictUnicodeUnpack():
with raises(UnicodeDecodeError):
unpackb(packb(b'abc\xeddef'), encoding='utf-8', use_list=1)
unpackb(packb(b'abc\xeddef'), raw_as_bytes=False, use_list=1)

def testStrictUnicodePack():
def testStrictUnicodePack(): # deprecated
with raises(UnicodeEncodeError):
packb("abc\xeddef", encoding='ascii', unicode_errors='strict')

def testIgnoreErrorsPack():
re = unpackb(packb("abcФФФdef", encoding='ascii', unicode_errors='ignore'), encoding='utf-8', use_list=1)
def testIgnoreErrorsPack(): # deprecated
re = unpackb(packb("abcФФФdef", encoding='ascii', unicode_errors='ignore'), raw_as_bytes=False, use_list=1)
assert re == "abcdef"

def testNoEncoding():
Expand Down
8 changes: 4 additions & 4 deletions test/test_stricttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def default(o):
return dict(o._asdict())
raise TypeError('Unsupported type %s' % (type(o),))
packed = packb(T(1, 42), strict_types=True, use_bin_type=True, default=default)
unpacked = unpackb(packed, encoding='utf-8')
unpacked = unpackb(packed, raw_as_bytes=False)
assert unpacked == {'foo': 1, 'bar': 42}


Expand All @@ -32,7 +32,7 @@ def convert(o):
return o

data = packb(t, strict_types=True, use_bin_type=True, default=default)
expected = unpackb(data, encoding='utf-8', object_hook=convert)
expected = unpackb(data, raw_as_bytes=False, object_hook=convert)

assert expected == t

Expand All @@ -53,10 +53,10 @@ def default(o):
def convert(code, payload):
if code == MSGPACK_EXT_TYPE_TUPLE:
# Unpack and convert to tuple
return tuple(unpackb(payload, encoding='utf-8', ext_hook=convert))
return tuple(unpackb(payload, raw_as_bytes=False, ext_hook=convert))
raise ValueError('Unknown Ext code {}'.format(code))

data = packb(t, strict_types=True, use_bin_type=True, default=default)
expected = unpackb(data, encoding='utf-8', ext_hook=convert)
expected = unpackb(data, raw_as_bytes=False, ext_hook=convert)

assert expected == t
10 changes: 5 additions & 5 deletions test/test_unpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def test_unpacker_ext_hook():
class MyUnpacker(Unpacker):

def __init__(self):
super(MyUnpacker, self).__init__(ext_hook=self._hook,
encoding='utf-8')
super(MyUnpacker, self).__init__(
ext_hook=self._hook, raw_as_bytes=False)

def _hook(self, code, data):
if code == 1:
Expand All @@ -57,11 +57,11 @@ def _hook(self, code, data):
return ExtType(code, data)

unpacker = MyUnpacker()
unpacker.feed(packb({'a': 1}, encoding='utf-8'))
unpacker.feed(packb({'a': 1}))
assert unpacker.unpack() == {'a': 1}
unpacker.feed(packb({'a': ExtType(1, b'123')}, encoding='utf-8'))
unpacker.feed(packb({'a': ExtType(1, b'123')}))
assert unpacker.unpack() == {'a': 123}
unpacker.feed(packb({'a': ExtType(2, b'321')}, encoding='utf-8'))
unpacker.feed(packb({'a': ExtType(2, b'321')}))
assert unpacker.unpack() == {'a': ExtType(2, b'321')}


Expand Down
0