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 by corona10 · Pull Request #99966 · python/cpython

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
corona10 merged 3 commits into python:main from corona10:gh-98778
Dec 8, 2022

Uh oh!

There was an error while loading. Please reload this page.

Merged

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

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
3 commits
Select commit Hold shift + click to select a range
448231f
gh-98778: Update HTTPError to initialize properly even if fp is None
corona10 Dec 3, 2022
91a6e6f
Address code review
corona10 Dec 7, 2022
c3a75a1
Update Lib/urllib/error.py
corona10 Dec 8, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

4 changes: 4 additions & 0 deletions 4 Lib/test/test_urllib2.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,10 @@ def test_HTTPError_interface(self):
expected_errmsg = '<HTTPError %s: %r>' % (err.code, err.msg)
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):
parse_proxy_test_cases = [
('proxy.example.com',
Expand Down
11 changes: 4 additions & 7 deletions 11 Lib/urllib/error.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
an application may want to handle an exception like a regular
response.
"""

import io
import urllib.response

__all__ = ['URLError', 'HTTPError', 'ContentTooShortError']
Expand Down Expand Up @@ -42,12 +42,9 @@ def __init__(self, url, code, msg, hdrs, fp):
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()
Comment thread

@gaborbernat gaborbernat Feb 15, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be BytesIO?

from urllib.request import urlopen
from urllib.error import HTTPError
try:
    urlopen('http://asadsad.sd')
except HTTPError as exception:
    content = exception.fp.read()
    print(type(content))
<class 'bytes'>

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
self.__super_init(fp, hdrs, url, code)

def __str__(self):
return 'HTTP Error %s: %s' % (self.code, self.msg)
Expand Down
2 changes: 2 additions & 0 deletions 2 Misc/NEWS.d/next/Library/2022-12-03-20-06-16.gh-issue-98778.t5U9uc.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Update :exc:`~urllib.error.HTTPError` to be initialized properly, even if
the ``fp`` is ``None``. Patch by Dong-hee Na.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — 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.