Author: jezdez
Date: 2011-08-12 07:15:17 -0700 (Fri, 12 Aug 2011)
New Revision: 16605

Modified:
   django/trunk/django/template/debug.py
   django/trunk/django/template/defaulttags.py
   django/trunk/tests/regressiontests/templates/nodelist.py
   django/trunk/tests/regressiontests/templates/templatetags/bad_tag.py
   django/trunk/tests/regressiontests/templates/tests.py
Log:
Fixed #5831 -- Made sure the ForNode reports the correct source of an exception 
happening in one of the loops. Thanks, Charmless and vladmos.

Modified: django/trunk/django/template/debug.py
===================================================================
--- django/trunk/django/template/debug.py       2011-08-12 14:15:08 UTC (rev 
16604)
+++ django/trunk/django/template/debug.py       2011-08-12 14:15:17 UTC (rev 
16605)
@@ -78,7 +78,7 @@
             from sys import exc_info
             wrapped = TemplateSyntaxError(u'Caught %s while rendering: %s' %
                 (e.__class__.__name__, force_unicode(e, errors='replace')))
-            wrapped.source = node.source
+            wrapped.source = getattr(e, 'template_node_source', node.source)
             wrapped.exc_info = exc_info()
             raise wrapped, None, wrapped.exc_info[2]
         return result

Modified: django/trunk/django/template/defaulttags.py
===================================================================
--- django/trunk/django/template/defaulttags.py 2011-08-12 14:15:08 UTC (rev 
16604)
+++ django/trunk/django/template/defaulttags.py 2011-08-12 14:15:17 UTC (rev 
16605)
@@ -227,8 +227,21 @@
                     context.update(unpacked_vars)
             else:
                 context[self.loopvars[0]] = item
-            for node in self.nodelist_loop:
-                nodelist.append(node.render(context))
+            # In TEMPLATE_DEBUG mode providing source of the node which
+            # actually raised an exception to DefaultNodeList.render_node
+            if settings.TEMPLATE_DEBUG:
+                for node in self.nodelist_loop:
+                    try:
+                        nodelist.append(node.render(context))
+                    except Exception, e:
+                        if not hasattr(e, 'template_node_source'):
+                            from sys import exc_info
+                            e.template_node_source = node.source
+                            raise e, None, exc_info()[2]
+                        raise
+            else:
+                for node in self.nodelist_loop:
+                    nodelist.append(node.render(context))
             if pop_context:
                 # The loop variables were pushed on to the context so pop them
                 # off again. This is necessary because the tag lets the length

Modified: django/trunk/tests/regressiontests/templates/nodelist.py
===================================================================
--- django/trunk/tests/regressiontests/templates/nodelist.py    2011-08-12 
14:15:08 UTC (rev 16604)
+++ django/trunk/tests/regressiontests/templates/nodelist.py    2011-08-12 
14:15:17 UTC (rev 16605)
@@ -1,8 +1,8 @@
+from django.conf import settings
+from django.template import VariableNode, Context, TemplateSyntaxError
 from django.template.loader import get_template_from_string
-from django.template import VariableNode
 from django.utils.unittest import TestCase
 
-
 class NodelistTest(TestCase):
 
     def test_for(self):
@@ -28,3 +28,37 @@
         template = get_template_from_string(source)
         vars = template.nodelist.get_nodes_by_type(VariableNode)
         self.assertEqual(len(vars), 1)
+
+
+class ErrorIndexTest(TestCase):
+    """
+    Checks whether index of error is calculated correctly in
+    template debugger in for loops. Refs ticket #5831
+    """
+    def setUp(self):
+        self.old_template_debug = settings.TEMPLATE_DEBUG
+        settings.TEMPLATE_DEBUG = True
+
+    def tearDown(self):
+        settings.TEMPLATE_DEBUG = self.old_template_debug
+
+    def test_correct_exception_index(self):
+        tests = [
+            ('{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% 
endfor %}', (38, 56)),
+            ('{% load bad_tag %}{% for i in range %}{% for j in range %}{% 
badsimpletag %}{% endfor %}{% endfor %}', (58, 76)),
+            ('{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% for j 
in range %}Hello{% endfor %}{% endfor %}', (38, 56)),
+            ('{% load bad_tag %}{% for i in range %}{% for j in five %}{% 
badsimpletag %}{% endfor %}{% endfor %}', (38, 57)),
+            ('{% load bad_tag %}{% for j in five %}{% badsimpletag %}{% endfor 
%}', (18, 37)),
+        ]
+        context = Context({
+            'range': range(5),
+            'five': 5,
+        })
+        for source, expected_error_source_index in tests:
+            template = get_template_from_string(source)
+            try:
+                template.render(context)
+            except TemplateSyntaxError, e:
+                error_source_index = e.source[1]
+                self.assertEqual(error_source_index,
+                                 expected_error_source_index)

Modified: django/trunk/tests/regressiontests/templates/templatetags/bad_tag.py
===================================================================
--- django/trunk/tests/regressiontests/templates/templatetags/bad_tag.py        
2011-08-12 14:15:08 UTC (rev 16604)
+++ django/trunk/tests/regressiontests/templates/templatetags/bad_tag.py        
2011-08-12 14:15:17 UTC (rev 16605)
@@ -5,3 +5,7 @@
 @register.tag
 def badtag(parser, token):
     raise RuntimeError("I am a bad tag")
+
[email protected]_tag
+def badsimpletag():
+    raise RuntimeError("I am a bad simpletag")

Modified: django/trunk/tests/regressiontests/templates/tests.py
===================================================================
--- django/trunk/tests/regressiontests/templates/tests.py       2011-08-12 
14:15:08 UTC (rev 16604)
+++ django/trunk/tests/regressiontests/templates/tests.py       2011-08-12 
14:15:17 UTC (rev 16605)
@@ -32,7 +32,7 @@
 from custom import CustomTagTests, CustomFilterTests
 from parser import ParserTests
 from unicode import UnicodeTests
-from nodelist import NodelistTest
+from nodelist import NodelistTest, ErrorIndexTest
 from smartif import *
 from response import *
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to