]> granicus.if.org Git - python/commitdiff
Merged revisions 81414 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Fri, 21 May 2010 21:03:02 +0000 (21:03 +0000)
committerBenjamin Peterson <benjamin@python.org>
Fri, 21 May 2010 21:03:02 +0000 (21:03 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r81414 | benjamin.peterson | 2010-05-21 15:51:45 -0500 (Fri, 21 May 2010) | 1 line

  return NotImplemented from Mapping when comparing to a non-mapping #8729
........

Lib/_abcoll.py
Lib/test/test_collections.py
Misc/NEWS

index 82ded379a4e0f8a97018141d441f1fe1593897ae..3743ea82f04d7d4093441cc2c3ee45d3baeca328 100644 (file)
@@ -369,8 +369,9 @@ class Mapping(Sized, Iterable, Container):
     __hash__ = None
 
     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)
index d1fd60eec53f1dda182130dae9276bdcff1a284a..f48ae5a4c2858f4593584c83a465aab39fac05e6 100644 (file)
@@ -1,4 +1,4 @@
-import unittest, doctest
+import unittest, doctest, operator
 from test import test_support
 from collections import namedtuple
 import pickle, cPickle, copy
@@ -232,6 +232,37 @@ class ABCTestCase(unittest.TestCase):
         self.assertFalse(isinstance(C(), abc))
         self.assertFalse(issubclass(C, abc))
 
+    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):
@@ -409,6 +440,14 @@ class TestCollectionABCs(ABCTestCase):
             self.failUnless(isinstance(sample(), Set))
             self.failUnless(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):
@@ -472,6 +511,14 @@ class TestCollectionABCs(ABCTestCase):
             self.failUnless(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]:
index 8e620cbdb4a1d7d6e039b67d951cef0729973ac9..2fc7890de4434947fb56e0206c125d6e7b2cc0e9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -55,6 +55,9 @@ C-API
 Library
 -------
 
+- Issue #8729: Return NotImplemented from collections.Mapping.__eq__ when
+  comparing to a non-mapping.
+
 - Issue #5918: Fix a crash in the parser module.
 
 - Issue #8688: Distutils now recalculates MANIFEST everytime.