[issue44566] StopIteration subclass raised in body of 'with' statement suppressed

Thomas Grainger report at bugs.python.org
Mon Jul 5 05:52:56 EDT 2021


Thomas Grainger <tagrain at gmail.com> added the comment:

This is the output:

```
Traceback (most recent call last):
  File "/home/graingert/projects/close.py", line 5, in foo
    yield
  File "/home/graingert/projects/close.py", line 12, in <module>
    raise StartIrritation
__main__.StartIrritation

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/graingert/projects/close.py", line 11, in <module>
    with foo():
  File "/usr/lib/python3.10/contextlib.py", line 151, in __exit__
    self.gen.throw(type, value, traceback)
RuntimeError: generator raised StopIteration
```


Note that this was fixed in @contextlib.asynccontextmanager
```
import asyncio
import contextlib

@contextlib.asynccontextmanager
async def foo():
    yield

class StartIrritation(StopIteration):
    pass


async def amain():
    try:
        async with foo():
            raise StartIrritation
    except StartIrritation:
        print("good")
    except RuntimeError:
        print("bad")

asyncio.run(amain())
```

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44566>
_______________________________________


More information about the Python-bugs-list mailing list