gh-128734: Fix ResourceWarning in urllib tests (GH-128735) · python/cpython@5ace717 · GitHub
Skip to content

Commit 5ace717

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

5 files changed

Lines changed: 40 additions & 20 deletions

File tree

Lib/test/test_urllib.py

Lines changed: 16 additions & 9 deletions

Lib/test/test_urllib2.py

Lines changed: 12 additions & 4 deletions
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 = [

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

Lib/test/test_urllibnet.py

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)