8000 Usage of HTTPResponse.url · Issue #86228 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Usage of HTTPResponse.url #86228

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

Open
fbidu mannequin opened this issue Oct 17, 2020 · 6 comments
Open

Usage of HTTPResponse.url #86228

fbidu mannequin opened this issue Oct 17, 2020 · 6 comments
Labels
stdlib Python modules in the Lib dir

Comments

@fbidu
Copy link
Mannequin
fbidu mannequin commented Oct 17, 2020
BPO 42062
Nosy @vadmium, @fbidu
PRs
  • bpo-42062: Set HTTPResponse.url at init #22738
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2020-10-17.14:13:56.336>
    labels = ['library']
    title = 'Usage of HTTPResponse.url'
    updated_at = <Date 2020-10-18.01:59:43.626>
    user = 'https://github.com/fbidu'

    bugs.python.org fields:

    activity = <Date 2020-10-18.01:59:43.626>
    actor = 'martin.panter'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2020-10-17.14:13:56.336>
    creator = 'fbidu'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42062
    keywords = ['patch']
    message_count = 2.0
    messages = ['378814', '378846']
    nosy_count = 2.0
    nosy_names = ['martin.panter', 'fbidu']
    pr_nums = ['22738']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue42062'
    versions = []

    @fbidu
    Copy link
    Mannequin Author
    fbidu mannequin commented Oct 17, 2020

    Hello all,

    While testing some static analysis tools on HTTP/client.py, Pylint pointed
    me to HTTPResponse.geturl() method with a "no-member" error for the url
    attribute. I tried invoking the geturl method and reading the
    HTTPResponse.url attribute using a sample code from the official docs:

    import http.client
    conn = http.client.HTTPSConnection("www.python.org")
    conn.request("GET", "/")
    r1 = conn.getresponse()
    print(r1.status, r1.reason)
    
    r1.geturl()
    r1.url
    
    import http.client
    conn = http.client.HTTPSConnection("www.python.org")
    conn.request("GET", "/")
    r1 = conn.getresponse()
    data1 = r1.read()
    
    conn.request("GET", "/")
    r1 = conn.getresponse()
    while chunk := r1.read(200):
        print(repr(chunk))
    r1.geturl()
    r1.url
    

    Both of those examples will raise an AttributeError: 'HTTPResponse' object has no attribute 'url'.

    I tried searching through this module's history from when this line originally appeared,
    6c5e28c but
    I wasn't able to find this attribute being set internally by the class, even
    though there is an url attribute at init.

    So, I wonder if this attribute was intended to be set externally as in r1.url = 'something'
    or if it is just a bug

    @fbidu fbidu mannequin added stdlib Python modules in the Lib dir labels Oct 17, 2020
    @vadmium
    Copy link
    Member
    vadmium commented Oct 18, 2020

    There is a comment in the HTTPResponse class regarding these methods:

    # For compatibility with old-style urllib responses.

    They were there for the "urlopen" API in "urllib.request", not for the "http.client" module on its own. I expect the "url" attribute is set by the "urlopen" code.

    However more recently (bpo-12707) the "url" attribute and "geturl" method were documented in the HTTPResponse documentation, which is awkward.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @dimo414
    Copy link
    dimo414 commented Jan 9, 2024

    This seems to still be broken in 3.12:

    $ docker run --rm python:3.12 python -c \
      'import http.client as httplib; conn = httplib.HTTPConnection("google.com"); conn.request("GET", "/"); print(conn.getresponse().geturl())'
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/local/lib/python3.12/http/client.py", line 790, in geturl
        return self.url
               ^^^^^^^^
    AttributeError: 'HTTPResponse' object has no attribute 'url'
    


    HTTPResponse.url
    doesn't exist, and HTTPResponse.geturl() is broken. I tried 3.12, 3.11, 3.10, 3.9, and 3.8 (prior to #11447), it doesn't look like this has ever worked.

    FYI @merwok and @matrixise who approved #11447.

    @merwok
    Copy link
    Member
    merwok commented Jan 9, 2024

    Tangent: I now wish the original PR had also updated the docstrings on the deprecated methods (it only added a code comment).

    @vadmium
    Member
    vadmium commented Feb 6, 2024

    Another problem with this documentation is it suggests HTTPResponse.headers is an EmailMessage instance, while HTTPResponse.msg remains only a (subclassed) instance of email.message.Message. And addinfourl.info and addinfourl.headers are now supposed to return an EmailMessage, although .info was previously (indirectly) documented to return an email.message.Message.

    I tested Python 3.10.2 and it seems HTTPResponse.headers has not changed because the object didn’t have one of the methods unique to EmailMessage:

    >>> u = urlopen('http://google.com')
    >>> u.headers.is_attachment
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'HTTPMessage' object has no attribute 'is_attachment'

    warren-bank added a commit to warren-bank/fork-pyodide-http that referenced this issue Jan 24, 2025
    Though not directly relevant,
    this is also an issue with cpython:
      python/cpython#86228
    and there is an open pull request:
      https://github.com/python/cpython/pull/22738/files
    
    Pyodide uses Javascript XHR to perform the network request,
    and constructs an 'HTTPResponse' object from the XHR response.
    When doing so, a 'url' attribute should be added.
    @futurend
    Copy link

    Still present on Python 3.13.3 in 26 may 2025

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants
    0