@@ -203,13 +203,19 @@ def format_exception_only(exc, /, value=_sentinel, *, show_group=False):
203
203
204
204
# -- not official API but folk probably use these two functions.
205
205
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 ):
207
207
valuestr = _safe_string (value ,'exception' )
208
208
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 .BOLD_MAGENTA } { etype } { _ANSIColors .RESET } { end_char } "
212
+ else :
213
+ line = f"{ _ANSIColors .BOLD_MAGENTA } { etype } { _ANSIColors .RESET } :{ _ANSIColors .MAGENTA } { valuestr } { _ANSIColors .RESET } { end_char } "
211
214
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 } "
213
219
return line
214
220
215
221
def _safe_string (value ,what ,func = str ):
@@ -440,6 +446,9 @@ def _get_code_position(code, instruction_index):
440
446
class _ANSIColors :
441
447
RED = '\x1b [31m'
442
448
BOLD_RED = '\x1b [1;31m'
449
+ MAGENTA = '\x1b [35m'
450
+ BOLD_MAGENTA = '\x1b [1;35m'
451
+ GREY = '\x1b [90m'
443
452
RESET = '\x1b [0m'
444
453
445
454
class StackSummary (list ):
@@ -543,8 +552,22 @@ def format_frame_summary(self, frame_summary, **kwargs):
543
552
filename = frame_summary .filename
544
553
if frame_summary .filename .startswith ("<stdin>-" ):
545
554
filename = "<stdin>"
546
- row .append (' File "{}", line {}, in {}\n ' .format (
547
- filename ,frame_summary .lineno ,frame_summary .name ))
555
+ if colorize :
556
+ row .append (' File {}"{}"{}, line {}{}{}, in {}{}{}\n ' .format (
557
+ _ANSIColors .MAGENTA ,
558
+ filename ,
559
+ _ANSIColors .RESET ,
560
+ _ANSIColors .MAGENTA ,
561
+ frame_summary .lineno ,
562
+ _ANSIColors .RESET ,
563
+ _ANSIColors .MAGENTA ,
564
+ frame_summary .name ,
565
+ _ANSIColors .RESET ,
566
+ )
567
+ )
568
+ else :
569
+ row .append (' File "{}", line {}, in {}\n ' .format (
570
+ filename ,frame_summary .lineno ,frame_summary .name ))
548
571
if frame_summary ._dedented_lines and frame_summary ._dedented_lines .strip ():
549
572
if (
550
573
frame_summary .colno is None or
@@ -1201,22 +1224,22 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
1201
1224
1202
1225
indent = 3 * _depth * ' '
1203
1226
if not self ._have_exc_type :
1204
- yield indent + _format_final_exc_line (None ,self ._str )
1227
+ yield indent + _format_final_exc_line (None ,self ._str , colorize = colorize )
1205
1228
return
1206
1229
1207
1230
stype = self .exc_type_str
1208
1231
if not self ._is_syntax_error :
1209
1232
if _depth > 0 :
1210
1233
# Nested exceptions needs correct handling of multiline messages.
1211
1234
formatted = _format_final_exc_line (
1212
- stype ,self ._str ,insert_final_newline = False ,
1235
+ stype ,self ._str ,insert_final_newline = False ,colorize = colorize
1213
1236
).split ('\n ' )
1214
1237
yield from [
1215
1238
indent + l + '\n '
1216
1239
for l in formatted
1217
1240
]
1218
1241
else :
1219
- yield _format_final_exc_line (stype ,self ._str )
1242
+ yield _format_final_exc_line (stype ,self ._str , colorize = colorize )
1220
1243
else :
1221
1244
yield from [indent + l for l in self ._format_syntax_error (stype ,colorize = colorize )]
1222
1245
@@ -1240,8 +1263,18 @@ def _format_syntax_error(self, stype, **kwargs):
1240
1263
colorize = kwargs .get ("colorize" ,False )
1241
1264
filename_suffix = ''
1242
1265
if self .lineno is not None :
1243
- yield ' File "{}", line {}\n ' .format (
1244
- self .filename or "<string>" ,self .lineno )
1266
+ if colorize :
1267
+ yield ' File {}"{}"{}, line {}{}{}\n ' .format (
1268
+ _ANSIColors .MAGENTA ,
1269
+ self .filename or "<string>" ,
1270
+ _ANSIColors .RESET ,
1271
+ _ANSIColors .MAGENTA ,
1272
+ self .lineno ,
1273
+ _ANSIColors .RESET ,
1274
+ )
1275
+ else :
1276
+ yield ' File "{}", line {}\n ' .format (
1277
+ self .filename or "<string>" ,self .lineno )
1245
1278
elif self .filename is not None :
1246
1279
filename_suffix = ' ({})' .format (self .filename )
1247
1280