From 32062e9be78ee49b563d1b7eb290a00e6fe16c0f Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 1 Jan 2011 22:38:00 +0000 Subject: [PATCH] Make it easier to extend OrderedDict without breaking it. --- Lib/collections.py | 4 ++-- Lib/test/test_collections.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Lib/collections.py b/Lib/collections.py index c69f4ca84ec..d0a44c28283 100644 --- a/Lib/collections.py +++ b/Lib/collections.py @@ -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 diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 32ce35bb1d5..8c959792de8 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -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