Skip to content

Commit

Permalink
Merge pull request #2850 from Textualize/empty-traceback-frames
Browse files Browse the repository at this point in the history
Empty traceback frames
  • Loading branch information
willmcgugan committed Mar 4, 2023
2 parents 89cd92f + 3469674 commit e897cd3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use `Console(stderr=True)` in `rich.traceback.install` to support io redirection.
- Fixes superfluous spaces in html output https://github.com/Textualize/rich/issues/2832
- Fixed duplicate output in Jupyter https://github.com/Textualize/rich/pulls/2804
- Fixes traceback failing when a frame filename is unreadable https://github.com/Textualize/rich/issues/2821

### Added

Expand Down
7 changes: 6 additions & 1 deletion rich/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,10 @@ def tokens_to_spans() -> Iterable[Tuple[str, Optional[Style]]]:

# Skip over tokens until line start
while line_no < _line_start:
_token_type, token = next(tokens)
try:
_token_type, token = next(tokens)
except StopIteration:
break
yield (token, None)
if token.endswith("\n"):
line_no += 1
Expand Down Expand Up @@ -671,6 +674,8 @@ def _get_syntax(
line_offset = max(0, start_line - 1)
lines: Union[List[Text], Lines] = text.split("\n", allow_blank=ends_on_nl)
if self.line_range:
if line_offset > len(lines):
return
lines = lines[line_offset:end_line]

if self.indent_guides and not options.ascii_only:
Expand Down
6 changes: 1 addition & 5 deletions rich/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ class Span(NamedTuple):
"""Style associated with the span."""

def __repr__(self) -> str:
return (
f"Span({self.start}, {self.end}, {self.style!r})"
if (isinstance(self.style, Style) and self.style._meta)
else f"Span({self.start}, {self.end}, {self.style!r})"
)
return f"Span({self.start}, {self.end}, {self.style!r})"

def __bool__(self) -> bool:
return self.end > self.start
Expand Down
13 changes: 12 additions & 1 deletion rich/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ def from_exception(
locals_hide_dunder=locals_hide_dunder,
locals_hide_sunder=locals_hide_sunder,
)

return cls(
rich_traceback,
width=width,
Expand Down Expand Up @@ -663,7 +664,13 @@ def render_locals(frame: Frame) -> Iterable[ConsoleRenderable]:
style="pygments.text",
)
else:
text = Text.assemble("in ", (frame.name, "pygments.function"))
text = Text.assemble(
"in ",
(frame.name, "pygments.function"),
(":", "pygments.text"),
(str(frame.lineno), "pygments.number"),
style="pygments.text",
)
if not frame.filename.startswith("<") and not first:
yield ""
yield text
Expand All @@ -673,6 +680,10 @@ def render_locals(frame: Frame) -> Iterable[ConsoleRenderable]:
if not suppressed:
try:
code = read_code(frame.filename)
if not code:
# code may be an empty string if the file doesn't exist, OR
# if the traceback filename is generated dynamically
continue
lexer_name = self._guess_lexer(frame.filename, code)
syntax = Syntax(
code,
Expand Down

0 comments on commit e897cd3

Please sign in to comment.