Skip to content

Commit 0a1a050

Browse files
[3.14] gh-161384: Fix IPv6Network.exploded with a scope ID (GH-162715) (#163092)
gh-161384: Fix IPv6Network.exploded with a scope ID (GH-162715) (cherry picked from commit 5639e5d) gh-161384: Fix IPv6Network.exploded with scope ID and avoid string round-trip Co-authored-by: gateway <gateway12@laindex.net>
1 parent 2658f4c commit 0a1a050

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
@@ -1865,21 +1865,22 @@ def _explode_shorthand_ip_string(self):
18651865
18661866
"""
18671867
if isinstance(self, IPv6Network):
1868-
ip_str = str(self.network_address)
1869-
elif isinstance(self, IPv6Interface):
1870-
ip_str = str(self.ip)
1868+
ip_int = self.network_address._ip
1869+
scope_id = self.network_address._scope_id
18711870
else:
1872-
ip_str = self._string_from_ip_int(self._ip)
1873-
1874-
ip_int = self._ip_int_from_string(ip_str)
1875-
hex_str = '%032x' % ip_int
1876-
exploded = ':'.join([hex_str[x:x+4] for x in range(0, 32, 4)])
1877-
if isinstance(self, _BaseNetwork):
1878-
return '%s/%d' % (exploded, self._prefixlen)
1879-
if self._scope_id:
1880-
exploded = '%s%%%s' % (exploded, self._scope_id)
1881-
if isinstance(self, IPv6Interface):
1882-
return '%s/%d' % (exploded, self._prefixlen)
1871+
ip_int = self._ip
1872+
scope_id = self._scope_id
1873+
1874+
exploded = (
1875+
f'{ip_int >> 112:04x}:{(ip_int >> 96) & 0xFFFF:04x}:'
1876+
f'{(ip_int >> 80) & 0xFFFF:04x}:{(ip_int >> 64) & 0xFFFF:04x}:'
1877+
f'{(ip_int >> 48) & 0xFFFF:04x}:{(ip_int >> 32) & 0xFFFF:04x}:'
1878+
f'{(ip_int >> 16) & 0xFFFF:04x}:{ip_int & 0xFFFF:04x}'
1879+
)
1880+
if scope_id:
1881+
exploded = f'{exploded}%{scope_id}'
1882+
if isinstance(self, (_BaseNetwork, IPv6Interface)):
1883+
return f'{exploded}/{self._prefixlen}'
18831884
return exploded
18841885

18851886
def _reverse_pointer(self):

‎Lib/test/test_ipaddress.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,6 +2679,10 @@ def testExplodeShortHandIpStr(self):
26792679
ipaddress.IPv6Address('fe80::1%eth0').exploded)
26802680
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0001%1/64',
26812681
ipaddress.IPv6Interface('fe80::1%1/64').exploded)
2682+
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0000%eth0/64',
2683+
ipaddress.IPv6Network('fe80::%eth0/64').exploded)
2684+
self.assertEqual('fe80:0000:0000:0000:0000:0000:0000:0000%1/64',
2685+
ipaddress.IPv6Network('fe80::%1/64').exploded)
26822686
# issue 77
26832687
self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1',
26842688
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)