Skip to content

Commit

Permalink
Don't crash in exception() if outside of an exception (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek authored Jul 29, 2023
1 parent 7c8366e commit 5338886
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/

- `FilteringBoundLogger.exception()` and `FilteringBoundLogger.aexception()` now support positional argument formatting like the rest of the methods.
[#531](https://github.com/hynek/structlog/issues/531)

- `structlog.processors.format_exc_info()` and `structlog.dev.ConsoleRenderer` do not crash anymore when told to format a non-existent exception.
[#533](https://github.com/hynek/structlog/issues/533)


## [23.1.0](https://github.com/hynek/structlog/compare/22.3.0...23.1.0) - 2023-04-06
Expand Down
3 changes: 3 additions & 0 deletions src/structlog/_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def _format_exception(exc_info: ExcInfo) -> str:
Shamelessly stolen from stdlib's logging module.
"""
if exc_info == (None, None, None): # type: ignore[comparison-overlap]
return "MISSING"

sio = StringIO()

traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], None, sio)
Expand Down
3 changes: 2 additions & 1 deletion src/structlog/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ def __call__( # noqa: PLR0912
if exc_info:
exc_info = _figure_out_exc_info(exc_info)

self._exception_formatter(sio, exc_info)
if exc_info != (None, None, None):
self._exception_formatter(sio, exc_info)
elif exc is not None:
if self._exception_formatter is not plain_traceback:
warnings.warn(
Expand Down
13 changes: 13 additions & 0 deletions tests/test_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,19 @@ def test_pickle(self, repr_native_str, force_colors, proto):
pickle.dumps(r, proto)
)(None, None, {"event": "foo"})

def test_no_exception(self):
"""
If there is no exception, don't blow up.
"""
r = dev.ConsoleRenderer(colors=False)

assert (
"hi"
== r(
None, None, {"event": "hi", "exc_info": (None, None, None)}
).rstrip()
)


class TestSetExcInfo:
def test_wrong_name(self):
Expand Down
12 changes: 11 additions & 1 deletion tests/test_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,17 @@ def test_format_exception(self):
except ValueError as e:
a = format_exc_info(None, None, {"exc_info": e})
b = ExceptionRenderer()(None, None, {"exc_info": e})
assert a == b

assert a == b

@pytest.mark.parametrize("ei", [True, (None, None, None)])
def test_no_exception(self, ei):
"""
A missing exception does not blow up.
"""
assert {"exception": "MISSING"} == format_exc_info(
None, None, {"exc_info": ei}
)


class TestUnicodeEncoder:
Expand Down

0 comments on commit 5338886

Please sign in to comment.