Skip to content

Commit 67d22ee

Browse files
committed
Ensure buf is bytes in GZip's decode on Python 3
On Python 3, `BytesIO` avoids a copy of `buf` if it is already of `bytes` type. So go ahead and convert it to `bytes` in the `decode` method. If it is already of `bytes` type, then this is a no-op that avoids a copy on Python 3. Unfortunately Python 2 copies the result regardless of what type it is. So simply make sure to take an `ndarray` view onto the data on Python 2. This will ensures that `buf` is something that satisfies the buffer protocol. That way we do not introduce any additional copies beyond what `BytesIO` will do on Python 2 anyways.
1 parent 6523ba2 commit 67d22ee

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

numcodecs/gzip.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
from .abc import Codec
8-
from .compat import ensure_ndarray, ensure_contiguous_ndarray, PY2
8+
from .compat import ensure_bytes, ensure_ndarray, ensure_contiguous_ndarray, PY2
99

1010

1111
class GZip(Codec):
@@ -50,7 +50,10 @@ def encode(self, buf):
5050
def decode(self, buf, out=None):
5151

5252
# normalise inputs
53-
buf = ensure_contiguous_ndarray(buf)
53+
if PY2: # pragma: py3 no cover
54+
buf = ensure_contiguous_ndarray(buf)
55+
else: # pragma: py2 no cover
56+
buf = ensure_bytes(buf)
5457

5558
# do decompression
5659
buf = io.BytesIO(buf)

0 commit comments

Comments
 (0)