Skip to content

Commit 2402e8e

Browse files
Improve shield usage in old Python. Code review fixes
1 parent 2a27e9e commit 2402e8e

File tree

8 files changed

+68
-108
lines changed

8 files changed

+68
-108
lines changed

httpcore/_async/connection_pool.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
88
from .._exceptions import ConnectionNotAvailable, UnsupportedProtocol
99
from .._models import Origin, Request, Response
10-
from .._synchronization import AsyncEvent, AsyncShieldCancellation, AsyncThreadLock
10+
from .._synchronization import AsyncEvent, AsyncThreadLock, async_cancel_shield
1111
from .connection import AsyncHTTPConnection
1212
from .interfaces import AsyncConnectionInterface, AsyncRequestInterface
1313

@@ -307,7 +307,7 @@ async def close() -> None:
307307
for connection in closing:
308308
await connection.aclose()
309309

310-
await AsyncShieldCancellation.shield(close)
310+
await async_cancel_shield(close)
311311

312312
async def aclose(self) -> None:
313313
# Explicitly close the connection pool.
@@ -376,7 +376,7 @@ async def aclose(self) -> None:
376376
self._closed = True
377377

378378
if hasattr(self._stream, "aclose"):
379-
await AsyncShieldCancellation.shield(self._stream.aclose)
379+
await async_cancel_shield(self._stream.aclose)
380380

381381
with self._pool._optional_thread_lock:
382382
self._pool._requests.remove(self._pool_request)

httpcore/_async/http11.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
map_exceptions,
2626
)
2727
from .._models import Origin, Request, Response
28-
from .._synchronization import AsyncLock, AsyncShieldCancellation
28+
from .._synchronization import AsyncLock, async_cancel_shield
2929
from .._trace import Trace
3030
from .interfaces import AsyncConnectionInterface
3131

@@ -138,7 +138,7 @@ async def handle_async_request(self, request: Request) -> Response:
138138
)
139139
except BaseException as exc:
140140
async with Trace("response_closed", logger, request) as trace:
141-
await AsyncShieldCancellation.shield(self._response_closed)
141+
await async_cancel_shield(self._response_closed)
142142
raise exc
143143

144144
# Sending the request...
@@ -343,7 +343,7 @@ async def __aiter__(self) -> AsyncIterator[bytes]:
343343
# If we get an exception while streaming the response,
344344
# we want to close the response (and possibly the connection)
345345
# before raising that exception.
346-
await AsyncShieldCancellation.shield(self.aclose)
346+
await async_cancel_shield(self.aclose)
347347
raise exc
348348

349349
async def aclose(self) -> None:

httpcore/_async/http2.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
RemoteProtocolError,
1818
)
1919
from .._models import Origin, Request, Response
20-
from .._synchronization import AsyncLock, AsyncSemaphore, AsyncShieldCancellation
20+
from .._synchronization import AsyncLock, AsyncSemaphore, async_cancel_shield
2121
from .._trace import Trace
2222
from .interfaces import AsyncConnectionInterface
2323

@@ -108,7 +108,7 @@ async def handle_async_request(self, request: Request) -> Response:
108108
async with Trace("send_connection_init", logger, request, kwargs):
109109
await self._send_connection_init(**kwargs)
110110
except BaseException as exc:
111-
await AsyncShieldCancellation.shield(self.aclose)
111+
await async_cancel_shield(self.aclose)
112112
raise exc
113113

114114
self._sent_connection_init = True
@@ -166,7 +166,7 @@ async def close() -> None:
166166
async with Trace("response_closed", logger, request, kwargs):
167167
await self._response_closed(stream_id=stream_id)
168168

169-
await AsyncShieldCancellation.shield(close)
169+
await async_cancel_shield(close)
170170

171171
if isinstance(exc, h2.exceptions.ProtocolError):
172172
# One case where h2 can raise a protocol error is when a
@@ -579,7 +579,7 @@ async def __aiter__(self) -> typing.AsyncIterator[bytes]:
579579
# If we get an exception while streaming the response,
580580
# we want to close the response (and possibly the connection)
581581
# before raising that exception.
582-
await AsyncShieldCancellation.shield(self.aclose)
582+
await async_cancel_shield(self.aclose)
583583
raise exc
584584

585585
async def aclose(self) -> None:

httpcore/_sync/connection_pool.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .._backends.base import SOCKET_OPTION, NetworkBackend
88
from .._exceptions import ConnectionNotAvailable, UnsupportedProtocol
99
from .._models import Origin, Request, Response
10-
from .._synchronization import Event, ShieldCancellation, ThreadLock
10+
from .._synchronization import Event, ThreadLock, sync_cancel_shield
1111
from .connection import HTTPConnection
1212
from .interfaces import ConnectionInterface, RequestInterface
1313

@@ -307,7 +307,7 @@ def close() -> None:
307307
for connection in closing:
308308
connection.close()
309309

310-
ShieldCancellation.shield(close)
310+
sync_cancel_shield(close)
311311

312312
def close(self) -> None:
313313
# Explicitly close the connection pool.
@@ -376,7 +376,7 @@ def close(self) -> None:
376376
self._closed = True
377377

378378
if hasattr(self._stream, "close"):
379-
ShieldCancellation.shield(self._stream.close)
379+
sync_cancel_shield(self._stream.close)
380380

381381
with self._pool._optional_thread_lock:
382382
self._pool._requests.remove(self._pool_request)

httpcore/_sync/http11.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
map_exceptions,
2626
)
2727
from .._models import Origin, Request, Response
28-
from .._synchronization import Lock, ShieldCancellation
28+
from .._synchronization import Lock, sync_cancel_shield
2929
from .._trace import Trace
3030
from .interfaces import ConnectionInterface
3131

@@ -138,7 +138,7 @@ def handle_request(self, request: Request) -> Response:
138138
)
139139
except BaseException as exc:
140140
with Trace("response_closed", logger, request) as trace:
141-
ShieldCancellation.shield(self._response_closed)
141+
sync_cancel_shield(self._response_closed)
142142
raise exc
143143

144144
# Sending the request...
@@ -343,7 +343,7 @@ def __iter__(self) -> Iterator[bytes]:
343343
# If we get an exception while streaming the response,
344344
# we want to close the response (and possibly the connection)
345345
# before raising that exception.
346-
ShieldCancellation.shield(self.close)
346+
sync_cancel_shield(self.close)
347347
raise exc
348348

349349
def close(self) -> None:

httpcore/_sync/http2.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
RemoteProtocolError,
1818
)
1919
from .._models import Origin, Request, Response
20-
from .._synchronization import Lock, Semaphore, ShieldCancellation
20+
from .._synchronization import Lock, Semaphore, sync_cancel_shield
2121
from .._trace import Trace
2222
from .interfaces import ConnectionInterface
2323

@@ -108,7 +108,7 @@ def handle_request(self, request: Request) -> Response:
108108
with Trace("send_connection_init", logger, request, kwargs):
109109
self._send_connection_init(**kwargs)
110110
except BaseException as exc:
111-
ShieldCancellation.shield(self.close)
111+
sync_cancel_shield(self.close)
112112
raise exc
113113

114114
self._sent_connection_init = True
@@ -166,7 +166,7 @@ def close() -> None:
166166
with Trace("response_closed", logger, request, kwargs):
167167
self._response_closed(stream_id=stream_id)
168168

169-
ShieldCancellation.shield(close)
169+
sync_cancel_shield(close)
170170

171171
if isinstance(exc, h2.exceptions.ProtocolError):
172172
# One case where h2 can raise a protocol error is when a
@@ -579,7 +579,7 @@ def __iter__(self) -> typing.Iterator[bytes]:
579579
# If we get an exception while streaming the response,
580580
# we want to close the response (and possibly the connection)
581581
# before raising that exception.
582-
ShieldCancellation.shield(self.close)
582+
sync_cancel_shield(self.close)
583583
raise exc
584584

585585
def close(self) -> None:

0 commit comments

Comments
 (0)