Comment 3 for bug 435370

Revision history for this message
Zed A. Shaw (zedshaw) wrote :

Here's a rewrite of that function so that it at least continues to operate. Feel free to do something similar that at least won't peg web.py at 100% CPU.

 def safeunicode(obj, encoding='utf-8'):
    r"""
    Converts any given object to unicode string.

        >>> safeunicode('hello')
        u'hello'
        >>> safeunicode(2)
        u'2'
        >>> safeunicode('\xe1\x88\xb4')
        u'\u1234'
    """
    if isinstance(obj, unicode):
        return obj
    elif isinstance(obj, str):
        try:
            return obj.decode(encoding)
        except UnicodeDecodeError:
            # failed to unicode decode, so they get it raw
            return obj
    else:
        if hasattr(obj, '__unicode__'):
            return unicode(obj)
        else:
            try:
                return str(obj).decode(encoding)
            except UnicodeDecodeError:
                # failed to unicode decode, so they get it raw
                return str(obj)