`contextlib.contextmanager` raises `dataclasses.FrozenInstanceError` when handling a frozen dataclass exception
未关闭
还没有人认领这个 Issue。
stdlib
topic-dataclasses
type-bug
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- 平均合并
- 1 天 9 小时
- 30 天内合并 PR
- 558
描述
Bug report
In Python 3.11, raising a frozen dataclass as an exception inside a contextlib.contextmanager context will raise a dataclasses.FrozenInstanceError exception. This issue does not occur in Python 3.10.
This is a result of the changes made in https://github.com/python/cpython/pull/92202
Reproducer
import contextlib
import dataclasses
@dataclasses.dataclass(frozen=True)
class Notice(Exception):
pass
@contextlib.contextmanager
def demo():
yield
try:
with demo():
raise Notice()
except Notice:
pass
Expected Result
$ python3.10 ./repro.py
Actual Result
$ python3.11 ./repro.py
Traceback (most recent call last):
File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__
self.gen.throw(typ, value, traceback)
File "/home/mclay/code/mattclay/contextlib-repro/./repro.py", line 12, in demo
yield
File "/home/mclay/code/mattclay/contextlib-repro/./repro.py", line 17, in <module>
raise Notice()
Notice
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/mclay/code/mattclay/contextlib-repro/./repro.py", line 16, in <module>
with demo():
File "/usr/lib/python3.11/contextlib.py", line 188, in __exit__
exc.__traceback__ = traceback
^^^^^^^^^^^^^^^^^
File "<string>", line 4, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field '__traceback__'
Your environment
$ python3.10 -V
Python 3.10.6
$ python3.11 -V
Python 3.11.0
$ source /etc/os-release; echo $PRETTY_NAME
Ubuntu 22.04.1 LTS
$ uname -a
Linux redstone 5.11.0-27-generic #29~20.04.1-Ubuntu SMP Wed Aug 11 15:58:17 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 contextlib.py 开始,尤其检查 traceback 中显示的 contextmanager 退出路径,并使用所提供的 frozen dataclass exception 示例在 Python 3.11 上重现该故障。当能够通过 context manager 处理该异常而不引发 FrozenInstanceError,同时保留 Python 3.10 所表现出的预期行为时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 45/100