def _smart_str(s): """ Returns a bytestring version of 's', encoded as specified in 'encoding'. If strings_only is True, don't convert (some) non-string-like objects. This function was borrowed from Django. """ if not isinstance(s, string_type): try: return bytes_(s) except UnicodeEncodeError: if isinstance(s, Exception): # An Exception subclass containing non-ASCII data that doesn't # know how to print itself properly. We shouldn't raise a # further exception. return ' '.join([ _smart_str(arg).decode('utf-8') for arg in s.args ]).encode('utf-8', 'strict') return unicode_text(s).encode('utf-8', 'strict') elif isinstance(s, unicode_text): return s.encode('utf-8', 'strict') else: return s
def _smart_str(s): """ Returns a bytestring version of 's', encoded as specified in 'encoding'. This function was borrowed from Django. """ if isinstance(s, byte_string): return s elif isinstance(s, unicode_text): return s.encode('utf-8', 'strict') else: try: return bytes_(s) except UnicodeEncodeError: if isinstance(s, Exception): # An Exception subclass containing non-ASCII data that doesn't # know how to print itself properly. We shouldn't raise a # further exception. return ' '.join([_smart_str(arg).decode('utf-8') for arg in s.args]).encode('utf-8', 'strict') return unicode_text(s).encode('utf-8', 'strict')
def _smart_str(s): """ Returns a bytestring version of 's', encoded as specified in 'encoding'. If strings_only is True, don't convert (some) non-string-like objects. This function was borrowed from Django. """ if not isinstance(s, string_type): try: return bytes_(s) except UnicodeEncodeError: if isinstance(s, Exception): # An Exception subclass containing non-ASCII data that doesn't # know how to print itself properly. We shouldn't raise a # further exception. return " ".join([_smart_str(arg).decode("utf-8") for arg in s.args]).encode("utf-8", "strict") return unicode_text(s).encode("utf-8", "strict") elif isinstance(s, unicode_text): return s.encode("utf-8", "strict") else: return s