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()
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))
+ note = colors["n"]
+ reset = colors["z"]
+ self.assertIn(note + "First note" + reset, lines)
+ self.assertIn(note + "Second note" + reset, lines)
+
def test_colorized_syntax_error(self):
try:
compile("a $ b", "<string>", "exec")
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',
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}, '
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}',
)
+def _format_note(note, indent, theme):
+ for l in note.split("\n"):
+ yield f"{indent}{theme.note}{l}{theme.reset}\n"
+
class _ExceptionPrintContext:
def __init__(self):
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:
):
for note in self.__notes__:
note = _safe_string(note, 'note')
- yield from [indent + l + '\n' for l in note.split('\n')]
+ yield from _format_note(note, indent, theme)
elif self.__notes__ is not None:
- yield indent + "{}\n".format(_safe_string(self.__notes__, '__notes__', func=repr))
+ note = _safe_string(self.__notes__, '__notes__', func=repr)
+ yield from _format_note(note, indent, theme)
if self.exceptions and show_group:
for ex in self.exceptions: