Kaydet (Commit) cf58dfb4 authored tarafından Serhiy Storchaka's avatar Serhiy Storchaka Kaydeden (comit) GitHub

[3.6] bpo-29931 fix __lt__ check in ipaddress.ip_interface for both v4 and v6. (GH-879) (#2217)

the original logic was just comparing the network address
but this is wrong because if the network address is equal then
we need to compare the ip address for breaking the tie

add more ip_interface comparison tests.
(cherry picked from commit 7bd8d3e7)
üst f75f6edb
...@@ -1410,7 +1410,8 @@ class IPv4Interface(IPv4Address): ...@@ -1410,7 +1410,8 @@ class IPv4Interface(IPv4Address):
if address_less is NotImplemented: if address_less is NotImplemented:
return NotImplemented return NotImplemented
try: try:
return self.network < other.network return (self.network < other.network or
self.network == other.network and address_less)
except AttributeError: except AttributeError:
# We *do* allow addresses and interfaces to be sorted. The # We *do* allow addresses and interfaces to be sorted. The
# unassociated address is considered less than all interfaces. # unassociated address is considered less than all interfaces.
...@@ -2100,7 +2101,8 @@ class IPv6Interface(IPv6Address): ...@@ -2100,7 +2101,8 @@ class IPv6Interface(IPv6Address):
if address_less is NotImplemented: if address_less is NotImplemented:
return NotImplemented return NotImplemented
try: try:
return self.network < other.network return (self.network < other.network or
self.network == other.network and address_less)
except AttributeError: except AttributeError:
# We *do* allow addresses and interfaces to be sorted. The # We *do* allow addresses and interfaces to be sorted. The
# unassociated address is considered less than all interfaces. # unassociated address is considered less than all interfaces.
......
...@@ -1405,14 +1405,35 @@ class IpaddrUnitTest(unittest.TestCase): ...@@ -1405,14 +1405,35 @@ class IpaddrUnitTest(unittest.TestCase):
ipaddress.ip_address('::2')) ipaddress.ip_address('::2'))
def testInterfaceComparison(self): def testInterfaceComparison(self):
self.assertTrue(ipaddress.ip_interface('1.1.1.1') <= self.assertTrue(ipaddress.ip_interface('1.1.1.1/24') ==
ipaddress.ip_interface('1.1.1.1')) ipaddress.ip_interface('1.1.1.1/24'))
self.assertTrue(ipaddress.ip_interface('1.1.1.1') <= self.assertTrue(ipaddress.ip_interface('1.1.1.1/16') <
ipaddress.ip_interface('1.1.1.2')) ipaddress.ip_interface('1.1.1.1/24'))
self.assertTrue(ipaddress.ip_interface('::1') <= self.assertTrue(ipaddress.ip_interface('1.1.1.1/24') <
ipaddress.ip_interface('::1')) ipaddress.ip_interface('1.1.1.2/24'))
self.assertTrue(ipaddress.ip_interface('::1') <= self.assertTrue(ipaddress.ip_interface('1.1.1.2/16') <
ipaddress.ip_interface('::2')) ipaddress.ip_interface('1.1.1.1/24'))
self.assertTrue(ipaddress.ip_interface('1.1.1.1/24') >
ipaddress.ip_interface('1.1.1.1/16'))
self.assertTrue(ipaddress.ip_interface('1.1.1.2/24') >
ipaddress.ip_interface('1.1.1.1/24'))
self.assertTrue(ipaddress.ip_interface('1.1.1.1/24') >
ipaddress.ip_interface('1.1.1.2/16'))
self.assertTrue(ipaddress.ip_interface('::1/64') ==
ipaddress.ip_interface('::1/64'))
self.assertTrue(ipaddress.ip_interface('::1/64') <
ipaddress.ip_interface('::1/80'))
self.assertTrue(ipaddress.ip_interface('::1/64') <
ipaddress.ip_interface('::2/64'))
self.assertTrue(ipaddress.ip_interface('::2/48') <
ipaddress.ip_interface('::1/64'))
self.assertTrue(ipaddress.ip_interface('::1/80') >
ipaddress.ip_interface('::1/64'))
self.assertTrue(ipaddress.ip_interface('::2/64') >
ipaddress.ip_interface('::1/64'))
self.assertTrue(ipaddress.ip_interface('::1/64') >
ipaddress.ip_interface('::2/48'))
def testNetworkComparison(self): def testNetworkComparison(self):
# ip1 and ip2 have the same network address # ip1 and ip2 have the same network address
......
...@@ -51,6 +51,9 @@ Core and Builtins ...@@ -51,6 +51,9 @@ Core and Builtins
Library Library
------- -------
- bpo-29931: Fixed comparison check for ipaddress.ip_interface objects.
Patch by Sanjay Sundaresan.
- bpo-30605: re.compile() no longer raises a BytesWarning when compiling a - bpo-30605: re.compile() no longer raises a BytesWarning when compiling a
bytes instance with misplaced inline modifier. Patch by Roy Williams. bytes instance with misplaced inline modifier. Patch by Roy Williams.
......
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