Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -1954,21 +1954,22 @@ def _explode_shorthand_ip_string(self):

"""
if isinstance(self, IPv6Network):
ip_str = str(self.network_address)
elif isinstance(self, IPv6Interface):
ip_str = str(self.ip)
ip_int = self.network_address._ip
scope_id = self.network_address._scope_id
else:
ip_str = self._string_from_ip_int(self._ip)

ip_int = self._ip_int_from_string(ip_str)
hex_str = '%032x' % ip_int
exploded = ':'.join([hex_str[x:x+4] for x in range(0, 32, 4)])
if isinstance(self, _BaseNetwork):
return '%s/%d' % (exploded, self._prefixlen)
if self._scope_id:
exploded = '%s%%%s' % (exploded, self._scope_id)
if isinstance(self, IPv6Interface):
return '%s/%d' % (exploded, self._prefixlen)
ip_int = self._ip
scope_id = self._scope_id

exploded = (
f'{ip_int >> 112:04x}:{(ip_int >> 96) & 0xFFFF:04x}:'
f'{(ip_int >> 80) & 0xFFFF:04x}:{(ip_int >> 64) & 0xFFFF:04x}:'
f'{(ip_int >> 48) & 0xFFFF:04x}:{(ip_int >> 32) & 0xFFFF:04x}:'
f'{(ip_int >> 16) & 0xFFFF:04x}:{ip_int & 0xFFFF:04x}'
)
if scope_id:
exploded = f'{exploded}%{scope_id}'
if isinstance(self, (_BaseNetwork, IPv6Interface)):
return f'{exploded}/{self._prefixlen}'
return exploded

def _reverse_pointer(self):
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,10 @@ def testExplodeShortHandIpStr(self):
ipaddress.IPv6Address('fe80::1%eth0').exploded)
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0001%1/64',
ipaddress.IPv6Interface('fe80::1%1/64').exploded)
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0000%eth0/64',
ipaddress.IPv6Network('fe80::%eth0/64').exploded)
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0000%1/64',
ipaddress.IPv6Network('fe80::%1/64').exploded)
# issue 77
self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1',
addr2.exploded)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :attr:`ipaddress.IPv6Network.exploded` for networks with a scope ID and
avoid redundant string round-tripping in ``_BaseV6._explode_shorthand_ip_string()``.
Loading