Skip to content

Commit 31ac9ce

Browse files
authored
Merge pull request #31 from cknd/add_etype_filter
Add option to disable full tracebacks for certain exceptions
2 parents bbbb380 + dd3ed34 commit 31ac9ce

File tree

4 files changed

+66
-21
lines changed

4 files changed

+66
-21
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Upcoming
2+
3+
## Added
4+
- Option to disable verbose formatting for given types of exceptions (generating a standard python-like traceback instead)
5+
6+
## Changed
7+
- Disabled verbose formatting for KeyboardInterrupts by default. Call `format(..., suppressed_exceptions=None`) to enforce verbose printing even on a keyboard interrupt.
8+
9+
# 0.2.3 - May 29, 2019
10+
11+
(beginning of time)

demo_keyboard_interrupt.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import time
2+
import stackprinter
3+
4+
5+
def blocking_function(x):
6+
print('ctrl-C me')
7+
time.sleep(5)
8+
9+
try:
10+
some_value = 'spam'
11+
blocking_function(some_value)
12+
except:
13+
# Default: Only print summary info
14+
stackprinter.show()
15+
# Override:
16+
# stackprinter.show(suppressed_exceptions=None)
17+
# stackprinter.show(suppressed_exceptions=[])
18+
19+

stackprinter/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ def format(thing=None, **kwargs):
117117
Example: To hide numpy internals from the traceback, set
118118
`suppressed_paths=[r"lib/python.*/site-packages/numpy"]`
119119
120+
suppressed_exception_types: list of exception classes
121+
Show less verbose formatting for exceptions in this list.
122+
By default, this list is `[KeyboardInterrupt]`. Set to `[]`
123+
to force verbose formatting even on a keyboard interrupt.
124+
120125
reverse: bool
121126
List the innermost frame first.
122127

stackprinter/formatting.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,20 @@ def format_stack_from_frame(fr, add_summary=False, **kwargs):
108108

109109

110110
def 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

Comments
 (0)