]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
optimize
authorThomas Grainger <tagrain@gmail.com>
Sun, 29 Dec 2024 15:33:30 +0000 (15:33 +0000)
committerThomas Grainger <tagrain@gmail.com>
Sun, 29 Dec 2024 15:33:30 +0000 (15:33 +0000)
starlette/_utils.py

index daa17d8c48589d16c34471b2bdbb60268f75acb8..143c660f36c6f02342546fac6d022928022bee28 100644 (file)
@@ -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", "")