##// END OF EJS Templates
simplified safe_unicode function
marcink -
r1222:2c246d24 beta
parent child Browse files
Show More
@@ -46,23 +46,22 b' def generate_api_key(username, salt=None'
46
46
47 return hashlib.sha1(username + salt).hexdigest()
47 return hashlib.sha1(username + salt).hexdigest()
48
48
49 def safe_unicode(_str):
49 def safe_unicode(_str, from_encoding='utf8'):
50 """
50 """
51 safe unicode function. In case of UnicodeDecode error we try to return
51 safe unicode function. In case of UnicodeDecode error we try to return
52 unicode with errors replace, if this fails we return unicode with
52 unicode with errors replace
53 string_escape decoding
53
54 :param _str: string to decode
55 :rtype: unicode
56 :returns: unicode object
54 """
57 """
55
58
56 if isinstance(_str, unicode):
59 if isinstance(_str, unicode):
57 return _str
60 return _str
58
61
59 try:
62 try:
60 u_str = unicode(_str)
63 u_str = unicode(_str, from_encoding)
61 except UnicodeDecodeError:
64 except UnicodeDecodeError:
62 try:
65 u_str = unicode(_str, from_encoding, 'replace')
63 u_str = _str.decode('utf-8', 'replace')
64 except UnicodeDecodeError:
65 #incase we have a decode error just represent as byte string
66 u_str = unicode(_str.encode('string_escape'))
67
66
68 return u_str
67 return u_str
General Comments 0
You need to be logged in to leave comments. Login now