Ticket #2972: text_to_unicode_r3109.diff
| File text_to_unicode_r3109.diff, 3.4 KB (added by cboos, 3 years ago) |
|---|
-
trac/config.py
19 19 import sys 20 20 21 21 from trac.core import * 22 from trac.util import doctrim 22 from trac.util import doctrim, text_to_unicode 23 23 24 24 __all__ = ['IConfigurable', 'ConfigSection', 'ConfigOption', 'Configuration', 25 25 'ConfigurationError', 'default_dir'] … … 211 211 if default is None: 212 212 return self.config._defaults.get((self.name, name), '') 213 213 return default 214 return self.config.parser.get(self.name, name)214 return text_to_unicode(self.config.parser.get(self.name, name)) 215 215 216 216 def getbool(self, name, default=None): 217 217 """Return the value of the specified option as boolean. … … 268 268 """ 269 269 if not self.config.parser.has_section(self.name): 270 270 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')) 272 272 273 273 274 274 def default_dir(name): -
trac/web/clearsilver.py
15 15 # Author: Christopher Lenz <cmlenz@gmx.de> 16 16 17 17 from trac.core import TracError 18 from trac.util import markup 18 from trac.util import markup, text_to_unicode 19 19 20 20 21 21 class HDFWrapper: … … 221 221 elif isinstance(value, str): 222 222 if escape: 223 223 # 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))) 226 225 else: 227 226 set_str(prefix, value) 228 227 elif isinstance(value, unicode): -
trac/util/__init__.py
75 75 76 76 If no charset is specified or if the decoding fails, then fallback 77 77 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. 79 80 80 81 If that unicode string is later displayed, this might result in some 81 82 garbled characters, but at least there will be something to show... … … 87 88 except UnicodeError: 88 89 return unicode(text, 'iso-8859-15') 89 90 91 def 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 90 108 def shorten_line(text, maxlen = 75): 91 109 if len(text or '') < maxlen: 92 110 return text
