Skip to content

Commit 1855c6c

Browse files
authored
gh-155358: Use named attributes with urllib.parse module (#155364)
urlparse(), replace: * parts[0] => parts.scheme * parts[1] => parts.netloc * parts[2] => parts.path urlsplit(), replace: * parts[0] => parts.scheme * parts[1] => parts.netloc * parts[2] => parts.path
1 parent 63f6626 commit 1855c6c

5 files changed

Lines changed: 12 additions & 10 deletions

File tree

Lib/http/cookiejar.py

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

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: 6 additions & 6 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

@@ -833,11 +833,11 @@ def reduce_uri(self, uri, default_port=True):
833833
"""Accept authority or URI and extract only the authority and path."""
834834
# note HTTP URLs do not have a userinfo component
835835
parts = urlsplit(uri)
836-
if parts[1]:
836+
if parts.netloc:
837837
# URI
838-
scheme = parts[0]
839-
authority = parts[1]
840-
path = parts[2] or '/'
838+
scheme = parts.scheme
839+
authority = parts.netloc
840+
path = parts.path or '/'
841841
else:
842842
# host or host:port
843843
scheme = None
@@ -1222,7 +1222,7 @@ class HTTPDigestAuthHandler(BaseHandler, AbstractDigestAuthHandler):
12221222
handler_order = 490 # before Basic auth
12231223

12241224
def http_error_401(self, req, fp, code, msg, headers):
1225-
host = urlparse(req.full_url)[1]
1225+
host = urlparse(req.full_url).netloc
12261226
retry = self.http_error_auth_reqed('www-authenticate',
12271227
host, req, headers)
12281228
self.reset_retry_count()

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."""

0 commit comments

Comments
 (0)