Changeset - a2efbfe84067
[Not reviewed]
beta
0 1 0
Marcin Kuzminski - 14 years ago 2011-09-30 17:11:50
marcin@python-works.com
fixes safe_unicode raise UnicodeDecodeError without any parameters
1 file changed with 10 insertions and 7 deletions:
0 comments (0 inline, 0 general)
rhodecode/lib/__init__.py
Show inline comments
 
@@ -165,30 +165,33 @@ def safe_unicode(str_, from_encoding='ut
 
    by chardet library if it fails fallback to unicode with errors replaced
 

	
 
    :param str_: string to decode
 
    :rtype: unicode
 
    :returns: unicode object
 
    """
 

	
 
    if isinstance(str_, unicode):
 
        return str_
 

	
 
    try:
 
        return unicode(str_)
 
    except UnicodeDecodeError:
 
        pass
 

	
 
    try:
 
        return unicode(str_, from_encoding)
 
    except UnicodeDecodeError:
 
        pass
 
    
 
    try:        
 

	
 
    try:
 
        import chardet
 
        encoding = chardet.detect(str_)['encoding']
 
        if encoding is None:
 
            raise UnicodeDecodeError()
 
        
 
            raise Exception()
 
        return str_.decode(encoding)
 
    except (ImportError, UnicodeDecodeError):
 
        return unicode(str_, from_encoding, 'replace')    
 
    except (ImportError, UnicodeDecodeError, Exception):
 
        return unicode(str_, from_encoding, 'replace')
 

	
 
def safe_str(unicode_, to_encoding='utf8'):
 
    """
 
    safe str function. Does few trick to turn unicode_ into string
 
     
 
    In case of UnicodeEncodeError we try to return it with encoding detected
0 comments (0 inline, 0 general)