# HG changeset patch # User Marcin Kuzminski # Date 2019-11-29 12:16:56 # Node ID 19a6ab7e5b4d4dd06cbe8103b4f2922a0c6b3472 # Parent eaa058196492eb34669ce85713cc1505366a6247 helpers: unicode/str add flag to control usage of chardet. - in case it's somehow installed we still want to control the behaviour via explicit flag. diff --git a/rhodecode/lib/utils2.py b/rhodecode/lib/utils2.py --- a/rhodecode/lib/utils2.py +++ b/rhodecode/lib/utils2.py @@ -207,7 +207,7 @@ def safe_int(val, default=None): return val -def safe_unicode(str_, from_encoding=None): +def safe_unicode(str_, from_encoding=None, use_chardet=False): """ safe unicode function. Does few trick to turn str_ into unicode @@ -240,17 +240,19 @@ def safe_unicode(str_, from_encoding=Non except UnicodeDecodeError: pass - try: - import chardet - encoding = chardet.detect(str_)['encoding'] - if encoding is None: - raise Exception() - return str_.decode(encoding) - except (ImportError, UnicodeDecodeError, Exception): + if use_chardet: + try: + import chardet + encoding = chardet.detect(str_)['encoding'] + if encoding is None: + raise Exception() + return str_.decode(encoding) + except (ImportError, UnicodeDecodeError, Exception): + return unicode(str_, from_encoding[0], 'replace') + else: return unicode(str_, from_encoding[0], 'replace') - -def safe_str(unicode_, to_encoding=None): +def safe_str(unicode_, to_encoding=None, use_chardet=False): """ safe str function. Does few trick to turn unicode_ into string @@ -283,14 +285,17 @@ def safe_str(unicode_, to_encoding=None) except UnicodeEncodeError: pass - try: - import chardet - encoding = chardet.detect(unicode_)['encoding'] - if encoding is None: - raise UnicodeEncodeError() + if use_chardet: + try: + import chardet + encoding = chardet.detect(unicode_)['encoding'] + if encoding is None: + raise UnicodeEncodeError() - return unicode_.encode(encoding) - except (ImportError, UnicodeEncodeError): + return unicode_.encode(encoding) + except (ImportError, UnicodeEncodeError): + return unicode_.encode(to_encoding[0], 'replace') + else: return unicode_.encode(to_encoding[0], 'replace')