Skip to content

Commit

Permalink
update to not require attribute access
Browse files Browse the repository at this point in the history
  • Loading branch information
bcail committed Nov 26, 2024
1 parent f75c6a5 commit 9cb076e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Lib/test/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1422,14 +1422,14 @@ def test_invalid_bracketed_hosts(self):
for case in cases:
with self.subTest(case=case):
with self.assertRaises(ValueError):
urllib.parse.urlsplit(case).hostname
urllib.parse.urlsplit(case)
with self.assertRaises(ValueError):
urllib.parse.urlparse(case).hostname
urllib.parse.urlparse(case)
bytes_case = case.encode('utf8')
with self.assertRaises(ValueError):
urllib.parse.urlsplit(bytes_case).hostname
urllib.parse.urlsplit(bytes_case)
with self.assertRaises(ValueError):
urllib.parse.urlparse(bytes_case).hostname
urllib.parse.urlparse(bytes_case)

def test_splitting_bracketed_hosts(self):
p1 = urllib.parse.urlsplit('scheme://user@[v6a.ip]/path?query')
Expand Down
28 changes: 14 additions & 14 deletions Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,10 @@ def _userinfo(self):
def _hostinfo(self):
netloc = self.netloc
_, _, hostinfo = netloc.rpartition('@')
bracket_prefix, have_open_br, bracketed = hostinfo.partition('[')
_, have_open_br, bracketed = hostinfo.partition('[')
if have_open_br:
if bracket_prefix:
raise ValueError('Invalid IPv6 URL')
hostname, _, port = bracketed.partition(']')
_check_bracketed_host(hostname)
bracket_suffix, _, port = port.partition(':')
if bracket_suffix:
raise ValueError('Invalid IPv6 URL')
_, _, port = port.partition(':')
else:
hostname, _, port = hostinfo.partition(':')
if not port:
Expand All @@ -241,15 +236,10 @@ def _userinfo(self):
def _hostinfo(self):
netloc = self.netloc
_, _, hostinfo = netloc.rpartition(b'@')
bracket_prefix, have_open_br, bracketed = hostinfo.partition(b'[')
_, have_open_br, bracketed = hostinfo.partition(b'[')
if have_open_br:
if bracket_prefix:
raise ValueError('Invalid IPv6 URL')
hostname, _, port = bracketed.partition(b']')
_check_bracketed_host(hostname.decode(_implicit_encoding, _implicit_errors))
bracket_suffix, _, port = port.partition(b':')
if bracket_suffix:
raise ValueError('Invalid IPv6 URL')
_, _, port = port.partition(b':')
else:
hostname, _, port = hostinfo.partition(b':')
if not port:
Expand Down Expand Up @@ -514,6 +504,16 @@ def _urlsplit(url, scheme=None, allow_fragments=True):
if (('[' in netloc and ']' not in netloc) or
(']' in netloc and '[' not in netloc)):
raise ValueError("Invalid IPv6 URL")
if '[' in netloc and ']' in netloc:
_, _, hostinfo = netloc.rpartition('@')
bracket_prefix, have_open_br, bracketed = hostinfo.partition('[')
if bracket_prefix:
raise ValueError('Invalid IPv6 URL')
hostname, _, port = bracketed.partition(']')
_check_bracketed_host(hostname)
bracket_suffix, _, port = port.partition(':')
if bracket_suffix:
raise ValueError('Invalid IPv6 URL')
if allow_fragments and '#' in url:
url, fragment = url.split('#', 1)
if '?' in url:
Expand Down

0 comments on commit 9cb076e

Please sign in to comment.