diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index b9c9d52a257b3a..0656f113e3c885 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -2056,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 1796e5adfaf9be..4fc6272546b50b 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -2850,7 +2850,29 @@ 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' + ) + ), + ( + '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 new file mode 100644 index 00000000000000..39af8e6d4fb3d3 --- /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 with a scope ID. The scope ID is not part of the reverse DNS +name and is no longer included in it.