Commit 4ad6bd54 authored by Benjamin Peterson's avatar Benjamin Peterson

Merged revisions 81414 via svnmerge from

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
........
parent 471ed00a
...@@ -376,8 +376,9 @@ class Mapping(Sized, Iterable, Container): ...@@ -376,8 +376,9 @@ class Mapping(Sized, Iterable, Container):
return ValuesView(self) return ValuesView(self)
def __eq__(self, other): def __eq__(self, other):
return isinstance(other, Mapping) and \ if not isinstance(other, Mapping):
dict(self.items()) == dict(other.items()) return NotImplemented
return dict(self.items()) == dict(other.items())
def __ne__(self, other): def __ne__(self, other):
return not (self == other) return not (self == other)
......
"""Unit tests for collections.py.""" """Unit tests for collections.py."""
import unittest, doctest import unittest, doctest, operator
import inspect import inspect
from test import support from test import support
from collections import namedtuple, Counter, OrderedDict from collections import namedtuple, Counter, OrderedDict
...@@ -246,6 +246,37 @@ class ABCTestCase(unittest.TestCase): ...@@ -246,6 +246,37 @@ class ABCTestCase(unittest.TestCase):
self.assertNotIsInstance(C(), abc) self.assertNotIsInstance(C(), abc)
self.assertFalse(issubclass(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): class TestOneTrickPonyABCs(ABCTestCase):
def test_Hashable(self): def test_Hashable(self):
...@@ -420,6 +451,14 @@ class TestCollectionABCs(ABCTestCase): ...@@ -420,6 +451,14 @@ class TestCollectionABCs(ABCTestCase):
self.assertIsInstance(sample(), Set) self.assertIsInstance(sample(), Set)
self.assertTrue(issubclass(sample, Set)) self.assertTrue(issubclass(sample, Set))
self.validate_abstract_methods(Set, '__contains__', '__iter__', '__len__') 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): def test_hash_Set(self):
class OneTwoThreeSet(Set): class OneTwoThreeSet(Set):
...@@ -483,6 +522,14 @@ class TestCollectionABCs(ABCTestCase): ...@@ -483,6 +522,14 @@ class TestCollectionABCs(ABCTestCase):
self.assertTrue(issubclass(sample, Mapping)) self.assertTrue(issubclass(sample, Mapping))
self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__len__', self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__len__',
'__getitem__') '__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): def test_MutableMapping(self):
for sample in [dict]: for sample in [dict]:
......
...@@ -393,6 +393,9 @@ C-API ...@@ -393,6 +393,9 @@ C-API
Library 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 - Issue #8774: tabnanny uses the encoding cookie (#coding:...) to use the
correct encoding correct encoding
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment