8000 [3.12] gh-130132: properly free resources in `urrlib.urlopen` example… · python/cpython@2601dae · GitHub
[go: up one dir, main page]

Skip to content

Commit 2601dae

Browse files
miss-islingtonMr-Sunglassessobolevnpicnixz
authored
[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 77d2fd4) Co-authored-by: Kanishk Pachauri <itskanishkp.py@gmail.com> Co-authored-by: sobolevn <mail@sobolevn.me> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent c23a2d3 commit 2601dae

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

Doc/library/urllib.request.rst

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,10 @@ It is also possible to achieve the same result without using the
12441244

12451245
>>> import urllib.request
12461246
>>> f = urllib.request.urlopen('http://www.python.org/')
1247-
>>> print(f.read(100).decode('utf-8'))
1247+
>>> try:
1248+
... print(f.read(100).decode('utf-8'))
1249+
... finally:
1250+
... f.close()
12481251
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
12491252
"http://www.w3.org/TR/xhtml1/DTD/xhtm
12501253

@@ -1289,7 +1292,8 @@ Use of Basic HTTP Authentication::
12891292
opener = urllib.request.build_opener(auth_handler)
12901293
# ...and install it globally so it can be used with urlopen.
12911294
urllib.request.install_opener(opener)
1292-
urllib.request.urlopen('http://www.example.com/login.html')
1295+
with urllib.request.urlopen('http://www.example.com/login.html') as f:
1296+
print(f.read().decode('utf-8'))
12931297

12941298
:func:`build_opener` provides many handlers by default, including a
12951299
:class:`ProxyHandler`. By default, :class:`ProxyHandler` uses the environment
@@ -1307,7 +1311,8 @@ programmatically supplied proxy URLs, and adds proxy authorization support with
13071311

13081312
opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
13091313
# This time, rather than install the OpenerDirector, we use it directly:
1310-
opener.open('http://www.example.com/login.html')
1314+
with opener.open('http://www.example.com/login.html') as f:
1315+
print(f.read().decode('utf-8'))
13111316

13121317
Adding HTTP headers:
13131318

@@ -1318,15 +1323,18 @@ Use the *headers* argument to the :class:`Request` constructor, or::
13181323
req.add_header('Referer', 'http://www.python.org/')
13191324
# Customize the default User-Agent header value:
13201325
req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)')
1321-
r = urllib.request.urlopen(req)
1326+
with urllib.request.urlopen(req) as f:
1327+
print(f.read().decode('utf-8'))
1328+
13221329

13231330
:class:`OpenerDirector` automatically adds a :mailheader:`User-Agent` header to
13241331
every :class:`Request`. To change this::
13251332

13261333
import urllib.request
13271334
opener = urllib.request.build_opener()
13281335
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
1329-
opener.open('http://www.example.com/')
1336+
with opener.open('http://www.example.com/') as f:
1337+
print(f.read().decode('utf-8'))
13301338

13311339
Also, remember that a few standard headers (:mailheader:`Content-Length`,
13321340
:mailheader:`Content-Type` and :mailheader:`Host`)

0 commit comments

Comments
 (0)
0