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

[traceback] Fix "missing whitespace" bug #2225

Merged
merged 1 commit into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed recursion error in Jupyter progress bars https://github.com/Textualize/rich/issues/2047
- Complex numbers are now identified by the highlighter https://github.com/Textualize/rich/issues/2214
- Fix crash on IDLE and forced is_terminal detection to False because IDLE can't do escape codes https://github.com/Textualize/rich/issues/2222
- Fixed missing blank line in traceback rendering https://github.com/Textualize/rich/issues/2206

### Changed

Expand Down
2 changes: 1 addition & 1 deletion rich/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ def render_locals(frame: Frame) -> Iterable[ConsoleRenderable]:
)
excluded = False

first = frame_index == 1
first = frame_index == 0
frame_filename = frame.filename
suppressed = any(frame_filename.startswith(path) for path in self.suppress)

Expand Down
34 changes: 33 additions & 1 deletion tests/test_traceback.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import re
import sys

import pytest
Expand All @@ -21,17 +22,48 @@
def test_handler():
console = Console(file=io.StringIO(), width=100, color_system=None)
expected_old_handler = sys.excepthook

def level1():
level2()

def level2():
return 1 / 0

try:
old_handler = install(console=console)
try:
1 / 0
level1()
except Exception:
exc_type, exc_value, traceback = sys.exc_info()
sys.excepthook(exc_type, exc_value, traceback)
rendered_exception = console.file.getvalue()
print(repr(rendered_exception))
assert "Traceback" in rendered_exception
assert "ZeroDivisionError" in rendered_exception

frame_blank_line_possible_preambles = (
# Start of the stack rendering:
"╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮",
# Each subsequent frame (starting with the file name) should then be preceded with a blank line:
"│" + (" " * 98) + "│",
)
for frame_start in re.finditer(
"^│ .+rich/tests/test_traceback\.py:",
rendered_exception,
flags=re.MULTILINE,
):
frame_start_index = frame_start.start()
for preamble in frame_blank_line_possible_preambles:
preamble_start, preamble_end = (
frame_start_index - len(preamble) - 1,
frame_start_index - 1,
)
if rendered_exception[preamble_start:preamble_end] == preamble:
break
else:
pytest.fail(
f"Frame {frame_start[0]} doesn't have the expected preamble"
)
finally:
sys.excepthook = old_handler
assert old_handler == expected_old_handler
Expand Down