Skip to content

Commit

Permalink
Make sure locals_max_{length,string} work as expected in case they ar…
Browse files Browse the repository at this point in the history
…e None
  • Loading branch information
Sh4pe committed Nov 13, 2024
1 parent 51641b7 commit d124c51
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/structlog/tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,10 @@ def __init__(
max_frames: int = MAX_FRAMES,
use_rich: bool = True,
) -> None:
if locals_max_length < 0:
if locals_max_length is not None and locals_max_length < 0:
msg = f'"locals_max_length" must be >= 0: {locals_max_length}'
raise ValueError(msg)
if locals_max_string < 0:
if locals_max_string is not None and locals_max_string < 0:
msg = f'"locals_max_string" must be >= 0: {locals_max_string}'
raise ValueError(msg)
if max_frames < 2:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,38 @@ def bar(n):
]


@pytest.mark.parametrize(
"kwargs, local_variable",
[
(
{"locals_max_string": None},
"x" * (tracebacks.LOCALS_MAX_STRING + 10),
),
(
{"locals_max_length": None},
list(range(tracebacks.LOCALS_MAX_LENGTH + 10)),
),
],
)
def test_exception_dict_transformer_locals_max_accept_none_argument(
kwargs, local_variable
):
try:
my_local = local_variable
1 / 0
except Exception as e:
format_json = tracebacks.ExceptionDictTransformer(
show_locals=True, **kwargs
)
result = format_json((type(e), e, e.__traceback__))

assert len(result) == 1
assert len(result[0]["frames"]) == 1

frame = result[0]["frames"][0]
assert frame["locals"]["my_local"].strip("'") == str(local_variable)


def test_json_traceback():
"""
Tracebacks are formatted to JSON with all information.
Expand Down

0 comments on commit d124c51

Please sign in to comment.