Skip to content

Commit 6d3daf1

Browse files
committed
Fix __suppress_context__ not being respected in exception rendering
1 parent 2ee39b9 commit 6d3daf1

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

news/508.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix `__suppress_context__` not being respected in exception rendering

src/cleo/ui/exception_trace/inspector.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ def __init__(self, exception: BaseException) -> None:
1212
self._frames: FrameCollection | None = None
1313
self._outer_frames = None
1414
self._inner_frames = None
15-
self._previous_exception = exception.__context__
15+
self._previous_exception = (
16+
None if exception.__suppress_context__ else exception.__context__
17+
)
1618

1719
@property
1820
def exception(self) -> BaseException:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
def raiser_with_suppressed_context() -> None:
3+
try:
4+
raise ValueError("This error should be suppressed.")
5+
except ValueError:
6+
raise RuntimeError("This error should be displayed.") from None

tests/ui/test_exception_trace.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from cleo.ui.exception_trace.component import ExceptionTrace
1212
from tests.fixtures.exceptions import nested1
1313
from tests.fixtures.exceptions import nested2
14+
from tests.fixtures.exceptions import raiser_with_suppressed_context
1415
from tests.fixtures.exceptions import recursion
1516
from tests.fixtures.exceptions import simple
1617

@@ -50,7 +51,7 @@ def test_render_debug_better_error_message() -> None:
5051

5152
trace.render(io)
5253

53-
lineno = 47
54+
lineno = 48
5455
expected = f"""
5556
Stack trace:
5657
@@ -84,7 +85,7 @@ def test_render_debug_better_error_message_recursion_error() -> None:
8485
except RecursionError as e:
8586
trace = ExceptionTrace(e)
8687

87-
lineno = 83
88+
lineno = 84
8889
trace.render(io)
8990

9091
expected = rf"""^
@@ -131,7 +132,7 @@ def test_render_very_verbose_better_error_message() -> None:
131132
expected = f"""
132133
Stack trace:
133134
134-
1 {trace._get_relative_file_path(__file__)}:125 in \
135+
1 {trace._get_relative_file_path(__file__)}:126 in \
135136
test_render_very_verbose_better_error_message
136137
simple.simple_exception()
137138
@@ -183,7 +184,7 @@ def test_render_can_ignore_given_files() -> None:
183184
trace.ignore_files_in(rf"^{re.escape(nested1.__file__)}$")
184185
trace.render(io)
185186

186-
lineno = 180
187+
lineno = 181
187188
expected = f"""
188189
Stack trace:
189190
@@ -221,7 +222,7 @@ def test_render_shows_ignored_files_if_in_debug_mode() -> None:
221222
trace.ignore_files_in(rf"^{re.escape(nested1.__file__)}$")
222223

223224
trace.render(io)
224-
lineno = 218
225+
lineno = 219
225226
expected = f"""
226227
Stack trace:
227228
@@ -344,7 +345,7 @@ def test_simple_render_aborts_if_no_message() -> None:
344345
trace = ExceptionTrace(e.value)
345346

346347
trace.render(io, simple=True)
347-
lineno = 342
348+
lineno = 343
348349

349350
expected = f"""
350351
AssertionError
@@ -364,3 +365,32 @@ def test_simple_render_aborts_if_no_message() -> None:
364365
{lineno + 4}│ trace.render(io, simple=True)
365366
"""
366367
assert expected == io.fetch_output()
368+
369+
370+
def test_render_with_suppressed_context() -> None:
371+
io = BufferedIO()
372+
lineno = 6
373+
374+
# Arrange: Trigger and catch the exception
375+
try:
376+
raiser_with_suppressed_context.raiser_with_suppressed_context()
377+
except Exception as e:
378+
trace = ExceptionTrace(e)
379+
380+
trace.render(io)
381+
382+
expected = f"""\
383+
384+
RuntimeError
385+
386+
This error should be displayed.
387+
388+
at tests/fixtures/exceptions/raiser_with_suppressed_context.py:{lineno} in raiser_with_suppressed_context
389+
{lineno - 4}│ def raiser_with_suppressed_context() -> None:
390+
{lineno - 3}│ try:
391+
{lineno - 2}│ raise ValueError("This error should be suppressed.")
392+
{lineno - 1}│ except ValueError:
393+
{lineno + 0}│ raise RuntimeError("This error should be displayed.") from None
394+
{lineno + 1}
395+
"""
396+
assert expected == io.fetch_output()

0 commit comments

Comments
 (0)