8000 [3.12] gh-128734: Explicitly close sockets in urllib tests (GH-128735… · python/cpython@c20c551 · GitHub
[go: up one dir, main page]

Skip to content

Commit c20c551

Browse files
[3.12] gh-128734: Explicitly close sockets in urllib tests (GH-128735) (GH-128750)
(cherry picked from commit 5ace717)
1 parent a600439 commit c20c551

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

Lib/test/test_urllib.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,9 @@ def test_read_bogus(self):
476476
Content-Type: text/html; charset=iso-8859-1
477477
''', mock_close=True)
478478
try:
479-
self.assertRaises(OSError, urlopen, "http://python.org/")
479+
with self.assertRaises(urllib.error.HTTPError) as cm:
480+
urllib.request.urlopen("http://python.org/")
481+
cm.exception.close()
480482
finally:
481483
self.unfakehttp()
482484

@@ -491,8 +493,9 @@ def test_invalid_redirect(self):
491493
''', mock_close=True)
492494
try:
493495
msg = "Redirection to url 'file:"
494-
with self.assertRaisesRegex(urllib.error.HTTPError, msg):
495-
urlopen("http://python.org/")
496+
with self.assertRaisesRegex(urllib.error.HTTPError, msg) as cm:
497+
urllib.request.urlopen("http://python.org/")
498+
cm.exception.close()
496499
finally:
497500
self.unfakehttp()
498501

@@ -635,10 +638,11 @@ def setUp(self):
635638
"QOjdAAAAAXNSR0IArs4c6QAAAA9JREFUCNdj%0AYGBg%2BP//PwAGAQL%2BCm8 "
636639
"vHgAAAABJRU5ErkJggg%3D%3D%0A%20")
637640

638-
self.text_url_resp = urllib.request.urlopen(self.text_url)
639-
self.text_url_base64_resp = urllib.request.urlopen(
640-
self.text_url_base64)
641-
self.image_url_resp = urllib.request.urlopen(self.image_url)
641+
self.text_url_resp = self.enterContext(
642+
urllib.request.urlopen(self.text_url))
643+
self.text_url_base64_resp = self.enterContext(
644+
urllib.request.urlopen(self.text_url_base64))
645+
self.image_url_resp = self.enterContext(urllib.request.urlopen(self.image_url))
642646

643647
def test_interface(self):
644648
# Make sure object returned by urlopen() has the specified methods
@@ -654,8 +658,10 @@ def test_info(self):
654658
[('text/plain', ''), ('charset', 'ISO-8859-1')])
655659
self.assertEqual(self.image_url_resp.info()['content-length'],
656660
str(len(self.image)))
657-
self.assertEqual(urllib.request.urlopen("data:,").info().get_params(),
661+
r = urllib.request.urlopen("data:,")
662+
self.assertEqual(r.info().get_params(),
658663
[('text/plain', ''), ('charset', 'US-ASCII')])
664+
r.close()
659665

660666
def test_geturl(self):
661667
self.assertEqual(self.text_url_resp.geturl(), self.text_url)

Lib/test/test_urllib2.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ def connect_ftp(self, user, passwd, host, port, dirs,
788788
headers = r.info()
789789
self.assertEqual(headers.get("Content-type"), mimetype)
790790
self.assertEqual(int(headers["Content-length"]), len(data))
791+
r.close()
791792

792793
def test_file(self):
793794
import email.utils
@@ -1227,10 +1228,11 @@ def test_redirect(self):
12271228
try:
12281229
method(req, MockFile(), code, "Blah",
12291230
MockHeaders({"location": to_url}))
1230-
except urllib.error.HTTPError:
1231+
except urllib.error.HTTPError as err:
12311232
# 307 and 308 in response to POST require user OK
12321233
self.assertIn(code, (307, 308))
12331234
self.assertIsNotNone(data)
1235+
err.close()
12341236
self.assertEqual(o.req.get_full_url(), to_url)
12351237
try:
12361238
self.assertEqual(o.req.get_method(), "GET")
@@ -1266,9 +1268,10 @@ def redirect(h, req, url=to_url):
12661268
while 1:
12671269
redirect(h, req, "http://example.com/")
12681270
count = count + 1
1269-
except urllib.error.HTTPError:
1271+
except urllib.error.HTTPError as err:
12701272
# don't stop until max_repeats, because cookies may introduce state
12711273
self.assertEqual(count, urllib.request.HTTPRedirectHandler.max_repeats)
1274+
err.close()
12721275

12731276
# detect endless non-repeating chain of redirects
12741277
req = Request(from_url, origin_req_host="example.com")
@@ -1278,9 +1281,10 @@ def redirect(h, req, url=to_url):
12781281
while 1:
12791282
redirect(h, req, "http://example.com/%d" % count)
12801283
count = count + 1
1281-
except urllib.error.HTTPError:
1284+
except urllib.error.HTTPError as err:
12821285
self.assertEqual(count,
12831286
urllib.request.HTTPRedirectHandler.max_redirections)
1287+
err.close()
12841288

12851289
def test_invalid_redirect(self):
12861290
from_url = "http://example.com/a.html"
@@ -1294,9 +1298,11 @@ def test_invalid_redirect(self):
12941298

12951299
for scheme in invalid_schemes:
12961300
invalid_url = scheme + '://' + schemeless_url
1297-
self.assertRaises(urllib.error.HTTPError, h.http_error_302,
1301+
with self.assertRaises(urllib.error.HTTPError) as cm:
1302+
h.http_error_302(
12981303
req, MockFile(), 302, "Security Loophole",
12991304
MockHeaders({"location": invalid_url}))
1305+
cm.exception.close()
13001306

13011307
for scheme in valid_schemes:
13021308
valid_url = scheme + '://' + schemeless_url
@@ -1883,11 +1889,13 @@ def test_HTTPError_interface(self):
18831889
self.assertEqual(str(err), expected_errmsg)
18841890
expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg)
18851891
self.assertEqual(repr(err), expected_errmsg)
1892+
err.close()
18861893

18871894
def test_gh_98778(self):
18881895
x = urllib.error.HTTPError("url", 405, "METHOD NOT ALLOWED", None, None)
18891896
self.assertEqual(getattr(x, "__notes__", ()), ())
18901897
self.assertIsInstance(x.fp.read(), bytes)
1898+
x.close()
18911899

18921900
def test_parse_proxy(self):
18931901
parse_proxy_test_cases = [

Lib/test/test_urllib2_localnet.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,9 @@ def test_basic_auth_httperror(self):
317317
ah = urllib.request.HTTPBasicAuthHandler()
318318
ah.add_password(self.REALM, self.server_url, self.USER, self.INCORRECT_PASSWD)
319319
urllib.request.install_opener(urllib.request.build_opener(ah))
320-
self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen, self.server_url)
320+
with self.assertRaises(urllib.error.HTTPError) as cm:
321+
urllib.request.urlopen(self.server_url)
322+
cm.exception.close()
321323

322324

323325
@hashlib_helper.requires_hashdigest("md5", openssl=True)
@@ -363,15 +365,15 @@ def test_proxy_with_bad_password_raises_httperror(self):
363365
self.proxy_digest_handler.add_password(self.REALM, self.URL,
364366
self.USER, self.PASSWD+"bad")
365367
self.digest_auth_handler.set_qop("auth")
366-
self.assertRaises(urllib.error.HTTPError,
367-
self.opener.open,
368-
self.URL)
368+
with self.assertRaises(urllib.error.HTTPError) as cm:
369+
self.opener.open(self.URL)
370+
cm.exception.close()
369371

370372
def test_proxy_with_no_password_raises_httperror(self):
371373
self.digest_auth_handler.set_qop("auth")
372-
self.assertRaises(urllib.error.HTTPError,
373-
self.opener.open,
374-
self.URL)
374+
with self.assertRaises(urllib.error.HTTPError) as cm:
375+
self.opener.open(self.URL)
376+
cm.exception.close()
375377

376378
def test_proxy_qop_auth_works(self):
377379
self.proxy_digest_handler.add_password(self.REALM, self.URL,

Lib/test/test_urllib_response.py

Lines c 97BF hanged: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def test_addinfo(self):
4848
info = urllib.response.addinfo(self.fp, self.test_headers)
4949
self.assertEqual(info.info(), self.test_headers)
5050
self.assertEqual(info.headers, self.test_headers)
51+
info.close()
5152

5253
def test_addinfourl(self):
5354
url = "http://www.python.org"
@@ -60,6 +61,7 @@ def test_addinfourl(self):
6061
self.assertEqual(infourl.headers, self.test_headers)
6162
self.assertEqual(infourl.url, url)
6263
self.assertEqual(infourl.status, code)
64+
infourl.close()
6365

6466
def tearDown(self):
6567
self.sock.close()

0 commit comments

Comments
 (0)
0