aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Eeo <xe0jun@hotmail.com>2014-12-14 00:45:48 +0800
committerEugene Eeo <xe0jun@hotmail.com>2014-12-14 00:45:48 +0800
commit940383d720d592e8428bbd641c1ba0052c367fb4 (patch)
tree3b2ce3dd23cf8177ff88b678650e48f05fa3eb7d
parent835e9b5d276cd7cf6fdc399a820ef210d805b0bd (diff)
downloaduritemplates-940383d720d592e8428bbd641c1ba0052c367fb4.tar.gz
add testcase and helper function
-rw-r--r--test_uritemplate.py6
-rw-r--r--uritemplate/template.py10
2 files changed, 14 insertions, 2 deletions
diff --git a/test_uritemplate.py b/test_uritemplate.py
index d23a7ed..756a9a6 100644
--- a/test_uritemplate.py
+++ b/test_uritemplate.py
@@ -491,6 +491,12 @@ class TestURITemplate(RFCTemplateExamples('RFCMeta', (TestCase,), {})):
d[u] += 1
self.assertEqual(d, {t: 2})
+ def test_no_mutate(self):
+ args = {}
+ t = URITemplate('')
+ t.expand(args, key=1)
+ self.assertEqual(args, {})
+
class TestURIVariable(TestCase):
def setUp(self):
diff --git a/uritemplate/template.py b/uritemplate/template.py
index 0d45214..363e8ea 100644
--- a/uritemplate/template.py
+++ b/uritemplate/template.py
@@ -21,6 +21,10 @@ from uritemplate.variable import URIVariable
template_re = re.compile('{([^\}]+)}')
+def _update(kwargs, var_dict):
+ kwargs.update((k, v) for k, v in var_dict.items() if k not in kwargs)
+
+
class URITemplate(object):
"""This parses the template and will be used to expand it.
@@ -121,7 +125,8 @@ class URITemplate(object):
``val2`` will be used instead of ``val1``.
"""
- kwargs.update(var_dict or {})
+ if var_dict:
+ _update(kwargs, var_dict)
return self._expand(kwargs, False)
@@ -141,6 +146,7 @@ class URITemplate(object):
t.partial() # => URITemplate('https://api.github.com{/end}')
"""
- kwargs.update(var_dict or {})
+ if var_dict:
+ _update(kwargs, var_dict)
return URITemplate(self._expand(kwargs, True))