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

Add enable_link_path option to tracebacks #3338

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- Add enable_link_path for traceback.link and traceback.Traceback similar to logging.RichHandler https://github.com/Textualize/rich/pull/3338

## [13.7.1] - 2024-02-28

### Fixed
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ The following people have contributed to the development of Rich:
- [Pierro](https://github.com/xpierroz)
- [Bernhard Wagner](https://github.com/bwagner)
- [Aaron Beaudoin](https://github.com/AaronBeaudoin)
- [Benedikt Volkmer](https://github.com/bvolkmer)
35 changes: 33 additions & 2 deletions rich/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def install(
indent_guides: bool = True,
suppress: Iterable[Union[str, ModuleType]] = (),
max_frames: int = 100,
enable_link_path: bool = True,
) -> Callable[[Type[BaseException], BaseException, Optional[TracebackType]], Any]:
"""Install a rich traceback handler.

Expand All @@ -81,6 +82,7 @@ def install(
locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False.
indent_guides (bool, optional): Enable indent guides in code and locals. Defaults to True.
suppress (Sequence[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback.
enable_link_path (bool): Enable terminal link of paths in traceback. Defaults to True.

Returns:
Callable: The previous exception handler that was replaced.
Expand Down Expand Up @@ -116,6 +118,7 @@ def excepthook(
indent_guides=indent_guides,
suppress=suppress,
max_frames=max_frames,
enable_link_path=enable_link_path,
)
)

Expand Down Expand Up @@ -227,6 +230,7 @@ class Traceback:
locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False.
suppress (Sequence[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback.
max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100.
enable_link_path (bool): Enable terminal link of paths in traceback. Defaults to True.

"""

Expand Down Expand Up @@ -254,6 +258,7 @@ def __init__(
indent_guides: bool = True,
suppress: Iterable[Union[str, ModuleType]] = (),
max_frames: int = 100,
enable_link_path: bool = True,
):
if trace is None:
exc_type, exc_value, traceback = sys.exc_info()
Expand All @@ -275,6 +280,7 @@ def __init__(
self.locals_max_string = locals_max_string
self.locals_hide_dunder = locals_hide_dunder
self.locals_hide_sunder = locals_hide_sunder
self.enable_link_path = enable_link_path

self.suppress: Sequence[str] = []
for suppress_entity in suppress:
Expand Down Expand Up @@ -308,6 +314,7 @@ def from_exception(
indent_guides: bool = True,
suppress: Iterable[Union[str, ModuleType]] = (),
max_frames: int = 100,
enable_link_path: bool = True,
) -> "Traceback":
"""Create a traceback from exception info

Expand All @@ -328,6 +335,7 @@ def from_exception(
locals_hide_sunder (bool, optional): Hide locals prefixed with single underscore. Defaults to False.
suppress (Iterable[Union[str, ModuleType]]): Optional sequence of modules or paths to exclude from traceback.
max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100.
enable_link_path (bool): Enable terminal link of paths in traceback. Defaults to True.

Returns:
Traceback: A Traceback instance that may be printed.
Expand Down Expand Up @@ -357,6 +365,7 @@ def from_exception(
locals_hide_sunder=locals_hide_sunder,
suppress=suppress,
max_frames=max_frames,
enable_link_path=enable_link_path,
)

@classmethod
Expand Down Expand Up @@ -655,9 +664,31 @@ def render_locals(frame: Frame) -> Iterable[ConsoleRenderable]:

if os.path.exists(frame.filename):
text = Text.assemble(
path_highlighter(Text(frame.filename, style="pygments.string")),
path_highlighter(
Text(
frame.filename,
style=self.theme.get_style_for_token(String)
+ Style(
link=(
f"file://{frame.filename}"
if self.enable_link_path
else None
)
),
)
),
(":", "pygments.text"),
(str(frame.lineno), "pygments.number"),
Text(
str(frame.lineno),
style=self.theme.get_style_for_token(Number)
+ Style(
link=(
f"file://{frame_filename}#{frame.lineno}"
if self.enable_link_path
else None
)
),
),
" in ",
(frame.name, "pygments.function"),
style="pygments.text",
Expand Down