Skip to content

Commit 9c98a2e

Browse files
committed
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.
1 parent 9eb0afc commit 9c98a2e

3 files changed

Lines changed: 21 additions & 17 deletions

File tree

‎Lib/ipaddress.py‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,11 +1978,7 @@ def _reverse_pointer(self):
19781978
This implements the method described in RFC3596 2.5.
19791979
19801980
"""
1981-
exploded = self.exploded
1982-
scope_id = getattr(self, '_scope_id', None)
1983-
if scope_id:
1984-
exploded = exploded.replace('%' + scope_id, '', 1)
1985-
reverse_chars = exploded[::-1].replace(':', '')
1981+
reverse_chars = self.exploded[::-1].replace(':', '')
19861982
return '.'.join(reverse_chars) + '.ip6.arpa'
19871983

19881984
@staticmethod
@@ -2060,15 +2056,9 @@ def _explode_shorthand_ip_string(self):
20602056
return f"{raw_exploded_str[:prefix_len]}{ipv4_mapped!s}"
20612057

20622058
def _reverse_pointer(self):
2063-
ipv4_mapped = self.ipv4_mapped
2064-
if ipv4_mapped is None:
2065-
return super()._reverse_pointer()
2066-
prefix_len = 30
2067-
raw_exploded_str = super()._explode_shorthand_ip_string()[:prefix_len]
2068-
# ipv4 encoded using hexadecimal nibbles instead of decimals
2069-
ipv4_int = ipv4_mapped._ip
2070-
reverse_chars = f"{raw_exploded_str}{ipv4_int:008x}"[::-1].replace(':', '')
2071-
return '.'.join(reverse_chars) + '.ip6.arpa'
2059+
# Built from the integer so that neither the scope ID nor the
2060+
# IPv4-mapped dotted form ends up in the name.
2061+
return '.'.join(f'{self._ip:032x}'[::-1]) + '.ip6.arpa'
20722062

20732063
def _ipv4_mapped_ipv6_to_str(self):
20742064
"""Return convenient text representation of IPv4-mapped IPv6 address

‎Lib/test/test_ipaddress.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2858,7 +2858,21 @@ def testReversePointer(self):
28582858
'0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.'
28592859
'ip6.arpa'
28602860
)
2861-
)
2861+
),
2862+
(
2863+
'fe80::1%1', (
2864+
'1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.'
2865+
'0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.'
2866+
'ip6.arpa'
2867+
)
2868+
),
2869+
(
2870+
'::FFFF:192.168.1.35%eth0', (
2871+
'3.2.1.0.8.a.0.c.f.f.f.f.0.0.0.0.'
2872+
'0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.'
2873+
'ip6.arpa'
2874+
)
2875+
),
28622876
]:
28632877
with self.subTest('ipv6_reverse_pointer', addr=addr_v6):
28642878
addr = ipaddress.IPv6Address(addr_v6)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Fix :attr:`ipaddress.IPv6Address.reverse_pointer` returning an invalid name
2-
for an address carrying a scope id; the scope id is no longer spliced into the
3-
reverse DNS pointer.
2+
for an address with a scope ID. The scope ID is not part of the reverse DNS
3+
name and is no longer included in it.

0 commit comments

Comments
 (0)