Last active
December 19, 2015 10:19
-
-
Save loic/5939296 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/django/template/context.py b/django/template/context.py | |
index 1ef7e88..8e4706c 100644 | |
--- a/django/template/context.py | |
+++ b/django/template/context.py | |
@@ -12,6 +12,19 @@ class ContextPopException(Exception): | |
"pop() has been called more times than push()" | |
pass | |
+class ContextDict(dict): | |
+ def __init__(self, context, *args, **kwargs): | |
+ super(ContextDict, self).__init__(*args, **kwargs) | |
+ | |
+ context.dicts.append(self) | |
+ self.context = context | |
+ | |
+ def __enter__(self): | |
+ return self | |
+ | |
+ def __exit__(self, *args, **kwargs): | |
+ self.context.pop() | |
+ | |
class BaseContext(object): | |
def __init__(self, dict_=None): | |
self._reset_dicts(dict_) | |
@@ -35,9 +48,7 @@ class BaseContext(object): | |
yield d | |
def push(self): | |
- d = {} | |
- self.dicts.append(d) | |
- return d | |
+ return ContextDict(self) | |
def pop(self): | |
if len(self.dicts) == 1: | |
diff --git a/tests/template_tests/test_context.py b/tests/template_tests/test_context.py | |
index 224b94d..29bdeb7 100644 | |
--- a/tests/template_tests/test_context.py | |
+++ b/tests/template_tests/test_context.py | |
@@ -16,3 +16,8 @@ class ContextTests(TestCase): | |
self.assertEqual(c.pop(), {"a": 2}) | |
self.assertEqual(c["a"], 1) | |
self.assertEqual(c.get("foo", 42), 42) | |
+ | |
+ with c.push(): | |
+ c["a"] = 2 | |
+ self.assertEqual(c["a"], 2) | |
+ self.assertEqual(c["a"], 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment