Skip to content

bpo-42062: Set HTTPResponse.url at init#22738

Open
fbidu wants to merge 1 commit into
python:mainfrom
fbidu:fix-httpresponse-url
Open

bpo-42062: Set HTTPResponse.url at init#22738
fbidu wants to merge 1 commit into
python:mainfrom
fbidu:fix-httpresponse-url

Conversation

@fbidu

@fbidu fbidu commented Oct 17, 2020

Copy link
Copy Markdown
Contributor

Apparently, HTTPResponse.url is accessed but not set by __init__ nor
any other method. This PR initializes it through __init__

https://bugs.python.org/issue42062

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Dec 16, 2020

@MaxwellDupre MaxwellDupre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked out this branch and modified the end of the your test script to:
a= r1.geturl() print (a) a =r1.url print (a)
And the output was None and None. For either of these to be useful shouldn't they output something other than none?
Also, could you add test(s) to the test module please?

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jul 30, 2022

@iritkatriel iritkatriel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned above, a PR like this requires a unittest to assert that the bug being fixed does not reappear (though it's not clear that there is a bug here).

@bedevere-bot

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@python-cla-bot

python-cla-bot Bot commented Apr 18, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@github-actions

github-actions Bot commented Apr 8, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 8, 2026
@fbidu fbidu force-pushed the fix-httpresponse-url branch from fbf155b to f6d81f5 Compare June 16, 2026 09:07
@fbidu

fbidu commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews, and apologies for the long delay here.

To clarify the bug (re: @iritkatriel "it's not clear that there is a bug here", and @MaxwellDupre seeing None): HTTPResponse.__init__ already accepts a url parameter, but it was never stored on the instance. So geturl() — which returns self.url — raises AttributeError on any response constructed directly through http.client rather than via urllib (which sets r.url externally in do_open).

Reproduction on current main:

>>> import http.client, io
>>> class S:
...     def makefile(self, *a, **k): return io.BytesIO(b"")
...     def close(self): pass
>>> http.client.HTTPResponse(S(), url="http://example.com/").geturl()
Traceback (most recent call last):
  ...
AttributeError: 'HTTPResponse' object has no attribute 'url'

This PR stores self.url = url in __init__, so geturl() returns the URL when one is passed (and None instead of raising when it isn't — matching the "or None" behavior of the neighboring accessors). I've also added a regression test (test_url_set_at_init) covering both cases, added a Misc/NEWS.d entry, and rebased onto current main.

I have made the requested changes; please review again

@bedevere-app

bedevere-app Bot commented Jun 16, 2026

Copy link
Copy Markdown

Thanks for making the requested changes!

@iritkatriel: please review the changes made to this pull request.

@bedevere-app bedevere-app Bot requested a review from iritkatriel June 16, 2026 09:08
HTTPResponse.__init__ accepted a ``url`` argument but never stored it,
so geturl() raised AttributeError on responses created directly via
http.client rather than through urllib.  Store it on self.url and add a
regression test.
@fbidu fbidu force-pushed the fix-httpresponse-url branch from f6d81f5 to 1d9213c Compare June 16, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting change review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants