mirror of
https://github.com/python/cpython.git
synced 2025-11-02 11:08:57 +00:00
Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__
This commit is contained in:
parent
d46f7d209b
commit
9a9c28ce7a
3 changed files with 25 additions and 7 deletions
|
|
@ -651,12 +651,12 @@ class _BaseNetwork(_IPAddressBase):
|
||||||
return not lt
|
return not lt
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if not isinstance(other, _BaseNetwork):
|
try:
|
||||||
raise TypeError('%s and %s are not of the same type' % (
|
return (self._version == other._version and
|
||||||
self, other))
|
self.network_address == other.network_address and
|
||||||
return (self._version == other._version and
|
int(self.netmask) == int(other.netmask))
|
||||||
self.network_address == other.network_address and
|
except AttributeError:
|
||||||
int(self.netmask) == int(other.netmask))
|
return NotImplemented
|
||||||
|
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
eq = self.__eq__(other)
|
eq = self.__eq__(other)
|
||||||
|
|
|
||||||
|
|
@ -462,7 +462,6 @@ class IpaddrUnitTest(unittest.TestCase):
|
||||||
self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128))
|
self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128))
|
||||||
self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network))
|
self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network))
|
||||||
self.assertEqual('0x1020318', hex(self.ipv4_network))
|
self.assertEqual('0x1020318', hex(self.ipv4_network))
|
||||||
self.assertRaises(TypeError, self.ipv4_network.__eq__, object())
|
|
||||||
|
|
||||||
def testMissingAddressVersion(self):
|
def testMissingAddressVersion(self):
|
||||||
class Broken(ipaddress._BaseAddress):
|
class Broken(ipaddress._BaseAddress):
|
||||||
|
|
@ -496,6 +495,22 @@ class IpaddrUnitTest(unittest.TestCase):
|
||||||
self.assertEqual(str(self.ipv6_network.hostmask),
|
self.assertEqual(str(self.ipv6_network.hostmask),
|
||||||
'::ffff:ffff:ffff:ffff')
|
'::ffff:ffff:ffff:ffff')
|
||||||
|
|
||||||
|
def testEqualityChecks(self):
|
||||||
|
# __eq__ should never raise TypeError directly
|
||||||
|
other = object()
|
||||||
|
def assertEqualityNotImplemented(instance):
|
||||||
|
self.assertEqual(instance.__eq__(other), NotImplemented)
|
||||||
|
self.assertEqual(instance.__ne__(other), NotImplemented)
|
||||||
|
self.assertFalse(instance == other)
|
||||||
|
self.assertTrue(instance != other)
|
||||||
|
|
||||||
|
assertEqualityNotImplemented(self.ipv4_address)
|
||||||
|
assertEqualityNotImplemented(self.ipv4_network)
|
||||||
|
assertEqualityNotImplemented(self.ipv4_interface)
|
||||||
|
assertEqualityNotImplemented(self.ipv6_address)
|
||||||
|
assertEqualityNotImplemented(self.ipv6_network)
|
||||||
|
assertEqualityNotImplemented(self.ipv6_interface)
|
||||||
|
|
||||||
def testBadVersionComparison(self):
|
def testBadVersionComparison(self):
|
||||||
# These should always raise TypeError
|
# These should always raise TypeError
|
||||||
v4addr = ipaddress.ip_address('1.1.1.1')
|
v4addr = ipaddress.ip_address('1.1.1.1')
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,9 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #14814: ipaddress network objects correctly return NotImplemented
|
||||||
|
when compared to arbitrary objects instead of raising TypeError
|
||||||
|
|
||||||
- Issue #14990: Correctly fail with SyntaxError on invalid encoding
|
- Issue #14990: Correctly fail with SyntaxError on invalid encoding
|
||||||
declaration.
|
declaration.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue