From c459292321568271346981caa8b135240ce3f485 Mon Sep 17 00:00:00 2001 From: Chris Somme Date: Thu, 4 Aug 2016 14:26:58 -0700 Subject: [PATCH] Allow Context to take another Context instance in new Related to https://code.djangoproject.com/ticket/24765. If a context instance is passed into new the context instance is appended to dicts. The flatten method fails on instances of Context because BaseContext#__iter__ returns iterates of the dicts. This change pulls the dicts over Context supplied in new Conflicts: tests/template_tests/test_context.py --- django/template/context.py | 4 +++- tests/template_tests/test_context.py | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/django/template/context.py b/django/template/context.py index b563e526f202..bf2f64f3480c 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -36,7 +36,9 @@ def __init__(self, dict_=None): def _reset_dicts(self, value=None): builtins = {'True': True, 'False': False, 'None': None} self.dicts = [builtins] - if value is not None: + if isinstance(value, BaseContext): + self.dicts += value.dicts[1:] + elif value is not None: self.dicts.append(value) def __copy__(self): diff --git a/tests/template_tests/test_context.py b/tests/template_tests/test_context.py index 87b2016aa500..6659ae6403be 100644 --- a/tests/template_tests/test_context.py +++ b/tests/template_tests/test_context.py @@ -131,7 +131,7 @@ def test_flatten_context(self): a.update({'b': 4}) a.update({'c': 8}) - self.assertEqual(a.flatten(), { + self.assertDictEqual(a.flatten(), { 'False': False, 'None': None, 'True': True, 'a': 2, 'b': 4, 'c': 8 }) @@ -150,6 +150,19 @@ def test_flatten_context_with_context(self): 'z': '8', }) + def test_flatten_context_new_context(self): + """ + Context.new with a Context argument should work. + """ + a = Context({'a': 2}) + b = a.new(Context({'b': 4})) + self.assertEqual(b.flatten(), { + 'False': False, + 'None': None, + 'True': True, + 'b': 4 + }) + def test_context_comparable(self): """ #21765 -- equality comparison should work