summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2009-04-07 22:02:34 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2009-04-07 22:02:34 +0000
commit6d6bbb6d0571d32b10978dc36a905c8d48c3ce1c (patch)
tree100f0566565e92b0fc62038829f2b37976932728
parent4f7950ac05bbb602d2cade142fafb5311c24fe9a (diff)
Fixed #9756: the for tag no longer leaves the context stack unbalanced when dealing with an empty iterable. Thanks, seanl.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10439 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/template/defaulttags.py1
-rw-r--r--tests/regressiontests/templates/tests.py13
2 files changed, 13 insertions, 1 deletions
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
index c8ad48b958..a9e7f40fad 100644
--- a/django/template/defaulttags.py
+++ b/django/template/defaulttags.py
@@ -125,6 +125,7 @@ class ForNode(Node):
values = list(values)
len_values = len(values)
if len_values < 1:
+ context.pop()
return self.nodelist_empty.render(context)
nodelist = NodeList()
if self.is_reversed:
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index 4ccf9d0c47..91c6d22b33 100644
--- a/tests/regressiontests/templates/tests.py
+++ b/tests/regressiontests/templates/tests.py
@@ -69,6 +69,9 @@ class SomeException(Exception):
class SomeOtherException(Exception):
pass
+
+class ContextStackException(Exception):
+ pass
class SomeClass:
def __init__(self):
@@ -231,6 +234,9 @@ class Templates(unittest.TestCase):
try:
test_template = loader.get_template(name)
output = self.render(test_template, vals)
+ except ContextStackException:
+ failures.append("Template test (TEMPLATE_STRING_IF_INVALID='%s'): %s -- FAILED. Context stack was left imbalanced" % (invalid_str, name))
+ continue
except Exception:
exc_type, exc_value, exc_tb = sys.exc_info()
if exc_type != result:
@@ -256,7 +262,12 @@ class Templates(unittest.TestCase):
('-'*70, ("\n%s\n" % ('-'*70)).join(failures)))
def render(self, test_template, vals):
- return test_template.render(template.Context(vals[1]))
+ context = template.Context(vals[1])
+ before_stack_size = len(context.dicts)
+ output = test_template.render(context)
+ if len(context.dicts) != before_stack_size:
+ raise ContextStackException
+ return output
def get_template_tests(self):
# SYNTAX --