-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
Calling read() on HTTPError may cause KeyError in tempfile #90113
Comments
HTTPError may not be fully initialized in some scenarios leading to an inconsistent interface. This is documented in code at: Lines 45 to 50 in 55fe1ae
Unfortunately the way this is implemented creates an inconsistent interface, and opaque code, without a number of inline comments explaining the behavior of HTTPError. Additionally, the way that it currently works, will cause a KeyError to be raised from tempfile, which is rather confusing. Instead of "partially initializing" the HTTPError object, I'd propose that when fp is None, that we provide it with something like io.BytesIO to fulfill the interface. There may be other recommended solutions, I've not thought through this extensively yet. I think I just prefer always calling self.__super_init but passing in something like io.BytesIO if fp is None I'm willing to create the PR once I know which direction seems to make the most sense. >>> from urllib.error import HTTPError
>>> from urllib.request import HTTPDigestAuthHandler, HTTPPasswordMgrWithDefaultRealm, build_opener
>>> passman = HTTPPasswordMgrWithDefaultRealm()
>>> passman.add_password(None, 'httpbin.org', 'user', 'wrong')
>>> opener = build_opener(HTTPDigestAuthHandler(passman))
>>> try:
... opener.open('https://httpbin.org/digest-auth/auth/user/passwd')
... except HTTPError as e:
... e.read()
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File ".../3.10.0/lib/python3.10/urllib/request.py", line 525, in open
response = meth(req, response)
File ".../3.10.0/lib/python3.10/urllib/request.py", line 634, in http_response
response = self.parent.error(
File ".../3.10.0/lib/python3.10/urllib/request.py", line 557, in error
result = self._call_chain(*args)
File ".../3.10.0/lib/python3.10/urllib/request.py", line 496, in _call_chain
result = func(*args)
File ".../3.10.0/lib/python3.10/urllib/request.py", line 1238, in http_error_401
retry = self.http_error_auth_reqed('www-authenticate',
File ".../3.10.0/lib/python3.10/urllib/request.py", line 1111, in http_error_auth_reqed
raise HTTPError(req.full_url, 401, "digest auth failed",
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
File ".../3.10.0/lib/python3.10/tempfile.py", line 473, in __getattr__
file = self.__dict__['file']
KeyError: 'file' |
Work around python/cpython#90113 which strikes on Python 3.11. Fix GHA: ubuntu-latest no longer contains Python 2.7 up to 3.6
Work around python/cpython#90113
* Fix GHA: ubuntu-latest no longer contains Python 3.5 and 3.6 * Add support for Python 3.11. * Work around python/cpython#90113
Work around python/cpython#90113 which strikes on Python 3.11. Fix GHA: ubuntu-latest no longer contains Python 2.7 up to 3.6
Calling This happens when |
Looks like be fixed. |
@corona10 I forgot to mention that I am using Python 3.9.16. Is there a chance to get the fix backported to that version? |
This issue was solved by the following issues. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: