Skip to content
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

[3.8] Eliminate side-effects from the ClientResponse.ok property #5407

Merged
merged 1 commit into from
Feb 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/5403.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Stop automatically releasing the ``ClientResponse`` object on calls to the ``ok`` property for the failed requests.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
A. Jesse Jiryu Davis
Adam Bannister
Adam Cooper
Adam Horacek
Adam Mills
Adrian Krupa
Adrián Chaves
Expand Down
8 changes: 2 additions & 6 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,14 +995,10 @@ def ok(self) -> bool:
This is **not** a check for ``200 OK`` but a check that the response
status is under 400.
"""
try:
self.raise_for_status()
except ClientResponseError:
return False
return True
return 400 > self.status

def raise_for_status(self) -> None:
if 400 <= self.status:
if not self.ok:
# reason should always be not None for a started response
assert self.reason is not None
self.release()
Expand Down
21 changes: 21 additions & 0 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,3 +1256,24 @@ def test_response_links_empty(loop, session) -> None:
)
response._headers = CIMultiDict()
assert response.links == {}


def test_response_not_closed_after_get_ok(mocker) -> None:
response = ClientResponse(
"get",
URL("http://del-cl-resp.org"),
request_info=mock.Mock(),
writer=mock.Mock(),
continue100=None,
timer=TimerNoop(),
traces=[],
loop=mock.Mock(),
session=mock.Mock(),
)
response.status = 400
response.reason = "Bad Request"
response._closed = False
spy = mocker.spy(response, "raise_for_status")
assert not response.ok
assert not response.closed
assert spy.call_count == 0