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 tracebacks raise another exception when suppressing sys #3277

Open
2 tasks done
juarezr opened this issue Feb 9, 2024 · 1 comment
Open
2 tasks done

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

juarezr opened this issue Feb 9, 2024 · 1 comment

Comments

@juarezr
Copy link

juarezr commented Feb 9, 2024

[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:

#!/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:

❯ ./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       │
                                            │ <module>                                 │
                                            │                                          │
                                            │   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       │
                                            │ <module>                                 │
                                            │                                          │
                                            │   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 <module>
    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 <module>
    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:

❯ 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:

❯ python3 -m rich.diagnose | cat
╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=104 None>                                                     │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = None                                                          │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' 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                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ 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"
Copy link

github-actions bot commented Feb 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant