diff --git a/pylib/utils.py b/pylib/utils.py index 2530c98..22543a9 100644 --- a/pylib/utils.py +++ b/pylib/utils.py @@ -1,3 +1,4 @@ +# coding: utf-8 from .structures import AttrDict from .py3helpers import PY2, itervalues, iteritems, text_type, string_types, imap, is_iter @@ -89,8 +90,8 @@ def safeunicode(obj, encoding='utf-8'): u'hello' >>> safeunicode(2) u'2' - >>> safeunicode('\xe1\x88\xb4') - u'\u1234' + >>> safeunicode('\xe4\xb8\xad\xe6\x96\x87') + u'中文' """ t = type(obj) if t is text_type: @@ -115,7 +116,7 @@ def safestr(obj, encoding='utf-8'): if PY2 and isinstance(obj, unicode): return obj.encode(encoding) - elif is_iter(obj): + elif is_iter(obj) or isinstance(obj, iters): return imap(safestr, obj) else: return str(obj) diff --git a/tests/utils/test_str_unicode.py b/tests/utils/test_str_unicode.py new file mode 100644 index 0000000..7f5ed17 --- /dev/null +++ b/tests/utils/test_str_unicode.py @@ -0,0 +1,14 @@ +# coding: utf-8 +from pylib.utils import safestr, safeunicode + + +def test_safestr(): + assert safestr('hello') == 'hello' + assert safestr(1) == '1' + assert list(safestr([1, 'a'])) == ['1', 'a'] + + +def test_safeunicode(): + assert safeunicode('hello') == u'hello' + assert safeunicode(1) == u'1' + assert safeunicode('中文') == u'中文'