Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Traceback.from_exception ignores locals_max_length and locals_max_string #2650

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [13.0.0] - Unreleased

### Fixed

- Handle passing `locals_max_length` and `locals_max_string` to `Traceback.from_exception` https://github.com/Textualize/rich/pull/2650

### Changed

- Bumped minimum Python version to 3.7 https://github.com/Textualize/rich/pull/2567
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The following people have contributed to the development of Rich:
<!-- Add your name below, sort alphabetically by surname. Link to GitHub profile / your home page. -->

- [Patrick Arminio](https://github.com/patrick91)
- [Gilad Barnea](https://github.com/giladbarnea)
- [Gregory Beauregard](https://github.com/GBeauregard/pyffstream)
- [Dennis Brakhane](https://github.com/brakhane)
- [Darren Burns](https://github.com/darrenburns)
Expand Down
7 changes: 6 additions & 1 deletion rich/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@ def from_exception(
Traceback: A Traceback instance that may be printed.
"""
rich_traceback = cls.extract(
exc_type, exc_value, traceback, show_locals=show_locals
exc_type,
exc_value,
traceback,
show_locals=show_locals,
locals_max_length=locals_max_length,
locals_max_string=locals_max_string,
)
return cls(
rich_traceback,
Expand Down
21 changes: 21 additions & 0 deletions tests/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,27 @@ def level3():
assert frame_names == expected_frame_names


def test_rich_traceback_from_exception_respects_locals_max():
console = Console(width=160, file=io.StringIO())
try:
local_string = "O" * 145
1 / 0
except Exception as e:
traceback = Traceback.from_exception(
e.__class__,
e,
e.__traceback__,
width=console.width,
show_locals=True,
locals_max_length=console.width,
locals_max_string=console.width,
)
console.print(traceback)
result = console.file.getvalue()
print(result)
assert "O'+" not in result


if __name__ == "__main__": # pragma: no cover

expected = render(get_exception())
Expand Down