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] Overwriting locals_max_length and locals_max_string does not affect printed Traceback #3301

Open
2 tasks done
CollinHeist opened this issue Mar 9, 2024 · 1 comment

Comments

@CollinHeist
Copy link

CollinHeist commented Mar 9, 2024

Describe the bug

Initializing a Traceback object with a non-default locals_max_length or locals_max_string does not affected the locals when printed. MRE below:

from rich.console import Console
from rich.traceback import Traceback

console = Console()

def bad_func():
    # Arbitrary local data which is longer than default max length
    from string import ascii_letters
    _locals = {k: k * 150 for k in ascii_letters}

    try:
        1 / 0
    except Exception:
        tb = Traceback(
            show_locals=True,locals_max_length=None, locals_max_string=None,
        )
        console.print(tb)
bad_func()

This prints the following traceback:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ test.py:16 in bad_func                                            │
│                                                                                                  │
│    13# }                                                                                    │14_locals = {k: k * 150 for k in ascii_letters}                                          │
│    15try:                                                                                   │
│ ❱  16 │   │   1 / 0                                                                              │
│    17except Exception:                                                                      │
│    18 │   │   tb = Traceback(                                                                    │
│    19 │   │   │   show_locals=True,locals_max_length=None, locals_max_string=None                │
│                                                                                                  │
│ ╭─────────────────────────────────────────── locals ───────────────────────────────────────────╮ │
│ │       _locals = {                                                                            │ │
│ │                 │   'a':                                                                     │ │
│ │                 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa… │ │
│ │                 │   'b':                                                                     │ │
│ │                 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb… │ │
│ │                 │   'c':                                                                     │ │
│ │                 'cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc… │ │
│ │                 │   'd':                                                                     │ │
│ │                 'dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd… │ │
│ │                 │   'e':                                                                     │ │
│ │                 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee… │ │
│ │                 │   'f':                                                                     │ │
│ │                 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff… │ │
│ │                 │   'g':                                                                     │ │
│ │                 'gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg… │ │
│ │                 │   'h':                                                                     │ │
│ │                 'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh… │ │
│ │                 │   'i':                                                                     │ │
│ │                 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii… │ │
│ │                 │   'j':                                                                     │ │
│ │                 'jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj… │ │
│ │                 │   ... +42                                                                  │ │
│ │                 }                                                                            │ │
│ │ ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'                       │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────╯ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
ZeroDivisionError: division by zero

Which, according to the docs, setting either limit to None should disable truncation and remove the ... and ... + 42.

The problem is not in the rich.pretty.traverse function, which implements this correctly, as can be seen by calling patching the function to hard-code in a non-default limit, which is then properly applied.

Platform

Click to expand

What platform (Win/Linux/Mac) are you running on? What terminal software are you using?

Problem is present on Linux and Mac, rich==13.7.1

───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=137 ColorSystem.EIGHT_BIT>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = '256'                                                         │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 55                                                            │
│    is_alt_screen = False                                                         │
│ is_dumb_terminal = False                                                         │
│   is_interactive = True                                                          │
│       is_jupyter = False                                                         │
│      is_terminal = True                                                          │
│   legacy_windows = False                                                         │
│         no_color = False                                                         │
│          options = ConsoleOptions(                                               │
│                        size=ConsoleDimensions(width=137, height=55),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=137,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=55,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=137, height=55)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 137                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭──────── Environment Variables ────────╮
│ {                                     │
│     'TERM': 'xterm-256color',         │
│     'COLORTERM': None,                │
│     'CLICOLOR': None,                 │
│     'NO_COLOR': None,                 │
│     'TERM_PROGRAM': 'Apple_Terminal', │
│     'COLUMNS': None,                  │
│     'LINES': None,                    │
│     'JUPYTER_COLUMNS': None,          │
│     'JUPYTER_LINES': None,            │
│     'JPY_PARENT_PID': None,           │
│     'VSCODE_VERBOSE_LOGGING': None    │
│ }                                     │
╰───────────────────────────────────────╯
platform="Darwin"
rich==13.7.1
Copy link

github-actions bot commented Mar 9, 2024

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

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