summaryrefslogtreecommitdiffstats
path: root/Lib/collections.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2009-03-03 22:59:25 (GMT)
committerRaymond Hettinger <python@rcn.com>2009-03-03 22:59:25 (GMT)
commit131af6505ace852a5754f380d451636c09e7d597 (patch)
treeaa743194c3662d4c41d0cc6845988b0666abedd6 /Lib/collections.py
parent288618e1a7342718afc32483270371b64734a1d9 (diff)
downloadcpython-131af6505ace852a5754f380d451636c09e7d597.zip
cpython-131af6505ace852a5754f380d451636c09e7d597.tar.gz
cpython-131af6505ace852a5754f380d451636c09e7d597.tar.bz2
Backport 70140, 70141, 70143, and 70144.
Adds tests, switches from list to deque, fixes __reduce__ which was unnecessarily copying __keys.
Diffstat (limited to 'Lib/collections.py')
-rw-r--r--Lib/collections.py14
1 files changed, 9 insertions, 5 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 3319378..0ff5673 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -27,11 +27,11 @@ class OrderedDict(dict, MutableMapping):
except AttributeError:
# Note the underlying data structure for this class is likely to
# change in the future. Do not rely on it or access it directly.
- self.__keys = []
+ self.__keys = deque()
self.update(*args, **kwds)
def clear(self):
- del self.__keys[:]
+ self.__keys.clear()
dict.clear(self)
def __setitem__(self, key, value):
@@ -58,16 +58,20 @@ class OrderedDict(dict, MutableMapping):
def __reduce__(self):
items = [[k, self[k]] for k in self]
+ tmp = self.__keys
+ del self.__keys
inst_dict = vars(self).copy()
- inst_dict.pop('__keys', None)
- return (self.__class__, (items,), inst_dict)
+ self.__keys = tmp
+ if inst_dict:
+ return (self.__class__, (items,), inst_dict)
+ return self.__class__, (items,)
setdefault = MutableMapping.setdefault
update = MutableMapping.update
pop = MutableMapping.pop
def keys(self):
- return self.__keys[:]
+ return list(self.__keys)
def values(self):
return map(self.__getitem__, self.__keys)