https://github.com/python/cpython/commit/1855c6c40b02a5c8e75de2832dbb6cbd24351f1b
commit: 1855c6c40b02a5c8e75de2832dbb6cbd24351f1b
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-08-31T18:03:33Z
summary:

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

files:
M Lib/http/cookiejar.py
M Lib/test/ssl_servers.py
M Lib/test/support/__init__.py
M Lib/urllib/request.py
M Lib/urllib/robotparser.py

diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
index 144db91a0f03173..5c5dc78b064885d 100644
--- a/Lib/http/cookiejar.py
+++ b/Lib/http/cookiejar.py
@@ -627,7 +627,7 @@ def request_host(request):
 
     """
     url = request.get_full_url()
-    host = urllib.parse.urlparse(url)[1]
+    host = urllib.parse.urlparse(url).netloc
     if host == "":
         host = request.get_header("Host", "")
 
diff --git a/Lib/test/ssl_servers.py b/Lib/test/ssl_servers.py
index 15b071e04dda1f3..e3416a822f65253 100644
--- a/Lib/test/ssl_servers.py
+++ b/Lib/test/ssl_servers.py
@@ -61,7 +61,7 @@ def translate_path(self, path):
 
         """
         # abandon query parameters
-        path = urllib.parse.urlparse(path)[2]
+        path = urllib.parse.urlparse(path).path
         path = os.path.normpath(urllib.parse.unquote(path))
         words = path.split('/')
         words = filter(None, words)
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index be71575a6ea06a9..1c28af08988d9f0 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -868,7 +868,7 @@ def open_urlresource(url, *args, **kw):
 
     check = kw.pop('check', None)
 
-    filename = urllib.parse.urlparse(url)[2].split('/')[-1] # '/': it's URL!
+    filename = urllib.parse.urlparse(url).path.split('/')[-1] # '/': it's URL!
 
     fn = os.path.join(TEST_DATA_DIR, filename)
 
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index 9fa92659a255ed4..59ed4a7140d59c6 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -274,7 +274,7 @@ def request_host(request):
 
     """
     url = request.full_url
-    host = urlparse(url)[1]
+    host = urlparse(url).netloc
     if host == "":
         host = request.get_header("Host", "")
 
@@ -833,11 +833,11 @@ def reduce_uri(self, uri, default_port=True):
         """Accept authority or URI and extract only the authority and path."""
         # note HTTP URLs do not have a userinfo component
         parts = urlsplit(uri)
-        if parts[1]:
+        if parts.netloc:
             # URI
-            scheme = parts[0]
-            authority = parts[1]
-            path = parts[2] or '/'
+            scheme = parts.scheme
+            authority = parts.netloc
+            path = parts.path or '/'
         else:
             # host or host:port
             scheme = None
@@ -1222,7 +1222,7 @@ class HTTPDigestAuthHandler(BaseHandler, 
AbstractDigestAuthHandler):
     handler_order = 490  # before Basic auth
 
     def http_error_401(self, req, fp, code, msg, headers):
-        host = urlparse(req.full_url)[1]
+        host = urlparse(req.full_url).netloc
         retry = self.http_error_auth_reqed('www-authenticate',
                                            host, req, headers)
         self.reset_retry_count()
diff --git a/Lib/urllib/robotparser.py b/Lib/urllib/robotparser.py
index 8d0311d96f5e0b4..985333c71004389 100644
--- a/Lib/urllib/robotparser.py
+++ b/Lib/urllib/robotparser.py
@@ -62,7 +62,9 @@ def set_url(self, url):
 
         if isinstance(url, urllib.request.Request):
             url = url.full_url
-        self.host, self.path = urllib.parse.urlsplit(url)[1:3]
+        parts = urllib.parse.urlsplit(url)
+        self.host = parts.netloc
+        self.path = parts.path
 
     def read(self):
         """Reads the robots.txt URL and feeds it to the parser."""

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to