diff --git a/rhodecode/lib/__init__.py b/rhodecode/lib/__init__.py --- a/rhodecode/lib/__init__.py +++ b/rhodecode/lib/__init__.py @@ -160,7 +160,7 @@ def generate_api_key(username, salt=None def safe_unicode(_str, from_encoding='utf8'): """ safe unicode function. In case of UnicodeDecode error we try to return - unicode with errors replace + unicode with errors replaceed :param _str: string to decode :rtype: unicode @@ -178,6 +178,28 @@ def safe_unicode(_str, from_encoding='ut return u_str +def safe_str(_unicode, to_encoding='utf8'): + """ + safe str function. In case of UnicodeEncode error we try to return + str with errors replaceed + + :param _unicode: unicode to encode + :rtype: str + :returns: str object + """ + + if isinstance(_unicode, str): + return _unicode + + try: + safe_str = str(_unicode) + except UnicodeEncodeError: + safe_str = _unicode.encode(to_encoding, 'replace') + + return safe_str + + + def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs): """ Custom engine_from_config functions that makes sure we use NullPool for