Skip to content

Commit

Permalink
Traceback: Expose local rendering options in install
Browse files Browse the repository at this point in the history
Add locals_max_depth and locals_overflow to tracebacks
  • Loading branch information
IoannisP-ITENG committed Jul 6, 2024
1 parent a060eed commit c076b05
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion rich/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .text import Text, TextType

if TYPE_CHECKING:
from .console import ConsoleRenderable
from .console import ConsoleRenderable, OverflowMethod


def render_scope(
Expand All @@ -19,6 +19,8 @@ def render_scope(
indent_guides: bool = False,
max_length: Optional[int] = None,
max_string: Optional[int] = None,
max_depth: Optional[int] = None,
overflow: Optional["OverflowMethod"] = None,
) -> "ConsoleRenderable":
"""Render python variables in a given scope.
Expand Down Expand Up @@ -57,6 +59,8 @@ def sort_items(item: Tuple[str, Any]) -> Tuple[bool, str]:
indent_guides=indent_guides,
max_length=max_length,
max_string=max_string,
max_depth=max_depth,
overflow=overflow,
),
)
return Panel.fit(
Expand Down
26 changes: 25 additions & 1 deletion rich/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
from . import pretty
from ._loop import loop_last
from .columns import Columns
from .console import Console, ConsoleOptions, ConsoleRenderable, RenderResult, group
from .console import (
Console,
ConsoleOptions,
ConsoleRenderable,
OverflowMethod,
RenderResult,
group,
)
from .constrain import Constrain
from .highlighter import RegexHighlighter, ReprHighlighter
from .panel import Panel
Expand All @@ -53,8 +60,10 @@ def install(
show_locals: bool = False,
locals_max_length: int = LOCALS_MAX_LENGTH,
locals_max_string: int = LOCALS_MAX_STRING,
locals_max_depth: Optional[int] = None,
locals_hide_dunder: bool = True,
locals_hide_sunder: Optional[bool] = None,
locals_overflow: Optional[OverflowMethod] = None,
indent_guides: bool = True,
suppress: Iterable[Union[str, ModuleType]] = (),
max_frames: int = 100,
Expand Down Expand Up @@ -111,8 +120,10 @@ def excepthook(
show_locals=show_locals,
locals_max_length=locals_max_length,
locals_max_string=locals_max_string,
locals_max_depth=locals_max_depth,
locals_hide_dunder=locals_hide_dunder,
locals_hide_sunder=bool(locals_hide_sunder),
locals_overflow=locals_overflow,
indent_guides=indent_guides,
suppress=suppress,
max_frames=max_frames,
Expand Down Expand Up @@ -251,8 +262,10 @@ def __init__(
show_locals: bool = False,
locals_max_length: int = LOCALS_MAX_LENGTH,
locals_max_string: int = LOCALS_MAX_STRING,
locals_max_depth: Optional[int] = None,
locals_hide_dunder: bool = True,
locals_hide_sunder: bool = False,
locals_overlow: Optional[OverflowMethod] = None,
indent_guides: bool = True,
suppress: Iterable[Union[str, ModuleType]] = (),
max_frames: int = 100,
Expand All @@ -276,8 +289,10 @@ def __init__(
self.indent_guides = indent_guides
self.locals_max_length = locals_max_length
self.locals_max_string = locals_max_string
self.locals_max_depth = locals_max_depth
self.locals_hide_dunder = locals_hide_dunder
self.locals_hide_sunder = locals_hide_sunder
self.locals_overflow = locals_overlow

self.suppress: Sequence[str] = []
for suppress_entity in suppress:
Expand Down Expand Up @@ -307,8 +322,10 @@ def from_exception(
show_locals: bool = False,
locals_max_length: int = LOCALS_MAX_LENGTH,
locals_max_string: int = LOCALS_MAX_STRING,
locals_max_depth: Optional[int] = None,
locals_hide_dunder: bool = True,
locals_hide_sunder: bool = False,
locals_overflow: Optional[OverflowMethod] = None,
indent_guides: bool = True,
suppress: Iterable[Union[str, ModuleType]] = (),
max_frames: int = 100,
Expand Down Expand Up @@ -344,6 +361,7 @@ def from_exception(
show_locals=show_locals,
locals_max_length=locals_max_length,
locals_max_string=locals_max_string,
locals_max_depth=locals_max_depth,
locals_hide_dunder=locals_hide_dunder,
locals_hide_sunder=locals_hide_sunder,
)
Expand All @@ -359,8 +377,10 @@ def from_exception(
indent_guides=indent_guides,
locals_max_length=locals_max_length,
locals_max_string=locals_max_string,
locals_max_depth=locals_max_depth,
locals_hide_dunder=locals_hide_dunder,
locals_hide_sunder=locals_hide_sunder,
locals_overlow=locals_overflow,
suppress=suppress,
max_frames=max_frames,
)
Expand All @@ -375,6 +395,7 @@ def extract(
show_locals: bool = False,
locals_max_length: int = LOCALS_MAX_LENGTH,
locals_max_string: int = LOCALS_MAX_STRING,
locals_max_depth: Optional[int] = None,
locals_hide_dunder: bool = True,
locals_hide_sunder: bool = False,
) -> Trace:
Expand Down Expand Up @@ -457,6 +478,7 @@ def get_locals(
value,
max_length=locals_max_length,
max_string=locals_max_string,
max_depth=locals_max_depth,
)
for key, value in get_locals(frame_summary.f_locals.items())
}
Expand Down Expand Up @@ -631,6 +653,8 @@ def render_locals(frame: Frame) -> Iterable[ConsoleRenderable]:
indent_guides=self.indent_guides,
max_length=self.locals_max_length,
max_string=self.locals_max_string,
max_depth=self.locals_max_depth,
overflow=self.locals_overflow,
)

exclude_frames: Optional[range] = None
Expand Down

0 comments on commit c076b05

Please sign in to comment.