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

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

Closed
aroberge opened this issue Feb 1, 2021 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@aroberge
Copy link

aroberge commented Feb 1, 2021

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)

@willmcgugan
Copy link
Collaborator

It probably is worth addressing. I'd want Rich to be at least as robust as standard tracebacks.

@willmcgugan willmcgugan added bug Something isn't working and removed Needs triage labels Feb 1, 2021
@willmcgugan
Copy link
Collaborator

Fixed in 9.11.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants