@@ -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 .RED } { etype } { _ANSIColors .RESET } { end_char } "
212
+ else :
213
+ line = f"{ _ANSIColors .RED } { etype } { _ANSIColors .RESET } : { valuestr } { 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,8 @@ 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
+ GREY = '\x1b [90m'
443
451
RESET = '\x1b [0m'
444
452
445
453
class StackSummary (list ):
@@ -543,8 +551,20 @@ def format_frame_summary(self, frame_summary, **kwargs):
543
551
filename = frame_summary .filename
544
552
if frame_summary .filename .startswith ("<stdin>-" ):
545
553
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 ))
548
568
if frame_summary ._dedented_lines and frame_summary ._dedented_lines .strip ():
549
569
if (
550
570
frame_summary .colno is None or
@@ -1201,22 +1221,22 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
1201
1221
1202
1222
indent = 3 * _depth * ' '
1203
1223
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 )
1205
1225
return
1206
1226
1207
1227
stype = self .exc_type_str
1208
1228
if not self ._is_syntax_error :
1209
1229
if _depth > 0 :
1210
1230
# Nested exceptions needs correct handling of multiline messages.
1211
1231
formatted = _format_final_exc_line (
1212
- stype , self ._str , insert_final_newline = False ,
1232
+ stype , self ._str , insert_final_newline = False , colorize = colorize
1213
1233
).split ('\n ' )
1214
1234
yield from [
1215
1235
indent + l + '\n '
1216
1236
for l in formatted
1217
1237
]
1218
1238
else :
1219
- yield _format_final_exc_line (stype , self ._str )
1239
+ yield _format_final_exc_line (stype , self ._str , colorize = colorize )
1220
1240
else :
1221
1241
yield from [indent + l for l in self ._format_syntax_error (stype , colorize = colorize )]
1222
1242
@@ -1240,8 +1260,18 @@ def _format_syntax_error(self, stype, **kwargs):
1240
1260
colorize = kwargs .get ("colorize" , False )
1241
1261
filename_suffix = ''
1242
1262
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 )
1245
1275
elif self .filename is not None :
1246
1276
filename_suffix = ' ({})' .format (self .filename )
1247
1277
0 commit comments