Skip to content
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

gh-109466: Add ipv6_mapped property to IPv4Address #109467

Merged
merged 10 commits into from
Nov 5, 2023
Merged
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
7 changes: 7 additions & 0 deletions Doc/library/ipaddress.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ write code that handles both IP versions correctly. Address objects are
``True`` if the address is reserved for link-local usage. See
:RFC:`3927`.

.. attribute:: ipv6_mapped

:class:`IPv4Address` object representing the IPv4-mapped IPv6 address. See :RFC:`4291`.

.. versionadded:: 3.13


.. _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

Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ and only logged in :ref:`Python Development Mode <devmode>` or on :ref:`Python
built on debug mode <debug-build>`.
(Contributed by Victor Stinner in :gh:`62948`.)

ipaddress
---------

* Add the :attr:`ipaddress.IPv4Address.ipv6_mapped` property, which returns the IPv4-mapped IPv6 address.
(Contributed by Charles Machalow in :gh:`109466`.)

opcode
------

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

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

Returns:
The IPv4-mapped IPv6 address per RFC 4291.

"""
return IPv6Address(f'::ffff:{self}')


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 @@ -303,6 +303,14 @@ def test_pickle(self):
def test_weakref(self):
weakref.ref(self.factory('192.0.2.1'))

def test_ipv6_mapped(self):
self.assertEqual(ipaddress.IPv4Address('192.168.1.1').ipv6_mapped,
ipaddress.IPv6Address('::ffff:192.168.1.1'))
self.assertEqual(ipaddress.IPv4Address('192.168.1.1').ipv6_mapped,
ipaddress.IPv6Address('::ffff:c0a8:101'))
self.assertEqual(ipaddress.IPv4Address('192.168.1.1').ipv6_mapped.ipv4_mapped,
ipaddress.IPv4Address('192.168.1.1'))


class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6):
factory = ipaddress.IPv6Address
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add the :attr:`ipaddress.IPv4Address.ipv6_mapped` property, which retuns the IPv4-mapped IPv6 address.