Backport 70140, 70141, 70143, and 70144.
Adds tests, switches from list to deque, fixes __reduce__ which was unnecessarily copying __keys.
This commit is contained in:
parent
288618e1a7
commit
131af6505a
|
@ -27,11 +27,11 @@ class OrderedDict(dict, MutableMapping):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
# Note the underlying data structure for this class is likely to
|
# Note the underlying data structure for this class is likely to
|
||||||
# change in the future. Do not rely on it or access it directly.
|
# change in the future. Do not rely on it or access it directly.
|
||||||
self.__keys = []
|
self.__keys = deque()
|
||||||
self.update(*args, **kwds)
|
self.update(*args, **kwds)
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
del self.__keys[:]
|
self.__keys.clear()
|
||||||
dict.clear(self)
|
dict.clear(self)
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
|
@ -58,16 +58,20 @@ class OrderedDict(dict, MutableMapping):
|
||||||
|
|
||||||
def __reduce__(self):
|
def __reduce__(self):
|
||||||
items = [[k, self[k]] for k in self]
|
items = [[k, self[k]] for k in self]
|
||||||
|
tmp = self.__keys
|
||||||
|
del self.__keys
|
||||||
inst_dict = vars(self).copy()
|
inst_dict = vars(self).copy()
|
||||||
inst_dict.pop('__keys', None)
|
self.__keys = tmp
|
||||||
return (self.__class__, (items,), inst_dict)
|
if inst_dict:
|
||||||
|
return (self.__class__, (items,), inst_dict)
|
||||||
|
return self.__class__, (items,)
|
||||||
|
|
||||||
setdefault = MutableMapping.setdefault
|
setdefault = MutableMapping.setdefault
|
||||||
update = MutableMapping.update
|
update = MutableMapping.update
|
||||||
pop = MutableMapping.pop
|
pop = MutableMapping.pop
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
return self.__keys[:]
|
return list(self.__keys)
|
||||||
|
|
||||||
def values(self):
|
def values(self):
|
||||||
return map(self.__getitem__, self.__keys)
|
return map(self.__getitem__, self.__keys)
|
||||||
|
|
|
@ -717,6 +717,23 @@ class TestOrderedDict(unittest.TestCase):
|
||||||
self.assertEquals(len(dup), len(od))
|
self.assertEquals(len(dup), len(od))
|
||||||
self.assertEquals(type(dup), type(od))
|
self.assertEquals(type(dup), type(od))
|
||||||
|
|
||||||
|
def test_yaml_linkage(self):
|
||||||
|
# Verify that __reduce__ is setup in a way that supports PyYAML's dump() feature.
|
||||||
|
# In yaml, lists are native but tuples are not.
|
||||||
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
||||||
|
od = OrderedDict(pairs)
|
||||||
|
# yaml.dump(od) -->
|
||||||
|
# '!!python/object/apply:__main__.OrderedDict\n- - [a, 1]\n - [b, 2]\n'
|
||||||
|
self.assert_(all(type(pair)==list for pair in od.__reduce__()[1]))
|
||||||
|
|
||||||
|
def test_reduce_not_too_fat(self):
|
||||||
|
# do not save instance dictionary if not needed
|
||||||
|
pairs = [('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)]
|
||||||
|
od = OrderedDict(pairs)
|
||||||
|
self.assertEqual(len(od.__reduce__()), 2)
|
||||||
|
od.x = 10
|
||||||
|
self.assertEqual(len(od.__reduce__()), 3)
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])
|
od = OrderedDict([('c', 1), ('b', 2), ('a', 3), ('d', 4), ('e', 5), ('f', 6)])
|
||||||
self.assertEqual(repr(od),
|
self.assertEqual(repr(od),
|
||||||
|
|
Loading…
Reference in New Issue