@@ -133,6 +133,7 @@ def __init__(
133133 max_length = 128 ,
134134 encoding = "ascii" ,
135135 extend = False ,
136+ skip_frame = None ,
136137 ):
137138 self ._colorize = colorize
138139 self ._show_values = show_values
@@ -141,6 +142,7 @@ def __init__(
141142 self ._syntax_highlighter = SyntaxHighlighter (style )
142143 self ._max_length = max_length
143144 self ._encoding = encoding
145+ self ._skip_frame = skip_frame
144146 self ._lib_dirs = self ._get_lib_dirs ()
145147 self ._pipe_char = self ._get_char ("\u2502 " , "|" )
146148 self ._cap_char = self ._get_char ("\u2514 " , "->" )
@@ -150,35 +152,36 @@ def __init__(
150152 r"(?P<end>\n[\s\S]*)"
151153 )
152154
153- def extend_traceback (self , tb , * , decorated = False ):
155+ def improve_traceback (self , tb ):
154156 if tb is None :
155157 return None
156158
157- frame = tb .tb_frame
159+ root = tb
160+ tbs = [traceback (tb .tb_frame , tb .tb_lasti , tb .tb_lineno , tb .tb_next )]
158161
159- if decorated :
160- bad_frame = (tb .tb_frame .f_code .co_filename , tb .tb_frame .f_lineno )
161- tb = tb .tb_next
162- caught = False
163- else :
164- bad_frame = None
165- tb = traceback (tb .tb_frame , tb .tb_lasti , tb .tb_lineno , tb .tb_next , caught_here = True )
166- caught = True
162+ if self ._extend :
163+ frame = tb .tb_frame .f_back
167164
168- while True :
169- frame = frame .f_back
165+ while frame :
166+ tbs .insert (0 , traceback (frame , frame .f_lasti , frame .f_lineno , tb ))
167+ frame = frame .f_back
170168
171- if not frame :
172- break
169+ for tb in reversed (tbs ):
170+ if (tb .tb_frame .f_code .co_filename , tb .tb_frame .f_code .co_name ) != self ._skip_frame :
171+ tb .caught_here = True
172+ break
173173
174- if (frame .f_code .co_filename , frame .f_lineno ) == bad_frame :
175- continue
174+ tb = root .tb_next
176175
177- if not caught :
178- caught = True
179- tb = traceback (frame , frame .f_lasti , frame .f_lineno , tb , caught_here = True )
180- else :
181- tb = traceback (frame , frame .f_lasti , frame .f_lineno , tb )
176+ while tb :
177+ tbs .append (traceback (tb .tb_frame , tb .tb_lasti , tb .tb_lineno , tb .tb_next ))
178+ tb = tb .tb_next
179+
180+ tb = None
181+
182+ for t in reversed (tbs ):
183+ if (t .tb_frame .f_code .co_filename , t .tb_frame .f_code .co_name ) != self ._skip_frame :
184+ tb = traceback (t .tb_frame , t .tb_lasti , t .tb_lineno , tb , caught_here = t .caught_here )
182185
183186 return tb
184187
@@ -463,9 +466,13 @@ def _format_exception(self, value, tb, seen=None):
463466 if self ._colorize and len (exception_only ) >= 3 and issubclass (exc_type , SyntaxError ):
464467 match = re .match (self ._location_regex , exception_only [0 ])
465468 group = match .groupdict ()
466- exception_only [ 0 ] = " \n " + self ._colorize_location (
469+ colorized_location = self ._colorize_location (
467470 group ["file" ], group ["line" ], group ["function" ], group ["end" ]
468471 )
472+ if self ._show_values :
473+ exception_only [0 ] = "\n " + colorized_location
474+ else :
475+ exception_only [0 ] = colorized_location
469476 exception_only [1 ] = self ._syntax_highlighter .highlight (exception_only [1 ])
470477
471478 error_message = exception_only [- 1 ]
@@ -481,7 +488,7 @@ def _format_exception(self, value, tb, seen=None):
481488 error_message = self ._theme ["exception_type" ].format (error_message )
482489 error_message += "\n "
483490
484- if formatted_frames and self ._show_values :
491+ if self ._show_values and formatted_frames :
485492 error_message = "\n " + error_message
486493
487494 exception_only [- 1 ] = error_message
@@ -491,7 +498,6 @@ def _format_exception(self, value, tb, seen=None):
491498
492499 yield "" .join (lines )
493500
494- def format_exception (self , type_ , value , tb , * , decorated = False ):
495- if self ._extend :
496- tb = self .extend_traceback (tb , decorated = decorated )
501+ def format_exception (self , type_ , value , tb ):
502+ tb = self .improve_traceback (tb )
497503 yield from self ._format_exception (value , tb )
0 commit comments