classification
Title: StopIteration subclass suppressed by contextlib.contextmanager
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: graingert, lukasz.langa, miss-islington, ncoghlan, rhettinger, yselivanov
Priority: normal Keywords: patch

Created on 2021-07-05 09:45 by graingert, last changed 2021-07-20 19:13 by lukasz.langa. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27024 merged graingert, 2021-07-05 10:02
PR 27266 merged miss-islington, 2021-07-20 18:16
PR 27269 merged lukasz.langa, 2021-07-20 18:42
Messages (6)
msg396979 - (view) Author: Thomas Grainger (graingert) * Date: 2021-07-05 09:45
https://bugs.python.org/issue1462485


import contextlib

@contextlib.contextmanager
def foo():
    yield

class StartIrritation(StopIteration):
    pass


with foo():
    raise StartIrritation
msg396983 - (view) Author: Thomas Grainger (graingert) * Date: 2021-07-05 09:52
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())
```
msg397892 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-20 18:15
New changeset 7f1c330da31c54e028dceaf3610877914c2a4497 by Thomas Grainger in branch 'main':
bpo-44566: resolve differences between asynccontextmanager and contextmanager (#27024)
https://github.com/python/cpython/commit/7f1c330da31c54e028dceaf3610877914c2a4497
msg397902 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-20 19:12
New changeset 68b4690b010d0006e9b0235903afd367191f3c51 by Miss Islington (bot) in branch '3.10':
bpo-44566: resolve differences between asynccontextmanager and contextmanager (GH-27024) (#27266)
https://github.com/python/cpython/commit/68b4690b010d0006e9b0235903afd367191f3c51
msg397903 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-20 19:13
New changeset 1c5c9c89ffc36875afaf4c3cc6a716d4bd089bbf by Łukasz Langa in branch '3.9':
[3.9] bpo-44566: resolve differences between asynccontextmanager and contextmanager (GH-27024). (#27269)
https://github.com/python/cpython/commit/1c5c9c89ffc36875afaf4c3cc6a716d4bd089bbf
msg397904 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-20 19:13
Thanks! ✨ 🍰 ✨
History
Date User Action Args
2021-07-20 19:13:37lukasz.langasetstatus: open -> closed
resolution: fixed
messages: + msg397904

stage: patch review -> resolved
2021-07-20 19:13:02lukasz.langasetmessages: + msg397903
2021-07-20 19:12:54lukasz.langasetmessages: + msg397902
2021-07-20 18:42:16lukasz.langasetpull_requests: + pull_request25813
2021-07-20 18:16:09miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request25810
2021-07-20 18:15:14lukasz.langasetnosy: + lukasz.langa
messages: + msg397892
2021-07-05 20:48:22rhettingersettitle: StopIteration subclass raised in body of 'with' statement suppressed -> StopIteration subclass suppressed by contextlib.contextmanager
type: behavior
components: + Library (Lib)
versions: - Python 3.6, Python 3.7, Python 3.8
2021-07-05 20:45:01rhettingersetnosy: + rhettinger
2021-07-05 10:02:11graingertsetkeywords: + patch
stage: patch review
pull_requests: + pull_request25590
2021-07-05 09:52:56graingertsetmessages: + msg396983
2021-07-05 09:45:40graingertcreate