Ticket #5831: forloop.nodelist.2.patch
File forloop.nodelist.2.patch, 5.2 KB (added by , 13 years ago) |
---|
-
tests/regressiontests/templates/tests.py
31 31 from custom import CustomTagTests, CustomFilterTests 32 32 from parser import ParserTests 33 33 from unicode import UnicodeTests 34 from nodelist import NodelistTest 34 from nodelist import NodelistTest, ErrorIndexTest 35 35 from smartif import * 36 36 from response import * 37 37 -
tests/regressiontests/templates/templatetags/bad_tag.py
5 5 @register.tag 6 6 def badtag(parser, token): 7 7 raise RuntimeError("I am a bad tag") 8 9 @register.simple_tag 10 def badsimpletag(): 11 raise RuntimeError("I am a bad simpletag") -
tests/regressiontests/templates/nodelist.py
1 1 from django.template.loader import get_template_from_string 2 from django.template import VariableNode 2 from django.template import VariableNode, Context, TemplateSyntaxError 3 3 from django.utils.unittest import TestCase 4 from django.conf import settings 4 5 5 6 6 class NodelistTest(TestCase): 7 7 8 8 def test_for(self): … … 28 28 template = get_template_from_string(source) 29 29 vars = template.nodelist.get_nodes_by_type(VariableNode) 30 30 self.assertEqual(len(vars), 1) 31 32 33 class 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
225 225 context.update(unpacked_vars) 226 226 else: 227 227 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)) 230 243 if pop_context: 231 244 # The loop variables were pushed on to the context so pop them 232 245 # off again. This is necessary because the tag lets the length -
django/template/debug.py
79 79 from sys import exc_info 80 80 wrapped = TemplateSyntaxError(u'Caught %s while rendering: %s' % 81 81 (e.__class__.__name__, force_unicode(e, errors='replace'))) 82 wrapped.source = node.source82 wrapped.source = getattr(e, 'template_node_source', node.source) 83 83 wrapped.exc_info = exc_info() 84 84 raise wrapped, None, wrapped.exc_info[2] 85 85 return result