From 48ca2d700d019bfec9abe3242a8eb3073c803951 Mon Sep 17 00:00:00 2001 From: Sergey Zhuravlev Date: Sun, 15 Dec 2013 16:22:39 +0000 Subject: Added support of bytearrays to msgpack/fallback.py --- msgpack/fallback.py | 2 ++ test/test_buffer.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/msgpack/fallback.py b/msgpack/fallback.py index bf5b1c2..3ef1341 100644 --- a/msgpack/fallback.py +++ b/msgpack/fallback.py @@ -193,6 +193,8 @@ class Unpacker(object): def feed(self, next_bytes): if isinstance(next_bytes, array.array): next_bytes = next_bytes.tostring() + elif isinstance(next_bytes, bytearray): + next_bytes = str(next_bytes) assert self._fb_feeding if self._fb_buf_n + len(next_bytes) > self._max_buffer_size: raise BufferFull diff --git a/test/test_buffer.py b/test/test_buffer.py index 04cc02d..5ae87ac 100644 --- a/test/test_buffer.py +++ b/test/test_buffer.py @@ -11,3 +11,10 @@ def test_unpack_buffer(): obj = unpackb(buf, use_list=1) assert [b'foo', b'bar'] == obj + +def test_unpack_bytearray(): + buf = bytearray(packb(('foo', 'bar'))) + obj = unpackb(buf, use_list=1) + assert [b'foo', b'bar'] == obj + assert all(type(s)==str for s in obj) + -- cgit v1.2.1 From 11a3b1561a07b28a37ac22637f636c4c5ac24c23 Mon Sep 17 00:00:00 2001 From: Sergey Zhuravlev Date: Wed, 12 Feb 2014 23:09:23 +0400 Subject: fixed support of python3 --- msgpack/fallback.py | 2 +- test/test_buffer.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/msgpack/fallback.py b/msgpack/fallback.py index 3ef1341..b673222 100644 --- a/msgpack/fallback.py +++ b/msgpack/fallback.py @@ -194,7 +194,7 @@ class Unpacker(object): if isinstance(next_bytes, array.array): next_bytes = next_bytes.tostring() elif isinstance(next_bytes, bytearray): - next_bytes = str(next_bytes) + next_bytes = (bytes if PY3 else str)(next_bytes) assert self._fb_feeding if self._fb_buf_n + len(next_bytes) > self._max_buffer_size: raise BufferFull diff --git a/test/test_buffer.py b/test/test_buffer.py index 5ae87ac..7b85e93 100644 --- a/test/test_buffer.py +++ b/test/test_buffer.py @@ -2,6 +2,7 @@ # coding: utf-8 from msgpack import packb, unpackb +import sys def test_unpack_buffer(): @@ -16,5 +17,7 @@ def test_unpack_bytearray(): buf = bytearray(packb(('foo', 'bar'))) obj = unpackb(buf, use_list=1) assert [b'foo', b'bar'] == obj - assert all(type(s)==str for s in obj) + expected_type = bytes if sys.version_info[0] == 3 else str + assert all(type(s)==expected_type for s in obj) + -- cgit v1.2.1 From 7effb4aac64f1c44241e0a88ab19fa6c8405c3fa Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 17 Feb 2014 04:05:04 +0900 Subject: fix --- msgpack/fallback.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/msgpack/fallback.py b/msgpack/fallback.py index b673222..0c5b50f 100644 --- a/msgpack/fallback.py +++ b/msgpack/fallback.py @@ -194,7 +194,7 @@ class Unpacker(object): if isinstance(next_bytes, array.array): next_bytes = next_bytes.tostring() elif isinstance(next_bytes, bytearray): - next_bytes = (bytes if PY3 else str)(next_bytes) + next_bytes = bytes(next_bytes) assert self._fb_feeding if self._fb_buf_n + len(next_bytes) > self._max_buffer_size: raise BufferFull -- cgit v1.2.1 From 518f886b111808d7d9f29b34c50baddb01610338 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Mon, 17 Feb 2014 04:06:58 +0900 Subject: fix --- test/test_buffer.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/test_buffer.py b/test/test_buffer.py index 7b85e93..6cb2295 100644 --- a/test/test_buffer.py +++ b/test/test_buffer.py @@ -8,7 +8,7 @@ import sys def test_unpack_buffer(): from array import array buf = array('b') - buf.fromstring(packb(('foo', 'bar'))) + buf.fromstring(packb((b'foo', b'bar'))) obj = unpackb(buf, use_list=1) assert [b'foo', b'bar'] == obj @@ -17,7 +17,5 @@ def test_unpack_bytearray(): buf = bytearray(packb(('foo', 'bar'))) obj = unpackb(buf, use_list=1) assert [b'foo', b'bar'] == obj - expected_type = bytes if sys.version_info[0] == 3 else str - assert all(type(s)==expected_type for s in obj) - - + expected_type = bytes + assert all(type(s) == expected_type for s in obj) -- cgit v1.2.1