Skip to content

Commit 41ee5e7

Browse files
Neaten up our try...except structure for ensuring responses (#1525)
1 parent 4b4769a commit 41ee5e7

2 files changed

Lines changed: 69 additions & 54 deletions

File tree

httpx/_client.py

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -762,21 +762,18 @@ def send(
762762
allow_redirects=allow_redirects,
763763
history=[],
764764
)
765-
766-
if not stream:
767-
try:
765+
try:
766+
if not stream:
768767
response.read()
769-
finally:
770-
response.close()
771768

772-
try:
773769
for hook in self._event_hooks["response"]:
774770
hook(response)
775-
except Exception:
776-
response.close()
777-
raise
778771

779-
return response
772+
return response
773+
774+
except Exception as exc:
775+
response.close()
776+
raise exc
780777

781778
def _send_handling_auth(
782779
self,
@@ -800,18 +797,20 @@ def _send_handling_auth(
800797
history=history,
801798
)
802799
try:
803-
next_request = auth_flow.send(response)
804-
except StopIteration:
805-
return response
806-
except BaseException as exc:
807-
response.close()
808-
raise exc from None
809-
else:
800+
try:
801+
next_request = auth_flow.send(response)
802+
except StopIteration:
803+
return response
804+
810805
response.history = list(history)
811806
response.read()
812807
request = next_request
813808
history.append(response)
814809

810+
except Exception as exc:
811+
response.close()
812+
raise exc
813+
815814
def _send_handling_redirects(
816815
self,
817816
request: Request,
@@ -826,19 +825,24 @@ def _send_handling_redirects(
826825
)
827826

828827
response = self._send_single_request(request, timeout)
829-
response.history = list(history)
828+
try:
829+
response.history = list(history)
830830

831-
if not response.is_redirect:
832-
return response
831+
if not response.is_redirect:
832+
return response
833833

834-
if allow_redirects:
835-
response.read()
836-
request = self._build_redirect_request(request, response)
837-
history = history + [response]
834+
request = self._build_redirect_request(request, response)
835+
history = history + [response]
838836

839-
if not allow_redirects:
840-
response.next_request = request
841-
return response
837+
if allow_redirects:
838+
response.read()
839+
else:
840+
response.next_request = request
841+
return response
842+
843+
except Exception as exc:
844+
response.close()
845+
raise exc
842846

843847
def _send_single_request(self, request: Request, timeout: Timeout) -> Response:
844848
"""
@@ -1394,21 +1398,18 @@ async def send(
13941398
allow_redirects=allow_redirects,
13951399
history=[],
13961400
)
1397-
1398-
if not stream:
1399-
try:
1401+
try:
1402+
if not stream:
14001403
await response.aread()
1401-
finally:
1402-
await response.aclose()
14031404

1404-
try:
14051405
for hook in self._event_hooks["response"]:
14061406
await hook(response)
1407-
except Exception:
1408-
await response.aclose()
1409-
raise
14101407

1411-
return response
1408+
return response
1409+
1410+
except Exception as exc:
1411+
await response.aclose()
1412+
raise exc
14121413

14131414
async def _send_handling_auth(
14141415
self,
@@ -1432,18 +1433,20 @@ async def _send_handling_auth(
14321433
history=history,
14331434
)
14341435
try:
1435-
next_request = await auth_flow.asend(response)
1436-
except StopAsyncIteration:
1437-
return response
1438-
except BaseException as exc:
1439-
await response.aclose()
1440-
raise exc from None
1441-
else:
1436+
try:
1437+
next_request = await auth_flow.asend(response)
1438+
except StopAsyncIteration:
1439+
return response
1440+
14421441
response.history = list(history)
14431442
await response.aread()
14441443
request = next_request
14451444
history.append(response)
14461445

1446+
except Exception as exc:
1447+
await response.aclose()
1448+
raise exc
1449+
14471450
async def _send_handling_redirects(
14481451
self,
14491452
request: Request,
@@ -1458,19 +1461,24 @@ async def _send_handling_redirects(
14581461
)
14591462

14601463
response = await self._send_single_request(request, timeout)
1461-
response.history = list(history)
1464+
try:
1465+
response.history = list(history)
14621466

1463-
if not response.is_redirect:
1464-
return response
1467+
if not response.is_redirect:
1468+
return response
14651469

1466-
if allow_redirects:
1467-
await response.aread()
1468-
request = self._build_redirect_request(request, response)
1469-
history = history + [response]
1470+
request = self._build_redirect_request(request, response)
1471+
history = history + [response]
14701472

1471-
if not allow_redirects:
1472-
response.next_request = request
1473-
return response
1473+
if allow_redirects:
1474+
await response.aread()
1475+
else:
1476+
response.next_request = request
1477+
return response
1478+
1479+
except Exception as exc:
1480+
await response.aclose()
1481+
raise exc
14741482

14751483
async def _send_single_request(
14761484
self, request: Request, timeout: Timeout

tests/client/test_redirects.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,10 @@ def test_redirect_custom_scheme():
393393
with pytest.raises(httpx.UnsupportedProtocol) as e:
394394
client.post("https://example.org/redirect_custom_scheme")
395395
assert str(e.value) == "Scheme 'market' not supported."
396+
397+
398+
@pytest.mark.usefixtures("async_environment")
399+
async def test_async_invalid_redirect():
400+
async with httpx.AsyncClient(transport=httpx.MockTransport(redirects)) as client:
401+
with pytest.raises(httpx.RemoteProtocolError):
402+
await client.get("http://example.org/invalid_redirect")

0 commit comments

Comments
 (0)