From f3b14fc9d5d702501e083f56e544f2ec3d43c1f4 Mon Sep 17 00:00:00 2001 From: AjobK Date: Sun, 19 Jul 2026 12:38:58 +0200 Subject: [PATCH 1/5] add test for httperror props such as reason and fp, and stringified urlerror test --- Lib/test/test_urllib.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 2dd739b77b8e4d1..f3957750ddbdffd 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -467,6 +467,18 @@ def test_redirect_limit_independent(self): finally: self.unfakehttp() + def test_http_error_attribute_values(self): + hdrs = { + "Authorization": "Bearer foobar", + "Accept": "application/json" + } + exc = urllib.error.HTTPError("http://something", 404, "foo", hdrs, None) + self.assertEqual(exc.url, "http://something") + self.assertEqual(exc.code, 404) + self.assertEqual(exc.msg, "foo") + self.assertEqual(exc.hdrs, hdrs) + self.assertIsInstance(exc.fp, io.BytesIO) + def test_empty_socket(self): # urlopen() raises OSError if the underlying socket does not send any # data. (#1680230) @@ -513,6 +525,12 @@ 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(reason, err.reason) + self.assertEqual(str(err), '' % reason) + class urlopen_DataTests(unittest.TestCase): """Test urlopen() opening a data URL.""" From 06620bf7c4277e4ca0628ac7509b9aa33a867cf8 Mon Sep 17 00:00:00 2001 From: AjobK Date: Sun, 19 Jul 2026 12:50:13 +0200 Subject: [PATCH 2/5] rm unnecessary 'reason' attr test, change url to filename and add reason and headers attr --- Lib/test/test_urllib.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index f3957750ddbdffd..d5660c29e2b74a9 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -473,10 +473,12 @@ def test_http_error_attribute_values(self): "Accept": "application/json" } exc = urllib.error.HTTPError("http://something", 404, "foo", hdrs, None) - self.assertEqual(exc.url, "http://something") + self.assertEqual(exc.filename, "http://something") self.assertEqual(exc.code, 404) self.assertEqual(exc.msg, "foo") + self.assertEqual(exc.reason, "foo") self.assertEqual(exc.hdrs, hdrs) + self.assertEqual(exc.headers, hdrs) self.assertIsInstance(exc.fp, io.BytesIO) def test_empty_socket(self): @@ -528,8 +530,7 @@ def test_ftp_nonexisting(self): def test_url_error_stringified(self): reason = 'sixseven' err = urllib.error.URLError(reason) - self.assertEqual(reason, err.reason) - self.assertEqual(str(err), '' % reason) + self.assertEqual(str(err), f'') class urlopen_DataTests(unittest.TestCase): From ca75c944c36bd71d2d99d7f91b0af0a95f523d08 Mon Sep 17 00:00:00 2001 From: AjobK Date: Sun, 19 Jul 2026 12:51:49 +0200 Subject: [PATCH 3/5] separate file pointer test --- Lib/test/test_urllib.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index d5660c29e2b74a9..ac025e63518e0af 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -479,6 +479,9 @@ def test_http_error_attribute_values(self): self.assertEqual(exc.reason, "foo") self.assertEqual(exc.hdrs, hdrs) self.assertEqual(exc.headers, hdrs) + + def test_http_error_default_fp(self): + exc = urllib.error.HTTPError("http://something", 404, "foo", {}, None) self.assertIsInstance(exc.fp, io.BytesIO) def test_empty_socket(self): From a975d7e37b9c51d0ea16aa6a0ef5a09360482b91 Mon Sep 17 00:00:00 2001 From: AjobK Date: Sun, 19 Jul 2026 13:54:02 +0200 Subject: [PATCH 4/5] prevent resource warning, close httperror exception --- Lib/test/test_urllib.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index ac025e63518e0af..903e8d4b22a1981 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -479,10 +479,12 @@ def test_http_error_attribute_values(self): self.assertEqual(exc.reason, "foo") self.assertEqual(exc.hdrs, hdrs) self.assertEqual(exc.headers, hdrs) + exc.close() def test_http_error_default_fp(self): exc = urllib.error.HTTPError("http://something", 404, "foo", {}, None) self.assertIsInstance(exc.fp, io.BytesIO) + exc.close() def test_empty_socket(self): # urlopen() raises OSError if the underlying socket does not send any From 49fda8f1b16138b086f2e11429186362f7c5267c Mon Sep 17 00:00:00 2001 From: AjobK Date: Sun, 19 Jul 2026 13:57:57 +0200 Subject: [PATCH 5/5] exc > err --- Lib/test/test_urllib.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 903e8d4b22a1981..1e5f79998e7cab2 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -472,19 +472,19 @@ def test_http_error_attribute_values(self): "Authorization": "Bearer foobar", "Accept": "application/json" } - exc = urllib.error.HTTPError("http://something", 404, "foo", hdrs, None) - self.assertEqual(exc.filename, "http://something") - self.assertEqual(exc.code, 404) - self.assertEqual(exc.msg, "foo") - self.assertEqual(exc.reason, "foo") - self.assertEqual(exc.hdrs, hdrs) - self.assertEqual(exc.headers, hdrs) - exc.close() + 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): - exc = urllib.error.HTTPError("http://something", 404, "foo", {}, None) - self.assertIsInstance(exc.fp, io.BytesIO) - exc.close() + 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