Skip to content

Commit 63891cc

Browse files
committed
bugfix/issue-129538-scope-id
Signed-off-by: RafaelJohn9 <[email protected]>
1 parent d89a5f6 commit 63891cc

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

Lib/ipaddress.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2199,6 +2199,7 @@ def __init__(self, address):
21992199
self.network = IPv6Network((addr, mask), strict=False)
22002200
self.netmask = self.network.netmask
22012201
self._prefixlen = self.network._prefixlen
2202+
self._scope_id = addr.scope_id if isinstance(addr, IPv6Address) else None
22022203

22032204
@functools.cached_property
22042205
def hostmask(self):
@@ -2239,7 +2240,8 @@ def __hash__(self):
22392240

22402241
@property
22412242
def ip(self):
2242-
return IPv6Address(self._ip)
2243+
addr_str = self._string_from_ip_int(self._ip) + (f'%{self._scope_id}' if self._scope_id else '')
2244+
return IPv6Address(addr_str)
22432245

22442246
@property
22452247
def with_prefixlen(self):

Lib/test/test_ipaddress.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,12 @@ def test_copy(self):
562562
self.assertEqual(addr, copy.copy(addr))
563563
self.assertEqual(addr, copy.deepcopy(addr))
564564

565+
# issue: 129538
566+
def test_ipv6_interface_scope_id(self):
567+
addr = ipaddress.IPv6Address("fe80::%10")
568+
addr_with_prefix = ipaddress.IPv6Interface((addr, 64))
569+
self.assertEqual(addr_with_prefix.ip, ipaddress.IPv6Address("fe80::%10"))
570+
565571

566572
class NetmaskTestMixin_v4(CommonTestMixin_v4):
567573
"""Input validation on interfaces and networks is very similar"""

0 commit comments

Comments
 (0)