Fixed #4563 -- Context.pop/push/update return the top-level dictionary (the new

one for push() and update(), the one removed for pop()). Based on a patch from
Brian Harring.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6854 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-12-02 23:57:22 +00:00
parent a88ca126fc
commit 67373009e0
3 changed files with 25 additions and 2 deletions

View File

@ -23,12 +23,14 @@ class Context(object):
yield d yield d
def push(self): def push(self):
self.dicts = [{}] + self.dicts d = {}
self.dicts = [d] + self.dicts
return d
def pop(self): def pop(self):
if len(self.dicts) == 1: if len(self.dicts) == 1:
raise ContextPopException raise ContextPopException
del self.dicts[0] return self.dicts.pop(0)
def __setitem__(self, key, value): def __setitem__(self, key, value):
"Set a variable in the current context" "Set a variable in the current context"
@ -62,6 +64,7 @@ class Context(object):
def update(self, other_dict): def update(self, other_dict):
"Like dict.update(). Pushes an entire dictionary's keys and values onto the context." "Like dict.update(). Pushes an entire dictionary's keys and values onto the context."
self.dicts = [other_dict] + self.dicts self.dicts = [other_dict] + self.dicts
return other_dict
# This is a function rather than module-level procedural code because we only # This is a function rather than module-level procedural code because we only
# want it to execute if somebody uses RequestContext. # want it to execute if somebody uses RequestContext.

View File

@ -0,0 +1,18 @@
# coding: utf-8
context_tests = r"""
>>> from django.template import Context
>>> c = Context({'a': 1, 'b': 'xyzzy'})
>>> c['a']
1
>>> c.push()
{}
>>> c['a'] = 2
>>> c['a']
2
>>> c.pop()
{'a': 2}
>>> c['a']
1
"""

View File

@ -18,11 +18,13 @@ from django.utils.safestring import mark_safe
from django.utils.tzinfo import LocalTimezone from django.utils.tzinfo import LocalTimezone
from unicode import unicode_tests from unicode import unicode_tests
from context import context_tests
import filters import filters
# Some other tests we would like to run # Some other tests we would like to run
__test__ = { __test__ = {
'unicode': unicode_tests, 'unicode': unicode_tests,
'context': context_tests,
} }
################################# #################################