@@ -250,30 +250,98 @@ def print_exception():
250250    sys .last_exc  =  val 
251251    seen  =  set ()
252252
253-     def  print_exc (typ , exc , tb ):
253+     def  print_exc (typ , exc , tb ,  prefix = "" ):
254254        seen .add (id (exc ))
255255        context  =  exc .__context__ 
256256        cause  =  exc .__cause__ 
257+         exclude  =  ("run.py" , "rpc.py" , "threading.py" , "queue.py" ,
258+                    "debugger_r.py" , "bdb.py" )
257259        if  cause  is  not   None  and  id (cause ) not  in   seen :
258-             print_exc (type (cause ), cause , cause .__traceback__ )
259-             print ("\n The above exception was the direct cause " 
260-                   "of the following exception:\n " , file = efile )
260+             print_exc (type (cause ), cause , cause .__traceback__ , prefix )
261+             if  prefix :
262+                 print (f"{ prefix }  |\n { prefix }  | The above exception was the direct cause " 
263+                       f"of the following exception:\n { prefix }  |" , file = efile )
264+             else :
265+                 print ("\n The above exception was the direct cause " 
266+                       "of the following exception:\n " , file = efile )
261267        elif  (context  is  not   None  and 
262268              not  exc .__suppress_context__  and 
263269              id (context ) not  in   seen ):
264-             print_exc (type (context ), context , context .__traceback__ )
265-             print ("\n During handling of the above exception, " 
266-                   "another exception occurred:\n " , file = efile )
267-         if  tb :
268-             tbe  =  traceback .extract_tb (tb )
269-             print ('Traceback (most recent call last):' , file = efile )
270-             exclude  =  ("run.py" , "rpc.py" , "threading.py" , "queue.py" ,
271-                        "debugger_r.py" , "bdb.py" )
272-             cleanup_traceback (tbe , exclude )
273-             traceback .print_list (tbe , file = efile )
274-         lines  =  get_message_lines (typ , exc , tb )
275-         for  line  in  lines :
276-             print (line , end = '' , file = efile )
270+             print_exc (type (context ), context , context .__traceback__ , prefix )
271+             if  prefix :
272+                 print (f"{ prefix }  |\n { prefix }  | During handling of the above exception, " 
273+                       f"another exception occurred:\n { prefix }  |" , file = efile )
274+             else :
275+                 print ("\n During handling of the above exception, " 
276+                       "another exception occurred:\n " , file = efile )
277+         if  isinstance (exc , BaseExceptionGroup ):
278+             if  tb :
279+                 if  not  prefix :
280+                     print ("  + Exception Group Traceback (most recent call last):" , file = efile )
281+                 else :
282+                     print (f"{ prefix }  | Exception Group Traceback (most recent call last):" , file = efile )
283+                 tbe  =  traceback .extract_tb (tb )
284+                 cleanup_traceback (tbe , exclude )
285+                 for  line  in  traceback .format_list (tbe ):
286+                     for  subline  in  line .rstrip ().splitlines ():
287+                         if  not  prefix :
288+                             print (f"  | { subline }  " , file = efile )
289+                         else :
290+                             print (f"{ prefix }  | { subline }  " , file = efile )
291+             lines  =  get_message_lines (typ , exc , tb )
292+             for  line  in  lines :
293+                 if  not  prefix :
294+                     print (f"  | { line }  " , end = "" , file = efile )
295+                 else :
296+                     print (f"{ prefix }  | { line }  " , end = "" , file = efile )
297+ 
298+             for  i , sub  in  enumerate (exc .exceptions , 1 ):
299+                 if  i  ==  1 :
300+                     first_line_pre  =  "+-" 
301+                 else :
302+                     first_line_pre  =  "  " 
303+                 if  not  prefix :
304+                     print (f"  { first_line_pre }  +---------------- { i }   ----------------" , file = efile )
305+                 else :
306+                     print (f"{ prefix } { first_line_pre }  +---------------- { i }   ----------------" , file = efile )
307+                 if  id (sub ) not  in   seen :
308+                     if  not  prefix :
309+                         print_exc (type (sub ), sub , sub .__traceback__ , "    " )
310+                     else :
311+                         print_exc (type (sub ), sub , sub .__traceback__ , prefix  +  "  " )
312+                     need_print_underline  =  not  isinstance (sub , BaseExceptionGroup )
313+                 else :
314+                     if  not  prefix :
315+                         print ("f    | <exception {type(sub).__name__} has printed>" )
316+                     else :
317+                         print (f"{ prefix }    | <exception { type (sub ).__name__ }   has printed>" )
318+                     need_print_underline  =  True 
319+                 if  need_print_underline :
320+                     if  not  prefix :
321+                         print ("    +------------------------------------" , file = efile )
322+                     else :
323+                         print (f"  { prefix }  +------------------------------------" , file = efile )
324+ 
325+         else :
326+             if  tb :
327+                 if  prefix :
328+                     print (f"{ prefix }  | Traceback (most recent call last):" , file = efile )
329+                 else :
330+                     print ("Traceback (most recent call last):" , file = efile )
331+                 tbe  =  traceback .extract_tb (tb )
332+                 cleanup_traceback (tbe , exclude )
333+                 if  prefix :
334+                     for  line  in  traceback .format_list (tbe ):
335+                         for  subline  in  line .rstrip ().splitlines ():
336+                             print (f"{ prefix }  | { subline }  " , file = efile )
337+                 else :
338+                     traceback .print_list (tbe , file = efile )
339+             lines  =  get_message_lines (typ , exc , tb )
340+             for  line  in  lines :
341+                 if  prefix :
342+                     print (f"{ prefix }  | { line }  " , end = "" , file = efile )
343+                 else :
344+                     print (line , end = '' , file = efile )
277345
278346    print_exc (typ , val , tb )
279347
0 commit comments