Skip to content

Commit cbe650c

Browse files
committed
Handle eval/exec variables better
1 parent 8dad341 commit cbe650c

2 files changed

Lines changed: 44 additions & 23 deletions

File tree

trepan/lib/stack.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def format_return_and_location(
262262
elif s == "?()":
263263
if (func_name := is_eval_or_exec_stmt(frame)):
264264
s = f"in {func_name}"
265-
exec_str = get_exec_string(frame.f_back)
265+
exec_str = get_exec_or_eval_string(frame.f_back)
266266
if exec_str is not None:
267267
filename = exec_str
268268
add_quotes_around_file = False
@@ -350,7 +350,7 @@ def frame2filesize(frame):
350350
return None, None
351351

352352

353-
def get_exec_string(frame: FrameType) -> Optional[str]:
353+
def get_exec_or_eval_string(frame: FrameType) -> Optional[str]:
354354
if (call_frame := frame.f_back) is not None:
355355
offset = call_frame.f_lasti - 2
356356
code = call_frame.f_code
@@ -362,6 +362,9 @@ def get_exec_string(frame: FrameType) -> Optional[str]:
362362
pass
363363
elif inst.opname == "LOAD_CONST":
364364
return inst.argval
365+
elif inst.opname == "LOAD_NAME":
366+
arg_name = call_frame.f_code.co_names[inst.argval]
367+
return call_frame.f_locals[arg_name]
365368
else:
366369
break
367370
offset -= 2
@@ -627,9 +630,7 @@ def __init__(self):
627630
style="tango",
628631
)
629632
)
630-
import sys
631633

632-
sys.exit(0)
633634
# print("frame count: %d" % count_frames(frame))
634635
# print("frame count: %d" % count_frames(frame.f_back))
635636
# print("frame count: %d" % count_frames(frame, 1))
@@ -645,9 +646,11 @@ def fn(x):
645646
eval_str = is_eval_or_exec_stmt(frame.f_back)
646647
if eval_str:
647648
print(f"Caller is {eval_str} stmt")
649+
eval_exec_arg = get_exec_or_eval_string(frame.f_back)
650+
print(f"{eval_str} argument is: {eval_exec_arg}")
648651
print(
649652
format_stack_entry(
650-
dd, (frame.f_back, frame.f_back.f_code.co_firstlineno)
653+
dd, (frame.f_back, frame.f_back.f_code.co_firstlineno, -1)
651654
)
652655
)
653656

@@ -658,12 +661,19 @@ def fn(x):
658661

659662
print("=" * 30)
660663
fn(5)
664+
print("=" * 30)
661665
eval("fn(5)")
666+
arg_str = "fn(5)"
667+
eval(arg_str)
668+
print("+" * 30)
662669
exec("fn(5)")
663-
# print("=" * 30)
664-
# print(print_obj("fn", fn))
665-
# print("=" * 30)
666-
# print(print_obj("len", len))
667-
# print("=" * 30)
668-
# print(print_obj("MockDebugger", MockDebugger))
670+
exec(arg_str)
671+
672+
print("=" * 30)
673+
print(print_obj("fn", fn))
674+
print("=" * 30)
675+
print(print_obj("len", len))
676+
print("=" * 30)
677+
print(print_obj("MockDebugger", MockDebugger))
678+
669679
pass

trepan/processor/print.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
import pyficache
2727

2828
from trepan.lib.format import Filename, Hex, LineNumber, Symbol, format_token # Opcode,
29-
from trepan.lib.stack import check_path_with_frame, frame2file, get_exec_string, is_eval_or_exec_stmt
29+
from trepan.lib.stack import (
30+
check_path_with_frame,
31+
frame2file,
32+
get_exec_or_eval_string,
33+
is_eval_or_exec_stmt,
34+
)
3035
from trepan.processor import cmdfns
3136
from trepan.processor.cmdfns import deparse_fn
3237

@@ -108,7 +113,7 @@ def print_source_location_info(
108113
L -- 2 import sys,os
109114
(trepan3k)
110115
"""
111-
col_str = f":{column_number}" if column_number >= 0 else ""
116+
col_str = f":{column_number}" if column_number >= 0 else ""
112117
if remapped_file and filename != remapped_file:
113118
mess = f"({remapped_file}:{line_number}{col_str} remapped {filename}"
114119
else:
@@ -202,7 +207,9 @@ def prefix_for_source_text(source_text: str, maxwidth: int) -> str:
202207
tempdir=proc_obj.settings("tempdir"),
203208
)
204209
pyficache.remap_file(filename, remapped_file)
205-
filename, line_number = pyficache.unmap_file_line(filename, line_number)
210+
filename, line_number = pyficache.unmap_file_line(
211+
filename, line_number
212+
)
206213
pass
207214
pass
208215

@@ -230,12 +237,12 @@ def prefix_for_source_text(source_text: str, maxwidth: int) -> str:
230237
# else:
231238
# print("Can't deparse", frame.f_code)
232239
if source_text is None and eval_kind:
233-
if (source_text := get_exec_string(frame)):
234-
filename = "string-" + prefix_for_filename(source_text) + "-"
235-
else:
236-
source_text = f"{eval_kind}(...)"
237-
pass
238-
pass
240+
if source_text := get_exec_or_eval_string(frame):
241+
filename = "string-" + prefix_for_filename(source_text) + "-"
242+
else:
243+
source_text = f"{eval_kind}(...)"
244+
pass
245+
pass
239246
pass
240247
pass
241248
else:
@@ -320,7 +327,9 @@ def prefix_for_source_text(source_text: str, maxwidth: int) -> str:
320327
pyficache.remap_file(remapped_file, filename)
321328
fd.close()
322329
if source_text:
323-
intf_obj.msg(f"remapped string {prefix_for_source_text(source_text, 10)} to file {remapped_file}")
330+
intf_obj.msg(
331+
f"remapped string {prefix_for_source_text(source_text, 10)} to file {remapped_file}"
332+
)
324333
else:
325334
intf_obj.msg(f"remapped file {filename} to {remapped_file}")
326335
proc_obj.list_filename = remapped_file
@@ -424,10 +433,12 @@ def five():
424433
cmdproc.curframe = cmdproc.stack[cmdproc.curindex][0]
425434
print_location(cmdproc)
426435

427-
exec("""
436+
exec(
437+
"""
428438
cmdproc.frame = currentframe()
429439
cmdproc.setup()
430440
print_location(cmdproc)
431-
""")
441+
"""
442+
)
432443

433444
exec("five()")

0 commit comments

Comments
 (0)