Skip to content

Commit 96b88df

Browse files
committed
Fix is_loopback() for IPv4-mapped loopbacks
While properties like is_private account for IPv4-mapped IPv6 addresses, such as for example: >>> ipaddress.ip_address("192.168.0.1").is_private True >>> ipaddress.ip_address("::ffff:192.168.0.1").is_private True ...the same doesn't currently apply to the is_loopback property: >>> ipaddress.ip_address("127.0.0.1").is_loopback True >>> ipaddress.ip_address("::ffff:127.0.0.1").is_loopback False At minimum, this inconsistency between different properties is counter-intuitive. Moreover, ::ffff:127.0.0.0/112 is for all intents and purposes a loopback address, and should be treated as such. For the record, this will now match the behavior of other languages such as Rust, Go and .NET, cf. rust-lang/rust#69772.
1 parent 9ceaee7 commit 96b88df

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Lib/ipaddress.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,9 @@ def is_loopback(self):
21422142
RFC 2373 2.5.3.
21432143
21442144
"""
2145+
ipv4_mapped = self.ipv4_mapped
2146+
if ipv4_mapped is not None:
2147+
return ipv4_mapped.is_loopback
21452148
return self._ip == 1
21462149

21472150
@property
@@ -2258,7 +2261,7 @@ def is_unspecified(self):
22582261

22592262
@property
22602263
def is_loopback(self):
2261-
return self._ip == 1 and self.network.is_loopback
2264+
return super().is_loopback and self.network.is_loopback
22622265

22632266

22642267
class IPv6Network(_BaseV6, _BaseNetwork):

0 commit comments

Comments
 (0)