# HG changeset patch # User cmlenz # Date 1181129504 0 # Node ID ed9f1300f3bfd8a7c9d6f2cde6c465360372d1dc # Parent 1bdccd3bda0001da812d10b9c5a0713215fdef06 Fix for #125 (text template handling unicode source). Thanks to Christian Boos for the patch. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -16,6 +16,7 @@ of the doctype as string, in addition to the `(name, pubid, sysid)` tuple. * The I18n filter was not replacing the original attributes with the translation, but instead adding a second attribute with the same name. + * `TextTemplate` can now handle unicode source (ticket #125). Version 0.4.1 diff --git a/genshi/template/tests/text.py b/genshi/template/tests/text.py --- a/genshi/template/tests/text.py +++ b/genshi/template/tests/text.py @@ -54,6 +54,11 @@ tmpl = TextTemplate(text, encoding='iso-8859-1') self.assertEqual(u'x\xf6y', unicode(tmpl.generate(foo='x', bar='y'))) + def test_unicode_input(self): + text = u'$foo\xf6$bar' + tmpl = TextTemplate(text) + self.assertEqual(u'x\xf6y', unicode(tmpl.generate(foo='x', bar='y'))) + def test_empty_lines1(self): tmpl = TextTemplate("""Your items: diff --git a/genshi/template/text.py b/genshi/template/text.py --- a/genshi/template/text.py +++ b/genshi/template/text.py @@ -64,10 +64,10 @@ stream = [] # list of events of the "compiled" template dirmap = {} # temporary mapping of directives to elements depth = 0 - if not encoding: - encoding = 'utf-8' - source = source.read().decode(encoding, 'replace') + source = source.read() + if isinstance(source, str): + source = source.decode(encoding or 'utf-8', 'replace') offset = 0 lineno = 1