Skip to content

Commit 949da49

Browse files
committed
Add more colors
1 parent 9ae4c77 commit 949da49

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

Lib/test/test_traceback.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4300,18 +4300,19 @@ def foo():
43004300

43014301
red = traceback._ANSIColors.RED
43024302
boldr = traceback._ANSIColors.BOLD_RED
4303+
magenta = traceback._ANSIColors.MAGENTA
43034304
reset = traceback._ANSIColors.RESET
43044305
lno_foo = foo.__code__.co_firstlineno
43054306
expected = ['Traceback (most recent call last):',
4306-
f' File "{__file__}", '
4307-
f'line {lno_foo+5}, in test_colorized_traceback_is_the_default',
4307+
f' File {magenta}"{__file__}"{reset}, '
4308+
f'line {magenta}{lno_foo+5}{reset}, in test_colorized_traceback_is_the_default',
43084309
f' {red}foo{reset+boldr}(){reset}',
43094310
f' {red}~~~{reset+boldr}^^{reset}',
4310-
f' File "{__file__}", '
4311-
f'line {lno_foo+1}, in foo',
4311+
f' File {magenta}"{__file__}"{reset}, '
4312+
f'line {magenta}{lno_foo+1}{reset}, in foo',
43124313
f' {red}1{reset+boldr}/{reset+red}0{reset}',
43134314
f' {red}~{reset+boldr}^{reset+red}~{reset}',
4314-
'ZeroDivisionError: division by zero']
4315+
f'{red}ZeroDivisionError{reset}: division by zero']
43154316
self.assertEqual(actual, expected)
43164317

43174318
def test_colorized_detection_checks_for_environment_variables(self):

Lib/traceback.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,19 @@ def format_exception_only(exc, /, value=_sentinel, *, show_group=False):
203203

204204
# -- not official API but folk probably use these two functions.
205205

206-
def _format_final_exc_line(etype, value, *, insert_final_newline=True):
206+
def _format_final_exc_line(etype, value, *, insert_final_newline=True, colorize=False):
207207
valuestr = _safe_string(value, 'exception')
208208
end_char = "\n" if insert_final_newline else ""
209-
if value is None or not valuestr:
210-
line = f"{etype}{end_char}"
209+
if colorize:
210+
if value is None or not valuestr:
211+
line = f"{_ANSIColors.RED}{etype}{_ANSIColors.RESET}{end_char}"
212+
else:
213+
line = f"{_ANSIColors.RED}{etype}{_ANSIColors.RESET}: {valuestr}{end_char}"
211214
else:
212-
line = f"{etype}: {valuestr}{end_char}"
215+
if value is None or not valuestr:
216+
line = f"{etype}{end_char}"
217+
else:
218+
line = f"{etype}: {valuestr}{end_char}"
213219
return line
214220

215221
def _safe_string(value, what, func=str):
@@ -440,6 +446,8 @@ def _get_code_position(code, instruction_index):
440446
class _ANSIColors:
441447
RED = '\x1b[31m'
442448
BOLD_RED = '\x1b[1;31m'
449+
MAGENTA = '\x1b[35m'
450+
GREY = '\x1b[90m'
443451
RESET = '\x1b[0m'
444452

445453
class StackSummary(list):
@@ -543,8 +551,20 @@ def format_frame_summary(self, frame_summary, **kwargs):
543551
filename = frame_summary.filename
544552
if frame_summary.filename.startswith("<stdin>-"):
545553
filename = "<stdin>"
546-
row.append(' File "{}", line {}, in {}\n'.format(
547-
filename, frame_summary.lineno, frame_summary.name))
554+
if colorize:
555+
row.append(' File {}"{}"{}, line {}{}{}, in {}\n'.format(
556+
_ANSIColors.MAGENTA,
557+
filename,
558+
_ANSIColors.RESET,
559+
_ANSIColors.MAGENTA,
560+
frame_summary.lineno,
561+
_ANSIColors.RESET,
562+
frame_summary.name,
563+
)
564+
)
565+
else:
566+
row.append(' File "{}", line {}, in {}\n'.format(
567+
filename, frame_summary.lineno, frame_summary.name))
548568
if frame_summary._dedented_lines and frame_summary._dedented_lines.strip():
549569
if (
550570
frame_summary.colno is None or
@@ -1201,22 +1221,22 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
12011221

12021222
indent = 3 * _depth * ' '
12031223
if not self._have_exc_type:
1204-
yield indent + _format_final_exc_line(None, self._str)
1224+
yield indent + _format_final_exc_line(None, self._str, colorize=colorize)
12051225
return
12061226

12071227
stype = self.exc_type_str
12081228
if not self._is_syntax_error:
12091229
if _depth > 0:
12101230
# Nested exceptions needs correct handling of multiline messages.
12111231
formatted = _format_final_exc_line(
1212-
stype, self._str, insert_final_newline=False,
1232+
stype, self._str, insert_final_newline=False, colorize=colorize
12131233
).split('\n')
12141234
yield from [
12151235
indent + l + '\n'
12161236
for l in formatted
12171237
]
12181238
else:
1219-
yield _format_final_exc_line(stype, self._str)
1239+
yield _format_final_exc_line(stype, self._str, colorize=colorize)
12201240
else:
12211241
yield from [indent + l for l in self._format_syntax_error(stype, colorize=colorize)]
12221242

@@ -1240,8 +1260,18 @@ def _format_syntax_error(self, stype, **kwargs):
12401260
colorize = kwargs.get("colorize", False)
12411261
filename_suffix = ''
12421262
if self.lineno is not None:
1243-
yield ' File "{}", line {}\n'.format(
1244-
self.filename or "<string>", self.lineno)
1263+
if colorize:
1264+
yield ' File {}"{}"{}, line {}{}{}\n'.format(
1265+
_ANSIColors.MAGENTA,
1266+
self.filename or "<string>",
1267+
_ANSIColors.RESET,
1268+
_ANSIColors.MAGENTA,
1269+
self.lineno,
1270+
_ANSIColors.RESET,
1271+
)
1272+
else:
1273+
yield ' File "{}", line {}\n'.format(
1274+
self.filename or "<string>", self.lineno)
12451275
elif self.filename is not None:
12461276
filename_suffix = ' ({})'.format(self.filename)
12471277

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy