Skip to content

Commit 5ace717

Browse files
gh-128734: Fix ResourceWarning in urllib tests (GH-128735)
1 parent cb72feb commit 5ace717

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

Diff for: Lib/test/test_urllib.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,9 @@ def test_read_bogus(self):
419419
Content-Type: text/html; charset=iso-8859-1
420420
''', mock_close=True)
421421
try:
422-
self.assertRaises(OSError, urllib.request.urlopen, "http://python.org/")
422+
with self.assertRaises(urllib.error.HTTPError) as cm:
423+
urllib.request.urlopen("http://python.org/")
424+
cm.exception.close()
423425
finally:
424426
self.unfakehttp()
425427

@@ -434,8 +436,9 @@ def test_invalid_redirect(self):
434436
''', mock_close=True)
435437
try:
436438
msg = "Redirection to url 'file:"
437-
with self.assertRaisesRegex(urllib.error.HTTPError, msg):
439+
with self.assertRaisesRegex(urllib.error.HTTPError, msg) as cm:
438440
urllib.request.urlopen("http://python.org/")
441+
cm.exception.close()
439442
finally:
440443
self.unfakehttp()
441444

@@ -448,8 +451,9 @@ def test_redirect_limit_independent(self):
448451
Connection: close
449452
''', mock_close=True)
450453
try:
451-
self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen,
452-
"http://something")
454+
with self.assertRaises(urllib.error.HTTPError) as cm:
455+
urllib.request.urlopen("http://something")
456+
cm.exception.close()
453457
finally:
454458
self.unfakehttp()
455459

@@ -529,10 +533,11 @@ def setUp(self):
529533
"QOjdAAAAAXNSR0IArs4c6QAAAA9JREFUCNdj%0AYGBg%2BP//PwAGAQL%2BCm8 "
530534
"vHgAAAABJRU5ErkJggg%3D%3D%0A%20")
531535

532-
self.text_url_resp = urllib.request.urlopen(self.text_url)
533-
self.text_url_base64_resp = urllib.request.urlopen(
534-
self.text_url_base64)
535-
self.image_url_resp = urllib.request.urlopen(self.image_url)
536+
self.text_url_resp = self.enterContext(
537+
urllib.request.urlopen(self.text_url))
538+
self.text_url_base64_resp = self.enterContext(
539+
urllib.request.urlopen(self.text_url_base64))
540+
self.image_url_resp = self.enterContext(urllib.request.urlopen(self.image_url))
536541

537542
def test_interface(self):
538543
# Make sure object returned by urlopen() has the specified methods
@@ -548,8 +553,10 @@ def test_info(self):
548553
[('text/plain', ''), ('charset', 'ISO-8859-1')])
549554
self.assertEqual(self.image_url_resp.info()['content-length'],
550555
str(len(self.image)))
551-
self.assertEqual(urllib.request.urlopen("data:,").info().get_params(),
556+
r = urllib.request.urlopen("data:,")
557+
self.assertEqual(r.info().get_params(),
552558
[('text/plain', ''), ('charset', 'US-ASCII')])
559+
r.close()
553560

554561
def test_geturl(self):
555562
self.assertEqual(self.text_url_resp.geturl(), self.text_url)

Diff for: Lib/test/test_urllib2.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ def connect_ftp(self, user, passwd, host, port, dirs,
782782
headers = r.info()
783783
self.assertEqual(headers.get("Content-type"), mimetype)
784784
self.assertEqual(int(headers["Content-length"]), len(data))
785+
r.close()
785786

786787
@support.requires_resource("network")
787788
def test_ftp_error(self):
@@ -1247,10 +1248,11 @@ def test_redirect(self):
12471248
try:
12481249
method(req, MockFile(), code, "Blah",
12491250
MockHeaders({"location": to_url}))
1250-
except urllib.error.HTTPError:
1251+
except urllib.error.HTTPError as err:
12511252
# 307 and 308 in response to POST require user OK
12521253
self.assertIn(code, (307, 308))
12531254
self.assertIsNotNone(data)
1255+
err.close()
12541256
self.assertEqual(o.req.get_full_url(), to_url)
12551257
try:
12561258
self.assertEqual(o.req.get_method(), "GET")
@@ -1286,9 +1288,10 @@ def redirect(h, req, url=to_url):
12861288
while 1:
12871289
redirect(h, req, "http://example.com/")
12881290
count = count + 1
1289-
except urllib.error.HTTPError:
1291+
except urllib.error.HTTPError as err:
12901292
# don't stop until max_repeats, because cookies may introduce state
12911293
self.assertEqual(count, urllib.request.HTTPRedirectHandler.max_repeats)
1294+
err.close()
12921295

12931296
# detect endless non-repeating chain of redirects
12941297
req = Request(from_url, origin_req_host="example.com")
@@ -1298,9 +1301,10 @@ def redirect(h, req, url=to_url):
12981301
while 1:
12991302
redirect(h, req, "http://example.com/%d" % count)
13001303
count = count + 1
1301-
except urllib.error.HTTPError:
1304+
except urllib.error.HTTPError as err:
13021305
self.assertEqual(count,
13031306
urllib.request.HTTPRedirectHandler.max_redirections)
1307+
err.close()
13041308

13051309
def test_invalid_redirect(self):
13061310
from_url = "http://example.com/a.html"
@@ -1314,9 +1318,11 @@ def test_invalid_redirect(self):
13141318

13151319
for scheme in invalid_schemes:
13161320
invalid_url = scheme + '://' + schemeless_url
1317-
self.assertRaises(urllib.error.HTTPError, h.http_error_302,
1321+
with self.assertRaises(urllib.error.HTTPError) as cm:
1322+
h.http_error_302(
13181323
req, MockFile(), 302, "Security Loophole",
13191324
MockHeaders({"location": invalid_url}))
1325+
cm.exception.close()
13201326

13211327
for scheme in valid_schemes:
13221328
valid_url = scheme + '://' + schemeless_url
@@ -1912,11 +1918,13 @@ def test_HTTPError_interface(self):
19121918
self.assertEqual(str(err), expected_errmsg)
19131919
expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg)
19141920
self.assertEqual(repr(err), expected_errmsg)
1921+
err.close()
19151922

19161923
def test_gh_98778(self):
19171924
x = urllib.error.HTTPError("url", 405, "METHOD NOT ALLOWED", None, None)
19181925
self.assertEqual(getattr(x, "__notes__", ()), ())
19191926
self.assertIsInstance(x.fp.read(), bytes)
1927+
x.close()
19201928

19211929
def test_parse_proxy(self):
19221930
parse_proxy_test_cases = [

Diff for: Lib/test/test_urllib2_localnet.py

+9-7
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,

Diff for: Lib/test/test_urllib_response.py

+2
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()

Diff for: Lib/test/test_urllibnet.py

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def test_getcode(self):
106106
with urllib.request.urlopen(URL):
107107
pass
108108
self.assertEqual(e.exception.code, 404)
109+
e.exception.close()
109110

110111
@support.requires_resource('walltime')
111112
def test_bad_address(self):

0 commit comments

Comments
 (0)