Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed SortedDict.__copy__()

Fixed #18175 -- Calling SortedDict.__copy__() resulted in changes to
the original dictionary. The reason was likely related to subclassing
dict.

Thanks to linovia for report and patch.
  • Loading branch information...
commit 4b11762f7d7aed2f4f36c4158326c0a4332038f9 1 parent aa1aa1a
@akaariai akaariai authored
View
10 django/utils/datastructures.py
@@ -128,6 +128,12 @@ def __deepcopy__(self, memo):
return self.__class__([(key, copy.deepcopy(value, memo))
for key, value in self.iteritems()])
+ def __copy__(self):
+ # The Python's default copy implementation will alter the state
+ # of self. The reason for this seems complex but is likely related to
+ # subclassing dict.
+ return self.copy()
+
def __setitem__(self, key, value):
if key not in self:
self.keyOrder.append(key)
@@ -200,9 +206,7 @@ def insert(self, index, key, value):
def copy(self):
"""Returns a copy of this object."""
# This way of initializing the copy means it works for subclasses, too.
- obj = self.__class__(self)
- obj.keyOrder = self.keyOrder[:]
- return obj
+ return self.__class__(self)
def __repr__(self):
"""
View
6 tests/regressiontests/utils/datastructures.py
@@ -111,6 +111,12 @@ def test_pickle(self):
{7: 'seven', 1: 'one', 9: 'nine'}
)
+ def test_copy(self):
+ orig = SortedDict(((1, "one"), (0, "zero"), (2, "two")))
+ copied = copy.copy(orig)
+ self.assertEqual(orig.keys(), [1, 0, 2])
+ self.assertEqual(copied.keys(), [1, 0, 2])
+
def test_clear(self):
self.d1.clear()
self.assertEqual(self.d1, {})
Please sign in to comment.
Something went wrong with that request. Please try again.