- 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.
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 @@
def u(s):
return s
+ def b(s):
+ return s.encode("latin-1")
+
def octal(lit):
return eval("0o" + lit)
@@ -45,6 +48,9 @@
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 @@
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 @@
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("""
<%!