Skip to content

Commit b379d47

Browse files
gatewaytofredommiss-islington
authored andcommitted
gh-161384: Fix IPv6Network.exploded with a scope ID (GH-162715)
(cherry picked from commit 5639e5d) Co-authored-by: gateway <gateway12@laindex.net> gh-161384: Fix IPv6Network.exploded with scope ID and avoid string round-trip
1 parent d3863ef commit b379d47

3 files changed

Lines changed: 21 additions & 14 deletions

File tree

‎Lib/ipaddress.py‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,21 +1866,22 @@ def _explode_shorthand_ip_string(self):
18661866
18671867
"""
18681868
if isinstance(self, IPv6Network):
1869-
ip_str = str(self.network_address)
1870-
elif isinstance(self, IPv6Interface):
1871-
ip_str = str(self.ip)
1869+
ip_int = self.network_address._ip
1870+
scope_id = self.network_address._scope_id
18721871
else:
1873-
ip_str = self._string_from_ip_int(self._ip)
1874-
1875-
ip_int = self._ip_int_from_string(ip_str)
1876-
hex_str = '%032x' % ip_int
1877-
exploded = ':'.join([hex_str[x:x+4] for x in range(0, 32, 4)])
1878-
if isinstance(self, _BaseNetwork):
1879-
return '%s/%d' % (exploded, self._prefixlen)
1880-
if self._scope_id:
1881-
exploded = '%s%%%s' % (exploded, self._scope_id)
1882-
if isinstance(self, IPv6Interface):
1883-
return '%s/%d' % (exploded, self._prefixlen)
1872+
ip_int = self._ip
1873+
scope_id = self._scope_id
1874+
1875+
exploded = (
1876+
f'{ip_int >> 112:04x}:{(ip_int >> 96) & 0xFFFF:04x}:'
1877+
f'{(ip_int >> 80) & 0xFFFF:04x}:{(ip_int >> 64) & 0xFFFF:04x}:'
1878+
f'{(ip_int >> 48) & 0xFFFF:04x}:{(ip_int >> 32) & 0xFFFF:04x}:'
1879+
f'{(ip_int >> 16) & 0xFFFF:04x}:{ip_int & 0xFFFF:04x}'
1880+
)
1881+
if scope_id:
1882+
exploded = f'{exploded}%{scope_id}'
1883+
if isinstance(self, (_BaseNetwork, IPv6Interface)):
1884+
return f'{exploded}/{self._prefixlen}'
18841885
return exploded
18851886

18861887
def _reverse_pointer(self):

‎Lib/test/test_ipaddress.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,6 +2702,10 @@ def testExplodeShortHandIpStr(self):
27022702
ipaddress.IPv6Address('fe80::1%eth0').exploded)
27032703
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0001%1/64',
27042704
ipaddress.IPv6Interface('fe80::1%1/64').exploded)
2705+
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0000%eth0/64',
2706+
ipaddress.IPv6Network('fe80::%eth0/64').exploded)
2707+
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0000%1/64',
2708+
ipaddress.IPv6Network('fe80::%1/64').exploded)
27052709
# issue 77
27062710
self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1',
27072711
addr2.exploded)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :attr:`ipaddress.IPv6Network.exploded` for networks with a scope ID and
2+
avoid redundant string round-tripping in ``_BaseV6._explode_shorthand_ip_string()``.

0 commit comments

Comments
 (0)