diff options
author | Dong-hee Na <donghee.na@python.org> | 2022-12-08 02:20:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-08 02:20:34 (GMT) |
commit | dc8a86893df37e137cfe992e95e7d66cd68e9eaf (patch) | |
tree | 4894d493412dbaea966d149bb35236f4a311ae12 /Lib/urllib | |
parent | 3c892022472eb975360fb3f0caa6f6fcc6fbf220 (diff) | |
download | cpython-dc8a86893df37e137cfe992e95e7d66cd68e9eaf.zip cpython-dc8a86893df37e137cfe992e95e7d66cd68e9eaf.tar.gz cpython-dc8a86893df37e137cfe992e95e7d66cd68e9eaf.tar.bz2 |
gh-98778: Update HTTPError to initialize properly even if fp is None (gh-99966)
Diffstat (limited to 'Lib/urllib')
-rw-r--r-- | Lib/urllib/error.py | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/Lib/urllib/error.py b/Lib/urllib/error.py index 8cd901f..feec0e7 100644 --- a/Lib/urllib/error.py +++ b/Lib/urllib/error.py @@ -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 response. """ - +import io import urllib.response __all__ = ['URLError', 'HTTPError', 'ContentTooShortError'] @@ -42,12 +42,9 @@ class HTTPError(URLError, urllib.response.addinfourl): self.hdrs = hdrs self.fp = fp self.filename = url - # The addinfourl classes depend on fp being a valid file - # object. In some cases, the HTTPError may not have a valid - # 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) + if fp is None: + fp = io.StringIO() + self.__super_init(fp, hdrs, url, code) def __str__(self): return 'HTTP Error %s: %s' % (self.code, self.msg) |