https://github.com/python/cpython/commit/51b012b2a8093c92ef2c06884f9719657f9b17f7
commit: 51b012b2a8093c92ef2c06884f9719657f9b17f7
branch: main
author: Bogdan Romanyuk <[email protected]>
committer: pablogsal <[email protected]>
date: 2024-10-27T01:57:10+01:00
summary:

gh-125593: Use colors to highlight error locations in tracebacks from exception 
group (#125681)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2024-10-18-10-11-43.gh-issue-125593.Q97m3A.rst
M Lib/test/test_traceback.py
M Lib/traceback.py

diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 77ef0c5b3c480d..ec69412f5511eb 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -4637,6 +4637,49 @@ def foo():
             f'{boldm}ZeroDivisionError{reset}: {magenta}division by 
zero{reset}']
         self.assertEqual(actual, expected)
 
+    def test_colorized_traceback_from_exception_group(self):
+        def foo():
+            exceptions = []
+            try:
+                1 / 0
+            except ZeroDivisionError as inner_exc:
+                exceptions.append(inner_exc)
+            raise ExceptionGroup("test", exceptions)
+
+        try:
+            foo()
+        except Exception as e:
+            exc = traceback.TracebackException.from_exception(
+                e, capture_locals=True
+            )
+
+        red = _colorize.ANSIColors.RED
+        boldr = _colorize.ANSIColors.BOLD_RED
+        magenta = _colorize.ANSIColors.MAGENTA
+        boldm = _colorize.ANSIColors.BOLD_MAGENTA
+        reset = _colorize.ANSIColors.RESET
+        lno_foo = foo.__code__.co_firstlineno
+        actual = "".join(exc.format(colorize=True)).splitlines()
+        expected = [f"  + Exception Group Traceback (most recent call last):",
+                   f'  |   File {magenta}"{__file__}"{reset}, line 
{magenta}{lno_foo+9}{reset}, in 
{magenta}test_colorized_traceback_from_exception_group{reset}',
+                   f'  |     {red}foo{reset}{boldr}(){reset}',
+                   f'  |     {red}~~~{reset}{boldr}^^{reset}',
+                   f"  |     e = ExceptionGroup('test', 
[ZeroDivisionError('division by zero')])",
+                   f"  |     foo = {foo}",
+                   f'  |     self = <{__name__}.TestColorizedTraceback 
testMethod=test_colorized_traceback_from_exception_group>',
+                   f'  |   File {magenta}"{__file__}"{reset}, line 
{magenta}{lno_foo+6}{reset}, in {magenta}foo{reset}',
+                   f'  |     raise ExceptionGroup("test", exceptions)',
+                   f"  |     exceptions = [ZeroDivisionError('division by 
zero')]",
+                   f'  | {boldm}ExceptionGroup{reset}: {magenta}test (1 
sub-exception){reset}',
+                   f'  +-+---------------- 1 ----------------',
+                   f'    | Traceback (most recent call last):',
+                   f'    |   File {magenta}"{__file__}"{reset}, line 
{magenta}{lno_foo+3}{reset}, in {magenta}foo{reset}',
+                   f'    |     {red}1 {reset}{boldr}/{reset}{red} 0{reset}',
+                   f'    |     {red}~~{reset}{boldr}^{reset}{red}~~{reset}',
+                   f"    |     exceptions = [ZeroDivisionError('division by 
zero')]",
+                   f'    | {boldm}ZeroDivisionError{reset}: {magenta}division 
by zero{reset}',
+                   f'    +------------------------------------']
+        self.assertEqual(actual, expected)
 
 if __name__ == "__main__":
     unittest.main()
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 0fe7187a0c6193..f73149271b9bc9 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -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 ''
diff --git a/Misc/NEWS.d/next/Core and 
Builtins/2024-10-18-10-11-43.gh-issue-125593.Q97m3A.rst b/Misc/NEWS.d/next/Core 
and Builtins/2024-10-18-10-11-43.gh-issue-125593.Q97m3A.rst
new file mode 100644
index 00000000000000..220e94467af849
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2024-10-18-10-11-43.gh-issue-125593.Q97m3A.rst 
@@ -0,0 +1 @@
+Use color to highlight error locations in traceback from exception group

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to