# HG changeset patch # User Marcin Kuzminski # Date 1302114351 -7200 # Node ID 2c246d24eb90fb805a13e6e159c3e9f3006e962b # Parent e4784e2b03f7d07d4f99965a1257ecca749a1a8b simplified safe_unicode function diff -r e4784e2b03f7 -r 2c246d24eb90 rhodecode/lib/__init__.py --- 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