From acafffdd5ab6f4491c5b87997d371c289e28b028 Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Wed, 19 Feb 2025 00:17:18 +0530 Subject: [PATCH 1/8] gh-130132: add an explicit close() call to urlopen examples without with --- Doc/library/urllib.request.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index b3efde3f189566..5b949cc9e0acd0 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1249,7 +1249,10 @@ It is also possible to achieve the same result without using the >>> import urllib.request >>> f = urllib.request.urlopen('http://www.python.org/') - >>> print(f.read(100).decode('utf-8')) + >>> try: + ... print(f.read(100).decode('utf-8')) + ... finally: + ... f.close() Date: Wed, 19 Feb 2025 01:28:41 +0530 Subject: [PATCH 2/8] gh-130132: add an explicit close() in Use of Basic HTTP Authentication --- Doc/library/urllib.request.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index 5b949cc9e0acd0..758d53d43e5345 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1297,7 +1297,10 @@ Use of Basic HTTP Authentication:: opener = urllib.request.build_opener(auth_handler) # ...and install it globally so it can be used with urlopen. urllib.request.install_opener(opener) - urllib.request.urlopen('http://www.example.com/login.html') + try: + r = urllib.request.urlopen('http://www.example.com/login.html') + finally: + r.close() :func:`build_opener` provides many handlers by default, including a :class:`ProxyHandler`. By default, :class:`ProxyHandler` uses the environment From 379b075bca3771b9e4ffa7d6220a43e6cddb380f Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Wed, 19 Feb 2025 01:29:51 +0530 Subject: [PATCH 3/8] gh-130132: add an explicit close() in Adding HTTP headers --- Doc/library/urllib.request.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index 758d53d43e5345..d5641ea59eb9ff 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1329,7 +1329,10 @@ Use the *headers* argument to the :class:`Request` constructor, or:: req.add_header('Referer', 'http://www.python.org/') # Customize the default User-Agent header value: req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)') - r = urllib.request.urlopen(req) + try: + r = urllib.request.urlopen(req) + finally: + r.close() :class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to every :class:`Request`. To change this:: From c9fbdb097d273055b89f94d25813031119fe1341 Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Thu, 20 Feb 2025 22:43:17 +0530 Subject: [PATCH 4/8] fix: use context manager in urllib.request examples --- Doc/library/urllib.request.rst | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index d5641ea59eb9ff..e78f967918e0c8 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1297,10 +1297,8 @@ Use of Basic HTTP Authentication:: opener = urllib.request.build_opener(auth_handler) # ...and install it globally so it can be used with urlopen. urllib.request.install_opener(opener) - try: - r = urllib.request.urlopen('http://www.example.com/login.html') - finally: - r.close() + with urllib.request.urlopen('http://www.example.com/login.html') as r: + print(r.read().decode('utf-8')) :func:`build_opener` provides many handlers by default, including a :class:`ProxyHandler`. By default, :class:`ProxyHandler` uses the environment @@ -1329,10 +1327,9 @@ Use the *headers* argument to the :class:`Request` constructor, or:: req.add_header('Referer', 'http://www.python.org/') # Customize the default User-Agent header value: req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)') - try: - r = urllib.request.urlopen(req) - finally: - r.close() + with urllib.request.urlopen(req) as r: + print(r.read().decode('utf-8')) + :class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to every :class:`Request`. To change this:: From 05d0a4e8122469f8b9de61c499c849be4161c774 Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Wed, 26 Feb 2025 01:34:13 +0530 Subject: [PATCH 5/8] fix: spaces in the docs, Co-authored-by: sobolevn --- Doc/library/urllib.request.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index e78f967918e0c8..7449b2c85ab934 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1298,7 +1298,7 @@ Use of Basic HTTP Authentication:: # ...and install it globally so it can be used with urlopen. urllib.request.install_opener(opener) with urllib.request.urlopen('http://www.example.com/login.html') as r: - print(r.read().decode('utf-8')) + print(r.read().decode('utf-8')) :func:`build_opener` provides many handlers by default, including a :class:`ProxyHandler`. By default, :class:`ProxyHandler` uses the environment From 9f1344a6872f1c6e9453f7b8f4b9005c282418e2 Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Wed, 26 Feb 2025 01:34:31 +0530 Subject: [PATCH 6/8] fix: spaces in the docs. Co-authored-by: sobolevn --- Doc/library/urllib.request.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index 7449b2c85ab934..9d2c1fec7f39f8 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1328,7 +1328,7 @@ Use the *headers* argument to the :class:`Request` constructor, or:: # Customize the default User-Agent header value: req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)') with urllib.request.urlopen(req) as r: - print(r.read().decode('utf-8')) + print(r.read().decode('utf-8')) :class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to From 97e6b183a97e036bc8d1040782ae1dea5f287c7e Mon Sep 17 00:00:00 2001 From: Kanishk Pachauri Date: Sun, 9 Mar 2025 23:32:29 +0530 Subject: [PATCH 7/8] docs: Add context manager in examples of `urllib.request` --- Doc/library/urllib.request.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index e78f967918e0c8..003b615b53e3ee 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1316,7 +1316,8 @@ programmatically supplied proxy URLs, and adds proxy authorization support with opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler) # This time, rather than install the OpenerDirector, we use it directly: - opener.open('http://www.example.com/login.html') + with opener.open('http://www.example.com/login.html') as response: + print(response.read().decode('utf-8')) Adding HTTP headers: @@ -1337,7 +1338,8 @@ every :class:`Request`. To change this:: import urllib.request opener = urllib.request.build_opener() opener.addheaders = [('User-agent', 'Mozilla/5.0')] - opener.open('http://www.example.com/') + with opener.open('http://www.example.com/') as response: + print(response.read().decode('utf-8')) Also, remember that a few standard headers (:mailheader:`Content-Length`, :mailheader:`Content-Type` and :mailheader:`Host`) From 38255379a1e0e9e9e035f7e8c06c1aa814669a45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 18 Mar 2025 10:21:30 +0100 Subject: [PATCH 8/8] use `f` as the `as` variable Other examples use `f` as the response variable. --- Doc/library/urllib.request.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index 60e6b92238e503..969e7daea7105b 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1297,8 +1297,8 @@ Use of Basic HTTP Authentication:: opener = urllib.request.build_opener(auth_handler) # ...and install it globally so it can be used with urlopen. urllib.request.install_opener(opener) - with urllib.request.urlopen('http://www.example.com/login.html') as r: - print(r.read().decode('utf-8')) + with urllib.request.urlopen('http://www.example.com/login.html') as f: + print(f.read().decode('utf-8')) :func:`build_opener` provides many handlers by default, including a :class:`ProxyHandler`. By default, :class:`ProxyHandler` uses the environment @@ -1316,8 +1316,8 @@ programmatically supplied proxy URLs, and adds proxy authorization support with opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler) # This time, rather than install the OpenerDirector, we use it directly: - with opener.open('http://www.example.com/login.html') as response: - print(response.read().decode('utf-8')) + with opener.open('http://www.example.com/login.html') as f: + print(f.read().decode('utf-8')) Adding HTTP headers: @@ -1328,8 +1328,8 @@ Use the *headers* argument to the :class:`Request` constructor, or:: req.add_header('Referer', 'http://www.python.org/') # Customize the default User-Agent header value: req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)') - with urllib.request.urlopen(req) as r: - print(r.read().decode('utf-8')) + with urllib.request.urlopen(req) as f: + print(f.read().decode('utf-8')) :class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to @@ -1338,8 +1338,8 @@ every :class:`Request`. To change this:: import urllib.request opener = urllib.request.build_opener() opener.addheaders = [('User-agent', 'Mozilla/5.0')] - with opener.open('http://www.example.com/') as response: - print(response.read().decode('utf-8')) + with opener.open('http://www.example.com/') as f: + print(f.read().decode('utf-8')) Also, remember that a few standard headers (:mailheader:`Content-Length`, :mailheader:`Content-Type` and :mailheader:`Host`)