Skip to content

Commit

Permalink
Old Python fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusSintonen committed Jun 10, 2024
1 parent 5235e3c commit 6ef797e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion httpcore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __init__(self, *args, **kwargs): # type: ignore
"WriteError",
]

__version__ = "1.0.6"
__version__ = "1.0.5"


__locals = locals()
Expand Down
28 changes: 23 additions & 5 deletions httpcore/_synchronization.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import sys
import threading
from types import TracebackType
from typing import (
Expand Down Expand Up @@ -29,6 +30,18 @@
anyio = None # type: ignore


if sys.version_info >= (3, 11): # pragma: nocover
import asyncio as asyncio_timeout

anyio_shield = None
else: # pragma: nocover
import async_timeout as asyncio_timeout

if anyio is None: # pragma: nocover
raise RuntimeError("Running in Python<3.11 requires anyio")
anyio_shield = anyio.CancelScope


AsyncBackend = Literal["asyncio", "trio"]


Expand Down Expand Up @@ -163,9 +176,11 @@ async def wait(self, timeout: Optional[float] = None) -> None:
with trio.fail_after(timeout_or_inf):
await event.wait()
else:
asyncio_exc_map: ExceptionMapping = {TimeoutError: PoolTimeout}
asyncio_exc_map: ExceptionMapping = {
asyncio.exceptions.TimeoutError: PoolTimeout
}
with map_exceptions(asyncio_exc_map):
async with asyncio.timeout(timeout):
async with asyncio_timeout.timeout(timeout):
await event.wait()


Expand Down Expand Up @@ -217,17 +232,20 @@ async def shield(shielded: Callable[[], Coroutine[Any, Any, None]]) -> None:
if current_async_backend() == "trio":
with trio.CancelScope(shield=True):
await shielded()
elif sys.version_info < (3, 11): # pragma: nocover
with anyio_shield(shield=True):
await shielded()
else:
await AsyncShieldCancellation._asyncio_shield(shielded)
await AsyncShieldCancellation._asyncio_shield(shielded) # pragma: nocover

@staticmethod
async def _asyncio_shield(
shielded: Callable[[], Coroutine[Any, Any, None]],
) -> None:
) -> None: # pragma: nocover
inner_task = asyncio.create_task(shielded())
try:
await asyncio.shield(inner_task)
except asyncio.CancelledError:
except (asyncio.exceptions.CancelledError, asyncio.CancelledError):
# Let the inner_task to complete as it was shielded from the cancellation
await inner_task

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ classifiers = [
dependencies = [
"certifi",
"h11>=0.13,<0.15",
"async-timeout==4.*; python_version < '3.11'",
]

[project.optional-dependencies]
Expand Down
16 changes: 0 additions & 16 deletions tests/conftest.py

This file was deleted.

0 comments on commit 6ef797e

Please sign in to comment.