https://github.com/python/cpython/commit/2601dae1830bccbb64b44b777b083aac6b72107a commit: 2601dae1830bccbb64b44b777b083aac6b72107a branch: 3.12 author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com> committer: picnixz <10796600+picn...@users.noreply.github.com> date: 2025-03-18T09:34:45Z summary:
[3.12] gh-130132: properly free resources in `urrlib.urlopen` examples (GH-130280) (#131395) gh-130132: properly free resources in `urrlib.urlopen` examples (GH-130280) (cherry picked from commit 77d2fd441320ca365c34e21c475d3878f3d5d833) Co-authored-by: Kanishk Pachauri <itskanishkp...@gmail.com> Co-authored-by: sobolevn <m...@sobolevn.me> Co-authored-by: Bénédikt Tran <10796600+picn...@users.noreply.github.com> files: M Doc/library/urllib.request.rst diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst index 0c5aa0458c6ebe..5d1d8a245907c6 100644 --- a/Doc/library/urllib.request.rst +++ b/Doc/library/urllib.request.rst @@ -1244,7 +1244,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() <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtm @@ -1289,7 +1292,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) - urllib.request.urlopen('http://www.example.com/login.html') + 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 @@ -1307,7 +1311,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 f: + print(f.read().decode('utf-8')) Adding HTTP headers: @@ -1318,7 +1323,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: . . .)') - r = urllib.request.urlopen(req) + with urllib.request.urlopen(req) as f: + print(f.read().decode('utf-8')) + :class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to every :class:`Request`. To change this:: @@ -1326,7 +1333,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 f: + print(f.read().decode('utf-8')) Also, remember that a few standard headers (:mailheader:`Content-Length`, :mailheader:`Content-Type` and :mailheader:`Host`) _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com