Skip to content

Use old-style buffer on Python 2 #119

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

Closed
wants to merge 8 commits into from
10 changes: 6 additions & 4 deletions numcodecs/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

if PY2: # pragma: py3 no cover

buffer = buffer
text_type = unicode
binary_type = str
integer_types = (int, long)
reduce = reduce

else: # pragma: py2 no cover

buffer = memoryview
text_type = str
binary_type = bytes
integer_types = int,
Expand All @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions numcodecs/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions numcodecs/zlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down