@@ -108,18 +108,20 @@ def format_stack_from_frame(fr, add_summary=False, **kwargs):
108108
109109
110110def format_exc_info (etype , evalue , tb , style = 'plaintext' , add_summary = 'auto' ,
111- reverse = False , ** kwargs ):
111+ reverse = False , suppressed_exceptions = [KeyboardInterrupt ],
112+ ** kwargs ):
112113 """
113114 Format an exception traceback, including the exception message
114115
115- keyword args like stackprinter.format()
116+ see stackprinter.format() for docs about the keyword arguments
116117 """
117118 msg = ''
118119 try :
119- # First, recursively format any chained exceptions (exceptions during whose handling
120- # the given one happened).
121- # TODO: refactor this whole messy function to return a more... structured datastructure
122- # before assembling a string, so that e.g. a summary of the whole chain can be shown at
120+ # First, recursively format any chained exceptions (exceptions
121+ # during whose handling the given one happened).
122+ # TODO: refactor this whole messy function to return a
123+ # more... structured datastructure before assembling a string,
124+ # so that e.g. a summary of the whole chain can be shown at
123125 # the end.
124126 context = getattr (evalue , '__context__' , None )
125127 cause = getattr (evalue , '__cause__' , None )
@@ -152,30 +154,38 @@ def format_exc_info(etype, evalue, tb, style='plaintext', add_summary='auto',
152154 msg += clr % chain_hint
153155
154156 # Now, actually do some formatting:
155- msgs = []
157+ parts = []
156158 if tb :
157159 frameinfos = [ex .get_info (tb_ ) for tb_ in _walk_traceback (tb )]
158- stack_msg = format_stack (frameinfos , style = style , reverse = reverse , ** kwargs )
159- msgs .append (stack_msg )
160+ if (suppressed_exceptions and
161+ issubclass (etype , tuple (suppressed_exceptions ))):
162+ summary = format_summary (frameinfos , style = style ,
163+ reverse = reverse , ** kwargs )
164+ parts = [summary ]
165+ else :
166+ whole_stack = format_stack (frameinfos , style = style ,
167+ reverse = reverse , ** kwargs )
168+ parts .append (whole_stack )
160169
161- if add_summary == 'auto' :
162- add_summary = stack_msg .count ('\n ' ) > 50
170+ if add_summary == 'auto' :
171+ add_summary = whole_stack .count ('\n ' ) > 50
163172
164- if add_summary :
165- summ = format_summary (frameinfos , style = style , reverse = reverse , ** kwargs )
166- summ += '\n '
167- msgs .append ('---- (full traceback below) ----\n \n ' if reverse else
168- '---- (full traceback above) ----\n ' )
169- msgs .append (summ )
173+ if add_summary :
174+ summary = format_summary (frameinfos , style = style ,
175+ reverse = reverse , ** kwargs )
176+ summary += '\n '
177+ parts .append ('---- (full traceback below) ----\n \n ' if reverse else
178+ '---- (full traceback above) ----\n ' )
179+ parts .append (summary )
170180
171181 exc = format_exception_message (etype , evalue , style = style )
172- msgs .append ('\n \n ' if reverse else '' )
173- msgs .append (exc )
182+ parts .append ('\n \n ' if reverse else '' )
183+ parts .append (exc )
174184
175185 if reverse :
176- msgs = reversed (msgs )
186+ parts = reversed (parts )
177187
178- msg += '' .join (msgs )
188+ msg += '' .join (parts )
179189
180190 except Exception as exc :
181191 import os
0 commit comments