def safe_decode(text, incoming=None, errors='strict'):
 if (not isinstance(text, (six.string_types, six.binary_type))):
  raise TypeError(("%s can't be decoded" % type(text)))
 if isinstance(text, six.text_type):
  return text
 if (not incoming):
  incoming = (sys.stdin.encoding or sys.getdefaultencoding())
 try:
  return text.decode(incoming, errors)
 except UnicodeDecodeError:
  return text.decode('utf-8', errors)

def safe_decode ( text , incoming = None , errors = 'strict' ) : if ( not isinstance ( text , ( six . string_types , six . binary_type ) ) ) : raise TypeError ( ( "%s can't be decoded" % type ( text ) ) ) if isinstance ( text , six . text_type ) : return text if ( not incoming ) : incoming = ( sys . stdin . encoding or sys . getdefaultencoding ( ) ) try : return text . decode ( incoming , errors ) except UnicodeDecodeError : return text . decode ( 'utf-8' , errors )
safely decodes a binary string to unicode .