Refactoring in the way the parse function is invoked.
authorArmin Ronacher <armin.ronacher@active-4.com>
Sun, 13 Sep 2009 07:22:50 +0000 (00:22 -0700)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sun, 13 Sep 2009 07:22:50 +0000 (00:22 -0700)
--HG--
branch : trunk

jinja2/compiler.py
jinja2/environment.py

index f0deeff5f30922abb823ab3f8f318550024b2892..51790037d8e7714f9372d8da68ad6ce37ab42289 100644 (file)
@@ -606,7 +606,7 @@ class CodeGenerator(NodeVisitor):
         )
         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
index 603c4aa08e15614e639b1a479039fb5a1cc4d69e..75f58d6f6095963462d4835d931f313fc2467db5 100644 (file)
@@ -365,14 +365,18 @@ class Environment(object):
         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 @@ class Environment(object):
         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