GeezerVison is showing github.com — HTTPError fp.read returns string instead of bytes · Issue #101936 · python/cpython
HTTPError fp.read returns string instead of bytes #101936DescriptionDue to https://github.com/python/cpython/pull/99966/files
However, this returns a bytes stream on the live run:
from http import HTTPStatus
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()))
<class 'bytes'>
<class 'str'>
Linked PRs
Activitysobolevn commented on Feb 15, 2023 corona10 commented on Feb 15, 2023 @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 Davies longvhdeveloper commented on Feb 20, 2023 longvhdeveloper commented on Feb 21, 2023 @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
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
|
raise HTTPError(req.full_url, code, msg, hdrs, fp) |
https://docs.python.org/3/library/http.client.html#http.client.HTTPResponse.read
Do I change StringIO to BytesIO when fp is None ? gaborbernat commented on Feb 21, 2023 ContributorAuthor More actions The title is incorrect, should be read returns string instead of bytes when fp is None. corona10 commented on Feb 21, 2023 @longvhdeveloper
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, 2023corona10 commented 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, 2023gh-101936: Update the default value of fp from io.StringIO to io.Byte… corona10 commented on Feb 22, 2023
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|