Revision dd1089b7
b/snf-common/synnefo/util/text.py | ||
---|---|---|
90 | 90 |
|
91 | 91 |
return str(thing) |
92 | 92 |
|
93 |
|
|
94 |
def udec(thing, encoding=None): |
|
95 |
"""Decode the argument into a unicode object. |
|
96 |
|
|
97 |
If the thing is already a unicode object, nothing happens to it. |
|
98 |
If it is a string object, it is decoded into a unicode object as |
|
99 |
specified by the encoding argument. |
|
100 |
If the thing is another type of object, str() is called on it. |
|
101 |
|
|
102 |
If the encoding argument is not specified, the module's default |
|
103 |
is used instead. See uenc_set_encoding() for setting the default. |
|
104 |
|
|
105 |
""" |
|
106 |
if encoding is None: |
|
107 |
encoding = uenc_encoding |
|
108 |
|
|
109 |
if isinstance(thing, unicode): |
|
110 |
return thing |
|
111 |
|
|
112 |
try: |
|
113 |
return thing.decode(encoding) |
|
114 |
except UnicodeDecodeError: |
|
115 |
return repr(thing.decode('ISO-8859-1')) |
|
116 |
|
|
117 |
|
|
93 | 118 |
uenc_set_encoding() |
Also available in: Unified diff