Edgewall Software

Ticket #2972: text_to_unicode_r3109.diff

File text_to_unicode_r3109.diff, 3.4 KB (added by cboos, 3 years ago)

Possible fix for the issue; consider trac.ini is an UTF-8 encoded file, or encoded using the system encoding

  • trac/config.py

     
    1919import sys 
    2020 
    2121from trac.core import * 
    22 from trac.util import doctrim 
     22from trac.util import doctrim, text_to_unicode 
    2323 
    2424__all__ = ['IConfigurable', 'ConfigSection', 'ConfigOption', 'Configuration', 
    2525           'ConfigurationError', 'default_dir'] 
     
    211211            if default is None: 
    212212                return self.config._defaults.get((self.name, name), '') 
    213213            return default 
    214         return self.config.parser.get(self.name, name) 
     214        return text_to_unicode(self.config.parser.get(self.name, name)) 
    215215 
    216216    def getbool(self, name, default=None): 
    217217        """Return the value of the specified option as boolean. 
     
    268268        """ 
    269269        if not self.config.parser.has_section(self.name): 
    270270            self.config.parser.add_section(self.name) 
    271         return self.config.parser.set(self.name, name, value) 
     271        return self.config.parser.set(self.name, name, value.decode('utf-8')) 
    272272 
    273273 
    274274def default_dir(name): 
  • trac/web/clearsilver.py

     
    1515# Author: Christopher Lenz <cmlenz@gmx.de> 
    1616 
    1717from trac.core import TracError 
    18 from trac.util import markup 
     18from trac.util import markup, text_to_unicode 
    1919 
    2020 
    2121class HDFWrapper: 
     
    221221            elif isinstance(value, str): 
    222222                if escape: 
    223223                    # Assume UTF-8 here, for backward compatibility reasons 
    224                     set_unicode(prefix, markup.escape(unicode(value, 'utf-8', 
    225                                                               'replace'))) 
     224                    set_unicode(prefix, markup.escape(text_to_unicode(value))) 
    226225                else: 
    227226                    set_str(prefix, value) 
    228227            elif isinstance(value, unicode): 
  • trac/util/__init__.py

     
    7575 
    7676    If no charset is specified or if the decoding fails, then fallback 
    7777    to 'iso-8859-15', which will always work as there will be one Unicode 
    78     character for each byte of the input. 
     78    character for each byte of the input. So this function can also be 
     79    used for arbitrary binary data. 
    7980 
    8081    If that unicode string is later displayed, this might result in some 
    8182    garbled characters, but at least there will be something to show... 
     
    8788    except UnicodeError: 
    8889        return unicode(text, 'iso-8859-15') 
    8990 
     91def text_to_unicode(text): 
     92    """Convert a textual string to `unicode`. 
     93 
     94    This is useful when we don't know anything about the string's encoding, 
     95    but contrary to the `to_unicode` function aboe, we know at least that 
     96    `text` is some kind of text and is not binary data. 
     97 
     98    We first try to encode using UTF-8, and if this fails, we try 
     99    the locale preferred encoding, in 'replace' mode. 
     100    """ 
     101    if isinstance(text, unicode): 
     102        return text 
     103    try: 
     104        return unicode(text, 'utf-8') 
     105    except UnicodeError: 
     106        return unicode(text, locale.getpreferredencoding(), 'replace') 
     107 
    90108def shorten_line(text, maxlen = 75): 
    91109    if len(text or '') < maxlen: 
    92110        return text