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

Fallback to text lexer when lexer not found #2133

Merged
merged 4 commits into from Apr 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -13,7 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Add missing `end` keyword argument to `Text.from_markup`
- Add missing `end` keyword argument to `Text.from_markup` https://github.com/Textualize/rich/pull/2095
- Fallback to text lexer when no lexer guessed https://github.com/Textualize/rich/pull/2133
- Fixed issue with decoding ANSI reset https://github.com/Textualize/rich/issues/2112

## [12.0.1] - 2022-03-22
Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions rich/traceback.py
Expand Up @@ -12,6 +12,7 @@
from pygments.token import Comment, Keyword, Name, Number, Operator, String
from pygments.token import Text as TextToken
from pygments.token import Token
from pygments.util import ClassNotFound

from . import pretty
from ._loop import loop_last
Expand Down Expand Up @@ -521,10 +522,10 @@ def _guess_lexer(cls, filename: str, code: str) -> str:
first_line = code[:new_line_index] if new_line_index != -1 else code
if first_line.startswith("#!") and "python" in first_line.lower():
return "python"
lexer_name = (
cls.LEXERS.get(ext) or guess_lexer_for_filename(filename, code).name
)
return lexer_name
try:
return cls.LEXERS.get(ext) or guess_lexer_for_filename(filename, code).name
except ClassNotFound:
return "text"

@group()
def _render_stack(self, stack: Stack) -> RenderResult:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_traceback.py
Expand Up @@ -237,6 +237,16 @@ def test_guess_lexer():
assert Traceback._guess_lexer("foo", "foo\nbnar") == "text"


def test_guess_lexer_yaml_j2():
# https://github.com/Textualize/rich/issues/2018
code = """\
foobar:
something: {{ raiser() }}
else: {{ 5 + 5 }}
"""
assert Traceback._guess_lexer("test.yaml.j2", code) == "text"


def test_recursive():
def foo(n):
return bar(n)
Expand Down