]> granicus.if.org Git - python/commitdiff
Issue 9865: add __sizeof__ to OrderedDict.
authorRaymond Hettinger <python@rcn.com>
Thu, 16 Sep 2010 19:10:17 +0000 (19:10 +0000)
committerRaymond Hettinger <python@rcn.com>
Thu, 16 Sep 2010 19:10:17 +0000 (19:10 +0000)
Lib/collections.py
Lib/test/test_collections.py
Misc/NEWS

index 68e9b2807acdbe1178f922b632f4352426917ca1..f05d7b4350256bf3be97dcfd14c7eb3e77780901 100644 (file)
@@ -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
index 514dc3991b6208e47f5b3039b7248f4b1e043f23..51e93e4cffd2abd2ca845e6cfeb62288ba251a52 100644 (file)
@@ -994,6 +994,12 @@ class TestOrderedDict(unittest.TestCase):
         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
 
index 60b62b2c20301c3cff6a597f58edfd1d5dbf5150..1903cb6a5ee42a82357c2959bd289f2aa1b67e18 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -52,6 +52,8 @@ Core and Builtins
 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.