diff --git a/poetry.lock b/poetry.lock index ffcad1b9..457d1012 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1002,7 +1002,7 @@ python-versions = ">=3.6" [[package]] name = "virtualenv" -version = "20.13.4" +version = "20.14.0" description = "Virtual Python Environment builder" category = "dev" optional = false @@ -1786,8 +1786,8 @@ typing-extensions = [ {file = "typing_extensions-4.1.1.tar.gz", hash = "sha256:1a9462dcc3347a79b1f1c0271fbe79e844580bb598bafa1ed208b94da3cdcd42"}, ] virtualenv = [ - {file = "virtualenv-20.13.4-py2.py3-none-any.whl", hash = "sha256:c3e01300fb8495bc00ed70741f5271fc95fed067eb7106297be73d30879af60c"}, - {file = "virtualenv-20.13.4.tar.gz", hash = "sha256:ce8901d3bbf3b90393498187f2d56797a8a452fb2d0d7efc6fd837554d6f679c"}, + {file = "virtualenv-20.14.0-py2.py3-none-any.whl", hash = "sha256:1e8588f35e8b42c6ec6841a13c5e88239de1e6e4e4cedfd3916b306dc826ec66"}, + {file = "virtualenv-20.14.0.tar.gz", hash = "sha256:8e5b402037287126e81ccde9432b95a8be5b19d36584f64957060a3488c11ca8"}, ] wcwidth = [ {file = "wcwidth-0.2.5-py2.py3-none-any.whl", hash = "sha256:beb4802a9cebb9144e99086eff703a642a13d6a0052920003a230f3294bbe784"}, diff --git a/rich/traceback.py b/rich/traceback.py index 0d764b7c..8f092c66 100644 --- a/rich/traceback.py +++ b/rich/traceback.py @@ -12,6 +12,7 @@ from pygments.lexers import guess_lexer_for_filename 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 @@ -521,10 +522,10 @@ class Traceback: 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: diff --git a/tests/test_traceback.py b/tests/test_traceback.py index 31658c5a..e6b4de3f 100644 --- a/tests/test_traceback.py +++ b/tests/test_traceback.py @@ -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)