Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed MultiValueDict's copy implementation to be consistant with all …

…other copies.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14366 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c6f90f00535ab55d36fe687305d4aebdad197001 1 parent 3d69b21
@alex alex authored
View
12 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 @@ def __setitem__(self, key, value):
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 @@ def itervalues(self):
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 @@ def __getitem__(self, key):
if use_func:
return self.func(value)
return value
-
View
21 tests/regressiontests/utils/datastructures.py
@@ -4,6 +4,7 @@
import pickle
import unittest
+from django.utils.copycompat import copy
from django.utils.datastructures import *
@@ -211,6 +212,26 @@ def test_multivaluedict(self):
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):
Please sign in to comment.
Something went wrong with that request. Please try again.