@@ -254,8 +254,7 @@ def _apply_soft_wrapping(
254
254
columns_in_buffer += width
255
255
current_line .append ((style , c ))
256
256
257
- if len (current_line ) > 0 :
258
- yield current_line
257
+ yield current_line
259
258
260
259
def _print_paginated_formatted_text (
261
260
self , lines : Iterable [StyleAndTextTuples ]
@@ -323,14 +322,20 @@ def show_pager() -> None:
323
322
def _format_exception_output (
324
323
self , e : BaseException , highlight : bool
325
324
) -> Generator [OneStyleAndTextTuple , None , None ]:
326
- # Instead of just calling ``traceback.format_exc``, we take the
327
- # traceback and skip the bottom calls of this framework.
328
- t , v , tb = sys .exc_info ()
329
-
330
- # Required for pdb.post_mortem() to work.
331
- sys .last_type , sys .last_value , sys .last_traceback = t , v , tb
332
-
333
- tblist = list (traceback .extract_tb (tb ))
325
+ if e .__cause__ :
326
+ yield from self ._format_exception_output (e .__cause__ , highlight = highlight )
327
+ yield (
328
+ "" ,
329
+ "\n The above exception was the direct cause of the following exception:\n \n " ,
330
+ )
331
+ elif e .__context__ :
332
+ yield from self ._format_exception_output (e .__context__ , highlight = highlight )
333
+ yield (
334
+ "" ,
335
+ "\n During handling of the above exception, another exception occurred:\n \n " ,
336
+ )
337
+
338
+ tblist = list (traceback .extract_tb (e .__traceback__ ))
334
339
335
340
for line_nr , tb_tuple in enumerate (tblist ):
336
341
if tb_tuple [0 ] == "<stdin>" :
@@ -340,7 +345,7 @@ def _format_exception_output(
340
345
tb_list = traceback .format_list (tblist )
341
346
if tb_list :
342
347
tb_list .insert (0 , "Traceback (most recent call last):\n " )
343
- tb_list .extend (traceback .format_exception_only (t , v ))
348
+ tb_list .extend (traceback .format_exception_only (type ( e ), e ))
344
349
345
350
tb_str = "" .join (tb_list )
346
351
0 commit comments