Repository navigation
HTTPError fp.read returns string instead of bytes #101936
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Feb 15, 2023 cc @corona10
@gaborbernat @sobolevn
Thanks, I will take a look and use this issue through the internal OSS sprint as the good first bug.
I am mentoring them this year Thank you for your understanding!Reacted by sobolevn and Owain DaviesI'll take it
@corona10 I ran code in live run the code
from mailbox import Message from urllib.error import HTTPError from urllib.request import urlopen try: urlopen("http://asadsad.sd") except HTTPError as exception: content = exception.fp.read() print(type(content)) error = HTTPError(url="url", code=HTTPStatus.IM_A_TEAPOT, msg="msg", hdrs=Message(), fp=None) print(type(error.fp.read()))I checked type of fp
print(exception.fp)is <class 'http.client.HTTPResponse'>
it is not StringIO, that mean fp is not None. And if you call read() method it will return bytes
Line 639 in dc8a868
raise HTTPError(req.full_url, code, msg, hdrs, fp)
https://docs.python.org/3/library/http.client.html#http.client.HTTPResponse.readDo I change StringIO to BytesIO when fp is None ?
The title is incorrect, should be read returns string instead of bytes when fp is None.
This script might explain better.
from urllib.request import urlopen from urllib.error import HTTPError try: urlopen("https://httpstat.us/500") except HTTPError as exception: content = exception.fp.read() print(type(exception.fp)) print(type(content))
Do I change StringIO to BytesIO when fp is None ?
Yes please go ahead with nice test code.
- changed the title
[-]HTTPError fp.read returns bytes instead of string [/-][+]HTTPError fp.read returns string instead of bytes [/+]on Feb 21, 2023 The title is incorrect, should be read returns string instead of bytes when fp is None.
Thanks, I modified the title.
- added a commit that references this issue
on Feb 21, 2023 @longvhdeveloper Thank you for your hard work!
- added a commit that references this issue
on Feb 23, 2023
Due to https://github.com/python/cpython/pull/99966/files
However, this returns a bytes stream on the live run:
Linked PRs