Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4637,6 +4637,27 @@ def foo():
f'{boldm}ZeroDivisionError{reset}: {magenta}division by zero{reset}']
self.assertEqual(actual, expected)

def test_colorized_traceback_from_exception_group(self):
try:
exceptions = []
try:
1 / 0
except ZeroDivisionError as inner_exc:
exceptions.append(inner_exc)
raise ExceptionGroup("test", exceptions)
except Exception as e:
exc = traceback.TracebackException.from_exception(
e, capture_locals=True
)

actual = "".join(exc.format(colorize=True))
red = _colorize.ANSIColors.RED
boldr = _colorize.ANSIColors.BOLD_RED
reset = _colorize.ANSIColors.RESET

self.assertIn(f"{red}1 {reset+boldr}/{reset+red} 0{reset}", actual)
self.assertIn(f"{red}~~{reset+boldr}^{reset+red}~~{reset}", actual)
Copy link
Member

@Eclips4 Eclips4 Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to test for the presence of the magenta color, and, more importantly, we need to ensure that the exception group has been colored and that their sub-exception have also been colored.



if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ def format(self, *, chain=True, _ctx=None, **kwargs):
f'+---------------- {title} ----------------\n')
_ctx.exception_group_depth += 1
if not truncated:
yield from exc.exceptions[i].format(chain=chain, _ctx=_ctx)
yield from exc.exceptions[i].format(chain=chain, _ctx=_ctx, colorize=colorize)
else:
remaining = num_excs - self.max_group_width
plural = 's' if remaining > 1 else ''
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use color to highlight error locations in traceback from exception group
Loading