Skip to content

Commit 51b012b

Browse files
authored
gh-125593: Use colors to highlight error locations in tracebacks from exception group (#125681)
1 parent f6cc7c8 commit 51b012b

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

Lib/test/test_traceback.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4637,6 +4637,49 @@ def foo():
46374637
f'{boldm}ZeroDivisionError{reset}: {magenta}division by zero{reset}']
46384638
self.assertEqual(actual, expected)
46394639

4640+
def test_colorized_traceback_from_exception_group(self):
4641+
def foo():
4642+
exceptions = []
4643+
try:
4644+
1 / 0
4645+
except ZeroDivisionError as inner_exc:
4646+
exceptions.append(inner_exc)
4647+
raise ExceptionGroup("test", exceptions)
4648+
4649+
try:
4650+
foo()
4651+
except Exception as e:
4652+
exc = traceback.TracebackException.from_exception(
4653+
e, capture_locals=True
4654+
)
4655+
4656+
red = _colorize.ANSIColors.RED
4657+
boldr = _colorize.ANSIColors.BOLD_RED
4658+
magenta = _colorize.ANSIColors.MAGENTA
4659+
boldm = _colorize.ANSIColors.BOLD_MAGENTA
4660+
reset = _colorize.ANSIColors.RESET
4661+
lno_foo = foo.__code__.co_firstlineno
4662+
actual = "".join(exc.format(colorize=True)).splitlines()
4663+
expected = [f" + Exception Group Traceback (most recent call last):",
4664+
f' | File {magenta}"{__file__}"{reset}, line {magenta}{lno_foo+9}{reset}, in {magenta}test_colorized_traceback_from_exception_group{reset}',
4665+
f' | {red}foo{reset}{boldr}(){reset}',
4666+
f' | {red}~~~{reset}{boldr}^^{reset}',
4667+
f" | e = ExceptionGroup('test', [ZeroDivisionError('division by zero')])",
4668+
f" | foo = {foo}",
4669+
f' | self = <{__name__}.TestColorizedTraceback testMethod=test_colorized_traceback_from_exception_group>',
4670+
f' | File {magenta}"{__file__}"{reset}, line {magenta}{lno_foo+6}{reset}, in {magenta}foo{reset}',
4671+
f' | raise ExceptionGroup("test", exceptions)',
4672+
f" | exceptions = [ZeroDivisionError('division by zero')]",
4673+
f' | {boldm}ExceptionGroup{reset}: {magenta}test (1 sub-exception){reset}',
4674+
f' +-+---------------- 1 ----------------',
4675+
f' | Traceback (most recent call last):',
4676+
f' | File {magenta}"{__file__}"{reset}, line {magenta}{lno_foo+3}{reset}, in {magenta}foo{reset}',
4677+
f' | {red}1 {reset}{boldr}/{reset}{red} 0{reset}',
4678+
f' | {red}~~{reset}{boldr}^{reset}{red}~~{reset}',
4679+
f" | exceptions = [ZeroDivisionError('division by zero')]",
4680+
f' | {boldm}ZeroDivisionError{reset}: {magenta}division by zero{reset}',
4681+
f' +------------------------------------']
4682+
self.assertEqual(actual, expected)
46404683

46414684
if __name__ == "__main__":
46424685
unittest.main()

Lib/traceback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ def format(self, *, chain=True, _ctx=None, **kwargs):
14281428
f'+---------------- {title} ----------------\n')
14291429
_ctx.exception_group_depth += 1
14301430
if not truncated:
1431-
yield from exc.exceptions[i].format(chain=chain, _ctx=_ctx)
1431+
yield from exc.exceptions[i].format(chain=chain, _ctx=_ctx, colorize=colorize)
14321432
else:
14331433
remaining = num_excs - self.max_group_width
14341434
plural = 's' if remaining > 1 else ''
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use color to highlight error locations in traceback from exception group

0 commit comments

Comments
 (0)