From: Christoph Hack Date: Tue, 8 Apr 2008 14:48:30 +0000 (+0200) Subject: optimize filters on constant values X-Git-Tag: 2.0rc1~208 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f4fdc72befcf4855d60aa9d8fa5a81f9334c9ae5;p=jinja2.git optimize filters on constant values --HG-- branch : trunk --- diff --git a/jinja2/compiler.py b/jinja2/compiler.py index 43cb439..a1e8783 100644 --- a/jinja2/compiler.py +++ b/jinja2/compiler.py @@ -530,13 +530,22 @@ class CodeGenerator(NodeVisitor): self.visit(node.step, frame) def visit_Filter(self, node, frame): - for filter in node.filters: + value = node.node + flen = len(node.filters) + if isinstance(value, nodes.Const): + # try to optimize filters on constant values + for filter in reversed(node.filters): + value = nodes.Const(self.environment.filters \ + .get(filter.name)(self.environment, value.value)) + print value + flen -= 1 + for filter in node.filters[:flen]: if filter.name in frame.identifiers.declared_filter: self.write('f_%s(' % filter.name) else: self.write('context.filter[%r](' % filter.name) - self.visit(node.node, frame) - for filter in reversed(node.filters): + self.visit(value, frame) + for filter in reversed(node.filters[:flen]): self.signature(filter, frame) self.write(')') diff --git a/jinja2/defaults.py b/jinja2/defaults.py index 37473ea..84c1b08 100644 --- a/jinja2/defaults.py +++ b/jinja2/defaults.py @@ -8,7 +8,7 @@ :copyright: 2007 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ -from jinja.filters import FILTERS as DEFAULT_FILTERS +from jinja2.filters import FILTERS as DEFAULT_FILTERS from jinja.tests import TESTS as DEFAULT_TESTS DEFAULT_NAMESPACE = {} diff --git a/jinja2/filters.py b/jinja2/filters.py index 6098b6e..4518822 100644 --- a/jinja2/filters.py +++ b/jinja2/filters.py @@ -15,6 +15,7 @@ try: except ImportError: itemgetter = lambda a: lambda b: b[a] from urllib import urlencode, quote +from jinja.utils import escape _striptags_re = re.compile(r'(|<[^>]*>)') @@ -94,18 +95,16 @@ def do_upper(s): Convert a value to uppercase. """ return s.upper() -do_upper = stringfilter(do_upper) -def do_lower(s): +def do_lower(env, s): """ Convert a value to lowercase. """ return s.lower() -do_lower = stringfilter(do_lower) -def do_escape(attribute=False): +def do_escape(env, s, attribute=False): """ XML escape ``&``, ``<``, and ``>`` in a string of data. If the optional parameter is `true` this filter will also convert @@ -114,20 +113,12 @@ def do_escape(attribute=False): This method will have no effect it the value is already escaped. """ - #: because filters are cached we can make a local alias to - #: speed things up a bit - e = escape - def wrapped(env, context, s): - if isinstance(s, TemplateData): - return s - elif hasattr(s, '__html__'): - return s.__html__() - #: small speedup, do not convert to unicode if we already - #: have an unicode object. - if s.__class__ is not unicode: - s = env.to_unicode(s) - return e(s, attribute) - return wrapped + # XXX: Does this still exists? + #if isinstance(s, TemplateData): + # return s + if hasattr(s, '__html__'): + return s.__html__() + return escape(unicode(s), attribute) def do_xmlattr(autospace=False): diff --git a/test.py b/test.py index 68ed7b8..186959a 100644 --- a/test.py +++ b/test.py @@ -4,13 +4,7 @@ from jinja2.compiler import generate env = Environment() ast = env.parse(""" -{% (a, b), c = foo() %} -{% macro foo(a, b, c=42) %} - 42 {{ arguments }} -{% endmacro %} -{% block body %} - {% bar = 23 %} -{% endblock %} +Hallo {{ name|e }}!, wie geht es {{ ""|e|lower }}? """) print ast print