#NEWLINE# def smart_str( s , encoding = 'utf-8' , strings_only = False , errors = 'strict' ) : #NEWLINE# #INDENT# "\n    Returns a bytestring version of 's', encoded as specified in 'encoding'.\n\n    If strings_only is True, don't convert (some) non-string-like objects.\n    " #NEWLINE# if ( strings_only and ( ( s is None ) or isinstance ( s , int ) ) ) : #NEWLINE# #INDENT# return s #NEWLINE# #UNINDENT# if isinstance ( s , Promise ) : #NEWLINE# #INDENT# return unicode ( s ) . encode ( encoding , errors ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not isinstance ( s , basestring ) ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# return str ( s ) #NEWLINE# #UNINDENT# except UnicodeEncodeError : #NEWLINE# #INDENT# if isinstance ( s , Exception ) : #NEWLINE# #INDENT# return ' ' . join ( [ smart_str ( arg , encoding , strings_only , errors ) for arg in s ] ) #NEWLINE# #UNINDENT# return unicode ( s ) . encode ( encoding , errors ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( s , unicode ) : #NEWLINE# #INDENT# return s . encode ( encoding , errors ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( s and ( encoding != 'utf-8' ) ) : #NEWLINE# #INDENT# return s . decode ( 'utf-8' , errors ) . encode ( encoding , errors ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return s