#NEWLINE# def decode_text( text , is_html = False , guess_charset = True , try_common_charsets = True , charsets = None , fallback_charset = 'utf-8' ) : #NEWLINE# #INDENT# if ( not isinstance ( text , bytes ) ) : #NEWLINE# #INDENT# return ( text , None ) #NEWLINE# #UNINDENT# _charsets = [ ] #NEWLINE# if guess_charset : #NEWLINE# #INDENT# c = guess_text_charset ( text , is_html = is_html ) #NEWLINE# if c : #NEWLINE# #INDENT# _charsets . append ( c ) #NEWLINE# #UNINDENT# #UNINDENT# if charsets : #NEWLINE# #INDENT# _charsets . extend ( c ) #NEWLINE# #UNINDENT# if try_common_charsets : #NEWLINE# #INDENT# _charsets . extend ( COMMON_CHARSETS ) #NEWLINE# #UNINDENT# if fallback_charset : #NEWLINE# #INDENT# _charsets . append ( fallback_charset ) #NEWLINE# #UNINDENT# _last_exc = None #NEWLINE# for enc in _charsets : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# return ( to_unicode ( text , charset = enc ) , enc ) #NEWLINE# #UNINDENT# except UnicodeDecodeError as exc : #NEWLINE# #INDENT# _last_exc = exc #NEWLINE# #UNINDENT# #UNINDENT# raise _last_exc