Skip to content

Commit 435551d

Browse files
committed
gh-155358: Use named attributes with urllib.parse module
urlparse(), replace: * parts[0] => parts.scheme * parts[1] => parts.netloc * parts[2] => parts.path * parts[3] => parts.query * parts[4] => parts.fragment urlsplit(), replace: * parts[0] => parts.scheme * parts[1] => parts.netloc * parts[2] => parts.path * parts[3] => parts.query * parts[4] => parts.fragment
1 parent 115400b commit 435551d

7 files changed

Lines changed: 18 additions & 14 deletions

File tree

Lib/http/cookiejar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def request_host(request):
626626
627627
"""
628628
url = request.get_full_url()
629-
host = urllib.parse.urlparse(url)[1]
629+
host = urllib.parse.urlparse(url).netloc
630630
if host == "":
631631
host = request.get_header("Host", "")
632632

Lib/http/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,8 @@ def send_head(self):
796796
if not parts.path.endswith(('/', '%2f', '%2F')):
797797
# redirect browser - doing basically what apache does
798798
self.send_response(HTTPStatus.MOVED_PERMANENTLY)
799-
new_parts = (parts[0], parts[1], parts[2] + '/',
800-
parts[3], parts[4])
799+
new_parts = (parts.scheme, parts.netloc, parts.path + '/',
800+
parts.query, parts.fragment)
801801
new_url = urllib.parse.urlunsplit(new_parts)
802802
self.send_header("Location", new_url)
803803
self.send_header("Content-Length", "0")

Lib/test/ssl_servers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def translate_path(self, path):
6161
6262
"""
6363
# abandon query parameters
64-
path = urllib.parse.urlparse(path)[2]
64+
path = urllib.parse.urlparse(path).path
6565
path = os.path.normpath(urllib.parse.unquote(path))
6666
words = path.split('/')
6767
words = filter(None, words)

Lib/test/support/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ def open_urlresource(url, *args, **kw):
868868

869869
check = kw.pop('check', None)
870870

871-
filename = urllib.parse.urlparse(url)[2].split('/')[-1] # '/': it's URL!
871+
filename = urllib.parse.urlparse(url).path.split('/')[-1] # '/': it's URL!
872872

873873
fn = os.path.join(TEST_DATA_DIR, filename)
874874

Lib/urllib/request.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def request_host(request):
274274
275275
"""
276276
url = request.full_url
277-
host = urlparse(url)[1]
277+
host = urlparse(url).netloc
278278
if host == "":
279279
host = request.get_header("Host", "")
280280

@@ -832,11 +832,11 @@ def reduce_uri(self, uri, default_port=True):
832832
"""Accept authority or URI and extract only the authority and path."""
833833
# note HTTP URLs do not have a userinfo component
834834
parts = urlsplit(uri)
835-
if parts[1]:
835+
if parts.netloc:
836836
# URI
837-
scheme = parts[0]
838-
authority = parts[1]
839-
path = parts[2] or '/'
837+
scheme = parts.scheme
838+
authority = parts.netloc
839+
path = parts.path or '/'
840840
else:
841841
# host or host:port
842842
scheme = None
@@ -1209,7 +1209,7 @@ class HTTPDigestAuthHandler(BaseHandler, AbstractDigestAuthHandler):
12091209
handler_order = 490 # before Basic auth
12101210

12111211
def http_error_401(self, req, fp, code, msg, headers):
1212-
host = urlparse(req.full_url)[1]
1212+
host = urlparse(req.full_url).netloc
12131213
retry = self.http_error_auth_reqed('www-authenticate',
12141214
host, req, headers)
12151215
self.reset_retry_count()
@@ -1668,7 +1668,9 @@ def url2pathname(url, *, require_scheme=False, resolve_host=False):
16681668
"""
16691669
if not require_scheme:
16701670
url = 'file:' + url
1671-
scheme, authority, url = urlsplit(url)[:3] # Discard query and fragment.
1671+
parts = urlsplit(url)
1672+
# Discard query and fragment.
1673+
scheme, authority, url = parts.scheme, parts.netloc, parts.path
16721674
if scheme != 'file':
16731675
raise URLError("URL is missing a 'file:' scheme")
16741676
if os.name == 'nt':

Lib/urllib/robotparser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def set_url(self, url):
6262

6363
if isinstance(url, urllib.request.Request):
6464
url = url.full_url
65-
self.host, self.path = urllib.parse.urlsplit(url)[1:3]
65+
parts = urllib.parse.urlsplit(url)
66+
self.host = parts.netloc
67+
self.path = parts.path
6668

6769
def read(self):
6870
"""Reads the robots.txt URL and feeds it to the parser."""

Lib/xmlrpc/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ def __init__(self, uri, transport=None, encoding=None, verbose=False,
14021402
if p.scheme not in ("http", "https"):
14031403
raise OSError("unsupported XML-RPC protocol")
14041404
self.__host = p.netloc
1405-
self.__handler = urllib.parse.urlunsplit(["", "", *p[2:]])
1405+
self.__handler = urllib.parse.urlunsplit(["", "", p.path, p.query, p.fragment])
14061406
if not self.__handler:
14071407
self.__handler = "/RPC2"
14081408

0 commit comments

Comments
 (0)