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