some more stuff for jinja2
--HG--
branch : trunk
diff --git a/jinja2/compiler.py b/jinja2/compiler.py
index e291f1d..e692184 100644
--- a/jinja2/compiler.py
+++ b/jinja2/compiler.py
@@ -564,9 +564,9 @@
for arg in node.defaults:
self.visit(arg)
self.write(', ')
- self.write('), %r, %r)' % (
- macro_frame.accesses_arguments,
- macro_frame.accesses_caller
+ self.write('), %s, %s)' % (
+ macro_frame.accesses_arguments and '1' or '0',
+ macro_frame.accesses_caller and '1' or '0'
))
def visit_CallBlock(self, node, frame):
@@ -581,7 +581,7 @@
for arg in node.defaults:
self.visit(arg)
self.write(', ')
- self.write('), %r, False)' % call_frame.accesses_arguments)
+ self.write('), %s, 0)' % (call_frame.accesses_arguments and '1' or '0'))
if frame.buffer is None:
self.writeline('yield ', node)
else:
diff --git a/jinja2/environment.py b/jinja2/environment.py
index fb8101b..8458a23 100644
--- a/jinja2/environment.py
+++ b/jinja2/environment.py
@@ -107,7 +107,6 @@
source = self.parse(source, filename)
node = optimize(source, self)
source = generate(node, self, filename)
- print source
if raw:
return source
if isinstance(filename, unicode):
diff --git a/jinja2/nodes.py b/jinja2/nodes.py
index 3b0ac3c..e1857d2 100644
--- a/jinja2/nodes.py
+++ b/jinja2/nodes.py
@@ -389,13 +389,14 @@
"""{{ foo|bar|baz }}"""
fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
- def as_const(self):
- if self.node is None:
+ def as_const(self, obj=None):
+ if self.node is obj is None:
raise Impossible()
filter = self.environment.filters.get(self.name)
if filter is None or getattr(filter, 'contextfilter', False):
raise nodes.Impossible()
- obj = self.node.as_const()
+ if obj is None:
+ obj = self.node.as_const()
args = [x.as_const() for x in self.args]
kwargs = dict(x.as_const() for x in self.kwargs)
if self.dyn_args is not None:
diff --git a/jinja2/optimizer.py b/jinja2/optimizer.py
index bd97fa0..d2550f4 100644
--- a/jinja2/optimizer.py
+++ b/jinja2/optimizer.py
@@ -95,6 +95,32 @@
finally:
context.pop()
+ def visit_FilterBlock(self, node, context):
+ """Try to filter a block at compile time."""
+ node = self.generic_visit(node, context)
+ context.push()
+
+ # check if we can evaluate the wrapper body into a string
+ # at compile time
+ buffer = []
+ for child in node.body:
+ if not isinstance(child, nodes.Output):
+ return node
+ for item in child.optimized_nodes():
+ if isinstance(item, nodes.Node):
+ return node
+ buffer.append(item)
+
+ # now check if we can evaluate the filter at compile time.
+ try:
+ data = node.filter.as_const(u''.join(buffer))
+ except nodes.Impossible:
+ return node
+
+ context.pop()
+ const = nodes.Const(data, lineno=node.lineno)
+ return nodes.Output([const], lineno=node.lineno)
+
def visit_For(self, node, context):
"""Loop unrolling for iterable constant values."""
try: