Ticket #2414: 2414.2.diff
File 2414.2.diff, 5.4 KB (added by , 15 years ago) |
---|
-
django/template/loader_tags.py
diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py index b8bc741..b4cf7ec 100644
a b 1 from django.template import TemplateSyntaxError, TemplateDoesNotExist,Variable1 from django.template import TemplateSyntaxError, Variable 2 2 from django.template import Library, Node, TextNode 3 3 from django.template.loader import get_template 4 4 from django.conf import settings … … from django.utils.safestring import mark_safe 7 7 register = Library() 8 8 9 9 BLOCK_CONTEXT_KEY = 'block_context' 10 EXTENDS_SEEN_KEY = 'extends_seen_parents' 10 11 11 class ExtendsError(Exception): 12 class ExtendsError(TemplateSyntaxError): 13 pass 14 15 class ExtendsRecursionError(ExtendsError): 12 16 pass 13 17 14 18 class BlockContext(object): … … class ExtendsNode(Node): 94 98 error_msg = "Invalid template name in 'extends' tag: %r." % parent 95 99 if self.parent_name_expr: 96 100 error_msg += " Got this from the '%s' variable." % self.parent_name_expr.token 97 raise TemplateSyntaxError(error_msg)101 raise ExtendsError(error_msg) 98 102 if hasattr(parent, 'render'): 99 103 return parent # parent is a Template object 104 if EXTENDS_SEEN_KEY not in context.render_context: 105 seen_parents = set() 106 context.render_context[EXTENDS_SEEN_KEY] = seen_parents 107 else: 108 seen_parents = context.render_context[EXTENDS_SEEN_KEY] 109 if parent in seen_parents: 110 raise ExtendsRecursionError('Template %r cannot be extended ' 111 'because it would cause a circular recursion' % parent) 112 seen_parents.add(parent) 100 113 return get_template(parent) 101 114 102 115 def render(self, context): … … class ExtendsNode(Node): 109 122 # Add the block nodes from this node to the block context 110 123 block_context.add_blocks(self.blocks) 111 124 112 # If this block's parent doesn't have an extends node it is the root, 113 # and its block nodes also need to be added to the block context. 125 # If this template's parent doesn't have an extends node it is the root 126 # then the parent's block nodes also need to be added to the block 127 # context. 114 128 for node in compiled_parent.nodelist: 115 129 # The ExtendsNode has to be the first non-text node. 116 130 if not isinstance(node, TextNode): -
tests/regressiontests/templates/tests.py
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 5902e8d..e13623b 100644
a b import unittest 15 15 16 16 from django import template 17 17 from django.core import urlresolvers 18 from django.template import loader 18 from django.template import loader, loader_tags 19 19 from django.template.loaders import app_directories, filesystem, cached 20 20 from django.utils.translation import activate, deactivate, ugettext as _ 21 21 from django.utils.safestring import mark_safe … … class Templates(unittest.TestCase): 642 642 'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateDoesNotExist), 643 643 644 644 # Raise exception for invalid template name (in variable) 645 'exception02': ("{% extends nonexistent %}", {}, ( template.TemplateSyntaxError, template.TemplateDoesNotExist)),645 'exception02': ("{% extends nonexistent %}", {}, (loader_tags.ExtendsError, template.TemplateDoesNotExist)), 646 646 647 647 # Raise exception for extra {% extends %} tags 648 648 'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError), … … class Templates(unittest.TestCase): 1041 1041 'inheritance40': ("{% extends 'inheritance33' %}{% block opt %}new{{ block.super }}{% endblock %}", {'optional': 1}, '1new23'), 1042 1042 'inheritance41': ("{% extends 'inheritance36' %}{% block opt %}new{{ block.super }}{% endblock %}", {'numbers': '123'}, '_new1_new2_new3_'), 1043 1043 1044 ### CIRCULAR INHERITANCE EXCEPTIONS ####################################### 1045 1046 # Self-extending template 1047 'circularinheritance01': ("{% extends 'circularinheritance01' %}", {}, loader_tags.ExtendsRecursionError), 1048 1049 # Circular extending template 1050 'circularinheritance02': ("{% extends 'circularinheritance04' %}", {}, loader_tags.ExtendsRecursionError), 1051 'circularinheritance03': ("{% extends 'circularinheritance02' %}", {}, loader_tags.ExtendsRecursionError), 1052 'circularinheritance04': ("{% extends 'circularinheritance03' %}", {}, loader_tags.ExtendsRecursionError), 1053 1054 # Self-extending template (by variable) 1055 'circularinheritance05': ("{% extends var %}", {'var': 'circularinheritance05'}, loader_tags.ExtendsRecursionError), 1056 1057 # Circular template (by variable) 1058 'circularinheritance06': ("{% extends var %}", {'var': 'circularinheritance08'}, loader_tags.ExtendsRecursionError), 1059 'circularinheritance07': ("{% extends var %}", {'var': 'circularinheritance06'}, loader_tags.ExtendsRecursionError), 1060 'circularinheritance08': ("{% extends var %}", {'var': 'circularinheritance07'}, loader_tags.ExtendsRecursionError), 1061 1044 1062 ### I18N ################################################################## 1045 1063 1046 1064 # {% spaceless %} tag