8000 gh-122792: Make IPv4-mapped IPv6 address properties consistent with I… · python/cpython@76a1c5d · GitHub
[go: up one dir, main page]

Skip to content

Commit 76a1c5d

Browse files
authored
gh-122792: Make IPv4-mapped IPv6 address properties consistent with IPv4 (GH-122793)
Make IPv4-mapped IPv6 address properties consistent with IPv4.
1 parent 033510e commit 76a1c5d

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Lib/ipaddress.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,9 @@ def is_multicast(self):
20292029
See RFC 2373 2.7 for details.
20302030
20312031
"""
2032+
ipv4_mapped = self.ipv4_mapped
2033+
if ipv4_mapped is not None:
2034+
return ipv4_mapped.is_multicast
20322035
return self in self._constants._multicast_network
20332036

20342037
@property
@@ -2040,6 +2043,9 @@ def is_reserved(self):
20402043
reserved IPv6 Network ranges.
20412044
20422045
"""
2046+
ipv4_mapped = self.ipv4_mapped
2047+
if ipv4_mapped is not None:
2048+
return ipv4_mapped.is_reserved
20432049
return any(self in x for x in self._constants._reserved_networks)
20442050

20452051
@property
@@ -2050,6 +2056,9 @@ def is_link_local(self):
20502056
A boolean, True if the address is reserved per RFC 4291.
20512057
20522058
"""
2059+
ipv4_mapped = self.ipv4_mapped
2060+
if ipv4_mapped is not None:
2061+
return ipv4_mapped.is_link_local
20532062
return self in self._constants._linklocal_network
20542063

20552064
@property
@@ -2106,6 +2115,9 @@ def is_global(self):
21062115
``is_global`` has value opposite to :attr:`is_private`, except for the ``100.64.0.0/10``
21072116
IPv4 range where they are both ``False``.
21082117
"""
2118+
ipv4_mapped = self.ipv4_mapped
2119+
if ipv4_mapped is not None:
2120+
return ipv4_mapped.is_global
21092121
return not self.is_private
21102122

21112123
@property
@@ -2117,6 +2129,9 @@ def is_unspecified(self):
21172129
RFC 2373 2.5.2.
21182130
21192131
"""
2132+
ipv4_mapped = self.ipv4_mapped
2133+
if ipv4_mapped is not None:
2134+
return ipv4_mapped.is_unspecified
21202135
return self._ip == 0
21212136

21222137
@property

Lib/test/test_ipaddress.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,30 @@ def testIpv4Mapped(self):
24462446
self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped,
24472447
ipaddress.ip_address('192.168.1.1'))
24482448

2449+
def testIpv4MappedProperties(self):
2450+
# Test that an IPv4 mapped IPv6 address has
2451+
# the same properties as an IPv4 address.
2452+
for addr4 in (
2453+
"178.62.3.251", # global
2454+
"169.254.169.254", # link local
2455+
"127.0.0.1", # loopback
2456+
"224.0.0.1", # multicast
2457+
"192.168.0.1", # private
2458+
"0.0.0.0", # unspecified
2459+
"100.64.0.1", # public and not global
2460+
):
2461+
with self.subTest(addr4):
2462+
ipv4 = ipaddress.IPv4Address(addr4)
2463+
ipv6 = ipaddress.IPv6Address(f"::ffff:{addr4}")
2464+
2465+
self.assertEqual(ipv4.is_global, ipv6.is_global)
2466+
self.assertEqual(ipv4.is_private, ipv6.is_private)
2467+
self.assertEqual(ipv4.is_reserved, ipv6.is_reserved)
2468+
self.assertEqual(ipv4.is_multicast, ipv6.is_multicast)
2469+
self.assertEqual(ipv4.is_unspecified, ipv6.is_unspecified)
2470+
self.assertEqual(ipv4.is_link_local, ipv6.is_link_local)
2471+
self.assertEqual(ipv4.is_loopback, ipv6.is_loopback)
2472+
24492473
def testIpv4MappedPrivateCheck(self):
24502474
self.assertEqual(
24512475
True, ipaddress.ip_address('::ffff:192.168.1.1').is_private)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Changed IPv4-mapped ``ipaddress.IPv6Address`` to consistently use the mapped IPv4
2+
address value for deciding properties. Properties which have their behavior fixed
3+
are ``is_multicast``, ``is_reserved``, ``is_link_local``, ``is_global``, and ``is_unspecified``.

0 commit comments

Comments
 (0)
0