Skip to content

Commit

Permalink
Merge branch 'main' into add-render-to-log-args-and-kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek authored Jan 4, 2025
2 parents 21cb2fd + 8ff0f8a commit 497a997
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.1
rev: v0.8.5
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/

[#677](https://github.com/hynek/structlog/pull/677)

- `structlog.tracebacks.ExceptionDictTransformer` now actually accepts `None` for `locals_max_length` and `locals_max_string`.

[#675](https://github.com/hynek/structlog/pull/675)


## [24.4.0](https://github.com/hynek/structlog/compare/24.3.0...24.4.0) - 2024-07-17

Expand Down
8 changes: 6 additions & 2 deletions src/structlog/tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ class ExceptionDictTransformer:
.. versionchanged:: 24.3.0
Added *locals_max_length*, *locals_hide_sunder*, *locals_hide_dunder*,
*suppress* and *use_rich* arguments.
.. versionchanged:: 25.1.0
*locals_max_length* and *locals_max_string* may be None to disable
truncation.
"""

def __init__(
Expand All @@ -382,10 +386,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
40 changes: 40 additions & 0 deletions tests/test_tracebacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,46 @@ 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
):
"""
ExceptionDictTransformer works when either locals_max_string or
locals_max_length is set to None.
"""

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.
Expand Down

0 comments on commit 497a997

Please sign in to comment.