Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stackprinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def format(thing=None, **kwargs):
return fmt.format_stack_from_frame(thing, **kwargs)
elif isinstance(thing, Thread):
return format_thread(thing, **kwargs)
elif isinstance(thing, Exception):
elif isinstance(thing, BaseException):
exc_info = (thing.__class__, thing, thing.__traceback__)
return format(exc_info, **kwargs)
elif _is_exc_info(thing):
Expand Down
47 changes: 47 additions & 0 deletions tests/test_exception_instances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from io import StringIO

import pytest

import stackprinter


def _raise_exception(exc):
raise exc


@pytest.mark.parametrize('exception_type', [
BaseException, KeyboardInterrupt, SystemExit, GeneratorExit,
Exception, ValueError,
])
@pytest.mark.parametrize('with_traceback', [False, True])
@pytest.mark.parametrize('options', [
{}, {'style': 'darkbg', 'suppressed_exceptions': []},
])
def test_exception_instance_matches_exc_info(exception_type, with_traceback,
options):
exc = exception_type('message')
if with_traceback:
try:
_raise_exception(exc)
except BaseException:
pass

exc_info = (type(exc), exc, exc.__traceback__)
expected = stackprinter.format(exc_info, show_vals=None, **options)
assert exception_type.__name__ in expected
assert 'message' in expected
if with_traceback:
assert '_raise_exception' in expected

assert stackprinter.format(exc, show_vals=None, **options) == expected
output = StringIO()
stackprinter.show(exc, file=output, show_vals=None, **options)
assert output.getvalue() == expected + '\n'


@pytest.mark.parametrize('thing', [
42, 'message', object(), [], BaseException, KeyboardInterrupt, ValueError,
])
def test_format_rejects_non_exception_instances(thing):
with pytest.raises(ValueError, match='Expected an exception instance'):
stackprinter.format(thing)
Loading