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