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
(s, encoding='utf-8', errors='strict')
| 80 | |
| 81 | |
| 82 | def smart_str(s, encoding='utf-8', errors='strict'): |
| 83 | """ |
| 84 | Returns a bytestring version of 's', encoded as specified in 'encoding'. |
| 85 | |
| 86 | If strings_only is True, don't convert (some) non-string-like objects. |
| 87 | |
| 88 | from django |
| 89 | """ |
| 90 | if not isinstance(s, basestring): |
| 91 | try: |
| 92 | return str(s) |
| 93 | except UnicodeEncodeError: |
| 94 | if isinstance(s, Exception): |
| 95 | # An Exception subclass containing non-ASCII data that doesn't |
| 96 | # know how to print itself properly. We shouldn't raise a |
| 97 | # further exception. |
| 98 | return ' '.join(smart_str(arg, encoding, errors) for arg in s) |
| 99 | return unicode(s).encode(encoding, errors) |
| 100 | elif isinstance(s, unicode): |
| 101 | return s.encode(encoding, errors) |
| 102 | elif s and encoding != 'utf-8': |
| 103 | return s.decode('utf-8', errors).encode(encoding, errors) |
| 104 | else: |
| 105 | return s |
| 106 | |
| 107 | |
| 108 | class WebFault(Exception): |