1. Mike Bayer
  2. mako

Commits

Mike Bayer  committed 51b2338

- 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.

  • Participants
  • Parent commits 5d032c4
  • Branches master

Comments (0)

Files changed (3)

File mako/compat.py

View file
  • Ignore whitespace
     def u(s):
         return s
 
+    def b(s):
+        return s.encode("latin-1")
+
     def octal(lit):
         return eval("0o" + lit)
 
     def u(s):
         return unicode(s, "utf-8")
 
+    def b(s):
+        return s
+
     def octal(lit):
         return eval("0" + lit)
 

File mako/filters.py

View file
  • Ignore whitespace
             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

File test/test_filters.py

View file
  • Ignore whitespace
             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("""
         <%!