Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,10 @@ def __int__(self):
return self._ip

def __eq__(self, other):
try:
return (self._ip == other._ip
and self.version == other.version)
except AttributeError:
if not isinstance(other, _BaseAddress):
return NotImplemented
return (self._ip == other._ip
and self.version == other.version)

def __lt__(self, other):
if not isinstance(other, _BaseAddress):
Expand Down Expand Up @@ -718,12 +717,11 @@ def __lt__(self, other):
return False

def __eq__(self, other):
try:
return (self.version == other.version and
self.network_address == other.network_address and
int(self.netmask) == int(other.netmask))
except AttributeError:
if not isinstance(other, _BaseNetwork):
return NotImplemented
return (self.version == other.version
and self.network_address == other.network_address
and int(self.netmask) == int(other.netmask))

def __hash__(self):
return hash((int(self.network_address), int(self.netmask)))
Expand Down
35 changes: 35 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,41 @@ def test_incompatible_versions(self):
self.assertRaises(TypeError, v6net_scoped.__lt__, v4net)
self.assertRaises(TypeError, v6net_scoped.__gt__, v4net)

def test_address_eq_with_faux_object(self):
class AlwaysTrue:
def __init__(self, ip_value):
self._ip = ip_value
self.version = 42

def __eq__(self, other):
return True

addr4 = ipaddress.IPv4Address('192.168.1.1')
always_true4 = AlwaysTrue(addr4._ip)
self.assertEqual(addr4, always_true4)

addr6 = ipaddress.IPv6Address('::1')
always_true6 = AlwaysTrue(addr6._ip)
self.assertEqual(addr6, always_true6)

intf4 = ipaddress.IPv4Interface('192.168.1.1/24')
always_true_intf4 = AlwaysTrue(intf4._ip)
self.assertEqual(intf4, always_true_intf4)

intf6 = ipaddress.IPv6Interface('::1/128')
always_true_intf6 = AlwaysTrue(intf6._ip)
self.assertEqual(intf6, always_true_intf6)

def test_network_eq_with_faux_object(self):
class AlwaysTrue:
version = 42
def __eq__(self, other):
return True

always_true = AlwaysTrue()
self.assertEqual(ipaddress.IPv4Network('43.48.0.0/12'), always_true)
self.assertEqual(ipaddress.IPv6Network('::eeff:ae3f:d473/128'), always_true)


class IpaddrUnitTest(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:mod:`ipaddress`: fix equality testing for :class:`~ipaddress.IPv4Address`,
:class:`~ipaddress.IPv6Address`, :class:`~ipaddress.IPv4Network`, and
:class:`~ipaddress.IPv6Network` to avoid comparing instances of incorrect
types.
Loading