Skip to content

[BUG] Rich.traceback cannot handle Exceptions with incorrect str() method #980

@aroberge

Description

@aroberge

Read the docs

This is not adressed in the documentation.

Describe the bug

When a custom exception that does not have a valid str() method is raised, a secondary exception is raised by Rich's excepthook. This is likely a very rare case which might not be worth considering. However, this issue was reported in my own project (aroberge/friendly#181) and I thought I should share it for your consideration.

To Reproduce

After installing rich.traceback, import a file containing the following code:

class C():
    def __str__(self):
        assert False

assert False, C() 

Using Python's 3.8 repl, here's the output:

>>> import ignore
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\andre\github\friendly-traceback\ignore.py", line 5, in <module>
    assert False, C()
AssertionError: <exception str() failed>

As you can see, Python seems to guard againts cases where str() fails. Using Rich, the following is generated

>>> from rich.traceback import install
>>> install()
<built-in function excepthook>
>>> import ignore
Error in sys.excepthook:
Traceback (most recent call last):
  File "C:\Users\andre\github\friendly-traceback\venv-friendly3.8\lib\site-packages\rich\traceback.py", line 78, in excepthook
    Traceback.from_exception(
  File "C:\Users\andre\github\friendly-traceback\venv-friendly3.8\lib\site-packages\rich\traceback.py", line 216, in from_exception
    rich_traceback = cls.extract(
  File "C:\Users\andre\github\friendly-traceback\venv-friendly3.8\lib\site-packages\rich\traceback.py", line 264, in extract
    exc_value=str(exc_value),
  File "C:\Users\andre\github\friendly-traceback\ignore.py", line 3, in __repr__
    assert False
AssertionError

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\andre\github\friendly-traceback\ignore.py", line 5, in <module>
    assert False, C()
AssertionError: <exception str() failed>

Platform

I'm convinced that the issue is independent of the platform and the terminal type. I'm using Windows and Windows Terminal.

Diagnose

I'm using rich version 9.10.0

> python -m rich.diagnose
╭────────────────────────── <class 'rich.console.Console'> ───────────────────────────╮
│ A high level console interface.                                                     │
│                                                                                     │
│ ╭─────────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=87 ColorSystem.TRUECOLOR>                                        │ │
│ ╰─────────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                     │
│     color_system = 'truecolor'                                                      │
│         encoding = 'utf-8'                                                          │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>    │
│ is_dumb_terminal = False                                                            │
│       is_jupyter = False                                                            │
│      is_terminal = True                                                             │
│   legacy_windows = False                                                            │
│         no_color = False                                                            │
│          options = ConsoleOptions(legacy_windows=False, min_width=1, max_width=87,  │
│                    is_terminal=True, encoding='utf-8', justify=None, overflow=None, │
│                    no_wrap=False, highlight=None)                                   │
│           record = False                                                            │
│         safe_box = True                                                             │
│             size = ConsoleDimensions(width=87, height=44)                           │
│        soft_wrap = False                                                            │
│           stderr = False                                                            │
│            style = None                                                             │
│         tab_size = 8                                                                │
│            width = 87                                                               │
╰─────────────────────────────────────────────────────────────────────────────────────╯

(venv-friendly3.8) C:\Users\andre\github\friendly-traceback
> python -m rich._windows
platform="Windows"
WindowsConsoleFeatures(vt=True, truecolor=True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions