diff --git a/doc/build/changelog.rst b/doc/build/changelog.rst index 6b79b30cc6204af2739575a00f222b8f17c04d38..279a0d571e06a3aabe4a75062e20723a2322ba6d 100644 --- a/doc/build/changelog.rst +++ b/doc/build/changelog.rst @@ -9,6 +9,12 @@ Changelog :version: 0.9.2 :released: + .. change:: + :tags: bug, py3k + + Fixed bug in ``decode.<encoding>`` filter where a non-string object + would not be correctly interpreted in Python 3. + .. change:: :tags: bug, py3k :tickets: 227 diff --git a/mako/filters.py b/mako/filters.py index 369cbc3d7ecd235379d346fec26349f891fe1c58..ab7925f3ef90fa6e16454b250ab9dee336b8d2b2 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(str(x), encoding=key) + return compat.text_type(x) else: return compat.text_type(x, encoding=key) return decode diff --git a/test/test_filters.py b/test/test_filters.py index cf292ede9c9caa1d5015fcc4c519c34bdb167a67..f0c55c6eb9aa4beae0adb2c718aba2d55c4e5e85 100644 --- a/test/test_filters.py +++ b/test/test_filters.py @@ -5,6 +5,7 @@ import unittest from test import TemplateTest, eq_, requires_python_2 from test.util import result_lines, flatten_result from mako.compat import u +from mako import compat class FilterTest(TemplateTest): def test_basic(self): @@ -94,12 +95,20 @@ class FilterTest(TemplateTest): t = Template("""# coding: utf-8 some stuff.... ${x} """, default_filters=['decode.utf8']) - #print t.code eq_( t.render_unicode(x=u("voix m’a réveillé")).strip(), u("some stuff.... voix m’a réveillé") ) + def test_encode_filter_non_str(self): + t = Template("""# coding: utf-8 + some stuff.... ${x} + """, default_filters=['decode.utf8']) + eq_( + t.render_unicode(x=3).strip(), + u("some stuff.... 3") + ) + def test_custom_default(self): t = Template(""" <%!