diff --git a/client-barcode/freitagslib/encoding.py b/client-barcode/freitagslib/encoding.py index 0c254ab..00ee211 100644 --- a/client-barcode/freitagslib/encoding.py +++ b/client-barcode/freitagslib/encoding.py @@ -6,7 +6,14 @@ from __future__ import print_function -def asciify(utf8): +def ensure_unicode(candidate, encoding='utf-8'): + if isinstance(candidate, basestring) \ + and not isinstance(candidate, unicode): + return unicode(candidate, encoding) + return candidate + + +def asciify(text): _mapping = ( ('ö', 'oe'), ('Ö', 'Oe'), @@ -26,7 +33,7 @@ def asciify(utf8): return _mapping.get(ord(unichar), fix(unichar)) - unichars = utf8.decode('utf-8') + unichars = ensure_unicode(text, 'utf-8') unitext = ''.join(subst(e) for e in unichars) return unitext.encode('ascii')