From 9eb0afc95fc069c8f2a1b0958d228679711c44d9 Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Sat, 15 Aug 2026 19:09:44 +0800 Subject: [PATCH 1/2] gh-155836: Fix IPv6Address.reverse_pointer for addresses with a scope id _reverse_pointer derives from self.exploded, which since gh-88178 carries the %scope_id suffix. Strip the scope id before building the pointer so it is not spliced into the reversed nibbles. --- Lib/ipaddress.py | 6 +++++- Lib/test/test_ipaddress.py | 8 ++++++++ .../2026-08-15-10-00-00.gh-issue-155836.Rv6Ptr.rst | 3 +++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-08-15-10-00-00.gh-issue-155836.Rv6Ptr.rst diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index b9c9d52a257b3a..c483a1ef0a6247 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1978,7 +1978,11 @@ def _reverse_pointer(self): This implements the method described in RFC3596 2.5. """ - reverse_chars = self.exploded[::-1].replace(':', '') + exploded = self.exploded + scope_id = getattr(self, '_scope_id', None) + if scope_id: + exploded = exploded.replace('%' + scope_id, '', 1) + reverse_chars = exploded[::-1].replace(':', '') return '.'.join(reverse_chars) + '.ip6.arpa' @staticmethod diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 1796e5adfaf9be..3df78ac86bcbb7 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -2850,6 +2850,14 @@ def testReversePointer(self): '2.0.0.0.1.0.0.0.0.0.0.0.1.2.3.4.' 'ip6.arpa' ) + ), + # a scope id is not part of the pointer name + ( + 'fe80::1%eth0', ( + '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.' + '0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.' + 'ip6.arpa' + ) ) ]: with self.subTest('ipv6_reverse_pointer', addr=addr_v6): diff --git a/Misc/NEWS.d/next/Library/2026-08-15-10-00-00.gh-issue-155836.Rv6Ptr.rst b/Misc/NEWS.d/next/Library/2026-08-15-10-00-00.gh-issue-155836.Rv6Ptr.rst new file mode 100644 index 00000000000000..fda69fbfcfedf1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-15-10-00-00.gh-issue-155836.Rv6Ptr.rst @@ -0,0 +1,3 @@ +Fix :attr:`ipaddress.IPv6Address.reverse_pointer` returning an invalid name +for an address carrying a scope id; the scope id is no longer spliced into the +reverse DNS pointer. From 9c98a2ed34f2e1f6ffeb0ea4a01155ac9a756e39 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Thu, 3 Dec 2026 16:20:12 +0100 Subject: [PATCH 2/2] gh-155836: Build IPv6Address.reverse_pointer from the integer Derive the name from the address's integer value instead of stripping the scope ID out of the exploded string. This also drops the special case for IPv4-mapped addresses. Add tests for a numeric scope ID and for an IPv4-mapped address with a scope ID. --- Lib/ipaddress.py | 18 ++++-------------- Lib/test/test_ipaddress.py | 16 +++++++++++++++- ...6-08-15-10-00-00.gh-issue-155836.Rv6Ptr.rst | 4 ++-- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index c483a1ef0a6247..0656f113e3c885 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1978,11 +1978,7 @@ def _reverse_pointer(self): This implements the method described in RFC3596 2.5. """ - exploded = self.exploded - scope_id = getattr(self, '_scope_id', None) - if scope_id: - exploded = exploded.replace('%' + scope_id, '', 1) - reverse_chars = exploded[::-1].replace(':', '') + reverse_chars = self.exploded[::-1].replace(':', '') return '.'.join(reverse_chars) + '.ip6.arpa' @staticmethod @@ -2060,15 +2056,9 @@ def _explode_shorthand_ip_string(self): return f"{raw_exploded_str[:prefix_len]}{ipv4_mapped!s}" def _reverse_pointer(self): - ipv4_mapped = self.ipv4_mapped - if ipv4_mapped is None: - return super()._reverse_pointer() - prefix_len = 30 - raw_exploded_str = super()._explode_shorthand_ip_string()[:prefix_len] - # ipv4 encoded using hexadecimal nibbles instead of decimals - ipv4_int = ipv4_mapped._ip - reverse_chars = f"{raw_exploded_str}{ipv4_int:008x}"[::-1].replace(':', '') - return '.'.join(reverse_chars) + '.ip6.arpa' + # Built from the integer so that neither the scope ID nor the + # IPv4-mapped dotted form ends up in the name. + return '.'.join(f'{self._ip:032x}'[::-1]) + '.ip6.arpa' def _ipv4_mapped_ipv6_to_str(self): """Return convenient text representation of IPv4-mapped IPv6 address diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 3df78ac86bcbb7..4fc6272546b50b 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -2858,7 +2858,21 @@ def testReversePointer(self): '0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.' 'ip6.arpa' ) - ) + ), + ( + 'fe80::1%1', ( + '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.' + '0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.' + 'ip6.arpa' + ) + ), + ( + '::FFFF:192.168.1.35%eth0', ( + '3.2.1.0.8.a.0.c.f.f.f.f.0.0.0.0.' + '0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.' + 'ip6.arpa' + ) + ), ]: with self.subTest('ipv6_reverse_pointer', addr=addr_v6): addr = ipaddress.IPv6Address(addr_v6) diff --git a/Misc/NEWS.d/next/Library/2026-08-15-10-00-00.gh-issue-155836.Rv6Ptr.rst b/Misc/NEWS.d/next/Library/2026-08-15-10-00-00.gh-issue-155836.Rv6Ptr.rst index fda69fbfcfedf1..39af8e6d4fb3d3 100644 --- a/Misc/NEWS.d/next/Library/2026-08-15-10-00-00.gh-issue-155836.Rv6Ptr.rst +++ b/Misc/NEWS.d/next/Library/2026-08-15-10-00-00.gh-issue-155836.Rv6Ptr.rst @@ -1,3 +1,3 @@ Fix :attr:`ipaddress.IPv6Address.reverse_pointer` returning an invalid name -for an address carrying a scope id; the scope id is no longer spliced into the -reverse DNS pointer. +for an address with a scope ID. The scope ID is not part of the reverse DNS +name and is no longer included in it.