`contextlib.contextmanager` raises `dataclasses.FrozenInstanceError` when handling a frozen dataclass exception
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 36k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit contextlib.py, insbesondere mit dem im Traceback gezeigten Exit-Pfad von contextmanager, und reproduziere den Fehler mit dem bereitgestellten Beispiel einer eingefrorenen Dataclass-Ausnahme unter Python 3.11. Als erledigt gilt die Aufgabe, wenn die Ausnahme über den Context Manager behandelt werden kann, ohne FrozenInstanceError auszulösen, und dabei das erwartete Verhalten von Python 3.10 erhalten bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100