summaryrefslogtreecommitdiffstats
path: root/Lib/collections.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-09-16 19:10:17 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-09-16 19:10:17 (GMT)
commit35c87f2b8e5cc62c91a3bdbaec4de50d366f6228 (patch)
treeed16a4b9ea45825df7510db71ae11ea1a6ebf6b3 /Lib/collections.py
parent234f88dc73d4324851c4b624110f7afe699c1dbf (diff)
downloadcpython-35c87f2b8e5cc62c91a3bdbaec4de50d366f6228.zip
cpython-35c87f2b8e5cc62c91a3bdbaec4de50d366f6228.tar.gz
cpython-35c87f2b8e5cc62c91a3bdbaec4de50d366f6228.tar.bz2
Issue 9865: add __sizeof__ to OrderedDict.
Diffstat (limited to 'Lib/collections.py')
-rw-r--r--Lib/collections.py31
1 files changed, 20 insertions, 11 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 68e9b28..f05d7b4 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -97,17 +97,6 @@ class OrderedDict(dict, MutableMapping):
yield curr.key
curr = curr.prev
- def __reduce__(self):
- 'Return state information for pickling'
- items = [[k, self[k]] for k in self]
- tmp = self.__map, self.__root, self.__hardroot
- del self.__map, self.__root, self.__hardroot
- inst_dict = vars(self).copy()
- self.__map, self.__root, self.__hardroot = tmp
- if inst_dict:
- return (self.__class__, (items,), inst_dict)
- return self.__class__, (items,)
-
def clear(self):
'od.clear() -> None. Remove all items from od.'
root = self.__root
@@ -162,6 +151,26 @@ class OrderedDict(dict, MutableMapping):
link.next = first
root.next = first.prev = link
+ def __reduce__(self):
+ 'Return state information for pickling'
+ items = [[k, self[k]] for k in self]
+ tmp = self.__map, self.__root, self.__hardroot
+ del self.__map, self.__root, self.__hardroot
+ inst_dict = vars(self).copy()
+ self.__map, self.__root, self.__hardroot = tmp
+ if inst_dict:
+ return (self.__class__, (items,), inst_dict)
+ return self.__class__, (items,)
+
+ def __sizeof__(self):
+ sizeof = _sys.getsizeof
+ n = len(self) + 1 # number of links including root
+ size = sizeof(self.__dict__) # instance dictionary
+ size += sizeof(self.__map) * 2 # internal dict and inherited dict
+ size += sizeof(self.__hardroot) * n # link objects
+ size += sizeof(self.__root) * n # proxy objects
+ return size
+
setdefault = MutableMapping.setdefault
update = MutableMapping.update
pop = MutableMapping.pop