Permalink
Browse files

Refactoring in the way the parse function is invoked.

--HG--
branch : trunk
  • Loading branch information...
1 parent eabf3dd commit efcc0e5ad10714a41092705b1ac4ffadbdb404e4 @mitsuhiko mitsuhiko committed Sep 13, 2009
Showing with 26 additions and 16 deletions.
  1. +1 −1 jinja2/compiler.py
  2. +25 −15 jinja2/environment.py
View
@@ -606,7 +606,7 @@ def function_scoping(self, node, frame, children=None,
)
if overriden_closure_vars:
self.fail('It\'s not possible to set and access variables '
- 'derived from an outer scope! (affects: %s' %
+ 'derived from an outer scope! (affects: %s)' %
', '.join(sorted(overriden_closure_vars)), node.lineno)
# remove variables from a closure from the frame's undeclared
View
@@ -365,14 +365,18 @@ def parse(self, source, name=None, filename=None):
If you are :ref:`developing Jinja2 extensions <writing-extensions>`
this gives you a good overview of the node tree generated.
"""
- if isinstance(filename, unicode):
- filename = filename.encode('utf-8')
try:
- return Parser(self, source, name, filename).parse()
+ return self._parse(source, name, filename)
except TemplateSyntaxError:
exc_info = sys.exc_info()
self.handle_exception(exc_info, source_hint=source)
+ def _parse(self, source, name, filename):
+ """Internal parsing function used by `parse` and `compile`."""
+ if isinstance(filename, unicode):
+ filename = filename.encode('utf-8')
+ return Parser(self, source, name, filename).parse()
+
def lex(self, source, name=None, filename=None):
"""Lex the given sourcecode and return a generator that yields
tokens as tuples in the form ``(lineno, token_type, value)``.
@@ -424,18 +428,24 @@ def compile(self, source, name=None, filename=None, raw=False):
code equivalent to the bytecode returned otherwise. This method is
mainly used internally.
"""
- if isinstance(source, basestring):
- source = self.parse(source, name, filename)
- if self.optimized:
- source = optimize(source, self)
- source = generate(source, self, name, filename)
- if raw:
- return source
- if filename is None:
- filename = '<template>'
- elif isinstance(filename, unicode):
- filename = filename.encode('utf-8')
- return compile(source, filename, 'exec')
+ source_hint = None
+ try:
+ if isinstance(source, basestring):
+ source_hint = source
+ source = self._parse(source, name, filename)
+ if self.optimized:
+ source = optimize(source, self)
+ source = generate(source, self, name, filename)
+ if raw:
+ return source
+ if filename is None:
+ filename = '<template>'
+ elif isinstance(filename, unicode):
+ filename = filename.encode('utf-8')
+ return compile(source, filename, 'exec')
+ except TemplateSyntaxError:
+ exc_info = sys.exc_info()
+ self.handle_exception(exc_info, source_hint=source)
def compile_expression(self, source, undefined_to_none=True):
"""A handy helper method that returns a callable that accepts keyword

0 comments on commit efcc0e5

Please sign in to comment.