##// END OF EJS Templates
fixes safe_unicode raise UnicodeDecodeError without any parameters
marcink -
r1502:a2efbfe8 beta
parent child Browse files
Show More
@@ -168,24 +168,27 b" def safe_unicode(str_, from_encoding='ut"
168 :rtype: unicode
168 :rtype: unicode
169 :returns: unicode object
169 :returns: unicode object
170 """
170 """
171
172 if isinstance(str_, unicode):
171 if isinstance(str_, unicode):
173 return str_
172 return str_
174
173
175 try:
174 try:
175 return unicode(str_)
176 except UnicodeDecodeError:
177 pass
178
179 try:
176 return unicode(str_, from_encoding)
180 return unicode(str_, from_encoding)
177 except UnicodeDecodeError:
181 except UnicodeDecodeError:
178 pass
182 pass
179
183
180 try:
184 try:
181 import chardet
185 import chardet
182 encoding = chardet.detect(str_)['encoding']
186 encoding = chardet.detect(str_)['encoding']
183 if encoding is None:
187 if encoding is None:
184 raise UnicodeDecodeError()
188 raise Exception()
185
186 return str_.decode(encoding)
189 return str_.decode(encoding)
187 except (ImportError, UnicodeDecodeError):
190 except (ImportError, UnicodeDecodeError, Exception):
188 return unicode(str_, from_encoding, 'replace')
191 return unicode(str_, from_encoding, 'replace')
189
192
190 def safe_str(unicode_, to_encoding='utf8'):
193 def safe_str(unicode_, to_encoding='utf8'):
191 """
194 """
General Comments 0
You need to be logged in to leave comments. Login now