Textualize / rich

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

[BUG] Overwriting `locals_max_length` and `locals_max_string` does not affect printed Traceback #3301

Open CollinHeist opened 6 months ago

CollinHeist commented 6 months ago

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}                                          │
│    15 │   try:                                                                                   │
│ ❱  16 │   │   1 / 0                                                                              │
│    17 │   except 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` ``` ───────────────────────── ─────────────────────────╮ │ A high level console interface. │ │ │ │ ╭──────────────────────────────────────────────────────────────────────────────╮ │ │ │ │ │ │ ╰──────────────────────────────────────────────────────────────────────────────╯ │ │ │ │ color_system = '256' │ │ encoding = 'utf-8' │ │ file = <_io.TextIOWrapper name='' 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 │ ╰──────────────────────────────────────────────────────────────────────────────────╯ ╭─── ────╮ │ 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 ```
github-actions[bot] commented 6 months ago

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