MCPcopy Create free account
hub / github.com/ronreiter/interactive-tutorials / smart_str

Function smart_str

suds/__init__.py:82–105  ·  view source on GitHub ↗

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')

Source from the content-addressed store, hash-verified

80
81
82def 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
108class WebFault(Exception):

Callers 1

__init__Method · 0.85

Calls 2

decodeMethod · 0.80
encodeMethod · 0.45

Tested by

no test coverage detected