[Python-checkins] r87594 - in python/branches/py3k: Lib/collections.py Lib/test/test_collections.py Misc/NEWS
raymond.hettinger
python-checkins at python.org
Sat Jan 1 00:16:17 CET 2011
Author: raymond.hettinger
Date: Sat Jan 1 00:16:17 2011
New Revision: 87594
Log:
Fix OrderedDict.setdefault() to work for subclasses that define __missing__().
Modified:
python/branches/py3k/Lib/collections.py
python/branches/py3k/Lib/test/test_collections.py
python/branches/py3k/Misc/NEWS
Modified: python/branches/py3k/Lib/collections.py
==============================================================================
--- python/branches/py3k/Lib/collections.py (original)
+++ python/branches/py3k/Lib/collections.py Sat Jan 1 00:16:17 2011
@@ -171,7 +171,6 @@
size += sizeof(self.__root) * n # proxy objects
return size
- setdefault = MutableMapping.setdefault
update = MutableMapping.update
pop = MutableMapping.pop
keys = MutableMapping.keys
@@ -179,6 +178,13 @@
items = MutableMapping.items
__ne__ = MutableMapping.__ne__
+ def setdefault(self, key, default=None):
+ 'OD.setdefault(k[,d]) -> OD.get(k,d), also set OD[k]=d if k not in OD'
+ if key in self:
+ return self[key]
+ self[key] = default
+ return default
+
@_recursive_repr()
def __repr__(self):
'od.__repr__() <==> repr(od)'
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 00:16:17 2011
@@ -976,6 +976,12 @@
# make sure 'x' is added to the end
self.assertEqual(list(od.items())[-1], ('x', 10))
+ # make sure setdefault still works when __missing__ is defined
+ class Missing(OrderedDict):
+ def __missing__(self, key):
+ return 0
+ self.assertEqual(Missing().setdefault(5, 9), 9)
+
def test_reinsert(self):
# Given insert a, insert b, delete a, re-insert a,
# verify that a is now later than b.
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS (original)
+++ python/branches/py3k/Misc/NEWS Sat Jan 1 00:16:17 2011
@@ -20,6 +20,9 @@
Library
-------
+- Fix collections.OrderedDict.setdefault() so that it works in
+ subclasses that define __missing__().
+
- Issue 10786: unittest.TextTestRunner default stream no longer bound at
import time. `sys.stderr` now looked up at instantiation time. Fix contributed
by Mark Roddy.
More information about the Python-checkins
mailing list