Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions numcodecs/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# flake8: noqa
from __future__ import absolute_import, print_function, division
import sys
import codecs
import array


Expand All @@ -26,11 +27,11 @@
from functools import reduce


def ensure_text(l, encoding='utf-8'):
if isinstance(l, text_type):
return l
else: # pragma: py3 no cover
return text_type(l, encoding=encoding)
def ensure_text(s, encoding='utf-8'):
if not isinstance(s, text_type):
s = ensure_contiguous_ndarray(s)
s = codecs.decode(s, 'ascii')
return s


def ensure_ndarray(buf):
Expand Down
14 changes: 13 additions & 1 deletion numcodecs/tests/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@
import pytest


from numcodecs.compat import ensure_bytes, PY2, ensure_contiguous_ndarray
from numcodecs.compat import ensure_text, ensure_bytes, PY2, ensure_contiguous_ndarray, text_type


def test_ensure_text():
bufs = [
b'adsdasdas',
u'adsdasdas',
np.asarray(memoryview(b'adsdasdas')),
array.array('B', b'qwertyuiqwertyui')
]
for buf in bufs:
b = ensure_text(buf)
assert isinstance(b, text_type)


def test_ensure_bytes():
Expand Down