def safe_unicode(s):
    '''Creates unicode object from string s.
    It tries to decode string as UTF-8, fallbacks to current locale
    or ISO-8859-1 if both decode attemps fail'''

    if type(s) == unicode:
        return s
    elif isinstance(s, Exception):
        s = str(s)
    
    try:
        return s.decode('UTF-8')
    except UnicodeDecodeError:
        pass

    try:
        lang,encoding = locale.getdefaultlocale()
    except ValueError:
        lang,encoding = 'C','UTF-8'

    if encoding != 'UTF-8':
        try:
            return s.decode(encoding)
        except __HOLE__:
            pass

    return s.decode('ISO-8859-1')
dataset/ETHPy150Open gyim/stupidgit/stupidgit_gui/util.py/safe_unicode