Fix UnicodeError in HTML output

* Alter `test_unicode_append` so that actual unicode characters outside the ASCII bytestring range are tested.

 * Make sure output of `render` in markup.py is unicode

 * Add wrapper around output of `export_set` in _html.py so that unicode characters are output.
This commit is contained in:
Jim Dalton 2012-05-10 11:14:17 -07:00
parent e8c923d712
commit 21c11b9911
3 changed files with 11 additions and 8 deletions

View File

@ -11,6 +11,7 @@ if sys.version_info[0] > 2:
from tablib.packages import markup3 as markup
else:
from cStringIO import StringIO
import codecs
from tablib.packages import markup
import tablib
@ -45,7 +46,9 @@ def export_set(dataset):
page.table.close()
stream.writelines(str(page))
# Allow unicode characters in output
wrapper = codecs.getwriter("utf8")(stream)
wrapper.writelines(unicode(page))
return stream.getvalue()

View File

@ -67,7 +67,7 @@ class element:
def render( self, tag, single, between, kwargs ):
"""Append the actual tags to content."""
out = "<%s" % tag
out = u"<%s" % tag
for key, value in kwargs.iteritems( ):
if value is not None: # when value is None that means stuff like <... checked>
key = key.strip('_') # strip this so class_ will mean class, etc.
@ -75,16 +75,16 @@ class element:
key = 'http-equiv'
elif key == 'accept_charset':
key = 'accept-charset'
out = "%s %s=\"%s\"" % ( out, key, escape( value ) )
out = u"%s %s=\"%s\"" % ( out, key, escape( value ) )
else:
out = "%s %s" % ( out, key )
out = u"%s %s" % ( out, key )
if between is not None:
out = "%s>%s</%s>" % ( out, between, tag )
out = u"%s>%s</%s>" % ( out, between, tag )
else:
if single:
out = "%s />" % out
out = u"%s />" % out
else:
out = "%s>" % out
out = u"%s>" % out
if self.parent is not None:
self.parent.content.append( out )
else:

View File

@ -308,7 +308,7 @@ class TablibTestCase(unittest.TestCase):
def test_unicode_append(self):
"""Passes in a single unicode charecter and exports."""
new_row = ('å', 'é')
new_row = (u'å', u'é')
data.append(new_row)
data.json