Skip to content

Commit 7af67a3

Browse files
Support the scope ID in IPv6Interface.exploded too
IPv6Interface.exploded silently omitted the scope ID, unlike str() and compressed. Attach it in one place, before the prefix length, so that all three representations agree. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 1693a1b commit 7af67a3

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

Lib/ipaddress.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,14 +1911,18 @@ def _explode_shorthand_ip_string(self):
19111911
elif isinstance(self, IPv6Interface):
19121912
ip_str = str(self.ip)
19131913
else:
1914-
ip_str = str(self._string_from_ip_int(self._ip))
1914+
ip_str = self._string_from_ip_int(self._ip)
19151915

19161916
ip_int = self._ip_int_from_string(ip_str)
19171917
hex_str = '%032x' % ip_int
19181918
exploded = ':'.join([hex_str[x:x+4] for x in range(0, 32, 4)])
1919-
if isinstance(self, (_BaseNetwork, IPv6Interface)):
1919+
if isinstance(self, _BaseNetwork):
19201920
return '%s/%d' % (exploded, self._prefixlen)
1921-
return exploded + '%' + self._scope_id if self._scope_id else exploded
1921+
if self._scope_id:
1922+
exploded = '%s%%%s' % (exploded, self._scope_id)
1923+
if isinstance(self, IPv6Interface):
1924+
return '%s/%d' % (exploded, self._prefixlen)
1925+
return exploded
19221926

19231927
def _reverse_pointer(self):
19241928
"""Return the reverse DNS pointer name for the IPv6 address.

Lib/test/test_ipaddress.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,10 @@ def testExplodeShortHandIpStr(self):
27202720
ipaddress.IPv6Interface('::1/128').exploded)
27212721
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0001%1',
27222722
ipaddress.IPv6Address('fe80::1%1').exploded)
2723+
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0001%eth0',
2724+
ipaddress.IPv6Address('fe80::1%eth0').exploded)
2725+
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0001%1/64',
2726+
ipaddress.IPv6Interface('fe80::1%1/64').exploded)
27232727
# issue 77
27242728
self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1',
27252729
addr2.exploded)
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
:meth:`ipaddress.IPv6Address.exploded` now supports IPv6 addresses with a ``scope_id`` (link-local addresses)
1+
:attr:`ipaddress.IPv6Address.exploded` and
2+
:attr:`ipaddress.IPv6Interface.exploded` now support addresses with a scope
3+
ID (link-local addresses).
4+
Previously the former raised :exc:`~ipaddress.AddressValueError`
5+
and the latter omitted the scope ID.

0 commit comments

Comments
 (0)