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

gh-87799: Improve the textual representation of IPv4-mapped IPv6 addresses#29345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
asvetlov merged 13 commits intopython:mainfromopavlyuk:fix-issue-43633
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
3407d76
📜🤖 Added by blurb_it.
blurb-it[bot]Oct 31, 2021
118e335
bpo-43633 Improve the textual representation of IPv4-mapped IPv6 addr…
opavlyukApr 13, 2023
f3642ca
bpo-43633 Update tests
opavlyukApr 13, 2023
669cc90
bpo-43633 Fix tests
opavlyukApr 13, 2023
8be267a
Merge branch 'main' into fix-issue-43633
opavlyukApr 13, 2023
4e7ad6b
Merge branch 'main' into fix-issue-43633
asvetlovJul 27, 2023
c2842ce
Add failed test
asvetlovJul 31, 2023
a06e745
Merge branch 'main' into fix-issue-43633
asvetlovJul 31, 2023
97bd01c
User shorter variable names
asvetlovJul 31, 2023
2d2deea
Revert test back
asvetlovJul 31, 2023
105d82a
Merge branch 'main' into fix-issue-43633
asvetlovJul 31, 2023
e8a7015
Update 2021-10-31-16-06-28.bpo-43633.vflwXv.rst
asvetlovJul 31, 2023
d74759b
Update 2021-10-31-16-06-28.bpo-43633.vflwXv.rst
asvetlovJul 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletionLib/ipaddress.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1923,8 +1923,40 @@ def __init__(self, address):

self._ip = self._ip_int_from_string(addr_str)

def _explode_shorthand_ip_string(self):
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is None:
long_form = super()._explode_shorthand_ip_string()
else:
prefix_len = 30
raw_exploded_str = super()._explode_shorthand_ip_string()
long_form = "%s%s" % (raw_exploded_str[:prefix_len], str(ipv4_mapped))
return long_form

def _ipv4_mapped_ipv6_to_str(self):
"""Return convenient text representation of IPv4-mapped IPv6 address

See RFC 4291 2.5.5.2, 2.2 p.3 for details.

Returns:
A string, '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) as defined in RFC 4291 2.2 p.3.

"""
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is None:
raise AddressValueError("Can not apply to non-IPv4-mapped IPv6 address %s" % str(self))
high_order_bits = self._ip >> 32
return "%s:%s" % (self._string_from_ip_int(high_order_bits), str(ipv4_mapped))

def __str__(self):
ip_str = super().__str__()
ipv4_mapped = self.ipv4_mapped
if ipv4_mapped is None:
ip_str = super().__str__()
else:
ip_str = self._ipv4_mapped_ipv6_to_str()
return ip_str + '%' + self._scope_id if self._scope_id else ip_str

def __hash__(self):
Expand Down
11 changes: 11 additions & 0 deletionsLib/test/test_ipaddress.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1321,6 +1321,17 @@ def testGetIp(self):
self.assertEqual(str(self.ipv6_scoped_interface.ip),
'2001:658:22a:cafe:200::1')

deftestIPv6IPv4MappedStringRepresentation(self):
long_prefix='0000:0000:0000:0000:0000:ffff:'
short_prefix='::ffff:'
ipv4='1.2.3.4'
ipv6_ipv4_str=short_prefix+ipv4
ipv6_ipv4_addr=ipaddress.IPv6Address(ipv6_ipv4_str)
ipv6_ipv4_iface=ipaddress.IPv6Interface(ipv6_ipv4_str)
self.assertEqual(str(ipv6_ipv4_addr),ipv6_ipv4_str)
self.assertEqual(ipv6_ipv4_addr.exploded,long_prefix+ipv4)
self.assertEqual(str(ipv6_ipv4_iface.ip),ipv6_ipv4_str)

deftestGetScopeId(self):
self.assertEqual(self.ipv6_address.scope_id,
None)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +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.

[8]ページ先頭

©2009-2025 Movatter.jp