Skip to content

gh-117566: fix IPv6Address.is_loopback for IPv4-mapped loopbacks #117567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2024
Merged
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
5 changes: 4 additions & 1 deletion Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,9 @@ def is_loopback(self):
RFC 2373 2.5.3.

"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is not None:
return ipv4_mapped.is_loopback
return self._ip == 1

@property
Expand Down Expand Up @@ -2258,7 +2261,7 @@ def is_unspecified(self):

@property
def is_loopback(self):
return self._ip == 1 and self.network.is_loopback
return super().is_loopback and self.network.is_loopback


class IPv6Network(_BaseV6, _BaseNetwork):
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,22 @@ def testIpv4MappedPrivateCheck(self):
self.assertEqual(
False, ipaddress.ip_address('::ffff:172.32.0.0').is_private)

def testIpv4MappedLoopbackCheck(self):
# test networks
self.assertEqual(True, ipaddress.ip_network(
'::ffff:127.100.200.254/128').is_loopback)
self.assertEqual(True, ipaddress.ip_network(
'::ffff:127.42.0.0/112').is_loopback)
self.assertEqual(False, ipaddress.ip_network(
'::ffff:128.0.0.0').is_loopback)
# test addresses
self.assertEqual(True, ipaddress.ip_address(
'::ffff:127.100.200.254').is_loopback)
self.assertEqual(True, ipaddress.ip_address(
'::ffff:127.42.0.0').is_loopback)
self.assertEqual(False, ipaddress.ip_address(
'::ffff:128.0.0.0').is_loopback)

def testAddrExclude(self):
addr1 = ipaddress.ip_network('10.1.1.0/24')
addr2 = ipaddress.ip_network('10.1.1.0/26')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:meth:`ipaddress.IPv6Address.is_loopback` will now return ``True`` for
IPv4-mapped loopback addresses, i.e. addresses in the
``::ffff:127.0.0.0/104`` address space.
Loading