summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorCharles Machalow <csm10495@gmail.com>2023-11-05 16:50:31 (GMT)
committerGitHub <noreply@github.com>2023-11-05 16:50:31 (GMT)
commitba8aa1fd3735aa3dd13a36ad8f059a422d25ff37 (patch)
tree37338f169a6502ed9ccb371c07d7e4a23e58aaa6 /Lib
parent24b5cbd3dce3fe37cdc787ccedd1e73a4f8cfc3c (diff)
downloadcpython-ba8aa1fd3735aa3dd13a36ad8f059a422d25ff37.zip
cpython-ba8aa1fd3735aa3dd13a36ad8f059a422d25ff37.tar.gz
cpython-ba8aa1fd3735aa3dd13a36ad8f059a422d25ff37.tar.bz2
gh-109466: Add ipv6_mapped property to IPv4Address (#109467)
Adds the `IPv4Address.ipv6_mapped` property. Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/ipaddress.py10
-rw-r--r--Lib/test/test_ipaddress.py8
2 files changed, 18 insertions, 0 deletions
diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py
index 68ddfbe..e398cc1 100644
--- a/Lib/ipaddress.py
+++ b/Lib/ipaddress.py
@@ -1389,6 +1389,16 @@ class IPv4Address(_BaseV4, _BaseAddress):
"""
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):
diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py
index 33a0f98..b4952ac 100644
--- a/Lib/test/test_ipaddress.py
+++ b/Lib/test/test_ipaddress.py
@@ -303,6 +303,14 @@ class AddressTestCase_v4(BaseTestCase, CommonTestMixin_v4):
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