summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_urllib.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2025-01-12 10:53:17 (GMT)
committerGitHub <noreply@github.com>2025-01-12 10:53:17 (GMT)
commit5ace71713b03cb37d829f50c849a8bb8a518738d (patch)
tree1e06b1e4d0b9931735145087fe928b617db89120 /Lib/test/test_urllib.py
parentcb72feb8a30edb448bc8dc31330ed7420279d7c2 (diff)
downloadcpython-5ace71713b03cb37d829f50c849a8bb8a518738d.zip
cpython-5ace71713b03cb37d829f50c849a8bb8a518738d.tar.gz
cpython-5ace71713b03cb37d829f50c849a8bb8a518738d.tar.bz2
gh-128734: Fix ResourceWarning in urllib tests (GH-128735)
Diffstat (limited to 'Lib/test/test_urllib.py')
-rw-r--r--Lib/test/test_urllib.py25
1 files changed, 16 insertions, 9 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 042d3b3..4842428 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -419,7 +419,9 @@ Connection: close
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 @@ Content-Type: text/html; charset=iso-8859-1
''', 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 @@ Location: file://guidocomputer.athome.com:/python/license
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 @@ 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
@@ -548,8 +553,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)