GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-98778: Update HTTPError to initialize properly even if fp is None … · python/cpython@846898e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Lib
    • test
      • test_urllib2.py
    • urllib
      • error.py
  • Misc/NEWS.d/next/Library
    • 2022-12-03-20-06-16.gh-issue-98778.t5U9uc.rst
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Lib/test/test_urllib2.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_urllib2.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,10 @@ def test_HTTPError_interface(self):
18251825
expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg)
18261826
self.assertEqual(repr(err), expected_errmsg)
18271827

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+
18281832
def test_parse_proxy(self):
18291833
parse_proxy_test_cases = [
18301834
('proxy.example.com',
Collapse file

‎Lib/urllib/error.py‎

Copy file name to clipboardExpand all lines: Lib/urllib/error.py
+4-7Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
an application may want to handle an exception like a regular
1111
response.
1212
"""
13-
13+
import io
1414
import urllib.response
1515

1616
__all__ = ['URLError', 'HTTPError', 'ContentTooShortError']
@@ -42,12 +42,9 @@ def __init__(self, url, code, msg, hdrs, fp):
4242
self.hdrs = hdrs
4343
self.fp = fp
4444
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)
5148

5249
def __str__(self):
5350
return 'HTTP Error %s: %s' % (self.code, self.msg)
Collapse file

‎Misc/NEWS.d/next/Library/2022-12-03-20-06-16.gh-issue-98778.t5U9uc.rst‎

Copy file name to clipboard
+2Lines changed: 2 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update :exc:`~urllib.error.HTTPError` to be initialized properly, even if
2+
the ``fp`` is ``None``. Patch by Dong-hee Na.

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.