GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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  #101936

New issue
Copy link
New issue
Copy link
Closed
Closed
HTTPError fp.read returns string instead of bytes #101936
Copy link
Assignees
longvhdevelopercorona10
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@gaborbernat
gaborbernat
opened on Feb 15, 2023
Issue body actions

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

  • gh-101936: Update the default value of fp from io.StringIO to io.BytesIO #102100
  • [3.11] gh-101936: Update the default value of fp from io.StringIO to io.BytesIO (gh-102100) #102117
  • [3.10] gh-101936: Update the default value of fp from io.StringIO to io.BytesIO (gh-102100) #102118

Activity

  1. gaborbernat
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Feb 15, 2023
  2. sobolevn commented on Feb 15, 2023

    @sobolevn
    sobolevn
    on Feb 15, 2023
    Member
    More actions

    cc @corona10

  3. corona10
    self-assigned this
    on Feb 15, 2023
  4. corona10 commented on Feb 15, 2023

    @corona10
    corona10
    on Feb 15, 2023
    Member
    More actions

    @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
  5. longvhdeveloper commented on Feb 20, 2023

    @longvhdeveloper
    longvhdeveloper
    on Feb 20, 2023
    Contributor
    More actions

    I'll take it

  6. corona10
    assigned
    longvhdeveloper
    on Feb 20, 2023
  7. longvhdeveloper commented on Feb 21, 2023

    @longvhdeveloper
    longvhdeveloper
    on Feb 21, 2023
    Contributor
    More actions

    @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

    cpython/Lib/urllib/request.py

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

    Do I change StringIO to BytesIO when fp is None ?

  8. gaborbernat commented on Feb 21, 2023

    @gaborbernat
    gaborbernat
    on Feb 21, 2023
    ContributorAuthor
    More actions

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

  9. corona10 commented on Feb 21, 2023

    @corona10
    corona10
    on Feb 21, 2023
    Member
    More actions

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

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

    @corona10
    corona10
    on Feb 21, 2023
    Member
    More actions

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

    Thanks, I modified the title.

  12. corona10
    added a commit that references this issue on Feb 21, 2023

    gh-101936: Update the default value of fp from io.StringIO to io.Byte…

    0d4c7fc
  13. miss-islington
    added 2 commits that reference this issue on Feb 21, 2023

    pythongh-101936: Update the default value of fp from io.StringIO to i…

    e99397c

    pythongh-101936: Update the default value of fp from io.StringIO to i…

    496a500
  14. corona10
    added 2 commits that reference this issue on Feb 22, 2023

    [3.11] gh-101936: Update the default value of fp from io.StringIO to …

    edbde8f

    [3.10] gh-101936: Update the default value of fp from io.StringIO to …

    0f28af5
  15. corona10 commented on Feb 22, 2023

    @corona10
    corona10
    on Feb 22, 2023
    Member
    More actions

    @longvhdeveloper Thank you for your hard work!

  16. corona10
    closed this as completedon Feb 22, 2023
  17. carljm
    added a commit that references this issue on Feb 23, 2023

    Merge branch 'main' into lockfreecp

    b257a16
  18. python-sidebar
    added 2 commits that reference this issue on Sep 1, 2024

    pythongh-101936: Update the default value of fp from io.StringIO to i…

    563bb72

    pythongh-101936: Update the default value of fp from io.StringIO to i…

    4f88406
  19. JelleZijlstra
    added a commit that references this issue on Sep 10, 2024

    pythongh-101936: Update the default value of fp from io.StringIO to i…

    c1a279c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @corona10
    corona10
  • @longvhdeveloper
    longvhdeveloper

Labels

type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.