Skip to content

Commit

Permalink
Merge branch 'master' into issue-2688/nf-escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan authored Mar 4, 2023
2 parents 541686e + fa09487 commit 24ce364
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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
- Filter ANSI character-encoding-change codes in `Text.from_ansi` parser
- Fixes traceback failing when a frame filename is unreadable https://github.com/Textualize/rich/issues/2821

### Added

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ include = ["rich/py.typed"]
[tool.poetry.dependencies]
python = ">=3.7.0"
typing-extensions = { version = ">=4.0.0, <5.0", python = "<3.9" }
pygments = "^2.14.0"
pygments = "^2.13.0"
ipywidgets = { version = ">=7.5.1,<9", optional = true }
markdown-it-py = "^2.2.0"

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
6 changes: 6 additions & 0 deletions tests/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import tempfile

import pkg_resources
import pytest
from pygments.lexers import PythonLexer

Expand All @@ -20,6 +21,9 @@

from .render import render

PYGMENTS_VERSION = pkg_resources.get_distribution("pygments").version
OLD_PYGMENTS = PYGMENTS_VERSION == "2.13.0"

CODE = '''\
def loop_first_last(values: Iterable[T]) -> Iterable[Tuple[bool, bool, T]]:
"""Iterate and generate a tuple with a flag for first and last value."""
Expand Down Expand Up @@ -98,6 +102,7 @@ def test_python_render_simple_passing_lexer_instance():
assert rendered_syntax == expected


@pytest.mark.skipif(OLD_PYGMENTS, reason="Pygments changed their tokenizer")
def test_python_render_simple_indent_guides():
syntax = Syntax(
CODE,
Expand All @@ -114,6 +119,7 @@ def test_python_render_simple_indent_guides():
assert rendered_syntax == expected


@pytest.mark.skipif(OLD_PYGMENTS, reason="Pygments changed their tokenizer")
def test_python_render_line_range_indent_guides():
syntax = Syntax(
CODE,
Expand Down

0 comments on commit 24ce364

Please sign in to comment.