@@ -826,7 +826,7 @@ def __eq__(self, other):
826826 def __str__ (self ):
827827 return self ._str
828828
829- def format_exception_only (self ):
829+ def format_exception_only (self , * , show_group = False , _depth = 0 ):
830830 """Format the exception part of the traceback.
831831
832832 The return value is a generator of strings, each ending in a newline.
@@ -839,8 +839,10 @@ def format_exception_only(self):
839839 The message indicating which exception occurred is always the last
840840 string in the output.
841841 """
842+
843+ indent = 3 * _depth * ' '
842844 if self .exc_type is None :
843- yield _format_final_exc_line (None , self ._str )
845+ yield indent + _format_final_exc_line (None , self ._str )
844846 return
845847
846848 stype = self .exc_type .__qualname__
@@ -851,19 +853,23 @@ def format_exception_only(self):
851853 stype = smod + '.' + stype
852854
853855 if not issubclass (self .exc_type , SyntaxError ):
854- yield _format_final_exc_line (stype , self ._str )
856+ yield indent + _format_final_exc_line (stype , self ._str )
855857 else :
856- yield from self ._format_syntax_error (stype )
858+ yield from [ indent + l for l in self ._format_syntax_error (stype )]
857859
858860 if (
859861 isinstance (self .__notes__ , collections .abc .Sequence )
860862 and not isinstance (self .__notes__ , (str , bytes ))
861863 ):
862864 for note in self .__notes__ :
863865 note = _safe_string (note , 'note' )
864- yield from [l + '\n ' for l in note .split ('\n ' )]
866+ yield from [indent + l + '\n ' for l in note .split ('\n ' )]
865867 elif self .__notes__ is not None :
866- yield "{}\n " .format (_safe_string (self .__notes__ , '__notes__' , func = repr ))
868+ yield indent + "{}\n " .format (_safe_string (self .__notes__ , '__notes__' , func = repr ))
869+
870+ if self .exceptions and show_group :
871+ for ex in self .exceptions :
872+ yield from ex .format_exception_only (show_group = show_group , _depth = _depth + 1 )
867873
868874 def _format_syntax_error (self , stype ):
869875 """Format SyntaxError exceptions (internal helper)."""
0 commit comments