Allow asciify to deal with instances of unicode

This commit is contained in:
Sebastian Pipping 2011-10-17 21:46:31 +02:00
parent bb468f25bf
commit bb880e494b
1 changed files with 9 additions and 2 deletions

View File

@ -6,7 +6,14 @@
from __future__ import print_function 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 = ( _mapping = (
('ö', 'oe'), ('ö', 'oe'),
('Ö', 'Oe'), ('Ö', 'Oe'),
@ -26,7 +33,7 @@ def asciify(utf8):
return _mapping.get(ord(unichar), fix(unichar)) 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) unitext = ''.join(subst(e) for e in unichars)
return unitext.encode('ascii') return unitext.encode('ascii')