Changeset - 8e76649f11ad
[Not reviewed]
beta
0 1 0
Marcin Kuzminski - 14 years ago 2011-09-29 21:55:32
marcin@python-works.com
fixes safe_str method
- trye directly to encode with given encoding instead of trying to make a string out of the unicode
1 file changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
rhodecode/lib/__init__.py
Show inline comments
 
@@ -168,25 +168,25 @@ def safe_unicode(str_, from_encoding='ut
 
    :rtype: unicode
 
    :returns: unicode object
 
    """
 

	
 
    if isinstance(str_, unicode):
 
        return str_
 

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

	
 
def safe_str(unicode_, to_encoding='utf8'):
 
    """
 
    safe str function. Does few trick to turn unicode_ into string
 
@@ -194,25 +194,25 @@ def safe_str(unicode_, to_encoding='utf8
 
    In case of UnicodeEncodeError we try to return it with encoding detected
 
    by chardet library if it fails fallback to string with errors replaced
 

	
 
    :param unicode_: unicode to encode
 
    :rtype: str
 
    :returns: str object
 
    """
 

	
 
    if isinstance(unicode_, str):
 
        return unicode_
 

	
 
    try:
 
        return str(unicode_)
 
        return unicode_.encode(to_encoding)
 
    except UnicodeEncodeError:
 
        pass
 
    
 
    try:
 
        import chardet
 
        encoding = chardet.detect(unicode_)['encoding']
 
        print encoding
 
        if encoding is None:
 
            raise UnicodeEncodeError()
 
        
 
        return unicode_.encode(encoding)
 
    except (ImportError, UnicodeEncodeError):
0 comments (0 inline, 0 general)