Skip to content

Commit d9c6e4c

Browse files
committed
Simplify FrameInfo
1 parent f56de41 commit d9c6e4c

2 files changed

Lines changed: 36 additions & 24 deletions

File tree

trepan/lib/core.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2008-2010, 2013-2016, 2020 2023-2025
3+
# Copyright (C) 2008-2010, 2013-2016, 2020 2023-2026
44
# Rocky Bernstein <rocky@gnu.org>
55
#
66
# This program is free software: you can redistribute it and/or modify
@@ -40,7 +40,7 @@
4040
from trepan.clifns import search_file
4141
from trepan.lib.breakpoint import BreakpointManager
4242
from trepan.lib.default import START_OPTS, STOP_OPTS
43-
from trepan.lib.stack import ExtraFrameInfo, FrameInfo, count_frames
43+
from trepan.lib.stack import FrameInfo, count_frames
4444
from trepan.misc import option_set
4545
from trepan.processor.cmdproc import CommandProcessor
4646
from trepan.processor.trace import PrintProcessor
@@ -422,14 +422,18 @@ def trace_dispatch(self, frame, event: str, arg):
422422
processor-specific I think it best to distribute the checks."""
423423

424424
# for debugging
425-
# print("XXX+ trace dispatch:", frame.f_code.co_name, frame.f_lineno, event, arg)
425+
# print("XXX+ trace dispatch:", frame.f_code.co_name, frame.f_lineno, event, arg, frame.f_trace)
426426

427427
# Check to see if are in a call but we should be stepping over the call
428428
# using "next" of "finish". If so, then we can speed tracing by
429429
# removing further tracing. Not though that we *also* must make sure
430430
# that we don't have any breakpoint set, since we have to check
431431
# for breakpoints in a kind of slow way of checking all events.
432432

433+
remove_frame_on_return = False
434+
# FIXME:
435+
# instead of self.bpmgr.bplist == 0, we should be counting on
436+
# per breakpoints in code object.
433437
if event == "call":
434438
if (self.last_frame != frame
435439
and len(self.bpmgr.bplist) == 0
@@ -439,20 +443,22 @@ def trace_dispatch(self, frame, event: str, arg):
439443
):
440444
# We are "finish"ing or "next"ing and should not be tracing into this call
441445
# or any other calls from this. Return None to not trace further.
442-
# print("""trace_dispatch: "finish"ing or "next"ing from call event""")
446+
# print("""XXX+ trace_dispatch: "finish"ing or "next"ing from call event""")
447+
frame.f_trace = None
443448
return None
444-
elif not self.is_stop_here(frame, event):
449+
450+
if frame not in FrameInfo:
451+
count_frames(frame)
452+
453+
if not self.is_stop_here(frame, event):
445454
# We might have a stop here as a result of a breakpoint set inside
446455
# this function. In this case we need to ignore this stop, but
447-
# make sure we don't turn off breapoints inside this function which
456+
# make sure we don't turn off breakpoints inside this function which
448457
# we do by returning "self".
449458
return self
450-
if frame not in FrameInfo:
451-
FrameInfo[frame] = ExtraFrameInfo(-1, frame.f_code.co_filename)
452-
elif event == "return":
453-
if frame in FrameInfo:
454-
print("XXX deleting {frame}")
455-
del FrameInfo[frame]
459+
460+
elif event == "return" and frame in FrameInfo:
461+
remove_frame_on_return = True
456462

457463
self.event = event
458464

@@ -465,10 +471,14 @@ def trace_dispatch(self, frame, event: str, arg):
465471
# f_lineno can only be set by a trace function
466472
if self.ignore_filter and self.ignore_filter.is_excluded(frame):
467473
# print("trace_dispatch: ignore_filter", self.ignore_filter, frame, frame.f_lineno, event, arg) # for debugging
474+
if remove_frame_on_return:
475+
del FrameInfo[frame]
468476
return self
469477

470478
if self.trace_hook_suspend:
471479
# print("XXX trace_dispatch: hook suspended")
480+
if remove_frame_on_return:
481+
del FrameInfo[frame]
472482
return None
473483

474484
# print("XXX+ trace dispatch", frame, frame.f_lineno, event, arg) # for debugging
@@ -483,6 +493,8 @@ def trace_dispatch(self, frame, event: str, arg):
483493
if self.until_condition:
484494
if not self.matches_condition(frame):
485495
# print(f"XXX trace until condition not met for {frame}") # for debugging
496+
if remove_frame_on_return:
497+
del FrameInfo[frame]
486498
return self
487499
pass
488500

@@ -494,6 +506,8 @@ def trace_dispatch(self, frame, event: str, arg):
494506
trace_event_set = self.debugger.settings["events"]
495507
if trace_event_set is None or self.event not in trace_event_set:
496508
# print(f"trace_dispatch: self.event not in {trace_event_set}")
509+
if remove_frame_on_return:
510+
del FrameInfo[frame]
497511
return self
498512

499513
# I think we *have* to run is_stop_here() before
@@ -514,7 +528,8 @@ def trace_dispatch(self, frame, event: str, arg):
514528
except Exception:
515529
pass
516530
pass
517-
531+
if remove_frame_on_return:
532+
del FrameInfo[frame]
518533
pass
519534

520535

trepan/lib/stack.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,9 @@ def deparse_offset(_code, _name: str, _list_i: int, _) -> tuple:
6767

6868
opc = xdis.get_opcode_module(PYTHON_VERSION_TRIPLE, PYTHON_IMPLEMENTATION)
6969

70-
@dataclass
71-
class ExtraFrameInfo:
72-
depth: int = -1
73-
filename: str = "" # Note frame filenames can be remapped.
74-
75-
7670
# A mapping frame to its ExtraFrameInfo. This is a weak dictionary so that
7771
# frames are automatically removed.
78-
FrameInfo: Dict[FrameType, ExtraFrameInfo] = {}
72+
FrameInfo: Dict[FrameType, int] = {}
7973

8074
def count_frames(frame: FrameType) -> int:
8175
"""Return a count of the number of frames"""
@@ -86,8 +80,8 @@ def count_frames(frame: FrameType) -> int:
8680
for _ in range(1000):
8781
if frame is None:
8882
break
89-
elif frame_info := FrameInfo.get(frame):
90-
depth = frame_info.depth
83+
elif depth_or_None := FrameInfo.get(frame):
84+
depth = depth_or_None
9185
count += depth
9286
break
9387
else:
@@ -100,7 +94,7 @@ def count_frames(frame: FrameType) -> int:
10094
# Populate or update FrameInfo
10195
while len(frames) > 0 and frame not in FrameInfo:
10296
frame = frames.pop()
103-
FrameInfo[frame] = ExtraFrameInfo(depth, frame.f_code.co_filename)
97+
FrameInfo[frame] = depth
10498
depth += 1
10599
return count
106100

@@ -345,6 +339,7 @@ def frame2file(core_obj, frame, canonic=True):
345339
else:
346340
filename = core_obj.filename(frame.f_code.co_filename)
347341

342+
print("WOOT")
348343
# if frame_info := FrameInfo.get(frame):
349344
# if canonic:
350345
# return core_obj.filename(frame_info.filename)
@@ -632,7 +627,8 @@ def __init__(self):
632627
pass
633628

634629
frame = inspect.currentframe()
635-
# print(frame2filesize(frame))
630+
print(frame2filesize(frame))
631+
636632
pyc_file = osp.join(
637633
osp.dirname(__file__), "__pycache__", osp.basename(__file__)[:-3] + ".pyc"
638634
)
@@ -649,6 +645,7 @@ def __init__(self):
649645
dd.core.processor.stack = [(my_frame, 100, 1)]
650646
dd.core.processor.curframe = my_frame
651647
print_stack_entry(dd.core.processor, 0, "fruity")
648+
print(frame2file(dd.core, frame))
652649

653650
print(
654651
format_stack_entry(

0 commit comments

Comments
 (0)