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

Skip to content

Commit 6116e1b

Browse files
[3.13] gh-128734: Explicitly close sockets in urllib tests (GH-128735) (GH-128748)
(cherry picked from commit 5ace717)
1 parent 436064a commit 6116e1b

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

Lib/test/test_urllib.py

Lines changed: 17 additions & 10 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

@@ -505,8 +508,9 @@ def test_redirect_limit_independent(self):
505508
Connection: close
506509
''', mock_close=True)
507510
try:
508-
self.assertRaises(urllib.error.HTTPError, urlopen,
509-
"http://something")
511+
with self.assertRaises(urllib.error.HTTPError) as cm:
512+
urllib.request.urlopen("http://something")
513+
cm.exception.close()
510514
finally:
511515
self.unfakehttp()
512516

@@ -626,10 +630,11 @@ def setUp(self):
626630
"QOjdAAAAAXNSR0IArs4c6QAAAA9JREFUCNdj%0AYGBg%2BP//PwAGAQL%2BCm8 "
627631
"vHgAAAABJRU5ErkJggg%3D%3D%0A%20")
628632

629-
self.text_url_resp = urllib.request.urlopen(self.text_url)
630-
self.text_url_base64_resp = urllib.request.urlopen(
631-
self.text_url_base64)
632-
self.image_url_resp = urllib.request.urlopen(self.image_url)
633+
self.text_url_resp = self.enterContext(
634+
urllib.request.urlopen(self.text_url))
635+
self.text_url_base64_resp = self.enterContext(
636+
urllib.request.urlopen(self.text_url_base64))
637+
self.image_url_resp = self.enterContext(urllib.request.urlopen(self.image_url))
633638

634639
def test_interface(self):
635640
# Make sure object returned by urlopen() has the specified methods
@@ -645,8 +650,10 @@ def test_info(self):
645650
[('text/plain', ''), ('charset', 'ISO-8859-1')])
646651
self.assertEqual(self.image_url_resp.info()['content-length'],
647652
str(len(self.image)))
648-
self.assertEqual(urllib.request.urlopen("data:,").info().get_params(),
653+
r = urllib.request.urlopen("data:,")
654+
self.assertEqual(r.info().get_params(),
649655
[('text/plain', ''), ('charset', 'US-ASCII')])
656+
r.close()
650657

651658
def test_geturl(self):
652659
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
@@ -789,6 +789,7 @@ def connect_ftp(self, user, passwd, host, port, dirs,
789789
headers = r.info()
790790
self.assertEqual(headers.get("Content-type"), mimetype)
791791
self.assertEqual(int(headers["Content-length"]), len(data))
792+
r.close()
792793

793794
def test_file(self):
794795
import email.utils
@@ -1229,10 +1230,11 @@ def test_redirect(self):
12291230
try:
12301231
method(req, MockFile(), code, "Blah",
12311232
MockHeaders({"location": to_url}))
1232-
except urllib.error.HTTPError:
1233+
except urllib.error.HTTPError as err:
12331234
# 307 and 308 in response to POST require user OK
12341235
self.assertIn(code, (307, 308))
12351236
self.assertIsNotNone(data)
1237+
err.close()
12361238
self.assertEqual(o.req.get_full_url(), to_url)
12371239
try:
12381240
self.assertEqual(o.req.get_method(), "GET")
@@ -1268,9 +1270,10 @@ def redirect(h, req, url=to_url):
12681270
while 1:
12691271
redirect(h, req, "http://example.com/")
12701272
count = count + 1
1271-
except urllib.error.HTTPError:
1273+
except urllib.error.HTTPError as err:
12721274
# don't stop until max_repeats, because cookies may introduce state
12731275
self.assertEqual(count, urllib.request.HTTPRedirectHandler.max_repeats)
1276+
err.close()
12741277

12751278
# detect endless non-repeating chain of redirects
12761279
req = Request(from_url, origin_req_host="example.com")
@@ -1280,9 +1283,10 @@ def redirect(h, req, url=to_url):
12801283
while 1:
12811284
redirect(h, req, "http://example.com/%d" % count)
12821285
count = count + 1
1283-
except urllib.error.HTTPError:
1286+
except urllib.error.HTTPError as err:
12841287
self.assertEqual(count,
12851288
urllib.request.HTTPRedirectHandler.max_redirections)
1289+
err.close()
12861290

12871291
def test_invalid_redirect(self):
12881292
from_url = "http://example.com/a.html"
@@ -1296,9 +1300,11 @@ def test_invalid_redirect(self):
12961300

12971301
for scheme in invalid_schemes:
12981302
invalid_url = scheme + '://' + schemeless_url
1299-
self.assertRaises(urllib.error.HTTPError, h.http_error_302,
1303+
with self.assertRaises(urllib.error.HTTPError) as cm:
1304+
h.http_error_302(
13001305
req, MockFile(), 302, "Security Loophole",
13011306
MockHeaders({"location": invalid_url}))
1307+
cm.exception.close()
13021308

13031309
for scheme in valid_schemes:
13041310
valid_url = scheme + '://' + schemeless_url
@@ -1894,11 +1900,13 @@ def test_HTTPError_interface(self):
18941900
self.assertEqual(str(err), expected_errmsg)
18951901
expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg)
18961902
self.assertEqual(repr(err), expected_errmsg)
1903+
err.close()
18971904

18981905
def test_gh_98778(self):
18991906
x = urllib.error.HTTPError("url", 405, "METHOD NOT ALLOWED", None, None)
19001907
self.assertEqual(getattr(x, "__notes__", ()), ())
19011908
self.assertIsInstance(x.fp.read(), bytes)
1909+
x.close()
19021910

19031911
def test_parse_proxy(self):
19041912
parse_proxy_test_cases = [

Lib/test/test_urllib2_localnet.py

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

321323

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

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

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

Lib/test/test_urllib_response.py

Lines changed: 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