Skip to content

Commit 1921da6

Browse files
allow __suppress_context__ and __notes__ to be mutated on frozen exceptions (#1365)
* allow __suppress_context__ to be mutated on frozen exceptions * add changelog * and __notes__! lets' not forget that. * add notes to news * add tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix improt * fix test * fix tests again --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 58cd3ae commit 1921da6

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

changelog.d/1365.change.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow mutating `__suppress_context__` and `__notes__` on frozen exceptions.

src/attr/_make.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,8 @@ def _frozen_setattrs(self, name, value):
543543
"__cause__",
544544
"__context__",
545545
"__traceback__",
546+
"__suppress_context__",
547+
"__notes__",
546548
):
547549
BaseException.__setattr__(self, name, value)
548550
return
@@ -554,6 +556,10 @@ def _frozen_delattrs(self, name):
554556
"""
555557
Attached to frozen classes as __delattr__.
556558
"""
559+
if isinstance(self, BaseException) and name in ("__notes__",):
560+
BaseException.__delattr__(self, name)
561+
return
562+
557563
raise FrozenInstanceError
558564

559565

tests/test_next_gen.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import attr as _attr # don't use it by accident
1515
import attrs
1616

17+
from attr._compat import PY_3_11_PLUS
18+
1719

1820
@attrs.define
1921
class C:
@@ -332,7 +334,7 @@ class MyException(Exception):
332334
attrs.mutable,
333335
],
334336
)
335-
def test_setting_traceback_on_exception(self, decorator):
337+
def test_setting_exception_mutable_attributes(self, decorator):
336338
"""
337339
contextlib.contextlib (re-)sets __traceback__ on raised exceptions.
338340
@@ -354,6 +356,16 @@ def do_nothing():
354356

355357
# this should not raise an exception either
356358
ei.value.__traceback__ = ei.value.__traceback__
359+
ei.value.__cause__ = ValueError("cause")
360+
ei.value.__context__ = TypeError("context")
361+
ei.value.__suppress_context__ = True
362+
ei.value.__suppress_context__ = False
363+
ei.value.__notes__ = []
364+
del ei.value.__notes__
365+
366+
if PY_3_11_PLUS:
367+
ei.value.add_note("note")
368+
del ei.value.__notes__
357369

358370
def test_converts_and_validates_by_default(self):
359371
"""

0 commit comments

Comments
 (0)