Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
48.13k stars 1.69k forks source link

[BUG] Rich tracebacks raise another exception when suppressing sys #3277

Open juarezr opened 4 months ago

juarezr commented 4 months ago

[BUG] Rich tracebacks raise another exception when suppressing sys

Describe the bug

I'm not entirely sure if this is a bug, but it got through testing as exception handling is hard to figure out because one tends to focus on checking the correct program behavior.

When the sys module is added to the tracebacks_suppres option of RichHandler, an error occurs inside the call of the `log.exception()' method:

During handling of the above exception, another exception occurred: ... AttributeError: module 'sys' has no attribute 'file'. Did you mean: 'name'?

Checklist

How to Reproduce

Minimal reproducible source code example: ```python #!/usr/bin/env python3 import logging import sys from rich.logging import RichHandler rh1 = RichHandler( rich_tracebacks=True, log_time_format="RichHandler #1 [%x %X]", ) rh2 = RichHandler( rich_tracebacks=True, tracebacks_suppress=[logging], log_time_format="RichHandler #2 [%x %X]", ) rh3 = RichHandler( rich_tracebacks=True, tracebacks_suppress=[sys], ## Fails log_time_format="RichHandler #3 [%x %X]", ) logging.basicConfig(format="%(message)s", handlers=[rh1, rh2, rh3]) log = logging.getLogger("rich") try: print(1 / 0) except Exception: log.exception("Unable to divide") ``` Error Output: ```bash ❯ ./reproduce.py | cat RichHandler #1 [02/09/24 15:00:26] ERROR Unable to divide reproduce.py:32 ╭─── Traceback (most recent call last) ────╮ │ /home/userx/src/reproduce.py:30 in │ │ │ │ │ │ 27 log = logging.getLogger("rich") │ │ 28 │ │ 29 try: │ │ ❱ 30 │ print(1 / 0) │ │ 31 except Exception: │ │ 32 │ log.exception("Unable to divide │ │ 33 │ ╰──────────────────────────────────────────╯ ZeroDivisionError: division by zero RichHandler #2 [02/09/24 15:00:26] ERROR Unable to divide reproduce.py:32 ╭─── Traceback (most recent call last) ────╮ │ /home/userx/src/reproduce.py:30 in │ │ │ │ │ │ 27 log = logging.getLogger("rich") │ │ 28 │ │ 29 try: │ │ ❱ 30 │ print(1 / 0) │ │ 31 except Exception: │ │ 32 │ log.exception("Unable to divide │ │ 33 │ ╰──────────────────────────────────────────╯ ZeroDivisionError: division by zero Traceback (most recent call last): File "/home/userx/src/reproduce.py", line 30, in print(1 / 0) ~~^~~ ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/userx/src/reproduce.py", line 32, in log.exception("Unable to divide") File "/usr/lib/python3.11/logging/__init__.py", line 1524, in exception self.error(msg, *args, exc_info=exc_info, **kwargs) File "/usr/lib/python3.11/logging/__init__.py", line 1518, in error self._log(ERROR, msg, args, **kwargs) File "/usr/lib/python3.11/logging/__init__.py", line 1634, in _log self.handle(record) File "/usr/lib/python3.11/logging/__init__.py", line 1644, in handle self.callHandlers(record) File "/usr/lib/python3.11/logging/__init__.py", line 1706, in callHandlers hdlr.handle(record) File "/usr/lib/python3.11/logging/__init__.py", line 978, in handle self.emit(record) File "/usr/lib/python3/dist-packages/rich/logging.py", line 138, in emit traceback = Traceback.from_exception( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3/dist-packages/rich/traceback.py", line 345, in from_exception return cls( ^^^^ File "/usr/lib/python3/dist-packages/rich/traceback.py", line 283, in __init__ suppress_entity.__file__ is not None ^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: module 'sys' has no attribute '__file__'. Did you mean: '__name__'? ```

Platform

System Information: ```bash ❯ python3 --version Python 3.11.7 ❯ uname -a Linux ~~~~~~~~~~ 6.5.0-17-generic #17-Ubuntu SMP PREEMPT_DYNAMIC Thu Jan 11 14:01:59 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux ❯ pip freeze | grep rich rich==13.3.1 ``` Diagnostics: ```bash ❯ python3 -m rich.diagnose | cat ╭───────────────────────── ─────────────────────────╮ │ A high level console interface. │ │ │ │ ╭──────────────────────────────────────────────────────────────────────────────╮ │ │ │ │ │ │ ╰──────────────────────────────────────────────────────────────────────────────╯ │ │ │ │ color_system = None │ │ encoding = 'utf-8' │ │ file = <_io.TextIOWrapper name='' mode='w' encoding='utf-8'> │ │ height = 91 │ │ is_alt_screen = False │ │ is_dumb_terminal = False │ │ is_interactive = False │ │ is_jupyter = False │ │ is_terminal = False │ │ legacy_windows = False │ │ no_color = False │ │ options = ConsoleOptions( │ │ size=ConsoleDimensions(width=104, height=91), │ │ legacy_windows=False, │ │ min_width=1, │ │ max_width=104, │ │ is_terminal=False, │ │ encoding='utf-8', │ │ max_height=91, │ │ justify=None, │ │ overflow=None, │ │ no_wrap=False, │ │ highlight=None, │ │ markup=None, │ │ height=None │ │ ) │ │ quiet = False │ │ record = False │ │ safe_box = True │ │ size = ConsoleDimensions(width=104, height=91) │ │ soft_wrap = False │ │ stderr = False │ │ style = None │ │ tab_size = 8 │ │ width = 104 │ ╰──────────────────────────────────────────────────────────────────────────────────╯ ╭─── ────╮ │ Windows features available. │ │ │ │ ╭───────────────────────────────────────────────────╮ │ │ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │ │ ╰───────────────────────────────────────────────────╯ │ │ │ │ truecolor = False │ │ vt = False │ ╰───────────────────────────────────────────────────────╯ ╭────── Environment Variables ───────╮ │ { │ │ 'TERM': 'xterm-256color', │ │ 'COLORTERM': 'truecolor', │ │ 'CLICOLOR': None, │ │ 'NO_COLOR': None, │ │ 'TERM_PROGRAM': 'vscode', │ │ 'COLUMNS': None, │ │ 'LINES': None, │ │ 'JUPYTER_COLUMNS': None, │ │ 'JUPYTER_LINES': None, │ │ 'JPY_PARENT_PID': None, │ │ 'VSCODE_VERBOSE_LOGGING': None │ │ } │ ╰────────────────────────────────────╯ platform="Linux" ```
github-actions[bot] commented 4 months ago

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory