Skip to content

Commit f3272d8

Browse files
miss-islingtonopavlyukblurb-it[bot]asvetlov
authored
[3.12] gh-87799: Improve the textual representation of IPv4-mapped IPv6 addresses (GH-29345) (GH-135081)
Represent IPv4-mapped IPv6 address as x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values of the six high-order 16-bit pieces of the address, and the 'd's are the decimal values of the four low-order 8-bit pieces of the address (standard IPv4 representation). (cherry picked from commit f22bf8e) Co-authored-by: opavliuk <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Andrew Svetlov <[email protected]>
1 parent d4cf1fa commit f3272d8

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

Lib/ipaddress.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1964,8 +1964,40 @@ def __init__(self, address):
19641964

19651965
self._ip = self._ip_int_from_string(addr_str)
19661966

1967+
def _explode_shorthand_ip_string(self):
1968+
ipv4_mapped = self.ipv4_mapped
1969+
if ipv4_mapped is None:
1970+
long_form = super()._explode_shorthand_ip_string()
1971+
else:
1972+
prefix_len = 30
1973+
raw_exploded_str = super()._explode_shorthand_ip_string()
1974+
long_form = "%s%s" % (raw_exploded_str[:prefix_len], str(ipv4_mapped))
1975+
return long_form
1976+
1977+
def _ipv4_mapped_ipv6_to_str(self):
1978+
"""Return convenient text representation of IPv4-mapped IPv6 address
1979+
1980+
See RFC 4291 2.5.5.2, 2.2 p.3 for details.
1981+
1982+
Returns:
1983+
A string, 'x:x:x:x:x:x:d.d.d.d', where the 'x's are the hexadecimal values of
1984+
the six high-order 16-bit pieces of the address, and the 'd's are
1985+
the decimal values of the four low-order 8-bit pieces of the
1986+
address (standard IPv4 representation) as defined in RFC 4291 2.2 p.3.
1987+
1988+
"""
1989+
ipv4_mapped = self.ipv4_mapped
1990+
if ipv4_mapped is None:
1991+
raise AddressValueError("Can not apply to non-IPv4-mapped IPv6 address %s" % str(self))
1992+
high_order_bits = self._ip >> 32
1993+
return "%s:%s" % (self._string_from_ip_int(high_order_bits), str(ipv4_mapped))
1994+
19671995
def __str__(self):
1968-
ip_str = super().__str__()
1996+
ipv4_mapped = self.ipv4_mapped
1997+
if ipv4_mapped is None:
1998+
ip_str = super().__str__()
1999+
else:
2000+
ip_str = self._ipv4_mapped_ipv6_to_str()
19692001
return ip_str + '%' + self._scope_id if self._scope_id else ip_str
19702002

19712003
def __hash__(self):

Lib/test/test_ipaddress.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,17 @@ def testGetIp(self):
13401340
self.assertEqual(str(self.ipv6_scoped_interface.ip),
13411341
'2001:658:22a:cafe:200::1')
13421342

1343+
def testIPv6IPv4MappedStringRepresentation(self):
1344+
long_prefix = '0000:0000:0000:0000:0000:ffff:'
1345+
short_prefix = '::ffff:'
1346+
ipv4 = '1.2.3.4'
1347+
ipv6_ipv4_str = short_prefix + ipv4
1348+
ipv6_ipv4_addr = ipaddress.IPv6Address(ipv6_ipv4_str)
1349+
ipv6_ipv4_iface = ipaddress.IPv6Interface(ipv6_ipv4_str)
1350+
self.assertEqual(str(ipv6_ipv4_addr), ipv6_ipv4_str)
1351+
self.assertEqual(ipv6_ipv4_addr.exploded, long_prefix + ipv4)
1352+
self.assertEqual(str(ipv6_ipv4_iface.ip), ipv6_ipv4_str)
1353+
13431354
def testGetScopeId(self):
13441355
self.assertEqual(self.ipv6_address.scope_id,
13451356
None)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve the textual representation of IPv4-mapped IPv6 addresses (:rfc:`4291` Sections 2.2, 2.5.5.2) in :mod:`ipaddress`. Patch by Oleksandr Pavliuk.

0 commit comments

Comments
 (0)