Skip to content

Commit

Permalink
make the filename in a traceback a terminal hyperlink
Browse files Browse the repository at this point in the history
  • Loading branch information
cfbolz committed May 17, 2024
1 parent b683994 commit 167ac55
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ def _get_code_position(code, instruction_index):

_RECURSIVE_CUTOFF = 3 # Also hardcoded in traceback.c.

def _hyperlink(url, text):
if text is None:
text = url
# OSC 8 ; params ; URI ST <name> OSC 8 ;; ST
return f'\033]8;;{url}\033\\{text}\033]8;;\033\\'

class StackSummary(list):
"""A list of FrameSummary objects, representing a stack of frames."""
Expand Down Expand Up @@ -522,9 +527,10 @@ def format_frame_summary(self, frame_summary, **kwargs):
if frame_summary.filename.startswith("<stdin>-"):
filename = "<stdin>"
if colorize:
import socket
row.append(' File {}"{}"{}, line {}{}{}, in {}{}{}\n'.format(
ANSIColors.MAGENTA,
filename,
_hyperlink(f"file://{socket.gethostname()}{filename}", filename),
ANSIColors.RESET,
ANSIColors.MAGENTA,
frame_summary.lineno,
Expand Down

0 comments on commit 167ac55

Please sign in to comment.