summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-04-21 00:27:17 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2014-04-21 00:27:17 -0400
commit51b233818d06942c5866d047d6f76324e45d550e (patch)
tree19e382d75de97166569fddd7b3a2460b3ffca2cf
parent5d032c477f68e2a2fd7dc4d36377686fed024c56 (diff)
downloadmako-51b233818d06942c5866d047d6f76324e45d550e.tar.gz
- add addtional test, py2K only, for when and object's __str__() method returns
non-ASCII bytes. This is invalid on py3K as __str__() must return a string. send the object through the function recursively when we get the __str__() value.
-rw-r--r--mako/compat.py6
-rw-r--r--mako/filters.py2
-rw-r--r--test/test_filters.py13
3 files changed, 20 insertions, 1 deletions
diff --git a/mako/compat.py b/mako/compat.py
index c5ef84b..82b6c05 100644
--- a/mako/compat.py
+++ b/mako/compat.py
@@ -24,6 +24,9 @@ if py3k:
def u(s):
return s
+ def b(s):
+ return s.encode("latin-1")
+
def octal(lit):
return eval("0o" + lit)
@@ -45,6 +48,9 @@ else:
def u(s):
return unicode(s, "utf-8")
+ def b(s):
+ return s
+
def octal(lit):
return eval("0" + lit)
diff --git a/mako/filters.py b/mako/filters.py
index ab7925f..af7abc2 100644
--- a/mako/filters.py
+++ b/mako/filters.py
@@ -64,7 +64,7 @@ class Decode(object):
if isinstance(x, compat.text_type):
return x
elif not isinstance(x, compat.binary_type):
- return compat.text_type(x)
+ return decode(str(x))
else:
return compat.text_type(x, encoding=key)
return decode
diff --git a/test/test_filters.py b/test/test_filters.py
index f0c55c6..5bd9766 100644
--- a/test/test_filters.py
+++ b/test/test_filters.py
@@ -109,6 +109,19 @@ class FilterTest(TemplateTest):
u("some stuff.... 3")
)
+ @requires_python_2
+ def test_encode_filter_non_str_we_return_bytes(self):
+ class Foo(object):
+ def __str__(self):
+ return compat.b("å")
+ t = Template("""# coding: utf-8
+ some stuff.... ${x}
+ """, default_filters=['decode.utf8'])
+ eq_(
+ t.render_unicode(x=Foo()).strip(),
+ u("some stuff.... å")
+ )
+
def test_custom_default(self):
t = Template("""
<%!