Skip to content

Commit 9e37bfa

Browse files
[3.13] gh-125593: Use colors to highlight error locations in tracebacks from exception group (GH-125681) (#126021)
gh-125593: Use colors to highlight error locations in tracebacks from exception group (GH-125681) (cherry picked from commit 51b012b) Co-authored-by: Bogdan Romanyuk <65823030+wrongnull@users.noreply.github.com>
1 parent e4204e8 commit 9e37bfa

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
@@ -4634,6 +4634,49 @@ def foo():
46344634
f'{boldm}ZeroDivisionError{reset}: {magenta}division by zero{reset}']
46354635
self.assertEqual(actual, expected)
46364636

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

46384681
if __name__ == "__main__":
46394682
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)