From 2c6872aa537a41a584b363b8948492841f5b31a2 Mon Sep 17 00:00:00 2001 From: matham Date: Tue, 11 Aug 2020 15:34:00 -0400 Subject: [PATCH 1/2] Fix @asynccontextmanager doesn't work well with async generators Workaround for https://bugs.python.org/issue33786 that wasn't backported to python 3.7 --- async_generator/_util.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/async_generator/_util.py b/async_generator/_util.py index aa6f113..bf6655d 100644 --- a/async_generator/_util.py +++ b/async_generator/_util.py @@ -51,9 +51,10 @@ async def __aexit__(self, type, value, traceback): assert value is not None try: await self._agen.athrow(type, value, traceback) - raise RuntimeError( - "async generator didn't stop after athrow()" - ) + if sys.version_info[:2] >= (3, 8) or not isinstance(value, GeneratorExit): + raise RuntimeError( + "async generator didn't stop after athrow()" + ) except StopAsyncIteration as exc: # Suppress StopIteration *unless* it's the same exception # that was passed to throw(). This prevents a From c7710e53c3b7a436a87eb9dfc41accfe865363af Mon Sep 17 00:00:00 2001 From: matham Date: Mon, 24 Aug 2020 15:49:31 -0400 Subject: [PATCH 2/2] The fix was backported to 3.7.9. --- async_generator/_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async_generator/_util.py b/async_generator/_util.py index bf6655d..f10096c 100644 --- a/async_generator/_util.py +++ b/async_generator/_util.py @@ -51,7 +51,7 @@ async def __aexit__(self, type, value, traceback): assert value is not None try: await self._agen.athrow(type, value, traceback) - if sys.version_info[:2] >= (3, 8) or not isinstance(value, GeneratorExit): + if sys.version_info[:3] >= (3, 7, 9) or not isinstance(value, GeneratorExit): raise RuntimeError( "async generator didn't stop after athrow()" )