Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit24bd183

Browse files
miss-islingtonopavlyukblurb-it[bot]asvetlov
authored
[3.10]gh-87799: Improve the textual representation of IPv4-mapped IPv6 addresses (GH-29345) (GH-135079)
Represent IPv4-mapped IPv6 address as x:x:x:x:x:x:d.d.d.d,where the 'x's are the hexadecimal valuesof the six high-order 16-bit pieces of the address,and the 'd's are the decimal valuesof the four low-order 8-bit pieces of the address(standard IPv4 representation).(cherry picked from commitf22bf8e)Co-authored-by: opavliuk <40970635+opavlyuk@users.noreply.github.com>Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
1 parente0cb9af commit24bd183

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

19431943
self._ip=self._ip_int_from_string(addr_str)
19441944

1945+
def_explode_shorthand_ip_string(self):
1946+
ipv4_mapped=self.ipv4_mapped
1947+
ifipv4_mappedisNone:
1948+
long_form=super()._explode_shorthand_ip_string()
1949+
else:
1950+
prefix_len=30
1951+
raw_exploded_str=super()._explode_shorthand_ip_string()
1952+
long_form="%s%s"% (raw_exploded_str[:prefix_len],str(ipv4_mapped))
1953+
returnlong_form
1954+
1955+
def_ipv4_mapped_ipv6_to_str(self):
1956+
"""Return convenient text representation of IPv4-mapped IPv6 address
1957+
1958+
See RFC 4291 2.5.5.2, 2.2 p.3 for details.
1959+
1960+
Returns:
1961+
A string, 'x:x:x:x:x:x:d.d.d.d', where the 'x's are the hexadecimal values of
1962+
the six high-order 16-bit pieces of the address, and the 'd's are
1963+
the decimal values of the four low-order 8-bit pieces of the
1964+
address (standard IPv4 representation) as defined in RFC 4291 2.2 p.3.
1965+
1966+
"""
1967+
ipv4_mapped=self.ipv4_mapped
1968+
ifipv4_mappedisNone:
1969+
raiseAddressValueError("Can not apply to non-IPv4-mapped IPv6 address %s"%str(self))
1970+
high_order_bits=self._ip>>32
1971+
return"%s:%s"% (self._string_from_ip_int(high_order_bits),str(ipv4_mapped))
1972+
19451973
def__str__(self):
1946-
ip_str=super().__str__()
1974+
ipv4_mapped=self.ipv4_mapped
1975+
ifipv4_mappedisNone:
1976+
ip_str=super().__str__()
1977+
else:
1978+
ip_str=self._ipv4_mapped_ipv6_to_str()
19471979
returnip_str+'%'+self._scope_idifself._scope_idelseip_str
19481980

19491981
def__hash__(self):

‎Lib/test/test_ipaddress.py‎

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

1325+
deftestIPv6IPv4MappedStringRepresentation(self):
1326+
long_prefix='0000:0000:0000:0000:0000:ffff:'
1327+
short_prefix='::ffff:'
1328+
ipv4='1.2.3.4'
1329+
ipv6_ipv4_str=short_prefix+ipv4
1330+
ipv6_ipv4_addr=ipaddress.IPv6Address(ipv6_ipv4_str)
1331+
ipv6_ipv4_iface=ipaddress.IPv6Interface(ipv6_ipv4_str)
1332+
self.assertEqual(str(ipv6_ipv4_addr),ipv6_ipv4_str)
1333+
self.assertEqual(ipv6_ipv4_addr.exploded,long_prefix+ipv4)
1334+
self.assertEqual(str(ipv6_ipv4_iface.ip),ipv6_ipv4_str)
1335+
13251336
deftestGetScopeId(self):
13261337
self.assertEqual(self.ipv6_address.scope_id,
13271338
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)

[8]ページ先頭

©2009-2025 Movatter.jp