[FIX] report.interface: toxml() should return unicode to avoid any encoding issue propagation

It used to return utf-8 data but was brittle in the conversion, and would fail downstream
if the calling code did not pay attention to handle utf-8 data properly.
Returning unicode forces callers to auto-cast to unicode for any string operation, saving
potential trouble later. 
From the addons that currently call toxml(), none use it in a manner that would fail in case
unicode data is returned, so we should be on the safe side there too.

lp bug: https://launchpad.net/bugs/695960 fixed

bzr revid: odo@openerp.com-20110117184444-mxjeneuoa0r5a85n
This commit is contained in:
Olivier Dony 2011-01-17 19:44:44 +01:00
parent 6cd9f64973
commit 3d4d87b718
1 changed files with 5 additions and 9 deletions

View File

@ -33,16 +33,12 @@ import render
import urllib
#
# encode a value to a string in utf8 and converts XML entities
# coerce any type to a unicode string (to preserve non-ascii characters)
# and escape XML entities
#
def toxml(val):
if isinstance(val, str):
str_utf8 = val
elif isinstance(val, unicode):
str_utf8 = val.encode('utf-8')
else:
str_utf8 = str(val)
return str_utf8.replace('&', '&amp;').replace('<','&lt;').replace('>','&gt;')
def toxml(value):
unicode_value = tools.ustr(value)
return unicode_value.replace('&', '&amp;').replace('<','&lt;').replace('>','&gt;')
class report_int(netsvc.Service):
def __init__(self, name, audience='*'):