Skip to content

HTTPError fp.read returns string instead of bytes  #101936

Description

@gaborbernat

Due 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

Activity

  1. sobolevn commented on Feb 15, 2023

    @sobolevn
    Member
  2. self-assigned this
    on Feb 15, 2023
  3. corona10 commented on Feb 15, 2023

    @corona10
    Member

    @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!

  4. longvhdeveloper commented on Feb 20, 2023

    @longvhdeveloper
    Contributor

    I'll take it

  5. longvhdeveloper commented on Feb 21, 2023

    @longvhdeveloper
    Contributor

    @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

    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 ?

  6. gaborbernat commented on Feb 21, 2023

    @gaborbernat
    ContributorAuthor

    The title is incorrect, should be read returns string instead of bytes when fp is None.

  7. corona10 commented on Feb 21, 2023

    @corona10
    Member

    @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.

  8. changed the title [-]HTTPError fp.read returns bytes instead of string [/-] [+]HTTPError fp.read returns string instead of bytes [/+] on Feb 21, 2023
  9. corona10 commented on Feb 21, 2023

    @corona10
    Member

    The title is incorrect, should be read returns string instead of bytes when fp is None.

    Thanks, I modified the title.

  10. added a commit that references this issue on Feb 21, 2023
  11. added 2 commits that reference this issue on Feb 21, 2023
  12. added 2 commits that reference this issue on Feb 22, 2023
  13. corona10 commented on Feb 22, 2023

    @corona10
    Member

    @longvhdeveloper Thank you for your hard work!

  14. added a commit that references this issue on Feb 23, 2023
  15. added 2 commits that reference this issue on Sep 1, 2024
  16. added a commit that references this issue on Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

type-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions