]> granicus.if.org Git - python/commitdiff
Make it easier to extend OrderedDict without breaking it.
authorRaymond Hettinger <python@rcn.com>
Sat, 1 Jan 2011 22:38:00 +0000 (22:38 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 1 Jan 2011 22:38:00 +0000 (22:38 +0000)
Lib/collections.py
Lib/test/test_collections.py

index c69f4ca84ec049d2dd0b1da525cc8f99bbe68e6c..d0a44c2828338b86d0396e4649b72639b29be1b9 100644 (file)
@@ -52,7 +52,7 @@ class OrderedDict(dict, MutableMapping):
             self.__root = root = _proxy(self.__hardroot)
             root.prev = root.next = root
             self.__map = {}
-        self.update(*args, **kwds)
+        self.__update(*args, **kwds)
 
     def __setitem__(self, key, value,
                     dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
@@ -171,7 +171,7 @@ class OrderedDict(dict, MutableMapping):
         size += sizeof(self.__root) * n         # proxy objects
         return size
 
-    update = MutableMapping.update
+    update = __update = MutableMapping.update
     pop = MutableMapping.pop
     keys = MutableMapping.keys
     values = MutableMapping.values
index 32ce35bb1d589b2f47d942530875f81c91233d08..8c959792de8a80da642a4c57addd43de02e5202d 100644 (file)
@@ -1012,6 +1012,14 @@ class TestOrderedDict(unittest.TestCase):
         od = OrderedDict(**d)
         self.assertGreater(sys.getsizeof(od), sys.getsizeof(d))
 
+    def test_override_update(self):
+        # Verify that subclasses can override update() without breaking __init__()
+        class MyOD(OrderedDict):
+            def update(self, *args, **kwds):
+                raise Exception()
+        items = [('a', 1), ('c', 3), ('b', 2)]
+        self.assertEqual(list(MyOD(items).items()), items)
+
 class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
     type2test = OrderedDict