[Python-checkins] r81426 - in python/branches/release31-maint: Lib/_abcoll.py Lib/test/test_collections.py Misc/NEWS

benjamin.peterson python-checkins at python.org
Fri May 21 23:05:45 CEST 2010


Author: benjamin.peterson
Date: Fri May 21 23:05:45 2010
New Revision: 81426
Log:
Merged revisions 81417 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k
................
 r81417 | benjamin.peterson | 2010年05月21日 15:55:22 -0500 (2010年5月21日) | 9 lines
 
 Merged revisions 81414 via svnmerge from 
 svn+ssh://pythondev@svn.python.org/python/trunk
 
 ........
 r81414 | benjamin.peterson | 2010年05月21日 15:51:45 -0500 (2010年5月21日) | 1 line
 
 return NotImplemented from Mapping when comparing to a non-mapping #8729
 ........
................
Modified:
 python/branches/release31-maint/ (props changed)
 python/branches/release31-maint/Lib/_abcoll.py
 python/branches/release31-maint/Lib/test/test_collections.py
 python/branches/release31-maint/Misc/NEWS
Modified: python/branches/release31-maint/Lib/_abcoll.py
==============================================================================
--- python/branches/release31-maint/Lib/_abcoll.py	(original)
+++ python/branches/release31-maint/Lib/_abcoll.py	Fri May 21 23:05:45 2010
@@ -376,8 +376,9 @@
 return ValuesView(self)
 
 def __eq__(self, other):
- return isinstance(other, Mapping) and \
- dict(self.items()) == dict(other.items())
+ if not isinstance(other, Mapping):
+ return NotImplemented
+ return dict(self.items()) == dict(other.items())
 
 def __ne__(self, other):
 return not (self == other)
Modified: python/branches/release31-maint/Lib/test/test_collections.py
==============================================================================
--- python/branches/release31-maint/Lib/test/test_collections.py	(original)
+++ python/branches/release31-maint/Lib/test/test_collections.py	Fri May 21 23:05:45 2010
@@ -1,6 +1,6 @@
 """Unit tests for collections.py."""
 
-import unittest, doctest
+import unittest, doctest, operator
 import inspect
 from test import support
 from collections import namedtuple, Counter, OrderedDict
@@ -230,6 +230,37 @@
 self.assertRaises(TypeError, C, name)
 
 
+ def validate_comparison(self, instance):
+ ops = ['lt', 'gt', 'le', 'ge', 'ne', 'or', 'and', 'xor', 'sub']
+ operators = {}
+ for op in ops:
+ name = '__' + op + '__'
+ operators[name] = getattr(operator, name)
+
+ class Other:
+ def __init__(self):
+ self.right_side = False
+ def __eq__(self, other):
+ self.right_side = True
+ return True
+ __lt__ = __eq__
+ __gt__ = __eq__
+ __le__ = __eq__
+ __ge__ = __eq__
+ __ne__ = __eq__
+ __ror__ = __eq__
+ __rand__ = __eq__
+ __rxor__ = __eq__
+ __rsub__ = __eq__
+
+ for name, op in operators.items():
+ if not hasattr(instance, name):
+ continue
+ other = Other()
+ op(instance, other)
+ self.assertTrue(other.right_side,'Right side not called for %s.%s'
+ % (type(instance), name))
+
 class TestOneTrickPonyABCs(ABCTestCase):
 
 def test_Hashable(self):
@@ -398,6 +429,14 @@
 self.assertTrue(isinstance(sample(), Set))
 self.assertTrue(issubclass(sample, Set))
 self.validate_abstract_methods(Set, '__contains__', '__iter__', '__len__')
+ class MySet(Set):
+ def __contains__(self, x):
+ return False
+ def __len__(self):
+ return 0
+ def __iter__(self):
+ return iter([])
+ self.validate_comparison(MySet())
 
 def test_hash_Set(self):
 class OneTwoThreeSet(Set):
@@ -461,6 +500,14 @@
 self.assertTrue(issubclass(sample, Mapping))
 self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__len__',
 '__getitem__')
+ class MyMapping(collections.Mapping):
+ def __len__(self):
+ return 0
+ def __getitem__(self, i):
+ raise IndexError
+ def __iter__(self):
+ return iter(())
+ self.validate_comparison(MyMapping())
 
 def test_MutableMapping(self):
 for sample in [dict]:
Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS	(original)
+++ python/branches/release31-maint/Misc/NEWS	Fri May 21 23:05:45 2010
@@ -54,6 +54,9 @@
 Library
 -------
 
+- Issue #8729: Return NotImplemented from collections.Mapping.__eq__ when
+ comparing to a non-mapping.
+
 - Issue #8774: tabnanny uses the encoding cookie (#coding:...) to use the
 correct encoding
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /