Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f38f9cf

Browse files
miss-islingtonopavlyukasvetlov
authored
[3.11] gh-87799: Improve the textual representation of IPv4-mapped IPv6 addresses (GH-29345) (GH-135080)
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: Andrew Svetlov <[email protected]>
1 parent f8b4421 commit f38f9cf

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
@@ -1957,8 +1957,40 @@ def __init__(self, address):
19571957

19581958
self._ip = self._ip_int_from_string(addr_str)
19591959

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

19641996
def __hash__(self):

Lib/test/test_ipaddress.py

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

1331+
def testIPv6IPv4MappedStringRepresentation(self):
1332+
long_prefix = '0000:0000:0000:0000:0000:ffff:'
1333+
short_prefix = '::ffff:'
1334+
ipv4 = '1.2.3.4'
1335+
ipv6_ipv4_str = short_prefix + ipv4
1336+
ipv6_ipv4_addr = ipaddress.IPv6Address(ipv6_ipv4_str)
1337+
ipv6_ipv4_iface = ipaddress.IPv6Interface(ipv6_ipv4_str)
1338+
self.assertEqual(str(ipv6_ipv4_addr), ipv6_ipv4_str)
1339+
self.assertEqual(ipv6_ipv4_addr.exploded, long_prefix + ipv4)
1340+
self.assertEqual(str(ipv6_ipv4_iface.ip), ipv6_ipv4_str)
1341+
13311342
def testGetScopeId(self):
13321343
self.assertEqual(self.ipv6_address.scope_id,
13331344
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)