Skip to content

Commit

Permalink
Fix unicode problem on template test
Browse files Browse the repository at this point in the history
Since strings are already unicode by default in p3, unicode_escape was
causing double decoding. Also a broken test was preventing to discover
this fix
  • Loading branch information
serkanyersen committed Sep 2, 2013
1 parent 5a34500 commit 171f87a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/underscore.py
Expand Up @@ -1440,14 +1440,14 @@ def interpolate(matchobj):
if getattr(str, 'decode', False):
key = (matchobj.group(1).decode('string-escape')).strip()
else:
key = (bytes(matchobj.group(1), "utf-8").decode('unicode_escape')).strip()
key = (bytes(matchobj.group(1), "utf-8").decode()).strip()
return "' + str(" + unescape(key) + " or '') + '"

def evaluate(matchobj):
if getattr(str, 'decode', False):
code = (matchobj.group(1).decode('string-escape')).strip()
else:
code = (bytes(matchobj.group(1), "utf-8").decode('unicode_escape')).strip()
code = (bytes(matchobj.group(1), "utf-8").decode()).strip()
if code.startswith("end"):
return "')\n" + indent(-1) + "ns.__p += ('"
elif code.endswith(':'):
Expand All @@ -1461,7 +1461,7 @@ def escape(matchobj):
if getattr(str, 'decode', False):
key = (matchobj.group(1).decode('string-escape')).strip()
else:
key = (bytes(matchobj.group(1), "utf-8").decode('unicode_escape')).strip()
key = (bytes(matchobj.group(1), "utf-8").decode()).strip()
return "' + _.escape(str(" + unescape(key) + " or '')) + '"

source = indent() + 'class closure(object):\n pass' + \
Expand All @@ -1478,7 +1478,7 @@ def escape(matchobj):
if getattr(str, 'decode', False):
source += indent() + 'return ns.__p.decode("string_escape")\n'
else:
source += indent() + 'return bytes(ns.__p, "utf-8").decode("unicode_escape")\n'
source += indent() + 'return bytes(ns.__p, "utf-8").decode()\n'

f = self.create_function(settings.get("variable")
or "obj=None", source)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utility.py
Expand Up @@ -69,7 +69,7 @@ def test_template(self):
sansSemicolonTemplate = _.template("A <% this %> B")
self.assertEqual(sansSemicolonTemplate(), "A B")

backslashTemplate = _.template("<%= thing %> is \\ridanculous")
backslashTemplate = _.template("<%= thing %> is \ridanculous")
self.assertEqual(backslashTemplate({"thing": 'This'}), "This is \ridanculous")

escapeTemplate = _.template('<%= "checked=\\"checked\\"" if a else "" %>')
Expand Down

0 comments on commit 171f87a

Please sign in to comment.