Skip to content

Commit 3ad403e

Browse files
committed
Better eval string printing
1 parent 1e9b4f0 commit 3ad403e

3 files changed

Lines changed: 36 additions & 23 deletions

File tree

trepan/lib/format.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def format_token(token_type, token_value: str, style: Optional[str]) -> str:
102102
Decorate ``token_value`` with coloring matching `token_type` and return
103103
the resulting string.
104104
"""
105-
if style == "none" or style is None:
105+
if style is None or style == "none":
106106
return token_value
107107
terminal_256_formatter = Terminal256Formatter(style=style)
108108
return format([[token_type, token_value]], terminal_256_formatter)
@@ -403,11 +403,11 @@ def format_unencoded(self, tokensource, outfile):
403403
python_lexer = PythonLexer()
404404

405405

406-
def format_python(python_str: str, style) -> str:
406+
def format_python(python_str: str, style: Optional[str]) -> str:
407407
"""Add terminial formatting for a Python string
408408
``python_str``, using pygments style ``style``.
409409
"""
410-
if style is None:
410+
if style is None or style == "none":
411411
return python_str
412412
terminal_formatter = Terminal256Formatter(style=style)
413413
return highlight(python_str, python_lexer, terminal_formatter)

trepan/lib/stack.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
import linecache
2323
import os
2424
import os.path as osp
25+
import pyficache
2526
import re
26-
from dataclasses import dataclass
27+
2728
from opcode import opname
2829
from reprlib import repr
2930
from types import CodeType, FrameType
@@ -67,8 +68,7 @@ def deparse_offset(_code, _name: str, _list_i: int, _) -> tuple:
6768

6869
opc = xdis.get_opcode_module(PYTHON_VERSION_TRIPLE, PYTHON_IMPLEMENTATION)
6970

70-
# A mapping frame to its ExtraFrameInfo. This is a weak dictionary so that
71-
# frames are automatically removed.
71+
# A mapping frame to its ExtraFrameInfo.
7272
FrameInfo: Dict[FrameType, int] = {}
7373

7474
def count_frames(frame: FrameType) -> int:
@@ -227,7 +227,12 @@ def format_function_and_parameters(
227227
else:
228228
is_module = False
229229
try:
230-
params = inspect.formatargvalues(args, varargs, varkw, local_vars)
230+
if is_eval_or_exec_stmt(frame):
231+
# Nuke the function name
232+
s = ""
233+
params = get_exec_or_eval_string(frame)
234+
else:
235+
params = inspect.formatargvalues(args, varargs, varkw, local_vars)
231236
formatted_params = format_python(params, style=style)
232237
except Exception:
233238
pass
@@ -271,12 +276,14 @@ def format_return_and_location(
271276
if include_location:
272277
is_pseudo_file = _re_pseudo_file.match(filename)
273278
add_quotes_around_file = not is_pseudo_file
274-
if is_module:
275-
if filename == "<string>":
276-
if (func_name := is_eval_or_exec_stmt(frame)):
277-
s += f" in {func_name}"
278-
elif not is_eval_or_exec_stmt(frame) and not is_pseudo_file:
279-
s += " file"
279+
# FIXME: DRY
280+
if filename == "<string>":
281+
if (func_name := is_eval_or_exec_stmt(frame)):
282+
s += f" in {func_name}"
283+
if remapped_filename := pyficache.main.code2tempfile.get(frame.f_code):
284+
filename = remapped_filename
285+
elif not is_eval_or_exec_stmt(frame) and not is_pseudo_file:
286+
s += " file"
280287
elif s == "?()":
281288
if (func_name := is_eval_or_exec_stmt(frame)):
282289
s = f"in {func_name}"
@@ -677,6 +684,9 @@ def fn(x):
677684
dd, (frame.f_back, frame.f_back.f_code.co_firstlineno, -1)
678685
)
679686
)
687+
dd.core.processor.curframe = frame.f_back
688+
dd.core.processor.stack = [(dd.core.processor.curframe, 1, 0)]
689+
print_stack_entry(dd.core.processor, 0)
680690

681691
_, mess = format_function_and_parameters(frame, dd, style="tango")
682692
print(mess)

trepan/processor/print.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,23 +199,26 @@ def prefix_for_source_text(source_text: str, maxwidth: int) -> str:
199199
# break
200200

201201
filename = frame2file(core_obj, frame, canonic=False)
202+
# FIXME: DRY
202203
if "<string>" == filename:
203204
if remapped_file := pyficache.main.code2tempfile.get(frame.f_code):
204205
filename = remapped_file
205206
elif dbgr_obj.eval_string:
206207
remapped_file = filename
207208
filename = pyficache.unmap_file(filename)
209+
# FIXME: DRY
208210
if "<string>" == filename:
209-
remapped_file = cmdfns.source_tempfile_remap(
210-
"eval_string",
211-
dbgr_obj.eval_string,
212-
tempdir=proc_obj.settings("tempdir"),
213-
)
214-
# pyficache.remap_file(filename, remapped_file)
215-
pyficache.main.code2tempfile[frame.f_code] = filename
216-
filename, line_number = pyficache.unmap_file_line(
217-
remapped_file, line_number
218-
)
211+
if not (remapped_file := pyficache.main.code2tempfile.get(frame.f_code)):
212+
remapped_file = cmdfns.source_tempfile_remap(
213+
"eval_string",
214+
dbgr_obj.eval_string,
215+
tempdir=proc_obj.settings("tempdir"),
216+
)
217+
# pyficache.remap_file(filename, remapped_file)
218+
pyficache.main.code2tempfile[frame.f_code] = filename
219+
filename, line_number = pyficache.unmap_file_line(
220+
remapped_file, line_number
221+
)
219222
pass
220223
pass
221224

0 commit comments

Comments
 (0)