[Python-checkins] r84852 - in python/branches/py3k: Lib/collections.py Lib/test/test_collections.py Misc/NEWS

raymond.hettinger python-checkins at python.org
Thu Sep 16 21:10:17 CEST 2010


Author: raymond.hettinger
Date: Thu Sep 16 21:10:17 2010
New Revision: 84852
Log:
Issue 9865: add __sizeof__ to OrderedDict.
Modified:
 python/branches/py3k/Lib/collections.py
 python/branches/py3k/Lib/test/test_collections.py
 python/branches/py3k/Misc/NEWS
Modified: python/branches/py3k/Lib/collections.py
==============================================================================
--- python/branches/py3k/Lib/collections.py	(original)
+++ python/branches/py3k/Lib/collections.py	Thu Sep 16 21:10:17 2010
@@ -97,17 +97,6 @@
 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 @@
 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
Modified: python/branches/py3k/Lib/test/test_collections.py
==============================================================================
--- python/branches/py3k/Lib/test/test_collections.py	(original)
+++ python/branches/py3k/Lib/test/test_collections.py	Thu Sep 16 21:10:17 2010
@@ -994,6 +994,12 @@
 with self.assertRaises(KeyError):
 od.move_to_end('x')
 
+ def test_sizeof(self):
+ # Wimpy test: Just verify the reported size is larger than a regular dict
+ d = dict(a=1)
+ od = OrderedDict(**d)
+ self.assertGreater(sys.getsizeof(od), sys.getsizeof(d))
+
 class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
 type2test = OrderedDict
 
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Thu Sep 16 21:10:17 2010
@@ -52,6 +52,8 @@
 Library
 -------
 
+- Issue #9865: collections.OrderedDict now has a __sizeof__ method.
+
 - Issue #9854: The default read() implementation in io.RawIOBase now
 handles non-blocking readinto() returning None correctly.
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /