]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Allow unicode string literals to appear in template expressions.
authorBen Darnell <ben@bendarnell.com>
Tue, 26 Jul 2011 17:50:32 +0000 (10:50 -0700)
committerBen Darnell <ben@bendarnell.com>
Tue, 26 Jul 2011 17:50:32 +0000 (10:50 -0700)
Closes #313.

tornado/template.py
tornado/test/template_test.py

index 8963fd4528bc09d9b12801d512e41b87f3acf2ec..ce83a12e372c1b4348a0e90915627a2aed99f7c1 100644 (file)
@@ -121,7 +121,8 @@ class Template(object):
         self.file = _File(_parse(reader, self))
         self.code = self._generate_python(loader, compress_whitespace)
         try:
-            self.compiled = compile(self.code, "<template %s>" % self.name,
+            self.compiled = compile(escape.to_unicode(self.code),
+                                    "<template %s>" % self.name,
                                     "exec")
         except Exception:
             formatted_code = _format_code(self.code).rstrip()
index 037e3625c6dedd69a3a30ec817732ffc438dfddf..e04c2544acc477eebd839e3bfc5b02ef6d61de8b 100644 (file)
@@ -57,6 +57,23 @@ class TemplateTest(LogTrapTestCase):
         self.assertEqual(Template("{%!").generate(), b("{%"))
         self.assertEqual(Template("{{ 'expr' }} {{!jquery expr}}").generate(),
                          b("expr {{jquery expr}}"))
+
+    def test_unicode_template(self):
+        template = Template(utf8(u"\u00e9"))
+        self.assertEqual(template.generate(), utf8(u"\u00e9"))
+
+    def test_unicode_literal_expression(self):
+        # Unicode literals should be usable in templates.  Note that this
+        # test simulates unicode characters appearing directly in the
+        # template file (with utf8 encoding), i.e. \u escapes would not
+        # be used in the template file itself.
+        if str is unicode:
+            # python 3 needs a different version of this test since
+            # 2to3 doesn't run on template internals
+            template = Template(utf8(u'{{ "\u00e9" }}'))
+        else:
+            template = Template(utf8(u'{{ u"\u00e9" }}'))
+        self.assertEqual(template.generate(), utf8(u"\u00e9"))
         
 
 class AutoEscapeTest(LogTrapTestCase):