diff --git a/numcodecs/compat.py b/numcodecs/compat.py index f3b6e95c..82528a7a 100644 --- a/numcodecs/compat.py +++ b/numcodecs/compat.py @@ -14,6 +14,7 @@ if PY2: # pragma: py3 no cover + buffer = buffer text_type = unicode binary_type = str integer_types = (int, long) @@ -21,6 +22,7 @@ else: # pragma: py2 no cover + buffer = memoryview text_type = str binary_type = bytes integer_types = int, @@ -33,10 +35,10 @@ def buffer_tobytes(v): return v elif isinstance(v, np.ndarray): return v.tobytes(order='A') - elif PY2 and isinstance(v, array.array): # pragma: py3 no cover - return v.tostring() - else: - return memoryview(v).tobytes() + elif PY2: # pragma: py3 no cover + v = buffer(v) + + return memoryview(v).tobytes() def buffer_copy(buf, out=None): diff --git a/numcodecs/gzip.py b/numcodecs/gzip.py index 802d9fcd..c6bb1af6 100644 --- a/numcodecs/gzip.py +++ b/numcodecs/gzip.py @@ -8,7 +8,7 @@ from .abc import Codec -from .compat import buffer_copy, handle_datetime, buffer_tobytes, PY2 +from .compat import buffer, buffer_copy, handle_datetime, PY2 class GZip(Codec): @@ -42,8 +42,8 @@ def encode(self, buf): buf = buf.tobytes(order='A') if PY2: # pragma: py3 no cover - # ensure bytes, PY2 cannot handle things like bytearray - buf = buffer_tobytes(buf) + # ensure buffer, PY2 cannot handle things like bytearray + buf = buffer(buf) # do compression compressed = io.BytesIO() @@ -59,8 +59,8 @@ def encode(self, buf): def decode(self, buf, out=None): if PY2: # pragma: py3 no cover - # ensure bytes, PY2 cannot handle things like bytearray - buf = buffer_tobytes(buf) + # ensure buffer, PY2 cannot handle things like bytearray + buf = buffer(buf) # do decompression buf = io.BytesIO(buf) diff --git a/numcodecs/zlib.py b/numcodecs/zlib.py index 0ab68d98..dfa29f02 100644 --- a/numcodecs/zlib.py +++ b/numcodecs/zlib.py @@ -7,7 +7,7 @@ from .abc import Codec -from .compat import buffer_copy, handle_datetime, buffer_tobytes, PY2 +from .compat import buffer, buffer_copy, handle_datetime, PY2 class Zlib(Codec): @@ -41,8 +41,8 @@ def encode(self, buf): buf = buf.tobytes(order='A') if PY2: # pragma: py3 no cover - # ensure bytes, PY2 cannot handle things like bytearray - buf = buffer_tobytes(buf) + # ensure buffer, PY2 cannot handle things like bytearray + buf = buffer(buf) # do compression return _zlib.compress(buf, self.level) @@ -51,8 +51,8 @@ def encode(self, buf): def decode(self, buf, out=None): if PY2: # pragma: py3 no cover - # ensure bytes, PY2 cannot handle things like bytearray - buf = buffer_tobytes(buf) + # ensure buffer, PY2 cannot handle things like bytearray + buf = buffer(buf) # do decompression dec = _zlib.decompress(buf)