From e44b25429e83d7817c9e63ce53fa770ef8be72c2 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 29 Dec 2024 15:33:30 +0000 Subject: [PATCH] optimize --- starlette/_utils.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/starlette/_utils.py b/starlette/_utils.py index daa17d8c..143c660f 100644 --- a/starlette/_utils.py +++ b/starlette/_utils.py @@ -4,7 +4,7 @@ import asyncio import functools import sys import typing -from contextlib import asynccontextmanager, contextmanager +from contextlib import asynccontextmanager import anyio.abc @@ -74,10 +74,11 @@ class AwaitableOrContextManagerWrapper(typing.Generic[SupportsAsyncCloseType]): return None -@contextmanager -def _collapse_excgroups() -> typing.Generator[None, None, None]: +@asynccontextmanager +async def create_collapsing_task_group() -> typing.AsyncGenerator[anyio.abc.TaskGroup, None]: try: - yield + async with anyio.create_task_group() as tg: + yield tg except BaseExceptionGroup as excs: if len(excs.exceptions) != 1: raise @@ -97,13 +98,6 @@ def _collapse_excgroups() -> typing.Generator[None, None, None]: del exc, cause, tb, context -@asynccontextmanager -async def create_collapsing_task_group() -> typing.AsyncGenerator[anyio.abc.TaskGroup, None]: - with _collapse_excgroups(): - async with anyio.create_task_group() as tg: - yield tg - - def get_route_path(scope: Scope) -> str: path: str = scope["path"] root_path = scope.get("root_path", "") -- 2.47.3