Skip to content

Commit 441c527

Browse files
committed
Smooth over Python 2/3 memoryview coercion
In Python 2, everything can be comfortably coerced to the old-style buffer interface, which is easily coerced to the new buffer interface. This is a nice path to proceed down as it can cleanly get a `memoryview` on Python 2 without copying. This strategy works well on Python 3 as well as long as we make `buffer` a no-op.
1 parent 62a5df0 commit 441c527

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

numcodecs/compat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
binary_type = str
1919
integer_types = (int, long)
2020
reduce = reduce
21+
buffer = buffer
2122

2223
else: # pragma: py2 no cover
2324

2425
text_type = str
2526
binary_type = bytes
2627
integer_types = int,
2728
from functools import reduce
29+
buffer = lambda a: a
2830

2931

3032
def buffer_tobytes(v):
@@ -33,10 +35,8 @@ def buffer_tobytes(v):
3335
return v
3436
elif isinstance(v, np.ndarray):
3537
return v.tobytes(order='A')
36-
elif PY2: # pragma: py3 no cover
37-
v = buffer(v)
38-
39-
return memoryview(v).tobytes()
38+
else:
39+
return memoryview(buffer(v)).tobytes()
4040

4141

4242
def buffer_copy(buf, out=None):

0 commit comments

Comments
 (0)