changeset 1222:2c246d24eb90 beta

simplified safe_unicode function
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 06 Apr 2011 20:25:51 +0200
parents e4784e2b03f7
children f7b24987d5fb
files rhodecode/lib/__init__.py
diffstat 1 files changed, 8 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/__init__.py	Wed Apr 06 20:16:02 2011 +0200
+++ b/rhodecode/lib/__init__.py	Wed Apr 06 20:25:51 2011 +0200
@@ -46,23 +46,22 @@
 
     return hashlib.sha1(username + salt).hexdigest()
 
-def safe_unicode(_str):
+def safe_unicode(_str, from_encoding='utf8'):
     """
     safe unicode function. In case of UnicodeDecode error we try to return
-    unicode with errors replace, if this fails we return unicode with
-    string_escape decoding
+    unicode with errors replace
+    
+    :param _str: string to decode
+    :rtype: unicode
+    :returns: unicode object
     """
 
     if isinstance(_str, unicode):
         return _str
 
     try:
-        u_str = unicode(_str)
+        u_str = unicode(_str, from_encoding)
     except UnicodeDecodeError:
-        try:
-            u_str = _str.decode('utf-8', 'replace')
-        except UnicodeDecodeError:
-            #incase we have a decode error just represent as byte string
-            u_str = unicode(_str.encode('string_escape'))
+        u_str = unicode(_str, from_encoding, 'replace')
 
     return u_str