Skip to content

contextlib.contextmanager raises dataclasses.FrozenInstanceError when handling a frozen dataclass exception #99856

Open
@mattclay

Description

@mattclay

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 #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-dataclassestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions