Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 Gaynor authored
12  django/utils/datastructures.py
... ...
@@ -1,6 +1,6 @@
1 1
 from types import GeneratorType
2 2
 
3  
-from django.utils.copycompat import deepcopy
  3
+from django.utils.copycompat import copy, deepcopy
4 4
 
5 5
 
6 6
 class MergeDict(object):
@@ -263,7 +263,10 @@ def __setitem__(self, key, value):
263 263
         super(MultiValueDict, self).__setitem__(key, [value])
264 264
 
265 265
     def __copy__(self):
266  
-        return self.__class__(super(MultiValueDict, self).items())
  266
+        return self.__class__([
  267
+            (k, v[:])
  268
+            for k, v in self.lists()
  269
+        ])
267 270
 
268 271
     def __deepcopy__(self, memo=None):
269 272
         import django.utils.copycompat as copy
@@ -361,8 +364,8 @@ def itervalues(self):
361 364
             yield self[key]
362 365
 
363 366
     def copy(self):
364  
-        """Returns a copy of this object."""
365  
-        return self.__deepcopy__()
  367
+        """Returns a shallow copy of this object."""
  368
+        return copy(self)
366 369
 
367 370
     def update(self, *args, **kwargs):
368 371
         """
@@ -491,4 +494,3 @@ def __getitem__(self, key):
491 494
         if use_func:
492 495
             return self.func(value)
493 496
         return value
494  
-
21  tests/regressiontests/utils/datastructures.py
@@ -4,6 +4,7 @@
4 4
 import pickle
5 5
 import unittest
6 6
 
  7
+from django.utils.copycompat import copy
7 8
 from django.utils.datastructures import *
8 9
 
9 10
 
@@ -211,6 +212,26 @@ def test_multivaluedict(self):
211 212
         self.assertEquals(list(d.itervalues()),
212 213
                           ['Developer', 'Simon', 'Willison'])
213 214
 
  215
+    def test_copy(self):
  216
+        for copy_func in [copy, lambda d: d.copy()]:
  217
+            d1 = MultiValueDict({
  218
+                "developers": ["Carl", "Fred"]
  219
+            })
  220
+            self.assertEqual(d1["developers"], "Fred")
  221
+            d2 = copy_func(d1)
  222
+            d2.update({"developers": "Groucho"})
  223
+            self.assertEqual(d2["developers"], "Groucho")
  224
+            self.assertEqual(d1["developers"], "Fred")
  225
+
  226
+            d1 = MultiValueDict({
  227
+                "key": [[]]
  228
+            })
  229
+            self.assertEqual(d1["key"], [])
  230
+            d2 = copy_func(d1)
  231
+            d2["key"].append("Penguin")
  232
+            self.assertEqual(d1["key"], ["Penguin"])
  233
+            self.assertEqual(d2["key"], ["Penguin"])
  234
+
214 235
 
215 236
 class DotExpandedDictTests(DatastructuresTestCase):
216 237
 

0 notes on commit c6f90f0

Please sign in to comment.
Something went wrong with that request. Please try again.