Skip to content

Commit e5bbeea

Browse files
Commit
1 parent 52996aa commit e5bbeea

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

Lib/_colorize.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class Syntax(ThemeSection):
202202
class Traceback(ThemeSection):
203203
type: str = ANSIColors.BOLD_MAGENTA
204204
message: str = ANSIColors.MAGENTA
205+
note: str = ANSIColors.MAGENTA
205206
filename: str = ANSIColors.MAGENTA
206207
line_no: str = ANSIColors.MAGENTA
207208
frame: str = ANSIColors.MAGENTA

Lib/test/test_traceback.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
test_frame = namedtuple('frame', ['f_code', 'f_globals', 'f_locals'])
3838
test_tb = namedtuple('tb', ['tb_frame', 'tb_lineno', 'tb_next', 'tb_lasti'])
3939

40-
color_overrides = {"reset": "z", "filename": "fn", "error_highlight": "E"}
40+
color_overrides = {"reset": "z", "filename": "fn", "error_highlight": "E", "note": "n"}
4141
colors = {
4242
color_overrides.get(k, k[0].lower()): v
4343
for k, v in _colorize.default_theme.traceback.items()
@@ -5085,6 +5085,23 @@ def bar():
50855085
self.assertIn("return baz1(1,\n 2,3\n ,4)", lines)
50865086
self.assertIn(red + "bar" + reset + boldr + "()" + reset, lines)
50875087

5088+
def test_colorized_exception_notes(self):
5089+
def foo():
5090+
raise ValueError()
5091+
5092+
try:
5093+
foo()
5094+
except Exception as e:
5095+
e.add_note("First note")
5096+
e.add_note("Second note")
5097+
exc = traceback.TracebackException.from_exception(e)
5098+
5099+
lines = "".join(exc.format(colorize=True))
5100+
magenta = colors["m"]
5101+
reset = colors["z"]
5102+
self.assertIn(magenta + "First note" + reset, lines)
5103+
self.assertIn(magenta + "Second note" + reset, lines)
5104+
50885105
def test_colorized_syntax_error(self):
50895106
try:
50905107
compile("a $ b", "<string>", "exec")
@@ -5093,7 +5110,7 @@ def test_colorized_syntax_error(self):
50935110
e, capture_locals=True
50945111
)
50955112
actual = "".join(exc.format(colorize=True))
5096-
def expected(t, m, fn, l, f, E, e, z):
5113+
def expected(t, m, fn, l, f, E, e, z, n):
50975114
return "".join(
50985115
[
50995116
f' File {fn}"<string>"{z}, line {l}1{z}\n',
@@ -5119,7 +5136,7 @@ def foo():
51195136
actual = tbstderr.getvalue().splitlines()
51205137

51215138
lno_foo = foo.__code__.co_firstlineno
5122-
def expected(t, m, fn, l, f, E, e, z):
5139+
def expected(t, m, fn, l, f, E, e, z, n):
51235140
return [
51245141
'Traceback (most recent call last):',
51255142
f' File {fn}"{__file__}"{z}, '
@@ -5152,7 +5169,7 @@ def foo():
51525169

51535170
lno_foo = foo.__code__.co_firstlineno
51545171
actual = "".join(exc.format(colorize=True)).splitlines()
5155-
def expected(t, m, fn, l, f, E, e, z):
5172+
def expected(t, m, fn, l, f, E, e, z, n):
51565173
return [
51575174
f" + Exception Group Traceback (most recent call last):",
51585175
f' | File {fn}"{__file__}"{z}, line {l}{lno_foo+9}{z}, in {f}test_colorized_traceback_from_exception_group{z}',

Lib/traceback.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,10 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
12531253
well, recursively, with indentation relative to their nesting depth.
12541254
"""
12551255
colorize = kwargs.get("colorize", False)
1256+
if colorize:
1257+
theme = _colorize.get_theme(force_color=True).traceback
1258+
else:
1259+
theme = _colorize.get_theme(force_no_color=True).traceback
12561260

12571261
indent = 3 * _depth * ' '
12581262
if not self._have_exc_type:
@@ -1281,9 +1285,9 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
12811285
):
12821286
for note in self.__notes__:
12831287
note = _safe_string(note, 'note')
1284-
yield from [indent + l + '\n' for l in note.split('\n')]
1288+
yield from [indent + theme.note + l + theme.reset + '\n' for l in note.split('\n')]
12851289
elif self.__notes__ is not None:
1286-
yield indent + "{}\n".format(_safe_string(self.__notes__, '__notes__', func=repr))
1290+
yield indent + theme.note + "{}".format(_safe_string(self.__notes__, '__notes__', func=repr)) + theme.reset + "\n"
12871291

12881292
if self.exceptions and show_group:
12891293
for ex in self.exceptions:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:func:`traceback.format_exception_only` now colorizes exception notes.

0 commit comments

Comments
 (0)