https://github.com/python/cpython/commit/c44dca07b01300a5d30e9908fc974f170a1a472d
commit: c44dca07b01300a5d30e9908fc974f170a1a472d
branch: main
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-08-10T09:52:29+03:00
summary:
gh-62917: Add tests for urllib.request.urlcleanup() (GH-155446)
Test that it removes temporary files created by urlretrieve() and resets
the cached opener.
files:
M Lib/test/test_urllib.py
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 1e5f79998e7cab..ab59727a8fd182 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -807,6 +807,61 @@ def
test_short_content_raises_ContentTooShortError_without_reporthook(self):
self.unfakehttp()
+class urlcleanup_Tests(unittest.TestCase, FakeHTTPMixin):
+ """Test urllib.request.urlcleanup()"""
+
+ def setUp(self):
+ self.addCleanup(urllib.request.urlcleanup)
+
+ def urlretrieve(self):
+ self.fakehttp(b'HTTP/1.1 200 OK\r\n\r\ndata')
+ try:
+ filename, headers = urllib.request.urlretrieve(
+ support.TEST_HTTP_URL)
+ finally:
+ self.unfakehttp()
+ self.addCleanup(os_helper.unlink, filename)
+ return filename
+
+ def fake_urlopen(self, data):
+ self.fakehttp(b'HTTP/1.1 200 OK\r\n\r\n' + data)
+ try:
+ with urllib.request.urlopen(support.TEST_HTTP_URL) as fp:
+ return fp.read()
+ finally:
+ self.unfakehttp()
+
+ def test_temporary_files(self):
+ filename = self.urlretrieve()
+ self.assertTrue(os.path.exists(filename))
+
+ urllib.request.urlcleanup()
+ self.assertFalse(os.path.exists(filename))
+
+ # A file created after the cleanup is not deleted.
+ os_helper.create_empty_file(filename)
+ urllib.request.urlcleanup()
+ self.assertTrue(os.path.exists(filename))
+
+ def test_opener(self):
+ # The implicitly created opener supports http.
+ self.assertEqual(self.fake_urlopen(b'first'), b'first')
+
+ # An installed opener replaces it and supports only its handlers.
+ opener = urllib.request.OpenerDirector()
+ opener.add_handler(urllib.request.DataHandler())
+ opener.add_handler(urllib.request.UnknownHandler())
+ urllib.request.install_opener(opener)
+ with urllib.request.urlopen('data:,hello') as fp:
+ self.assertEqual(fp.read(), b'hello')
+ with self.assertRaises(urllib.error.URLError):
+ self.fake_urlopen(b'')
+
+ # urlcleanup() resets the opener.
+ urllib.request.urlcleanup()
+ self.assertEqual(self.fake_urlopen(b'second'), b'second')
+
+
class QuotingTests(unittest.TestCase):
r"""Tests for urllib.quote() and urllib.quote_plus()
_______________________________________________
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]