[3.12] gh-128734: Explicitly close sockets in urllib tests (GH-128735) (GH-128750)

(cherry picked from commit 5ace71713b)
This commit is contained in:
Serhiy Storchaka 2025-01-12 13:30:07 +02:00 committed by GitHub
parent a600439e42
commit c20c551ce3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 37 additions and 19 deletions

View file

@ -476,7 +476,9 @@ Connection: close
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()
@ -491,8 +493,9 @@ Content-Type: text/html; charset=iso-8859-1
''', 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()
@ -635,10 +638,11 @@ class urlopen_DataTests(unittest.TestCase):
"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
@ -654,8 +658,10 @@ class urlopen_DataTests(unittest.TestCase):
[('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)