diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index 30ce28d38d..7425ea2ce2 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -1,6 +1,6 @@ from types import GeneratorType -from django.utils.copycompat import deepcopy +from django.utils.copycompat import copy, deepcopy class MergeDict(object): @@ -263,7 +263,10 @@ class MultiValueDict(dict): super(MultiValueDict, self).__setitem__(key, [value]) def __copy__(self): - return self.__class__(super(MultiValueDict, self).items()) + return self.__class__([ + (k, v[:]) + for k, v in self.lists() + ]) def __deepcopy__(self, memo=None): import django.utils.copycompat as copy @@ -361,8 +364,8 @@ class MultiValueDict(dict): yield self[key] def copy(self): - """Returns a copy of this object.""" - return self.__deepcopy__() + """Returns a shallow copy of this object.""" + return copy(self) def update(self, *args, **kwargs): """ @@ -491,4 +494,3 @@ class DictWrapper(dict): if use_func: return self.func(value) return value - diff --git a/tests/regressiontests/utils/datastructures.py b/tests/regressiontests/utils/datastructures.py index a41281cd37..3c29a24a63 100644 --- a/tests/regressiontests/utils/datastructures.py +++ b/tests/regressiontests/utils/datastructures.py @@ -4,6 +4,7 @@ Tests for stuff in django.utils.datastructures. import pickle import unittest +from django.utils.copycompat import copy from django.utils.datastructures import * @@ -211,6 +212,26 @@ class MultiValueDictTests(DatastructuresTestCase): self.assertEquals(list(d.itervalues()), ['Developer', 'Simon', 'Willison']) + def test_copy(self): + for copy_func in [copy, lambda d: d.copy()]: + d1 = MultiValueDict({ + "developers": ["Carl", "Fred"] + }) + self.assertEqual(d1["developers"], "Fred") + d2 = copy_func(d1) + d2.update({"developers": "Groucho"}) + self.assertEqual(d2["developers"], "Groucho") + self.assertEqual(d1["developers"], "Fred") + + d1 = MultiValueDict({ + "key": [[]] + }) + self.assertEqual(d1["key"], []) + d2 = copy_func(d1) + d2["key"].append("Penguin") + self.assertEqual(d1["key"], ["Penguin"]) + self.assertEqual(d2["key"], ["Penguin"]) + class DotExpandedDictTests(DatastructuresTestCase):