From 5d56fa79694d3827975c36aa7986a80e164d6c60 Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Wed, 10 Apr 2019 11:45:50 +0200 Subject: [PATCH] to_unicode: Handle memoryview Otherwise unicode() on a memoryview will not actually read the text inside. I've encountered this type while making a Codec class calling to_unicode(). --- weboob/tools/misc.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/weboob/tools/misc.py b/weboob/tools/misc.py index afde6cf095..67c8d57b28 100644 --- a/weboob/tools/misc.py +++ b/weboob/tools/misc.py @@ -29,7 +29,7 @@ __all__ = ['get_backtrace', 'get_bytes_size', 'iter_fields', - 'to_unicode', 'input', 'limit', 'find_exe'] + 'to_unicode', 'input', 'limit', 'find_exe'] def get_backtrace(empty="Empty backtrace."): @@ -88,6 +88,9 @@ def to_unicode(text): if isinstance(text, unicode): return text + if isinstance(text, memoryview): + text = text.tobytes() + if not isinstance(text, bytes): if sys.version_info.major >= 3: return unicode(text) @@ -197,4 +200,3 @@ def find_exe(basename): fpath = os.path.join(path, ex) if os.path.exists(fpath) and os.access(fpath, os.X_OK): return fpath - -- GitLab