def smart_str(s, encoding='<STR_LIT:utf-8>', errors='<STR_LIT:strict>'):
if not isinstance(s, basestring):<EOL><INDENT>try:<EOL><INDENT>return str(s)<EOL><DEDENT>except UnicodeEncodeError:<EOL><INDENT>if isinstance(s, Exception):<EOL><INDENT>return '<STR_LIT:U+0020>'.join([smart_str(arg, encoding, errors) for arg in s])<EOL><DEDENT>return unicode(s).encode(encoding, errors)<EOL><DEDENT><DEDENT>elif isinstance(s, unicode):<EOL><INDENT>return s.encode(encoding, errors)<EOL><DEDENT>elif s and encoding != '<STR_LIT:utf-8>':<EOL><INDENT>return s.decode('<STR_LIT:utf-8>', errors).encode(encoding, errors)<EOL><DEDENT>else:<EOL><INDENT>return s<EOL><DEDENT>
Returns a bytestring version of 's', encoded as specified in 'encoding'.

If strings_only is True, don't convert (some) non-string-like objects.

from django