Commit 78bb39e3 by Chris Jerdonek

Fixed issue #51: "Passing **kwargs to Template.__init__() modifies the context"

Also added a test case.
parent a484f30f
......@@ -62,13 +62,20 @@ class Template(object):
modifiers = Modifiers()
def __init__(self, template=None, context=None, **kwargs):
"""
The **kwargs arguments are only supported if the context is
a dictionary (i.e. not a View).
"""
from .view import View
self.template = template
if context is None:
context = {}
elif not isinstance(context, View):
# Views do not support copy() or update().
context = context.copy()
if kwargs:
context.update(kwargs)
......
......@@ -12,6 +12,8 @@ from pystache.template import Template
class TemplateTestCase(unittest.TestCase):
"""Test the Template class."""
def test_init__kwargs_with_no_context(self):
"""
Test passing **kwargs with no context.
......@@ -20,3 +22,12 @@ class TemplateTestCase(unittest.TestCase):
# This test checks that the following line raises no exception.
template = Template(foo="bar")
def test_init__kwargs_does_not_modify_context(self):
"""
Test that passing **kwargs does not modify the passed context.
"""
context = {}
template = Template(context=context, foo="bar")
self.assertEquals(context, {})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment