Skip to content

Commit 3403949

Browse files
committed
Add functions for formatting code and frame
1 parent b107f9a commit 3403949

4 files changed

Lines changed: 57 additions & 5 deletions

File tree

trepan/lib/format.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from pygments.formatter import Formatter
2828
from pygments.formatters import Terminal256Formatter, TerminalFormatter
2929
from pygments.formatters.terminal import TERMINAL_COLORS
30-
from pygments.lexers import RstLexer
30+
from pygments.lexers import PythonLexer, RstLexer
3131
from pygments.token import (
3232
Comment,
3333
Generic,
@@ -397,8 +397,18 @@ def format_unencoded(self, tokensource, outfile):
397397
rst_lex.add_filter(rst_filt)
398398
color_tf = RSTTerminalFormatter(colorscheme=color_scheme)
399399
mono_tf = MonoRSTTerminalFormatter()
400+
python_lexer = PythonLexer()
400401

401402

403+
def format_python(python_str: str, style) -> str:
404+
"""Add terminial formatting for a Python string
405+
``python_str``, using pygments style ``style``.
406+
"""
407+
if style is None:
408+
return python_str
409+
terminal_formatter = Terminal256Formatter(style=style)
410+
return highlight(python_str, python_lexer, terminal_formatter)
411+
402412
def rst_text(text, mono, width=80):
403413
if mono:
404414
tf = mono_tf

trepan/lib/stack.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
Function,
3838
LineNumber,
3939
Return,
40+
format_python,
4041
format_token,
4142
)
4243
from trepan.lib.pp import pp
@@ -156,14 +157,16 @@ def format_function_and_parameters(frame, debugger, style: str) -> Tuple[bool, s
156157
is_module = False
157158
try:
158159
params = inspect.formatargvalues(args, varargs, varkw, local_vars)
160+
formatted_params = format_python(params, style=style)
159161
except Exception:
160162
pass
161163
else:
162164
maxargstrsize = debugger.settings["maxargstrsize"]
163165
if len(params) >= maxargstrsize:
164166
params = f"{params[0:maxargstrsize]}...)"
167+
formatted_params = format_python(params, style=style)
165168
pass
166-
s += params
169+
s += formatted_params
167170
pass
168171

169172
return is_module, s

trepan/processor/command/info_subcmd/frame.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from trepan.lib.complete import complete_token
2222
from trepan.lib.stack import format_function_name
2323
from trepan.processor import frame as Mframe
24+
from trepan.processor.print import format_code, format_frame
2425

2526
# Our local modules
2627
from trepan.processor.command import base_subcmd as Mbase_subcmd
@@ -137,12 +138,12 @@ def run(self, args):
137138
if line_text is None:
138139
self.msg(f" current line number: {frame.f_lineno}")
139140
else:
140-
formatted_text = highlight_string(line_text.strip())
141+
formatted_text = highlight_string(line_text.strip(), style=style)
141142
self.msg(f" current line number: {frame.f_lineno}: {formatted_text}")
142143

143144
self.msg(f" last instruction: {frame.f_lasti}")
144-
self.msg(f" code: {code}")
145-
self.msg(f" previous frame: {frame.f_back}")
145+
self.msg(f" code: {format_code(code, style)}")
146+
self.msg(f" previous frame: {format_frame(frame.f_back, style)}")
146147
self.msg(f" tracing function: {frame.f_trace}")
147148

148149
if is_verbose:

trepan/processor/print.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,22 @@
2121
import sys
2222
from inspect import ismodule
2323
from tempfile import NamedTemporaryFile
24+
from types import CodeType
2425

2526
import pyficache
2627

2728
from trepan.lib.stack import check_path_with_frame, frame2file
2829
from trepan.processor import cmdfns
2930
from trepan.processor.cmdfns import deparse_fn
3031

32+
from trepan.lib.format import ( # Opcode,
33+
Filename,
34+
Hex,
35+
LineNumber,
36+
Symbol,
37+
format_token,
38+
)
39+
3140
try:
3241
from trepan.lib.deparse import deparse_and_cache
3342

@@ -39,6 +48,35 @@
3948
warned_file_mismatches = set()
4049

4150

51+
def format_code(code_object: CodeType, style) -> str:
52+
"""
53+
Format according to "style" a Python code object. The
54+
formatted string is returned.
55+
"""
56+
formatted_line = format_token(LineNumber, str(code_object.co_firstlineno), style=style)
57+
formatted_id = format_token(Hex, hex(id(code_object)), style=style)
58+
formatted_name = format_token(Symbol, code_object.co_name, style=style)
59+
formatted_filename = format_token(Filename, code_object.co_filename, style=style)
60+
return (
61+
f"<code object {formatted_name} at {formatted_id} "
62+
f"file {formatted_filename}, line {formatted_line}"
63+
)
64+
65+
66+
def format_frame(frame_object, style) -> str:
67+
"""
68+
Format according to "style" a Python frame object. The
69+
formatted string is returned.
70+
"""
71+
formatted_line = format_token(LineNumber, str(frame_object.f_lineno), style=style)
72+
formatted_id = format_token(Hex, hex(id(frame_object)), style=style)
73+
formatted_filename = format_token(Filename, frame_object.f_code.co_filename, style=style)
74+
return (
75+
f"<frame at {formatted_id} "
76+
f"file {formatted_filename}, line {formatted_line}"
77+
)
78+
79+
4280
def print_source_line(msg, lineno, line, event_str=None):
4381
"""Print out a source line of text , e.g. the second
4482
line in:

0 commit comments

Comments
 (0)