Skip to content

Commit 49840e3

Browse files
authored
Merge pull request #2225 from Textualize/bugfix-missing-whitespace-in-traceback
[traceback] Fix "missing whitespace" bug
2 parents a7e8a6b + 83af951 commit 49840e3

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Fixed recursion error in Jupyter progress bars https://github.com/Textualize/rich/issues/2047
2121
- Complex numbers are now identified by the highlighter https://github.com/Textualize/rich/issues/2214
2222
- 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
23+
- Fixed missing blank line in traceback rendering https://github.com/Textualize/rich/issues/2206
2324

2425
### Changed
2526

rich/traceback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ def render_locals(frame: Frame) -> Iterable[ConsoleRenderable]:
584584
)
585585
excluded = False
586586

587-
first = frame_index == 1
587+
first = frame_index == 0
588588
frame_filename = frame.filename
589589
suppressed = any(frame_filename.startswith(path) for path in self.suppress)
590590

tests/test_traceback.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import io
2+
import re
23
import sys
34

45
import pytest
@@ -21,17 +22,48 @@
2122
def test_handler():
2223
console = Console(file=io.StringIO(), width=100, color_system=None)
2324
expected_old_handler = sys.excepthook
25+
26+
def level1():
27+
level2()
28+
29+
def level2():
30+
return 1 / 0
31+
2432
try:
2533
old_handler = install(console=console)
2634
try:
27-
1 / 0
35+
level1()
2836
except Exception:
2937
exc_type, exc_value, traceback = sys.exc_info()
3038
sys.excepthook(exc_type, exc_value, traceback)
3139
rendered_exception = console.file.getvalue()
3240
print(repr(rendered_exception))
3341
assert "Traceback" in rendered_exception
3442
assert "ZeroDivisionError" in rendered_exception
43+
44+
frame_blank_line_possible_preambles = (
45+
# Start of the stack rendering:
46+
"╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮",
47+
# Each subsequent frame (starting with the file name) should then be preceded with a blank line:
48+
"│" + (" " * 98) + "│",
49+
)
50+
for frame_start in re.finditer(
51+
"^│ .+rich/tests/test_traceback\.py:",
52+
rendered_exception,
53+
flags=re.MULTILINE,
54+
):
55+
frame_start_index = frame_start.start()
56+
for preamble in frame_blank_line_possible_preambles:
57+
preamble_start, preamble_end = (
58+
frame_start_index - len(preamble) - 1,
59+
frame_start_index - 1,
60+
)
61+
if rendered_exception[preamble_start:preamble_end] == preamble:
62+
break
63+
else:
64+
pytest.fail(
65+
f"Frame {frame_start[0]} doesn't have the expected preamble"
66+
)
3567
finally:
3668
sys.excepthook = old_handler
3769
assert old_handler == expected_old_handler

0 commit comments

Comments
 (0)