Permalink
Browse files

fixed a bug in extension handling

--HG--
branch : trunk
  • Loading branch information...
1 parent d1ff858 commit 27069d73ec6cc9bbf6ce55f6ef48bafa825508d8 @mitsuhiko mitsuhiko committed May 11, 2008
Showing with 58 additions and 7 deletions.
  1. +2 −4 docs/cache_extension.py
  2. +1 −1 docs/extensions.rst
  3. +39 −0 jinja2-debug.py
  4. +2 −1 jinja2/compiler.py
  5. +3 −1 jinja2/environment.py
  6. +11 −0 jinja2/ext.py
View
@@ -38,10 +38,8 @@ def parse(self, parser):
# now return a `CallBlock` node that calls our _cache_support
# helper method on this extension.
- return nodes.CallBlock(
- nodes.Call(self.attr('_cache_support'), args, [], None, None),
- [], [], body
- ).set_lineno(lineno)
+ return nodes.CallBlock(self.call_method('_cache_support', args),
+ [], [], body).set_lineno(lineno)
def _cache_support(self, name, timeout, caller):
"""Helper callback."""
View
@@ -134,7 +134,7 @@ Extension API
Extensions always have to extend the :class:`jinja2.ext.Extension` class:
.. autoclass:: Extension
- :members: parse, attr
+ :members: parse, attr, call_method
.. attribute:: identifier
View
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+ Jinja2 Debug Interface
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ Helper script for internal Jinja2 debugging. Requires Werkzeug.
+
+ :copyright: Copyright 2008 by Armin Ronacher.
+ :license: BSD.
+"""
+import sys
+import jinja2
+from werkzeug import script
+
+env = jinja2.Environment()
+
+def shell_init_func():
+ def _compile(x):
+ print env.compile(x, raw=True)
+ result = {
+ 'e': env,
+ 'c': _compile,
+ 't': env.from_string,
+ 'p': env.parse
+ }
+ for key in jinja2.__all__:
+ result[key] = getattr(jinja2, key)
+ return result
+
+
+def action_compile():
+ print env.compile(sys.stdin.read(), raw=True)
+
+action_shell = script.make_shell(shell_init_func)
+
+
+if __name__ == '__main__':
+ script.run()
View
@@ -719,7 +719,8 @@ def visit_Extends(self, node, frame):
# if this extends statement was in the root level we can take
# advantage of that information and simplify the generated code
# in the top level from this point onwards
- self.has_known_extends = True
+ if frame.rootlevel:
+ self.has_known_extends = True
# and now we have one more
self.extends_so_far += 1
View
@@ -15,7 +15,6 @@
from jinja2.optimizer import optimize
from jinja2.compiler import generate
from jinja2.runtime import Undefined, Context
-from jinja2.debug import translate_exception, translate_syntax_error
from jinja2.exceptions import TemplateSyntaxError
from jinja2.utils import import_string, LRUCache, Markup, missing, concat
@@ -299,6 +298,7 @@ def parse(self, source, filename=None):
try:
return Parser(self, source, filename).parse()
except TemplateSyntaxError, e:
+ from jinja2.debug import translate_syntax_error
exc_type, exc_value, tb = translate_syntax_error(e)
raise exc_type, exc_value, tb
@@ -486,6 +486,7 @@ def render(self, *args, **kwargs):
try:
return concat(self._generate(*args, **kwargs))
except:
+ from jinja2.debug import translate_exception
exc_type, exc_value, tb = translate_exception(sys.exc_info())
raise exc_type, exc_value, tb
@@ -507,6 +508,7 @@ def generate(self, *args, **kwargs):
for item in self._generate(*args, **kwargs):
yield item
except:
+ from jinja2.debug import translate_exception
exc_type, exc_value, tb = translate_exception(sys.exc_info())
raise exc_type, exc_value, tb
View
@@ -73,6 +73,7 @@ def parse(self, parser):
is the name token that matched. This method has to return one or a
list of multiple nodes.
"""
+ raise NotImplementedError()
def attr(self, name, lineno=None):
"""Return an attribute node for the current extension. This is useful
@@ -84,6 +85,16 @@ def attr(self, name, lineno=None):
"""
return nodes.ExtensionAttribute(self.identifier, name, lineno=lineno)
+ def call_method(self, name, args=None, kwargs=None, dyn_args=None,
+ dyn_kwargs=None, lineno=None):
+ """Call a method of the extension."""
+ if args is None:
+ args = []
+ if kwargs is None:
+ kwargs = []
+ return nodes.Call(self.attr(name, lineno=lineno), args, kwargs,
+ dyn_args, dyn_kwargs, lineno=lineno)
+
class InternationalizationExtension(Extension):
"""This extension adds gettext support to Jinja2."""

0 comments on commit 27069d7

Please sign in to comment.