Original error not present in traceback #830
-
I don't think it's specifically an AnyIO issue, but I find it strange that the following code crashes with no mention to the original error (that from contextlib import AsyncExitStack
from anyio import Event, create_task_group, run
class Foo:
def __init__(self):
self.started = Event()
async def __aenter__(self):
async with AsyncExitStack() as exit_stack:
self.task_group = await exit_stack.enter_async_context(create_task_group())
self._exit_stack = exit_stack.pop_all()
self.task_group.start_soon(self.task)
await self.started.wait()
return self
async def __aexit__(self, exc_type, exc_value, exc_tb):
return await self._exit_stack.__aexit__(exc_type, exc_value, exc_tb)
async def task(self):
bar # should raise a NameError
self.started.set()
async def main():
async with Foo():
pass
run(main) It just raises a from contextlib import AsyncExitStack
from anyio import Event, create_task_group, get_cancelled_exc_class, run
class Foo:
def __init__(self):
self.started = Event()
async def __aenter__(self):
async with AsyncExitStack() as exit_stack:
self.task_group = await exit_stack.enter_async_context(create_task_group())
self._exit_stack = exit_stack.pop_all()
self.task_group.start_soon(self.task)
try:
await self.started.wait()
except get_cancelled_exc_class():
pass
return self
async def __aexit__(self, exc_type, exc_value, exc_tb):
return await self._exit_stack.__aexit__(exc_type, exc_value, exc_tb)
async def task(self):
bar
self.started.set()
async def main():
async with Foo():
pass
run(main) So this means that |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There's a mistake in how you are running your AsyncExitStack: --- before.py 2024-11-30 07:16:46.519537586 +0000
+++ after.py 2024-11-30 07:17:41.005645305 +0000
@@ -10,9 +10,9 @@
async def __aenter__(self):
async with AsyncExitStack() as exit_stack:
self.task_group = await exit_stack.enter_async_context(create_task_group())
- self._exit_stack = exit_stack.pop_all()
self.task_group.start_soon(self.task)
await self.started.wait()
+ self._exit_stack = exit_stack.pop_all()
return self
async def __aexit__(self, exc_type, exc_value, exc_tb): the pop all needs to be the absolute last operation before the return |
Beta Was this translation helpful? Give feedback.
There's a mistake in how you are running your AsyncExitStack:
the pop all needs to be the absolute last operation before the r…