Skip to content

Commit a17538a

Browse files
author
opavliuk
committed
bpo-34788 Convert scope_id to property
1 parent 9a3f7ec commit a17538a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

Lib/ipaddress.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,12 +1790,11 @@ def _split_scope_id(ip_str):
17901790
"""Helper function to parse IPv6 string address with scope id.
17911791
17921792
See RFC 4007 for details.
1793-
17941793
Arg:
17951794
ip_str: A string, the IPv6 address.
17961795
17971796
Returns:
1798-
[addr, scope_id] tuple.
1797+
(addr, scope_id) tuple.
17991798
"""
18001799
if '%' not in ip_str:
18011800
return ip_str, None
@@ -1820,7 +1819,7 @@ class IPv6Address(_BaseV6, _BaseAddress):
18201819

18211820
"""Represent and manipulate single IPv6 Addresses."""
18221821

1823-
__slots__ = ('_ip', 'scope_id', '__weakref__')
1822+
__slots__ = ('_ip', '_scope_id', '__weakref__')
18241823

18251824
def __init__(self, address):
18261825
"""Instantiate a new IPv6 address object.
@@ -1839,7 +1838,7 @@ def __init__(self, address):
18391838
AddressValueError: If address isn't a valid IPv6 address.
18401839
18411840
"""
1842-
self.scope_id = None
1841+
self._scope_id = None
18431842

18441843
# Efficient constructor from integer.
18451844
if isinstance(address, int):
@@ -1858,14 +1857,23 @@ def __init__(self, address):
18581857
addr_str = str(address)
18591858
if '/' in addr_str:
18601859
raise AddressValueError("Unexpected '/' in %r" % address)
1861-
addr_str, self.scope_id = self._split_scope_id(addr_str)
1860+
addr_str, self._scope_id = self._split_scope_id(addr_str)
18621861

18631862
self._ip = self._ip_int_from_string(addr_str)
18641863

18651864
def __str__(self):
1866-
ip_str = self._string_from_ip_int(self._ip)
1865+
ip_str = super().__str__()
18671866
return ip_str if self.scope_id is None else ip_str + '%' + self.scope_id
18681867

1868+
@property
1869+
def scope_id(self):
1870+
"""Identifier of a particular zone of the address's scope.
1871+
1872+
Returns:
1873+
A string, identifying the zone of the address if specified, else None.
1874+
"""
1875+
return self._scope_id
1876+
18691877
@property
18701878
def packed(self):
18711879
"""The binary representation of this address."""

0 commit comments

Comments
 (0)