[3.15] urllib: Add tests for HTTP errors to complete coverage (GH-154102) (#154120)

tomasr8 <[email protected]>
Newsgroups gmane.comp.python.cvs
Message-ID <[email protected]>
https://github.com/python/cpython/commit/1df0ecd523b4c15cba26ed4df5ac22f2d209cfc7
commit: 1df0ecd523b4c15cba26ed4df5ac22f2d209cfc7
branch: 3.15
author: Miss Islington (bot) <[email protected]>
committer: tomasr8 <[email protected]>
date: 2026-07-20T08:38:10+02:00
summary:

[3.15] urllib: Add tests for HTTP errors to complete coverage (GH-154102) (#154120)

urllib: Add tests for HTTP errors to complete coverage (GH-154102)

* add test for httperror props such as reason and fp, and stringified urlerror test

* rm unnecessary 'reason' attr test, change url to filename and add reason and headers attr

* separate file pointer test

* prevent resource warning, close httperror exception

* exc > err
(cherry picked from commit b8ec956716c183430a93929e6415ceed74089af1)

Co-authored-by: Ajob Kustra <[email protected]>

files:
M Lib/test/test_urllib.py

diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
index 2dd739b77b8e4d..1e5f79998e7cab 100644
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -467,6 +467,25 @@ def test_redirect_limit_independent(self):
             finally:
                 self.unfakehttp()
 
+    def test_http_error_attribute_values(self):
+        hdrs = {
+            "Authorization": "Bearer foobar",
+            "Accept": "application/json"
+        }
+        err = urllib.error.HTTPError("http://something", 404, "foo", hdrs, None)
+        self.assertEqual(err.filename, "http://something")
+        self.assertEqual(err.code, 404)
+        self.assertEqual(err.msg, "foo")
+        self.assertEqual(err.reason, "foo")
+        self.assertEqual(err.hdrs, hdrs)
+        self.assertEqual(err.headers, hdrs)
+        err.close()
+
+    def test_http_error_default_fp(self):
+        err = urllib.error.HTTPError("http://something", 404, "foo", {}, None)
+        self.assertIsInstance(err.fp, io.BytesIO)
+        err.close()
+
     def test_empty_socket(self):
         # urlopen() raises OSError if the underlying socket does not send any
         # data. (#1680230)
@@ -513,6 +532,11 @@ def test_ftp_nonexisting(self):
         self.assertFalse(e.exception.filename)
         self.assertTrue(e.exception.reason)
 
+    def test_url_error_stringified(self):
+        reason = 'sixseven'
+        err = urllib.error.URLError(reason)
+        self.assertEqual(str(err), f'<urlopen error {reason}>')
+
 
 class urlopen_DataTests(unittest.TestCase):
     """Test urlopen() opening a data URL."""

_______________________________________________
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]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.