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
This commit is contained in:
Alex Gaynor 2010-10-27 20:39:20 +00:00
parent 3d69b21790
commit c6f90f0053
2 changed files with 28 additions and 5 deletions

View File

@ -1,6 +1,6 @@
from types import GeneratorType from types import GeneratorType
from django.utils.copycompat import deepcopy from django.utils.copycompat import copy, deepcopy
class MergeDict(object): class MergeDict(object):
@ -263,7 +263,10 @@ class MultiValueDict(dict):
super(MultiValueDict, self).__setitem__(key, [value]) super(MultiValueDict, self).__setitem__(key, [value])
def __copy__(self): 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): def __deepcopy__(self, memo=None):
import django.utils.copycompat as copy import django.utils.copycompat as copy
@ -361,8 +364,8 @@ class MultiValueDict(dict):
yield self[key] yield self[key]
def copy(self): def copy(self):
"""Returns a copy of this object.""" """Returns a shallow copy of this object."""
return self.__deepcopy__() return copy(self)
def update(self, *args, **kwargs): def update(self, *args, **kwargs):
""" """
@ -491,4 +494,3 @@ class DictWrapper(dict):
if use_func: if use_func:
return self.func(value) return self.func(value)
return value return value

View File

@ -4,6 +4,7 @@ Tests for stuff in django.utils.datastructures.
import pickle import pickle
import unittest import unittest
from django.utils.copycompat import copy
from django.utils.datastructures import * from django.utils.datastructures import *
@ -211,6 +212,26 @@ class MultiValueDictTests(DatastructuresTestCase):
self.assertEquals(list(d.itervalues()), self.assertEquals(list(d.itervalues()),
['Developer', 'Simon', 'Willison']) ['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): class DotExpandedDictTests(DatastructuresTestCase):