From 43c37a603cc7c4821852ea5b2fbaf11202495edb Mon Sep 17 00:00:00 2001 From: John Ralls Date: Sun, 6 Apr 2014 13:09:37 -0700 Subject: [PATCH] Streamline conv_to_unicode Also handle "None" strings and change default encoding to 'utf8' so that it works without args when converting from UI input. --- gramps/gen/constfunc.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/gramps/gen/constfunc.py b/gramps/gen/constfunc.py index b60d85737..40764083d 100644 --- a/gramps/gen/constfunc.py +++ b/gramps/gen/constfunc.py @@ -55,24 +55,17 @@ WINDOWS = ["Windows", "win32"] #python 2 and 3 support, use correct conversion to unicode if sys.version_info[0] < 3: - def conv_to_unicode(x, y=None): - if isinstance(x, unicode): - return x - return unicode(x, y) if y else unicode(x) - conv_to_unicode_direct = unicode STRTYPE = basestring UNITYPE = unicode else: - def conv_to_unicode(x, y): - if isinstance(x, str): - return x - else: - return x.decode(y) conv_to_unicode_direct = str STRTYPE = str UNITYPE = str cuni = conv_to_unicode_direct +def conv_to_unicode(x, y='utf8'): + return x if x is None or isinstance(x, UNITYPE) else cuni(x, y) + # handle in database is bytes, while internally Gramps wants unicode for py3 if sys.version_info[0] < 3: