def to_unicode(value): try: value = compat.text_type(force_text(value)) except (UnicodeEncodeError, UnicodeDecodeError): value = '(Error decoding value)' except Exception: # in some cases we get a different exception try: value = compat.binary_type(repr(type(value))) except Exception: value = '(Error decoding value)' return value
def to_string(value): try: return compat.binary_type(value.decode('utf-8').encode('utf-8')) except Exception: return to_unicode(value).encode('utf-8')