Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Lib/_colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class Syntax(ThemeSection):
class Traceback(ThemeSection):
type: str = ANSIColors.BOLD_MAGENTA
message: str = ANSIColors.MAGENTA
note: str = ANSIColors.MAGENTA
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
filename: str = ANSIColors.MAGENTA
line_no: str = ANSIColors.MAGENTA
frame: str = ANSIColors.MAGENTA
Expand Down
25 changes: 21 additions & 4 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
test_frame = namedtuple('frame', ['f_code', 'f_globals', 'f_locals'])
test_tb = namedtuple('tb', ['tb_frame', 'tb_lineno', 'tb_next', 'tb_lasti'])

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

def test_colorized_exception_notes(self):
def foo():
raise ValueError()

try:
foo()
except Exception as e:
e.add_note("First note")
e.add_note("Second note")
exc = traceback.TracebackException.from_exception(e)

lines = "".join(exc.format(colorize=True))
magenta = colors["m"]
reset = colors["z"]
self.assertIn(magenta + "First note" + reset, lines)
self.assertIn(magenta + "Second note" + reset, lines)

def test_colorized_syntax_error(self):
try:
compile("a $ b", "<string>", "exec")
Expand All @@ -5093,7 +5110,7 @@ def test_colorized_syntax_error(self):
e, capture_locals=True
)
actual = "".join(exc.format(colorize=True))
def expected(t, m, fn, l, f, E, e, z):
def expected(t, m, fn, l, f, E, e, z, n):
return "".join(
[
f' File {fn}"<string>"{z}, line {l}1{z}\n',
Expand All @@ -5119,7 +5136,7 @@ def foo():
actual = tbstderr.getvalue().splitlines()

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

lno_foo = foo.__code__.co_firstlineno
actual = "".join(exc.format(colorize=True)).splitlines()
def expected(t, m, fn, l, f, E, e, z):
def expected(t, m, fn, l, f, E, e, z, n):
return [
f" + Exception Group Traceback (most recent call last):",
f' | File {fn}"{__file__}"{z}, line {l}{lno_foo+9}{z}, in {f}test_colorized_traceback_from_exception_group{z}',
Expand Down
8 changes: 6 additions & 2 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,10 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
well, recursively, with indentation relative to their nesting depth.
"""
colorize = kwargs.get("colorize", False)
if colorize:
theme = _colorize.get_theme(force_color=True).traceback
else:
theme = _colorize.get_theme(force_no_color=True).traceback

indent = 3 * _depth * ' '
if not self._have_exc_type:
Expand Down Expand Up @@ -1281,9 +1285,9 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
):
for note in self.__notes__:
note = _safe_string(note, 'note')
yield from [indent + l + '\n' for l in note.split('\n')]
yield from [indent + theme.note + l + theme.reset + '\n' for l in note.split('\n')]
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated
elif self.__notes__ is not None:
yield indent + "{}\n".format(_safe_string(self.__notes__, '__notes__', func=repr))
yield indent + theme.note + "{}".format(_safe_string(self.__notes__, '__notes__', func=repr)) + theme.reset + "\n"
Comment thread
StanFromIreland marked this conversation as resolved.
Outdated

if self.exceptions and show_group:
for ex in self.exceptions:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:func:`traceback.format_exception_only` now colorizes exception notes.
Loading