From cab20c74a17d35876a13ee0cae4700c9f9c84a98 Mon Sep 17 00:00:00 2001 From: David Nies Date: Thu, 14 Nov 2024 00:09:04 +0100 Subject: [PATCH] Make sure locals_max_{length,string} work as expected in case they are None --- src/structlog/tracebacks.py | 4 ++-- tests/test_tracebacks.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/structlog/tracebacks.py b/src/structlog/tracebacks.py index 2392b342..8fada4da 100644 --- a/src/structlog/tracebacks.py +++ b/src/structlog/tracebacks.py @@ -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: diff --git a/tests/test_tracebacks.py b/tests/test_tracebacks.py index a086a042..273bc475 100644 --- a/tests/test_tracebacks.py +++ b/tests/test_tracebacks.py @@ -531,6 +531,40 @@ 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__)) + + _ = my_local + + 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.