Skip to content

Commit 3902135

Browse files
committed
bpo-44012: IPv6Address.exploded with scope_id
Support for scoped IPv6 addresses was implemented in bpo-34788 but missed implementing the .exploded method: >>> import ipaddress >>> ipaddress.IPv6Address('fe80::1%eth0').exploded Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.9/ipaddress.py", line 394, in exploded return self._explode_shorthand_ip_string() File "/usr/lib/python3.9/ipaddress.py", line 1824, in _explode_shorthand_ip_string ip_int = self._ip_int_from_string(ip_str) File "/usr/lib/python3.9/ipaddress.py", line 1705, in _ip_int_from_string raise AddressValueError("%s in %r" % (exc, ip_str)) from None ipaddress.AddressValueError: Only hex digits permitted in '1%eth0' in 'fe80::1%eth0' After this commit: >>> import ipaddress >>> ipaddress.IPv6Address('fe80::1%eth0').exploded 'fe80:0000:0000:0000:0000:0000:0000:0001%eth0'
1 parent 5fb06ed commit 3902135

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

Lib/ipaddress.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,9 +1812,6 @@ def _string_from_ip_int(cls, ip_int=None):
18121812
def _explode_shorthand_ip_string(self):
18131813
"""Expand a shortened IPv6 address.
18141814
1815-
Args:
1816-
ip_str: A string, the IPv6 address.
1817-
18181815
Returns:
18191816
A string, the expanded IPv6 address.
18201817
@@ -1824,14 +1821,14 @@ def _explode_shorthand_ip_string(self):
18241821
elif isinstance(self, IPv6Interface):
18251822
ip_str = str(self.ip)
18261823
else:
1827-
ip_str = str(self)
1824+
ip_str = str(self._string_from_ip_int(self._ip))
18281825

18291826
ip_int = self._ip_int_from_string(ip_str)
18301827
hex_str = '%032x' % ip_int
1831-
parts = [hex_str[x:x+4] for x in range(0, 32, 4)]
1828+
exploded = ':'.join([hex_str[x:x+4] for x in range(0, 32, 4)])
18321829
if isinstance(self, (_BaseNetwork, IPv6Interface)):
1833-
return '%s/%d' % (':'.join(parts), self._prefixlen)
1834-
return ':'.join(parts)
1830+
return '%s/%d' % (exploded, self._prefixlen)
1831+
return exploded + '%' + self._scope_id if self._scope_id else exploded
18351832

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

Lib/test/test_ipaddress.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2454,6 +2454,8 @@ def testExplodeShortHandIpStr(self):
24542454
addr1.exploded)
24552455
self.assertEqual('0000:0000:0000:0000:0000:0000:0000:0001/128',
24562456
ipaddress.IPv6Interface('::1/128').exploded)
2457+
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0001%1',
2458+
ipaddress.IPv6Address('fe80::1%1').exploded)
24572459
# issue 77
24582460
self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1',
24592461
addr2.exploded)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:meth:`ipaddress.IPv6Address.exploded` now supports IPv6 addresses with a ``scope_id`` (link-local addresses)

0 commit comments

Comments
 (0)