mirror of https://github.com/python/cpython
Issue #21408: The default __ne__() now returns NotImplemented if __eq__()
returned NotImplemented. Removed incorrect implementations of __ne__().
This commit is contained in:
parent
155ceaa454
commit
f4b7a02e93
|
@ -224,9 +224,6 @@ class Set(Sized, Iterable, Container):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return len(self) == len(other) and self.__le__(other)
|
return len(self) == len(other) and self.__le__(other)
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return not (self == other)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _from_iterable(cls, it):
|
def _from_iterable(cls, it):
|
||||||
'''Construct an instance of the class from any iterable input.
|
'''Construct an instance of the class from any iterable input.
|
||||||
|
@ -451,9 +448,6 @@ class Mapping(Sized, Iterable, Container):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return dict(self.items()) == dict(other.items())
|
return dict(self.items()) == dict(other.items())
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return not (self == other)
|
|
||||||
|
|
||||||
Mapping.register(mappingproxy)
|
Mapping.register(mappingproxy)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -481,9 +481,6 @@ class Example:
|
||||||
self.options == other.options and \
|
self.options == other.options and \
|
||||||
self.exc_msg == other.exc_msg
|
self.exc_msg == other.exc_msg
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return not self == other
|
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash((self.source, self.want, self.lineno, self.indent,
|
return hash((self.source, self.want, self.lineno, self.indent,
|
||||||
self.exc_msg))
|
self.exc_msg))
|
||||||
|
@ -547,9 +544,6 @@ class DocTest:
|
||||||
self.filename == other.filename and \
|
self.filename == other.filename and \
|
||||||
self.lineno == other.lineno
|
self.lineno == other.lineno
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return not self == other
|
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash((self.docstring, self.name, self.filename, self.lineno))
|
return hash((self.docstring, self.name, self.filename, self.lineno))
|
||||||
|
|
||||||
|
@ -2289,9 +2283,6 @@ class DocTestCase(unittest.TestCase):
|
||||||
self._dt_tearDown == other._dt_tearDown and \
|
self._dt_tearDown == other._dt_tearDown and \
|
||||||
self._dt_checker == other._dt_checker
|
self._dt_checker == other._dt_checker
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return not self == other
|
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash((self._dt_optionflags, self._dt_setUp, self._dt_tearDown,
|
return hash((self._dt_optionflags, self._dt_setUp, self._dt_tearDown,
|
||||||
self._dt_checker))
|
self._dt_checker))
|
||||||
|
|
|
@ -64,16 +64,6 @@ class Base(object):
|
||||||
|
|
||||||
__hash__ = None # For Py3 compatibility.
|
__hash__ = None # For Py3 compatibility.
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
"""
|
|
||||||
Compare two nodes for inequality.
|
|
||||||
|
|
||||||
This calls the method _eq().
|
|
||||||
"""
|
|
||||||
if self.__class__ is not other.__class__:
|
|
||||||
return NotImplemented
|
|
||||||
return not self._eq(other)
|
|
||||||
|
|
||||||
def _eq(self, other):
|
def _eq(self, other):
|
||||||
"""
|
"""
|
||||||
Compare two nodes for equality.
|
Compare two nodes for equality.
|
||||||
|
|
|
@ -141,11 +141,6 @@ class Complex(Number):
|
||||||
"""self == other"""
|
"""self == other"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
"""self != other"""
|
|
||||||
# The default __ne__ doesn't negate __eq__ until 3.0.
|
|
||||||
return not (self == other)
|
|
||||||
|
|
||||||
Complex.register(complex)
|
Complex.register(complex)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -665,9 +665,6 @@ class PurePath(object):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return self._cparts == other._cparts and self._flavour is other._flavour
|
return self._cparts == other._cparts and self._flavour is other._flavour
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return not self == other
|
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
try:
|
try:
|
||||||
return self._hash
|
return self._hash
|
||||||
|
|
|
@ -194,10 +194,6 @@ class Rat(object):
|
||||||
return float(self) == other
|
return float(self) == other
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
"""Compare two Rats for inequality."""
|
|
||||||
return not self == other
|
|
||||||
|
|
||||||
class RatTestCase(unittest.TestCase):
|
class RatTestCase(unittest.TestCase):
|
||||||
"""Unit tests for Rat class and its support utilities."""
|
"""Unit tests for Rat class and its support utilities."""
|
||||||
|
|
||||||
|
|
|
@ -48,8 +48,69 @@ class ComparisonTest(unittest.TestCase):
|
||||||
def test_ne_defaults_to_not_eq(self):
|
def test_ne_defaults_to_not_eq(self):
|
||||||
a = Cmp(1)
|
a = Cmp(1)
|
||||||
b = Cmp(1)
|
b = Cmp(1)
|
||||||
self.assertTrue(a == b)
|
c = Cmp(2)
|
||||||
self.assertFalse(a != b)
|
self.assertIs(a == b, True)
|
||||||
|
self.assertIs(a != b, False)
|
||||||
|
self.assertIs(a != c, True)
|
||||||
|
|
||||||
|
def test_ne_high_priority(self):
|
||||||
|
"""object.__ne__() should allow reflected __ne__() to be tried"""
|
||||||
|
calls = []
|
||||||
|
class Left:
|
||||||
|
# Inherits object.__ne__()
|
||||||
|
def __eq__(*args):
|
||||||
|
calls.append('Left.__eq__')
|
||||||
|
return NotImplemented
|
||||||
|
class Right:
|
||||||
|
def __eq__(*args):
|
||||||
|
calls.append('Right.__eq__')
|
||||||
|
return NotImplemented
|
||||||
|
def __ne__(*args):
|
||||||
|
calls.append('Right.__ne__')
|
||||||
|
return NotImplemented
|
||||||
|
Left() != Right()
|
||||||
|
self.assertSequenceEqual(calls, ['Left.__eq__', 'Right.__ne__'])
|
||||||
|
|
||||||
|
def test_ne_low_priority(self):
|
||||||
|
"""object.__ne__() should not invoke reflected __eq__()"""
|
||||||
|
calls = []
|
||||||
|
class Base:
|
||||||
|
# Inherits object.__ne__()
|
||||||
|
def __eq__(*args):
|
||||||
|
calls.append('Base.__eq__')
|
||||||
|
return NotImplemented
|
||||||
|
class Derived(Base): # Subclassing forces higher priority
|
||||||
|
def __eq__(*args):
|
||||||
|
calls.append('Derived.__eq__')
|
||||||
|
return NotImplemented
|
||||||
|
def __ne__(*args):
|
||||||
|
calls.append('Derived.__ne__')
|
||||||
|
return NotImplemented
|
||||||
|
Base() != Derived()
|
||||||
|
self.assertSequenceEqual(calls, ['Derived.__ne__', 'Base.__eq__'])
|
||||||
|
|
||||||
|
def test_other_delegation(self):
|
||||||
|
"""No default delegation between operations except __ne__()"""
|
||||||
|
ops = (
|
||||||
|
('__eq__', lambda a, b: a == b),
|
||||||
|
('__lt__', lambda a, b: a < b),
|
||||||
|
('__le__', lambda a, b: a <= b),
|
||||||
|
('__gt__', lambda a, b: a > b),
|
||||||
|
('__ge__', lambda a, b: a >= b),
|
||||||
|
)
|
||||||
|
for name, func in ops:
|
||||||
|
with self.subTest(name):
|
||||||
|
def unexpected(*args):
|
||||||
|
self.fail('Unexpected operator method called')
|
||||||
|
class C:
|
||||||
|
__ne__ = unexpected
|
||||||
|
for other, _ in ops:
|
||||||
|
if other != name:
|
||||||
|
setattr(C, other, unexpected)
|
||||||
|
if name == '__eq__':
|
||||||
|
self.assertIs(func(C(), object()), False)
|
||||||
|
else:
|
||||||
|
self.assertRaises(TypeError, func, C(), object())
|
||||||
|
|
||||||
def test_issue_1393(self):
|
def test_issue_1393(self):
|
||||||
x = lambda: None
|
x = lambda: None
|
||||||
|
|
|
@ -1342,9 +1342,6 @@ class FunctionTestCase(TestCase):
|
||||||
self._testFunc == other._testFunc and \
|
self._testFunc == other._testFunc and \
|
||||||
self._description == other._description
|
self._description == other._description
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return not self == other
|
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash((type(self), self._setUpFunc, self._tearDownFunc,
|
return hash((type(self), self._setUpFunc, self._tearDownFunc,
|
||||||
self._testFunc, self._description))
|
self._testFunc, self._description))
|
||||||
|
|
|
@ -31,9 +31,6 @@ class BaseTestSuite(object):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return list(self) == list(other)
|
return list(self) == list(other)
|
||||||
|
|
||||||
def __ne__(self, other):
|
|
||||||
return not self == other
|
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return iter(self._tests)
|
return iter(self._tests)
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,9 @@ Release date: TBA
|
||||||
Core and Builtins
|
Core and Builtins
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
- Issue #21408: The default __ne__() now returns NotImplemented if __eq__()
|
||||||
|
returned NotImplemented.
|
||||||
|
|
||||||
- Issue #23321: Fixed a crash in str.decode() when error handler returned
|
- Issue #23321: Fixed a crash in str.decode() when error handler returned
|
||||||
replacment string longer than mailformed input data.
|
replacment string longer than mailformed input data.
|
||||||
|
|
||||||
|
@ -47,6 +50,10 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #21408: Removed incorrect implementations of __ne__() which didn't
|
||||||
|
returned NotImplemented if __eq__() returned NotImplemented. The default
|
||||||
|
__ne__() now works correctly.
|
||||||
|
|
||||||
- Issue #19996: :class:`email.feedparser.FeedParser` now handles (malformed)
|
- Issue #19996: :class:`email.feedparser.FeedParser` now handles (malformed)
|
||||||
headers with no key rather than amusing the body has started.
|
headers with no key rather than amusing the body has started.
|
||||||
|
|
||||||
|
|
|
@ -3348,9 +3348,14 @@ object_richcompare(PyObject *self, PyObject *other, int op)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Py_NE:
|
case Py_NE:
|
||||||
/* By default, != returns the opposite of ==,
|
/* By default, __ne__() delegates to __eq__() and inverts the result,
|
||||||
unless the latter returns NotImplemented. */
|
unless the latter returns NotImplemented. */
|
||||||
res = PyObject_RichCompare(self, other, Py_EQ);
|
if (self->ob_type->tp_richcompare == NULL) {
|
||||||
|
res = Py_NotImplemented;
|
||||||
|
Py_INCREF(res);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
res = (*self->ob_type->tp_richcompare)(self, other, Py_EQ);
|
||||||
if (res != NULL && res != Py_NotImplemented) {
|
if (res != NULL && res != Py_NotImplemented) {
|
||||||
int ok = PyObject_IsTrue(res);
|
int ok = PyObject_IsTrue(res);
|
||||||
Py_DECREF(res);
|
Py_DECREF(res);
|
||||||
|
|
Loading…
Reference in New Issue