Skip to content

Commit

Permalink
[traceback] Fix "missing whitespace" bug
Browse files Browse the repository at this point in the history
It turns out that we intentionally don't add such a whitespace at the beginning of the trace rendering, but we were actually doing so for the 2nd item of the stack rather than the 1st one
  • Loading branch information
Olivier Philippon committed Apr 26, 2022
1 parent a7e8a6b commit 83af951
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
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

0 comments on commit 83af951

Please sign in to comment.