mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Make it easier to extend OrderedDict without breaking it.
This commit is contained in:
parent
60db46758f
commit
32062e9be7
2 changed files with 10 additions and 2 deletions
|
@ -52,7 +52,7 @@ class OrderedDict(dict, MutableMapping):
|
||||||
self.__root = root = _proxy(self.__hardroot)
|
self.__root = root = _proxy(self.__hardroot)
|
||||||
root.prev = root.next = root
|
root.prev = root.next = root
|
||||||
self.__map = {}
|
self.__map = {}
|
||||||
self.update(*args, **kwds)
|
self.__update(*args, **kwds)
|
||||||
|
|
||||||
def __setitem__(self, key, value,
|
def __setitem__(self, key, value,
|
||||||
dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
|
dict_setitem=dict.__setitem__, proxy=_proxy, Link=_Link):
|
||||||
|
@ -171,7 +171,7 @@ class OrderedDict(dict, MutableMapping):
|
||||||
size += sizeof(self.__root) * n # proxy objects
|
size += sizeof(self.__root) * n # proxy objects
|
||||||
return size
|
return size
|
||||||
|
|
||||||
update = MutableMapping.update
|
update = __update = MutableMapping.update
|
||||||
pop = MutableMapping.pop
|
pop = MutableMapping.pop
|
||||||
keys = MutableMapping.keys
|
keys = MutableMapping.keys
|
||||||
values = MutableMapping.values
|
values = MutableMapping.values
|
||||||
|
|
|
@ -1012,6 +1012,14 @@ class TestOrderedDict(unittest.TestCase):
|
||||||
od = OrderedDict(**d)
|
od = OrderedDict(**d)
|
||||||
self.assertGreater(sys.getsizeof(od), sys.getsizeof(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):
|
class GeneralMappingTests(mapping_tests.BasicTestMappingProtocol):
|
||||||
type2test = OrderedDict
|
type2test = OrderedDict
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue