@@ -59,29 +59,40 @@ class Traceback:
5959 TREE_UTIL_LIB_DIRECTORY ,
6060 )
6161
62+ def extract (self ) -> Trace | None :
63+ """Extract a Rich trace using pytask's frame filtering and locals setting.
64+
65+ Return an existing Rich trace as is, or return None for a preformatted
66+ string or missing exception information.
67+ """
68+ exc_info = self .exc_info
69+ if isinstance (exc_info [1 ], Exit ):
70+ exc_info = remove_traceback_from_exc_info (exc_info )
71+
72+ exc_type , exc_value , traceback = exc_info
73+ if isinstance (traceback , Trace ):
74+ return traceback
75+ if isinstance (traceback , str ) or exc_type is None or exc_value is None :
76+ return None
77+
78+ _ , _ , filtered_traceback = _remove_internal_traceback_frames_from_exc_info (
79+ exc_info , suppress = self .suppress
80+ )
81+ assert filtered_traceback is None or isinstance (
82+ filtered_traceback , TracebackType
83+ )
84+ return RichTraceback .extract (
85+ exc_type , exc_value , filtered_traceback , show_locals = self .show_locals
86+ )
87+
6288 def __rich_console__ (
6389 self , console : Console , console_options : ConsoleOptions
6490 ) -> RenderResult :
65- if isinstance (self .exc_info [1 ], Exit ):
66- self .exc_info = remove_traceback_from_exc_info (self .exc_info )
67-
68- filtered_exc_info = _remove_internal_traceback_frames_from_exc_info (
69- self .exc_info , suppress = self .suppress
70- )
71-
72- if isinstance (filtered_exc_info [2 ], str ):
73- yield filtered_exc_info [2 ]
74- elif isinstance (filtered_exc_info [2 ], Trace ):
75- yield RichTraceback (
76- trace = filtered_exc_info [2 ], show_locals = self .show_locals
77- )
78- elif filtered_exc_info [0 ] is not None and filtered_exc_info [1 ] is not None :
79- yield RichTraceback .from_exception (
80- filtered_exc_info [0 ],
81- filtered_exc_info [1 ],
82- filtered_exc_info [2 ],
83- show_locals = self .show_locals ,
84- )
91+ _ , exc_value , traceback = self .exc_info
92+ if isinstance (traceback , str ) and not isinstance (exc_value , Exit ):
93+ yield traceback
94+ elif (trace := self .extract ()) is not None :
95+ yield RichTraceback (trace = trace , show_locals = self .show_locals )
8596
8697
8798def remove_traceback_from_exc_info (
0 commit comments