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

Skip to content

gh-92573: Add IPv4Address.ipv6_mapped attribute #92581

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion Doc/library/ipaddress.rst
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ write code that handles both IP versions correctly. Address objects are
.. _iana-ipv4-special-registry: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
.. _iana-ipv6-special-registry: https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml

.. attribute:: ipv6_mapped

This property gives the IPv4-mapped IPv6 address corresponding to this IPv4
address. The resulting :class:`IPv6Address` is in the range
``::ffff:0:0/96`` as defined by :RFC:`4291`. See also
:attr:`~IPv6Address.ipv4_mapped`.

.. method:: IPv4Address.__format__(fmt)

Returns a string representation of the IP address, controlled by
Expand Down Expand Up @@ -321,7 +328,8 @@ write code that handles both IP versions correctly. Address objects are

For addresses that appear to be IPv4 mapped addresses (starting with
``::FFFF/96``), this property will report the embedded IPv4 address.
For any other address, this property will be ``None``.
For any other address, this property will be ``None``. See also
:attr:`~IPv4Address.ipv6_mapped`.

.. attribute:: scope_id

Expand Down
9 changes: 9 additions & 0 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,15 @@ def is_link_local(self):
"""
return self in self._constants._linklocal_network

@property
def ipv6_mapped(self):
"""Return the IPv4-mapped IPv6 address corresponding to this IPv4 address.

Returns:
The corresponding IPv4-mapped IPv6 address.
"""
return IPv6Address(0xffff_0000_0000 | self._ip)


class IPv4Interface(IPv4Address):

Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2368,6 +2368,14 @@ def testIpv4MappedPrivateCheck(self):
self.assertEqual(
False, ipaddress.ip_address('::ffff:172.32.0.0').is_private)

def testIpv6Mapped(self):
self.assertEqual(
ipaddress.ip_address('1.2.3.4').ipv6_mapped,
ipaddress.ip_address('::ffff:102:304'))
self.assertEqual(
ipaddress.ip_address('1.2.3.4').ipv6_mapped,
ipaddress.ip_address('::ffff:1.2.3.4'))

def testAddrExclude(self):
addr1 = ipaddress.ip_network('10.1.1.0/24')
addr2 = ipaddress.ip_network('10.1.1.0/26')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :attr:`ipaddress.IPv4Address.ipv6_mapped` to mirror
:attr:`ipaddress.IPv6Address.ipv4_mapped`.