From 98c5a0c0d6ae69b714adb422bebe1301458ded17 Mon Sep 17 00:00:00 2001 From: Mike Bayer <mike_mp@zzzcomputing.com> Date: Wed, 3 Jan 2007 04:58:39 +0000 Subject: [PATCH] - fix to expression filters so that string conversion (actually unicode) properly occurs before filtering - removed encoding() filter - it conflicts with the fact that unicode conversion has to occur on the value first before being sent to the filter. recommended way for encoded strings is to just say unicode(x, encoding='whatever') --- CHANGES | 5 ++--- lib/mako/codegen.py | 4 ++-- lib/mako/filters.py | 4 ---- test/filters.py | 11 +++++------ test_htdocs/read_unicode.html | 2 +- 5 files changed, 10 insertions(+), 16 deletions(-) diff --git a/CHANGES b/CHANGES index 5eee6da..56fb1fa 100644 --- a/CHANGES +++ b/CHANGES @@ -1,11 +1,10 @@ 0.1.1 - AST parsing fixes: fixed TryExcept identifier parsing -- added "encoding()" filter; this allows a filter expression to specify the encoding and error -handling for the given expression. -usage is like: ${data | encoding('utf-8', errors='strict')} - removed textmate tmbundle from contrib and into separate SVN location; windows users cant handle those files, setuptools not very good at "pruning" certain directories - fix so that "cache_timeout" parameter is propigated +- fix to expression filters so that string conversion (actually unicode) properly +occurs before filtering 0.1.0 diff --git a/lib/mako/codegen.py b/lib/mako/codegen.py index 7396769..efb67cc 100644 --- a/lib/mako/codegen.py +++ b/lib/mako/codegen.py @@ -398,8 +398,8 @@ class _GenerateRenderMethod(object): def visitExpression(self, node): self.write_source_comment(node) if len(node.escapes) or (self.compiler.pagetag is not None and len(self.compiler.pagetag.filter_args.args)): - s = self.create_filter_callable(node.escapes_code.args, node.text) - self.printer.writeline("context.write(unicode(%s))" % s) + s = self.create_filter_callable(node.escapes_code.args, "unicode(%s)" % node.text) + self.printer.writeline("context.write(%s)" % s) else: self.printer.writeline("context.write(unicode(%s))" % node.text) diff --git a/lib/mako/filters.py b/lib/mako/filters.py index b6fdb8e..ff6332e 100644 --- a/lib/mako/filters.py +++ b/lib/mako/filters.py @@ -38,9 +38,6 @@ def url_unescape(string): def trim(string): return string.strip() -def encoding(encoding, errors='strict'): - return lambda x:unicode(x, encoding=encoding, errors=errors) - _ASCII_re = re.compile(r'\A[\x00-\x7f]*\Z') def is_ascii_str(text): @@ -150,7 +147,6 @@ DEFAULT_ESCAPES = { 'u':url_escape, 'trim':trim, 'entity':html_entities_escape, - 'encoding':encoding } diff --git a/test/filters.py b/test/filters.py index 0e77c1c..8033ee4 100644 --- a/test/filters.py +++ b/test/filters.py @@ -20,14 +20,13 @@ class FilterTest(unittest.TestCase): return lambda x: "MYFILTER->%s<-%s" % (x, y) assert flatten_result(t.render(x="this is x", myfilter=myfilter, y="this is y")) == "MYFILTER->this is x<-this is y" - def test_builtin_func(self): + def test_convert_str(self): + """test that string conversion happens in expressions before sending to filters""" t = Template(""" - ${x | encoding('utf-8')} + ${x | trim} """) - udata = u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" - utf8data = udata.encode('utf-8') - assert flatten_result(t.render_unicode(x=utf8data)) == udata - + assert flatten_result(t.render(x=5)) == "5" + def test_def(self): t = Template(""" <%def name="foo()" filter="myfilter"> diff --git a/test_htdocs/read_unicode.html b/test_htdocs/read_unicode.html index aeb7281..50f00c3 100644 --- a/test_htdocs/read_unicode.html +++ b/test_htdocs/read_unicode.html @@ -7,4 +7,4 @@ doc_content = ''.join(file_content.readlines()) file_content.close() %> -${doc_content | encoding('utf-8')} +${unicode(doc_content, encoding='utf-8')} -- GitLab