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

raymond.hettinger python-checkins at python.org
Sat Jan 1 23:38:00 CET 2011


Author: raymond.hettinger
Date: Sat Jan  1 23:38:00 2011
New Revision: 87611

Log:
Make it easier to extend OrderedDict without breaking it.

Modified:
   python/branches/py3k/Lib/collections.py
   python/branches/py3k/Lib/test/test_collections.py

Modified: python/branches/py3k/Lib/collections.py
==============================================================================
--- python/branches/py3k/Lib/collections.py	(original)
+++ python/branches/py3k/Lib/collections.py	Sat Jan  1 23:38:00 2011
@@ -52,7 +52,7 @@
             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 @@
         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

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	Sat Jan  1 23:38:00 2011
@@ -1012,6 +1012,14 @@
         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
 


More information about the Python-checkins mailing list