gh-98778: Update HTTPError to initialize properly even if fp is None (gh-99966)

(cherry picked from commit dc8a86893d)

Co-authored-by: Dong-hee Na <donghee.na@python.org>
This commit is contained in:
Miss Islington (bot) 2022-12-07 20:17:23 -08:00 committed by GitHub
parent 2997f3913a
commit 846898e5ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 7 deletions

View file

@ -1825,6 +1825,10 @@ class MiscTests(unittest.TestCase):
expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg) expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg)
self.assertEqual(repr(err), expected_errmsg) self.assertEqual(repr(err), expected_errmsg)
def test_gh_98778(self):
x = urllib.error.HTTPError("url", 405, "METHOD NOT ALLOWED", None, None)
self.assertEqual(getattr(x, "__notes__", ()), ())
def test_parse_proxy(self): def test_parse_proxy(self):
parse_proxy_test_cases = [ parse_proxy_test_cases = [
('proxy.example.com', ('proxy.example.com',

View file

@ -10,7 +10,7 @@ responses, with a status code, headers, and a body. In some contexts,
an application may want to handle an exception like a regular an application may want to handle an exception like a regular
response. response.
""" """
import io
import urllib.response import urllib.response
__all__ = ['URLError', 'HTTPError', 'ContentTooShortError'] __all__ = ['URLError', 'HTTPError', 'ContentTooShortError']
@ -42,11 +42,8 @@ class HTTPError(URLError, urllib.response.addinfourl):
self.hdrs = hdrs self.hdrs = hdrs
self.fp = fp self.fp = fp
self.filename = url self.filename = url
# The addinfourl classes depend on fp being a valid file if fp is None:
# object. In some cases, the HTTPError may not have a valid fp = io.StringIO()
# file object. If this happens, the simplest workaround is to
# not initialize the base classes.
if fp is not None:
self.__super_init(fp, hdrs, url, code) self.__super_init(fp, hdrs, url, code)
def __str__(self): def __str__(self):

View file

@ -0,0 +1,2 @@
Update :exc:`~urllib.error.HTTPError` to be initialized properly, even if
the ``fp`` is ``None``. Patch by Dong-hee Na.