Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
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.
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
corona10 commented Dec 3, 2022 • edited by bedevere-bot
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by bedevere-bot
Uh oh!
There was an error while loading. Please reload this page.
corona10 commented Dec 3, 2022
Since the |
corona10 commented Dec 3, 2022
|
netlifybot commented Dec 7, 2022 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
✅ Deploy Preview for python-cpython-preview canceled.
|
ambv left a comment
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.
You made changes to _TemporaryFileCloser but urllib.response.addinfourl here is a subclass of _TemporaryFileWrapper. Now after your changes the .close() and .cleanup() operations work but the wrapper is still in an inconsistent state. It cannot be used as a context manager, and cannot be iterated. The wrapper proxies attribute access to the file handle so asking things like exc.mode, exc.seekable(), etc. will all fail.
The cleanest solution would be to change HTTPError to not subclass this thing at all... but that ship has sailed a long time ago.
Your change weakens _TemporaryFileCloser by allowing it to silently accept None in lieu of a file handle. This makes tempfile worse, I'm not convinced this is a good compromise.
Instead, since HTTPError is already a hack (and there was literally a comment about why it's hacky where the bug was created), I would just replace the hack with a different one: set fp to io.StringIO() if the passed fp is None. It's still not perfect but I feel that would be more robust.
bedevere-bot commented Dec 7, 2022
When you're done making the requested changes, leave the comment: |
corona10 commented Dec 7, 2022
+1 |
corona10 commented Dec 7, 2022
Thank you for the review and for pointing out what I missed :) I have made the requested changes; please review again |
bedevere-bot commented Dec 7, 2022
Thanks for making the requested changes! @ambv: please review the changes made to this pull request. |
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
miss-islington commented Dec 8, 2022
Thanks @corona10 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10, 3.11. |
bedevere-bot commented Dec 8, 2022
GH-100096 is a backport of this pull request to the 3.11 branch. |
… None (pythongh-99966) (cherry picked from commit dc8a868) Co-authored-by: Dong-hee Na <donghee.na@python.org>
… None (pythongh-99966) (cherry picked from commit dc8a868) Co-authored-by: Dong-hee Na <donghee.na@python.org>
bedevere-bot commented Dec 8, 2022
GH-100097 is a backport of this pull request to the 3.10 branch. |
| iffpisnotNone: | ||
| self.__super_init(fp, hdrs, url, code) | ||
| iffpisNone: | ||
| fp=io.StringIO() |
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?
fromurllib.requestimporturlopenfromurllib.errorimportHTTPErrortry: urlopen('http://asadsad.sd') exceptHTTPErrorasexception: content=exception.fp.read() print(type(content))<class 'bytes'>
urllib.error.HTTPError(..., fp=None)raises aKeyErrorinstead of anAttributeErroron attribute access #98778