Ticket #5831: forloop.nodelist.2.patch

File forloop.nodelist.2.patch, 5.2 KB (added by Vladimir Moskva, 13 years ago)

Fixed typo in a comment

  • tests/regressiontests/templates/tests.py

     
    3131from custom import CustomTagTests, CustomFilterTests
    3232from parser import ParserTests
    3333from unicode import UnicodeTests
    34 from nodelist import NodelistTest
     34from nodelist import NodelistTest, ErrorIndexTest
    3535from smartif import *
    3636from response import *
    3737
  • tests/regressiontests/templates/templatetags/bad_tag.py

     
    55@register.tag
    66def badtag(parser, token):
    77    raise RuntimeError("I am a bad tag")
     8
     9@register.simple_tag
     10def badsimpletag():
     11    raise RuntimeError("I am a bad simpletag")
  • tests/regressiontests/templates/nodelist.py

     
    11from django.template.loader import get_template_from_string
    2 from django.template import VariableNode
     2from django.template import VariableNode, Context, TemplateSyntaxError
    33from django.utils.unittest import TestCase
     4from django.conf import settings
    45
    5 
    66class NodelistTest(TestCase):
    77
    88    def test_for(self):
     
    2828        template = get_template_from_string(source)
    2929        vars = template.nodelist.get_nodes_by_type(VariableNode)
    3030        self.assertEqual(len(vars), 1)
     31
     32
     33class ErrorIndexTest(TestCase):
     34    '''
     35    Checks whether index of error is calculated correctly in template debugger in for loops
     36    Ticket #5831
     37    '''
     38   
     39    def setUp(self):
     40        self.old_template_debug = settings.TEMPLATE_DEBUG
     41        settings.TEMPLATE_DEBUG = True
     42
     43    def test_correct_exception_index(self):
     44        tests = [
     45            ('{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% endfor %}', (38, 56)),
     46            ('{% load bad_tag %}{% for i in range %}{% for j in range %}{% badsimpletag %}{% endfor %}{% endfor %}', (58, 76)),
     47            ('{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% for j in range %}Hello{% endfor %}{% endfor %}', (38, 56)),
     48            ('{% load bad_tag %}{% for i in range %}{% for j in five %}{% badsimpletag %}{% endfor %}{% endfor %}', (38, 57)),
     49            ('{% load bad_tag %}{% for j in five %}{% badsimpletag %}{% endfor %}', (18, 37)),
     50        ]
     51        context = Context({
     52            'range': range(5),
     53            'five': 5,
     54        })
     55        for source, expected_error_source_index in tests:
     56            template = get_template_from_string(source)
     57            try:
     58                template.render(context)
     59            except TemplateSyntaxError, e:
     60                error_source_index = e.source[1]
     61                self.assertEqual(error_source_index, expected_error_source_index)
     62
     63    def tearDown(self):
     64        settings.TEMPLATE_DEBUG = self.old_template_debug
  • django/template/defaulttags.py

     
    225225                    context.update(unpacked_vars)
    226226            else:
    227227                context[self.loopvars[0]] = item
    228             for node in self.nodelist_loop:
    229                 nodelist.append(node.render(context))
     228            # In TEMPLATE_DEBUG mode providing source of the node which actually
     229            # raised an exception to DefaultNodeList.render_node method
     230            if settings.TEMPLATE_DEBUG:
     231                for node in self.nodelist_loop:
     232                    try:
     233                        nodelist.append(node.render(context))
     234                    except Exception, e:
     235                        if not hasattr(e, 'template_node_source'):
     236                            from sys import exc_info
     237                            e.template_node_source = node.source
     238                            raise e, None, exc_info()[2]
     239                        raise
     240            else:
     241                for node in self.nodelist_loop:
     242                    nodelist.append(node.render(context))
    230243            if pop_context:
    231244                # The loop variables were pushed on to the context so pop them
    232245                # off again. This is necessary because the tag lets the length
  • django/template/debug.py

     
    7979            from sys import exc_info
    8080            wrapped = TemplateSyntaxError(u'Caught %s while rendering: %s' %
    8181                (e.__class__.__name__, force_unicode(e, errors='replace')))
    82             wrapped.source = node.source
     82            wrapped.source = getattr(e, 'template_node_source', node.source)
    8383            wrapped.exc_info = exc_info()
    8484            raise wrapped, None, wrapped.exc_info[2]
    8585        return result
Back to Top