Skip to content

Commit 6ef797e

Browse files
Old Python fixes
1 parent 5235e3c commit 6ef797e

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

httpcore/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def __init__(self, *args, **kwargs): # type: ignore
130130
"WriteError",
131131
]
132132

133-
__version__ = "1.0.6"
133+
__version__ = "1.0.5"
134134

135135

136136
__locals = locals()

httpcore/_synchronization.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import sys
23
import threading
34
from types import TracebackType
45
from typing import (
@@ -29,6 +30,18 @@
2930
anyio = None # type: ignore
3031

3132

33+
if sys.version_info >= (3, 11): # pragma: nocover
34+
import asyncio as asyncio_timeout
35+
36+
anyio_shield = None
37+
else: # pragma: nocover
38+
import async_timeout as asyncio_timeout
39+
40+
if anyio is None: # pragma: nocover
41+
raise RuntimeError("Running in Python<3.11 requires anyio")
42+
anyio_shield = anyio.CancelScope
43+
44+
3245
AsyncBackend = Literal["asyncio", "trio"]
3346

3447

@@ -163,9 +176,11 @@ async def wait(self, timeout: Optional[float] = None) -> None:
163176
with trio.fail_after(timeout_or_inf):
164177
await event.wait()
165178
else:
166-
asyncio_exc_map: ExceptionMapping = {TimeoutError: PoolTimeout}
179+
asyncio_exc_map: ExceptionMapping = {
180+
asyncio.exceptions.TimeoutError: PoolTimeout
181+
}
167182
with map_exceptions(asyncio_exc_map):
168-
async with asyncio.timeout(timeout):
183+
async with asyncio_timeout.timeout(timeout):
169184
await event.wait()
170185

171186

@@ -217,17 +232,20 @@ async def shield(shielded: Callable[[], Coroutine[Any, Any, None]]) -> None:
217232
if current_async_backend() == "trio":
218233
with trio.CancelScope(shield=True):
219234
await shielded()
235+
elif sys.version_info < (3, 11): # pragma: nocover
236+
with anyio_shield(shield=True):
237+
await shielded()
220238
else:
221-
await AsyncShieldCancellation._asyncio_shield(shielded)
239+
await AsyncShieldCancellation._asyncio_shield(shielded) # pragma: nocover
222240

223241
@staticmethod
224242
async def _asyncio_shield(
225243
shielded: Callable[[], Coroutine[Any, Any, None]],
226-
) -> None:
244+
) -> None: # pragma: nocover
227245
inner_task = asyncio.create_task(shielded())
228246
try:
229247
await asyncio.shield(inner_task)
230-
except asyncio.CancelledError:
248+
except (asyncio.exceptions.CancelledError, asyncio.CancelledError):
231249
# Let the inner_task to complete as it was shielded from the cancellation
232250
await inner_task
233251

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ classifiers = [
3131
dependencies = [
3232
"certifi",
3333
"h11>=0.13,<0.15",
34+
"async-timeout==4.*; python_version < '3.11'",
3435
]
3536

3637
[project.optional-dependencies]

tests/conftest.py

-16
This file was deleted.

0 commit comments

Comments
 (0)