Skip to content

Commit

Permalink
Allow unicode string literals to appear in template expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdarnell committed Jul 26, 2011
1 parent 6fcc459 commit a0ff0ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tornado/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def __init__(self, template_string, name="<string>", loader=None,
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()
Expand Down
17 changes: 17 additions & 0 deletions tornado/test/template_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ def test_escaping(self):
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):
Expand Down

0 comments on commit a0ff0ed

Please sign in to comment.