https://github.com/python/cpython/commit/5ace71713b03cb37d829f50c849a8bb8a518738d
commit: 5ace71713b03cb37d829f50c849a8bb8a518738d
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-01-12T12:53:17+02:00
summary:
gh-128734: Fix ResourceWarning in urllib tests (GH-128735)
files:
M Lib/test/test_urllib.py
M Lib/test/test_urllib2.py
M Lib/test/test_urllib2_localnet.py
M Lib/test/test_urllib_response.py
M Lib/test/test_urllibnet.py
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 042d3b35b77022..4842428d6fd103 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -419,7 +419,9 @@ def test_read_bogus(self):
Content-Type: text/html; charset=iso-8859-1
''', mock_close=True)
try:
- self.assertRaises(OSError, urllib.request.urlopen,
"http://python.org/")
+ with self.assertRaises(urllib.error.HTTPError) as cm:
+ urllib.request.urlopen("http://python.org/")
+ cm.exception.close()
finally:
self.unfakehttp()
@@ -434,8 +436,9 @@ def test_invalid_redirect(self):
''', mock_close=True)
try:
msg = "Redirection to url 'file:"
- with self.assertRaisesRegex(urllib.error.HTTPError, msg):
+ with self.assertRaisesRegex(urllib.error.HTTPError, msg) as cm:
urllib.request.urlopen("http://python.org/")
+ cm.exception.close()
finally:
self.unfakehttp()
@@ -448,8 +451,9 @@ def test_redirect_limit_independent(self):
Connection: close
''', mock_close=True)
try:
- self.assertRaises(urllib.error.HTTPError,
urllib.request.urlopen,
- "http://something")
+ with self.assertRaises(urllib.error.HTTPError) as cm:
+ urllib.request.urlopen("http://something")
+ cm.exception.close()
finally:
self.unfakehttp()
@@ -529,10 +533,11 @@ def setUp(self):
"QOjdAAAAAXNSR0IArs4c6QAAAA9JREFUCNdj%0AYGBg%2BP//PwAGAQL%2BCm8 "
"vHgAAAABJRU5ErkJggg%3D%3D%0A%20")
- self.text_url_resp = urllib.request.urlopen(self.text_url)
- self.text_url_base64_resp = urllib.request.urlopen(
- self.text_url_base64)
- self.image_url_resp = urllib.request.urlopen(self.image_url)
+ self.text_url_resp = self.enterContext(
+ urllib.request.urlopen(self.text_url))
+ self.text_url_base64_resp = self.enterContext(
+ urllib.request.urlopen(self.text_url_base64))
+ self.image_url_resp =
self.enterContext(urllib.request.urlopen(self.image_url))
def test_interface(self):
# Make sure object returned by urlopen() has the specified methods
@@ -548,8 +553,10 @@ def test_info(self):
[('text/plain', ''), ('charset', 'ISO-8859-1')])
self.assertEqual(self.image_url_resp.info()['content-length'],
str(len(self.image)))
- self.assertEqual(urllib.request.urlopen("data:,").info().get_params(),
+ r = urllib.request.urlopen("data:,")
+ self.assertEqual(r.info().get_params(),
[('text/plain', ''), ('charset', 'US-ASCII')])
+ r.close()
def test_geturl(self):
self.assertEqual(self.text_url_resp.geturl(), self.text_url)
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
index 085b24c25b2daa..44e6af8c6b6868 100644
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -782,6 +782,7 @@ def connect_ftp(self, user, passwd, host, port, dirs,
headers = r.info()
self.assertEqual(headers.get("Content-type"), mimetype)
self.assertEqual(int(headers["Content-length"]), len(data))
+ r.close()
@support.requires_resource("network")
def test_ftp_error(self):
@@ -1247,10 +1248,11 @@ def test_redirect(self):
try:
method(req, MockFile(), code, "Blah",
MockHeaders({"location": to_url}))
- except urllib.error.HTTPError:
+ except urllib.error.HTTPError as err:
# 307 and 308 in response to POST require user OK
self.assertIn(code, (307, 308))
self.assertIsNotNone(data)
+ err.close()
self.assertEqual(o.req.get_full_url(), to_url)
try:
self.assertEqual(o.req.get_method(), "GET")
@@ -1286,9 +1288,10 @@ def redirect(h, req, url=to_url):
while 1:
redirect(h, req, "http://example.com/")
count = count + 1
- except urllib.error.HTTPError:
+ except urllib.error.HTTPError as err:
# don't stop until max_repeats, because cookies may introduce state
self.assertEqual(count,
urllib.request.HTTPRedirectHandler.max_repeats)
+ err.close()
# detect endless non-repeating chain of redirects
req = Request(from_url, origin_req_host="example.com")
@@ -1298,9 +1301,10 @@ def redirect(h, req, url=to_url):
while 1:
redirect(h, req, "http://example.com/%d" % count)
count = count + 1
- except urllib.error.HTTPError:
+ except urllib.error.HTTPError as err:
self.assertEqual(count,
urllib.request.HTTPRedirectHandler.max_redirections)
+ err.close()
def test_invalid_redirect(self):
from_url = "http://example.com/a.html"
@@ -1314,9 +1318,11 @@ def test_invalid_redirect(self):
for scheme in invalid_schemes:
invalid_url = scheme + '://' + schemeless_url
- self.assertRaises(urllib.error.HTTPError, h.http_error_302,
+ with self.assertRaises(urllib.error.HTTPError) as cm:
+ h.http_error_302(
req, MockFile(), 302, "Security Loophole",
MockHeaders({"location": invalid_url}))
+ cm.exception.close()
for scheme in valid_schemes:
valid_url = scheme + '://' + schemeless_url
@@ -1912,11 +1918,13 @@ def test_HTTPError_interface(self):
self.assertEqual(str(err), expected_errmsg)
expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg)
self.assertEqual(repr(err), expected_errmsg)
+ err.close()
def test_gh_98778(self):
x = urllib.error.HTTPError("url", 405, "METHOD NOT ALLOWED", None,
None)
self.assertEqual(getattr(x, "__notes__", ()), ())
self.assertIsInstance(x.fp.read(), bytes)
+ x.close()
def test_parse_proxy(self):
parse_proxy_test_cases = [
diff --git a/Lib/test/test_urllib2_localnet.py
b/Lib/test/test_urllib2_localnet.py
index 50c491a3cfd3d0..9cb15d61c2ad4d 100644
--- a/Lib/test/test_urllib2_localnet.py
+++ b/Lib/test/test_urllib2_localnet.py
@@ -316,7 +316,9 @@ def test_basic_auth_httperror(self):
ah = urllib.request.HTTPBasicAuthHandler()
ah.add_password(self.REALM, self.server_url, self.USER,
self.INCORRECT_PASSWD)
urllib.request.install_opener(urllib.request.build_opener(ah))
- self.assertRaises(urllib.error.HTTPError, urllib.request.urlopen,
self.server_url)
+ with self.assertRaises(urllib.error.HTTPError) as cm:
+ urllib.request.urlopen(self.server_url)
+ cm.exception.close()
@hashlib_helper.requires_hashdigest("md5", openssl=True)
@@ -362,15 +364,15 @@ def test_proxy_with_bad_password_raises_httperror(self):
self.proxy_digest_handler.add_password(self.REALM, self.URL,
self.USER, self.PASSWD+"bad")
self.digest_auth_handler.set_qop("auth")
- self.assertRaises(urllib.error.HTTPError,
- self.opener.open,
- self.URL)
+ with self.assertRaises(urllib.error.HTTPError) as cm:
+ self.opener.open(self.URL)
+ cm.exception.close()
def test_proxy_with_no_password_raises_httperror(self):
self.digest_auth_handler.set_qop("auth")
- self.assertRaises(urllib.error.HTTPError,
- self.opener.open,
- self.URL)
+ with self.assertRaises(urllib.error.HTTPError) as cm:
+ self.opener.open(self.URL)
+ cm.exception.close()
def test_proxy_qop_auth_works(self):
self.proxy_digest_handler.add_password(self.REALM, self.URL,
diff --git a/Lib/test/test_urllib_response.py b/Lib/test/test_urllib_response.py
index b76763f4ed824f..d949fa38bfc42f 100644
--- a/Lib/test/test_urllib_response.py
+++ b/Lib/test/test_urllib_response.py
@@ -48,6 +48,7 @@ def test_addinfo(self):
info = urllib.response.addinfo(self.fp, self.test_headers)
self.assertEqual(info.info(), self.test_headers)
self.assertEqual(info.headers, self.test_headers)
+ info.close()
def test_addinfourl(self):
url = "http://www.python.org"
@@ -60,6 +61,7 @@ def test_addinfourl(self):
self.assertEqual(infourl.headers, self.test_headers)
self.assertEqual(infourl.url, url)
self.assertEqual(infourl.status, code)
+ infourl.close()
def tearDown(self):
self.sock.close()
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py
index f824dddf711761..ce4e60e3a8011d 100644
--- a/Lib/test/test_urllibnet.py
+++ b/Lib/test/test_urllibnet.py
@@ -106,6 +106,7 @@ def test_getcode(self):
with urllib.request.urlopen(URL):
pass
self.assertEqual(e.exception.code, 404)
+ e.exception.close()
@support.requires_resource('walltime')
def test_bad_address(self):
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]