GeezerVison is showing github.com — gh-98778: Update HTTPError to initialize properly even if fp is None … · python/cpython@846898e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Lib/test/test_urllib2.py +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1825,6 +1825,10 @@ def test_HTTPError_interface(self): | 1825 | 1825 | expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg) | 1826 | 1826 | self.assertEqual(repr(err), expected_errmsg) | 1827 | 1827 |
| | 1828 | + def test_gh_98778(self): | | 1829 | + x = urllib.error.HTTPError("url", 405, "METHOD NOT ALLOWED", None, None) | | 1830 | + self.assertEqual(getattr(x, "__notes__", ()), ()) | | 1831 | + | 1828 | 1832 | def test_parse_proxy(self): | 1829 | 1833 | parse_proxy_test_cases = [ | 1830 | 1834 | ('proxy.example.com', | |
Collapse file Copy file name to clipboardExpand all lines: Lib/urllib/error.py +4-7Lines changed: 4 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | an application may want to handle an exception like a regular | 11 | 11 | response. | 12 | 12 | """ | 13 | | - | | 13 | +import io | 14 | 14 | import urllib.response | 15 | 15 |
| 16 | 16 | __all__ = ['URLError', 'HTTPError', 'ContentTooShortError'] | @@ -42,12 +42,9 @@ def __init__(self, url, code, msg, hdrs, fp): | 42 | 42 | self.hdrs = hdrs | 43 | 43 | self.fp = fp | 44 | 44 | self.filename = url | 45 | | - # The addinfourl classes depend on fp being a valid file | 46 | | - # object. In some cases, the HTTPError may not have a valid | 47 | | - # file object. If this happens, the simplest workaround is to | 48 | | - # not initialize the base classes. | 49 | | - if fp is not None: | 50 | | - self.__super_init(fp, hdrs, url, code) | | 45 | + if fp is None: | | 46 | + fp = io.StringIO() | | 47 | + self.__super_init(fp, hdrs, url, code) | 51 | 48 |
| 52 | 49 | def __str__(self): | 53 | 50 | return 'HTTP Error %s: %s' % (self.code, self.msg) | |
Collapse file Copy file name to clipboard +2Lines changed: 2 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +Update :exc:`~urllib.error.HTTPError` to be initialized properly, even if | | 2 | +the ``fp`` is ``None``. Patch by Dong-hee Na. |
|