Skip to content

Commit

Permalink
[3.12] gh-128734: Explicitly close sockets in urllib tests (GH-128735) (
Browse files Browse the repository at this point in the history
GH-128750)

(cherry picked from commit 5ace717)
  • Loading branch information
serhiy-storchaka authored Jan 12, 2025
1 parent a600439 commit c20c551
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
22 changes: 14 additions & 8 deletions Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,9 @@ def test_read_bogus(self):
Content-Type: text/html; charset=iso-8859-1
''', mock_close=True)
try:
self.assertRaises(OSError, urlopen, "http://python.org/")
with self.assertRaises(urllib.error.HTTPError) as cm:
urllib.request.urlopen("http://python.org/")
cm.exception.close()
finally:
self.unfakehttp()

Expand All @@ -491,8 +493,9 @@ def test_invalid_redirect(self):
''', mock_close=True)
try:
msg = "Redirection to url 'file:"
with self.assertRaisesRegex(urllib.error.HTTPError, msg):
urlopen("http://python.org/")
with self.assertRaisesRegex(urllib.error.HTTPError, msg) as cm:
urllib.request.urlopen("http://python.org/")
cm.exception.close()
finally:
self.unfakehttp()

Expand Down Expand Up @@ -635,10 +638,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
Expand All @@ -654,8 +658,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)
Expand Down
16 changes: 12 additions & 4 deletions Lib/test/test_urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,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()

def test_file(self):
import email.utils
Expand Down Expand Up @@ -1227,10 +1228,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")
Expand Down Expand Up @@ -1266,9 +1268,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")
Expand All @@ -1278,9 +1281,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"
Expand All @@ -1294,9 +1298,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
Expand Down Expand Up @@ -1883,11 +1889,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 = [
Expand Down
16 changes: 9 additions & 7 deletions Lib/test/test_urllib2_localnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,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)
Expand Down Expand Up @@ -363,15 +365,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,
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_urllib_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down

0 comments on commit c20c551

Please sign in to comment.