Skip to content

Commit 9a3f7ec

Browse files
author
opavliuk
committed
bpo-34788 Refactor address split and validation
1 parent a841e9a commit 9a3f7ec

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

Lib/ipaddress.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,18 +1795,16 @@ def _split_scope_id(ip_str):
17951795
ip_str: A string, the IPv6 address.
17961796
17971797
Returns:
1798-
[addr, scope_id] list.
1798+
[addr, scope_id] tuple.
17991799
"""
18001800
if '%' not in ip_str:
18011801
return ip_str, None
18021802

1803-
split_addr = ip_str.split('%')
1804-
if len(split_addr) > 2 or split_addr[-1] == '':
1803+
addr, sep, scope_id = ip_str.partition('%')
1804+
if not sep:
1805+
scope_id = None
1806+
elif not scope_id:
18051807
raise AddressValueError('Invalid IPv6 address: "%r"' % ip_str)
1806-
try:
1807-
addr, scope_id = split_addr
1808-
except ValueError:
1809-
return split_addr, None
18101808
return addr, scope_id
18111809

18121810
@property
@@ -1855,7 +1853,6 @@ def __init__(self, address):
18551853
self._ip = int.from_bytes(address, 'big')
18561854
return
18571855

1858-
18591856
# Assume input argument to be string or any object representation
18601857
# which converts into a formatted IP string.
18611858
addr_str = str(address)

0 commit comments

Comments
 (0)