From a53195d274750e0d636d9651dc834dd109594a2b Mon Sep 17 00:00:00 2001 From: 0z5a Date: Thu, 5 Nov 2026 10:36:53 +0100 Subject: [PATCH] gh-161384: Fix IPv6Network.exploded with a scope ID --- Lib/ipaddress.py | 8 +++++--- Lib/test/test_ipaddress.py | 2 ++ .../2026-11-05-09-50-00.gh-issue-161384.abc123.rst | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-05-09-50-00.gh-issue-161384.abc123.rst diff --git a/Lib/ipaddress.py b/Lib/ipaddress.py index b11999a0f44bccc..5772f2539e3b113 100644 --- a/Lib/ipaddress.py +++ b/Lib/ipaddress.py @@ -1920,7 +1920,7 @@ def _explode_shorthand_ip_string(self): """ if isinstance(self, IPv6Network): - ip_str = str(self.network_address) + ip_str = self._string_from_ip_int(int(self.network_address)) elif isinstance(self, IPv6Interface): ip_str = str(self.ip) else: @@ -1929,10 +1929,12 @@ def _explode_shorthand_ip_string(self): 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)]) + scope_id = (self.network_address._scope_id + if isinstance(self, IPv6Network) else self._scope_id) + if scope_id: + exploded = '%s%%%s' % (exploded, scope_id) 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) return exploded diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 9110124d9423cff..78971a2d5853ef4 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -2754,6 +2754,8 @@ 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) # issue 77 self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1', addr2.exploded) diff --git a/Misc/NEWS.d/next/Library/2026-11-05-09-50-00.gh-issue-161384.abc123.rst b/Misc/NEWS.d/next/Library/2026-11-05-09-50-00.gh-issue-161384.abc123.rst new file mode 100644 index 000000000000000..7535060e89d9243 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-05-09-50-00.gh-issue-161384.abc123.rst @@ -0,0 +1 @@ +Fix :meth:`ipaddress.IPv6Network.exploded` for networks with a scope ID.