Skip to content

Commit 003dbd9

Browse files
committed
gh-98778: Update HTTPError to initialize properly even if fp is None
1 parent ee60156 commit 003dbd9

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

Lib/tempfile.py

+7
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ def __init__(self, file, name, delete=True, delete_on_close=True):
428428
self.delete_on_close = delete_on_close
429429

430430
def cleanup(self, windows=(_os.name == 'nt'), unlink=_os.unlink):
431+
if self.file is None:
432+
self.cleanup_called = True
433+
return
431434
if not self.cleanup_called:
432435
self.cleanup_called = True
433436
try:
@@ -444,6 +447,10 @@ def cleanup(self, windows=(_os.name == 'nt'), unlink=_os.unlink):
444447
pass
445448

446449
def close(self):
450+
if self.file is None:
451+
self.close_called = True
452+
return
453+
447454
if not self.close_called:
448455
self.close_called = True
449456
try:

Lib/test/test_urllib2.py

+4
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,10 @@ def test_HTTPError_interface(self):
18241824
expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg)
18251825
self.assertEqual(repr(err), expected_errmsg)
18261826

1827+
def test_gh_98778(self):
1828+
x = urllib.error.HTTPError("url", 405, "METHOD NOT ALLOWED", None, None)
1829+
self.assertEqual(getattr(x, "__notes__", ()), ())
1830+
18271831
def test_parse_proxy(self):
18281832
parse_proxy_test_cases = [
18291833
('proxy.example.com',

Lib/urllib/error.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,7 @@ 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+
self.__super_init(fp, hdrs, url, code)
5146

5247
def __str__(self):
5348
return 'HTTP Error %s: %s' % (self.code, self.msg)
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)