changeset 512:bdaf75981ec7

Fix for #125 (text template handling unicode source). Thanks to Christian Boos for the patch.
author cmlenz
date Wed, 06 Jun 2007 11:31:44 +0000
parents ca7d707d51b0
children e293bbb40507
files ChangeLog genshi/template/tests/text.py genshi/template/text.py
diffstat 3 files changed, 9 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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:
 
--- 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
 
Copyright (C) 2012-2017 Edgewall Software