Skip to content

Fix for #22232 #2431

Closed
wants to merge 1 commit into from

5 participants

@NathanRSmith

Fix for #22232 preventing RuntimeError Maximum Recursion Depth Exceeded when a template extends itself.
https://code.djangoproject.com/ticket/22232

@evildmp evildmp commented on an outdated diff Jun 14, 2014
django/template/base.py
@@ -76,6 +76,8 @@
class TemplateSyntaxError(Exception):
pass
+class TemplateRecursionError(Exception):
+ pass
@evildmp
evildmp added a note Jun 14, 2014

Two lines between class definitions please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@evildmp
evildmp commented Jun 14, 2014

Please see https://docs.djangoproject.com/en/dev/internals/contributing/committing-code/#committing-guidelines for the style of the commit message. This can be addressed when rebasing to squash your four commits.

@coder9042

Please make the above suggested changes. Also those new html files must end in blank lines.

@NathanRSmith

Hi, sorry it's taken so long to get back to this. I did a git rebase -i on the commits and squashed them together into 32eb8ee but when I was trying to push, it wanted to merge. Could somebody explain how this squashing should be done correctly? Thanks

@charettes
Django member

Since you've been working in your own ticket_22232 you should be able to force push your change.

@NathanRSmith

Thanks @charettes, I think that worked now.

@charettes
Django member

Buildbot, test this please.

@charettes
Django member

The failures are unrelated. Could you add a test for the case when the extended template is a variable?

@charettes charettes commented on the diff Aug 4, 2014
django/template/loader_tags.py
@@ -102,6 +103,13 @@ def get_parent(self, context):
def render(self, context):
compiled_parent = self.get_parent(context)
+
+ if EXTENDS_HISTORY_CONTEXT_KEY not in context.render_context:
+ context.render_context[EXTENDS_HISTORY_CONTEXT_KEY] = []
+ if compiled_parent.name not in context.render_context[EXTENDS_HISTORY_CONTEXT_KEY]:
+ context.render_context[EXTENDS_HISTORY_CONTEXT_KEY].append(compiled_parent.name)
+ else:
+ raise TemplateRecursionError('Circular template extension error in %s' % context.render_context[EXTENDS_HISTORY_CONTEXT_KEY])
@charettes
Django member
charettes added a note Aug 4, 2014

Since you're referring to context.render_context[EXTENDS_HISTORY_CONTEXT_KEY] in a couple of places here it might be a good idea to store it in a local variable. e.g.

extends_history = context.render_context.setdefault(EXTENDS_HISTORY_CONTEXT_KEY, [])
parent_name = compiled_parent.name
if parent_name in extends_history:
    raise TemplateRecursionError('Circular template extension error in %s' % extends_history)
else:
    extends_history.append(parent_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham
Django member

Updated in #3014.

@timgraham timgraham closed this Aug 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.